How to find out which process is eating that much memory? - unix

Output of # top -o size
last pid: 61935; load averages: 0.82, 0.44, 0.39 up 10+13:28:42 16:49:43
152 processes: 2 running, 150 sleeping
CPU: 10.3% user, 0.0% nice, 1.8% system, 0.2% interrupt, 87.7% idle
Mem: 5180M Active, 14G Inact, 2962M Wired, 887M Cache, 2465M Buf, 83M Free
Swap: 512M Total, 26M Used, 486M Free, 5% Inuse
PID USERNAME THR PRI NICE SIZE RES STATE C TIME WCPU COMMAND
1471 mysql 62 44 0 763M 349M ucond 3 222:19 74.76% mysqld
1171 root 4 44 0 645M 519M sbwait 0 20:56 3.86% tfs
41173 root 4 44 0 629M 516M sbwait 4 19:17 0.59% tfs
41350 root 4 44 0 585M 467M sbwait 7 15:17 0.10% tfs
36382 root 4 45 0 581M 401M sbwait 1 206:50 0.10% tfs
41157 root 4 44 0 551M 458M sbwait 5 16:23 0.98% tfs
36401 root 4 45 0 199M 108M uwait 2 17:50 0.00% tfs
36445 root 4 44 0 199M 98M uwait 4 20:11 0.00% tfs
36420 root 4 45 0 191M 98M uwait 4 19:57 0.00% tfs
3491 root 9 45 0 79320K 41292K uwait 4 40:22 0.00% tfs_db
40690 root 1 44 0 29896K 4104K select 1 0:05 0.00% sshd
44636 root 1 44 0 29896K 3896K select 4 0:00 0.00% sshd
22224 root 1 44 0 29896K 3848K select 6 0:00 0.00% sshd
42956 root 1 44 0 29896K 3848K select 4 0:00 0.00% sshd
909 bind 11 76 0 27308K 14396K kqread 1 0:00 0.00% named
1586 root 1 44 0 26260K 3464K select 4 0:00 0.00% sshd
40590 root 4 45 0 23480K 7592K uwait 1 5:11 0.00% auth
1472 root 1 44 0 22628K 8776K select 0 0:41 0.00% perl5.8.9
22229 root 1 44 0 20756K 2776K select 0 0:00 0.00% sftp-server
42960 root 1 44 0 20756K 2772K select 2 0:00 0.00% sftp-server
44638 root 1 44 0 10308K 2596K pause 2 0:00 0.00% csh
42958 root 1 47 0 10308K 1820K pause 3 0:00 0.00% csh
22227 root 1 48 0 10308K 1820K pause 0 0:00 0.00% csh
36443 root 1 57 0 10248K 1792K wait 0 0:00 0.00% bash
36418 root 1 51 0 10248K 1788K wait 2 0:00 0.00% bash
41171 root 1 63 0 10248K 1788K wait 0 0:00 0.00% bash
36399 root 1 50 0 10248K 1784K wait 2 0:00 0.00% bash
41155 root 1 56 0 10248K 1784K wait 0 0:00 0.00% bash
40588 root 1 76 0 10248K 1776K wait 6 0:00 0.00% bash
36380 root 1 50 0 10248K 1776K wait 2 0:00 0.00% bash
41348 root 1 54 0 10248K 1776K wait 0 0:00 0.00% bash
1169 root 1 54 0 10248K 1772K wait 0 0:00 0.00% bash
3485 root 1 76 0 10248K 1668K wait 4 0:00 0.00% bash
61934 root 1 44 0 9372K 2356K CPU4 4 0:00 0.00% top
1185 mysql 1 76 0 8296K 1356K wait 3 0:00 0.00% sh
1611 root 1 44 0 7976K 1372K nanslp 0 0:08 0.00% cron
824 root 1 44 0 7048K 1328K select 0 0:03 0.00% syslogd
1700 root 1 76 0 6916K 1052K ttyin 3 0:00 0.00% getty
1703 root 1 76 0 6916K 1052K ttyin 2 0:00 0.00% getty
1702 root 1 76 0 6916K 1052K ttyin 5 0:00 0.00% getty
1706 root 1 76 0 6916K 1052K ttyin 0 0:00 0.00% getty
1705 root 1 76 0 6916K 1052K ttyin 1 0:00 0.00% getty
1701 root 1 76 0 6916K 1052K ttyin 6 0:00 0.00% getty
1707 root 1 76 0 6916K 1052K ttyin 4 0:00 0.00% getty
1704 root 1 76 0 6916K 1052K ttyin 7 0:00 0.00% getty
490 root 1 44 0 3204K 556K select 1 0:00 0.00% devd
My game server lag so much and I have noticed that there is only 83M of free ram.
Its not just top because I have also tried to use other app:
# /usr/local/bin/freem
SYSTEM MEMORY INFORMATION:
mem_wire: 3104976896 ( 2961MB) [ 12%] Wired: disabled for paging out
mem_active: + 5440778240 ( 5188MB) [ 21%] Active: recently referenced
mem_inactive:+ 15324811264 ( 14614MB) [ 61%] Inactive: recently not referenced
mem_cache: + 1015689216 ( 968MB) [ 4%] Cached: almost avail. for allocation
mem_free: + 86818816 ( 82MB) [ 0%] Free: fully available for allocation
mem_gap_vm: + 946176 ( 0MB) [ 0%] Memory gap: UNKNOWN
-------------- ------------ ----------- ------
mem_all: = 24974020608 ( 23817MB) [100%] Total real memory managed
mem_gap_sys: + 772571136 ( 736MB) Memory gap: Kernel?!
-------------- ------------ -----------
mem_phys: = 25746591744 ( 24553MB) Total real memory available
mem_gap_hw: + 23212032 ( 22MB) Memory gap: Segment Mappings?!
-------------- ------------ -----------
mem_hw: = 25769803776 ( 24576MB) Total real memory installed
SYSTEM MEMORY SUMMARY:
mem_used: 9342484480 ( 8909MB) [ 36%] Logically used memory
mem_avail: + 16427319296 ( 15666MB) [ 63%] Logically available memory
-------------- ------------ ----------- ------
mem_total: = 25769803776 ( 24576MB) [100%] Logically total memory
As you can see, the output is similar:
mem_free: + 86818816 ( 82MB) [ 0%] Free: fully available for allocation.
My dedicated has 24GB of RAM and it's pretty much for my game server.
How can I find out which process is eating that amount of memory?
I am using FreeBSD 8.2.

