Reputation:
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?
Upvotes: 6
Views: 13838
Reputation: 80761
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:
- 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).
And here you will find some examples of usage of sar (the main command of the sysstat package).
Upvotes: 2
Reputation: 15406
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.
Upvotes: 11