返回题库

进程 CPU 使用率

Process Cpu Usage

专题
Systems & Architecture / 系统与架构
难度
L1
来源
MyntBit

题目详情

低延迟交易系统需要严格的性能监控,确保确定性执行时间和防止资源耗尽。在 Linux 环境中,量化开发者常与 /proc 伪文件系统交互提取进程级 CPU 和内存使用统计。

任务:实现 ProcessMonitor 类,从 /proc/self/stat 和 /proc/self/status 读取 CPU 使用率(用户态和内核态时间)和内存使用(RSS 和 VMS)。提供 getCpuUsage() 和 getMemoryUsage() 方法。

英文原题

Low-latency trading systems require rigorous performance monitoring to ensure deterministic execution times and prevent resource exhaustion. In Linux environments, quantitative developers frequently interact with the /proc pseudo-filesystem to extract telemetry data without the overhead of external monitoring agents.
Task
Implement the calculateUsage method to determine a process's CPU usage percentage over an interval. The function receives two stat file snapshots (stat1, stat2), the elapsed_t

解析

问题分析

精确测量进程 CPU 使用率对性能调优至关重要。Linux /proc/self/stat 提供进程的 utime(用户态 CPU 滴答数)和 stime(内核态滴答数),两次采样间的差值除以时钟滴答频率即为 CPU 使用率。

实现

struct CpuUsage {
    unsigned long long utime, stime;
    static CpuUsage read() {
        std::ifstream f("/proc/self/stat");
        std::string _; CpuUsage u;
        for (int i = 0; i < 13; ++i) f >> _;  // 跳过前 13 个字段
        f >> u.utime >> u.stime;
        return u;
    }
};
double cpuPercent(const CpuUsage& prev, const CpuUsage& curr, double elapsed_sec) {
    long long diff = (curr.utime - prev.utime) + (curr.stime - prev.stime);
    return 100.0 * diff / (elapsed_sec * sysconf(_SC_CLK_TCK));
}

复杂度与边界

  • 时间复杂度:read O(1) 文件读取
  • 空间复杂度:O(1)
  • 边界条件:(1) CLK_TCK 通常为 100 (2) 短时间内采样精度受限于滴答粒度 (3) 多核 CPU 使用率可超过 100%

英文解析

Analysis

Precise process CPU usage measurement is critical for performance tuning. Linux /proc/self/stat provides utime (user-mode CPU ticks) and stime (kernel-mode ticks). The difference between two samples divided by clock tick frequency yields CPU utilization percentage.

Solution

struct CpuUsage {
    unsigned long long utime, stime;
    static CpuUsage read() {
        std::ifstream f("/proc/self/stat");
        std::string _; CpuUsage u;
        for (int i = 0; i < 13; ++i) f >> _;  // Skip first 13 fields
        f >> u.utime >> u.stime;
        return u;
    }
};
double cpuPercent(const CpuUsage& prev, const CpuUsage& curr, double elapsed_sec) {
    long long diff = (curr.utime - prev.utime) + (curr.stime - prev.stime);
    return 100.0 * diff / (elapsed_sec * sysconf(_SC_CLK_TCK));
}

Complexity & Edge Cases

  • Time complexity: read O(1) file read
  • Space complexity: O(1)
  • Edge cases: (1) CLK_TCK is typically 100 (2) Short-interval sampling precision is limited by tick granularity (3) Multi-core CPU usage can exceed 100%

Verification

Sample CPU usage during busy loop and idle periods. Verify busy loop yields high percentage, idle yields near-zero. Test multi-core scenario where usage exceeds 100%.

Key Considerations

/proc/self/stat provides the most accurate per-process CPU measurement on Linux. In trading system monitoring, tracking user vs kernel time separately reveals whether CPU is consumed by strategy logic (user) or I/O/syscalls (kernel), guiding optimization efforts.