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"
18 const char *cpu_model
;
25 typedef struct PlugTestData PlugTestData
;
27 static void test_plug_with_cpu_add(gconstpointer data
)
29 const PlugTestData
*s
= data
;
34 args
= g_strdup_printf("-machine %s -cpu %s "
35 "-smp 1,sockets=%u,cores=%u,threads=%u,maxcpus=%u",
36 s
->machine
, s
->cpu_model
,
37 s
->sockets
, s
->cores
, s
->threads
, s
->maxcpus
);
40 for (i
= 1; i
< s
->maxcpus
; i
++) {
41 response
= qmp("{ 'execute': 'cpu-add',"
42 " 'arguments': { 'id': %d } }", i
);
44 g_assert(!qdict_haskey(response
, "error"));
45 qobject_unref(response
);
52 static void test_plug_without_cpu_add(gconstpointer data
)
54 const PlugTestData
*s
= data
;
58 args
= g_strdup_printf("-machine %s -cpu %s "
59 "-smp 1,sockets=%u,cores=%u,threads=%u,maxcpus=%u",
60 s
->machine
, s
->cpu_model
,
61 s
->sockets
, s
->cores
, s
->threads
, s
->maxcpus
);
64 response
= qmp("{ 'execute': 'cpu-add',"
65 " 'arguments': { 'id': %d } }",
66 s
->sockets
* s
->cores
* s
->threads
);
68 g_assert(qdict_haskey(response
, "error"));
69 qobject_unref(response
);
75 static void test_plug_with_device_add_x86(gconstpointer data
)
77 const PlugTestData
*td
= data
;
82 args
= g_strdup_printf("-machine %s -cpu %s "
83 "-smp 1,sockets=%u,cores=%u,threads=%u,maxcpus=%u",
84 td
->machine
, td
->cpu_model
,
85 td
->sockets
, td
->cores
, td
->threads
, td
->maxcpus
);
86 qts
= qtest_init(args
);
88 for (s
= 1; s
< td
->sockets
; s
++) {
89 for (c
= 0; c
< td
->cores
; c
++) {
90 for (t
= 0; t
< td
->threads
; t
++) {
91 char *id
= g_strdup_printf("id-%i-%i-%i", s
, c
, t
);
92 qtest_qmp_device_add(qts
, td
->device_model
, id
,
93 "{'socket-id':%u, 'core-id':%u,"
105 static void test_plug_with_device_add_coreid(gconstpointer data
)
107 const PlugTestData
*td
= data
;
112 args
= g_strdup_printf("-machine %s -cpu %s "
113 "-smp 1,sockets=%u,cores=%u,threads=%u,maxcpus=%u",
114 td
->machine
, td
->cpu_model
,
115 td
->sockets
, td
->cores
, td
->threads
, td
->maxcpus
);
116 qts
= qtest_init(args
);
118 for (c
= 1; c
< td
->cores
; c
++) {
119 char *id
= g_strdup_printf("id-%i", c
);
120 qtest_qmp_device_add(qts
, td
->device_model
, id
,
121 "{'core-id':%u}", c
);
129 static void test_data_free(gpointer data
)
131 PlugTestData
*pc
= data
;
134 g_free(pc
->device_model
);
138 static void add_pc_test_case(const char *mname
)
143 if (!g_str_has_prefix(mname
, "pc-")) {
146 data
= g_new(PlugTestData
, 1);
147 data
->machine
= g_strdup(mname
);
148 data
->cpu_model
= "Haswell"; /* 1.3+ theoretically */
149 data
->device_model
= g_strdup_printf("%s-%s-cpu", data
->cpu_model
,
154 data
->maxcpus
= data
->sockets
* data
->cores
* data
->threads
;
155 if (g_str_has_suffix(mname
, "-1.4") ||
156 (strcmp(mname
, "pc-1.3") == 0) ||
157 (strcmp(mname
, "pc-1.2") == 0) ||
158 (strcmp(mname
, "pc-1.1") == 0) ||
159 (strcmp(mname
, "pc-1.0") == 0) ||
160 (strcmp(mname
, "pc-0.15") == 0) ||
161 (strcmp(mname
, "pc-0.14") == 0) ||
162 (strcmp(mname
, "pc-0.13") == 0) ||
163 (strcmp(mname
, "pc-0.12") == 0)) {
164 path
= g_strdup_printf("cpu-plug/%s/init/%ux%ux%u&maxcpus=%u",
165 mname
, data
->sockets
, data
->cores
,
166 data
->threads
, data
->maxcpus
);
167 qtest_add_data_func_full(path
, data
, test_plug_without_cpu_add
,
171 PlugTestData
*data2
= g_memdup(data
, sizeof(PlugTestData
));
173 data2
->machine
= g_strdup(data
->machine
);
174 data2
->device_model
= g_strdup(data
->device_model
);
176 path
= g_strdup_printf("cpu-plug/%s/cpu-add/%ux%ux%u&maxcpus=%u",
177 mname
, data
->sockets
, data
->cores
,
178 data
->threads
, data
->maxcpus
);
179 qtest_add_data_func_full(path
, data
, test_plug_with_cpu_add
,
182 path
= g_strdup_printf("cpu-plug/%s/device-add/%ux%ux%u&maxcpus=%u",
183 mname
, data2
->sockets
, data2
->cores
,
184 data2
->threads
, data2
->maxcpus
);
185 qtest_add_data_func_full(path
, data2
, test_plug_with_device_add_x86
,
191 static void add_pseries_test_case(const char *mname
)
196 if (!g_str_has_prefix(mname
, "pseries-") ||
197 (g_str_has_prefix(mname
, "pseries-2.") && atoi(&mname
[10]) < 7)) {
200 data
= g_new(PlugTestData
, 1);
201 data
->machine
= g_strdup(mname
);
202 data
->cpu_model
= "power8_v2.0";
203 data
->device_model
= g_strdup("power8_v2.0-spapr-cpu-core");
207 data
->maxcpus
= data
->sockets
* data
->cores
* data
->threads
;
209 path
= g_strdup_printf("cpu-plug/%s/device-add/%ux%ux%u&maxcpus=%u",
210 mname
, data
->sockets
, data
->cores
,
211 data
->threads
, data
->maxcpus
);
212 qtest_add_data_func_full(path
, data
, test_plug_with_device_add_coreid
,
217 static void add_s390x_test_case(const char *mname
)
220 PlugTestData
*data
, *data2
;
222 if (!g_str_has_prefix(mname
, "s390-ccw-virtio-")) {
226 data
= g_new(PlugTestData
, 1);
227 data
->machine
= g_strdup(mname
);
228 data
->cpu_model
= "qemu";
229 data
->device_model
= g_strdup("qemu-s390x-cpu");
233 data
->maxcpus
= data
->sockets
* data
->cores
* data
->threads
;
235 data2
= g_memdup(data
, sizeof(PlugTestData
));
236 data2
->machine
= g_strdup(data
->machine
);
237 data2
->device_model
= g_strdup(data
->device_model
);
239 path
= g_strdup_printf("cpu-plug/%s/cpu-add/%ux%ux%u&maxcpus=%u",
240 mname
, data
->sockets
, data
->cores
,
241 data
->threads
, data
->maxcpus
);
242 qtest_add_data_func_full(path
, data
, test_plug_with_cpu_add
,
246 path
= g_strdup_printf("cpu-plug/%s/device-add/%ux%ux%u&maxcpus=%u",
247 mname
, data2
->sockets
, data2
->cores
,
248 data2
->threads
, data2
->maxcpus
);
249 qtest_add_data_func_full(path
, data2
, test_plug_with_device_add_coreid
,
254 int main(int argc
, char **argv
)
256 const char *arch
= qtest_get_arch();
258 g_test_init(&argc
, &argv
, NULL
);
260 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
261 qtest_cb_for_every_machine(add_pc_test_case
, g_test_quick());
262 } else if (g_str_equal(arch
, "ppc64")) {
263 qtest_cb_for_every_machine(add_pseries_test_case
, g_test_quick());
264 } else if (g_str_equal(arch
, "s390x")) {
265 qtest_cb_for_every_machine(add_s390x_test_case
, g_test_quick());