Skip to content

Sys_CpuMonitor

SweerItTer edited this page Feb 1, 2026 · 3 revisions

CpuMonitor API 文档

概述

CpuMonitor 是 utilsCore Sys 模块的核心类,提供 CPU 使用率监控功能,继承自 ResourceMonitor。

职责

  • 监控 CPU 使用率
  • 自动暂停和恢复监控
  • 输出监控数据到文件
  • 线程安全访问

适用场景

  • 系统性能监控
  • 负载均衡
  • 资源调度
  • 性能分析

依赖关系

  • 依赖: Linux /proc 文件系统, ResourceMonitor
  • 被依赖: VisionPipeline, DisplayManager 等模块

类分析

CpuMonitor 类

职责与用途

CpuMonitor 是 CPU 监控的封装类,提供:

  • CPU 使用率监控
  • 继承自 ResourceMonitor
  • 自动暂停和恢复机制
  • 数据输出到 /tmp/cpu_usage

设计模式

  • 继承: 继承自 ResourceMonitor
  • 模板方法: 实现 sampleUsage() 纯虚函数

公共 API 方法

构造函数

CpuMonitor(int sleeptime = 1000);

参数说明:

  • sleeptime (输入): 轮询间隔(毫秒),默认 1000

返回值: 无

所有权归属:

  • CpuMonitor 拥有内部资源的所有权

注意事项:

  1. 默认输出到 /tmp/cpu_usage
  2. 自动启动监控线程
  3. sleeptime 控制采样频率

使用例程:

// 创建监控器(默认每 1000ms 采样一次)
CpuMonitor cpu_monitor;

// 自定义轮询间隔(每 500ms 采样一次)
CpuMonitor cpu_monitor(500);

getUsage() - 获取 CPU 使用率

float getUsage();

参数说明: 无

返回值: CPU 使用率(0.0 - 100.0)

所有权归属:

  • 只读访问

注意事项:

  1. 继承自 ResourceMonitor
  2. 线程安全操作
  3. 首次调用返回 0.0(需要两次采样)
  4. 自动唤醒暂停的监控线程

使用例程:

CpuMonitor cpu_monitor;

// 首次调用
float usage1 = cpu_monitor.getUsage();  // 返回 0.0

// 等待采样
std::this_thread::sleep_for(std::chrono::seconds(1));

// 第二次调用
float usage2 = cpu_monitor.getUsage();  // 返回实际使用率
printf("CPU Usage: %.1f%%\n", usage2);

保护方法

sampleUsage() - 采样 CPU 使用率

bool sampleUsage(float& usage) override;

参数说明:

  • usage (输出): 采样到的 CPU 使用率

返回值:

  • true: 采样成功
  • false: 采样失败或首次采样

所有权归属:

  • 无所有权转移

注意事项:

  1. 实现 ResourceMonitor 的纯虚函数
  2. 读取 /proc/stat 文件
  3. 首次采样返回 false(无有效数据)
  4. 后续采样返回 true

内部实现

readProcStat() - 读取 CPU 状态

bool readProcStat(unsigned long long& total, unsigned long long& idle);

参数说明:

  • total (输出): CPU 总时间
  • idle (输出): CPU 空闲时间

返回值:

  • true: 读取成功
  • false: 读取失败

行为:

  1. 打开 /proc/stat 文件
  2. 读取第一行(总 CPU 信息)
  3. 解析 user, nice, system, idle, iowait, irq, softirq, steal
  4. 计算 total = user + nice + system + idle + iowait + irq + softirq + steal
  5. 计算 idle = idle(不含 iowait,与 top 一致)

CPU 使用率计算

unsigned long long delta_total = total - last_total_;
unsigned long long delta_idle = idle - last_idle_;
usage = 100.0f * (delta_total - delta_idle) / delta_total;

说明:

  • 使用率 = (总时间增量 - 空闲时间增量) / 总时间增量 * 100%
  • 不含 iowait(与 top 一致)

线程安全说明

同步机制

  • 继承自 ResourceMonitor 的同步机制
  • getUsage() 是线程安全的
  • sampleUsage() 在监控线程中调用

线程安全建议

  • 可以并发调用 getUsage()
  • 自动暂停和恢复机制由 ResourceMonitor 管理

典型使用场景

场景 1: 基本监控

CpuMonitor cpu_monitor;

// 等待首次采样
std::this_thread::sleep_for(std::chrono::seconds(1));

// 获取使用率
float usage = cpu_monitor.getUsage();
printf("CPU Usage: %.1f%%\n", usage);

场景 2: 持续监控

CpuMonitor cpu_monitor;

while (running) {
    float usage = cpu_monitor.getUsage();
    printf("CPU Usage: %.1f%%\n", usage);
    
    std::this_thread::sleep_for(std::chrono::seconds(1));
}

场景 3: 自定义轮询间隔

// 每 500ms 采样一次
CpuMonitor cpu_monitor(500);

while (running) {
    float usage = cpu_monitor.getUsage();
    printf("CPU Usage: %.1f%%\n", usage);
    
    std::this_thread::sleep_for(std::chrono::milliseconds(500));
}

场景 4: 自动暂停和恢复

CpuMonitor cpu_monitor;

// 获取使用率
float usage1 = cpu_monitor.getUsage();

// 等待 30 秒(监控线程会自动暂停)
std::this_thread::sleep_for(std::chrono::seconds(30));

// 再次获取使用率(线程会自动恢复)
float usage2 = cpu_monitor.getUsage();

场景 5: 输出到文件

CpuMonitor cpu_monitor;

// 数据会自动写入到 /tmp/cpu_usage
// 每行一个值,格式: 12.5

// 读取文件内容
std::ifstream file("/tmp/cpu_usage");
std::string line;
while (std::getline(file, line)) {
    printf("CPU Usage: %s%%\n", line.c_str());
}

注意事项

  1. 首次采样: 首次调用 getUsage() 返回 0.0(需要两次采样)
  2. 输出文件: 数据自动写入到 /tmp/cpu_usage
  3. 自动暂停: 30 秒无访问自动暂停监控线程
  4. 自动恢复: 调用 getUsage() 时自动恢复
  5. 线程安全: getUsage() 是线程安全的
  6. 轮询间隔: 构造时指定轮询间隔(默认 1000ms)
  7. 使用率范围: 0.0 - 100.0
  8. /proc/stat: 依赖 /proc/stat 文件

相关文档


参考资料

主页

API 文档

DMA 模块

DRM 模块

NET 模块

V4L2 模块

V4L2Param 模块

RGA 模块

MPP 模块

Sys 模块

Mouse 模块

Utils 模块

Clone this wiki locally