Linux中每个进程的程序化资源监视 [英] Programmatic resource monitoring per process in Linux

查看:87
本文介绍了Linux中每个进程的程序化资源监视的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种有效的解决方案可以监视Linux中的进程资源消耗(CPU,内存,网络带宽).我想用C ++编写一个守护程序,对某些给定的PID进行监视.据我所知,经典的解决方案是定期从/proc中读取信息,但这似乎不是最有效的方法(它涉及许多系统调用).例如,要每秒监视50个进程的内存使用情况,我必须每秒从/proc打开,读取和关闭50个文件(即150个系统调用).更不用说读取这些文件时涉及的解析.

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.

另一个问题是网络带宽消耗:这对于我要监视的每个进程都无法轻松计算.我认为NetHogs采用的解决方案涉及相当高的开销:它使用libpcap捕获并分析每个数据包,然后为每个数据包确定本地端口并在/proc中进行搜索以找到相应的进程.

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?

推荐答案

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

Taskstats是一个基于Netlink的界面,用于发送每个任务和 从内核到用户空间的每个进程的统计信息.

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

Taskstats的设计具有以下优点:

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

此界面使您可以通过选择的进程监视CPU,内存和I/O的使用情况.您只需要在单个套接字上设置和接收消息即可.

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.

这不会区分(例如)磁盘I/O与网络I/O.如果那对您很重要,则可以使用LD_PRELOAD侦听库来跟踪套接字操作.假定您可以控制希望观察的程序的启动,并且它们当然不会在您的背后作假.

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.

如果这些方法仍然失败,我想不出任何轻便的解决方案,但是 linux-audit 可以全局跟踪系统调用,这似乎比重新捕获和分析您自己的网络流量要直接得多.

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.

这篇关于Linux中每个进程的程序化资源监视的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