I am putting together a cron job script that will pull top information and format it into a log that I can pull into Excel for readability (one row per execution - run every 10 minutes outputting to a log file). I am using the command
to generate the lines needed to get CPU usage per CPU. My test system is FreeBSD13 (vanilla install) in VirtualBox on Windows 10.
Any time I run one execution of top ("-d 1"), it always shows all CPUs at 99% idle. I am running a shell session that is just counting up in a variable from 0 to a billion. That should show CPU usage. If I run more than 1 execution of top, all executions after the initial execution show significant CPU usage. So, my one pass at top gives inaccurate CPU usage, but is accurate after that. Is this a config/caching issue? How can I get a single pass at top to give accurate CPU usage?
Here is one execution with just the first few processes shown - see WCPU on the process, yet CPU shows idle:
Now see 2 executions - note that the same process is still running with high cpu usage. First pass shows CPU idle, subsequent executions show real usage:
sh:
top -nPd 1
Any time I run one execution of top ("-d 1"), it always shows all CPUs at 99% idle. I am running a shell session that is just counting up in a variable from 0 to a billion. That should show CPU usage. If I run more than 1 execution of top, all executions after the initial execution show significant CPU usage. So, my one pass at top gives inaccurate CPU usage, but is accurate after that. Is this a config/caching issue? How can I get a single pass at top to give accurate CPU usage?
Here is one execution with just the first few processes shown - see WCPU on the process, yet CPU shows idle:
chris@FreeBSD13:~ $ top -nPd 1
top -nPd 1
last pid: 1763; load averages: 0.90, 0.58, 0.45 up 0+07:59:55 16:18:45
28 processes: 2 running, 26 sleeping
CPU 0: 0.2% user, 0.0% nice, 0.3% system, 0.1% interrupt, 99.3% idle
CPU 1: 0.3% user, 0.0% nice, 0.3% system, 0.1% interrupt, 99.3% idle
CPU 2: 0.2% user, 0.0% nice, 0.4% system, 0.1% interrupt, 99.3% idle
Mem: 8704K Active, 31M Inact, 117M Wired, 3775M Free
ARC: 29M Total, 8078K MFU, 19M MRU, 242K Header, 1433K Other
14M Compressed, 42M Uncompressed, 2.98:1 Ratio
Swap: 2048M Total, 2048M Free
PID USERNAME THR PRI NICE SIZE RES STATE C TIME WCPU COMMAND
1421 chris 1 101 0 13M 3364K CPU0 0 11:04 100.00% sh
494 root 1 20 0 11M 1564K select 0 0:14 0.00% devd
691 root 1 20 0 13M 2764K select 2 0:07 0.00% syslogd
Now see 2 executions - note that the same process is still running with high cpu usage. First pass shows CPU idle, subsequent executions show real usage:
chris@FreeBSD13:~ $ top -nPd 2
top -nPd 2
last pid: 1764; load averages: 0.90, 0.58, 0.45 up 0+07:59:58 16:18:48
28 processes: 2 running, 26 sleeping
CPU 0: 0.2% user, 0.0% nice, 0.3% system, 0.1% interrupt, 99.3% idle
CPU 1: 0.3% user, 0.0% nice, 0.3% system, 0.1% interrupt, 99.3% idle
CPU 2: 0.2% user, 0.0% nice, 0.4% system, 0.1% interrupt, 99.3% idle
Mem: 8768K Active, 31M Inact, 117M Wired, 3775M Free
ARC: 29M Total, 8078K MFU, 19M MRU, 242K Header, 1433K Other
14M Compressed, 42M Uncompressed, 2.98:1 Ratio
Swap: 2048M Total, 2048M Free
PID USERNAME THR PRI NICE SIZE RES STATE C TIME WCPU COMMAND
1421 chris 1 102 0 13M 3364K RUN 2 11:10 100.00% sh
494 root 1 20 0 11M 1564K select 2 0:14 0.00% devd
691 root 1 20 0 13M 2764K select 1 0:07 0.00% syslogd
1350 chris 1 20 0 21M 9984K select 1 0:03 0.00% sshd
<snip>
last pid: 1764; load averages: 1.07, 0.62, 0.47 up 0+08:00:01 16:18:51
28 processes: 2 running, 26 sleeping
CPU 0: 0.0% user, 0.0% nice, 0.4% system, 0.0% interrupt, 99.6% idle
CPU 1: 33.9% user, 0.0% nice, 1.2% system, 0.0% interrupt, 65.0% idle
CPU 2: 66.1% user, 0.0% nice, 0.0% system, 0.4% interrupt, 33.5% idle
Mem: 9012K Active, 31M Inact, 117M Wired, 3775M Free
ARC: 29M Total, 8078K MFU, 19M MRU, 242K Header, 1433K Other
14M Compressed, 42M Uncompressed, 2.98:1 Ratio
Swap: 2048M Total, 2048M Free
PID USERNAME THR PRI NICE SIZE RES STATE C TIME WCPU COMMAND
1421 chris 1 102 0 13M 3364K CPU0 0 11:14 203.36% sh
1764 chris 1 20 0 14M 3624K CPU1 1 0:00 0.30% top
1350 chris 1 20 0 21M 9984K select 1 0:03 0.19% sshd
494 root 1 20 0 11M 1564K select 0 0:14 0.03% devd
691 root 1 20 0 13M 2764K select 2 0:07 0.01% syslogd