xref: /qemu/tests/qtest/cpu-plug-test.c (revision 63e79833c46fa6d21efb1c4d966b0a204c4b7b0f)
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 
127fe55c3cSAndreas Färber #include "qemu-common.h"
13dd210749SThomas Huth #include "libqtest-single.h"
14452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h"
15021a007eSIgor Mammedov #include "qapi/qmp/qlist.h"
167fe55c3cSAndreas Färber 
17152e0393SThomas Huth struct PlugTestData {
1834e46f60SMarc-André Lureau     char *machine;
197fe55c3cSAndreas Färber     const char *cpu_model;
2080b8c0beSThomas Huth     char *device_model;
217fe55c3cSAndreas Färber     unsigned sockets;
227fe55c3cSAndreas Färber     unsigned cores;
237fe55c3cSAndreas Färber     unsigned threads;
247fe55c3cSAndreas Färber     unsigned maxcpus;
257fe55c3cSAndreas Färber };
26152e0393SThomas Huth typedef struct PlugTestData PlugTestData;
277fe55c3cSAndreas Färber 
28021a007eSIgor Mammedov static void test_plug_with_device_add(gconstpointer data)
2980b8c0beSThomas Huth {
3080b8c0beSThomas Huth     const PlugTestData *td = data;
3180b8c0beSThomas Huth     char *args;
32e5758de4SThomas Huth     QTestState *qts;
33021a007eSIgor Mammedov     QDict *resp;
34021a007eSIgor Mammedov     QList *cpus;
35021a007eSIgor Mammedov     QObject *e;
36021a007eSIgor Mammedov     int hotplugged = 0;
3780b8c0beSThomas Huth 
3880b8c0beSThomas Huth     args = g_strdup_printf("-machine %s -cpu %s "
39bc1fb850SIgor Mammedov                            "-smp 1,sockets=%u,cores=%u,threads=%u,maxcpus=%u",
4080b8c0beSThomas Huth                            td->machine, td->cpu_model,
4180b8c0beSThomas Huth                            td->sockets, td->cores, td->threads, td->maxcpus);
42e5758de4SThomas Huth     qts = qtest_init(args);
4380b8c0beSThomas Huth 
44021a007eSIgor Mammedov     resp = qtest_qmp(qts, "{ 'execute': 'query-hotpluggable-cpus'}");
45021a007eSIgor Mammedov     g_assert(qdict_haskey(resp, "return"));
46021a007eSIgor Mammedov     cpus = qdict_get_qlist(resp, "return");
47021a007eSIgor Mammedov     g_assert(cpus);
48021a007eSIgor Mammedov 
49021a007eSIgor Mammedov     while ((e = qlist_pop(cpus))) {
50021a007eSIgor Mammedov         const QDict *cpu, *props;
51021a007eSIgor Mammedov 
52021a007eSIgor Mammedov         cpu = qobject_to(QDict, e);
53021a007eSIgor Mammedov         if (qdict_haskey(cpu, "qom-path")) {
5474130913SMarc-André Lureau             qobject_unref(e);
55021a007eSIgor Mammedov             continue;
5680b8c0beSThomas Huth         }
5780b8c0beSThomas Huth 
58021a007eSIgor Mammedov         g_assert(qdict_haskey(cpu, "props"));
59021a007eSIgor Mammedov         props = qdict_get_qdict(cpu, "props");
60021a007eSIgor Mammedov 
61021a007eSIgor Mammedov         qtest_qmp_device_add_qdict(qts, td->device_model, props);
62021a007eSIgor Mammedov         hotplugged++;
6374130913SMarc-André Lureau         qobject_unref(e);
6480b8c0beSThomas Huth     }
6580b8c0beSThomas Huth 
66021a007eSIgor Mammedov     /* make sure that there were hotplugged CPUs */
67021a007eSIgor Mammedov     g_assert(hotplugged);
68021a007eSIgor Mammedov     qobject_unref(resp);
69e5758de4SThomas Huth     qtest_quit(qts);
7073a7d31eSThomas Huth     g_free(args);
7173a7d31eSThomas Huth }
7273a7d31eSThomas Huth 
7334e46f60SMarc-André Lureau static void test_data_free(gpointer data)
7434e46f60SMarc-André Lureau {
75152e0393SThomas Huth     PlugTestData *pc = data;
7634e46f60SMarc-André Lureau 
7734e46f60SMarc-André Lureau     g_free(pc->machine);
7880b8c0beSThomas Huth     g_free(pc->device_model);
7934e46f60SMarc-André Lureau     g_free(pc);
8034e46f60SMarc-André Lureau }
8134e46f60SMarc-André Lureau 
8202ef6e87SThomas Huth static void add_pc_test_case(const char *mname)
837fe55c3cSAndreas Färber {
8434e46f60SMarc-André Lureau     char *path;
85152e0393SThomas Huth     PlugTestData *data;
867fe55c3cSAndreas Färber 
877fe55c3cSAndreas Färber     if (!g_str_has_prefix(mname, "pc-")) {
8802ef6e87SThomas Huth         return;
897fe55c3cSAndreas Färber     }
90152e0393SThomas Huth     data = g_new(PlugTestData, 1);
9134e46f60SMarc-André Lureau     data->machine = g_strdup(mname);
927fe55c3cSAndreas Färber     data->cpu_model = "Haswell"; /* 1.3+ theoretically */
9380b8c0beSThomas Huth     data->device_model = g_strdup_printf("%s-%s-cpu", data->cpu_model,
9480b8c0beSThomas Huth                                          qtest_get_arch());
957fe55c3cSAndreas Färber     data->sockets = 1;
967fe55c3cSAndreas Färber     data->cores = 3;
977fe55c3cSAndreas Färber     data->threads = 2;
98bc1fb850SIgor Mammedov     data->maxcpus = data->sockets * data->cores * data->threads;
9980b8c0beSThomas Huth 
10080b8c0beSThomas Huth     path = g_strdup_printf("cpu-plug/%s/device-add/%ux%ux%u&maxcpus=%u",
101*63e79833SIgor Mammedov                            mname, data->sockets, data->cores,
102*63e79833SIgor Mammedov                            data->threads, data->maxcpus);
103*63e79833SIgor Mammedov     qtest_add_data_func_full(path, data, test_plug_with_device_add,
10480b8c0beSThomas Huth                              test_data_free);
10580b8c0beSThomas Huth     g_free(path);
1067fe55c3cSAndreas Färber }
1077fe55c3cSAndreas Färber 
10873a7d31eSThomas Huth static void add_pseries_test_case(const char *mname)
10973a7d31eSThomas Huth {
11073a7d31eSThomas Huth     char *path;
11173a7d31eSThomas Huth     PlugTestData *data;
11273a7d31eSThomas Huth 
11373a7d31eSThomas Huth     if (!g_str_has_prefix(mname, "pseries-") ||
11473a7d31eSThomas Huth         (g_str_has_prefix(mname, "pseries-2.") && atoi(&mname[10]) < 7)) {
11573a7d31eSThomas Huth         return;
11673a7d31eSThomas Huth     }
11773a7d31eSThomas Huth     data = g_new(PlugTestData, 1);
11873a7d31eSThomas Huth     data->machine = g_strdup(mname);
11973a7d31eSThomas Huth     data->cpu_model = "power8_v2.0";
12073a7d31eSThomas Huth     data->device_model = g_strdup("power8_v2.0-spapr-cpu-core");
12173a7d31eSThomas Huth     data->sockets = 2;
12273a7d31eSThomas Huth     data->cores = 3;
12373a7d31eSThomas Huth     data->threads = 1;
124bc1fb850SIgor Mammedov     data->maxcpus = data->sockets * data->cores * data->threads;
12573a7d31eSThomas Huth 
12673a7d31eSThomas Huth     path = g_strdup_printf("cpu-plug/%s/device-add/%ux%ux%u&maxcpus=%u",
12773a7d31eSThomas Huth                            mname, data->sockets, data->cores,
12873a7d31eSThomas Huth                            data->threads, data->maxcpus);
129021a007eSIgor Mammedov     qtest_add_data_func_full(path, data, test_plug_with_device_add,
13073a7d31eSThomas Huth                              test_data_free);
13173a7d31eSThomas Huth     g_free(path);
13273a7d31eSThomas Huth }
13373a7d31eSThomas Huth 
1347d8b00faSThomas Huth static void add_s390x_test_case(const char *mname)
1357d8b00faSThomas Huth {
1367d8b00faSThomas Huth     char *path;
137*63e79833SIgor Mammedov     PlugTestData *data;
1387d8b00faSThomas Huth 
1397d8b00faSThomas Huth     if (!g_str_has_prefix(mname, "s390-ccw-virtio-")) {
1407d8b00faSThomas Huth         return;
1417d8b00faSThomas Huth     }
1427d8b00faSThomas Huth 
1437d8b00faSThomas Huth     data = g_new(PlugTestData, 1);
1447d8b00faSThomas Huth     data->machine = g_strdup(mname);
1457d8b00faSThomas Huth     data->cpu_model = "qemu";
1467d8b00faSThomas Huth     data->device_model = g_strdup("qemu-s390x-cpu");
1477d8b00faSThomas Huth     data->sockets = 1;
1487d8b00faSThomas Huth     data->cores = 3;
1497d8b00faSThomas Huth     data->threads = 1;
150bc1fb850SIgor Mammedov     data->maxcpus = data->sockets * data->cores * data->threads;
1517d8b00faSThomas Huth 
152*63e79833SIgor Mammedov     path = g_strdup_printf("cpu-plug/%s/device-add/%ux%ux%u&maxcpus=%u",
1537d8b00faSThomas Huth                            mname, data->sockets, data->cores,
1547d8b00faSThomas Huth                            data->threads, data->maxcpus);
155*63e79833SIgor Mammedov     qtest_add_data_func_full(path, data, test_plug_with_device_add,
1567d8b00faSThomas Huth                              test_data_free);
1577d8b00faSThomas Huth     g_free(path);
1587d8b00faSThomas Huth }
1597d8b00faSThomas Huth 
1607fe55c3cSAndreas Färber int main(int argc, char **argv)
1617fe55c3cSAndreas Färber {
1627fe55c3cSAndreas Färber     const char *arch = qtest_get_arch();
1637fe55c3cSAndreas Färber 
1647fe55c3cSAndreas Färber     g_test_init(&argc, &argv, NULL);
1657fe55c3cSAndreas Färber 
1667fe55c3cSAndreas Färber     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
1671f4a0d81SThomas Huth         qtest_cb_for_every_machine(add_pc_test_case, g_test_quick());
16873a7d31eSThomas Huth     } else if (g_str_equal(arch, "ppc64")) {
1691f4a0d81SThomas Huth         qtest_cb_for_every_machine(add_pseries_test_case, g_test_quick());
1707d8b00faSThomas Huth     } else if (g_str_equal(arch, "s390x")) {
1711f4a0d81SThomas Huth         qtest_cb_for_every_machine(add_s390x_test_case, g_test_quick());
1727fe55c3cSAndreas Färber     }
1737fe55c3cSAndreas Färber 
1747fe55c3cSAndreas Färber     return g_test_run();
1757fe55c3cSAndreas Färber }
176