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 "qemu-common.h"
13 #include "libqtest-single.h"
14 #include "qapi/qmp/qdict.h"
15 #include "qapi/qmp/qlist.h"
19 const char *cpu_model
;
26 typedef struct PlugTestData PlugTestData
;
28 static void test_plug_with_cpu_add(gconstpointer data
)
30 const PlugTestData
*s
= data
;
35 args
= g_strdup_printf("-machine %s -cpu %s "
36 "-smp 1,sockets=%u,cores=%u,threads=%u,maxcpus=%u",
37 s
->machine
, s
->cpu_model
,
38 s
->sockets
, s
->cores
, s
->threads
, s
->maxcpus
);
41 for (i
= 1; i
< s
->maxcpus
; i
++) {
42 response
= qmp("{ 'execute': 'cpu-add',"
43 " 'arguments': { 'id': %d } }", i
);
45 g_assert(!qdict_haskey(response
, "error"));
46 qobject_unref(response
);
53 static void test_plug_without_cpu_add(gconstpointer data
)
55 const PlugTestData
*s
= data
;
59 args
= g_strdup_printf("-machine %s -cpu %s "
60 "-smp 1,sockets=%u,cores=%u,threads=%u,maxcpus=%u",
61 s
->machine
, s
->cpu_model
,
62 s
->sockets
, s
->cores
, s
->threads
, s
->maxcpus
);
65 response
= qmp("{ 'execute': 'cpu-add',"
66 " 'arguments': { 'id': %d } }",
67 s
->sockets
* s
->cores
* s
->threads
);
69 g_assert(qdict_haskey(response
, "error"));
70 qobject_unref(response
);
76 static void test_plug_with_device_add(gconstpointer data
)
78 const PlugTestData
*td
= data
;
86 args
= g_strdup_printf("-machine %s -cpu %s "
87 "-smp 1,sockets=%u,cores=%u,threads=%u,maxcpus=%u",
88 td
->machine
, td
->cpu_model
,
89 td
->sockets
, td
->cores
, td
->threads
, td
->maxcpus
);
90 qts
= qtest_init(args
);
92 resp
= qtest_qmp(qts
, "{ 'execute': 'query-hotpluggable-cpus'}");
93 g_assert(qdict_haskey(resp
, "return"));
94 cpus
= qdict_get_qlist(resp
, "return");
97 while ((e
= qlist_pop(cpus
))) {
98 const QDict
*cpu
, *props
;
100 cpu
= qobject_to(QDict
, e
);
101 if (qdict_haskey(cpu
, "qom-path")) {
105 g_assert(qdict_haskey(cpu
, "props"));
106 props
= qdict_get_qdict(cpu
, "props");
108 qtest_qmp_device_add_qdict(qts
, td
->device_model
, props
);
112 /* make sure that there were hotplugged CPUs */
113 g_assert(hotplugged
);
119 static void test_data_free(gpointer data
)
121 PlugTestData
*pc
= data
;
124 g_free(pc
->device_model
);
128 static void add_pc_test_case(const char *mname
)
133 if (!g_str_has_prefix(mname
, "pc-")) {
136 data
= g_new(PlugTestData
, 1);
137 data
->machine
= g_strdup(mname
);
138 data
->cpu_model
= "Haswell"; /* 1.3+ theoretically */
139 data
->device_model
= g_strdup_printf("%s-%s-cpu", data
->cpu_model
,
144 data
->maxcpus
= data
->sockets
* data
->cores
* data
->threads
;
145 if (g_str_has_suffix(mname
, "-1.4") ||
146 (strcmp(mname
, "pc-1.3") == 0) ||
147 (strcmp(mname
, "pc-1.2") == 0) ||
148 (strcmp(mname
, "pc-1.1") == 0) ||
149 (strcmp(mname
, "pc-1.0") == 0) ||
150 (strcmp(mname
, "pc-0.15") == 0) ||
151 (strcmp(mname
, "pc-0.14") == 0) ||
152 (strcmp(mname
, "pc-0.13") == 0) ||
153 (strcmp(mname
, "pc-0.12") == 0)) {
154 path
= g_strdup_printf("cpu-plug/%s/init/%ux%ux%u&maxcpus=%u",
155 mname
, data
->sockets
, data
->cores
,
156 data
->threads
, data
->maxcpus
);
157 qtest_add_data_func_full(path
, data
, test_plug_without_cpu_add
,
161 PlugTestData
*data2
= g_memdup(data
, sizeof(PlugTestData
));
163 data2
->machine
= g_strdup(data
->machine
);
164 data2
->device_model
= g_strdup(data
->device_model
);
166 path
= g_strdup_printf("cpu-plug/%s/cpu-add/%ux%ux%u&maxcpus=%u",
167 mname
, data
->sockets
, data
->cores
,
168 data
->threads
, data
->maxcpus
);
169 qtest_add_data_func_full(path
, data
, test_plug_with_cpu_add
,
172 path
= g_strdup_printf("cpu-plug/%s/device-add/%ux%ux%u&maxcpus=%u",
173 mname
, data2
->sockets
, data2
->cores
,
174 data2
->threads
, data2
->maxcpus
);
175 qtest_add_data_func_full(path
, data2
, test_plug_with_device_add
,
181 static void add_pseries_test_case(const char *mname
)
186 if (!g_str_has_prefix(mname
, "pseries-") ||
187 (g_str_has_prefix(mname
, "pseries-2.") && atoi(&mname
[10]) < 7)) {
190 data
= g_new(PlugTestData
, 1);
191 data
->machine
= g_strdup(mname
);
192 data
->cpu_model
= "power8_v2.0";
193 data
->device_model
= g_strdup("power8_v2.0-spapr-cpu-core");
197 data
->maxcpus
= data
->sockets
* data
->cores
* data
->threads
;
199 path
= g_strdup_printf("cpu-plug/%s/device-add/%ux%ux%u&maxcpus=%u",
200 mname
, data
->sockets
, data
->cores
,
201 data
->threads
, data
->maxcpus
);
202 qtest_add_data_func_full(path
, data
, test_plug_with_device_add
,
207 static void add_s390x_test_case(const char *mname
)
210 PlugTestData
*data
, *data2
;
212 if (!g_str_has_prefix(mname
, "s390-ccw-virtio-")) {
216 data
= g_new(PlugTestData
, 1);
217 data
->machine
= g_strdup(mname
);
218 data
->cpu_model
= "qemu";
219 data
->device_model
= g_strdup("qemu-s390x-cpu");
223 data
->maxcpus
= data
->sockets
* data
->cores
* data
->threads
;
225 data2
= g_memdup(data
, sizeof(PlugTestData
));
226 data2
->machine
= g_strdup(data
->machine
);
227 data2
->device_model
= g_strdup(data
->device_model
);
229 path
= g_strdup_printf("cpu-plug/%s/cpu-add/%ux%ux%u&maxcpus=%u",
230 mname
, data
->sockets
, data
->cores
,
231 data
->threads
, data
->maxcpus
);
232 qtest_add_data_func_full(path
, data
, test_plug_with_cpu_add
,
236 path
= g_strdup_printf("cpu-plug/%s/device-add/%ux%ux%u&maxcpus=%u",
237 mname
, data2
->sockets
, data2
->cores
,
238 data2
->threads
, data2
->maxcpus
);
239 qtest_add_data_func_full(path
, data2
, test_plug_with_device_add
,
244 int main(int argc
, char **argv
)
246 const char *arch
= qtest_get_arch();
248 g_test_init(&argc
, &argv
, NULL
);
250 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
251 qtest_cb_for_every_machine(add_pc_test_case
, g_test_quick());
252 } else if (g_str_equal(arch
, "ppc64")) {
253 qtest_cb_for_every_machine(add_pseries_test_case
, g_test_quick());
254 } else if (g_str_equal(arch
, "s390x")) {
255 qtest_cb_for_every_machine(add_s390x_test_case
, g_test_quick());