Note that there are some explanatory texts on larger screens.

plurals
  1. POTesting write/read speed on NTFS, FAT, EXT4
    primarykey
    data
    text
    <p>I have to write a program in C (or C++) in <code>Linux</code> that will tests write and read speed on different file systems. I have to be sure that all data are written to the disk (not in cache). </p> <p>So my first question - what function should I use to open a new file? I used before <code>open</code> function with parameters <code>O_DIRECT</code> and <code>O_SYNC</code> and everything was fine except one thing - writing small files like 1KB was extremely slow, something like 0.01MB/s.</p> <p>I tried to use <code>fopen</code> function instead <code>open</code>, and <code>fflush</code> function to be sure that all data writes direct to the disk, and I tested it first on <code>FAT32</code> file system. 1000 files with 1KB was written to disk (here SD card) in 5 sec. something like 0.18MB/s, and I think that is correct.</p> <p>Now the problem occurs when testing <code>EXT4</code> and <code>NTFS</code> file systems. On <code>EXT4</code>. 1KB files was written something like 12MB/s (wrong), when testing 100KB transfer was 180MB/s (terribly wrong, my SD card has transfer rate only 20MB/s).</p> <p>My actually code for write files looks like this:</p> <pre><code>clock_gettime(CLOCK_REALTIME, &amp;ts); for ( int i = 0; i &lt; amount; ++i) { p = fopen(buffer2, "w+"); fwrite(buff, size*1024, 1, p); if ( fflush(p) != 0 ) { cout &lt;&lt; "fflush error"; return 0; } fclose(p); } clock_gettime(CLOCK_REALTIME, &amp;ts2); time2 = diff2(ts,ts2); </code></pre> <p>works only good for <code>FAT32</code> file system. The second code (used before) looks like this:</p> <pre><code>for ( int i = 0; i &lt; amount; ++i) { int fd = open(buffer2, O_WRONLY | O_CREAT, 0777); if ( error(fd, "open") ) return false; if ( (write(fd, buff, size*1024)) &lt; 0 ) { perror("write error"); return 0; } if ( (fsync(fd)) == -1 ) { perror("fsync"); return 0; } close(fd); } </code></pre> <p>works for all file systems but small files writes extremely slow. Maybe I should use different code for different file system? Any ideas?</p> <p><code>EDIT</code>:</p> <p>I have found why writing small files is slow. It is because of <code>fsync</code> function, and on different file systems it takes different time. I am calling <code>fsync</code> every write, so here is the problem. </p> <p>Is there any way to call it at the end, when all files are written? Or maybe every few seconds? Does I have to use different thread?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload