How to monitor CPU or memory usage on a per-user basis

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

How to monitor CPU or memory usage on a per-user basis

https://access.redhat.com/solutions/239483

 

 SOLUTION 已验证 - 已更新 2015年九月15日19:08 - 

English 

环境

  • 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

    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

  • ps is perfect for quickly getting info on RAM or CPU usage, but it pales in comparison to top 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 ps command with a one-liner:

    Raw

    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:

    Raw

    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 called utop, but this is arbitrary and configurable -- simply change the initial name=utop line as desired.
  1. First, a custom top 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 .toprc file inside a new directory in /usr/local/etc/.

    Raw

    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
    
  2. Do the same (copy & paste) for the following to create the script in /usr/local/bin/.

    Raw

    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
    
  3. Finally, run utop (or whatever name was chosen) as any user. Example output:

    Raw

    $ 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 1 to a 0 in each occurrence of %.1f.


  1. Why is a separate top config file necessary? Creating a special directory just to contain a top config file might seem excessive, but this will allow toggling top 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 top 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). 

【最优潮流】直流最优潮流(OPF)课设(Matlab代码实现)内容概要:本文档主要围绕“直流最优潮流(OPF)课设”的Matlab代码实现展开,属于电力系统优化领域的教学与科研实践内容。文档介绍了通过Matlab进行电力系统最优潮流计算的基本原理与编程实现方法,重点聚焦于直流最优潮流模型的构建与求解过程,适用于课程设计或科研入门实践。文中提及使用YALMIP等优化工具包进行建模,并提供了相关资源下载链接,便于读者复现与学习。此外,文档还列举了大量与电力系统、智能优化算法、机器学习、路径规划等相关的Matlab仿真案例,体现出其服务于科研仿真辅导的综合性平台性质。; 适合人群:电气工程、自动化、电力系统及相关专业的本科生、研究生,以及从事电力系统优化、智能算法应用研究的科研人员。; 使用场景及目标:①掌握直流最优潮流的基本原理与Matlab实现方法;②完成课程设计或科研项目中的电力系统优化任务;③借助提供的丰富案例资源,拓展在智能优化、状态估计、微电网调度等方向的研究思路与技术手段。; 阅读建议:建议读者结合文档中提供的网盘资源,下载完整代码与工具包,边学习理论边动手实践。重点关注YALMIP工具的使用方法,并通过复现文中提到的多个案例,加深对电力系统优化问题建模与求解的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值