migration/qemu-file: cleanup and refine qemu-file
cover migration/qemu-file: cleanup and refine qemu-file
Two cleanup:
Patch #1 make code consistent on calling add_to_iovec Patch #2 refine the code to handle the case when buf already flushed
Wei Yang (2):
migration/qemu-file: remove check on writev_buffer in
qemu_put_compression_data
migration/qemu-file: fix potential buf waste for extra buf_index
adjustment
migration/qemu-file.c | 44 +++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-)
Signature · 1 line
2.17.1
PATCH 1/2 migration/qemu-file: remove check on writev_buffer in qemu_put_compression_data
The check of writev_buffer is in qemu_fflush, which means it is not harmful if it is NULL.
And removing it will make the code consistent since all other add_to_iovec() is called without the check.
--- migration/qemu-file.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
migration/qemu-file.c +1 -3
@@ -717,9 +717,7 @@ ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream, }qemu_put_be32 (f, blen); -if (f->ops->writev_buffer) { -add_to_iovec (f, f->buf + f->buf_index, blen, false); - } +add_to_iovec (f, f->buf + f->buf_index, blen, false); f->buf_index += blen;if (f->buf_index ==IO_BUF_SIZE ) {qemu_fflush (f);
Signature · 1 line
2.17.1
Re: [Qemu-devel] [PATCH 1/2] migration/qemu-file: remove check on writev_buffer in qemu_put_compression_data
Quoted 7 lines · * Wei Yang ([email protected]) wrote:
The check of
writev_bufferis inqemu_fflush, which means it is not harmful if it is NULL.And removing it will make the code consistent since all other
add_to_iovec()is called without the check.Signed-off-by Wei Yang <[email protected]>
So I think the real reason here is commit 11808bb which removed support for non-iovec writers.
Quoted 22 lines
--- migration/qemu-file.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)migration/qemu-file.c +1
-3@@ -717,9 +717,7 @@ ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream, }qemu_put_be32 (f, blen); -if (f->ops->writev_buffer) { -add_to_iovec (f, f->buf + f->buf_index, blen, false); - } +add_to_iovec (f, f->buf + f->buf_index, blen, false); f->buf_index += blen;if (f->buf_index ==IO_BUF_SIZE ) {qemu_fflush (f);Signature · 1 line
2.17.1
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
PATCH 2/2 migration/qemu-file: fix potential buf waste for extra buf_index adjustment
In reply to Wei YangIn add_to_iovec(), qemu_fflush() will be called if iovec is full. If this happens, buf_index is reset. Currently, this is not checked and buf_index would always been adjust with buf size.
This is not harmful, but will waste some space in file buffer.
This patch make add_to_iovec() return 1 when it has flushed the file. Then the caller could check the return value to see whether it is necessary to adjust the buf_index any more.
--- migration/qemu-file.c | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-)
migration/qemu-file.c +28 -14
@@ -343,8 +343,16 @@ int qemu_fclose(QEMUFile *f)return ret; } -static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size, - bool may_free) +/* +* Add buf to iovec. Do flush if iovec is full. +* +* Return values: +* 1 iovec is full and flushed +* 0 iovec is not flushed +* +*/ +static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size, + bool may_free) { /* check for adjacent buffer and coalesce them */ if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
@@ -362,7 +370,10 @@ static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,if (f->iovcnt >=MAX_IOV_SIZE ) {qemu_fflush (f); +return 1 ; } + +return 0 ; } void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,
@@ -391,10 +402,11 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size) }memcpy (f->buf + f->buf_index, buf, l); f->bytes_xfer += l; -add_to_iovec (f, f->buf + f->buf_index, l, false); - f->buf_index += l; -if (f->buf_index ==IO_BUF_SIZE ) { -qemu_fflush (f); +if (!add_to_iovec (f, f->buf + f->buf_index, l, false)) { + f->buf_index += l; +if (f->buf_index ==IO_BUF_SIZE ) { +qemu_fflush (f); + } }if (qemu_file_get_error (f)) {break ;
@@ -412,10 +424,11 @@ void qemu_put_byte(QEMUFile *f, int v) f->buf[f->buf_index] = v; f->bytes_xfer++; -add_to_iovec (f, f->buf + f->buf_index,1 , false); - f->buf_index++; -if (f->buf_index ==IO_BUF_SIZE ) { -qemu_fflush (f); +if (!add_to_iovec (f, f->buf + f->buf_index,1 , false)) { + f->buf_index++; +if (f->buf_index ==IO_BUF_SIZE ) { +qemu_fflush (f); + } } }
@@ -717,10 +730,11 @@ ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream, }qemu_put_be32 (f, blen); -add_to_iovec (f, f->buf + f->buf_index, blen, false); - f->buf_index += blen; -if (f->buf_index ==IO_BUF_SIZE ) { -qemu_fflush (f); +if (!add_to_iovec (f, f->buf + f->buf_index, blen, false)) { + f->buf_index += blen; +if (f->buf_index ==IO_BUF_SIZE ) { +qemu_fflush (f); + } }return blen +sizeof (int32_t ); }
Signature · 1 line
2.17.1
Re: [Qemu-devel] [PATCH 2/2] migration/qemu-file: fix potential buf waste for extra buf_index adjustment
(Copying Dan in)
Quoted 5 lines · * Wei Yang ([email protected]) wrote:
In
add_to_iovec(),qemu_fflush()will be called if iovec is full. If this happens,buf_indexis reset. Currently, this is not checked andbuf_indexwould always been adjust with buf size.This is not harmful, but will waste some space in file buffer.
That's a nice find.
Quoted 5 lines
This patch make
add_to_iovec()return 1 when it has flushed the file. Then the caller could check the return value to see whether it is necessary to adjust thebuf_indexany more.Signed-off-by Wei Yang <[email protected]>
(I wonder if there's a way to wrap that little add_to_iovec, check, add to index, flush in a little wrapper).
Dave
Quoted 89 lines
--- migration/qemu-file.c | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-)migration/qemu-file.c +28
-14@@ -343,8 +343,16 @@ int qemu_fclose(QEMUFile *f)return ret; } -static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size, - bool may_free) +/* +* Add buf to iovec. Do flush if iovec is full. +* +* Return values: +* 1 iovec is full and flushed +* 0 iovec is not flushed +* +*/ +static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size, + bool may_free) { /* check for adjacent buffer and coalesce them */ if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +@@ -362,7 +370,10 @@ static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,if (f->iovcnt >=MAX_IOV_SIZE ) {qemu_fflush (f); +return 1 ; } + +return 0 ; } void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,@@ -391,10 +402,11 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size) }memcpy (f->buf + f->buf_index, buf, l); f->bytes_xfer += l; -add_to_iovec (f, f->buf + f->buf_index, l, false); - f->buf_index += l; -if (f->buf_index ==IO_BUF_SIZE ) { -qemu_fflush (f); +if (!add_to_iovec (f, f->buf + f->buf_index, l, false)) { + f->buf_index += l; +if (f->buf_index ==IO_BUF_SIZE ) { +qemu_fflush (f); + } }if (qemu_file_get_error (f)) {break ;@@ -412,10 +424,11 @@ void qemu_put_byte(QEMUFile *f, int v) f->buf[f->buf_index] = v; f->bytes_xfer++; -add_to_iovec (f, f->buf + f->buf_index,1 , false); - f->buf_index++; -if (f->buf_index ==IO_BUF_SIZE ) { -qemu_fflush (f); +if (!add_to_iovec (f, f->buf + f->buf_index,1 , false)) { + f->buf_index++; +if (f->buf_index ==IO_BUF_SIZE ) { +qemu_fflush (f); + } } }@@ -717,10 +730,11 @@ ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream, }qemu_put_be32 (f, blen); -add_to_iovec (f, f->buf + f->buf_index, blen, false); - f->buf_index += blen; -if (f->buf_index ==IO_BUF_SIZE ) { -qemu_fflush (f); +if (!add_to_iovec (f, f->buf + f->buf_index, blen, false)) { + f->buf_index += blen; +if (f->buf_index ==IO_BUF_SIZE ) { +qemu_fflush (f); + } }return blen +sizeof (int32_t ); }Signature · 1 line
2.17.1
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [Qemu-devel] [PATCH 2/2] migration/qemu-file: fix potential buf waste for extra buf_index adjustment
Quoted 15 lines · Dr. David Alan Gilbert
(Copying Dan in)
Quoted 5 lines · * Wei Yang ([email protected]) wrote:
In
add_to_iovec(),qemu_fflush()will be called if iovec is full. If this happens,buf_indexis reset. Currently, this is not checked andbuf_indexwould always been adjust with buf size.This is not harmful, but will waste some space in file buffer.
That's a nice find.
Quoted 4 lines
This patch make
add_to_iovec()return 1 when it has flushed the file. Then the caller could check the return value to see whether it is necessary to adjust thebuf_indexany more.
Quoted 6 lines
Quoted 1 line
Signed-off-by Wei Yang <[email protected]>Reviewed-by Dr. David Alan Gilbert <[email protected]>(I wonder if there's a way to wrap that little
add_to_iovec, check, add to index, flush in a little wrapper).
Given the name "add_to_iovec" I think it is pretty surprising that it calls "qemu_flush" at all.
It is also pretty wierd that we're checking two different conditions in two different places.
Right now the code is essentially doing this:
if (f->iovcnt >= MAX_IOV_SIZE) {
qemu_fflush(f);
}
if (f->buf_index == IO_BUF_SIZE) {
qemu_fflush(f);
}Except that in the qemu_put_buffer_async() case, we're only doing the first of these two checks. This feels very odd indeed - I would have thought either it should do both, or do neither.
Assuming doing both flushs is ok for qemu_put_buffer_async then I'd suggest renaming 'add_to_iovec' to 'queue_buffer' and have that method do both of these qemu_fflush() calls.
Quoted 83 lines
Quoted 83 lines
--- migration/qemu-file.c | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-)migration/qemu-file.c +28
-14@@ -343,8 +343,16 @@ int qemu_fclose(QEMUFile *f)return ret; } -static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size, - bool may_free) +/* +* Add buf to iovec. Do flush if iovec is full. +* +* Return values: +* 1 iovec is full and flushed +* 0 iovec is not flushed +* +*/ +static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size, + bool may_free) { /* check for adjacent buffer and coalesce them */ if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +@@ -362,7 +370,10 @@ static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,if (f->iovcnt >=MAX_IOV_SIZE ) {qemu_fflush (f); +return 1 ; } + +return 0 ; } void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,@@ -391,10 +402,11 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size) }memcpy (f->buf + f->buf_index, buf, l); f->bytes_xfer += l; -add_to_iovec (f, f->buf + f->buf_index, l, false); - f->buf_index += l; -if (f->buf_index ==IO_BUF_SIZE ) { -qemu_fflush (f); +if (!add_to_iovec (f, f->buf + f->buf_index, l, false)) { + f->buf_index += l; +if (f->buf_index ==IO_BUF_SIZE ) { +qemu_fflush (f); + } }if (qemu_file_get_error (f)) {break ;@@ -412,10 +424,11 @@ void qemu_put_byte(QEMUFile *f, int v) f->buf[f->buf_index] = v; f->bytes_xfer++; -add_to_iovec (f, f->buf + f->buf_index,1 , false); - f->buf_index++; -if (f->buf_index ==IO_BUF_SIZE ) { -qemu_fflush (f); +if (!add_to_iovec (f, f->buf + f->buf_index,1 , false)) { + f->buf_index++; +if (f->buf_index ==IO_BUF_SIZE ) { +qemu_fflush (f); + } } }@@ -717,10 +730,11 @@ ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream, }qemu_put_be32 (f, blen); -add_to_iovec (f, f->buf + f->buf_index, blen, false); - f->buf_index += blen; -if (f->buf_index ==IO_BUF_SIZE ) { -qemu_fflush (f); +if (!add_to_iovec (f, f->buf + f->buf_index, blen, false)) { + f->buf_index += blen; +if (f->buf_index ==IO_BUF_SIZE ) { +qemu_fflush (f); + }
Quoted 9 lines
Quoted 6 lines
migration/qemu-file.c +0
-0}return blen +sizeof (int32_t ); }Signature · 1 line
2.17.1--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [Qemu-devel] [PATCH 2/2] migration/qemu-file: fix potential buf waste for extra buf_index adjustment
Quoted 38 lines · * Daniel P. Berrangé ([email protected]) wrote:
Quoted 15 lines · Dr. David Alan Gilbert
(Copying Dan in)
Quoted 5 lines · * Wei Yang ([email protected]) wrote:
In
add_to_iovec(),qemu_fflush()will be called if iovec is full. If this happens,buf_indexis reset. Currently, this is not checked andbuf_indexwould always been adjust with buf size.This is not harmful, but will waste some space in file buffer.
That's a nice find.
Quoted 4 lines
This patch make
add_to_iovec()return 1 when it has flushed the file. Then the caller could check the return value to see whether it is necessary to adjust thebuf_indexany more.Quoted 6 lines
Quoted 1 line
Signed-off-by Wei Yang <[email protected]>Reviewed-by Dr. David Alan Gilbert <[email protected]>(I wonder if there's a way to wrap that little
add_to_iovec, check, add to index, flush in a little wrapper).Given the name "
add_to_iovec" I think it is pretty surprising that it calls "qemu_flush" at all.It is also pretty wierd that we're checking two different conditions in two different places.
Right now the code is essentially doing this:
if (f->iovcnt >= MAX_IOV_SIZE) { qemu_fflush(f); } if (f->buf_index == IO_BUF_SIZE) { qemu_fflush(f); }
Quoted 4 lines
Except that in the
qemu_put_buffer_async()case, we're only doing the first of these two checks. This feels very odd indeed - I would have thought either it should do both, or do neither.
No, there's two separate types of buffers.
There's f->buf which is a single allocated buffer in the QEMUFile with an offset buf_index, and there are arbitrary RAM pages added typically via qemu_put_buffer_async.
The check for >= IO_BUF_SIZE is only done when adding to the f->buf, where as the check on f->iovcnt is done when you add an element to the iovec and that can happen potentially in either case.
Dave
Quoted 97 lines
Assuming doing both flushs is ok for
qemu_put_buffer_asyncthen I'd suggest renaming 'add_to_iovec' to 'queue_buffer' and have that method do both of theseqemu_fflush()calls.Quoted 83 lines
Quoted 83 lines
--- migration/qemu-file.c | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-)migration/qemu-file.c +28
-14@@ -343,8 +343,16 @@ int qemu_fclose(QEMUFile *f)return ret; } -static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size, - bool may_free) +/* +* Add buf to iovec. Do flush if iovec is full. +* +* Return values: +* 1 iovec is full and flushed +* 0 iovec is not flushed +* +*/ +static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size, + bool may_free) { /* check for adjacent buffer and coalesce them */ if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +@@ -362,7 +370,10 @@ static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,if (f->iovcnt >=MAX_IOV_SIZE ) {qemu_fflush (f); +return 1 ; } + +return 0 ; } void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,@@ -391,10 +402,11 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size) }memcpy (f->buf + f->buf_index, buf, l); f->bytes_xfer += l; -add_to_iovec (f, f->buf + f->buf_index, l, false); - f->buf_index += l; -if (f->buf_index ==IO_BUF_SIZE ) { -qemu_fflush (f); +if (!add_to_iovec (f, f->buf + f->buf_index, l, false)) { + f->buf_index += l; +if (f->buf_index ==IO_BUF_SIZE ) { +qemu_fflush (f); + } }if (qemu_file_get_error (f)) {break ;@@ -412,10 +424,11 @@ void qemu_put_byte(QEMUFile *f, int v) f->buf[f->buf_index] = v; f->bytes_xfer++; -add_to_iovec (f, f->buf + f->buf_index,1 , false); - f->buf_index++; -if (f->buf_index ==IO_BUF_SIZE ) { -qemu_fflush (f); +if (!add_to_iovec (f, f->buf + f->buf_index,1 , false)) { + f->buf_index++; +if (f->buf_index ==IO_BUF_SIZE ) { +qemu_fflush (f); + } } }@@ -717,10 +730,11 @@ ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream, }qemu_put_be32 (f, blen); -add_to_iovec (f, f->buf + f->buf_index, blen, false); - f->buf_index += blen; -if (f->buf_index ==IO_BUF_SIZE ) { -qemu_fflush (f); +if (!add_to_iovec (f, f->buf + f->buf_index, blen, false)) { + f->buf_index += blen; +if (f->buf_index ==IO_BUF_SIZE ) { +qemu_fflush (f); + }Quoted 9 lines
Quoted 6 lines
migration/qemu-file.c +0
-0}return blen +sizeof (int32_t ); }Signature · 1 line
2.17.1--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Quoted 6 lines
Regards,
DanielSignature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [Qemu-devel] [PATCH 2/2] migration/qemu-file: fix potential buf waste for extra buf_index adjustment
Quoted 44 lines · Dr. David Alan Gilbert
Quoted 38 lines · * Daniel P. Berrang? ([email protected]) wrote:
Quoted 15 lines · Dr. David Alan Gilbert
(Copying Dan in)
Quoted 5 lines · * Wei Yang ([email protected]) wrote:
In
add_to_iovec(),qemu_fflush()will be called if iovec is full. If this happens,buf_indexis reset. Currently, this is not checked andbuf_indexwould always been adjust with buf size.This is not harmful, but will waste some space in file buffer.
That's a nice find.
Quoted 4 lines
This patch make
add_to_iovec()return 1 when it has flushed the file. Then the caller could check the return value to see whether it is necessary to adjust thebuf_indexany more.Quoted 6 lines
Quoted 1 line
Signed-off-by Wei Yang <[email protected]>Reviewed-by Dr. David Alan Gilbert <[email protected]>(I wonder if there's a way to wrap that little
add_to_iovec, check, add to index, flush in a little wrapper).Given the name "
add_to_iovec" I think it is pretty surprising that it calls "qemu_flush" at all.It is also pretty wierd that we're checking two different conditions in two different places.
Right now the code is essentially doing this:
if (f->iovcnt >= MAX_IOV_SIZE) { qemu_fflush(f); } if (f->buf_index == IO_BUF_SIZE) { qemu_fflush(f); }Quoted 4 lines
Except that in the
qemu_put_buffer_async()case, we're only doing the first of these two checks. This feels very odd indeed - I would have thought either it should do both, or do neither.
Quoted 6 lines
No, there's two separate types of buffers.
There's
f->bufwhich is a single allocated buffer in the QEMUFile with an offsetbuf_index, and there are arbitrary RAM pages added typically viaqemu_put_buffer_async.
qemu_put_buffer_async is the only one which put a range not in f->buf into the iovec.
And one thing confused me is even its name is async, add_to_iovec still would call qemu_fflush when iovec is full. So it is not a always async function. From the function name, it is a little difficult to differentiate qemu_put_buffer and qemu_put_buffer_async.
Quoted 111 lines
The check for >=
IO_BUF_SIZEis only done when adding to thef->buf, where as the check onf->iovcntis done when you add an element to the iovec and that can happen potentially in either case.Dave
Quoted 97 lines
Assuming doing both flushs is ok for
qemu_put_buffer_asyncthen I'd suggest renaming 'add_to_iovec' to 'queue_buffer' and have that method do both of theseqemu_fflush()calls.Quoted 83 lines
Quoted 83 lines
--- migration/qemu-file.c | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-)migration/qemu-file.c +28
-14@@ -343,8 +343,16 @@ int qemu_fclose(QEMUFile *f)return ret; } -static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size, - bool may_free) +/* +* Add buf to iovec. Do flush if iovec is full. +* +* Return values: +* 1 iovec is full and flushed +* 0 iovec is not flushed +* +*/ +static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size, + bool may_free) { /* check for adjacent buffer and coalesce them */ if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +@@ -362,7 +370,10 @@ static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,if (f->iovcnt >=MAX_IOV_SIZE ) {qemu_fflush (f); +return 1 ; } + +return 0 ; } void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,@@ -391,10 +402,11 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size) }memcpy (f->buf + f->buf_index, buf, l); f->bytes_xfer += l; -add_to_iovec (f, f->buf + f->buf_index, l, false); - f->buf_index += l; -if (f->buf_index ==IO_BUF_SIZE ) { -qemu_fflush (f); +if (!add_to_iovec (f, f->buf + f->buf_index, l, false)) { + f->buf_index += l; +if (f->buf_index ==IO_BUF_SIZE ) { +qemu_fflush (f); + } }if (qemu_file_get_error (f)) {break ;@@ -412,10 +424,11 @@ void qemu_put_byte(QEMUFile *f, int v) f->buf[f->buf_index] = v; f->bytes_xfer++; -add_to_iovec (f, f->buf + f->buf_index,1 , false); - f->buf_index++; -if (f->buf_index ==IO_BUF_SIZE ) { -qemu_fflush (f); +if (!add_to_iovec (f, f->buf + f->buf_index,1 , false)) { + f->buf_index++; +if (f->buf_index ==IO_BUF_SIZE ) { +qemu_fflush (f); + } } }@@ -717,10 +730,11 @@ ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream, }qemu_put_be32 (f, blen); -add_to_iovec (f, f->buf + f->buf_index, blen, false); - f->buf_index += blen; -if (f->buf_index ==IO_BUF_SIZE ) { -qemu_fflush (f); +if (!add_to_iovec (f, f->buf + f->buf_index, blen, false)) { + f->buf_index += blen; +if (f->buf_index ==IO_BUF_SIZE ) { +qemu_fflush (f); + }Quoted 9 lines
Quoted 6 lines
migration/qemu-file.c +0
-0}return blen +sizeof (int32_t ); }Signature · 1 line
2.17.1--
Dr. David Alan Gilbert / [email protected] / Manchester, UKQuoted 6 lines
Regards,
DanielSignature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Signature · 2 lines
Wei Yang Help you, Help me
Re: [Qemu-devel] [PATCH 2/2] migration/qemu-file: fix potential buf waste for extra buf_index adjustment
Quoted 52 lines · * Wei Yang ([email protected]) wrote:
Quoted 44 lines · Dr. David Alan Gilbert
Quoted 38 lines · * Daniel P. Berrang? ([email protected]) wrote:
Quoted 15 lines · Dr. David Alan Gilbert
(Copying Dan in)
Quoted 5 lines · * Wei Yang ([email protected]) wrote:
In
add_to_iovec(),qemu_fflush()will be called if iovec is full. If this happens,buf_indexis reset. Currently, this is not checked andbuf_indexwould always been adjust with buf size.This is not harmful, but will waste some space in file buffer.
That's a nice find.
Quoted 4 lines
This patch make
add_to_iovec()return 1 when it has flushed the file. Then the caller could check the return value to see whether it is necessary to adjust thebuf_indexany more.Quoted 6 lines
Quoted 1 line
Signed-off-by Wei Yang <[email protected]>Reviewed-by Dr. David Alan Gilbert <[email protected]>(I wonder if there's a way to wrap that little
add_to_iovec, check, add to index, flush in a little wrapper).Given the name "
add_to_iovec" I think it is pretty surprising that it calls "qemu_flush" at all.It is also pretty wierd that we're checking two different conditions in two different places.
Right now the code is essentially doing this:
if (f->iovcnt >= MAX_IOV_SIZE) { qemu_fflush(f); } if (f->buf_index == IO_BUF_SIZE) { qemu_fflush(f); }Quoted 4 lines
Except that in the
qemu_put_buffer_async()case, we're only doing the first of these two checks. This feels very odd indeed - I would have thought either it should do both, or do neither.Quoted 6 lines
No, there's two separate types of buffers.
There's
f->bufwhich is a single allocated buffer in the QEMUFile with an offsetbuf_index, and there are arbitrary RAM pages added typically viaqemu_put_buffer_async.
Quoted 7 lines
qemu_put_buffer_asyncis the only one which put a range not inf->bufinto the iovec.And one thing confused me is even its name is async,
add_to_iovecstill would callqemu_fflushwhen iovec is full. So it is not a always async function. From the function name, it is a little difficult to differentiateqemu_put_bufferandqemu_put_buffer_async.
It's a bit confusing, but I think it is the right name; the 'async' refers to the fact that the data is read from the buffer we pass sometime later, not synchronously before the return of the qemu_put_buffer_async call.
Dave
Quoted 103 lines
Quoted 103 lines
The check for >=
IO_BUF_SIZEis only done when adding to thef->buf, where as the check onf->iovcntis done when you add an element to the iovec and that can happen potentially in either case.Dave
Quoted 97 lines
Assuming doing both flushs is ok for
qemu_put_buffer_asyncthen I'd suggest renaming 'add_to_iovec' to 'queue_buffer' and have that method do both of theseqemu_fflush()calls.Quoted 92 lines
Quoted 89 lines
--- migration/qemu-file.c | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-)migration/qemu-file.c +28
-14@@ -343,8 +343,16 @@ int qemu_fclose(QEMUFile *f)return ret; } -static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size, - bool may_free) +/* +* Add buf to iovec. Do flush if iovec is full. +* +* Return values: +* 1 iovec is full and flushed +* 0 iovec is not flushed +* +*/ +static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size, + bool may_free) { /* check for adjacent buffer and coalesce them */ if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +@@ -362,7 +370,10 @@ static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,if (f->iovcnt >=MAX_IOV_SIZE ) {qemu_fflush (f); +return 1 ; } + +return 0 ; } void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,@@ -391,10 +402,11 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size) }memcpy (f->buf + f->buf_index, buf, l); f->bytes_xfer += l; -add_to_iovec (f, f->buf + f->buf_index, l, false); - f->buf_index += l; -if (f->buf_index ==IO_BUF_SIZE ) { -qemu_fflush (f); +if (!add_to_iovec (f, f->buf + f->buf_index, l, false)) { + f->buf_index += l; +if (f->buf_index ==IO_BUF_SIZE ) { +qemu_fflush (f); + } }if (qemu_file_get_error (f)) {break ;@@ -412,10 +424,11 @@ void qemu_put_byte(QEMUFile *f, int v) f->buf[f->buf_index] = v; f->bytes_xfer++; -add_to_iovec (f, f->buf + f->buf_index,1 , false); - f->buf_index++; -if (f->buf_index ==IO_BUF_SIZE ) { -qemu_fflush (f); +if (!add_to_iovec (f, f->buf + f->buf_index,1 , false)) { + f->buf_index++; +if (f->buf_index ==IO_BUF_SIZE ) { +qemu_fflush (f); + } } }@@ -717,10 +730,11 @@ ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream, }qemu_put_be32 (f, blen); -add_to_iovec (f, f->buf + f->buf_index, blen, false); - f->buf_index += blen; -if (f->buf_index ==IO_BUF_SIZE ) { -qemu_fflush (f); +if (!add_to_iovec (f, f->buf + f->buf_index, blen, false)) { + f->buf_index += blen; +if (f->buf_index ==IO_BUF_SIZE ) { +qemu_fflush (f); + } }return blen +sizeof (int32_t ); }Signature · 1 line
2.17.1--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Quoted 12 lines
Quoted 8 lines
Quoted 6 lines
Regards,
DanielSignature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|--
Dr. David Alan Gilbert / [email protected] / Manchester, UKSignature · 2 lines
Wei Yang Help you, Help me
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [Qemu-devel] [PATCH 2/2] migration/qemu-file: fix potential buf waste for extra buf_index adjustment
In reply to Dr. David Alan GilbertQuoted 61 lines · Dr. David Alan Gilbert
(Copying Dan in)
Quoted 5 lines · * Wei Yang ([email protected]) wrote:
In
add_to_iovec(),qemu_fflush()will be called if iovec is full. If this happens,buf_indexis reset. Currently, this is not checked andbuf_indexwould always been adjust with buf size.This is not harmful, but will waste some space in file buffer.
That's a nice find.
Quoted 5 lines
This patch make
add_to_iovec()return 1 when it has flushed the file. Then the caller could check the return value to see whether it is necessary to adjust thebuf_indexany more.Signed-off-by Wei Yang <[email protected]>Reviewed-by Dr. David Alan Gilbert <[email protected]>(I wonder if there's a way to wrap that little
add_to_iovec, check, add to index, flush in a little wrapper).Dave
Quoted 37 lines
--- migration/qemu-file.c | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-)migration/qemu-file.c +13
-2@@ -343,8 +343,16 @@ int qemu_fclose(QEMUFile *f)return ret; } -static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size, - bool may_free) +/* +* Add buf to iovec. Do flush if iovec is full. +* +* Return values: +* 1 iovec is full and flushed +* 0 iovec is not flushed +* +*/ +static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size, + bool may_free) { /* check for adjacent buffer and coalesce them */ if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +@@ -362,7 +370,10 @@ static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,if (f->iovcnt >=MAX_IOV_SIZE ) {qemu_fflush (f); +return 1 ; } + +return 0 ; }
Quoted 14 lines
Quoted 4 lines
migration/qemu-file.c +0
-0void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,@@ -391,10 +402,11 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size) }memcpy (f->buf + f->buf_index, buf, l);migration/qemu-file.c void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size) · line 409
Quoted 10 lines
migration/qemu-file.c +5
-4f->bytes_xfer += l; -add_to_iovec (f, f->buf + f->buf_index, l, false); - f->buf_index += l; -if (f->buf_index ==IO_BUF_SIZE ) { -qemu_fflush (f); +if (!add_to_iovec (f, f->buf + f->buf_index, l, false)) { + f->buf_index += l; +if (f->buf_index ==IO_BUF_SIZE ) { +qemu_fflush (f); + }
You mean put these four lines into a wrapper?
Name it as add_buf_to_iovec?
Quoted 40 lines
Quoted 38 lines
migration/qemu-file.c +10
-8}if (qemu_file_get_error (f)) {break ;@@ -412,10 +424,11 @@ void qemu_put_byte(QEMUFile *f, int v) f->buf[f->buf_index] = v; f->bytes_xfer++; -add_to_iovec (f, f->buf + f->buf_index,1 , false); - f->buf_index++; -if (f->buf_index ==IO_BUF_SIZE ) { -qemu_fflush (f); +if (!add_to_iovec (f, f->buf + f->buf_index,1 , false)) { + f->buf_index++; +if (f->buf_index ==IO_BUF_SIZE ) { +qemu_fflush (f); + } } }@@ -717,10 +730,11 @@ ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream, }qemu_put_be32 (f, blen); -add_to_iovec (f, f->buf + f->buf_index, blen, false); - f->buf_index += blen; -if (f->buf_index ==IO_BUF_SIZE ) { -qemu_fflush (f); +if (!add_to_iovec (f, f->buf + f->buf_index, blen, false)) { + f->buf_index += blen; +if (f->buf_index ==IO_BUF_SIZE ) { +qemu_fflush (f); + } }return blen +sizeof (int32_t ); }Signature · 1 line
2.17.1--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Signature · 2 lines
Wei Yang Help you, Help me
Re: [Qemu-devel] [PATCH 2/2] migration/qemu-file: fix potential buf waste for extra buf_index adjustment
Quoted 79 lines · * Wei Yang ([email protected]) wrote:
Quoted 61 lines · Dr. David Alan Gilbert
(Copying Dan in)
Quoted 5 lines · * Wei Yang ([email protected]) wrote:
In
add_to_iovec(),qemu_fflush()will be called if iovec is full. If this happens,buf_indexis reset. Currently, this is not checked andbuf_indexwould always been adjust with buf size.This is not harmful, but will waste some space in file buffer.
That's a nice find.
Quoted 5 lines
This patch make
add_to_iovec()return 1 when it has flushed the file. Then the caller could check the return value to see whether it is necessary to adjust thebuf_indexany more.Signed-off-by Wei Yang <[email protected]>Reviewed-by Dr. David Alan Gilbert <[email protected]>(I wonder if there's a way to wrap that little
add_to_iovec, check, add to index, flush in a little wrapper).Dave
Quoted 37 lines
--- migration/qemu-file.c | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-)migration/qemu-file.c +13
-2@@ -343,8 +343,16 @@ int qemu_fclose(QEMUFile *f)return ret; } -static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size, - bool may_free) +/* +* Add buf to iovec. Do flush if iovec is full. +* +* Return values: +* 1 iovec is full and flushed +* 0 iovec is not flushed +* +*/ +static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size, + bool may_free) { /* check for adjacent buffer and coalesce them */ if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +@@ -362,7 +370,10 @@ static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,if (f->iovcnt >=MAX_IOV_SIZE ) {qemu_fflush (f); +return 1 ; } + +return 0 ; }Quoted 14 lines
Quoted 4 lines
migration/qemu-file.c +0
-0void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,@@ -391,10 +402,11 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size) }memcpy (f->buf + f->buf_index, buf, l);migration/qemu-file.c void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size) · line 409
Quoted 10 lines
migration/qemu-file.c +5
-4f->bytes_xfer += l; -add_to_iovec (f, f->buf + f->buf_index, l, false); - f->buf_index += l; -if (f->buf_index ==IO_BUF_SIZE ) { -qemu_fflush (f); +if (!add_to_iovec (f, f->buf + f->buf_index, l, false)) { + f->buf_index += l; +if (f->buf_index ==IO_BUF_SIZE ) { +qemu_fflush (f); + }You mean put these four lines into a wrapper?
Quoted 1 line
Name it as
add_buf_to_iovec?
Yes.
Dave
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
migration/qemu-file.c +10
-8}if (qemu_file_get_error (f)) {break ;@@ -412,10 +424,11 @@ void qemu_put_byte(QEMUFile *f, int v) f->buf[f->buf_index] = v; f->bytes_xfer++; -add_to_iovec (f, f->buf + f->buf_index,1 , false); - f->buf_index++; -if (f->buf_index ==IO_BUF_SIZE ) { -qemu_fflush (f); +if (!add_to_iovec (f, f->buf + f->buf_index,1 , false)) { + f->buf_index++; +if (f->buf_index ==IO_BUF_SIZE ) { +qemu_fflush (f); + } } }@@ -717,10 +730,11 @@ ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream, }qemu_put_be32 (f, blen); -add_to_iovec (f, f->buf + f->buf_index, blen, false); - f->buf_index += blen; -if (f->buf_index ==IO_BUF_SIZE ) { -qemu_fflush (f); +if (!add_to_iovec (f, f->buf + f->buf_index, blen, false)) { + f->buf_index += blen; +if (f->buf_index ==IO_BUF_SIZE ) { +qemu_fflush (f); + }
Quoted 12 lines
Quoted 8 lines
Quoted 6 lines
migration/qemu-file.c +0
-0}return blen +sizeof (int32_t ); }Signature · 1 line
2.17.1--
Dr. David Alan Gilbert / [email protected] / Manchester, UKSignature · 2 lines
Wei Yang Help you, Help me
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [Qemu-devel] [PATCH 2/2] migration/qemu-file: fix potential buf waste for extra buf_index adjustment
Quoted 80 lines · Dr. David Alan Gilbert
Quoted 79 lines · * Wei Yang ([email protected]) wrote:
Quoted 61 lines · Dr. David Alan Gilbert
(Copying Dan in)
Quoted 5 lines · * Wei Yang ([email protected]) wrote:
In
add_to_iovec(),qemu_fflush()will be called if iovec is full. If this happens,buf_indexis reset. Currently, this is not checked andbuf_indexwould always been adjust with buf size.This is not harmful, but will waste some space in file buffer.
That's a nice find.
Quoted 5 lines
This patch make
add_to_iovec()return 1 when it has flushed the file. Then the caller could check the return value to see whether it is necessary to adjust thebuf_indexany more.Signed-off-by Wei Yang <[email protected]>Reviewed-by Dr. David Alan Gilbert <[email protected]>(I wonder if there's a way to wrap that little
add_to_iovec, check, add to index, flush in a little wrapper).Dave
Quoted 37 lines
--- migration/qemu-file.c | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-)migration/qemu-file.c +13
-2@@ -343,8 +343,16 @@ int qemu_fclose(QEMUFile *f)return ret; } -static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size, - bool may_free) +/* +* Add buf to iovec. Do flush if iovec is full. +* +* Return values: +* 1 iovec is full and flushed +* 0 iovec is not flushed +* +*/ +static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size, + bool may_free) { /* check for adjacent buffer and coalesce them */ if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +@@ -362,7 +370,10 @@ static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,if (f->iovcnt >=MAX_IOV_SIZE ) {qemu_fflush (f); +return 1 ; } + +return 0 ; }Quoted 14 lines
Quoted 4 lines
migration/qemu-file.c +0
-0void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,@@ -391,10 +402,11 @@ void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size) }memcpy (f->buf + f->buf_index, buf, l);migration/qemu-file.c void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size) · line 409
Quoted 10 lines
migration/qemu-file.c +5
-4f->bytes_xfer += l; -add_to_iovec (f, f->buf + f->buf_index, l, false); - f->buf_index += l; -if (f->buf_index ==IO_BUF_SIZE ) { -qemu_fflush (f); +if (!add_to_iovec (f, f->buf + f->buf_index, l, false)) { + f->buf_index += l; +if (f->buf_index ==IO_BUF_SIZE ) { +qemu_fflush (f); + }You mean put these four lines into a wrapper?
Quoted 3 lines
Quoted 1 line
Name it as
add_buf_to_iovec?Yes.
Sure, Let me prepare v2 with this.
Signature · 2 lines
Wei Yang Help you, Help me
Quoted 5 lines · Wei Yang
Two cleanup:
Patch #1 make code consistent on calling
add_to_iovecPatch #2 refine the code to handle the case when buf already flushed
Ping~
Quoted 11 lines
Wei Yang (2):
migration/qemu-file: remove check onwritev_bufferin
qemu_put_compression_data
migration/qemu-file: fix potential buf waste for extrabuf_index
adjustment
migration/qemu-file.c| 44 +++++++++++++++++++++++++++----------------1 file changed, 28 insertions(+), 16 deletions(-)Signature · 1 line
2.17.1
Signature · 2 lines
Wei Yang Help you, Help me