Skip to content

Commit 0961525

Browse files
nirsebblake
authored andcommitted
qemu-nbd: Change default cache mode to writeback
Both qemu and qemu-img use writeback cache mode by default, which is already documented in qemu(1). qemu-nbd uses writethrough cache mode by default, and the default cache mode is not documented. According to the qemu-nbd(8): --cache=CACHE The cache mode to be used with the file. See the documentation of the emulator's -drive cache=... option for allowed values. qemu(1) says: The default mode is cache=writeback. So users have no reason to assume that qemu-nbd is using writethough cache mode. The only hint is the painfully slow writing when using the defaults. Looking in git history, it seems that qemu used writethrough in the past to support broken guests that did not flush data properly, or could not flush due to limitations in qemu. But qemu-nbd clients can use NBD_CMD_FLUSH to flush data, so using writethrough does not help anyone. Change the default cache mode to writback, and document the default and available values properly in the online help and manual. With this change converting image via qemu-nbd is 3.5 times faster. $ qemu-img create dst.img 50g $ qemu-nbd -t -f raw -k /tmp/nbd.sock dst.img Before this change: $ hyperfine -r3 "./qemu-img convert -p -f raw -O raw -T none -W fedora34.img nbd+unix:///?socket=/tmp/nbd.sock" Benchmark espressif#1: ./qemu-img convert -p -f raw -O raw -T none -W fedora34.img nbd+unix:///?socket=/tmp/nbd.sock Time (mean ± σ): 83.639 s ± 5.970 s [User: 2.733 s, System: 6.112 s] Range (min … max): 76.749 s … 87.245 s 3 runs After this change: $ hyperfine -r3 "./qemu-img convert -p -f raw -O raw -T none -W fedora34.img nbd+unix:///?socket=/tmp/nbd.sock" Benchmark espressif#1: ./qemu-img convert -p -f raw -O raw -T none -W fedora34.img nbd+unix:///?socket=/tmp/nbd.sock Time (mean ± σ): 23.522 s ± 0.433 s [User: 2.083 s, System: 5.475 s] Range (min … max): 23.234 s … 24.019 s 3 runs Users can avoid the issue by using --cache=writeback[1] but the defaults should give good performance for the common use case. [1] https://bugzilla.redhat.com/1990656 Signed-off-by: Nir Soffer <[email protected]> Message-Id: <[email protected]> Reviewed-by: Eric Blake <[email protected]> CC: [email protected] Signed-off-by: Eric Blake <[email protected]>
1 parent 6b54a31 commit 0961525

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

docs/tools/qemu-nbd.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,10 @@ driver options if ``--image-opts`` is specified.
9999

100100
.. option:: --cache=CACHE
101101

102-
The cache mode to be used with the file. See the documentation of
103-
the emulator's ``-drive cache=...`` option for allowed values.
102+
The cache mode to be used with the file. Valid values are:
103+
``none``, ``writeback`` (the default), ``writethrough``,
104+
``directsync`` and ``unsafe``. See the documentation of
105+
the emulator's ``-drive cache=...`` option for more info.
104106

105107
.. option:: -n, --nocache
106108

qemu-nbd.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ static void usage(const char *name)
135135
" 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
136136
" '[ID_OR_NAME]'\n"
137137
" -n, --nocache disable host cache\n"
138-
" --cache=MODE set cache mode (none, writeback, ...)\n"
138+
" --cache=MODE set cache mode used to access the disk image, the\n"
139+
" valid options are: 'none', 'writeback' (default),\n"
140+
" 'writethrough', 'directsync' and 'unsafe'\n"
139141
" --aio=MODE set AIO mode (native, io_uring or threads)\n"
140142
" --discard=MODE set discard mode (ignore, unmap)\n"
141143
" --detect-zeroes=MODE set detect-zeroes mode (off, on, unmap)\n"
@@ -552,7 +554,7 @@ int main(int argc, char **argv)
552554
bool alloc_depth = false;
553555
const char *tlscredsid = NULL;
554556
bool imageOpts = false;
555-
bool writethrough = true;
557+
bool writethrough = false; /* Client will flush as needed. */
556558
bool fork_process = false;
557559
bool list = false;
558560
int old_stderr = -1;

0 commit comments

Comments
 (0)