Linux进程及其子进程读取/写入的总字节数 [英] Total number of bytes read/written by a Linux process and its children

查看:263
本文介绍了Linux进程及其子进程读取/写入的总字节数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印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 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.

可以使用诸如 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屋!

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