2 * NUMA configuration test cases
4 * Copyright (c) 2017 Red Hat Inc.
8 * This work is licensed under the terms of the GNU GPL, version 2 or later.
9 * See the COPYING file in the top-level directory.
12 #include "qemu/osdep.h"
14 #include "qapi/qmp/qdict.h"
15 #include "qapi/qmp/qlist.h"
17 static char *make_cli(const char *generic_cli, const char *test_cli)
19 return g_strdup_printf("%s %s", generic_cli ? generic_cli : "", test_cli);
22 static void test_mon_explicit(const void *data)
27 cli = make_cli(data, "-smp 8 "
28 "-numa node,nodeid=0,cpus=0-3 "
29 "-numa node,nodeid=1,cpus=4-7 ");
33 g_assert(strstr(s, "node 0 cpus: 0 1 2 3"));
34 g_assert(strstr(s, "node 1 cpus: 4 5 6 7"));
41 static void test_mon_default(const void *data)
46 cli = make_cli(data, "-smp 8 -numa node -numa node");
50 g_assert(strstr(s, "node 0 cpus: 0 2 4 6"));
51 g_assert(strstr(s, "node 1 cpus: 1 3 5 7"));
58 static void test_mon_partial(const void *data)
63 cli = make_cli(data, "-smp 8 "
64 "-numa node,nodeid=0,cpus=0-1 "
65 "-numa node,nodeid=1,cpus=4-5 ");
69 g_assert(strstr(s, "node 0 cpus: 0 1 2 3 6 7"));
70 g_assert(strstr(s, "node 1 cpus: 4 5"));
77 static QList *get_cpus(QDict **resp)
79 *resp = qmp("{ 'execute': 'query-cpus' }");
81 g_assert(qdict_haskey(*resp, "return"));
82 return qdict_get_qlist(*resp, "return");
85 static void test_query_cpus(const void *data)
92 cli = make_cli(data, "-smp 8 -numa node,cpus=0-3 -numa node,cpus=4-7");
94 cpus = get_cpus(&resp);
97 while ((e = qlist_pop(cpus))) {
99 int64_t cpu_idx, node;
101 cpu = qobject_to(QDict, e);
102 g_assert(qdict_haskey(cpu, "CPU"));
103 g_assert(qdict_haskey(cpu, "props"));
105 cpu_idx = qdict_get_int(cpu, "CPU");
106 props = qdict_get_qdict(cpu, "props");
107 g_assert(qdict_haskey(props, "node-id"));
108 node = qdict_get_int(props, "node-id");
109 if (cpu_idx >= 0 && cpu_idx < 4) {
110 g_assert_cmpint(node, ==, 0);
112 g_assert_cmpint(node, ==, 1);
122 static void pc_numa_cpu(const void *data)
129 cli = make_cli(data, "-cpu pentium -smp 8,sockets=2,cores=2,threads=2 "
130 "-numa node,nodeid=0 -numa node,nodeid=1 "
131 "-numa cpu,node-id=1,socket-id=0 "
132 "-numa cpu,node-id=0,socket-id=1,core-id=0 "
133 "-numa cpu,node-id=0,socket-id=1,core-id=1,thread-id=0 "
134 "-numa cpu,node-id=1,socket-id=1,core-id=1,thread-id=1");
136 cpus = get_cpus(&resp);
139 while ((e = qlist_pop(cpus))) {
141 int64_t socket, core, thread, node;
143 cpu = qobject_to(QDict, e);
144 g_assert(qdict_haskey(cpu, "props"));
145 props = qdict_get_qdict(cpu, "props");
147 g_assert(qdict_haskey(props, "node-id"));
148 node = qdict_get_int(props, "node-id");
149 g_assert(qdict_haskey(props, "socket-id"));
150 socket = qdict_get_int(props, "socket-id");
151 g_assert(qdict_haskey(props, "core-id"));
152 core = qdict_get_int(props, "core-id");
153 g_assert(qdict_haskey(props, "thread-id"));
154 thread = qdict_get_int(props, "thread-id");
157 g_assert_cmpint(node, ==, 1);
158 } else if (socket == 1 && core == 0) {
159 g_assert_cmpint(node, ==, 0);
160 } else if (socket == 1 && core == 1 && thread == 0) {
161 g_assert_cmpint(node, ==, 0);
162 } else if (socket == 1 && core == 1 && thread == 1) {
163 g_assert_cmpint(node, ==, 1);
175 static void spapr_numa_cpu(const void *data)
182 cli = make_cli(data, "-smp 4,cores=4 "
183 "-numa node,nodeid=0 -numa node,nodeid=1 "
184 "-numa cpu,node-id=0,core-id=0 "
185 "-numa cpu,node-id=0,core-id=1 "
186 "-numa cpu,node-id=0,core-id=2 "
187 "-numa cpu,node-id=1,core-id=3");
189 cpus = get_cpus(&resp);
192 while ((e = qlist_pop(cpus))) {
196 cpu = qobject_to(QDict, e);
197 g_assert(qdict_haskey(cpu, "props"));
198 props = qdict_get_qdict(cpu, "props");
200 g_assert(qdict_haskey(props, "node-id"));
201 node = qdict_get_int(props, "node-id");
202 g_assert(qdict_haskey(props, "core-id"));
203 core = qdict_get_int(props, "core-id");
205 if (core >= 0 && core < 3) {
206 g_assert_cmpint(node, ==, 0);
207 } else if (core == 3) {
208 g_assert_cmpint(node, ==, 1);
220 static void aarch64_numa_cpu(const void *data)
227 cli = make_cli(data, "-smp 2 "
228 "-numa node,nodeid=0 -numa node,nodeid=1 "
229 "-numa cpu,node-id=1,thread-id=0 "
230 "-numa cpu,node-id=0,thread-id=1");
232 cpus = get_cpus(&resp);
235 while ((e = qlist_pop(cpus))) {
237 int64_t thread, node;
239 cpu = qobject_to(QDict, e);
240 g_assert(qdict_haskey(cpu, "props"));
241 props = qdict_get_qdict(cpu, "props");
243 g_assert(qdict_haskey(props, "node-id"));
244 node = qdict_get_int(props, "node-id");
245 g_assert(qdict_haskey(props, "thread-id"));
246 thread = qdict_get_int(props, "thread-id");
249 g_assert_cmpint(node, ==, 1);
250 } else if (thread == 1) {
251 g_assert_cmpint(node, ==, 0);
263 int main(int argc, char **argv)
265 const char *args = NULL;
266 const char *arch = qtest_get_arch();
268 if (strcmp(arch, "aarch64") == 0) {
269 args = "-machine virt";
272 g_test_init(&argc, &argv, NULL);
274 qtest_add_data_func("/numa/mon/default", args, test_mon_default);
275 qtest_add_data_func("/numa/mon/cpus/explicit", args, test_mon_explicit);
276 qtest_add_data_func("/numa/mon/cpus/partial", args, test_mon_partial);
277 qtest_add_data_func("/numa/qmp/cpus/query-cpus", args, test_query_cpus);
279 if (!strcmp(arch, "i386") || !strcmp(arch, "x86_64")) {
280 qtest_add_data_func("/numa/pc/cpu/explicit", args, pc_numa_cpu);
283 if (!strcmp(arch, "ppc64")) {
284 qtest_add_data_func("/numa/spapr/cpu/explicit", args, spapr_numa_cpu);
287 if (!strcmp(arch, "aarch64")) {
288 qtest_add_data_func("/numa/aarch64/cpu/explicit", args,