How to monitor CPU or memory usage on a per-user basis
Red Hat Enterprise Linux
Need a way to determine what percentage of the CPUs and memory (RAM) each user is utilizing. Ideally, would like to have a command that printed something like the following.
Raw
<code>user1 13% of total cpu usage, 25% of total ram user2 22% of total cpu usage, 2% of total ram</code>
Is there any command to fetch proportional set size of memory per user basis?
This can be accomplished using <code>top</code> or <code>ps</code>.
Why to use <code>ps</code>, and why not to
<code>ps</code> is perfect for quickly getting info on RAM or CPU usage, but it pales in comparison to <code>top</code> for real-time CPU usage stats. For further explanation, see the article: CPU usage reporting in ps versus top
That said, here's how one could get the requested info from the <code>ps</code> command with a one-liner:
<code>ps axo user,pcpu,pmem,rss --no-heading | awk '{pCPU[$1]+=$2; pMEM[$1]+=$3; sRSS[$1]+=$4} END {for (user in pCPU) if (pCPU[user]>0 || sRSS[user]>10240) printf "%s:@%.1f%% of total CPU,@%.1f%% of total MEM@(%.2f GiB used)\n", user, pCPU[user], pMEM[user], sRSS[user]/1024/1024}' | column -ts@ | sort -rnk2</code>
Example output:
<code>qemu: 118.0% of total CPU, 0.4% of total MEM (0.04 GiB used) rsaw: 7.7% of total CPU, 22.0% of total MEM (1.87 GiB used) root: 1.2% of total CPU, 1.3% of total MEM (0.23 GiB used)</code>
How to use <code>top</code>, for better CPU-usage inspection
The following steps walk through creating a simple <code>top</code>-wrapper script. By default the script and its config file will be called <code>utop</code>, but this is arbitrary and configurable -- simply change the initial <code>name=utop</code> line as desired.
First, a custom <code>top</code> config file needs to be created to make it possible to properly-customize top's options. (Why?1)
Copy and paste the following into a root shell to create the necessary <code>.toprc</code> file inside a new directory in <code>/usr/local/etc/</code>.
<code>name=utop mkdir -p /usr/local/etc/$name cat >/usr/local/etc/$name/.toprc <<\EOF RCfile for "top with windows" # shameless braggin' Id:a, Mode_altscr=0, Mode_irixps=0, Delay_time=3.000, Curwin=0 Def fieldscur=aEhioqtwKNmbcdfgjplrsuvyzx winflags=34105, sortindx=10, maxtasks=0 summclr=1, msgsclr=1, headclr=3, taskclr=1 Job fieldscur=ABcefgjlrstuvyzMKNHIWOPQDX winflags=62777, sortindx=0, maxtasks=0 summclr=6, msgsclr=6, headclr=7, taskclr=6 Mem fieldscur=ANOPQRSTUVbcdefgjlmyzWHIKX winflags=62777, sortindx=13, maxtasks=0 summclr=5, msgsclr=5, headclr=4, taskclr=5 Usr fieldscur=ABDECGfhijlopqrstuvyzMKNWX winflags=62777, sortindx=4, maxtasks=0 summclr=3, msgsclr=3, headclr=2, taskclr=3 EOF echo "$name's .toprc config file is in this dir" >/usr/local/etc/$name/README</code>
Do the same (copy & paste) for the following to create the script in <code>/usr/local/bin/</code>.
<code>cat >/usr/local/bin/$name <<EOF #!/bin/bash HOME=/usr/local/etc/$name top -bn1 | awk ' NR>2 && NF>0 { pCPU[\$1]+=\$2 ; pMEM[\$1]+=\$3 } END { for (user in pCPU) if (pCPU[user]>0.5 || pMEM[user]>0.5) printf "%s:@%.1f%% of total CPU,@%.1f%% of total MEM\\n", user, pCPU[user], pMEM[user] } ' | column -ts@ | sort -rnk2 EOF chmod +x /usr/local/bin/$name</code>
Finally, run <code>utop</code> (or whatever name was chosen) as any user. Example output:
<code>$ utop rsaw: 24.0% of total CPU, 68.7% of total MEM root: 2.0% of total CPU, 4.0% of total MEM lex: 1.1% of total CPU, 20.2% of total MEM named: 0.6% of total CPU, 0.4% of total MEM</code>
Customization notes:
The way the awk command is crafted, it will only print info for users that are using greater than 0.5% of total CPU or memory (i.e., half of 1%). To change this, look for the <code>if (pCPU[user]>0.5 || pMEM[user]>0.5)</code> code. Each number represents a percentage and so could either be changed to a different number (like 0 or 1 or 5 or 20) or the whole code block could be removed completely to see info on all users' processes.
If the extra decimal point of precision is not desired, change the <code>1</code> to a <code>0</code> in each occurrence of <code>%.1f</code>.
Why is a separate <code>top</code> config file necessary? Creating a special directory just to contain a <code>top</code> config file might seem excessive, but this will allow toggling <code>top</code> options, both for increased performance and for enhanced display, i.e.: toggling Irix/Solaris mode so that the number of logical processors are taken into account in the calculation of the cpu usage %. Example: If a system has four logical processors and a user's program is pegging a processor at 100%, top's default "Solaris" mode will report 100% cpu utilization for that program, whereas "Irix" mode would report 25%. In summary: Without changing <code>top</code> to use Irix mode, the cpu usage of different users can be compared; however, users' cpu usage cannot be easily judged against what is available on the system (unless the admin behind the keyboard gives extra thought to the number of logical processors present). ↩