2 * NUMA configuration test cases
4 * Copyright (c) 2017 Red Hat Inc.
6 * Igor Mammedov <imammedo@redhat.com>
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"
15 static char *make_cli(const char *generic_cli
, const char *test_cli
)
17 return g_strdup_printf("%s %s", generic_cli
? generic_cli
: "", test_cli
);
20 static void test_mon_explicit(const void *data
)
25 cli
= make_cli(data
, "-smp 8 "
26 "-numa node,nodeid=0,cpus=0-3 "
27 "-numa node,nodeid=1,cpus=4-7 ");
31 g_assert(strstr(s
, "node 0 cpus: 0 1 2 3"));
32 g_assert(strstr(s
, "node 1 cpus: 4 5 6 7"));
39 static void test_mon_default(const void *data
)
44 cli
= make_cli(data
, "-smp 8 -numa node -numa node");
48 g_assert(strstr(s
, "node 0 cpus: 0 2 4 6"));
49 g_assert(strstr(s
, "node 1 cpus: 1 3 5 7"));
56 static void test_mon_partial(const void *data
)
61 cli
= make_cli(data
, "-smp 8 "
62 "-numa node,nodeid=0,cpus=0-1 "
63 "-numa node,nodeid=1,cpus=4-5 ");
67 g_assert(strstr(s
, "node 0 cpus: 0 1 2 3 6 7"));
68 g_assert(strstr(s
, "node 1 cpus: 4 5"));
75 static QList
*get_cpus(QDict
**resp
)
77 *resp
= qmp("{ 'execute': 'query-cpus' }");
79 g_assert(qdict_haskey(*resp
, "return"));
80 return qdict_get_qlist(*resp
, "return");
83 static void test_query_cpus(const void *data
)
90 cli
= make_cli(data
, "-smp 8 -numa node,cpus=0-3 -numa node,cpus=4-7");
92 cpus
= get_cpus(&resp
);
95 while ((e
= qlist_pop(cpus
))) {
97 int64_t cpu_idx
, node
;
99 cpu
= qobject_to_qdict(e
);
100 g_assert(qdict_haskey(cpu
, "CPU"));
101 g_assert(qdict_haskey(cpu
, "props"));
103 cpu_idx
= qdict_get_int(cpu
, "CPU");
104 props
= qdict_get_qdict(cpu
, "props");
105 g_assert(qdict_haskey(props
, "node-id"));
106 node
= qdict_get_int(props
, "node-id");
107 if (cpu_idx
>= 0 && cpu_idx
< 4) {
108 g_assert_cmpint(node
, ==, 0);
110 g_assert_cmpint(node
, ==, 1);
120 static void pc_numa_cpu(const void *data
)
127 cli
= make_cli(data
, "-cpu pentium -smp 8,sockets=2,cores=2,threads=2 "
128 "-numa node,nodeid=0 -numa node,nodeid=1 "
129 "-numa cpu,node-id=1,socket-id=0 "
130 "-numa cpu,node-id=0,socket-id=1,core-id=0 "
131 "-numa cpu,node-id=0,socket-id=1,core-id=1,thread-id=0 "
132 "-numa cpu,node-id=1,socket-id=1,core-id=1,thread-id=1");
134 cpus
= get_cpus(&resp
);
137 while ((e
= qlist_pop(cpus
))) {
139 int64_t socket
, core
, thread
, node
;
141 cpu
= qobject_to_qdict(e
);
142 g_assert(qdict_haskey(cpu
, "props"));
143 props
= qdict_get_qdict(cpu
, "props");
145 g_assert(qdict_haskey(props
, "node-id"));
146 node
= qdict_get_int(props
, "node-id");
147 g_assert(qdict_haskey(props
, "socket-id"));
148 socket
= qdict_get_int(props
, "socket-id");
149 g_assert(qdict_haskey(props
, "core-id"));
150 core
= qdict_get_int(props
, "core-id");
151 g_assert(qdict_haskey(props
, "thread-id"));
152 thread
= qdict_get_int(props
, "thread-id");
155 g_assert_cmpint(node
, ==, 1);
156 } else if (socket
== 1 && core
== 0) {
157 g_assert_cmpint(node
, ==, 0);
158 } else if (socket
== 1 && core
== 1 && thread
== 0) {
159 g_assert_cmpint(node
, ==, 0);
160 } else if (socket
== 1 && core
== 1 && thread
== 1) {
161 g_assert_cmpint(node
, ==, 1);
173 static void spapr_numa_cpu(const void *data
)
180 cli
= make_cli(data
, "-smp 4,cores=4 "
181 "-numa node,nodeid=0 -numa node,nodeid=1 "
182 "-numa cpu,node-id=0,core-id=0 "
183 "-numa cpu,node-id=0,core-id=1 "
184 "-numa cpu,node-id=0,core-id=2 "
185 "-numa cpu,node-id=1,core-id=3");
187 cpus
= get_cpus(&resp
);
190 while ((e
= qlist_pop(cpus
))) {
194 cpu
= qobject_to_qdict(e
);
195 g_assert(qdict_haskey(cpu
, "props"));
196 props
= qdict_get_qdict(cpu
, "props");
198 g_assert(qdict_haskey(props
, "node-id"));
199 node
= qdict_get_int(props
, "node-id");
200 g_assert(qdict_haskey(props
, "core-id"));
201 core
= qdict_get_int(props
, "core-id");
203 if (core
>= 0 && core
< 3) {
204 g_assert_cmpint(node
, ==, 0);
205 } else if (core
== 3) {
206 g_assert_cmpint(node
, ==, 1);
218 static void aarch64_numa_cpu(const void *data
)
225 cli
= make_cli(data
, "-smp 2 "
226 "-numa node,nodeid=0 -numa node,nodeid=1 "
227 "-numa cpu,node-id=1,thread-id=0 "
228 "-numa cpu,node-id=0,thread-id=1");
230 cpus
= get_cpus(&resp
);
233 while ((e
= qlist_pop(cpus
))) {
235 int64_t thread
, node
;
237 cpu
= qobject_to_qdict(e
);
238 g_assert(qdict_haskey(cpu
, "props"));
239 props
= qdict_get_qdict(cpu
, "props");
241 g_assert(qdict_haskey(props
, "node-id"));
242 node
= qdict_get_int(props
, "node-id");
243 g_assert(qdict_haskey(props
, "thread-id"));
244 thread
= qdict_get_int(props
, "thread-id");
247 g_assert_cmpint(node
, ==, 1);
248 } else if (thread
== 1) {
249 g_assert_cmpint(node
, ==, 0);
261 int main(int argc
, char **argv
)
263 const char *args
= NULL
;
264 const char *arch
= qtest_get_arch();
266 if (strcmp(arch
, "aarch64") == 0) {
267 args
= "-machine virt";
270 g_test_init(&argc
, &argv
, NULL
);
272 qtest_add_data_func("/numa/mon/default", args
, test_mon_default
);
273 qtest_add_data_func("/numa/mon/cpus/explicit", args
, test_mon_explicit
);
274 qtest_add_data_func("/numa/mon/cpus/partial", args
, test_mon_partial
);
275 qtest_add_data_func("/numa/qmp/cpus/query-cpus", args
, test_query_cpus
);
277 if (!strcmp(arch
, "i386") || !strcmp(arch
, "x86_64")) {
278 qtest_add_data_func("/numa/pc/cpu/explicit", args
, pc_numa_cpu
);
281 if (!strcmp(arch
, "ppc64")) {
282 qtest_add_data_func("/numa/spapr/cpu/explicit", args
, spapr_numa_cpu
);
285 if (!strcmp(arch
, "aarch64")) {
286 qtest_add_data_func("/numa/aarch64/cpu/explicit", args
,