You can use open
, read
, and write
calls for direct measurement of disk I/O speed, avoiding automatic stream buffering (like fopen
).
For read
and write
you should verify bytes read and written.
To time just use clock_gettime(CLOCK_PROCESS_CPUTIME_ID,*tp)
or CLOCK_THREAD_CPUTIME_ID
(see this question). I say that because Linux includes kernel/system time on behalf of the process (this is explicit on the man entry for clock_gettime
only on OpenBSD). You may want to make sure you are filling the disk cache, and potentially the kernel disk page cache, if you want to measure continuous (non-burst) speed. This way your call to open
will return only after completing file write.
Only using the above process will give you the results you would get in normal usage using kernel disk page caching. You can further bypass kernel page caching as follows. But keep in mind that these are "raw" results and will not necessarily correspond to results when you are using kernel page caching, as Basile Starynkevitch described.
Call open
with O_SYNC|O_DIRECT
operating mode flags (see this question). O_DIRECT
bypasses kernel page caching. This way your data will be confirmed to be completely transferred to and from disk rather than computer RAM cache.
O_DIRECT (since Linux 2.4.10)
Try to minimize cache effects of the I/O to and from this
file. In general this will degrade performance, but it is
useful in special situations, such as when applications do
their own caching. File I/O is done directly to/from user-
space buffers. The O_DIRECT flag on its own makes an effort
to transfer data synchronously, but does not give the
guarantees of the O_SYNC flag that data and necessary metadata
are transferred. To guarantee synchronous I/O, O_SYNC must be
used in addition to O_DIRECT. See NOTES below for further
discussion.
Unless I am mistaken you will not have to worry about the kernel scheduling another process onto your CPU in testing unless most of your CPUs are busy. Even if it does schedule another process it may return to your process as soon as the synchronous write has completed (please let me know as a comment if you know). Therefore using CLOCK_MONOTONIC_RAW
instead may not matter. cpuset
might be useful.
Consult Linux man
entries and The GNU C Library Reference Manual. Also see fsync
and this question about how long page-cached writes take to hit disk.