Cpu占用率检测以及 Cpu Burn

目录

Linux lscpu指令

概念-超线程

概念-sys高的理解:

system (sy)

user (us)

nice (ni)

idle (id)

iowait (wa)

Other statistics

Stress CPU

C++控制线程运行在哪个核:

C计算cpu的整体以及各个逻辑核的使用率:

Code实现

工具 - linux查看某个进程被分配的cpu 核:

工具 - Linux查看某个进程的所有线程:

工具-htop


Linux lscpu指令

该指令的输出项理解:

Socket物理插槽的个数;

Core(s) per socket:每个socket中的物理核的个数

Thread(s) per core:   

每个物理核所支持的线程数。如果支持hyper-threading,每个支持2个线程,相当于一个物理核上有两个逻辑核;

CPU(s):  逻辑核的总数;

NUMA node:

“NUMA node” represents the memory architecture; “NUMA” stands for “non-uniform memory architecture”. In your system, each socket is attached to certain DIMM slots, and each physical CPU package contains a memory controller which handles part of the total RAM. As a result, not all physical memory is equally accessible from all CPUs: one physical CPU can directly access the memory it controls, but has to go through the other physical CPU to access the rest of memory. In your system, logical cores 0–13 and 28–41 are in one NUMA node, the rest in the other. So yes, one NUMA node equals one socket, at least in typical multi-socket Xeon systems.

Non-uniform memory access (NUMA) is a computer memory design used in multiprocessing, where the memory access time depends on the memory location relative to the processor. Under NUMA, a processor can access its own local memory faster than non-local memory (memory local to another processor or memory shared between processors). The benefits of NUMA are limited to particular workloads, notably on servers where the data is often associated strongly with certain tasks or users.[1]

cpu - Understanding output of lscpu - Unix & Linux Stack Exchange

Other:

lscpu(1) - Linux manual page

Linux lscpu Command Tutorial for Beginners (5 Examples)

How many physical and logical CPU cores in your computer | Wei Bai 白巍

概念-超线程

https://en.wikipedia.org/wiki/Hyper-threading

Hyper-threading (officially called Hyper-Threading Technology or HT Technology and abbreviated as HTT or HT) is Intel's proprietary simultaneous multithreading (SMT) implementation used to improve parallelization of computations (doing multiple tasks at once) performed on x86 microprocessors. It was introduced on Xeon server processors in February 2002 and on Pentium 4 desktop processors in November 2002.[4] Since then, Intel has included this technology in ItaniumAtom, and Core 'i' Series CPUs, among others. [5]

For each processor core that is physically present, the operating system addresses two virtual (logical) cores and shares the workload between them when possible. The main function of hyper-threading is to increase the number of independent instructions in the pipeline; it takes advantage of superscalar architecture, in which multiple instructions operate on separate data in parallel. With HTT, one physical core appears as two processors to the operating system, allowing concurrent scheduling of two processes per core. In addition, two or more processes can use the same resources: If resources for one process are not available, then another process can continue if its resources are available.

In addition to requiring simultaneous multithreading support in the operating system, hyper-threading can be properly utilized only with an operating system specifically optimized for it.

概念-sys高的理解:

Understanding CPU statistics | AppSignal Blog

 

On a Linux machine, running top will print a CPU line that looks like this:

%Cpu(s): 13.2 us,  1.3 sy,  0.0 ni, 85.3 id,  0.0 wa,  0.0 hi,  0.2 si,  0.0 st

These eight states are "user" (us), "system", (sy), "nice" (ni), "idle" (id), "iowait" (wa), "hardware interrupt" (hi), "software interrupt" (si), and "steal" (st).

Of these eight, "system", "user" and "idle" are the main states the CPU can be in.

system (sy)

The "system" CPU state shows the amount of CPU time used by the kernel. The kernel is responsible for low-level tasks, like interacting with the hardware, memory allocation, communicating between OS processes, running device drivers and managing the file system. Even the CPU scheduler, which determines which process gets access to the CPU, is run by the kernel.

While usually low, the "system" category can spike when a lot of data is being read from or written to disk, for example. If it stays high for longer periods of time, you might have a problem with a device driver.

user (us)

One level up, the "user" CPU state shows CPU time used by user space processes. These are higher-level processes, like your application, or the database server running on your machine. In short, every CPU time used by anything else than the kernel is marked "user", even if it wasn't started from any user account. If a user-space process needs access to the hardware, it needs to ask the kernel, meaning that would count towards "system" state.

Usually, the "user" category uses most of your CPU time. If it stays close to the maximum without leaving much idle time for too long, you might have a problem with your application, or another utility running on the machine.

nice (ni)

The "nice" CPU state is a subset of the "user" state and shows the CPU time used by processes that have a positive niceness, meaning a lower priority than other tasks. The nice utility is used to start a program with a particular priority. The default niceness is 0, but can be set anywhere between -20 for the highest priority to 19 for the lowest. CPU time in the "nice" category marks lower-priority tasks that are run when the CPU has some time left to do extra tasks.

idle (id)

The "idle" CPU state shows the CPU time that's not actively being used. Internally, idle time is usually calculated by a task with the lowest possible priority (using a positive nice value).

iowait (wa)

"iowait" is a sub category of the "idle" state. It marks time spent waiting for input or output operations, like reading or writing to disk. When the processor waits for a file to be opened, for example, the time spend will be marked as "iowait".

Elevated CPU time in the "iowait" category can reveal problems outside of the processor. For example, when an in-memory database needs to flush a lot of data to disk, or when memory is swapped to disk.

Other statistics

Besides "nice" in "user" and "iowait" in idle, there are more subcategories the main CPU states can be divided in. The "hardware interrupt" (hi or irq) and "software interrupt" (si, or softirq) categories are time spent servicing interrupts, and the "steal" (st) subcategory marks time spent waiting for a virtual CPU in a virtual machine.

Stress CPU

C++控制线程运行在哪个核:

C++11 threads, affinity and hyperthreading - Eli Bendersky's website

C计算cpu的整体以及各个逻辑核的使用率:

Finding overall and per core CPU utilization – Phoxis

Code实现
 

工具 - linux查看某个进程被分配的cpu 核:

taskset -c -p  xxx, xxx也可为线程id

显示的是逻辑核,有超线程的话,一个物理核对应两个逻辑核。


Note: 如果该进程的主线程没有被制定在一个具体的核, 子线程都被指定过。该指令显示的是所有核。

工具 - Linux查看某个进程的所有线程:

ps -Tp pid

pstree -p 407057 (有层次结构,better)

工具-htop

可以查看各个cpu逻辑核的占用率

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

First Snowflakes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值