I am using sa -m
to display CPU usage details of each user on an RHEL 7 system. It displays the information in below format.
oracle 15335 88164.18re 176.77cp 0avio 167603k
root 9640 8294.42re 10.90cp 0avio 33737k
3rdeye 250 0.33re 0.04cp 0avio 28358k
sshd 4 2.11re 0.00cp 0avio 22068k
dbus 2 0.00re 0.00cp 0avio 7600k
smmsp 2 0.00re 0.00cp 0avio 21408k
I found this site which gives a brief info about the fields. (https://www.cyberciti.biz/tips/howto-log-user-activity-using-process-accounting.html#comments)
re - “real time” in wall clock minutes cp - sum of system and user time in cpu minutes k - cpu-time averaged core usage, in 1k units
Is there a way to calculate the percentage of CPU usage for each user from this data?
UPDATE 1:
I am adding this update since Alex is suggesting to use a script which is based on top command to obtain CPU usage per user. I have been using a script with that logic (top -b -n 1 -u $USERNAME | awk 'NR>7 { sum += $9; } END { print sum; }'
)to calculate CPU usage of a user. But if I observe closely it looks like the script does not give you the correct value. I somewhat found a reason for that. When you run top it shows a value for CPU usage then keeps updating it every 3 (default) seconds. But the initial value seems to be constant every time you run top.
[root@myserver unix]# top -b -n 1 -u oracle |grep -i "cpu"|head -1
%Cpu(s): 5.6 us, 2.6 sy, 0.0 ni, 79.0 id, 12.6 wa, 0.0 hi, 0.2 si, 0.0 st
[root@myserver unix]# top -b -n 1 -u oracle |grep -i "cpu"|head -1
%Cpu(s): 5.6 us, 2.6 sy, 0.0 ni, 79.0 id, 12.6 wa, 0.0 hi, 0.2 si, 0.0 st
[root@myserver unix]# top -b -n 1 -u oracle |grep -i "cpu"|head -1
%Cpu(s): 5.6 us, 2.6 sy, 0.0 ni, 79.0 id, 12.6 wa, 0.0 hi, 0.2 si, 0.0 st
[root@myserver unix]# top -b -n 1 -u oracle |grep -i "cpu"|head -1
%Cpu(s): 5.6 us, 2.6 sy, 0.0 ni, 79.0 id, 12.6 wa, 0.0 hi, 0.2 si, 0.0 st
I executed top for the user oracle four times and it keeps giving the same values for CPU usage. I could see it clearly when I run top without -b and -n options:
# top -u oracle
top - 08:47:44 up 3 days, 2:58, 2 users, load average: 2.21, 1.69, 1.42
Tasks: 1084 total, 3 running, 1081 sleeping, 0 stopped, 0 zombie
%Cpu(s): 5.6 us, 2.6 sy, 0.0 ni, 79.0 id, 12.6 wa, 0.0 hi, 0.2 si, 0.0 st
Notice how it starts with those values for CPU usage. Ofcourse, then after 3 seconds it refreshes and starts displaying correct values. This is the reason I want to change my approach in determining CPU usage per user. Please correct me if I am missing something.