Linux 中每个进程的编程资源监控

发布于 2024-08-09 22:44:40 字数 395 浏览 2 评论 0原文

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

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

您知道是否有更有效的替代方法来替代这些方法或任何处理此问题的库?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情魔剑神 2024-08-16 22:44:40

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

Taskstats 是一个基于 netlink 的接口,用于发送每个任务和
从内核到用户空间的每个进程统计信息。

Taskstats 的设计具有以下优点:

  • 在任务生命周期内及其退出时有效提供统计信息
  • 多个会计子系统的统一界面
  • 可供未来会计补丁使用的可扩展性

该界面可让您监控进程的 CPU、内存和 I/O 使用情况你的选择。您只需要在单个套接字上设置和接收消息。

这并不区分(例如)磁盘 I/O 与网络 I/O。如果这对您很重要,您可能会使用跟踪套接字操作的 LD_PRELOAD 拦截库。当然,假设您可以控制您想要观察的程序的启动,并且它们不会在您背后进行欺骗。

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

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

小情绪 2024-08-16 22:44:40

查看 linux 跟踪工具包 (LTTng)。它将跟踪点插入内核并进行一些后处理以获得您所询问的一些统计信息。如果您捕获所有内容,则跟踪文件会变得很大,但如果您限制所配置的事件类型,则可以使事情变得易于管理。

http://lttng.org 了解更多信息...

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

你是暖光i 2024-08-16 22:44:40

关于网络带宽: 这个超级用户答案描述了处理 /proc/net/tcp 以收集网络带宽使用情况。

我知道 iptables 可用于进行网络记帐(请参见,例如 LWN 的Linux.com 的,或 Shorewall 的 文章),但我没有看到任何实用的方法可以在每个进程的基础上进行核算。

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.

软甜啾 2024-08-16 22:44:40

我只是在寻找同一件事的答案时遇到了这个。请注意 - 使用 /proc 文件系统时,您不必在每次读取后关闭文件。您可以保持文件打开,每次读取时您都会获得新的统计信息...所以,您不应该在每次想要获取统计信息时打开和关闭文件...我有这个工作如果你想要一个例子,node.js 上的 javascript...

i just came across this as i was looking for answers to the same thing. just a note - when using /proc filesystem, you do not have to close the file after each read. you can keep the file open and each time you do a read you will get new statistics... so, you shouldn't have the overhead of opening and closing each time you want to get the stats... i have this working in javascript on node.js if you want an example...

北城半夏 2024-08-16 22:44:40

读取 /proc 最终是监视各个进程的 CPU 和内存使用情况而无需将代码注入内核的唯一方法。如果您查看 top(1),您会发现它每秒都会读取 /proc 中的大量文件。所有检索此类信息的用户模式工具和库都必须从 /proc 获取它。

与网络带宽使用一样,有多种方法,它们或多或少都可以归结为捕获进出盒子的所有网络流量。您还可以考虑编写一个特殊的 netfilter (iptables) 模块,它可以完全执行您需要的计数类型,而无需流量捕获的开销。

Reading /proc is ultimately the only way to monitor CPU and memory usage by individual processes without injecting your code into the kernel. If you look at top(1), you'll see reading lots of files in /proc is exactly what it does every second. All user-mode tools and libraries that retrive this sort of information have to get it from /proc.

As with network bandwidth usage, there are several approaches, which all more or less boil down to capturing all network traffic in and out of the box. You can also consider writing a special netfilter (iptables) module that does exactly the type of counting you need without the overhead of traffic capturing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文