1 
2 /*
3  * Copyright © 2009 CNRS
4  * Copyright © 2009-2017 Inria.  All rights reserved.
5  * Copyright © 2009 Université Bordeaux
6  * Copyright © 2011 Cisco Systems, Inc.  All rights reserved.
7  * See COPYING in top-level directory.
8  */
9 
10 #include "hwloc.h"
11 
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <assert.h>
16 
17 /* check hwloc_bitmap_asprintf(), hwloc_bitmap_snprintf() and hwloc_bitmap_sscanf() */
18 
check_cpuset(hwloc_bitmap_t set,const char * expected1,const char * expected2,const char * expected3)19 static void check_cpuset(hwloc_bitmap_t set, const char *expected1, const char *expected2, const char *expected3)
20 {
21   hwloc_bitmap_t set2 = hwloc_bitmap_alloc();
22   char *string = NULL;
23 
24   hwloc_bitmap_asprintf(&string, set);
25   fprintf(stderr, "exported to %s\n", string);
26   if (expected1 && strcmp(string, expected1)) {
27     fprintf(stderr, "got %s instead of %s\n", string, expected1);
28     assert(0);
29   }
30   hwloc_bitmap_sscanf(set2, string);
31   free(string);
32   assert(hwloc_bitmap_isequal(set, set2));
33 
34   hwloc_bitmap_list_asprintf(&string, set);
35   fprintf(stderr, "exported to list %s\n", string);
36   if (expected2 && strcmp(string, expected2)) {
37     fprintf(stderr, "got %s instead of %s\n", string, expected2);
38     assert(0);
39   }
40   hwloc_bitmap_list_sscanf(set2, string);
41   free(string);
42   assert(hwloc_bitmap_isequal(set, set2));
43 
44   hwloc_bitmap_taskset_asprintf(&string, set);
45   fprintf(stderr, "exported to taskset %s\n", string);
46   if (expected3 && strcmp(string, expected3)) {
47     fprintf(stderr, "got %s instead of %s\n", string, expected3);
48     assert(0);
49   }
50   hwloc_bitmap_taskset_sscanf(set2, string);
51   free(string);
52   assert(hwloc_bitmap_isequal(set, set2));
53 
54   hwloc_bitmap_free(set2);
55 }
56 
main(void)57 int main(void)
58 {
59   hwloc_topology_t topology;
60   int depth;
61   char *string = NULL;
62   int stringlen, len;
63   hwloc_obj_t obj;
64   hwloc_bitmap_t set;
65 
66   /* check an empty cpuset */
67   set = hwloc_bitmap_alloc();
68   check_cpuset(set, "0x0", "", "0x0");
69   printf("empty cpuset converted back and forth, ok\n");
70 
71   /* make sure the first ulong is allocated even if empty */
72   hwloc_bitmap_set_ith_ulong(set, 0, 0UL);
73   check_cpuset(set, "0x0", "", "0x0");
74   printf("uselessly modified empty cpuset converted back and forth, ok\n");
75 
76   /* make sure the 6th ulong is allocated even if empty */
77   hwloc_bitmap_set_ith_ulong(set, 5, 0UL);
78   check_cpuset(set, "0x0", "", "0x0");
79   printf("twice uselessly modified empty cpuset converted back and forth, ok\n");
80   hwloc_bitmap_free(set);
81 
82   /* check a full (and infinite) cpuset */
83   set = hwloc_bitmap_alloc_full();
84   check_cpuset(set, "0xf...f", "0-", "0xf...f");
85   printf("full cpuset converted back and forth, ok\n");
86 
87   /* make sure the first ulong is allocated even if full */
88   hwloc_bitmap_set_ith_ulong(set, 0, ~0UL);
89   check_cpuset(set, "0xf...f", "0-", "0xf...f");
90   printf("uselessly modified full cpuset converted back and forth, ok\n");
91 
92   /* make sure the 9th ulong is allocated even if full */
93   hwloc_bitmap_set_ith_ulong(set, 8, ~0UL);
94   check_cpuset(set, "0xf...f", "0-", "0xf...f");
95   printf("twice uselessly modified full cpuset converted back and forth, ok\n");
96   hwloc_bitmap_free(set);
97 
98   /* check an infinite (but non full) cpuset */
99   set = hwloc_bitmap_alloc_full();
100   hwloc_bitmap_clr(set, 173);
101   hwloc_bitmap_clr_range(set, 60, 70);
102   check_cpuset(set, "0xf...f,0xffffdfff,0xffffffff,0xffffffff,0xffffff80,0x0fffffff,0xffffffff", "0-59,71-172,174-", "0xf...fffffdfffffffffffffffffffffffff800fffffffffffffff");
103   hwloc_bitmap_free(set);
104   printf("infinite/nonfull cpuset converted back and forth, ok\n");
105 
106   /* check an finite cpuset */
107   set = hwloc_bitmap_alloc();
108   hwloc_bitmap_set(set, 2);
109   hwloc_bitmap_set_range(set, 67, 70);
110   check_cpuset(set, "0x00000078,,0x00000004", "2,67-70", "0x780000000000000004");
111   hwloc_bitmap_free(set);
112   printf("finite/nonnull cpuset converted back and forth, ok\n");
113 
114   hwloc_topology_init(&topology);
115   hwloc_topology_set_synthetic(topology, "6 5 4 3 2");
116   hwloc_topology_load(topology);
117   depth = hwloc_topology_get_depth(topology);
118 
119   obj = hwloc_get_root_obj(topology);
120   stringlen = hwloc_bitmap_asprintf(&string, obj->cpuset);
121   printf("system cpuset is %s\n", string);
122   check_cpuset(obj->cpuset, NULL, NULL, NULL);
123   printf("system cpuset converted back and forth, ok\n");
124 
125   printf("truncating system cpuset to NULL buffer\n");
126   len = hwloc_bitmap_snprintf(NULL, 0, obj->cpuset);
127   assert(len == stringlen);
128 
129   printf("truncating system cpuset to 0 char (no modification)\n");
130   memset(string, 'X', 1);
131   string[1] = 0;
132   len = hwloc_bitmap_snprintf(string, 0, obj->cpuset);
133 printf("%d == %d (%c)\n", len, stringlen, string[0]);
134   assert(len == stringlen);
135   assert(string[0] == 'X');
136 
137   printf("truncating system cpuset to 1 char (empty string)\n");
138   memset(string, 'X', 2);
139   string[2] = 0;
140   len = hwloc_bitmap_snprintf(string, 1, obj->cpuset);
141   printf("got %s\n", string);
142   assert(len == stringlen);
143   assert(string[0] == 0);
144   assert(string[1] == 'X');
145 
146   printf("truncating system cpuset to 10 chars (single 32bit subset except last char)\n");
147   memset(string, 'X', 11);
148   string[11] = 0;
149   len = hwloc_bitmap_snprintf(string, 10, obj->cpuset);
150   printf("got %s\n", string);
151   assert(len == stringlen);
152   assert(string[8] == 'f');
153   assert(string[9] == 0);
154   assert(string[10] == 'X');
155 
156   printf("truncating system cpuset to 11 chars (single 32bit subset)\n");
157   memset(string, 'X', 12);
158   string[12] = 0;
159   len = hwloc_bitmap_snprintf(string, 11, obj->cpuset);
160   printf("got %s\n", string);
161   assert(len == stringlen);
162   assert(string[9] == 'f');
163   assert(string[10] == 0);
164   assert(string[11] == 'X');
165 
166   printf("truncating system cpuset to 23 chars (two 32bit subsets with ending comma)\n");
167   memset(string, 'X', 24);
168   string[24] = 0;
169   len = hwloc_bitmap_snprintf(string, 23, obj->cpuset);
170   printf("got %s\n", string);
171   assert(len == stringlen);
172   assert(string[20] == 'f');
173   assert(string[21] == ',');
174   assert(string[22] == 0);
175   assert(string[23] == 'X');
176 
177   printf("truncating system cpuset to 51 chars (truncate to four and a half 32bit subsets)\n");
178   memset(string, 'X', 52);
179   string[52] = 0;
180   len = hwloc_bitmap_snprintf(string, 51, obj->cpuset);
181   printf("got %s\n", string);
182   assert(len == stringlen);
183   assert(string[49] == 'f');
184   assert(string[50] == 0);
185   assert(string[51] == 'X');
186 
187   obj = hwloc_get_obj_by_depth(topology, depth-1, 0);
188   hwloc_bitmap_snprintf(string, stringlen+1, obj->cpuset);
189   printf("first cpu cpuset is %s\n", string);
190   check_cpuset(obj->cpuset, NULL, NULL, NULL);
191   printf("first cpu cpuset converted back and forth, ok\n");
192 
193   obj = hwloc_get_obj_by_depth(topology, depth-1, hwloc_get_nbobjs_by_depth(topology, depth-1) - 1);
194   hwloc_bitmap_snprintf(string, stringlen+1, obj->cpuset);
195   printf("last cpu cpuset is %s\n", string);
196   check_cpuset(obj->cpuset, NULL, NULL, NULL);
197   printf("last cpu cpuset converted back and forth, ok\n");
198 
199   free(string);
200 
201   hwloc_topology_destroy(topology);
202 
203   return 0;
204 }
205