JSON array of cpu info is sorted in order to find thread id of cpu smoothly. Signed-off-by: Zhu Guihua <zhugh.fnst@xxxxxxxxxxxxxx> --- src/libvirt_private.syms | 1 + src/qemu/qemu_monitor_json.c | 31 +++++++++++++++++++++++++++++-- src/util/virbitmap.c | 2 +- src/util/virbitmap.h | 2 ++ 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 032e9a9..d2c7c73 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1042,6 +1042,7 @@ virBitmapFree; virBitmapGetBit; virBitmapIsAllClear; virBitmapIsAllSet; +virBitmapIsSet; virBitmapLastSetBit; virBitmapNew; virBitmapNewCopy; diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c index da5c14d..96a964c 100644 --- a/src/qemu/qemu_monitor_json.c +++ b/src/qemu/qemu_monitor_json.c @@ -1162,6 +1162,21 @@ int qemuMonitorJSONSystemReset(qemuMonitorPtr mon) return ret; } +static int +qemuCPUInfoCompare(const void *a, + const void *b) +{ + virJSONValuePtr *entrya = (virJSONValuePtr *)a; + virJSONValuePtr *entryb = (virJSONValuePtr *)b; + int ia; + int ib; + + virJSONValueObjectGetNumberInt(*entrya, "CPU", &ia); + virJSONValueObjectGetNumberInt(*entryb, "CPU", &ib); + + return ia - ib; +} + /* * [ { "CPU": 0, "current": true, "halted": false, "pc": 3227107138 }, @@ -1176,6 +1191,7 @@ qemuMonitorJSONExtractCPUInfo(virJSONValuePtr reply, size_t i; int *threads = NULL; int ncpus; + virJSONValuePtr *entryarray = NULL; if (!(data = virJSONValueObjectGet(reply, "return"))) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", @@ -1198,16 +1214,26 @@ qemuMonitorJSONExtractCPUInfo(virJSONValuePtr reply, if (VIR_ALLOC_N(threads, ncpus) < 0) goto cleanup; + if (VIR_ALLOC_N(entryarray, ncpus) < 0) + goto cleanup; + for (i = 0; i < ncpus; i++) { virJSONValuePtr entry = virJSONValueArrayGet(data, i); - int thread; if (!entry) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("cpu information was missing an array element")); goto cleanup; } - if (virJSONValueObjectGetNumberInt(entry, "thread_id", &thread) < 0) { + entryarray[i] = entry; + } + + qsort(entryarray, ncpus, sizeof(entryarray[0]), qemuCPUInfoCompare); + + for (i = 0; i < ncpus; i++) { + int thread; + if (virJSONValueObjectGetNumberInt(entryarray[i], "thread_id", &thread) < 0) { + /* Some older qemu versions don't report the thread_id, * so treat this as non-fatal, simply returning no data */ ret = 0; @@ -1223,6 +1249,7 @@ qemuMonitorJSONExtractCPUInfo(virJSONValuePtr reply, cleanup: VIR_FREE(threads); + VIR_FREE(entryarray); return ret; } diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c index 05c50e4..168b8db 100644 --- a/src/util/virbitmap.c +++ b/src/util/virbitmap.c @@ -153,7 +153,7 @@ int virBitmapClearBit(virBitmapPtr bitmap, size_t b) } /* Helper function. caller must ensure b < bitmap->max_bit */ -static bool virBitmapIsSet(virBitmapPtr bitmap, size_t b) +bool virBitmapIsSet(virBitmapPtr bitmap, size_t b) { return !!(bitmap->map[VIR_BITMAP_UNIT_OFFSET(b)] & VIR_BITMAP_BIT(b)); } diff --git a/src/util/virbitmap.h b/src/util/virbitmap.h index 565264c..57fb195 100644 --- a/src/util/virbitmap.h +++ b/src/util/virbitmap.h @@ -60,6 +60,8 @@ int virBitmapSetBit(virBitmapPtr bitmap, size_t b) int virBitmapClearBit(virBitmapPtr bitmap, size_t b) ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK; +bool virBitmapIsSet(virBitmapPtr bitmap, size_t b); + /* * Get setting of bit position @b in @bitmap and store in @result */ -- 1.9.3 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list