migration: Support QLIST migration
PATCH v3 1/1 migration: Support QLIST migration
Support QLIST migration using the same principle as QTAILQ:94869d5c52 ("migration: migrate QTAILQ").
The VMSTATE_QLIST_V macro has the same proto as VMSTATE_QTAILQ_V. The change mainly resides in QLIST_RAW_INSERT_TAIL implementation.
Tests also are provided.
---
v2 -> v3:
- remove 2 spurious changes in gtree tests
v1 -> v2:
- rebase on top of gtree addition
- add trace points
- add g_free on error
--- include/migration/vmstate.h | 21 ++++++ include/qemu/queue.h | 30 ++++++++ migration/trace-events | 5 ++ migration/vmstate-types.c | 69 +++++++++++++++++++ tests/test-vmstate.c | 133 ++++++++++++++++++++++++++++++++++++ 5 files changed, 258 insertions(+)
include/migration/vmstate.h +21 -0
@@ -225,6 +225,7 @@ extern const VMStateInfo vmstate_info_tmp;extern const VMStateInfo vmstate_info_bitmap;extern const VMStateInfo vmstate_info_qtailq;extern const VMStateInfo vmstate_info_gtree; +extern const VMStateInfo vmstate_info_qlist;#define type_check_2darray (t1,t2,n,m) ((t1(*)[n][m])0 - (t2*)0)/*
@@ -794,6 +795,26 @@ extern const VMStateInfo vmstate_info_gtree; .offset = offsetof(_state, _field), \ } +/* +* For migrating a QLIST +* Target QLIST needs be properly initialized. +* _type: type of QLIST element +* _next: name of QLIST_ENTRY entry field in QLIST element +* _vmsd: VMSD for QLIST element +* size: size of QLIST element +* start: offset of QLIST_ENTRY in QTAILQ element +*/ +#define VMSTATE_QLIST_V (_field, _state, _version, _vmsd, _type, _next) \ +{ \ + .name = (stringify(_field)), \ + .version_id = (_version), \ + .vmsd = &(_vmsd), \ + .size = sizeof(_type), \ + .info = &vmstate_info_qlist, \ + .offset = offsetof(_state, _field), \ + .start = offsetof(_type, _next), \ +} +/* _f : field name _f_n : num of elements field_name _n : num of elements
include/qemu/queue.h +30 -0
@@ -491,4 +491,34 @@ union { \QTAILQ_RAW_TQH_CIRC (head)->tql_prev =QTAILQ_RAW_TQE_CIRC (elm, entry); \ } while (/*CONSTCOND*/0) +#define QLIST_RAW_FIRST (head) \ + field_at_offset(head, 0, void *) + +#define QLIST_RAW_NEXT (elm, entry) \ + field_at_offset(elm, entry, void *) + +#define QLIST_RAW_PREVIOUS (elm, entry) \ + field_at_offset(elm, entry + sizeof(void *), void *) + +#define QLIST_RAW_FOREACH (elm, head, entry) \ + for ((elm) = *QLIST_RAW_FIRST(head); \ + (elm); \ + (elm) = *QLIST_RAW_NEXT(elm, entry)) + +#define QLIST_RAW_INSERT_TAIL (head, elm, entry) do { \ + void *iter, *last = NULL; \ + *QLIST_RAW_NEXT(elm, entry) = NULL; \ + if (!*QLIST_RAW_FIRST(head)) { \ + *QLIST_RAW_FIRST(head) = elm; \ + *QLIST_RAW_PREVIOUS(elm, entry) = head; \ + break; \ + } \ + for (iter = *QLIST_RAW_FIRST(head); \ + iter; last = iter, iter = *QLIST_RAW_NEXT(iter, entry)) \ + { } \ + *QLIST_RAW_NEXT(last, entry) = elm; \ + *QLIST_RAW_PREVIOUS(elm, entry) = last; \ +} while (0) + +#endif /* QEMU_SYS_QUEUE_H */
migration/trace-events +5 -0
@@ -76,6 +76,11 @@ get_gtree_end(const char *field_name, const char *key_vmsd_name, const char *val put_gtree(const char *field_name, const char *key_vmsd_name, const char *val_vmsd_name, uint32_t nnodes) "%s(%s/%s) nnodes=%d" put_gtree_end(const char *field_name, const char *key_vmsd_name, const char *val_vmsd_name, int ret) "%s(%s/%s) %d" +get_qlist(const char *field_name, const char *vmsd_name, int version_id) "%s(%s v%d)" +get_qlist_end(const char *field_name, const char *vmsd_name) "%s(%s)" +put_qlist(const char *field_name, const char *vmsd_name, int version_id) "%s(%s v%d)" +put_qlist_end(const char *field_name, const char *vmsd_name) "%s(%s)" + # qemu-file.c qemu_file_fclose(void) ""
migration/vmstate-types.c +69 -0
72 lines not on this page · raw message
tests/test-vmstate.c +133 -0
157 lines not on this page · raw message
Signature · 1 line
2.20.1
Quoted 9 lines · Eric Auger
Support QLIST migration using the same principle as QTAILQ:
94869d5c52("migration: migrate QTAILQ").The
VMSTATE_QLIST_Vmacro has the same proto asVMSTATE_QTAILQ_V. The change mainly resides inQLIST_RAW_INSERT_TAILimplementation.Tests also are provided.
Signed-off-by Eric Auger <[email protected]>
Hi
How long are these lists normally? I think that the INSERT_TAIL is the wrong approach. If lists can be long, it is much better to just insert at the beggining and as last operation, reverse things, no?
include/qemu/queue.h union { \ · line 520
Quoted 13 lines
include/qemu/queue.h +13
-0+#define QLIST_RAW_INSERT_TAIL (head, elm, entry) do { \ + void *iter, *last = NULL; \ + *QLIST_RAW_NEXT(elm, entry) = NULL; \ + if (!*QLIST_RAW_FIRST(head)) { \ + *QLIST_RAW_FIRST(head) = elm; \ + *QLIST_RAW_PREVIOUS(elm, entry) = head; \ + break; \ + } \ + for (iter = *QLIST_RAW_FIRST(head); \ + iter; last = iter, iter = *QLIST_RAW_NEXT(iter, entry)) \ + { } \ + *QLIST_RAW_NEXT(last, entry) = elm; \ + *QLIST_RAW_PREVIOUS(elm, entry) = last; \
I think that you normally want to do this two instructions in the reverse order, just in case (famous last words).
Quoted 22 lines
migration/vmstate-types.c +22
-0+static int get_qlist (QEMUFile *f,void *pv,size_t unused_size, +const VMStateField *field) +{ +int ret =0 ; +const VMStateDescription *vmsd = field->vmsd; +/* size of a QLIST element */ +size_t size = field->size; +/* offset of the QLIST entry in a QLIST element */ +size_t entry_offset = field->start; +int version_id = field->version_id; +void *elm; + +trace_get_qlist (field->name, vmsd->name, vmsd->version_id); +if (version_id > vmsd->version_id) { +error_report ("%s %s" , vmsd->name,"too new" ); +return -EINVAL ; + } +if (version_id < vmsd->minimum_version_id) { +error_report ("%s %s" , vmsd->name,"too old" ); +return -EINVAL ; + } +
migration/vmstate-types.c const VMStateInfo vmstate_info_gtree = { · line 903
Quoted 10 lines
migration/vmstate-types.c +10
-0+while (qemu_get_byte (f)) { + elm =g_malloc (size); + ret =vmstate_load_state (f, vmsd, elm, version_id); +if (ret) { +error_report ("%s: failed to load %s (%d)" , field->name, + vmsd->name, ret); +g_free (elm); +return ret; + } +QLIST_RAW_INSERT_TAIL (pv, elm, entry_offset);
Here we insert at the beggining.
Quoted 1 line
+ }
Here we reverse?
We move from O(n^2) to O(2n), much better, no? As said, except if the lists are normally very short.
The rest of the patch looks ok to me.
Later, Juan.
Hi Juan,
Quoted 70 lines · Juan Quintela
Quoted 9 lines · Eric Auger
Support QLIST migration using the same principle as QTAILQ:
94869d5c52("migration: migrate QTAILQ").The
VMSTATE_QLIST_Vmacro has the same proto asVMSTATE_QTAILQ_V. The change mainly resides inQLIST_RAW_INSERT_TAILimplementation.Tests also are provided.
Signed-off-by Eric Auger <[email protected]>Hi
How long are these lists normally? I think that the
INSERT_TAILis the wrong approach. If lists can be long, it is much better to just insert at the beggining and as last operation, reverse things, no?>include/qemu/queue.h union { \ · line 520
Quoted 13 lines
include/qemu/queue.h +13
-0+#define QLIST_RAW_INSERT_TAIL (head, elm, entry) do { \ + void *iter, *last = NULL; \ + *QLIST_RAW_NEXT(elm, entry) = NULL; \ + if (!*QLIST_RAW_FIRST(head)) { \ + *QLIST_RAW_FIRST(head) = elm; \ + *QLIST_RAW_PREVIOUS(elm, entry) = head; \ + break; \ + } \ + for (iter = *QLIST_RAW_FIRST(head); \ + iter; last = iter, iter = *QLIST_RAW_NEXT(iter, entry)) \ + { } \ + *QLIST_RAW_NEXT(last, entry) = elm; \ + *QLIST_RAW_PREVIOUS(elm, entry) = last; \I think that you normally want to do this two instructions in the reverse order, just in case (famous last words).
Quoted 22 lines
migration/vmstate-types.c +22
-0+static int get_qlist (QEMUFile *f,void *pv,size_t unused_size, +const VMStateField *field) +{ +int ret =0 ; +const VMStateDescription *vmsd = field->vmsd; +/* size of a QLIST element */ +size_t size = field->size; +/* offset of the QLIST entry in a QLIST element */ +size_t entry_offset = field->start; +int version_id = field->version_id; +void *elm; + +trace_get_qlist (field->name, vmsd->name, vmsd->version_id); +if (version_id > vmsd->version_id) { +error_report ("%s %s" , vmsd->name,"too new" ); +return -EINVAL ; + } +if (version_id < vmsd->minimum_version_id) { +error_report ("%s %s" , vmsd->name,"too old" ); +return -EINVAL ; + } +migration/vmstate-types.c const VMStateInfo vmstate_info_gtree = { · line 903
Quoted 10 lines
migration/vmstate-types.c +10
-0+while (qemu_get_byte (f)) { + elm =g_malloc (size); + ret =vmstate_load_state (f, vmsd, elm, version_id); +if (ret) { +error_report ("%s: failed to load %s (%d)" , field->name, + vmsd->name, ret); +g_free (elm); +return ret; + } +QLIST_RAW_INSERT_TAIL (pv, elm, entry_offset);Here we insert at the beggining.
Quoted 6 lines
Quoted 1 line
+ }
Here we reverse?
We move from O(n^2) to O(2n), much better, no? As said, except if the lists are normally very short.
Yes I agree with you. I derived the QTAILQ code without much thinking about perf. Also in my case I expect the list to be short.
But as we want this code to be useful for other cases, I rewrote as you suggested.
Thank you for your review.
Eric
Quoted 6 lines
The rest of the patch looks ok to me.
Later, Juan.