How to monitor CPU or memory usage on a per-user basis
https://access.redhat.com/solutions/239483
SOLUTION 已验证 - 已更新 2015年九月15日19:08 -
环境
- 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.
user1 13% of total cpu usage, 25% of total ram user2 22% of total cpu usage, 2% of total ram - Is there any command to fetch proportional set size of memory per user basis?
决议
This can be accomplished using top or ps.
Why to use ps, and why not to
-
psis perfect for quickly getting info on RAM or CPU usage, but it pales in comparison totopfor 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
pscommand with a one-liner: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 -
Example output:
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)
How to use top, for better CPU-usage inspection
- The following steps walk through creating a simple
top-wrapper script. By default the script and its config file will be calledutop, but this is arbitrary and configurable -- simply change the initialname=utopline as desired.
-
First, a custom
topconfig 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.toprcfile inside a new directory in/usr/local/etc/.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 -
Do the same (copy & paste) for the following to create the script in
/usr/local/bin/.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 -
Finally, run
utop(or whatever name was chosen) as any user. Example output:$ 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
-
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
if (pCPU[user]>0.5 || pMEM[user]>0.5)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
1to a0in each occurrence of%.1f.
-
-
Why is a separate
topconfig file necessary? Creating a special directory just to contain atopconfig file might seem excessive, but this will allow togglingtopoptions, 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 changingtopto 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). ↩

本文介绍如何使用top和ps命令监测每个用户消耗的CPU和内存比例,提供了一种通过自定义top配置文件和脚本的方法,以实现更精确的资源使用情况报告。
3372

被折叠的 条评论
为什么被折叠?



