Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatic resource monitoring per process in Linux

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 this monitoring for some given PIDs. From what I know, the classic solution is to periodically read the information from /proc, but this doesn't seem the most efficient way (it involves many system calls). For example to monitor the memory usage every second for 50 processes, I have to open, read and close 50 files (that means 150 system calls) every second from /proc. Not to mention the parsing involved when reading these files.

Another problem is the network bandwidth consumption: this cannot be easily computed for each process I want to monitor. The solution adopted by NetHogs involves a pretty high overhead in my opinion: it captures and analyzes every packet using libpcap, then for each packet the local port is determined and searched in /proc to find the corresponding process.

Do you know if there are more efficient alternatives to these methods presented or any libraries that deal with this problems?

like image 346
tuxx Avatar asked Nov 02 '09 21:11

tuxx


People also ask

How do I monitor a specific process in Linux?

Usually, we can use the Linux built-in top command. This command displays a real-time view of a running system in the command prompt. If we want to have an idea of a single process, we can use the -p parameter.

How do I check system resources in Linux?

Navigate to Show Applications. Enter System Monitor in the search bar and access the application. Select the Resources tab. A graphical overview of your memory consumption in real time, including historical information is displayed.

How do I monitor services in Linux?

To monitor Linux server performance, you'll need to install a node exporter utility that collects multiple hardware-related and kernel-related metrics (CPU, disk utilization, memory, network statistics, etc.), then makes them available to the Prometheus server to scrape.

Which Linux command is used for monitoring network utilization?

nload is a command-line tool that displays the network usage on the system. It belongs to the category of network monitoring tool in Linux that simply sum up all the network traffic on a network interface.


3 Answers

/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.

like image 177
ephemient Avatar answered Oct 27 '22 05:10

ephemient


Take a look at the linux trace toolkit (LTTng). It inserts tracepoints into the kernel and has some post processing to get some of the kind of statistics you're asking about. The trace files get large if you capture everything, but you can keep things manageable if you limit the types of events you arm.

http://lttng.org for more info...

like image 35
Matt Brandt Avatar answered Oct 27 '22 04:10

Matt Brandt


Regarding network bandwidth: This Superuser answer describes processing /proc/net/tcp to collect network bandwidth usage.

I know that iptables can be used to do network accounting (see, e.g., LWN's, Linux.com's, or Shorewall's articles), but I don't see any practical way to do accounting that on a per-process basis.

like image 44
Josh Kelley Avatar answered Oct 27 '22 05:10

Josh Kelley