linux 获取一段时间间隔的内存高水位线

idfiyjo8  于 2023-02-15  发布在  Linux
关注(0)|答案(1)|浏览(179)

我试图得到一个长时间运行的linux进程在短时间间隔内使用的最大内存量,例如:

resetmaxrss(); // hypothetical new command
void* foo = malloc(4096);
free(foo);
getrusage(...); // 'ru_maxrss' reports 4096 plus whatever else is alive

resetmaxrss();
void* bar = malloc(2048);
free(bar);
getrusage(...); // 'ru_maxrss' reports 2048 + whatever, *not* 4096

我发现并排除的选项:

  • 无法重置getrusage的最大RSS。
  • cgmemtime似乎在幕后使用了wait4,因此无法在进程运行时对其进行查询。
  • tstime报告退出的进程,因此在进程运行时对其进行查询也是不可行的。

其他选择,没有一个是好的:

  • 投票。很容易错过我们的分配。
  • 测试我们的代码。我们不能访问所有正在使用的内存分配器,所以这不会很优雅或直接。我也宁愿使用操作系统报告的值来保证准确性。

除了给Linux内核打补丁之外,还有什么方法可以做到这一点吗?

wyyhbhjk

wyyhbhjk1#

事实证明,自Linux 4.0以来,RSS峰值 * 可以 * 重置:

/proc/[pid]/clear_refs (since Linux 2.6.22)

    This is a write-only file, writable only by owner of the
    process.

    The following values may be written to the file:

    [snip]

    5 (since Linux 4.0)
        Reset the peak resident set size ("high water mark") to
        the process's current resident set size value.

该HWM/峰值RSS可通过/proc/[pid]/status-〉VmHWMgetrusage()读取。
补丁RFC

相关问题