linuxiostat

iotop does not show any disk read statistics


I am running a test to check disk read statistics. Here is the code for the same:

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

int main(int argc, char* argv)
{
int count=1000, size;
char block[4096]="0";
int fd = open("file1.txt",O_RDONLY | O_SYNC);
//int pid = getpid();
system("pid=$(ps -a | grep 'a.out' | awk '{print $1}'); iotop -bokp $pid > test1c.out &");
system("echo 'Starts reading in 10'");
srand(time(NULL));
system("sleep 1");
    while(count--){
      int random = (rand()%16)*666;
      printf("%d;",random);
      lseek(fd, random, SEEK_SET);
      size = read(fd,block,4096);
      printf("Number of bytes read: %d\n", size);
      fsync(fd);
      //printf("Read 4kb from the file.\n");
    }
system("sleep 1");
system("killall iotop");
}

As you can see, I am opening a large file, getting the PID of my a.out file, and passing it to iotop. After that I am randomly seeking to a 4kb block in the file and reading data.

If you run this code on your system, you'll realize that iotop output shows 0kb reads throughout, which makes no sense. Am I doing something wrong?


Solution

  • Clearing the caches solved the issue. I found the script for clearing caches on this page:

    https://www.tecmint.com/clear-ram-memory-cache-buffer-and-swap-space-on-linux/

    sync; echo 1 > /proc/sys/vm/drop_caches
    
    sync; echo 2 > /proc/sys/vm/drop_caches
    
    sync; echo 3 > /proc/sys/vm/drop_caches
    

    Does the trick!