1 /*
2  * Copyright © 2009 CNRS
3  * Copyright © 2009-2020 Inria.  All rights reserved.
4  * Copyright © 2009-2010 Université Bordeaux
5  * Copyright © 2011 Cisco Systems, Inc.  All rights reserved.
6  * See COPYING in top-level directory.
7  */
8 
9 #include "hwloc.h"
10 
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <assert.h>
15 
main(void)16 int main(void)
17 {
18   hwloc_topology_t topology, topology2, reload;
19   hwloc_obj_t obj;
20   hwloc_bitmap_t set;
21   char *buf1, *buf2;
22   int buflen1, buflen2, err;
23   hwloc_topology_diff_t diff;
24 
25   /* build and annotate a topology */
26   err = hwloc_topology_init(&topology);
27   assert(!err);
28   err = hwloc_topology_set_synthetic(topology, "NUMA:2 pack:2 core:2 pu:2");
29   assert(!err);
30   err = hwloc_topology_set_type_filter(topology, HWLOC_OBJ_MISC, HWLOC_TYPE_FILTER_KEEP_ALL);
31   assert(!err);
32   err = hwloc_topology_load(topology);
33   assert(!err);
34   hwloc_topology_check(topology);
35   /* Misc below root */
36   obj = hwloc_get_root_obj(topology);
37   obj = hwloc_topology_insert_misc_object(topology, obj, "below root");
38   assert(obj);
39   /* Misc below previous Misc */
40   obj = hwloc_topology_insert_misc_object(topology, obj, "below Misc below root");
41   assert(obj);
42   /* Misc below last NUMA node */
43   obj = hwloc_get_obj_by_type(topology, HWLOC_OBJ_NUMANODE, 1);
44   assert(obj);
45   obj = hwloc_topology_insert_misc_object(topology, obj, "below last NUMA");
46   assert(obj);
47   /* Misc below last Package */
48   obj = hwloc_get_obj_by_type(topology, HWLOC_OBJ_PACKAGE, 3);
49   assert(obj);
50   obj = hwloc_topology_insert_misc_object(topology, obj, "below last Package");
51   assert(obj);
52   /* Misc below last Core */
53   obj = hwloc_get_obj_by_type(topology, HWLOC_OBJ_CORE, 7);
54   assert(obj);
55   obj = hwloc_topology_insert_misc_object(topology, obj, "below last Core");
56   assert(obj);
57   /* Misc below first PU */
58   obj = hwloc_get_obj_by_type(topology, HWLOC_OBJ_PU, 0);
59   assert(obj);
60   obj = hwloc_topology_insert_misc_object(topology, obj, "below first PU");
61   assert(obj);
62   hwloc_topology_check(topology);
63   /* restrict it to only 3 Packages node without dropping Misc objects */
64   set = hwloc_bitmap_dup(hwloc_topology_get_topology_cpuset(topology));
65   obj = hwloc_get_obj_by_type(topology, HWLOC_OBJ_PACKAGE, 3);
66   assert(obj);
67   hwloc_bitmap_andnot(set, set, obj->cpuset);
68   err = hwloc_topology_restrict(topology, set, HWLOC_RESTRICT_FLAG_ADAPT_MISC);
69   assert(!err);
70   hwloc_topology_check(topology);
71 
72   /* check that export/reimport/export gives same export buffer */
73   err = hwloc_topology_export_xmlbuffer(topology, &buf1, &buflen1, 0);
74   assert(!err);
75   err = hwloc_topology_init(&reload);
76   assert(!err);
77   err = hwloc_topology_set_xmlbuffer(reload, buf1, buflen1);
78   assert(!err);
79   err = hwloc_topology_set_type_filter(reload, HWLOC_OBJ_MISC, HWLOC_TYPE_FILTER_KEEP_ALL);
80   assert(!err);
81   err = hwloc_topology_set_flags(reload, HWLOC_TOPOLOGY_FLAG_IMPORT_SUPPORT);
82   assert(!err);
83   err = hwloc_topology_load(reload);
84   assert(!err);
85   hwloc_topology_check(reload);
86   err = hwloc_topology_export_xmlbuffer(reload, &buf2, &buflen2, 0);
87   assert(!err);
88   assert(buflen1 == buflen2);
89   err = strcmp(buf1, buf2);
90   assert(!err);
91   hwloc_free_xmlbuffer(reload, buf2);
92   hwloc_topology_destroy(reload);
93 
94   /* build another restricted topology manually without Packages */
95   err = hwloc_topology_init(&topology2);
96   assert(!err);
97   err = hwloc_topology_set_synthetic(topology2, "NUMA:2 pack:2 core:2 pu:2"); /* must keep the same topology string to avoid SyntheticDescription info difference */
98   assert(!err);
99   err = hwloc_topology_set_type_filter(topology2, HWLOC_OBJ_PACKAGE, HWLOC_TYPE_FILTER_KEEP_NONE);
100   assert(!err);
101   err = hwloc_topology_set_type_filter(topology2, HWLOC_OBJ_MISC, HWLOC_TYPE_FILTER_KEEP_ALL);
102   assert(!err);
103   err = hwloc_topology_load(topology2);
104   assert(!err);
105   hwloc_topology_check(topology2);
106   err = hwloc_topology_restrict(topology2, set, HWLOC_RESTRICT_FLAG_ADAPT_MISC);
107   assert(!err);
108 
109   /* reimport without Packages and Misc, and check they are equal */
110   err = hwloc_topology_init(&reload);
111   assert(!err);
112   err = hwloc_topology_set_xmlbuffer(reload, buf1, buflen1);
113   assert(!err);
114   err = hwloc_topology_set_type_filter(reload, HWLOC_OBJ_PACKAGE, HWLOC_TYPE_FILTER_KEEP_NONE);
115   assert(!err);
116   err = hwloc_topology_load(reload);
117   assert(!err);
118   err = hwloc_topology_diff_build(reload, topology2, 0, &diff);
119   assert(!err);
120   assert(!diff);
121   hwloc_topology_destroy(reload);
122 
123   /* re-add some Misc now */
124   /* Misc below root */
125   obj = hwloc_get_root_obj(topology2);
126   obj = hwloc_topology_insert_misc_object(topology2, obj, "below root");
127   assert(obj);
128   /* Misc below previous Misc */
129   obj = hwloc_topology_insert_misc_object(topology2, obj, "below Misc below root");
130   assert(obj);
131   /* Misc below last NUMA node */
132   obj = hwloc_get_obj_by_type(topology2, HWLOC_OBJ_NUMANODE, 1);
133   assert(obj);
134   hwloc_topology_insert_misc_object(topology2, obj, "below last NUMA");
135   /* Misc below parent Group of this NUMA node (where Package and Core Misc will end up) */
136   assert(obj->parent->type == HWLOC_OBJ_GROUP);
137   hwloc_topology_insert_misc_object(topology2, obj->parent, "below last Package");
138   hwloc_topology_insert_misc_object(topology2, obj->parent, "below last Core");
139   assert(obj);
140   /* Misc below first PU */
141   obj = hwloc_get_obj_by_type(topology2, HWLOC_OBJ_PU, 0);
142   assert(obj);
143   obj = hwloc_topology_insert_misc_object(topology2, obj, "below first PU");
144   assert(obj);
145 
146   /* reimport without Packages and check they are equal*/
147   err = hwloc_topology_init(&reload);
148   assert(!err);
149   err = hwloc_topology_set_xmlbuffer(reload, buf1, buflen1);
150   assert(!err);
151   err = hwloc_topology_set_type_filter(reload, HWLOC_OBJ_PACKAGE, HWLOC_TYPE_FILTER_KEEP_NONE);
152   assert(!err);
153   err = hwloc_topology_set_type_filter(reload, HWLOC_OBJ_MISC, HWLOC_TYPE_FILTER_KEEP_ALL);
154   assert(!err);
155   err = hwloc_topology_load(reload);
156   assert(!err);
157   err = hwloc_topology_diff_build(reload, topology2, 0, &diff);
158   assert(!err);
159   assert(!diff);
160   hwloc_topology_destroy(reload);
161 
162   hwloc_free_xmlbuffer(topology, buf1);
163   hwloc_topology_destroy(topology);
164   hwloc_topology_destroy(topology2);
165   hwloc_bitmap_free(set);
166 
167   return 0;
168 }
169