如何使用 C++ 在 Linux 中获得总 CPU 使用率

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3017162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:07:56  来源:igfitidea点击:

How to get total cpu usage in Linux using C++

c++linuxcpu-usage

提问by David Polák

I am trying to get total cpu usage in %. First I should start by saying that "top" will simply not do, as there is a delay between cpu dumps, it requires 2 dumps and several seconds, which hangs my program (I do not want to give it its own thread)

我正在尝试以 % 为单位获得总 CPU 使用率。首先,我应该首先说“top”根本不会做,因为 cpu 转储之间存在延迟,它需要 2 次转储和几秒钟,这会挂起我的程序(我不想给它自己的线程)

next thing what I tried is "ps" which is instant but always gives very high number in total (20+) and when I actually got my cpu to do something it stayed at about 20...

接下来我尝试的是“ps”,它是即时的,但总是给出非常高的总数(20+),当我真正让我的 CPU 做某事时,它保持在大约 20 ......

Is there any other way that I could get total cpu usage? It does not matter if it is over one second or longer periods of time... Longer periods would be more useful, though.

有没有其他方法可以获得总 CPU 使用率?不管是超过一秒还是更长的时间段都没有关系……不过,更长的时间段会更有用。

采纳答案by Hitobat

cat /proc/stat

http://www.linuxhowtos.org/System/procstat.htm

猫/过程/统计

http://www.linuxhowtos.org/System/procstat.htm

I agree with this answer above. The cpu line in this file gives the total number of "jiffies" your system has spent doing different types of processing.

我同意上面的这个答案。该文件中的 cpu 行给出了您的系统执行不同类型处理所花费的“jiffies”总数。

What you need to do is take 2 readings of this file, seperated by whatever interval of time you require. The numbers are increasing values (subject to integer rollover) so to get the %cpu you need to calculate how many jiffies have elapsed over your interval, versus how many jiffies were spend doing work.

您需要做的是读取该文件的 2 个读数,以您需要的任何时间间隔分开。这些数字是递增的值(取决于整数翻转),因此要获得 %cpu,您需要计算在您的时间间隔内已经过去了多少 jiffies,以及有多少 jiffies 花费在工作上。

e.g. Suppose at 14:00:00 you have

例如假设在 14:00:00 你有

cpu 4698 591 262 8953 916 449 531

total_jiffies_1 = (sum of all values) = 16400

work_jiffies_1 = (sum of user,nice,system = the first 3 values) = 5551

中央处理器 4698 591 262 8953 916 449 531

total_jiffies_1 =(所有值的总和)= 16400

work_jiffies_1 = (user,nice,system = 前 3 个值的总和) = 5551

and at 14:00:05 you have

在 14:00:05 你有

cpu 4739 591 289 9961 936 449 541

total_jiffies_2 = 17506

work_jiffies_2 = 5619

中央处理器 4739 591 289 9961 936 449 541

total_jiffies_2 = 17506

work_jiffies_2 = 5619

So the %cpu usage over this period is:

所以这段时间的 %cpu 使用率是:

work_over_period = work_jiffies_2 - work_jiffies_1 = 68

total_over_period = total_jiffies_2 - total_jiffies_1 = 1106

%cpu = work_over_period / total_over_period * 100 = 6.1%

work_over_period = work_jiffies_2 - work_jiffies_1 = 68

total_over_period = total_jiffies_2 - total_jiffies_1 = 1106

%cpu = work_over_period / total_over_period * 100 = 6.1%

Hope that helps a bit.

希望那些对你有帮助。

回答by wdebeaum

Try reading /proc/loadavg. The first three numbers are the number of processes actually running (i.e., using a CPU), averaged over the last 1, 5, and 15 minutes, respectively.

尝试阅读/proc/loadavg。前三个数字是实际运行(即使用 CPU)的进程数,分别是过去 1、5 和 15 分钟的平均值。

http://www.linuxinsight.com/proc_loadavg.html

http://www.linuxinsight.com/proc_loadavg.html

回答by Lauro Oliveira

I suggest two files to starting...

我建议两个文件开始...

/proc/stat and /proc/cpuinfo.

/proc/stat 和 /proc/cpuinfo。

http://www.mjmwired.net/kernel/Documentation/filesystems/proc.txt

http://www.mjmwired.net/kernel/Documentation/filesystems/proc.txt

回答by Dummy00001

Read /proc/cpuinfoto find the number of CPU/cores available to the systems. Call the getloadavg()(or alternatively read the /proc/loadavg), take the first value, multiply it by 100 (to convert to percents), divide by number of CPU/cores. If the value is greater than 100, truncate it to 100. Done.

阅读/proc/cpuinfo以查找系统可用的 CPU/内核数。调用getloadavg()(或读取/proc/loadavg),取第一个值,乘以 100(转换为百分比),除以 CPU/内核数。如果该值大于 100,则将其截断为 100。完成。

Relevant documentation: man getloadavgand man 5 proc

相关文件:man getloadavgman 5 proc

N.B. Load average, usual to *NIX systems, can be more than 100% (per CPU/core) because it actually measures number of processes ready to be run by scheduler. With Windows-like CPU metric, when load is at 100% you do not really know whether it is optimal use of CPU resources or system is overloaded. Under *NIX, optimal use of CPU loadavg would give you value ~1.0 (or 2.0 for dual system). If the value is much greater than number CPU/cores, then you might want to plug extra CPUs into the box.

NB 平均负载,通常用于 *NIX 系统,可以超过 100%(每个 CPU/核心),因为它实际上测量准备由调度程序运行的进程数。使用类似于 Windows 的 CPU 指标,当负载为 100% 时,您并不真正知道这是 CPU 资源的最佳使用还是系统过载。在 *NIX 下,CPU loadavg 的最佳使用将为您提供 ~1.0(或双系统为 2.0)的值。如果该值远大于 CPU/内核数,则您可能需要将额外的 CPU 插入框中。

Otherwise, dig the /procfile system.

否则,挖掘/proc文件系统。

回答by souch

cpu-statis a C++ project that permits to read Linux CPU counter from /proc/stat .

cpu-stat是一个 C++ 项目,它允许从 /proc/stat 读取 Linux CPU 计数器。

Get CPUData.* and CPUSnaphot.* files from cpu-stat's src directory.

从 cpu-stat 的 src 目录中获取 CPUData.* 和 CPUSnaphot.* 文件。

Quick implementation to get overall cpu usage:

快速实现以获取整体 CPU 使用率:

#include "CPUSnapshot.h"

#include <chrono>
#include <thread>
#include <iostream>

int main()
{
  CPUSnapshot previousSnap;
  std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  CPUSnapshot curSnap;

  const float ACTIVE_TIME = curSnap.GetActiveTimeTotal() - previousSnap.GetActiveTimeTotal();
  const float IDLE_TIME   = curSnap.GetIdleTimeTotal() - previousSnap.GetIdleTimeTotal();
  const float TOTAL_TIME  = ACTIVE_TIME + IDLE_TIME;
  int usage = 100.f * ACTIVE_TIME / TOTAL_TIME;
  std::cout << "total cpu usage: " << usage << std::endl;
}

Compile it:

编译它:

g++ -std=c++11 -o CPUUsage main.cpp CPUSnapshot.cpp CPUData.cpp