【问题标题】:Aggregate Top CPU Using Processes使用进程聚合顶级 CPU
【发布时间】:2023-01-12 10:42:06
【问题描述】:

当我运行 top -n 1 -d 2 | head -n 12; 时,它会根据需要返回按 %cpu desc 排序的某些进程的处理器使用情况,但我不相信结果会按应有的方式聚合。我想把这些结果放在一个文件中,比如

while true; do
    top -n 1 -d 2 | head -n 12;
done > top_cpu_users;

当我以交互方式运行 top -d 2; 时,我首先看到了一些结果,然后两秒钟后我看到了更新的结果,它们似乎是在过去两秒内聚合的。第一个结果似乎没有以相同的方式聚合。

如何在前两秒内每两秒汇总一次 top cpu 用户?

【问题讨论】:

    标签: bash file logging io-redirection htop


    【解决方案1】:

    最佳将始终捕获进程信息的第一次完整扫描以用作基线。它使用它来初始化实用程序的值数据库,用于以后的比较报告。这是呈现在屏幕上的第一份报告的基础。

    后续报告是指定评估间隔的真实度量。

    因此,您的代码 sn-p 永远不会提供您真正想要的东西。

    您需要跳过第一次扫描的结果,只使用后续报告,但唯一的方法是通过指定所需扫描次数从单个命令生成它们,然后解析生成的组合报告。

    为此,这里有一个建议的解决方案:

    #!/bin/bash
    
    output="top_cpu_users"
    rm -f ${output} ${output}.tmp
    
    snapshots=5
    interval=2
    process_count=6         ### Number of heavy hitter processes being monitored
    top_head=7          ### Number of header lines in top report
    lines=$(( ${process_count} + ${top_head} )) ### total lines saved from each report run
    
    echo -e "
     Collecting process snapshots every ${interval} seconds ..."
    top -b -n $(( ${snapshots} + 1 )) -d ${interval} > ${output}.tmp
    
    echo -e "
     Parsing snapshots ..."
    awk -v max="${lines}" 'BEGIN{
        doprint=0 ;
        first=1 ;
    }
    {
        if( $1 == "top" ){
            if( first == 1 ){
                first=0 ;
            }else{
                print NR | "cat >&2" ;
                print "" ;
                doprint=1 ;
                entry=0 ;
            } ;
        } ;
        if( doprint == 1 ){
            entry++ ;
            print $0 ;
            if( entry == max ){
                doprint=0 ;
            } ;
        } ;
    }' ${output}.tmp >${output}
    
    more ${output}
    

    该会话的输出将如下所示:

     Collecting process snapshots every 2 seconds ...
    
     Parsing snapshots ...
    266
    531
    796
    1061
    1326
    
    top - 20:14:02 up  8:37,  1 user,  load average: 0.15, 0.13, 0.15
    Tasks: 257 total,   1 running, 256 sleeping,   0 stopped,   0 zombie
    %Cpu(s):  0.5 us,  1.0 sy,  0.0 ni, 98.5 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
    MiB Mem :   3678.9 total,    157.6 free,   2753.7 used,    767.6 buff/cache
    MiB Swap:   2048.0 total,   1116.4 free,    931.6 used.    629.2 avail Mem 
    
        PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
      31773 root      20   0       0      0      0 I   1.5   0.0   0:09.08 kworker/0:3-events
      32254 ericthe+  20   0   14500   3876   3092 R   1.0   0.1   0:00.04 top
       1503 mysql     20   0 2387360  20664   2988 S   0.5   0.5   3:10.11 mysqld
       2250 ericthe+  20   0 1949412 130004  20272 S   0.5   3.5   0:46.16 caja
       3104 ericthe+  20   0 4837044 461944 127416 S   0.5  12.3  81:26.50 firefox
      29998 ericthe+  20   0 2636764 165632  54700 S   0.5   4.4   0:36.97 Isolated Web Co
    
    top - 20:14:04 up  8:37,  1 user,  load average: 0.14, 0.13, 0.15
    Tasks: 257 total,   1 running, 256 sleeping,   0 stopped,   0 zombie
    %Cpu(s):  1.5 us,  0.7 sy,  0.0 ni, 97.4 id,  0.4 wa,  0.0 hi,  0.0 si,  0.0 st
    MiB Mem :   3678.9 total,    157.5 free,   2753.7 used,    767.6 buff/cache
    MiB Swap:   2048.0 total,   1116.4 free,    931.6 used.    629.2 avail Mem 
    
        PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
       3104 ericthe+  20   0 4837044 462208 127416 S   3.0  12.3  81:26.56 firefox
       1503 mysql     20   0 2387360  20664   2988 S   1.0   0.5   3:10.13 mysqld
      32254 ericthe+  20   0   14500   3876   3092 R   1.0   0.1   0:00.06 top
       1489 root      20   0  546692  61584  48956 S   0.5   1.6  17:23.78 Xorg
       2233 ericthe+  20   0  303744  11036   7500 S   0.5   0.3   4:46.84 compton
       7239 ericthe+  20   0 2617520 127452  44768 S   0.5   3.4   1:41.13 Isolated Web Co
    
    top - 20:14:06 up  8:37,  1 user,  load average: 0.14, 0.13, 0.15
    Tasks: 257 total,   1 running, 256 sleeping,   0 stopped,   0 zombie
    %Cpu(s):  0.6 us,  0.4 sy,  0.0 ni, 99.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
    MiB Mem :   3678.9 total,    157.5 free,   2753.7 used,    767.6 buff/cache
    MiB Swap:   2048.0 total,   1116.4 free,    931.6 used.    629.2 avail Mem 
    
        PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
       1489 root      20   0  546700  61584  48956 S   1.5   1.6  17:23.81 Xorg
       3104 ericthe+  20   0 4837044 462208 127416 S   1.5  12.3  81:26.59 firefox
       1503 mysql     20   0 2387360  20664   2988 S   0.5   0.5   3:10.14 mysqld
       2233 ericthe+  20   0  303744  11036   7500 S   0.5   0.3   4:46.85 compton
       2478 ericthe+  20   0  346156  10368   8792 S   0.5   0.3   0:22.97 mate-cpufreq-ap
       2481 ericthe+  20   0  346540  11148   9168 S   0.5   0.3   0:41.73 mate-sensors-ap
    
    top - 20:14:08 up  8:37,  1 user,  load average: 0.14, 0.13, 0.15
    Tasks: 257 total,   1 running, 256 sleeping,   0 stopped,   0 zombie
    %Cpu(s):  0.6 us,  0.5 sy,  0.0 ni, 98.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
    MiB Mem :   3678.9 total,    157.5 free,   2753.6 used,    767.7 buff/cache
    MiB Swap:   2048.0 total,   1116.4 free,    931.6 used.    629.3 avail Mem 
    
        PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
      32254 ericthe+  20   0   14500   3876   3092 R   1.0   0.1   0:00.08 top
       3104 ericthe+  20   0 4837044 462208 127416 S   0.5  12.3  81:26.60 firefox
      18370 ericthe+  20   0 2682392  97268  45144 S   0.5   2.6   0:55.36 Isolated Web Co
      19436 ericthe+  20   0 2618496 123608  52540 S   0.5   3.3   1:55.08 Isolated Web Co
      26630 ericthe+  20   0 2690464 179020  56060 S   0.5   4.8   1:45.57 Isolated Web Co
      29998 ericthe+  20   0 2636764 165632  54700 S   0.5   4.4   0:36.98 Isolated Web Co
    
    top - 20:14:10 up  8:37,  1 user,  load average: 0.13, 0.13, 0.15
    Tasks: 257 total,   1 running, 256 sleeping,   0 stopped,   0 zombie
    %Cpu(s):  2.5 us,  0.9 sy,  0.0 ni, 96.6 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
    MiB Mem :   3678.9 total,    157.5 free,   2753.6 used,    767.7 buff/cache
    MiB Swap:   2048.0 total,   1116.4 free,    931.6 used.    629.3 avail Mem 
    
        PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
       3104 ericthe+  20   0 4837076 463000 127416 S   7.5  12.3  81:26.75 firefox
       1489 root      20   0  546716  61584  48956 S   1.5   1.6  17:23.84 Xorg
       1503 mysql     20   0 2387360  20664   2988 S   1.0   0.5   3:10.16 mysqld
      32254 ericthe+  20   0   14500   3876   3092 R   1.0   0.1   0:00.10 top
       2233 ericthe+  20   0  303744  11036   7500 S   0.5   0.3   4:46.86 compton
       2481 ericthe+  20   0  346540  11148   9168 S   0.5   0.3   0:41.74 mate-sensors-ap
    

    【讨论】:

      猜你喜欢
      • 2011-07-01
      • 1970-01-01
      • 2022-12-12
      • 1970-01-01
      • 2023-02-24
      • 1970-01-01
      • 1970-01-01
      • 2020-11-06
      • 2021-09-19
      相关资源
      最近更新 更多