Home > Software design >  Aggregate Top CPU Using Processes
Aggregate Top CPU Using Processes

Time:01-13

When I run top -n 1 -d 2 | head -n 12; it returns processor usage for some processes sorted by %cpu desc as desired, but I'm not convinced that the results are aggregated as they should be. I'm wanting to put these results in a file maybe like

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

When I run top -d 2; interactively, I first see some results, then two seconds later I see the results updated and they appear to be aggregated over the last two seconds. The first results do not appear to be aggregated in the same way.

How do I get top cpu users every two seconds aggregated over the previous two seconds?

CodePudding user response:

top will always capture a first full scan of process info for use as a baseline. It uses that to initialize the utility's database of values used for later comparative reporting. That is the basis of the first report presented to the screen.

The follow-on reports are the true measures for the specified evaluation intervals.

Your code snippet will therefore never provide what you are really looking for.

You need to skip the results from the first scan and only use the follow on reports, but the only way to do that is to generate them from a single command by specifying the count of scans desired, then parse the resulting combined report.

To that end, here is a proposed solution:

#!/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 "\n Collecting process snapshots every ${interval} seconds ..."
top -b -n $(( ${snapshots}   1 )) -d ${interval} > ${output}.tmp

echo -e "\n 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}

The session output for that will look like this:

 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
  • Related