2 * QTest testcase for CPU plugging
4 * Copyright (c) 2015 SUSE Linux GmbH
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
10 #include "qemu/osdep.h"
12 #include "libqtest-single.h"
13 #include "qapi/qmp/qdict.h"
14 #include "qapi/qmp/qlist.h"
18 const char *cpu_model
;
25 typedef struct PlugTestData PlugTestData
;
27 static void test_plug_with_device_add(gconstpointer data
)
29 const PlugTestData
*td
= data
;
37 args
= g_strdup_printf("-machine %s -cpu %s "
38 "-smp 1,sockets=%u,cores=%u,threads=%u,maxcpus=%u",
39 td
->machine
, td
->cpu_model
,
40 td
->sockets
, td
->cores
, td
->threads
, td
->maxcpus
);
41 qts
= qtest_init(args
);
43 resp
= qtest_qmp(qts
, "{ 'execute': 'query-hotpluggable-cpus'}");
44 g_assert(qdict_haskey(resp
, "return"));
45 cpus
= qdict_get_qlist(resp
, "return");
48 while ((e
= qlist_pop(cpus
))) {
49 const QDict
*cpu
, *props
;
51 cpu
= qobject_to(QDict
, e
);
52 if (qdict_haskey(cpu
, "qom-path")) {
57 g_assert(qdict_haskey(cpu
, "props"));
58 props
= qdict_get_qdict(cpu
, "props");
60 qtest_qmp_device_add_qdict(qts
, td
->device_model
, props
);
65 /* make sure that there were hotplugged CPUs */
72 static void test_data_free(gpointer data
)
74 PlugTestData
*pc
= data
;
77 g_free(pc
->device_model
);
81 static void add_pc_test_case(const char *mname
)
86 if (!g_str_has_prefix(mname
, "pc-")) {
89 data
= g_new(PlugTestData
, 1);
90 data
->machine
= g_strdup(mname
);
91 data
->cpu_model
= "Haswell"; /* 1.3+ theoretically */
92 data
->device_model
= g_strdup_printf("%s-%s-cpu", data
->cpu_model
,
97 data
->maxcpus
= data
->sockets
* data
->cores
* data
->threads
;
99 path
= g_strdup_printf("cpu-plug/%s/device-add/%ux%ux%u&maxcpus=%u",
100 mname
, data
->sockets
, data
->cores
,
101 data
->threads
, data
->maxcpus
);
102 qtest_add_data_func_full(path
, data
, test_plug_with_device_add
,
107 static void add_pseries_test_case(const char *mname
)
112 if (!g_str_has_prefix(mname
, "pseries-") ||
113 (g_str_has_prefix(mname
, "pseries-2.") && atoi(&mname
[10]) < 7)) {
116 data
= g_new(PlugTestData
, 1);
117 data
->machine
= g_strdup(mname
);
118 data
->cpu_model
= "power8_v2.0";
119 data
->device_model
= g_strdup("power8_v2.0-spapr-cpu-core");
123 data
->maxcpus
= data
->sockets
* data
->cores
* data
->threads
;
125 path
= g_strdup_printf("cpu-plug/%s/device-add/%ux%ux%u&maxcpus=%u",
126 mname
, data
->sockets
, data
->cores
,
127 data
->threads
, data
->maxcpus
);
128 qtest_add_data_func_full(path
, data
, test_plug_with_device_add
,
133 static void add_s390x_test_case(const char *mname
)
138 if (!g_str_has_prefix(mname
, "s390-ccw-virtio-")) {
142 data
= g_new(PlugTestData
, 1);
143 data
->machine
= g_strdup(mname
);
144 data
->cpu_model
= "qemu";
145 data
->device_model
= g_strdup("qemu-s390x-cpu");
149 data
->maxcpus
= data
->sockets
* data
->cores
* data
->threads
;
151 path
= g_strdup_printf("cpu-plug/%s/device-add/%ux%ux%u&maxcpus=%u",
152 mname
, data
->sockets
, data
->cores
,
153 data
->threads
, data
->maxcpus
);
154 qtest_add_data_func_full(path
, data
, test_plug_with_device_add
,
159 int main(int argc
, char **argv
)
161 const char *arch
= qtest_get_arch();
163 g_test_init(&argc
, &argv
, NULL
);
165 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
166 qtest_cb_for_every_machine(add_pc_test_case
, g_test_quick());
167 } else if (g_str_equal(arch
, "ppc64")) {
168 qtest_cb_for_every_machine(add_pseries_test_case
, g_test_quick());
169 } else if (g_str_equal(arch
, "s390x")) {
170 qtest_cb_for_every_machine(add_s390x_test_case
, g_test_quick());