Linux 进程及其子进程读/写的总字节数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8852065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Total number of bytes read/written by a Linux process and its children
提问by
I would like to print the total number of bytes read/written by a Linux process. For example, I run
我想打印 Linux 进程读/写的总字节数。例如,我跑
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.
并且想看看 GCC 总共做了多少字节,包括它的子级,从 Linux 内核请求,以及它们发送到内核的字节数。
Incomplete solutions to this problem are:
这个问题的不完全解决方案是:
The fields
rchar
andwchar
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.
字段
rchar
和wchar
in/proc/PID/io
显示到目前为止读/写的字节数。它不考虑子进程。一旦进程终止,它就会丢失。诸如
strace
可用于打印出进程及其子进程的系统调用(例如:read
、write
系统调用)之类的工具,但它无法汇总读取/写入的字节数。
How to print the total number of bytes read/written by a Linux process and its child processes?
如何打印 Linux 进程及其子进程读/写的总字节数?
采纳答案by shodanex
A little awk, and strace is what you want.
有点 awk,strace 就是你想要的。
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 += } END {print sum}'
You might wan't to change the grep to match only a read at the beginning of the line.
您可能不想更改 grep 以仅匹配行开头的读取。
回答by Cédric Julien
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).
你可以看看iotop,它是一个类似 top 的工具,可以显示每个进程的磁盘消耗(实时和总写入和读取)。
EDIT:
编辑:
You can also check sysstatwhich looks very powerfull for monitoring a linux box. According to the documentation :
您还可以检查sysstat,它看起来非常强大,用于监控 linux 机器。根据文档:
Can monitor a huge number of different metrics:
- Input / Output and transfer rate statistics (global, per device, per partition, per network filesystem and per Linux task / PID).
- CPU statistics (global, per CPU and per Linux task / PID), including support for virtualization architectures.
- Memory, hugepages and swap space utilization statistics.
- Virtual memory, paging and fault statistics.
- Per-task (per-PID) memory and page fault statistics.
- Global CPU and page fault statistics for tasks and all their children.
- Process creation activity.
- Interrupt statistics (global, per CPU and per interrupt, including potential APIC interrupt sources, hardware and software interrupts).
- 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.
- NFS server and client activity.
- Socket statistics.
- Run queue and system load statistics.
- Kernel internal tables utilization statistics.
- System and per Linux task switching activity.
- Swapping statistics.
- TTY device activity.
- Power management statistics (instantaneous and average CPU clock frequency, fans speed, devices temperature, voltage inputs, USB devices plugged into the system).
可以监控大量不同的指标:
- 输入/输出和传输速率统计(全局、每个设备、每个分区、每个网络文件系统和每个 Linux 任务/PID)。
- CPU 统计数据(全局、每个 CPU 和每个 Linux 任务/PID),包括对虚拟化架构的支持。
- 内存、大页面和交换空间利用率统计信息。
- 虚拟内存、分页和故障统计。
- 每任务 (per-PID) 内存和页面错误统计信息。
- 任务及其所有子项的全局 CPU 和页面错误统计信息。
- 流程创建活动。
- 中断统计(全局、每个 CPU 和每个中断,包括潜在的 APIC 中断源、硬件和软件中断)。
- 广泛的网络统计:网络接口活动(每秒接收和传输的数据包数量和 kB 等),包括来自网络设备的故障;基于SNMPv2标准的IP、TCP、ICMP和UDP协议的网络流量统计;支持 IPv6 相关协议。
- NFS 服务器和客户端活动。
- 套接字统计。
- 运行队列和系统负载统计。
- 内核内部表利用率统计。
- 系统和每个 Linux 任务切换活动。
- 交换统计。
- TTY 设备活动。
- 电源管理统计信息(瞬时和平均 CPU 时钟频率、风扇速度、设备温度、电压输入、插入系统的 USB 设备)。
And here you will find some examples of usageof sar (the main command of the sysstat package).