1 /*
2 * Copyright © 2020 Inria. All rights reserved.
3 * See COPYING in top-level directory.
4 */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <assert.h>
9
10 #include "hwloc.h"
11
main(void)12 int main(void)
13 {
14 hwloc_topology_t topology;
15 int efficiency;
16 hwloc_bitmap_t cpuset;
17 struct hwloc_info_s info, *infosp;
18 unsigned nr_infos;
19 int err;
20
21 err = hwloc_topology_init(&topology);
22 assert(!err);
23 err = hwloc_topology_set_synthetic(topology, "pack:4 pu:3");
24 assert(!err);
25 err = hwloc_topology_load(topology);
26 assert(!err);
27
28 cpuset = hwloc_bitmap_alloc();
29 assert(cpuset);
30
31 /* no cpukinds */
32
33 /* get_nr with invalid flags */
34 err = hwloc_cpukinds_get_nr(topology, 1 /* invalid flag */);
35 assert(err == -1);
36 assert(errno == EINVAL);
37 /* get_nr */
38 err = hwloc_cpukinds_get_nr(topology, 0);
39 assert(err == 0);
40
41 /* get_info with invalid flags */
42 err = hwloc_cpukinds_get_info(topology, 0, NULL, NULL, NULL, NULL, 1 /* invalid flag */);
43 assert(err == -1);
44 assert(errno == EINVAL);
45 /* get kind, non-existing */
46 err = hwloc_cpukinds_get_info(topology, 0, NULL, NULL, NULL, NULL, 0);
47 assert(err == -1);
48 assert(errno == ENOENT);
49
50 /* get_by_cpuset with no cpuset */
51 err = hwloc_cpukinds_get_by_cpuset(topology, NULL, 0);
52 assert(err == -1);
53 assert(errno == EINVAL);
54 /* get_by_cpuset with empty cpuset */
55 hwloc_bitmap_zero(cpuset);
56 err = hwloc_cpukinds_get_by_cpuset(topology, cpuset, 0);
57 assert(err == -1);
58 assert(errno == EINVAL);
59 /* get_by_cpuset with invalid flags */
60 hwloc_bitmap_zero(cpuset);
61 hwloc_bitmap_set_range(cpuset, 0, 11);
62 err = hwloc_cpukinds_get_by_cpuset(topology, cpuset, 1 /* invalid flag */);
63 assert(err == -1);
64 assert(errno == EINVAL);
65 /* get_by_cpuset, non-existing */
66 hwloc_bitmap_zero(cpuset);
67 hwloc_bitmap_set_range(cpuset, 0, 11);
68 err = hwloc_cpukinds_get_by_cpuset(topology, cpuset, 0);
69 assert(err == -1);
70 assert(errno == ENOENT);
71
72 /* register with no cpuset */
73 err = hwloc_cpukinds_register(topology, NULL, 0, 0, NULL, 0);
74 assert(err == -1);
75 assert(errno == EINVAL);
76 /* register with empty cpuset */
77 hwloc_bitmap_zero(cpuset);
78 err = hwloc_cpukinds_register(topology, cpuset, 0, 0, NULL, 0);
79 assert(err == -1);
80 assert(errno == EINVAL);
81 /* register with invalid flags */
82 hwloc_bitmap_set_range(cpuset, 0, 5);
83 err = hwloc_cpukinds_register(topology, cpuset, 0, 0, NULL, 1 /* invalid flag */);
84 assert(err == -1);
85 assert(errno == EINVAL);
86
87 /* PU 0-5 (first two packages) are big and efficient */
88 hwloc_bitmap_zero(cpuset);
89 hwloc_bitmap_set_range(cpuset, 0, 5);
90 info.name = (char*) "CoreType";
91 info.value = (char*) "BigCore";
92 err = hwloc_cpukinds_register(topology, cpuset, 1000, 1, &info, 0);
93 assert(!err);
94 /* PU 6-9 (third package) are small and less efficient */
95 hwloc_bitmap_zero(cpuset);
96 hwloc_bitmap_set_range(cpuset, 6, 8);
97 info.name = (char*) "CoreType";
98 info.value = (char*) "SmallCore";
99 err = hwloc_cpukinds_register(topology, cpuset, 10, 1, &info, 0);
100 assert(!err);
101
102 /* check those kinds */
103 err = hwloc_cpukinds_get_nr(topology, 0);
104 assert(err == 2);
105 /* first one should be second registered one */
106 hwloc_bitmap_zero(cpuset);
107 err = hwloc_cpukinds_get_info(topology, 0, cpuset, &efficiency, &nr_infos, &infosp, 0);
108 assert(!err);
109 assert(hwloc_bitmap_weight(cpuset) == 3);
110 assert(hwloc_bitmap_first(cpuset) == 6);
111 assert(hwloc_bitmap_last(cpuset) == 8);
112 assert(efficiency == 0);
113 assert(nr_infos == 1);
114 assert(!strcmp(infosp[0].name, "CoreType"));
115 assert(!strcmp(infosp[0].value, "SmallCore"));
116 /* second one should be first registered one */
117 hwloc_bitmap_zero(cpuset);
118 err = hwloc_cpukinds_get_info(topology, 1, cpuset, &efficiency, &nr_infos, &infosp, 0);
119 assert(!err);
120 assert(hwloc_bitmap_weight(cpuset) == 6);
121 assert(hwloc_bitmap_first(cpuset) == 0);
122 assert(hwloc_bitmap_last(cpuset) == 5);
123 assert(efficiency == 1);
124 assert(nr_infos == 1);
125 assert(!strcmp(infosp[0].name, "CoreType"));
126 assert(!strcmp(infosp[0].value, "BigCore"));
127 /* try without getting any info too */
128 err = hwloc_cpukinds_get_info(topology, 1, NULL, NULL, NULL, NULL, 0);
129 assert(!err);
130 /* third one is invalid */
131 err = hwloc_cpukinds_get_info(topology, 2, NULL, NULL, NULL, NULL, 0);
132 assert(err == -1);
133 assert(errno == ENOENT);
134
135 /* get_by_cpuset */
136 hwloc_bitmap_zero(cpuset);
137 hwloc_bitmap_set_range(cpuset, 1, 3);
138 err = hwloc_cpukinds_get_by_cpuset(topology, cpuset, 0);
139 assert(err == 1);
140 hwloc_bitmap_zero(cpuset);
141 hwloc_bitmap_set_range(cpuset, 7, 8);
142 err = hwloc_cpukinds_get_by_cpuset(topology, cpuset, 0);
143 assert(err == 0);
144 /* get_by_cpuset across kinds */
145 hwloc_bitmap_zero(cpuset);
146 hwloc_bitmap_set_range(cpuset, 5, 6);
147 err = hwloc_cpukinds_get_by_cpuset(topology, cpuset, 0);
148 assert(err == -1);
149 assert(errno == EXDEV);
150 /* get_by_cpuset outside of kinds */
151 hwloc_bitmap_zero(cpuset);
152 hwloc_bitmap_set_range(cpuset, 9, 11);
153 err = hwloc_cpukinds_get_by_cpuset(topology, cpuset, 0);
154 assert(err == -1);
155 assert(errno == ENOENT);
156
157 /* PU 5-9 (part of packages #1 and #3, and all of #2) have some features */
158 hwloc_bitmap_zero(cpuset);
159 hwloc_bitmap_set_range(cpuset, 5, 10);
160 info.name = (char*) "Features";
161 info.value = (char*) "this, that and those";
162 err = hwloc_cpukinds_register(topology, cpuset, -1, 1, &info, 0);
163 assert(!err);
164
165 /* check those kinds */
166 err = hwloc_cpukinds_get_nr(topology, 0);
167 assert(err == 4);
168 /* WARNING: the order is now implementation dependent, since we cannot order by efficiency here */
169 /* first one is 6-8 SmallCore Features */
170 hwloc_bitmap_zero(cpuset);
171 err = hwloc_cpukinds_get_info(topology, 0, cpuset, &efficiency, &nr_infos, &infosp, 0);
172 assert(!err);
173 assert(hwloc_bitmap_weight(cpuset) == 3);
174 assert(hwloc_bitmap_first(cpuset) == 6);
175 assert(hwloc_bitmap_last(cpuset) == 8);
176 assert(efficiency == -1);
177 assert(nr_infos == 2);
178 assert(!strcmp(infosp[0].name, "CoreType"));
179 assert(!strcmp(infosp[0].value, "SmallCore"));
180 assert(!strcmp(infosp[1].name, "Features"));
181 assert(!strcmp(infosp[1].value, "this, that and those"));
182 /* second one is 0-4 BigCore */
183 hwloc_bitmap_zero(cpuset);
184 err = hwloc_cpukinds_get_info(topology, 1, cpuset, &efficiency, &nr_infos, &infosp, 0);
185 assert(!err);
186 assert(hwloc_bitmap_weight(cpuset) == 5);
187 assert(hwloc_bitmap_first(cpuset) == 0);
188 assert(hwloc_bitmap_last(cpuset) == 4);
189 assert(efficiency == -1);
190 assert(nr_infos == 1);
191 assert(!strcmp(infosp[0].name, "CoreType"));
192 assert(!strcmp(infosp[0].value, "BigCore"));
193 /* third one is 5-5 BigCore Features */
194 hwloc_bitmap_zero(cpuset);
195 err = hwloc_cpukinds_get_info(topology, 2, cpuset, &efficiency, &nr_infos, &infosp, 0);
196 assert(!err);
197 assert(hwloc_bitmap_weight(cpuset) == 1);
198 assert(hwloc_bitmap_first(cpuset) == 5);
199 assert(hwloc_bitmap_last(cpuset) == 5);
200 assert(efficiency == -1);
201 assert(nr_infos == 2);
202 assert(!strcmp(infosp[0].name, "CoreType"));
203 assert(!strcmp(infosp[0].value, "BigCore"));
204 assert(!strcmp(infosp[1].name, "Features"));
205 assert(!strcmp(infosp[1].value, "this, that and those"));
206 /* fourth one is 9-10 Features */
207 hwloc_bitmap_zero(cpuset);
208 err = hwloc_cpukinds_get_info(topology, 3, cpuset, &efficiency, &nr_infos, &infosp, 0);
209 assert(!err);
210 assert(hwloc_bitmap_weight(cpuset) == 2);
211 assert(hwloc_bitmap_first(cpuset) == 9);
212 assert(hwloc_bitmap_last(cpuset) == 10);
213 assert(efficiency == -1);
214 assert(nr_infos == 1);
215 assert(!strcmp(infosp[0].name, "Features"));
216 assert(!strcmp(infosp[0].value, "this, that and those"));
217 /* fifth one is invalid */
218 err = hwloc_cpukinds_get_info(topology, 4, NULL, NULL, NULL, NULL, 0);
219 assert(err == -1);
220 assert(errno == ENOENT);
221
222 /* get_by_cpuset */
223 hwloc_bitmap_zero(cpuset);
224 hwloc_bitmap_set_range(cpuset, 1, 3);
225 err = hwloc_cpukinds_get_by_cpuset(topology, cpuset, 0);
226 assert(err == 1);
227 hwloc_bitmap_zero(cpuset);
228 hwloc_bitmap_set_range(cpuset, 7, 8);
229 err = hwloc_cpukinds_get_by_cpuset(topology, cpuset, 0);
230 assert(err == 0);
231 hwloc_bitmap_zero(cpuset);
232 hwloc_bitmap_set_range(cpuset, 5, 5);
233 err = hwloc_cpukinds_get_by_cpuset(topology, cpuset, 0);
234 assert(err == 2);
235 /* get_by_cpuset across kinds */
236 hwloc_bitmap_zero(cpuset);
237 hwloc_bitmap_set_range(cpuset, 10, 11);
238 err = hwloc_cpukinds_get_by_cpuset(topology, cpuset, 0);
239 assert(err == -1);
240 assert(errno == EXDEV);
241 /* get_by_cpuset outside of kinds */
242 hwloc_bitmap_zero(cpuset);
243 hwloc_bitmap_set_range(cpuset, 11, 11);
244 err = hwloc_cpukinds_get_by_cpuset(topology, cpuset, 0);
245 assert(err == -1);
246 assert(errno == ENOENT);
247
248 /* now force efficiency of those kinds */
249 hwloc_bitmap_zero(cpuset);
250 hwloc_bitmap_set_range(cpuset, 0, 4);
251 err = hwloc_cpukinds_register(topology, cpuset, 1000, 0, NULL, 0);
252 assert(!err);
253 hwloc_bitmap_zero(cpuset);
254 hwloc_bitmap_set_range(cpuset, 5, 5);
255 err = hwloc_cpukinds_register(topology, cpuset, 100, 0, NULL, 0);
256 assert(!err);
257 hwloc_bitmap_zero(cpuset);
258 hwloc_bitmap_set_range(cpuset, 6, 8);
259 err = hwloc_cpukinds_register(topology, cpuset, 10, 0, NULL, 0);
260 assert(!err);
261 hwloc_bitmap_zero(cpuset);
262 hwloc_bitmap_set_range(cpuset, 9, 10);
263 err = hwloc_cpukinds_register(topology, cpuset, 1, 0, NULL, 0);
264 assert(!err);
265
266 /* check those kinds */
267 err = hwloc_cpukinds_get_nr(topology, 0);
268 assert(err == 4);
269 /* first one is 9-10 Features */
270 hwloc_bitmap_zero(cpuset);
271 err = hwloc_cpukinds_get_info(topology, 0, cpuset, &efficiency, &nr_infos, &infosp, 0);
272 assert(!err);
273 assert(hwloc_bitmap_weight(cpuset) == 2);
274 assert(hwloc_bitmap_first(cpuset) == 9);
275 assert(hwloc_bitmap_last(cpuset) == 10);
276 assert(efficiency == 0);
277 assert(nr_infos == 1);
278 assert(!strcmp(infosp[0].name, "Features"));
279 assert(!strcmp(infosp[0].value, "this, that and those"));
280 /* second one is 6-8 SmallCore Features */
281 hwloc_bitmap_zero(cpuset);
282 err = hwloc_cpukinds_get_info(topology, 1, cpuset, &efficiency, &nr_infos, &infosp, 0);
283 assert(!err);
284 assert(hwloc_bitmap_weight(cpuset) == 3);
285 assert(hwloc_bitmap_first(cpuset) == 6);
286 assert(hwloc_bitmap_last(cpuset) == 8);
287 assert(efficiency == 1);
288 assert(nr_infos == 2);
289 assert(!strcmp(infosp[0].name, "CoreType"));
290 assert(!strcmp(infosp[0].value, "SmallCore"));
291 assert(!strcmp(infosp[1].name, "Features"));
292 assert(!strcmp(infosp[1].value, "this, that and those"));
293 /* third one is 5-5 BigCore Features */
294 hwloc_bitmap_zero(cpuset);
295 err = hwloc_cpukinds_get_info(topology, 2, cpuset, &efficiency, &nr_infos, &infosp, 0);
296 assert(!err);
297 assert(hwloc_bitmap_weight(cpuset) == 1);
298 assert(hwloc_bitmap_first(cpuset) == 5);
299 assert(hwloc_bitmap_last(cpuset) == 5);
300 assert(efficiency == 2);
301 assert(nr_infos == 2);
302 assert(!strcmp(infosp[0].name, "CoreType"));
303 assert(!strcmp(infosp[0].value, "BigCore"));
304 assert(!strcmp(infosp[1].name, "Features"));
305 assert(!strcmp(infosp[1].value, "this, that and those"));
306 /* fourth one is 0-4 BigCore */
307 hwloc_bitmap_zero(cpuset);
308 err = hwloc_cpukinds_get_info(topology, 3, cpuset, &efficiency, &nr_infos, &infosp, 0);
309 assert(!err);
310 assert(hwloc_bitmap_weight(cpuset) == 5);
311 assert(hwloc_bitmap_first(cpuset) == 0);
312 assert(hwloc_bitmap_last(cpuset) == 4);
313 assert(efficiency == 3);
314 assert(nr_infos == 1);
315 assert(!strcmp(infosp[0].name, "CoreType"));
316 assert(!strcmp(infosp[0].value, "BigCore"));
317 /* fifth one is invalid */
318 err = hwloc_cpukinds_get_info(topology, 4, NULL, NULL, NULL, NULL, 0);
319 assert(err == -1);
320 assert(errno == ENOENT);
321
322 /* restrict to remove PU 5-6 (third kind and part of second), PU 11 (no kind) and PU 0-2 (part of fourth kind) */
323 hwloc_bitmap_zero(cpuset);
324 hwloc_bitmap_set_range(cpuset, 3, 10);
325 hwloc_bitmap_clr_range(cpuset, 5, 6);
326 err = hwloc_topology_restrict(topology, cpuset, 0);
327 assert(!err);
328
329 /* check remaining kinds */
330 err = hwloc_cpukinds_get_nr(topology, 0);
331 assert(err == 3);
332 /* first one is 9-10 Features */
333 hwloc_bitmap_zero(cpuset);
334 err = hwloc_cpukinds_get_info(topology, 0, cpuset, &efficiency, &nr_infos, &infosp, 0);
335 assert(!err);
336 assert(hwloc_bitmap_weight(cpuset) == 2);
337 assert(hwloc_bitmap_first(cpuset) == 9);
338 assert(hwloc_bitmap_last(cpuset) == 10);
339 assert(efficiency == 0);
340 assert(nr_infos == 1);
341 assert(!strcmp(infosp[0].name, "Features"));
342 assert(!strcmp(infosp[0].value, "this, that and those"));
343 /* second one is 7-8 SmallCore Features */
344 hwloc_bitmap_zero(cpuset);
345 err = hwloc_cpukinds_get_info(topology, 1, cpuset, &efficiency, &nr_infos, &infosp, 0);
346 assert(!err);
347 assert(hwloc_bitmap_weight(cpuset) == 2);
348 assert(hwloc_bitmap_first(cpuset) == 7);
349 assert(hwloc_bitmap_last(cpuset) == 8);
350 assert(efficiency == 1);
351 assert(nr_infos == 2);
352 assert(!strcmp(infosp[0].name, "CoreType"));
353 assert(!strcmp(infosp[0].value, "SmallCore"));
354 assert(!strcmp(infosp[1].name, "Features"));
355 assert(!strcmp(infosp[1].value, "this, that and those"));
356 /* third one is 3-4 BigCore */
357 hwloc_bitmap_zero(cpuset);
358 err = hwloc_cpukinds_get_info(topology, 2, cpuset, &efficiency, &nr_infos, &infosp, 0);
359 assert(!err);
360 assert(hwloc_bitmap_weight(cpuset) == 2);
361 assert(hwloc_bitmap_first(cpuset) == 3);
362 assert(hwloc_bitmap_last(cpuset) == 4);
363 assert(efficiency == 2);
364 assert(nr_infos == 1);
365 assert(!strcmp(infosp[0].name, "CoreType"));
366 assert(!strcmp(infosp[0].value, "BigCore"));
367 /* fourth one is invalid */
368 err = hwloc_cpukinds_get_info(topology, 3, NULL, NULL, NULL, NULL, 0);
369 assert(err == -1);
370 assert(errno == ENOENT);
371
372 hwloc_bitmap_free(cpuset);
373 hwloc_topology_destroy(topology);
374 return 0;
375 }
376