How to measure the closing time of an I / O file

I am writing some test programs in C to solve the following problems:

  • The speed at which you can read from a network drive. Print the seconds required to read 8192 bytes.
  • The speed at which you can read from the local / tmp directory on your local machine. Print the seconds required to read 8192 bytes.
  • The speed at which you can read from the disk page cache. Print the seconds required to read 8192 bytes.
  • The speed at which you can write to a network drive. Print out the seconds it takes to write 8192 bytes.
  • The speed at which you can write to the local / tmp directory on your local machine. Print the seconds it takes to write 8192 bytes.

The goal is to only measure the time it takes to read or write a file (using read

and write

to avoid any buffering time from fread

)

My general approach for 1 and 2 is to create an 8192 byte file and write it to disk (be it a local directory or a network drive) and then call sleep(10)

to wait for the page cache to be flushed which I measure the actual I / O time, and no I / O cache. I then measure the time it takes to run the empty loop several thousand times, then the time it takes to read 8192 bytes, then subtract the two, divided by the average of all iterations. My code for this looks like this:

struct timespec emptyLoop1, emptyLoop2;
clock_gettime(CLOCK_REALTIME, &emptyLoop1);
for(i = 0, j = 0; i < ITERATIONS; i++) {
    j+=i*i;
}
clock_gettime(CLOCK_REALTIME, &emptyLoop2);
char readbuf[NUM_BYTES];

struct timespec beforeRead, afterRead;
clock_gettime(CLOCK_REALTIME, &beforeRead);
for(i = 0, j = 0; i < ITERATIONS; i++){
    j+=i*i;
    read(fd, readbuf, NUM_BYTES);
}

      

Is that enough to accurately measure reading times from these locations?

Further I am confused about how to read from the page cache. Where does this exist on disk and how do I access it? Finally, there are some tricks for 4 and 5 that seem to be a lot harder than they seem, but I'm not sure what I'm missing.

+3


source to share


1 answer


Below is the file read function to use or not use the memory cache. If you're writing files first, you need similar open statements. Note that direct I / O cannot be used over a LAN and caching can be unpredictable. More information and access to source and runtime files can be found at http://www.roylongbottom.org.uk/linux_disk_usb_lan_benchmarks.htm .



int readFile(int use, int dsize)
{
    int p;

    if (useCache)
    {
          handle = open(testFile, O_RDONLY);
    }
    else
    {
          handle = open(testFile, O_RDONLY | O_DIRECT);
    }

    if (handle == -1)
    {
        printf (" Cannot open data file for reading\n\n");
        fprintf (outfile, " Cannot open data file for reading\n\n");
        fclose(outfile);
        printf(" Press Enter\n");
        g  = getchar();
        return 0;
    }

    for (p=0; p<use; p++)
    {
        if (read(handle, dataIn, dsize) == -1)
        {
            printf (" Error reading file\n\n");
            fprintf (outfile, " Error reading file\n\n");
            fclose(outfile);
            close(handle);
            printf(" Press Enter\n");
            g  = getchar();
            return 0;
        }           

    }
    close(handle);
    return 1;
}

      

+1


source







All Articles