Programmatic resource monitoring per process in Linux

后端 未结 1 679
失恋的感觉
失恋的感觉 2021-02-14 21:44

I want to know if there is an efficient solution to monitor a process resource consumption (cpu, memory, network bandwidth) in Linux. I want to write a daemon in C++ that does t

1条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-14 22:36

    /usr/src/linux/Documentation/accounting/taskstats.txt

    Taskstats is a netlink-based interface for sending per-task and per-process statistics from the kernel to userspace.

    Taskstats was designed for the following benefits:

    • efficiently provide statistics during lifetime of a task and on its exit
    • unified interface for multiple accounting subsystems
    • extensibility for use by future accounting patches

    This interface lets you monitor CPU, memory, and I/O usage by processes of your choosing. You only need to set up and receive messages on a single socket.

    This does not differentiate (for example) disk I/O versus network I/O. If that's important to you, you might go with a LD_PRELOAD interception library that tracks socket operations. Assuming that you can control the startup of the programs you wish to observe and that they won't do trickery behind your back, of course.

    I can't think of any light-weight solutions if those still fail, but linux-audit can globally trace syscalls, which seems a fair bit more direct than re-capturing and analyzing your own network traffic.

    0 讨论(0)
提交回复
热议问题