linux系统下如何获取进程和系统的cpu占用率

背景

在开发SDK的过程中,需要收集进程和系统相关的负载信息。其中,CPU占用率是一个非常关键的指标。在早期阶段,我们使用管道方式获取top命令返回结果。但后来发现在线上存在一些异常情况,即上报的CPU占用率为0。考虑到top命令会带来较大的开销,并且管道使用不当容易出现问题,最终我们采用了proc文件系统方式获取CPU占用率。

设计方案

在这里插入图片描述

  1. 从/proc/pid/stat文件中提取进程的CPU时间信息。
  2. 从/proc/stat文件中提取系统的CPU时间信息。
  3. 在一定间隔时间内(即统计周期)重复执行步骤1和2。
  4. 根据两次结果计算出统计周期内进程和系统的CPU占用率。

源码

定时器(统计周期)不断调用QuerySysProcessCpuInfo函数即可活动在统计周期内动态的CPU占用率。

void QuerySysProcessCpuInfo(void)
{
    
    
  // 每个统计周期内(2s)的系统和进程CPU占用率
  std::ifstream pid_stat_file("/proc/" + std::to_string(getpid()) + "/stat");
  if (!pid_stat_file.is_open()) {
    
    
      return kErrorFatal;
  }
  long utime, stime, cutime, cstime;
  std::string temp;
  for (int i = 0; i < 13; ++i) {
    
    
      pid_stat_file >> temp; // Skip to utime
  }
  pid_stat_file >> utime >> stime >> cutime >> cstime;
  long pid_work = utime + stime + cutime + cstime;
  static long prev_pid_work = 0;

  std::ifstream sys_stat_file("/proc/stat");
  if (!sys_stat_file.is_open()) {
    
    
      return kErrorFatal;
  }
  long user, nice, system, idle;
  std::string cpu;
  sys_stat_file >> cpu >> user >> nice >> system >> idle;
  long total = user + nice + system + idle;
  long work = user + nice + system;
  static long prev_total = 0, prev_work = 0;
  double pid_cpu_usage = 100.0 * (pid_work - prev_pid_work) / (total - prev_total);
  printf("进程CPU占用率: %.2f%%\n", pid_cpu_usage);
  double sys_cpu_usage = 100.0 * (work - prev_work) / (total - prev_total);
  printf("系统CPU占用率: %.2f%%\n", sys_cpu_usage );

  prev_pid_work = pid_work;
  prev_total = total;
  prev_work = work;

  pid_stat_file.close();
  sys_stat_file.close();

  return;
}

猜你喜欢

转载自blog.csdn.net/weixin_36623563/article/details/130770748