From bbc6de1cb86af16cda389c8e18e2d4d4c443b8c5 Mon Sep 17 00:00:00 2001 From: Michał Łyszczek Date: Sun, 9 Jun 2019 19:37:35 +0200 Subject: src/el-file.c: don't flush when no data has been written MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When no data has been written to file, there is not need to perform fsync() syscall, which in this case will only eat cycles for context switch without doing anything. Signed-off-by: Michał Łyszczek --- src/el-file.c | 10 ++++++++++ tst/test-el-file.c | 24 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/el-file.c b/src/el-file.c index dd98392..34d2cbc 100644 --- a/src/el-file.c +++ b/src/el-file.c @@ -554,6 +554,16 @@ int el_file_flush VALID(EBADF, el->fcurrent_log); VALID(EBADF, el->fcurrent_log[0] != '\0'); + if (el->fwritten_after_sync == 0) + { + /* if now writes have been performed between consecutive + * flush, then don't flush - since there is nothing to + * flush anyway + */ + + return 0; + } + /* first flush data from stdio library buffers into kernel */ diff --git a/tst/test-el-file.c b/tst/test-el-file.c index cfd524f..ad98061 100644 --- a/tst/test-el-file.c +++ b/tst/test-el-file.c @@ -1548,6 +1548,29 @@ static void file_sync_via_flush_function(void) } +/* ========================================================================== + ========================================================================== */ + + +static void file_consecutive_sync_with_flush_function(void) +{ + el_option(EL_FSYNC_EVERY, 16); + mt_fok(el_puts(s8)); + mt_fail(file_synced == 0); + mt_fok(el_flush()); + mt_fail(file_synced == 1); + file_synced = 0; + mt_fok(el_flush()); + mt_fail(file_synced == 0); + mt_fok(el_flush()); + mt_fail(file_synced == 0); + mt_fok(el_puts(s8)); + mt_fail(file_synced == 0); + mt_fok(el_flush()); + mt_fail(file_synced == 1); +} + + /* ========================================================================== ========================================================================== */ @@ -1889,6 +1912,7 @@ void el_file_test_group(void) mt_run(file_sync_periodic); mt_run(file_sync_level); mt_run(file_sync_via_flush_function); + mt_run(file_consecutive_sync_with_flush_function); rmdir(WORKDIR); #endif -- cgit v1.2.3-8-gadcc