c - Programatically Determining the File System Block Size -
this code i'm using time how time takes read file varying amount of bytes read.
for(int = 0; < trials; i++){ fd = open(argv[1], o_rdonly); //set file offset random position on file //but still multiple of current current test block size, //simulating jumps of given test block size //trying avoid prefetch lseek(fd, test_block * rand() % (fs / test_block), seek_set); //how time takes read `test_block` bytes clock_gettime(clock_monotonic, &ts_ini); ssize_t bytes_read = read(fd, buffer, test_block); clock_gettime(clock_monotonic, &ts_end); if(bytes_read > 0){ accum += (((double)(ts_end.tv_sec - ts_ini.tv_sec)) + (ts_end.tv_nsec - ts_ini.tv_nsec)/nano_to_sec) / trials; } //closing file after each trial release resources close(fd); }
now, if run program little shell script:
echo "block size(bytes) | avg. time(seconds)" block in 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 ./bin/blocksize /tmp/random_gen $block done
i result:
block size(bytes) | avg. time(seconds) 4 | 0.002927567500 8 | 0.003120735600 16 | 0.004888980800 32 | 0.003885210600 64 | 0.003578379700 128 | 0.001272970500 256 | 0.004926633700 512 | 0.001281894000 1024 | 0.000243394200 2048 | 0.000175361100 4096 | 0.000001048200 8192 | 0.000001938000 16384 | 0.000003214000
with result i'm assuming block size of system 4096 bytes (which agrees dumpe2fs
), because @ point doesn't need additional operations, pass block got file, fast, , after times duplicates. (this guess)
but here weird part, if modify sh script little bit, adding clean caches before each execution, this:
echo "block size(bytes) | avg. time(seconds)" block in 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 echo "echo 3 > /proc/sys/vm/drop_caches" | sudo sh ./bin/blocksize /tmp/random_gen $block done
then happens:
block size(bytes) | avg. time(seconds) 4 | 0.006217417300 8 | 0.003913319300 16 | 0.004674101500 32 | 0.005444699600 64 | 0.005125086700 128 | 0.004965967700 256 | 0.002433360800 512 | 0.002100266600 1024 | 0.002221131400 2048 | 0.001623008600 4096 | 0.001936151500 8192 | 0.001391976900 16384 | 0.001270749800
which doesn't make sense me. why times keeps decreasing test block size increases when clean cache first?
running on ubuntu 14.04lts 64bit
some points:
it possible filesystems have different block sizes on 1 system.
when rereading same file improved times because of caching. modern hd devices have onboard cache, , os has cache well.
posix provides standard way filesystem information block size: stavfs system call. stat, returns struct. shows 1 on system, each implementation may have extra/different fields, yours may differ:
u_long f_bsize; /* preferred file system block size */ u_long f_frsize; /* fundamental filesystem block (size if supported) */ fsblkcnt_t f_blocks; /* total # of blocks on file system in units of f_frsize */ fsblkcnt_t f_bfree; /* total # of free blocks */ fsblkcnt_t f_bavail; /* # of free blocks avail non-privileged user */ fsfilcnt_t f_files; /* total # of file nodes (inodes) */ fsfilcnt_t f_ffree; /* total # of free file nodes */ fsfilcnt_t f_favail; /* # of inodes avail non-privileged user*/ u_long f_fsid; /* file system id (dev now) */ char f_basetype[fstypsz]; /* target fs type name, null-terminated */ u_long f_flag; /* bit mask of flags */ u_long f_namemax; /* maximum file name length */ char f_fstr[32]; /* file system specific string */ u_long f_filler[16]; /* reserved future expansion */
http://pubs.opengroup.org/onlinepubs/009695399/basedefs/sys/statvfs.h.html
Comments
Post a Comment