Linux 进程及其子进程读取/写入的总字节数

发布于 2024-12-27 03:19:46 字数 447 浏览 1 评论 0原文

我想打印 Linux 进程读/写的总字节数。例如,我运行

gcc -c a.c

并想查看 GCC(包括其子级)总共向 Linux 内核请求了多少字节,以及它们向内核发送了多少字节。

该问题的不完整解决方案是:

  • /proc/PID/io中的字段rcharwchar显示读/写的数量到目前为止的字节数。它不考虑子进程。一旦进程终止,它就会丢失。

  • 诸如strace之类的工具可用于打印进程及其子进程的系统调用(例如:readwrite syscalls),但无法聚合读/写的字节数。

如何打印Linux进程及其子进程读/写的总字节数?

I would like to print the total number of bytes read/written by a Linux process. For example, I run

gcc -c a.c

and would like to see how many bytes in total did GCC, including its children, request from the Linux kernel and how many bytes they sent to the kernel.

Incomplete solutions to this problem are:

  • The fields rchar and wchar in /proc/PID/io show the number of read/written bytes so far. It does not account for child processes. It is lost as soon as the process terminates.

  • A tool such as strace can be used to print out the syscalls of a process and its children (such as: read, write syscalls), but it is unable to aggregate the number of bytes read/written.

How to print the total number of bytes read/written by a Linux process and its child processes?

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

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

发布评论

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

评论(2

若沐 2025-01-03 03:19:46

有点 awk,strace 就是你想要的。

strace -e trace=read,write -o ls.log ls

为您提供读取和写入系统调用的日志。现在您可以获取此日志并对最后一列进行求和,如下所示

cat ls.log | grep read | awk 'BEGIN {FS="="}{ sum += $2} END {print sum}'

您可能不想更改 grep 以仅匹配行开头的读取。

A little awk, and strace is what you want.

strace -e trace=read,write -o ls.log ls

gives you a log of the read and write syscalls. Now you can take this log and sum the last column like this

cat ls.log | grep read | awk 'BEGIN {FS="="}{ sum += $2} END {print sum}'

You might wan't to change the grep to match only a read at the beginning of the line.

半枫 2025-01-03 03:19:46

你可以看一下iotop,这是一个类似top的工具,可以显示磁盘消耗情况每个进程的数据(实时以及总的写入和读取)。

编辑:

您还可以检查sysstat,它看起来对于监控Linux机器非常强大。根据文档:

可以监控大量不同的指标:

  1. 输入/输出和传输速率统计信息(全局、每个设备、每个分区、每个网络文件系统和每个 Linux 任务/PID)。
  2. CPU 统计信息(全局、每个 CPU 和每个 Linux 任务/PID),包括对虚拟化架构的支持。
  3. 内存、大页和交换空间利用率统计信息。
  4. 虚拟内存、分页和故障统计。
  5. 每任务(每 PID)内存和页面错误统计信息。
  6. 任务及其所有子任务的全局 CPU 和页面错误统计信息。
  7. 流程创建活动。
  8. 中断统计信息(全局、每个 CPU 和每个中断,包括潜在的 APIC 中断源、硬件和软件中断)。
  9. 广泛的网络统计数据:网络接口活动(每秒接收和传输的数据包数和 kB 数等),包括
    网络设备故障; IP、TCP 的网络流量统计
    基于SNMPv2标准的ICMP和UDP协议;支持
    IPv6相关协议。
  10. NFS 服务器和客户端活动。
  11. 套接字统计信息。
  12. 运行队列和系统负载统计信息。
  13. 内核内部表利用率统计信息。
  14. 系统和每个 Linux 任务切换活动。
  15. 交换统计数据。
  16. TTY 设备活动。
  17. 电源管理统计数据(瞬时和平均 CPU 时钟频率、风扇速度、设备温度、电压输入、USB
    插入系统的设备)。

在这里您将找到 sar 的一些使用示例(主要命令sysstat 包的)。

You could take a look to iotop, it is a top-like tool that can display the disk consumption of each process (real time and total written and read).

EDIT:

You can also check sysstat which looks very powerfull for monitoring a linux box. According to the documentation :

Can monitor a huge number of different metrics:

  1. Input / Output and transfer rate statistics (global, per device, per partition, per network filesystem and per Linux task / PID).
  2. CPU statistics (global, per CPU and per Linux task / PID), including support for virtualization architectures.
  3. Memory, hugepages and swap space utilization statistics.
  4. Virtual memory, paging and fault statistics.
  5. Per-task (per-PID) memory and page fault statistics.
  6. Global CPU and page fault statistics for tasks and all their children.
  7. Process creation activity.
  8. Interrupt statistics (global, per CPU and per interrupt, including potential APIC interrupt sources, hardware and software interrupts).
  9. Extensive network statistics: network interface activity (number of packets and kB received and transmitted per second, etc.) including
    failures from network devices; network traffic statistics for IP, TCP,
    ICMP and UDP protocols based on SNMPv2 standards; support for
    IPv6-related protocols.
  10. NFS server and client activity.
  11. Socket statistics.
  12. Run queue and system load statistics.
  13. Kernel internal tables utilization statistics.
  14. System and per Linux task switching activity.
  15. Swapping statistics.
  16. TTY device activity.
  17. Power management statistics (instantaneous and average CPU clock frequency, fans speed, devices temperature, voltage inputs, USB
    devices plugged into the system).

And here you will find some examples of usage of sar (the main command of the sysstat package).

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