'top' and 'ps' always show zeros for CPU utilization, and CPU time, metrics
Building procps-ng 3.3.11 for use in OpenWrt Designated Driver w/ Linux 4.4 rc5 running on a Linksys WRT1900AC consumer router. However, the problem most likely has wider applicability than the specific use-case cited.
The top command always shows zero values in the CPU utilization summary (at the beginning of top output), and for the %CPU and TIME+ for each process. Below is a screen-grab demonstrating the incorrect output;
top - 13:28:37 up 14:46, 0 users, load average: 0.06, 0.07, 0.05
Tasks: 81 total, 1 running, 80 sleeping, 0 stopped, 0 zombie
%Cpu0 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 254720 total, 187632 free, 35508 used, 31580 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 181904 avail Mem
PID USER PR NI VIRT RES %CPU %MEM TIME+ S COMMAND
1 root 20 0 1364 720 0.0 0.3 0:00.00 S procd
2 root 20 0 0 0 0.0 0.0 0:00.00 S kthreadd
3 root 20 0 0 0 0.0 0.0 0:00.00 S ksoftirqd/0
4 root 20 0 0 0 0.0 0.0 0:00.00 S kworker/0:0
5 root 0 -20 0 0 0.0 0.0 0:00.00 S kworker/0:0H
6 root 20 0 0 0 0.0 0.0 0:00.00 S kworker/u4:0
7 root 20 0 0 0 0.0 0.0 0:00.00 S rcu_sched
8 root 20 0 0 0 0.0 0.0 0:00.00 S rcu_bh
9 root rt 0 0 0 0.0 0.0 0:00.00 S migration/0
10 root rt 0 0 0 0.0 0.0 0:00.00 S migration/1
11 root 20 0 0 0 0.0 0.0 0:00.00 S ksoftirqd/1
12 root 20 0 0 0 0.0 0.0 0:00.00 S kworker/1:0
13 root 0 -20 0 0 0.0 0.0 0:00.00 S kworker/1:0H
128 root 0 -20 0 0 0.0 0.0 0:00.00 S writeback
129 root 0 -20 0 0 0.0 0.0 0:00.00 S crypto
131 root 0 -20 0 0 0.0 0.0 0:00.00 S bioset
132 root 0 -20 0 0 0.0 0.0 0:00.00 S kblockd
148 root 20 0 0 0 0.0 0.0 0:00.00 S kworker/0:1
169 root 20 0 0 0 0.0 0.0 0:00.00 S kswapd0
170 root 0 -20 0 0 0.0 0.0 0:00.00 S vmstat
171 root 20 0 0 0 0.0 0.0 0:00.00 S fsnotify_mark
213 root -51 0 0 0 0.0 0.0 0:00.00 S irq/30-f10d0000
217 root 0 -20 0 0 0.0 0.0 0:00.00 S bioset
222 root 0 -20 0 0 0.0 0.0 0:00.00 S bioset
227 root 0 -20 0 0 0.0 0.0 0:00.00 S bioset
232 root 0 -20 0 0 0.0 0.0 0:00.00 S bioset
237 root 0 -20 0 0 0.0 0.0 0:00.00 S bioset
242 root 0 -20 0 0 0.0 0.0 0:00.00 S bioset
247 root 0 -20 0 0 0.0 0.0 0:00.00 S bioset
252 root 0 -20 0 0 0.0 0.0 0:00.00 S bioset
257 root 0 -20 0 0 0.0 0.0 0:00.00 S bioset
262 root 0 -20 0 0 0.0 0.0 0:00.00 S bioset
267 root 20 0 0 0 0.0 0.0 0:00.00 S spi0
272 root 0 -20 0 0 0.0 0.0 0:00.00 S bioset
The following screen-grab demonstrates the incorrect ps output;
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Dec19 ? 00:00:00 /sbin/procd
root 2 0 0 Dec19 ? 00:00:00 [kthreadd]
root 3 2 0 Dec19 ? 00:00:00 [ksoftirqd/0]
root 4 2 0 Dec19 ? 00:00:00 [kworker/0:0]
root 5 2 0 Dec19 ? 00:00:00 [kworker/0:0H]
root 7 2 0 Dec19 ? 00:00:00 [rcu_sched]
root 8 2 0 Dec19 ? 00:00:00 [rcu_bh]
root 9 2 0 Dec19 ? 00:00:00 [migration/0]
root 10 2 0 Dec19 ? 00:00:00 [migration/1]
root 11 2 0 Dec19 ? 00:00:00 [ksoftirqd/1]
root 12 2 0 Dec19 ? 00:00:00 [kworker/1:0]
root 13 2 0 Dec19 ? 00:00:00 [kworker/1:0H]
root 128 2 0 Dec19 ? 00:00:00 [writeback]
root 129 2 0 Dec19 ? 00:00:00 [crypto]
root 131 2 0 Dec19 ? 00:00:00 [bioset]
root 132 2 0 Dec19 ? 00:00:00 [kblockd]
root 148 2 0 Dec19 ? 00:00:00 [kworker/0:1]
root 169 2 0 Dec19 ? 00:00:00 [kswapd0]
root 170 2 0 Dec19 ? 00:00:00 [vmstat]
root 171 2 0 Dec19 ? 00:00:00 [fsnotify_mark]
root 213 2 0 Dec19 ? 00:00:00 [irq/30-f10d0000]
root 217 2 0 Dec19 ? 00:00:00 [bioset]
root 222 2 0 Dec19 ? 00:00:00 [bioset]
root 227 2 0 Dec19 ? 00:00:00 [bioset]
root 232 2 0 Dec19 ? 00:00:00 [bioset]
root 237 2 0 Dec19 ? 00:00:00 [bioset]
root 242 2 0 Dec19 ? 00:00:00 [bioset]
root 247 2 0 Dec19 ? 00:00:00 [bioset]
root 252 2 0 Dec19 ? 00:00:00 [bioset]
root 257 2 0 Dec19 ? 00:00:00 [bioset]
root 262 2 0 Dec19 ? 00:00:00 [bioset]
root 267 2 0 Dec19 ? 00:00:00 [spi0]
root 272 2 0 Dec19 ? 00:00:00 [bioset]
root 285 2 0 Dec19 ? 00:00:00 [kworker/1:1]
root 340 2 0 Dec19 ? 00:00:00 [ipv6_addrconf]
root 345 2 0 Dec19 ? 00:00:00 [deferwq]
root 351 2 0 Dec19 ? 00:00:00 [ubi_bgt0d]
root 352 2 0 Dec19 ? 00:00:00 [bioset]
root 360 2 0 Dec19 ? 00:00:00 [kworker/0:1H]
root 361 2 0 Dec19 ? 00:00:00 [kworker/1:1H]
root 401 2 0 Dec19 ? 00:00:00 [ata_sff]
root 410 2 0 Dec19 ? 00:00:00 [scsi_eh_0]
root 411 2 0 Dec19 ? 00:00:00 [scsi_tmf_0]
root 428 2 0 Dec19 ? 00:00:00 [scsi_eh_1]
root 429 2 0 Dec19 ? 00:00:00 [scsi_tmf_1]
root 430 2 0 Dec19 ? 00:00:00 [usb-storage]
root 466 2 0 Dec19 ? 00:00:00 [bioset]
root 495 2 0 Dec19 ? 00:00:00 [ubifs_bgt0_1]
root 503 2 0 Dec19 ? 00:00:00 [ubi_bgt1d]
root 509 2 0 Dec19 ? 00:00:00 [ubifs_bgt1_0]
root 594 2 0 Dec19 ? 00:00:00 [jbd2/sda1-8]
root 595 2 0 Dec19 ? 00:00:00 [ext4-rsv-conver]
root 596 1 0 Dec19 ? 00:00:00 /sbin/ubusd
root 645 1 0 Dec19 ttyS0 00:00:00 /sbin/askfirst /bin/ash --login
root 885 2 0 Dec19 ? 00:00:00 [cifsiod]
root 906 2 0 Dec19 ? 00:00:00 [cfg80211]
root 1196 1 0 Dec19 ? 00:00:00 /sbin/logd -S 16
root 1205 1 0 Dec19 ? 00:00:00 /sbin/rpcd
root 1239 1 0 Dec19 ? 00:00:00 /sbin/netifd
root 1264 1 0 Dec19 ? 00:00:00 /usr/sbin/odhcpd
root 1293 1 0 Dec19 ? 00:00:00 /usr/sbin/crond -f -c /etc/crontabs -l 8
root 1305 1 0 Dec19 ? 00:00:00 /usr/sbin/sshd -D
root 1320 1 0 Dec19 ? 00:00:00 /usr/sbin/uhttpd -f -h /www -r OpenWrt -x /cgi-bin -u /ubus -t 60 -T 30 -k 20 -A 1 -n 3
root 1373 1305 0 07:06 ? 00:00:00 sshd: root@pts/0
root 1375 1373 0 07:06 pts/0 00:00:00 -ash
root 1412 1 0 Dec19 ? 00:00:00 /usr/sbin/smbd -F
root 1413 1 0 Dec19 ? 00:00:00 /usr/sbin/nmbd -F
root 1517 1 0 Dec19 ? 00:00:00 /usr/sbin/openvpn --syslog openvpn(sample_server) --status /var/run/openvpn.sample_serve
root 1545 1 0 Dec19 ? 00:00:00 logread -f
root 1630 1 0 Dec19 ? 00:00:00 /usr/sbin/ntpd -n -S /usr/sbin/ntpd-hotplug -p 0.openwrt.pool.ntp.org -p 1.openwrt.pool.
root 1657 1239 0 Dec19 ? 00:00:00 odhcp6c -s /lib/netifd/dhcpv6.script -P0 -t120 eth1
root 1660 1 0 Dec19 ? 00:00:00 /usr/sbin/vnstatd -d
root 1662 1239 0 Dec19 ? 00:00:00 /usr/sbin/pppd nodetach ipparam wan ifname pppoe-wan +ipv6 set AUTOIPV6=1 nodefaultroute
root 1674 1 0 Dec19 ? 00:00:00 /usr/sbin/hostapd -P /var/run/wifi-phy1.pid -B /var/run/hostapd-phy1.conf
root 1697 1 0 Dec19 ? 00:00:00 /usr/sbin/hostapd -P /var/run/wifi-phy0.pid -B /var/run/hostapd-phy0.conf
root 1750 1239 0 Dec19 ? 00:00:00 odhcp6c -s /lib/netifd/dhcpv6.script -P0 -t120 pppoe-wan
nobody 1823 1 0 Dec19 ? 00:00:00 /usr/sbin/dnsmasq -C /var/etc/dnsmasq.conf -k -x /var/run/dnsmasq/dnsmasq.pid
root 1986 2 0 11:41 ? 00:00:00 [kworker/u4:0]
root 6035 2 0 11:21 ? 00:00:00 [kworker/u4:1]
root 11432 1375 0 13:59 pts/0 00:00:00 ps -ef
root 19412 1 0 Dec21 ? 00:00:00 /usr/sbin/collectd
I believe this is attributed to the code using improper format specifiers in calls to the 'sscanf' C library routine.
Specifically, in top/top.c;
at line #570
num = sscanf(S,
"%c "
"%d %d %d %d %d "
"%lu %lu %lu %lu %lu "
"%Lu %Lu %Lu %Lu " /* utime stime cutime cstime */
"%ld %ld "
"%d "
"%ld "
"%Lu " /* start_time */
"%lu "
"%ld "
"%lu %"KLF"u %"KLF"u %"KLF"u %"KLF"u %"KLF"u "
"%*s %*s %*s %*s " /* discard, no RT signals & Linux 2.1 used hex */
"%"KLF"u %*u %*u "
"%d %d "
"%lu %lu",
&P->state,
&P->ppid, &P->pgrp, &P->session, &P->tty, &P->tpgid,
&P->flags, &P->min_flt, &P->cmin_flt, &P->maj_flt, &P->cmaj_flt,
&P->utime, &P->stime, &P->cutime, &P->cstime,
&P->priority, &P->nice,
&P->nlwp,
&P->alarm,
&P->start_time,
&P->vsize,
&P->rss,
&P->rss_rlim, &P->start_code, &P->end_code, &P->start_stack, &P->kstk_esp, &P->kstk_eip,
/* P->signal, P->blocked, P->sigignore, P->sigcatch, */ /* can't use */
&P->wchan, /* &P->nswap, &P->cnswap, */ /* nswap and cnswap dead for 2.4.xx and up */
/* -- Linux 2.0.35 ends here -- */
&P->exit_signal, &P->processor, /* 2.2.1 ends with "exit_signal" */
/* -- Linux 2.2.8 to 2.5.17 end here -- */
&P->rtprio, &P->sched /* both added to 2.5.18 */
);
The "%Lu" format specifier is used for utime, stime, cutime, cstime, and start_time. The format specifier should be "%llu" rather than "%Lu".
Likewise, in proc/readproc.c;
at line #2388
if (4 > sscanf(bp, "cpu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu"
, &sum_ptr->cur.u, &sum_ptr->cur.n, &sum_ptr->cur.s
, &sum_ptr->cur.i, &sum_ptr->cur.w, &sum_ptr->cur.x
, &sum_ptr->cur.y, &sum_ptr->cur.z))
error_exit(N_txt(FAIL_statget_txt));
and at line #2419
if (4 > sscanf(bp, "cpu%d %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu", &cpu_ptr->id
, &cpu_ptr->cur.u, &cpu_ptr->cur.n, &cpu_ptr->cur.s
, &cpu_ptr->cur.i, &cpu_ptr->cur.w, &cpu_ptr->cur.x
, &cpu_ptr->cur.y, &cpu_ptr->cur.z)) {
memmove(cpu_ptr, sum_ptr, sizeof(CPU_t));
break; // tolerate cpus taken offline
}
The "%Lu" format specifiers should all be "%llu".
In my experience, after making the above proposed changes, and recompiling, the top and ps commands work as expected. I believe the proposed changes will be applicable to the wider community (ie. not just the OpenWrt users running on Linksys WRT1900AC).
Pat