0

I am trying to trace the total data that is written to or read from a disk for a particular process in Linux. Using the dstat tool, I am able to trace system-wide read, write calls, by using dstat -d. Using strace -e trace=read,write, I am able to trace the returning values of the system calls.

Here's a sample program for which I want to get real system read-write values (that includes the metadata written and read to and from the disk):

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

int main(){

char block[4096]="0";
int count=500;
int fd, size;

for(int i=0;i<4096;i++)
{
  char a='0';
  block[i]=a;
}

fd = open("file.txt",O_CREAT|O_WRONLY, 0644);

while(count--){
  size = write(fd,block,4096);
}
fsync(fd); //Flush all data to disk
close(fd);

Tools like iotop are also not useful since they give constantly changing values. The dstat -d option is the closest I've gotten to tracing the real read, write values, but I want to narrow it down to one specific process only and dstat does not have such an option.

Thank you for your help!

Saunved Mutalik
  • 381
  • 2
  • 19

1 Answers1

0

Well, it's not perfect, but you can still use strace in the following manner:

strace -o /tmp/out -e trace=read,write -p $(pidof name)  &

and on side you can run the following sequence in a loop every one second:

awk {'print $NF'} /tmp/out > /tmp/out2;
echo "" > /tmp/out;
awk '{ sum += $1 } END { print sum }' /tmp/out2

Other considerations:

  • You might need additional cleaning to the strace's ouput (For syscall failure, etc.).
  • read/write are not the only io relevant syscalls, but I assume you can adjust the trace options for the process that interest you.
  • If you have a multi-threaded program, you might want to add the "-f" strace flag.
  • read/write are not nesserily relevant for disk io, they can also be relevant for network io.
Eytan Naim
  • 159
  • 14