According to top's output, you are only using 5% of your swap. This means, you are not short on RAM -- whatever is slowing you down, it is not the memory shortage. If anything, I'd be suspecting mysqld -- not only was it quite busy, when you took the snapshot, it also accumulated quite a bit of CPU-time prior to that.
Perhaps, some frequently-running queries can be helped by a new index or two?

Related

Nginx and uwsgi setting for heavy calculation site?

I am using nginx and uwsgi(django) for site on AWS fargate.
This program doing a bit heavy calculating task.
So, I guess I should do some tuning for uwsgi or nginx.
I start uwsgi in django container with processes and threads.
uwsgi --http :8011 --processes 8 --threads 8 --module mysite.wsgi
and do nothing special for nginx
server {
listen 80;
server_name mysite;
charset utf-8;
location / {
proxy_pass http://127.0.0.1:8011/;
include /etc/nginx/uwsgi_params;
}
}
With this setting, program works, but even after heavy task finished.
Server response is still so heavy.
I checked with top command, even after task finished, not many memory are free.
top - 20:15:21 up 24 min, 0 users, load average: 4.65, 4.00, 1.91
Tasks: 15 total, 1 running, 11 sleeping, 0 stopped, 3 zombie
%Cpu(s): 0.0 us, 0.3 sy, 0.0 ni, 99.5 id, 0.2 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 3703.8 total, 176.6 free, 1095.6 used, 2431.5 buff/cache
MiB Swap: 0.0 total, 0.0 free, 0.0 used. 2382.4 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 5724 284 4 S 0.0 0.0 0:00.19 bash
9 root 20 0 1398196 6260 0 S 0.0 0.2 0:00.50 amazon-ssm-agen
24 root 20 0 1410428 11772 0 S 0.0 0.3 0:00.85 ssm-agent-worke
48 root 20 0 622428 37900 1168 S 0.0 1.0 0:00.38 uwsgi
49 root 20 0 47468 1396 36 S 0.0 0.0 0:00.18 uwsgi
50 root 20 0 0 0 0 Z 0.0 0.0 0:05.75 uwsgi
56 root 20 0 1755900 424896 177332 S 0.0 11.2 0:14.92 uwsgi
59 root 20 0 1754360 365476 120228 S 0.0 9.6 0:07.77 uwsgi
60 root 20 0 0 0 0 Z 0.0 0.0 0:13.31 uwsgi
68 root 20 0 622428 36788 56 S 0.0 1.0 0:00.00 uwsgi
69 root 20 0 1755600 373404 125260 S 0.0 9.8 0:09.18 uwsgi
77 root 20 0 0 0 0 Z 0.0 0.0 0:02.90 uwsgi
129 root 20 0 1327588 10376 0 S 0.0 0.3 0:10.33 ssm-session-wor
139 root 20 0 5988 2288 1764 S 0.0 0.1 0:00.64 bash
261 root 20 0 8900 3648 3136 R 0.0 0.1 0:00.00 top
I guess this means task is not correctly freed??
However where should I check or tuning??

