Linux进程及其子进程读取/写入的总字节数 [英] Total number of bytes read/written by a Linux process and its children
问题描述
我想打印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
并希望看到GCC(包括其子代)从Linux内核请求了多少字节以及它们发送到内核的字节数。
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:
-
字段
rchar
和wchar
在/ proc / PID / io
中显示读/写字节数至今。它不考虑子进程。一旦流程终止,它就会丢失。
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.
可以使用诸如 strace
之类的工具打印出进程及其子进程的系统调用(例如:读取
,写入
系统调用),但它无法聚合读/写的字节数。
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.
如何打印读取/写入的总字节数Linux进程及其子进程?
How to print the total number of bytes read/written by a Linux process and its child processes?
推荐答案
有点awk,strace就是你想要的。
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}'
您可能不想更改grep以仅匹配行开头的读数。
You might wan't to change the grep to match only a read at the beginning of the line.
这篇关于Linux进程及其子进程读取/写入的总字节数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!