b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

cpu使用率首先什么是字段字段的具体含义和含义

电脑杂谈  发布时间:2021-02-25 09:01:37  来源:网络整理

cpu使用率

首先,CPU使用率是多少?我总结了与cpu使用率有关的知识点。通常通过top命令查看该进程的cpu使用率。有时发现某个进程的CPU使用率超过100%。例如:

top - 10:12:55 up 3 days, 13:09,  1 user,  load average: 1.19, 0.87, 1.43
Tasks: 235 total,   1 running, 181 sleeping,   0 stopped,   1 zombie
%Cpu(s): 56.4 us,  2.6 sy,  0.0 ni, 40.5 id,  0.1 wa,  0.0 hi,  0.3 si,  0.0 st
KiB Mem :  7868576 total,  1022508 free,  2635576 used,  4210492 buff/cache
KiB Swap:        0 total,        0 free,        0 used.  4484128 avail Mem 
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                    
15736 root      20   0   31840   1564   1412 S 199.0  0.0   0:46.34 samples_test 

为什么这里的%CPU超过100%?即使cpu继续运行,最大值还是100%?

这时,我们需要了解该字段的具体含义:

cpu使用率1000_cpu使用率高_cpu使用100

$ man top
......
        1. %CPU  --  CPU Usage
           The tasks share of the elapsed CPU time since the last screen update, expressed as a percentage of total CPU time.
           In a true SMP environment, if a process is multi-threaded and top is not operating in Threads mode, amounts greater than 100% may be reported.  You toggle Threads mode with the `H inter‐
           active command.
           Also for multi-processor environments, if Irix mode is Off, top will operate in Solaris mode where a tasks cpu usage will be divided by the total number of CPUs.  You toggle Irix/Solaris
           modes with the `I interactive command.
......

从上面的解释中,我们可以知道%CPU代表从上次更新到现在所占用的CPU时间的百分比。在实际的SMP环境中,如果进程具有多个线程,并且top命令不程模式下,则可能导致结果大于100%。

这里的SMP(对称多处理)是一种对称多处理器结构,其特征是仅一个操作系统实例在多个CPU上运行,并且每个CPU具有相同的结构,共享内存和资源。

对于线程模式,因为top命令默认显示进程数据,所以如果使用top -H,则此选项将列出所有Linux线程。如果top已经在运行,则还可以通过按“ H”键将线程查看模式打开或关闭。

因此,此处的%CPU实际上显示了进程占用的内核百分比,而不是所有CPU的百分比。有时它大于100,因为该进程已启用多线程并占用了多个内核。

cpu使用100_cpu使用率1000_cpu使用率高

要查看进程中每个线程的CPU,请使用top -H,或在top运行后按H启动线程模式。

可以看到sample_test至少包含两个具有较高CPU使用率的线程,并且使用率接近100%,但不会超过100%。

top - 10:13:40 up 3 days, 13:10,  1 user,  load average: 1.88, 1.10, 1.49
Threads: 1018 total,   4 running, 961 sleeping,   0 stopped,   1 zombie
%Cpu(s): 56.8 us,  1.6 sy,  0.0 ni, 41.3 id,  0.3 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  7868576 total,  1009804 free,  2643240 used,  4215532 buff/cache
KiB Swap:        0 total,        0 free,        0 used.  4472252 avail Mem 
  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                                     
15737 root      20   0   31840   1564   1412 R 99.3  0.0   1:07.91 samples_test                                                                
15738 root      20   0   31840   1564   1412 R 98.0  0.0   1:07.83 samples_test

顶部-H -p pid号

您可以看到实际的samples_test总共有三个线程,其中两个线程的CPU使用率特别高,而一个线程的利用率特别低。

cpu使用率1000_cpu使用率高_cpu使用100

$ top -H -p 15736
top - 10:40:41 up 3 days, 13:37,  1 user,  load average: 2.65, 2.75, 2.44
Threads:   3 total,   2 running,   1 sleeping,   0 stopped,   0 zombie
%Cpu(s): 56.2 us,  2.2 sy,  0.0 ni, 41.6 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  7868576 total,   332464 free,  3167324 used,  4368788 buff/cache
KiB Swap:        0 total,        0 free,        0 used.  3846852 avail Mem 
   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                                     
 15737 root      20   0   31840   1680   1528 R 99.3  0.0  17:27.23 samples_test                                                                
 15738 root      20   0   31840   1680   1528 R 99.0  0.0  17:26.73 samples_test                                                                
 15736 root      20   0   31840   1680   1528 S  0.0  0.0   0:00.00 samples_test

您可以使用pstree命令快速查看进程中线程之间的关系:

可以看出,线程号为15736的线程是主线程,CPU使用率较低; 15737和15738线程是子线程,但CPU使用率很高。这与我们的代码一致。

samples_test测试代码:

#include 
#include 
void thread1() {
    while(true) {}
}
void thread2() {
    while(true) {
        //std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    }
}
int main() {
    std::thread th1(thread1);
    std::thread th2(thread2);
    th1.join();
    th2.join();
    return 0;
}


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shoujiruanjian/article-360666-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...