简体   繁体   English

实时实时平均总CPU负载命令

[英]Command for Total Average CPU LOAD in real time

In my java program, I have connected to a remote server using Jsch, and now I need to get the Cpu utilization of that server. 在我的Java程序中,我已使用Jsch连接到远程服务器,现在需要获取该服务器的Cpu利用率。 I have used iostat -c command to get the CPU details...;the details I got is as follows: 我已经使用iostat -c命令来获取CPU详细信息...;我得到的详细信息如下:

Linux 3.1.0-1.2-desktop (ccitsuse06)    10/05/2015      _i686_  (4 CPU)
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.06    0.00    0.03    0.00    0.00   99.91

I actually need the total average CPU load ; 我实际上需要总的average CPU load How can I get the same using commands? 如何使用命令获得相同的信息? Is there any simple single command that gives me total average Cpu at real time... or do I need to parse results from these data? 是否有任何简单的命令可以实时为我提供平均总Cpu ...还是我需要解析这些数据的结果? Again; 再次; what factors contribute to the total average cpu so that i can calculate. 什么因素会影响总平均CPU,以便我进行计算。 I have read previous similar questions but none of them cleared my doubts. 我读过以前的类似问题,但没有一个消除我的疑问。

*Please note that It is the current/real time average CPU load that I require for my program and I want the command to be executed as accurately and as fast as possible.. *请注意,这是我的程序需要的当前/实时平均CPU负载,并且我希望命令尽可能准确,尽快地执行。

I have been reading usage of awk, egrep, etc but still I'm clue less as to how to use this in command to achieve my desired result! 我一直在阅读awk,egrep等的用法,但是对于如何在命令中使用它来达到我想要的结果,我仍然不太了解!

Try 尝试

mpstat -u | grep -A 1 '%idle' | tail -1 | sed -r 's/ +/ /g' | cut -d' ' -f12

If you want to understand what it's doing, you should try executing it bit by bit (start with just the first part, then add on the grep part, etc.). 如果您想了解它在做什么,则应尝试一点一点地执行它(仅从第一部分开始,然后添加grep部分, grep类推)。

This grabs all the stats using mpstat ; 这将使用mpstat所有统计信息; then takes just the relevant lines using grep ; 然后使用grep仅提取相关行; gets rid of the header row using tail ; tail摆脱标题行; compresses extra spaces using sed ; 使用sed压缩多余的空间; then takes the 12th column using cut . 然后使用cut获取第12列。

This will give you the idle reading, as a value out of 100. To turn this into a percentage figure for CPU usage, you'll need to subtract this value from 100. 这将使您的闲置读数从100中得到一个值。要将其转换为CPU使用率的百分比数字,您需要从100中减去该值。

To get an average CPU load over a period of time you need to look at the CPU used at the start and end of that period. 要获得一段时间内的平均CPU负载,您需要查看该时间段开始和结束时使用的CPU。 You can sample the /proc/stat which has the amount of time each logical CPU has used, or you need to run a utility for the amount of time you want to sample. 您可以对/ proc / stat进行采样,该采样具有每个逻辑CPU已使用的时间,或者您需要在要采样的时间内运行实用程序。

eg 例如

head -1 /proc/stat

prints 版画

cpu  170661 1384 22478 7836920 3244 0 2645 0 0 0

From the docs, this is 从文档中,这是

cpu  3357 0 4313 1362393
     The amount of time, measured in units of USER_HZ
     (1/100ths of a second on most architectures, use
     sysconf(_SC_CLK_TCK) to obtain the right value), that
     the system spent in various states:

     user   (1) Time spent in user mode.

     nice   (2) Time spent in user mode with low priority
            (nice).

     system (3) Time spent in system mode.

     idle   (4) Time spent in the idle task.  This value
            should be USER_HZ times the second entry in the
            /proc/uptime pseudo-file.

     iowait (since Linux 2.5.41)
            (5) Time waiting for I/O to complete.

     irq (since Linux 2.6.0-test4)
            (6) Time servicing interrupts.

     softirq (since Linux 2.6.0-test4)
            (7) Time servicing softirqs.

     steal (since Linux 2.6.11)
            (8) Stolen time, which is the time spent in
            other operating systems when running in a
            virtualized environment

     guest (since Linux 2.6.24)
            (9) Time spent running a virtual CPU for guest
            operating systems under the control of the Linux
            kernel.

     guest_nice (since Linux 2.6.33)
            (10) Time spent running a niced guest (virtual
            CPU for guest operating systems under the
            control of the Linux kernel).

If you take the difference of these values between any two samples, you can the percentage of time busy. 如果您采用任意两个样本之间的这些值之差,则可以使用繁忙时间百分比。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM