JSON array of cpu info is sorted in order to find thread id of cpu smoothly.
Signed-off-by: Zhu Guihua <zhugh.fnst(a)cn.fujitsu.com>
---
src/qemu/qemu_monitor_json.c | 31 +++++++++++++++++++++++++++++--
1 file changed, 29 insertions(+), 2 deletions(-)
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;
}
--
1.9.3