Examining the file system block size

I'll admit this is for a class project first, as it will be pretty obvious. We have to read to check the block size of the filesystem. My problem is that the time taken for this seems to be linear, without any steps as I expected.

I read like this:

double startTime = getticks();
read = fread(x, 1, toRead, fp);
double endTime = getticks();

      

where getticks uses rdtsc commands. I am afraid there is caching / prefetching, which makes the reading take no time during the test. I tried to create a random file between each execution of my program, but that doesn't make my problem easier.

What's the best way to accurately measure time taken to read from disk? I'm sure my block size is 4096, but how can I get the data to support?

+2


source to share


2 answers


You can use the system calls ( open()

, read()

, write()

...) directly to reduce the impact buffering material performed FILE*

. Also, you can use synchronous I / O in some way. One way is to open the file with the flag set O_SYNC

(or O_DIRECT

as per the ephemeral answer). Quoting the Linux man page open

(2):

   O_SYNC The file is opened for synchronous I/O.  Any  write(2)s  on  the
          resulting  file  descriptor will block the calling process until
          the data has been physically written to the underlying hardware.
          But see NOTES below.

      



Other parameters can be setting the file system using -o sync

(see mount

(8)) or setting an attribute S

in the file using the command chattr

(1).

+1


source


The usual way to determine the block size of a filesystem is to ask the filesystem what its size is.

#include <sys/statvfs.h>
#include <stdio.h>
int main() {
    struct statvfs fs_stat;
    statvfs(".", &fs_stat);
    printf("%lu\n", fs_stat.f_bsize);
}

      



But if you really want, open(…,…|O_DIRECT)

or posix_fadvise(…,…,…,POSIX_FADV_DONTNEED)

will try to bypass the kernel cache buffer (not guaranteed).

+2


source







All Articles