spapr: Clean up local variable shadowing in spapr_dt_cpus()
[qemu/kevin.git] / tests / qtest / cpu-plug-test.c
blob7f5dd5f85a787701b5941fe9833c2f83370ca04b
1 /*
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.
8 */
10 #include "qemu/osdep.h"
12 #include "libqtest-single.h"
13 #include "qapi/qmp/qdict.h"
14 #include "qapi/qmp/qlist.h"
16 struct PlugTestData {
17 char *machine;
18 const char *cpu_model;
19 char *device_model;
20 unsigned sockets;
21 unsigned cores;
22 unsigned threads;
23 unsigned maxcpus;
25 typedef struct PlugTestData PlugTestData;
27 static void test_plug_with_device_add(gconstpointer data)
29 const PlugTestData *td = data;
30 char *args;
31 QTestState *qts;
32 QDict *resp;
33 QList *cpus;
34 QObject *e;
35 int hotplugged = 0;
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");
46 g_assert(cpus);
48 while ((e = qlist_pop(cpus))) {
49 const QDict *cpu, *props;
51 cpu = qobject_to(QDict, e);
52 if (qdict_haskey(cpu, "qom-path")) {
53 qobject_unref(e);
54 continue;
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);
61 hotplugged++;
62 qobject_unref(e);
65 /* make sure that there were hotplugged CPUs */
66 g_assert(hotplugged);
67 qobject_unref(resp);
68 qtest_quit(qts);
69 g_free(args);
72 static void test_data_free(gpointer data)
74 PlugTestData *pc = data;
76 g_free(pc->machine);
77 g_free(pc->device_model);
78 g_free(pc);
81 static void add_pc_test_case(const char *mname)
83 char *path;
84 PlugTestData *data;
86 if (!g_str_has_prefix(mname, "pc-")) {
87 return;
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,
93 qtest_get_arch());
94 data->sockets = 1;
95 data->cores = 3;
96 data->threads = 2;
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,
103 test_data_free);
104 g_free(path);
107 static void add_pseries_test_case(const char *mname)
109 char *path;
110 PlugTestData *data;
112 if (!g_str_has_prefix(mname, "pseries-") ||
113 (g_str_has_prefix(mname, "pseries-2.") && atoi(&mname[10]) < 7)) {
114 return;
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");
120 data->sockets = 2;
121 data->cores = 3;
122 data->threads = 1;
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,
129 test_data_free);
130 g_free(path);
133 static void add_s390x_test_case(const char *mname)
135 char *path;
136 PlugTestData *data;
138 if (!g_str_has_prefix(mname, "s390-ccw-virtio-")) {
139 return;
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");
146 data->sockets = 1;
147 data->cores = 3;
148 data->threads = 1;
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,
155 test_data_free);
156 g_free(path);
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());
173 return g_test_run();