-
Notifications
You must be signed in to change notification settings - Fork 1
Sys_CpuMonitor
SweerItTer edited this page Feb 1, 2026
·
3 revisions
CpuMonitor 是 utilsCore Sys 模块的核心类,提供 CPU 使用率监控功能,继承自 ResourceMonitor。
- 监控 CPU 使用率
- 自动暂停和恢复监控
- 输出监控数据到文件
- 线程安全访问
- 系统性能监控
- 负载均衡
- 资源调度
- 性能分析
- 依赖: Linux /proc 文件系统, ResourceMonitor
- 被依赖: VisionPipeline, DisplayManager 等模块
CpuMonitor 是 CPU 监控的封装类,提供:
- CPU 使用率监控
- 继承自 ResourceMonitor
- 自动暂停和恢复机制
- 数据输出到 /tmp/cpu_usage
- 继承: 继承自 ResourceMonitor
- 模板方法: 实现 sampleUsage() 纯虚函数
CpuMonitor(int sleeptime = 1000);参数说明:
-
sleeptime(输入): 轮询间隔(毫秒),默认 1000
返回值: 无
所有权归属:
- CpuMonitor 拥有内部资源的所有权
注意事项:
- 默认输出到 /tmp/cpu_usage
- 自动启动监控线程
- sleeptime 控制采样频率
使用例程:
// 创建监控器(默认每 1000ms 采样一次)
CpuMonitor cpu_monitor;
// 自定义轮询间隔(每 500ms 采样一次)
CpuMonitor cpu_monitor(500);float getUsage();参数说明: 无
返回值: CPU 使用率(0.0 - 100.0)
所有权归属:
- 只读访问
注意事项:
- 继承自 ResourceMonitor
- 线程安全操作
- 首次调用返回 0.0(需要两次采样)
- 自动唤醒暂停的监控线程
使用例程:
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);bool sampleUsage(float& usage) override;参数说明:
-
usage(输出): 采样到的 CPU 使用率
返回值:
-
true: 采样成功 -
false: 采样失败或首次采样
所有权归属:
- 无所有权转移
注意事项:
- 实现 ResourceMonitor 的纯虚函数
- 读取 /proc/stat 文件
- 首次采样返回 false(无有效数据)
- 后续采样返回 true
bool readProcStat(unsigned long long& total, unsigned long long& idle);参数说明:
-
total(输出): CPU 总时间 -
idle(输出): CPU 空闲时间
返回值:
-
true: 读取成功 -
false: 读取失败
行为:
- 打开 /proc/stat 文件
- 读取第一行(总 CPU 信息)
- 解析 user, nice, system, idle, iowait, irq, softirq, steal
- 计算 total = user + nice + system + idle + iowait + irq + softirq + steal
- 计算 idle = idle(不含 iowait,与 top 一致)
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 管理
CpuMonitor cpu_monitor;
// 等待首次采样
std::this_thread::sleep_for(std::chrono::seconds(1));
// 获取使用率
float usage = cpu_monitor.getUsage();
printf("CPU Usage: %.1f%%\n", usage);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));
}// 每 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));
}CpuMonitor cpu_monitor;
// 获取使用率
float usage1 = cpu_monitor.getUsage();
// 等待 30 秒(监控线程会自动暂停)
std::this_thread::sleep_for(std::chrono::seconds(30));
// 再次获取使用率(线程会自动恢复)
float usage2 = cpu_monitor.getUsage();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());
}- 首次采样: 首次调用 getUsage() 返回 0.0(需要两次采样)
- 输出文件: 数据自动写入到 /tmp/cpu_usage
- 自动暂停: 30 秒无访问自动暂停监控线程
- 自动恢复: 调用 getUsage() 时自动恢复
- 线程安全: getUsage() 是线程安全的
- 轮询间隔: 构造时指定轮询间隔(默认 1000ms)
- 使用率范围: 0.0 - 100.0
- /proc/stat: 依赖 /proc/stat 文件
- Base - 资源监控基类
- MemoryMonitor - 内存监控
- Sys 模块总览
主页
API 文档
DMA 模块
DRM 模块
- DRM 模块总览
- DeviceController - DRM 设备控制器
- DrmLayer - DRM 图层管理
- PlanesCompositor - DRM 平面合成器
- DrmBpp - DRM 格式定义
NET 模块
- NET 模块总览
- TcpServer - TCP 服务器
- SocketConnection - Socket 连接管理
- CommandHandler - 命令处理器
- DataPacket - 数据包
V4L2 模块
- V4L2 模块总览
- CameraController - V4L2 摄像头控制器
- Frame - V4L2 帧数据结构
- FormatTool - V4L2 格式工具
- Exception - V4L2 异常类
V4L2Param 模块
- V4L2Param 模块总览
- ParamControl - 参数控制
- ParamLogger - 参数日志
- ParamProcessor - 参数处理器
RGA 模块
- RGA 模块总览
- RgaConverter - RGA 转换器
- RgaProcessor - RGA 处理器
- FormatTool - RGA 格式工具
MPP 模块
- MPP 模块总览
- EncoderContext - 编码器上下文
- EncoderCore - 编码器核心
- JpegEncoder - JPEG 编码器
- StreamWriter - 流写入器
- MppResourceGuard - MPP 资源守护
- FileTools - 文件工具
- FormatTool - 格式工具
Sys 模块
- Sys 模块总览
- CpuMonitor - CPU 监控器
- MemoryMonitor - 内存监控器
- Base - 基础类
Mouse 模块
- Mouse 模块总览
- Watcher - 鼠标监视器
Utils 模块
- Utils 模块总览
- AsyncThreadPool - 异步线程池
- ConcurrentQueue - 并发队列
- FdWrapper - 文件描述符包装器
- FenceWatcher - 围栏监视器
- FixedSizePool - 固定大小对象池
- Logger - 日志记录器
- ObjectsPool - 对象池
- OrderedQueue - 有序队列
- ProgressBar - 进度条
- SafeQueue - 安全队列
- SharedBufferState - 共享缓冲区状态
- SimpleVariant - 简单变体类型
- ThreadPauser - 线程暂停器
- ThreadUtils - 线程工具
- Types - 类型定义
- UdevMonitor - Udev 监视器