0

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.

2
  • Have you considered using the native process accounting metrics ? cyberciti.biz/faq/…
    – steve
    Aug 18, 2017 at 18:20
  • Please guide me on what exactly I should be looking into.
    – Vinod
    Aug 19, 2017 at 3:00

1 Answer 1

0

I can suggest you an alternative way. And it's through the package systat (the link is for arch), and it provides commands as mpstat, iostat and many others.

mpstat's output:

Linux 4.9.43-1-lts (laptop)     18/08/2017  _x86_64_    (4 CPU)

19:51:50     CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
19:51:50     all    2,18    0,03    0,58    0,07    0,00    0,02    0,00    0,00    0,00   97,13

iostat's output:

Linux 4.9.43-1-lts (laptop)     18/08/2017  _x86_64_    (4 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           2,18    0,03    0,60    0,07    0,00   97,13

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               3,13        28,15        80,47    1029654    2943525
2
  • In what way can mpstat attribute cpu usage to an individual user ?
    – steve
    Aug 18, 2017 at 18:00
  • 1
    Ah I've misunderstood the question. So if you want for every user you could even write down a script like this. You take all the user avaiable in the system through: cut -d: -f1 /etc/passwd and after that you pass every single user in the command top -b -n 1 -u username | awk 'NR>7 { sum += $9; } END { print sum; }' . Of course you need top and username should be substituted
    – reuseman
    Aug 18, 2017 at 18:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .