]> git.ipfire.org Git - thirdparty/qemu.git/blame - tests/cpu-plug-test.c
Merge remote-tracking branch 'remotes/maxreitz/tags/pull-block-2019-11-04' into staging
[thirdparty/qemu.git] / tests / cpu-plug-test.c
CommitLineData
7fe55c3c 1/*
152e0393 2 * QTest testcase for CPU plugging
7fe55c3c
AF
3 *
4 * Copyright (c) 2015 SUSE Linux GmbH
5 *
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.
8 */
9
681c28a3 10#include "qemu/osdep.h"
7fe55c3c
AF
11
12#include "qemu-common.h"
dd210749 13#include "libqtest-single.h"
452fcdbc 14#include "qapi/qmp/qdict.h"
021a007e 15#include "qapi/qmp/qlist.h"
7fe55c3c 16
152e0393 17struct PlugTestData {
34e46f60 18 char *machine;
7fe55c3c 19 const char *cpu_model;
80b8c0be 20 char *device_model;
7fe55c3c
AF
21 unsigned sockets;
22 unsigned cores;
23 unsigned threads;
24 unsigned maxcpus;
25};
152e0393 26typedef struct PlugTestData PlugTestData;
7fe55c3c 27
152e0393 28static void test_plug_with_cpu_add(gconstpointer data)
7fe55c3c 29{
152e0393 30 const PlugTestData *s = data;
7fe55c3c
AF
31 char *args;
32 QDict *response;
33 unsigned int i;
34
35 args = g_strdup_printf("-machine %s -cpu %s "
bc1fb850 36 "-smp 1,sockets=%u,cores=%u,threads=%u,maxcpus=%u",
7fe55c3c
AF
37 s->machine, s->cpu_model,
38 s->sockets, s->cores, s->threads, s->maxcpus);
39 qtest_start(args);
40
bc1fb850 41 for (i = 1; i < s->maxcpus; i++) {
7fe55c3c
AF
42 response = qmp("{ 'execute': 'cpu-add',"
43 " 'arguments': { 'id': %d } }", i);
44 g_assert(response);
45 g_assert(!qdict_haskey(response, "error"));
cb3e7f08 46 qobject_unref(response);
7fe55c3c
AF
47 }
48
49 qtest_end();
50 g_free(args);
51}
52
152e0393 53static void test_plug_without_cpu_add(gconstpointer data)
7fe55c3c 54{
152e0393 55 const PlugTestData *s = data;
7fe55c3c
AF
56 char *args;
57 QDict *response;
58
59 args = g_strdup_printf("-machine %s -cpu %s "
bc1fb850 60 "-smp 1,sockets=%u,cores=%u,threads=%u,maxcpus=%u",
7fe55c3c
AF
61 s->machine, s->cpu_model,
62 s->sockets, s->cores, s->threads, s->maxcpus);
63 qtest_start(args);
64
65 response = qmp("{ 'execute': 'cpu-add',"
66 " 'arguments': { 'id': %d } }",
67 s->sockets * s->cores * s->threads);
68 g_assert(response);
69 g_assert(qdict_haskey(response, "error"));
cb3e7f08 70 qobject_unref(response);
7fe55c3c
AF
71
72 qtest_end();
73 g_free(args);
74}
75
021a007e 76static void test_plug_with_device_add(gconstpointer data)
80b8c0be
TH
77{
78 const PlugTestData *td = data;
79 char *args;
e5758de4 80 QTestState *qts;
021a007e
IM
81 QDict *resp;
82 QList *cpus;
83 QObject *e;
84 int hotplugged = 0;
80b8c0be
TH
85
86 args = g_strdup_printf("-machine %s -cpu %s "
bc1fb850 87 "-smp 1,sockets=%u,cores=%u,threads=%u,maxcpus=%u",
80b8c0be
TH
88 td->machine, td->cpu_model,
89 td->sockets, td->cores, td->threads, td->maxcpus);
e5758de4 90 qts = qtest_init(args);
80b8c0be 91
021a007e
IM
92 resp = qtest_qmp(qts, "{ 'execute': 'query-hotpluggable-cpus'}");
93 g_assert(qdict_haskey(resp, "return"));
94 cpus = qdict_get_qlist(resp, "return");
95 g_assert(cpus);
80b8c0be 96
021a007e
IM
97 while ((e = qlist_pop(cpus))) {
98 const QDict *cpu, *props;
80b8c0be 99
021a007e
IM
100 cpu = qobject_to(QDict, e);
101 if (qdict_haskey(cpu, "qom-path")) {
102 continue;
103 }
73a7d31e 104
021a007e
IM
105 g_assert(qdict_haskey(cpu, "props"));
106 props = qdict_get_qdict(cpu, "props");
73a7d31e 107
021a007e
IM
108 qtest_qmp_device_add_qdict(qts, td->device_model, props);
109 hotplugged++;
73a7d31e
TH
110 }
111
021a007e
IM
112 /* make sure that there were hotplugged CPUs */
113 g_assert(hotplugged);
114 qobject_unref(resp);
e5758de4 115 qtest_quit(qts);
73a7d31e
TH
116 g_free(args);
117}
118
34e46f60
MAL
119static void test_data_free(gpointer data)
120{
152e0393 121 PlugTestData *pc = data;
34e46f60
MAL
122
123 g_free(pc->machine);
80b8c0be 124 g_free(pc->device_model);
34e46f60
MAL
125 g_free(pc);
126}
127
02ef6e87 128static void add_pc_test_case(const char *mname)
7fe55c3c 129{
34e46f60 130 char *path;
152e0393 131 PlugTestData *data;
7fe55c3c 132
02ef6e87
TH
133 if (!g_str_has_prefix(mname, "pc-")) {
134 return;
135 }
152e0393 136 data = g_new(PlugTestData, 1);
02ef6e87
TH
137 data->machine = g_strdup(mname);
138 data->cpu_model = "Haswell"; /* 1.3+ theoretically */
80b8c0be
TH
139 data->device_model = g_strdup_printf("%s-%s-cpu", data->cpu_model,
140 qtest_get_arch());
02ef6e87
TH
141 data->sockets = 1;
142 data->cores = 3;
143 data->threads = 2;
bc1fb850 144 data->maxcpus = data->sockets * data->cores * data->threads;
02ef6e87
TH
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) ||
cc425b5d 153 (strcmp(mname, "pc-0.12") == 0)) {
80b8c0be 154 path = g_strdup_printf("cpu-plug/%s/init/%ux%ux%u&maxcpus=%u",
02ef6e87
TH
155 mname, data->sockets, data->cores,
156 data->threads, data->maxcpus);
152e0393 157 qtest_add_data_func_full(path, data, test_plug_without_cpu_add,
02ef6e87
TH
158 test_data_free);
159 g_free(path);
160 } else {
80b8c0be
TH
161 PlugTestData *data2 = g_memdup(data, sizeof(PlugTestData));
162
163 data2->machine = g_strdup(data->machine);
164 data2->device_model = g_strdup(data->device_model);
165
166 path = g_strdup_printf("cpu-plug/%s/cpu-add/%ux%ux%u&maxcpus=%u",
02ef6e87
TH
167 mname, data->sockets, data->cores,
168 data->threads, data->maxcpus);
152e0393 169 qtest_add_data_func_full(path, data, test_plug_with_cpu_add,
02ef6e87
TH
170 test_data_free);
171 g_free(path);
80b8c0be
TH
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);
021a007e 175 qtest_add_data_func_full(path, data2, test_plug_with_device_add,
80b8c0be
TH
176 test_data_free);
177 g_free(path);
7fe55c3c 178 }
7fe55c3c
AF
179}
180
73a7d31e
TH
181static void add_pseries_test_case(const char *mname)
182{
183 char *path;
184 PlugTestData *data;
185
186 if (!g_str_has_prefix(mname, "pseries-") ||
187 (g_str_has_prefix(mname, "pseries-2.") && atoi(&mname[10]) < 7)) {
188 return;
189 }
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");
194 data->sockets = 2;
195 data->cores = 3;
196 data->threads = 1;
bc1fb850 197 data->maxcpus = data->sockets * data->cores * data->threads;
73a7d31e
TH
198
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);
021a007e 202 qtest_add_data_func_full(path, data, test_plug_with_device_add,
73a7d31e
TH
203 test_data_free);
204 g_free(path);
205}
206
7d8b00fa
TH
207static void add_s390x_test_case(const char *mname)
208{
209 char *path;
210 PlugTestData *data, *data2;
211
212 if (!g_str_has_prefix(mname, "s390-ccw-virtio-")) {
213 return;
214 }
215
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");
220 data->sockets = 1;
221 data->cores = 3;
222 data->threads = 1;
bc1fb850 223 data->maxcpus = data->sockets * data->cores * data->threads;
7d8b00fa
TH
224
225 data2 = g_memdup(data, sizeof(PlugTestData));
226 data2->machine = g_strdup(data->machine);
227 data2->device_model = g_strdup(data->device_model);
228
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,
233 test_data_free);
234 g_free(path);
235
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);
021a007e 239 qtest_add_data_func_full(path, data2, test_plug_with_device_add,
7d8b00fa
TH
240 test_data_free);
241 g_free(path);
242}
243
7fe55c3c
AF
244int main(int argc, char **argv)
245{
246 const char *arch = qtest_get_arch();
247
248 g_test_init(&argc, &argv, NULL);
249
250 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
1f4a0d81 251 qtest_cb_for_every_machine(add_pc_test_case, g_test_quick());
73a7d31e 252 } else if (g_str_equal(arch, "ppc64")) {
1f4a0d81 253 qtest_cb_for_every_machine(add_pseries_test_case, g_test_quick());
7d8b00fa 254 } else if (g_str_equal(arch, "s390x")) {
1f4a0d81 255 qtest_cb_for_every_machine(add_s390x_test_case, g_test_quick());
7fe55c3c
AF
256 }
257
258 return g_test_run();
259}