How access to display device in embedded-linux + Qt ? /dev/fb0 not found

1-I have built Qt5.4.1 and Qt4.8.4 for an embedded device (based on armv5tejl) and all things work except GUI.
2-It seems that no frame buffer device exists:
~/bin $ ls -alh /dev/
drwxr-xr-x 3 0 sandbox 0 Nov 10 11:48 .
drwxr-xr-x 14 0 root 0 Nov 14 2017 ..
srw-rw-rw- 1 0 root 0 Nov 10 11:48 log
crw-rw-rw- 1 0 root 1, 3 Nov 10 11:48 null
crw-rw-rw- 1 0 root 5, 2 Nov 10 12:10 ptmx
drwxr-xr-x 2 0 root 0 Jan 1 1970 pts
crw-rw-rw- 1 0 root 5, 0 Nov 10 11:48 tty
crw-rw-rw- 1 0 root 204, 66 Nov 10 12:05 ttyAMA2
crw-r--r-- 1 0 root 1, 9 Nov 10 11:48 urandom
/proc $ cat devices
Character devices:
1 mem
4 /dev/vc/0
4 tty
5 /dev/tty
5 /dev/console
5 /dev/ptmx
7 vcs
10 misc
/proc $ cat devices
Character devices:
1 mem
4 /dev/vc/0
4 tty
5 /dev/tty
5 /dev/console
5 /dev/ptmx
7 vcs
10 misc
13 input
21 sg
29 fb
89 i2c
90 mtd
100 softkey
116 alsa
122 pman
128 ptm
136 pts
150 pwmr
153 upc
154 kdrv_csc
180 usb
189 usb_device
204 ttyAMA
254 rtc
Block devices:
1 ramdisk
259 blkext
8 sd
31 mtdblock
65 sd
66 sd
67 sd
68 sd
69 sd
70 sd
71 sd
128 sd
129 sd
130 sd
131 sd
132 sd
133 sd
134 sd
135 sd
3-from dmesg log:
Console: switching to colour frame buffer device 80x30
CLCD: PL110 CLCD hardware, HIE AT-T050QGH-02, VGA Landscape (supports 640x480 # 24Mhz) display
4-Device itself has a SDK and display things using that SDK and libs in HTML format.
5-I have a restricted user on the device and I'm using my own BusyBox v1.20.0.
My Questions:
1-Is it possible for me to use device display via Qt without using provided SDK?
2-How could I access to the display device?
Any help would be appreciated.
Thank you very much

What is the cause of cpu high for given result in centos [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I am using centos.
When i am running the command free -m then its showing me below:
total used free shared buffers cached
Mem: 2048 373 1674 10 0 147
-/+ buffers/cache: 225 1822
Swap: 0 0 0
I have run the command "Top" and get the below result:
top - 07:08:01 up 16:09, 3 users, load average: 0.00, 0.00, 0.00
Tasks: 39 total, 1 running, 38 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 2097152k total, 381024k used, 1716128k free, 0k buffers
Swap: 0k total, 0k used, 0k free, 150200k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 19236 1452 1212 S 0.0 0.1 0:00.02 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd/23354
3 root 20 0 0 0 0 S 0.0 0.0 0:00.00 khelper/23354
147 root 16 -4 10644 668 400 S 0.0 0.0 0:00.00 udevd
453 root 20 0 179m 1512 1056 S 0.0 0.1 0:00.27 rsyslogd
489 root 20 0 66692 1296 536 S 0.0 0.1 0:00.03 sshd
497 root 20 0 22192 972 716 S 0.0 0.0 0:00.00 xinetd
658 root 20 0 66876 1028 312 S 0.0 0.0 0:00.00 saslauthd
659 root 20 0 66876 764 48 S 0.0 0.0 0:00.00 saslauthd
731 root 20 0 114m 1260 620 S 0.0 0.1 0:00.24 crond
835 ossecm 20 0 10512 492 312 S 0.0 0.0 0:00.32 ossec-maild
839 root 20 0 13088 960 712 S 0.0 0.0 0:00.00 ossec-execd
843 ossec 20 0 12780 2380 620 S 0.0 0.1 0:10.15 ossec-analysisd
847 root 20 0 4200 444 304 S 0.0 0.0 0:00.84 ossec-logcollec
858 root 20 0 5004 1484 468 S 0.0 0.1 0:07.06 ossec-syscheckd
862 ossec 20 0 6388 624 372 S 0.0 0.0 0:00.03 ossec-monitord
870 root 20 0 92420 21m 1620 S 0.0 1.0 0:01.21 miniserv.pl
4363 root 20 0 96336 4448 3464 S 0.0 0.2 0:00.10 sshd
4365 root 20 0 105m 2024 1532 S 0.0 0.1 0:00.03 bash
4615 root 20 0 96776 4936 3460 S 0.0 0.2 0:00.61 sshd
4617 root 20 0 105m 2052 1548 S 0.0 0.1 0:00.20 bash
4674 root 20 0 96336 4452 3460 S 0.0 0.2 0:00.22 sshd
4676 root 20 0 105m 2012 1532 S 0.0 0.1 0:00.06 bash
7494 root 20 0 96336 4404 3428 S 0.0 0.2 0:00.03 sshd
7496 root 20 0 57712 2704 2028 S 0.0 0.1 0:00.01 sftp-server
7719 root 20 0 83116 2700 836 S 0.0 0.1 0:00.10 sendmail
7728 smmsp 20 0 78692 2128 636 S 0.0 0.1 0:00.00 sendmail
7742 root 20 0 402m 14m 7772 S 0.0 0.7 0:00.13 httpd
7744 asterisk 20 0 502m 22m 10m S 0.0 1.1 0:00.11 httpd
7938 root 20 0 105m 756 520 S 0.0 0.0 0:00.00 safe_asterisk
7940 asterisk 20 0 3157m 26m 8508 S 0.0 1.3 0:07.14 asterisk
8066 root 20 0 105m 1568 1304 S 0.0 0.1 0:00.01 mysqld_safe
8168 mysql 20 0 499m 21m 6472 S 0.0 1.1 0:01.44 mysqld
8607 asterisk 20 0 402m 8288 1404 S 0.0 0.4 0:00.00 httpd
8608 asterisk 20 0 402m 8288 1404 S 0.0 0.4 0:00.00 httpd
8611 asterisk 20 0 402m 8284 1400 S 0.0 0.4 0:00.00 httpd
8615 asterisk 20 0 402m 8296 1412 S 0.0 0.4 0:00.00 httpd
Even when i am trying see by disabling the services asterisk,httpd,sendmail,mysqld still its showing 100% cpu usage.
Can anybody know how can i check what is the actual thing which is taking this much CPU usages?
The CPU Usage in top says:
Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Your CPU is 100% idle. This is the explanation:
us: user cpu time (or) % CPU time spent in user space
sy: system cpu time (or) % CPU time spent in kernel space
ni: user nice cpu time (or) % CPU time spent on low priority processes
id: idle cpu time (or) % CPU time spent idle
wa: io wait cpu time (or) % CPU time spent in wait (on disk)
hi: hardware irq (or) % CPU time spent servicing/handling hardware interrupts
si: software irq (or) % CPU time spent servicing/handling software interrupts
st: steal time - - % CPU time in involuntary wait by virtual cpu while hypervisor is servicing another processor (or) % CPU time stolen from a virtual machine

High RAM usage by NGINX

There are 6 NGINX processes in the server. Ever since NGINX is started, the RES/VIRT values kept growing until it is out of memory. Is it indicating there is a memory leak?
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1941 root 20 0 621m 17m 4144 S 290.4 0.1 8415:03 mongod
16383 nobody 20 0 1675m 1.6g 724 S 21.0 5.2 13:19.30 nginx
16382 nobody 20 0 1671m 1.6g 724 S 17.2 5.1 13:21.39 nginx
16381 nobody 20 0 1674m 1.6g 724 S 15.3 5.1 13:28.45 nginx
16380 nobody 20 0 1683m 1.6g 724 S 13.4 5.2 13:24.77 nginx
16384 nobody 20 0 1674m 1.6g 724 S 13.4 5.1 13:19.83 nginx
16385 nobody 20 0 1685m 1.6g 724 S 13.4 5.2 13:25.00 nginx
Try look on this ngx_http_limit_conn_module nginx module.
Also take a look to client_max_body_size

How to redirect output of top command in a file

for monitoring purpose of system, i need to redirect the output of top command in a file so i will use/parse it.
i am trying to do same thing but CPU performance stats are not getting saved in a file see
screen shots.
expected output:
[root#v100 /usr/local/bin]# top
last pid: 6959; load averages: 0.01, 0.03, 0.03 up 0+02:47:34 17:51:16
114 processes: 1 running, 108 sleeping, 5 zombie
CPU: 0.0% user, 0.0% nice, 1.6% system, 0.0% interrupt, 98.4% idle
Mem: 734M Active, 515M Inact, 226M Wired, 212M Buf, 491M Free
Swap: 4095M Total, 4095M Free
PID USERNAME THR PRI NICE SIZE RES STATE TIME WCPU COMMAND
1953 root 150 20 0 3084M 635M uwait 2:44 0.00% java
1663 mysql 46 20 0 400M 139M sbwait 1:29 0.00% mysqld
1354 root 31 20 0 94020K 50796K uwait 0:24 0.00% beam
4233 root 1 20 0 122M 23940K select 0:06 0.00% python
1700 zabbix 1 20 0 20096K 2436K nanslp 0:03 0.00% zabbix_agentd
1799 zabbix 1 20 0 103M 7240K nanslp 0:02 0.00% zabbix_server
4222 root 1 30 0 122M 23300K select 0:02 0.00% python
1696 zabbix 1 20 0 19968K 2424K nanslp 0:02 0.00% zabbix_agentd
2853 root 1 20 0 126M 29780K select 0:02 0.00% python
1793 zabbix 1 20 0 103M 7152K nanslp 0:01 0.00% zabbix_server
1797 zabbix 1 20 0 103M 8348K nanslp 0:01 0.00% zabbix_server
1752 root 1 20 0 122M 22344K select 0:01 0.00% python
1796 zabbix 1 20 0 103M 8136K nanslp 0:01 0.00% zabbix_server
1795 zabbix 1 20 0 103M 8208K nanslp 0:01 0.00% zabbix_server
1801 zabbix 1 20 0 103M 7100K nanslp 0:01 0.00% zabbix_server
3392 root 1 20 0 122M 23392K select 0:01 0.00% python
1798 zabbix 1 20 0 103M 7860K nanslp 0:01 0.00% zabbix_server
2812 root 1 20 0 134M 25184K select 0:01 0.00% python
1791 zabbix 1 20 0 103M 7188K nanslp 0:01 0.00% zabbix_server
1827 root 1 -52 r0 14368K 1400K nanslp 0:01 0.00% watchdogd
1790 zabbix 1 20 0 103M 7164K nanslp 0:01 0.00% zabbix_server
1778 zabbix 1 20 0 103M 8608K nanslp 0:01 0.00% zabbix_server
1780 zabbix 1 20 0 103M 8608K nanslp 0:01 0.00% zabbix_server
2928 root 1 20 0 122M 23272K select 0:01 0.00% python
2960 root 1 20 0 116M 22288K select 0:01 0.00% python
1776 zabbix 1 20 0 103M 7248K nanslp 0:01 0.00% zabbix_server
2892 root 1 20 0 122M 22648K select 0:01 0.00% python
1789 zabbix 1 20 0 103M 7128K nanslp 0:01 0.00% zabbix_server
1814 root 1 20 0 216M 15796K select 0:01 0.00% httpd
1779 zabbix 1 20 0 103M 8608K nanslp 0:01 0.00% zabbix_server
1783 zabbix 1 20 0 103M 8608K nanslp 0:01 0.00% zabbix_server
1800 zabbix 1 20 0 103M 7124K nanslp 0:01 0.00% zabbix_server
1782 zabbix 1 20 0 103M 8608K nanslp 0:01 0.00% zabbix_server
1781 zabbix 1 20 0 103M 8608K nanslp 0:00 0.00% zabbix_server
1792 zabbix 1 20 0 103M 7172K nanslp 0:00 0.00% zabbix_server
2259 root 2 20 0 48088K 4112K uwait 0:00 0.00% cb_heuristics
If i do:
[root#v100 /usr/local/bin]# top > /tmp/top.output
then it shows:
[root#v100 /usr/local/bin]# cat /tmp/top.output
last pid: 7080; load averages: 0.09, 0.06, 0.03 up 0+02:52:24 17:56:06
114 processes: 1 running, 108 sleeping, 5 zombie
Mem: 731M Active, 515M Inact, 219M Wired, 212M Buf, 501M Free
Swap: 4095M Total, 4095M Free
PID USERNAME THR PRI NICE SIZE RES STATE TIME WCPU COMMAND
1953 root 150 20 0 3084M 633M uwait 2:17 0.00% java
1663 mysql 46 20 0 400M 136M sbwait 1:08 0.00% mysqld
1354 root 31 20 0 94020K 49924K uwait 0:18 0.00% beam
4233 root 1 20 0 122M 23776K select 0:04 0.00% python
1700 zabbix 1 20 0 20096K 2436K nanslp 0:02 0.00% zabbix_agentd
1799 zabbix 1 20 0 103M 7240K nanslp 0:01 0.00% zabbix_server
2853 root 1 20 0 126M 29780K select 0:01 0.00% python
1696 zabbix 1 20 0 19968K 2424K nanslp 0:01 0.00% zabbix_agentd
4222 root 1 28 0 122M 23264K select 0:01 0.00% python
1793 zabbix 1 20 0 103M 7152K nanslp 0:01 0.00% zabbix_server
1752 root 1 20 0 122M 22344K select 0:01 0.00% python
1797 zabbix 1 20 0 103M 8088K nanslp 0:01 0.00% zabbix_server
1796 zabbix 1 20 0 103M 7944K nanslp 0:01 0.00% zabbix_server
1795 zabbix 1 20 0 103M 8044K nanslp 0:01 0.00% zabbix_server
1801 zabbix 1 20 0 103M 7100K nanslp 0:01 0.00% zabbix_server
3392 root 1 20 0 122M 23312K select 0:01 0.00% python
2812 root 1 20 0 134M 25184K select 0:01 0.00% python
1798 zabbix 1 20 0 103M 7628K nanslp 0:01 0.00% zabbix_server
so here, I am able to monitor Memory but not CPU
reason is during redirect output of top CPU stats did not update
How can i capture CPU stats also?
if you have any suggestion pls tell me.
top -b -n 1 seems to work on my Linux box here (-b: batch mode operation, -n: number of iterations).
Edit:
I just tried it on FreeBSD 9.2 which uses the 3.5beta12 version of top. It seems it needs at least one additional iteration to get CPU stats. So you might want to use:
top -b -d2 -s1 | sed -e '1,/USERNAME/d' | sed -e '1,/^$/d'
-b: batch mode, -d2: 2 displays (the first one does not contain CPU stats, second one does), -s1: wait one seconds between displays
The sed pipeline removes the first display which does not contain CPU stats (by skipping header and process list).

Resources