xref: /qemu/tests/qtest/cpu-plug-test.c (revision 53f77e4562f85ccf82c8831a4448e9aefb538837)
17fe55c3cSAndreas Färber /*
27fe55c3cSAndreas Färber  * QTest testcase for PC CPUs
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 
107fe55c3cSAndreas Färber #include <glib.h>
117fe55c3cSAndreas Färber #include <string.h>
127fe55c3cSAndreas Färber 
137fe55c3cSAndreas Färber #include "qemu-common.h"
147fe55c3cSAndreas Färber #include "libqtest.h"
157fe55c3cSAndreas Färber #include "qemu/osdep.h"
167fe55c3cSAndreas Färber #include "qapi/qmp/types.h"
177fe55c3cSAndreas Färber 
187fe55c3cSAndreas Färber struct PCTestData {
197fe55c3cSAndreas Färber     const char *machine;
207fe55c3cSAndreas Färber     const char *cpu_model;
217fe55c3cSAndreas Färber     unsigned sockets;
227fe55c3cSAndreas Färber     unsigned cores;
237fe55c3cSAndreas Färber     unsigned threads;
247fe55c3cSAndreas Färber     unsigned maxcpus;
257fe55c3cSAndreas Färber };
267fe55c3cSAndreas Färber typedef struct PCTestData PCTestData;
277fe55c3cSAndreas Färber 
287fe55c3cSAndreas Färber static void test_pc_with_cpu_add(gconstpointer data)
297fe55c3cSAndreas Färber {
307fe55c3cSAndreas Färber     const PCTestData *s = data;
317fe55c3cSAndreas Färber     char *args;
327fe55c3cSAndreas Färber     QDict *response;
337fe55c3cSAndreas Färber     unsigned int i;
347fe55c3cSAndreas Färber 
357fe55c3cSAndreas Färber     args = g_strdup_printf("-machine %s -cpu %s "
367fe55c3cSAndreas Färber                            "-smp sockets=%u,cores=%u,threads=%u,maxcpus=%u",
377fe55c3cSAndreas Färber                            s->machine, s->cpu_model,
387fe55c3cSAndreas Färber                            s->sockets, s->cores, s->threads, s->maxcpus);
397fe55c3cSAndreas Färber     qtest_start(args);
407fe55c3cSAndreas Färber 
417fe55c3cSAndreas Färber     for (i = s->sockets * s->cores * s->threads; i < s->maxcpus; i++) {
427fe55c3cSAndreas Färber         response = qmp("{ 'execute': 'cpu-add',"
437fe55c3cSAndreas Färber                        "  'arguments': { 'id': %d } }", i);
447fe55c3cSAndreas Färber         g_assert(response);
457fe55c3cSAndreas Färber         g_assert(!qdict_haskey(response, "error"));
467fe55c3cSAndreas Färber         QDECREF(response);
477fe55c3cSAndreas Färber     }
487fe55c3cSAndreas Färber 
497fe55c3cSAndreas Färber     qtest_end();
507fe55c3cSAndreas Färber     g_free(args);
517fe55c3cSAndreas Färber }
527fe55c3cSAndreas Färber 
537fe55c3cSAndreas Färber static void test_pc_without_cpu_add(gconstpointer data)
547fe55c3cSAndreas Färber {
557fe55c3cSAndreas Färber     const PCTestData *s = data;
567fe55c3cSAndreas Färber     char *args;
577fe55c3cSAndreas Färber     QDict *response;
587fe55c3cSAndreas Färber 
597fe55c3cSAndreas Färber     args = g_strdup_printf("-machine %s -cpu %s "
607fe55c3cSAndreas Färber                            "-smp sockets=%u,cores=%u,threads=%u,maxcpus=%u",
617fe55c3cSAndreas Färber                            s->machine, s->cpu_model,
627fe55c3cSAndreas Färber                            s->sockets, s->cores, s->threads, s->maxcpus);
637fe55c3cSAndreas Färber     qtest_start(args);
647fe55c3cSAndreas Färber 
657fe55c3cSAndreas Färber     response = qmp("{ 'execute': 'cpu-add',"
667fe55c3cSAndreas Färber                    "  'arguments': { 'id': %d } }",
677fe55c3cSAndreas Färber                    s->sockets * s->cores * s->threads);
687fe55c3cSAndreas Färber     g_assert(response);
697fe55c3cSAndreas Färber     g_assert(qdict_haskey(response, "error"));
707fe55c3cSAndreas Färber     QDECREF(response);
717fe55c3cSAndreas Färber 
727fe55c3cSAndreas Färber     qtest_end();
737fe55c3cSAndreas Färber     g_free(args);
747fe55c3cSAndreas Färber }
757fe55c3cSAndreas Färber 
767fe55c3cSAndreas Färber static void add_pc_test_cases(void)
777fe55c3cSAndreas Färber {
787fe55c3cSAndreas Färber     QDict *response, *minfo;
797fe55c3cSAndreas Färber     QList *list;
807fe55c3cSAndreas Färber     const QListEntry *p;
817fe55c3cSAndreas Färber     QObject *qobj;
827fe55c3cSAndreas Färber     QString *qstr;
837fe55c3cSAndreas Färber     const char *mname, *path;
847fe55c3cSAndreas Färber     PCTestData *data;
857fe55c3cSAndreas Färber 
867fe55c3cSAndreas Färber     qtest_start("-machine none");
877fe55c3cSAndreas Färber     response = qmp("{ 'execute': 'query-machines' }");
887fe55c3cSAndreas Färber     g_assert(response);
897fe55c3cSAndreas Färber     list = qdict_get_qlist(response, "return");
907fe55c3cSAndreas Färber     g_assert(list);
917fe55c3cSAndreas Färber 
927fe55c3cSAndreas Färber     for (p = qlist_first(list); p; p = qlist_next(p)) {
937fe55c3cSAndreas Färber         minfo = qobject_to_qdict(qlist_entry_obj(p));
947fe55c3cSAndreas Färber         g_assert(minfo);
957fe55c3cSAndreas Färber         qobj = qdict_get(minfo, "name");
967fe55c3cSAndreas Färber         g_assert(qobj);
977fe55c3cSAndreas Färber         qstr = qobject_to_qstring(qobj);
987fe55c3cSAndreas Färber         g_assert(qstr);
997fe55c3cSAndreas Färber         mname = qstring_get_str(qstr);
1007fe55c3cSAndreas Färber         if (!g_str_has_prefix(mname, "pc-")) {
1017fe55c3cSAndreas Färber             continue;
1027fe55c3cSAndreas Färber         }
1037fe55c3cSAndreas Färber         data = g_malloc(sizeof(PCTestData));
1047fe55c3cSAndreas Färber         data->machine = mname;
1057fe55c3cSAndreas Färber         data->cpu_model = "Haswell"; /* 1.3+ theoretically */
1067fe55c3cSAndreas Färber         data->sockets = 1;
1077fe55c3cSAndreas Färber         data->cores = 3;
1087fe55c3cSAndreas Färber         data->threads = 2;
1097fe55c3cSAndreas Färber         data->maxcpus = data->sockets * data->cores * data->threads * 2;
1107fe55c3cSAndreas Färber         if (g_str_has_suffix(mname, "-1.4") ||
1117fe55c3cSAndreas Färber             (strcmp(mname, "pc-1.3") == 0) ||
1127fe55c3cSAndreas Färber             (strcmp(mname, "pc-1.2") == 0) ||
1137fe55c3cSAndreas Färber             (strcmp(mname, "pc-1.1") == 0) ||
1147fe55c3cSAndreas Färber             (strcmp(mname, "pc-1.0") == 0) ||
1157fe55c3cSAndreas Färber             (strcmp(mname, "pc-0.15") == 0) ||
1167fe55c3cSAndreas Färber             (strcmp(mname, "pc-0.14") == 0) ||
1177fe55c3cSAndreas Färber             (strcmp(mname, "pc-0.13") == 0) ||
1187fe55c3cSAndreas Färber             (strcmp(mname, "pc-0.12") == 0) ||
1197fe55c3cSAndreas Färber             (strcmp(mname, "pc-0.11") == 0) ||
1207fe55c3cSAndreas Färber             (strcmp(mname, "pc-0.10") == 0)) {
121*53f77e45SAndreas Färber             path = g_strdup_printf("cpu/%s/init/%ux%ux%u&maxcpus=%u",
122*53f77e45SAndreas Färber                                    mname, data->sockets, data->cores,
1237fe55c3cSAndreas Färber                                    data->threads, data->maxcpus);
124*53f77e45SAndreas Färber             qtest_add_data_func(path, data, test_pc_without_cpu_add);
1257fe55c3cSAndreas Färber         } else {
126*53f77e45SAndreas Färber             path = g_strdup_printf("cpu/%s/add/%ux%ux%u&maxcpus=%u",
127*53f77e45SAndreas Färber                                    mname, data->sockets, data->cores,
1287fe55c3cSAndreas Färber                                    data->threads, data->maxcpus);
129*53f77e45SAndreas Färber             qtest_add_data_func(path, data, test_pc_with_cpu_add);
1307fe55c3cSAndreas Färber         }
1317fe55c3cSAndreas Färber     }
1327fe55c3cSAndreas Färber     qtest_end();
1337fe55c3cSAndreas Färber }
1347fe55c3cSAndreas Färber 
1357fe55c3cSAndreas Färber int main(int argc, char **argv)
1367fe55c3cSAndreas Färber {
1377fe55c3cSAndreas Färber     const char *arch = qtest_get_arch();
1387fe55c3cSAndreas Färber 
1397fe55c3cSAndreas Färber     g_test_init(&argc, &argv, NULL);
1407fe55c3cSAndreas Färber 
1417fe55c3cSAndreas Färber     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
1427fe55c3cSAndreas Färber         add_pc_test_cases();
1437fe55c3cSAndreas Färber     }
1447fe55c3cSAndreas Färber 
1457fe55c3cSAndreas Färber     return g_test_run();
1467fe55c3cSAndreas Färber }
147