xref: /qemu/tests/qtest/cpu-plug-test.c (revision 98008aa41b93b2477782205dcbd9ce5978f26a7e)
17fe55c3cSAndreas Färber /*
2152e0393SThomas Huth  * QTest testcase for CPU plugging
37fe55c3cSAndreas Färber  *
47fe55c3cSAndreas Färber  * Copyright (c) 2015 SUSE Linux GmbH
57fe55c3cSAndreas Färber  *
67fe55c3cSAndreas Färber  * This work is licensed under the terms of the GNU GPL, version 2 or later.
77fe55c3cSAndreas Färber  * See the COPYING file in the top-level directory.
87fe55c3cSAndreas Färber  */
97fe55c3cSAndreas Färber 
10681c28a3SPeter Maydell #include "qemu/osdep.h"
117fe55c3cSAndreas Färber 
12dd210749SThomas Huth #include "libqtest-single.h"
13407bc4bfSDaniel P. Berrangé #include "qobject/qdict.h"
14407bc4bfSDaniel P. Berrangé #include "qobject/qlist.h"
157fe55c3cSAndreas Färber 
16152e0393SThomas Huth struct PlugTestData {
1734e46f60SMarc-André Lureau     char *machine;
187fe55c3cSAndreas Färber     const char *cpu_model;
1980b8c0beSThomas Huth     char *device_model;
207fe55c3cSAndreas Färber     unsigned sockets;
217fe55c3cSAndreas Färber     unsigned cores;
227fe55c3cSAndreas Färber     unsigned threads;
237fe55c3cSAndreas Färber     unsigned maxcpus;
247fe55c3cSAndreas Färber };
25152e0393SThomas Huth typedef struct PlugTestData PlugTestData;
267fe55c3cSAndreas Färber 
test_plug_with_device_add(gconstpointer data)27021a007eSIgor Mammedov static void test_plug_with_device_add(gconstpointer data)
2880b8c0beSThomas Huth {
2980b8c0beSThomas Huth     const PlugTestData *td = data;
3080b8c0beSThomas Huth     char *args;
31e5758de4SThomas Huth     QTestState *qts;
32021a007eSIgor Mammedov     QDict *resp;
33021a007eSIgor Mammedov     QList *cpus;
34021a007eSIgor Mammedov     QObject *e;
35021a007eSIgor Mammedov     int hotplugged = 0;
3680b8c0beSThomas Huth 
3780b8c0beSThomas Huth     args = g_strdup_printf("-machine %s -cpu %s "
38bc1fb850SIgor Mammedov                            "-smp 1,sockets=%u,cores=%u,threads=%u,maxcpus=%u",
3980b8c0beSThomas Huth                            td->machine, td->cpu_model,
4080b8c0beSThomas Huth                            td->sockets, td->cores, td->threads, td->maxcpus);
41e5758de4SThomas Huth     qts = qtest_init(args);
4280b8c0beSThomas Huth 
43021a007eSIgor Mammedov     resp = qtest_qmp(qts, "{ 'execute': 'query-hotpluggable-cpus'}");
44021a007eSIgor Mammedov     g_assert(qdict_haskey(resp, "return"));
45021a007eSIgor Mammedov     cpus = qdict_get_qlist(resp, "return");
46021a007eSIgor Mammedov     g_assert(cpus);
47021a007eSIgor Mammedov 
48021a007eSIgor Mammedov     while ((e = qlist_pop(cpus))) {
49021a007eSIgor Mammedov         const QDict *cpu, *props;
50021a007eSIgor Mammedov 
51021a007eSIgor Mammedov         cpu = qobject_to(QDict, e);
52021a007eSIgor Mammedov         if (qdict_haskey(cpu, "qom-path")) {
5374130913SMarc-André Lureau             qobject_unref(e);
54021a007eSIgor Mammedov             continue;
5580b8c0beSThomas Huth         }
5680b8c0beSThomas Huth 
57021a007eSIgor Mammedov         g_assert(qdict_haskey(cpu, "props"));
58021a007eSIgor Mammedov         props = qdict_get_qdict(cpu, "props");
59021a007eSIgor Mammedov 
60021a007eSIgor Mammedov         qtest_qmp_device_add_qdict(qts, td->device_model, props);
61021a007eSIgor Mammedov         hotplugged++;
6274130913SMarc-André Lureau         qobject_unref(e);
6380b8c0beSThomas Huth     }
6480b8c0beSThomas Huth 
65021a007eSIgor Mammedov     /* make sure that there were hotplugged CPUs */
66021a007eSIgor Mammedov     g_assert(hotplugged);
67021a007eSIgor Mammedov     qobject_unref(resp);
68e5758de4SThomas Huth     qtest_quit(qts);
6973a7d31eSThomas Huth     g_free(args);
7073a7d31eSThomas Huth }
7173a7d31eSThomas Huth 
test_data_free(gpointer data)7234e46f60SMarc-André Lureau static void test_data_free(gpointer data)
7334e46f60SMarc-André Lureau {
74152e0393SThomas Huth     PlugTestData *pc = data;
7534e46f60SMarc-André Lureau 
7634e46f60SMarc-André Lureau     g_free(pc->machine);
7780b8c0beSThomas Huth     g_free(pc->device_model);
7834e46f60SMarc-André Lureau     g_free(pc);
7934e46f60SMarc-André Lureau }
8034e46f60SMarc-André Lureau 
add_pc_test_case(const char * mname)8102ef6e87SThomas Huth static void add_pc_test_case(const char *mname)
827fe55c3cSAndreas Färber {
8334e46f60SMarc-André Lureau     char *path;
84152e0393SThomas Huth     PlugTestData *data;
857fe55c3cSAndreas Färber 
867fe55c3cSAndreas Färber     if (!g_str_has_prefix(mname, "pc-")) {
8702ef6e87SThomas Huth         return;
887fe55c3cSAndreas Färber     }
89152e0393SThomas Huth     data = g_new(PlugTestData, 1);
9034e46f60SMarc-André Lureau     data->machine = g_strdup(mname);
917fe55c3cSAndreas Färber     data->cpu_model = "Haswell"; /* 1.3+ theoretically */
9280b8c0beSThomas Huth     data->device_model = g_strdup_printf("%s-%s-cpu", data->cpu_model,
9380b8c0beSThomas Huth                                          qtest_get_arch());
947fe55c3cSAndreas Färber     data->sockets = 1;
957fe55c3cSAndreas Färber     data->cores = 3;
967fe55c3cSAndreas Färber     data->threads = 2;
97bc1fb850SIgor Mammedov     data->maxcpus = data->sockets * data->cores * data->threads;
9880b8c0beSThomas Huth 
9980b8c0beSThomas Huth     path = g_strdup_printf("cpu-plug/%s/device-add/%ux%ux%u&maxcpus=%u",
10063e79833SIgor Mammedov                            mname, data->sockets, data->cores,
10163e79833SIgor Mammedov                            data->threads, data->maxcpus);
10263e79833SIgor Mammedov     qtest_add_data_func_full(path, data, test_plug_with_device_add,
10380b8c0beSThomas Huth                              test_data_free);
10480b8c0beSThomas Huth     g_free(path);
1057fe55c3cSAndreas Färber }
1067fe55c3cSAndreas Färber 
add_pseries_test_case(const char * mname)10773a7d31eSThomas Huth static void add_pseries_test_case(const char *mname)
10873a7d31eSThomas Huth {
10973a7d31eSThomas Huth     char *path;
11073a7d31eSThomas Huth     PlugTestData *data;
11173a7d31eSThomas Huth 
11273a7d31eSThomas Huth     if (!g_str_has_prefix(mname, "pseries-") ||
11373a7d31eSThomas Huth         (g_str_has_prefix(mname, "pseries-2.") && atoi(&mname[10]) < 7)) {
11473a7d31eSThomas Huth         return;
11573a7d31eSThomas Huth     }
11673a7d31eSThomas Huth     data = g_new(PlugTestData, 1);
11773a7d31eSThomas Huth     data->machine = g_strdup(mname);
11873a7d31eSThomas Huth     data->cpu_model = "power8_v2.0";
11973a7d31eSThomas Huth     data->device_model = g_strdup("power8_v2.0-spapr-cpu-core");
12073a7d31eSThomas Huth     data->sockets = 2;
12173a7d31eSThomas Huth     data->cores = 3;
12273a7d31eSThomas Huth     data->threads = 1;
123bc1fb850SIgor Mammedov     data->maxcpus = data->sockets * data->cores * data->threads;
12473a7d31eSThomas Huth 
12573a7d31eSThomas Huth     path = g_strdup_printf("cpu-plug/%s/device-add/%ux%ux%u&maxcpus=%u",
12673a7d31eSThomas Huth                            mname, data->sockets, data->cores,
12773a7d31eSThomas Huth                            data->threads, data->maxcpus);
128021a007eSIgor Mammedov     qtest_add_data_func_full(path, data, test_plug_with_device_add,
12973a7d31eSThomas Huth                              test_data_free);
13073a7d31eSThomas Huth     g_free(path);
13173a7d31eSThomas Huth }
13273a7d31eSThomas Huth 
add_s390x_test_case(const char * mname)1337d8b00faSThomas Huth static void add_s390x_test_case(const char *mname)
1347d8b00faSThomas Huth {
1357d8b00faSThomas Huth     char *path;
13663e79833SIgor Mammedov     PlugTestData *data;
1377d8b00faSThomas Huth 
1387d8b00faSThomas Huth     if (!g_str_has_prefix(mname, "s390-ccw-virtio-")) {
1397d8b00faSThomas Huth         return;
1407d8b00faSThomas Huth     }
1417d8b00faSThomas Huth 
1427d8b00faSThomas Huth     data = g_new(PlugTestData, 1);
1437d8b00faSThomas Huth     data->machine = g_strdup(mname);
1447d8b00faSThomas Huth     data->cpu_model = "qemu";
1457d8b00faSThomas Huth     data->device_model = g_strdup("qemu-s390x-cpu");
1467d8b00faSThomas Huth     data->sockets = 1;
1477d8b00faSThomas Huth     data->cores = 3;
1487d8b00faSThomas Huth     data->threads = 1;
149bc1fb850SIgor Mammedov     data->maxcpus = data->sockets * data->cores * data->threads;
1507d8b00faSThomas Huth 
15163e79833SIgor Mammedov     path = g_strdup_printf("cpu-plug/%s/device-add/%ux%ux%u&maxcpus=%u",
1527d8b00faSThomas Huth                            mname, data->sockets, data->cores,
1537d8b00faSThomas Huth                            data->threads, data->maxcpus);
15463e79833SIgor Mammedov     qtest_add_data_func_full(path, data, test_plug_with_device_add,
1557d8b00faSThomas Huth                              test_data_free);
1567d8b00faSThomas Huth     g_free(path);
1577d8b00faSThomas Huth }
1587d8b00faSThomas Huth 
add_loongarch_test_case(const char * mname)159*98008aa4SBibo Mao static void add_loongarch_test_case(const char *mname)
160*98008aa4SBibo Mao {
161*98008aa4SBibo Mao     char *path;
162*98008aa4SBibo Mao     PlugTestData *data;
163*98008aa4SBibo Mao 
164*98008aa4SBibo Mao     data = g_new(PlugTestData, 1);
165*98008aa4SBibo Mao     data->machine = g_strdup(mname);
166*98008aa4SBibo Mao     data->cpu_model = "la464";
167*98008aa4SBibo Mao     data->device_model = g_strdup("la464-loongarch-cpu");
168*98008aa4SBibo Mao     data->sockets = 1;
169*98008aa4SBibo Mao     data->cores = 3;
170*98008aa4SBibo Mao     data->threads = 1;
171*98008aa4SBibo Mao     data->maxcpus = data->sockets * data->cores * data->threads;
172*98008aa4SBibo Mao 
173*98008aa4SBibo Mao     path = g_strdup_printf("cpu-plug/%s/device-add/%ux%ux%u&maxcpus=%u",
174*98008aa4SBibo Mao                            mname, data->sockets, data->cores,
175*98008aa4SBibo Mao                            data->threads, data->maxcpus);
176*98008aa4SBibo Mao     qtest_add_data_func_full(path, data, test_plug_with_device_add,
177*98008aa4SBibo Mao                              test_data_free);
178*98008aa4SBibo Mao     g_free(path);
179*98008aa4SBibo Mao }
180*98008aa4SBibo Mao 
main(int argc,char ** argv)1817fe55c3cSAndreas Färber int main(int argc, char **argv)
1827fe55c3cSAndreas Färber {
1837fe55c3cSAndreas Färber     const char *arch = qtest_get_arch();
1847fe55c3cSAndreas Färber 
1857fe55c3cSAndreas Färber     g_test_init(&argc, &argv, NULL);
1867fe55c3cSAndreas Färber 
1877fe55c3cSAndreas Färber     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
1881f4a0d81SThomas Huth         qtest_cb_for_every_machine(add_pc_test_case, g_test_quick());
18973a7d31eSThomas Huth     } else if (g_str_equal(arch, "ppc64")) {
1901f4a0d81SThomas Huth         qtest_cb_for_every_machine(add_pseries_test_case, g_test_quick());
1917d8b00faSThomas Huth     } else if (g_str_equal(arch, "s390x")) {
1921f4a0d81SThomas Huth         qtest_cb_for_every_machine(add_s390x_test_case, g_test_quick());
193*98008aa4SBibo Mao     } else if (g_str_equal(arch, "loongarch64")) {
194*98008aa4SBibo Mao         add_loongarch_test_case("virt");
1957fe55c3cSAndreas Färber     }
1967fe55c3cSAndreas Färber 
1977fe55c3cSAndreas Färber     return g_test_run();
1987fe55c3cSAndreas Färber }
199