-
Notifications
You must be signed in to change notification settings - Fork 168
/
hwloc-annotate.c
580 lines (520 loc) · 16.6 KB
/
hwloc-annotate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*
* Copyright © 2012-2020 Inria. All rights reserved.
* See COPYING in top-level directory.
*/
#include "private/autogen/config.h"
#include "hwloc-calc.h"
#include "hwloc.h"
#include "misc.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void usage(const char *callname __hwloc_attribute_unused, FILE *where)
{
fprintf(where, "Usage: hwloc-annotate [options] <input.xml> <output.xml> -- <location1> <location2> ... -- <annotation>\n");
fprintf(where, "Usage: hwloc-annotate [options] <input.xml> <output.xml> <location> <annotation>\n");
fprintf(where, " <location> may be:\n");
fprintf(where, " all, root, <type>:<logicalindex>, <type>:all\n");
fprintf(where, " <annotation> may be:\n");
fprintf(where, " info <name> <value>\n");
fprintf(where, " misc <name>\n");
fprintf(where, " distances <filename> [<flags>]\n");
fprintf(where, " memattr <name> <flags>\n");
fprintf(where, " memattr <name> <initiator> <value>\n");
fprintf(where, " cpukind <cpuset> <efficiency> <flags> [<infoname> <infovalue>]\n");
fprintf(where, " none\n");
fprintf(where, "Options:\n");
fprintf(where, " --ci\tClear existing infos\n");
fprintf(where, " --ri\tReplace or remove existing infos with same name (annotation must be info)\n");
fprintf(where, " --cu\tClear existing userdata\n");
fprintf(where, " --cd\tClear existing distances\n");
}
static char *infoname = NULL, *infovalue = NULL;
static char *miscname = NULL;
static char *distancesfilename = NULL;
static char *maname = NULL;
static unsigned long maflags;
static char *mavname = NULL;
static hwloc_memattr_id_t mavid;
static hwloc_cpuset_t mavicpuset = NULL;
static char *maviobjstr = NULL;
static hwloc_obj_t maviobj = NULL;
static hwloc_uint64_t mavvalue;
static unsigned long distancesflags = 0;
static hwloc_cpuset_t ckcpuset = NULL;
static int ckefficiency = -1;
static unsigned long ckflags = 0;
static char * ckiname = NULL;
static char * ckivalue = NULL;
static int clearinfos = 0;
static int replaceinfos = 0;
static int clearuserdata = 0;
static int cleardistances = 0;
static void apply(hwloc_topology_t topology, hwloc_obj_t obj)
{
unsigned i,j;
if (clearinfos) {
/* this may be considered dangerous, applications should not modify objects directly */
for(i=0; i<obj->infos_count; i++) {
struct hwloc_info_s *info = &obj->infos[i];
free(info->name);
free(info->value);
}
free(obj->infos);
obj->infos = NULL;
obj->infos_count = 0;
}
if (clearuserdata) {
hwloc_utils_userdata_free(obj);
}
if (infoname) {
if (replaceinfos) {
/* this may be considered dangerous, applications should not modify objects directly */
for(i=0, j=0; i<obj->infos_count; i++) {
struct hwloc_info_s *info = &obj->infos[i];
if (!strcmp(infoname, info->name)) {
/* remove info */
free(info->name);
info->name = NULL;
free(info->value);
} else {
if (i != j) {
/* shift info to where it belongs */
obj->infos[j].name = info->name;
obj->infos[j].value = info->value;
}
j++;
}
}
obj->infos_count = j;
if (!j) {
free(obj->infos);
obj->infos = NULL;
}
}
if (infovalue)
hwloc_obj_add_info(obj, infoname, infovalue);
}
if (miscname)
hwloc_topology_insert_misc_object(topology, obj, miscname);
if (mavname) {
struct hwloc_location loc, *locp = NULL;
if (maviobj) {
loc.type = HWLOC_LOCATION_TYPE_OBJECT;
loc.location.object = maviobj;
locp = &loc;
} else if (mavicpuset) {
loc.type = HWLOC_LOCATION_TYPE_CPUSET;
loc.location.cpuset = mavicpuset;
locp = &loc;
}
if (hwloc_memattr_set_value(topology, mavid, obj, locp, 0, mavvalue) < 0) {
fprintf(stderr, "Failed to add memattr value (%s)\n", strerror(errno));
}
}
}
static void apply_recursive(hwloc_topology_t topology, hwloc_obj_t obj)
{
hwloc_obj_t child = NULL;
while ((child = hwloc_get_next_child(topology, obj, child)) != NULL)
apply_recursive(topology, child);
apply(topology, obj);
}
static void
hwloc_calc_process_location_annotate_cb(struct hwloc_calc_location_context_s *lcontext,
void *_data __hwloc_attribute_unused,
hwloc_obj_t obj)
{
apply(lcontext->topology, obj);
}
static void
hwloc_calc_get_unique_obj_cb(struct hwloc_calc_location_context_s *lcontext __hwloc_attribute_unused,
void *_data,
hwloc_obj_t obj)
{
hwloc_obj_t *objp = _data;
if (!*objp)
*objp = obj;
}
static hwloc_obj_t
get_unique_obj(hwloc_topology_t topology, int topodepth, char *str,
int *ignored_multiple)
{
struct hwloc_calc_location_context_s lcontext;
hwloc_obj_t obj;
size_t length;
size_t typelen;
int err;
typelen = strspn(str, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
if (!typelen || str[typelen] != ':')
return NULL;
lcontext.topology = topology;
lcontext.topodepth = topodepth;
lcontext.only_hbm = -1;
lcontext.logical = 1;
lcontext.verbose = 0;
obj = NULL;
length = strspn(str+typelen+1, "0123456789");
if (str[typelen+1+length] == '-' || str[typelen+1+length] == ':') {
*ignored_multiple = 1;
} else {
*ignored_multiple = 0;
}
str[typelen+1+length] = '\0';
err = hwloc_calc_process_location(&lcontext, str, typelen,
hwloc_calc_get_unique_obj_cb, &obj);
if (err < 0)
return NULL;
return obj;
}
static void
add_distances(hwloc_topology_t topology, int topodepth)
{
unsigned long kind = 0;
unsigned nbobjs = 0;
hwloc_obj_t *objs = NULL;
hwloc_uint64_t *values = NULL;
FILE *file;
char line[64];
unsigned i, x, y, z;
hwloc_distances_add_handle_t handle;
int err;
file = fopen(distancesfilename, "r");
if (!file) {
fprintf(stderr, "Failed to open distances file %s\n", distancesfilename);
return;
}
if (!fgets(line, sizeof(line), file)) {
fprintf(stderr, "Failed to read kind line\n");
goto out;
}
kind = strtoul(line, NULL, 0);
if (!fgets(line, sizeof(line), file)) {
fprintf(stderr, "Failed to read nbobjs line\n");
goto out;
}
nbobjs = strtoul(line, NULL, 0);
if (nbobjs < 2) {
fprintf(stderr, "Invalid distances with nbobjs == %u\n", nbobjs);
goto out;
}
objs = malloc(nbobjs * sizeof(*objs));
values = malloc(nbobjs*nbobjs * sizeof(*values));
if (!objs || !values)
goto out;
for(i=0; i<nbobjs; i++) {
hwloc_obj_t obj;
int ignored_multiple;
if (!fgets(line, sizeof(line), file)) {
fprintf(stderr, "Failed to read object line #%u.\n", i);
goto out;
}
obj = get_unique_obj(topology, topodepth, line, &ignored_multiple);
if (!obj) {
fprintf(stderr, "Couldn't parse object line #%u.\n", i);
goto out;
}
if (ignored_multiple) {
fprintf(stderr, "Only the first object specified on line #%u was used.\n", i);
}
objs[i] = obj;
}
/* scan the first value line to see if we have all of them or just a combination */
if (!fgets(line, sizeof(line), file)) {
fprintf(stderr, "Failed to read object #%u line\n", i);
goto out;
}
z = 1; /* default if sscanf finds only 2 values below */
if (sscanf(line, "%u*%u*%u", &x, &y, &z) >= 2) {
/* combination: generate the matrix to create x groups of y elements */
unsigned j;
if (x*y*z != nbobjs) {
fprintf(stderr, "Invalid distances combination (%u*%u*%u=%u instead of %u)\n",
x, y, z, x*y*z, nbobjs);
goto out;
}
for(i=0; i<nbobjs; i++)
for(j=0; j<nbobjs; j++)
if (i==j)
values[i*nbobjs+j] = 10;
else if (i/z == j/z)
values[i*nbobjs+j] = 20;
else if (i/z/y == j/z/y)
values[i*nbobjs+j] = 40;
else
values[i*nbobjs+j] = 80;
} else {
/* read all other values */
values[0] = strtoull(line, NULL, 10);
for(i=1; i<nbobjs*nbobjs; i++) {
if (!fgets(line, sizeof(line), file)) {
fprintf(stderr, "Failed to read object #%u line\n", i);
goto out;
}
values[i] = strtoull(line, NULL, 10);
}
}
err = -1;
handle = hwloc_distances_add_create(topology, NULL, kind, 0);
if (handle) {
err = hwloc_distances_add_values(topology, handle, nbobjs, objs, values, 0);
if (!err) {
err = hwloc_distances_add_commit(topology, handle, distancesflags);
}
}
if (err < 0 || !handle) {
fprintf(stderr, "Failed to add distances\n");
goto out;
}
out:
free(objs);
free(values);
fclose(file);
return;
}
int main(int argc, char *argv[])
{
hwloc_topology_t topology;
char *callname, *input, *output;
char **locations;
int nr_locations;
int topodepth;
int err;
callname = argv[0];
/* skip argv[0], handle options */
argc--;
argv++;
hwloc_utils_check_api_version(callname);
if (!getenv("HWLOC_XML_VERBOSE"))
putenv((char *) "HWLOC_XML_VERBOSE=1");
while (argc && *argv[0] == '-') {
if (!strcmp(argv[0], "--ci"))
clearinfos = 1;
else if (!strcmp(argv[0], "--ri"))
replaceinfos = 1;
else if (!strcmp(argv[0], "--cu"))
clearuserdata = 1;
else if (!strcmp(argv[0], "--cd"))
cleardistances = 1;
else if (!strcmp(argv[0], "-h") || !strcmp(argv[0], "--help")) {
usage(callname, stdout);
exit(EXIT_SUCCESS);
} else {
fprintf(stderr, "Unrecognized options: %s\n", argv[0]);
usage(callname, stderr);
exit(EXIT_FAILURE);
}
argc--;
argv++;
}
if (argc < 3) {
usage(callname, stderr);
exit(EXIT_FAILURE);
}
input = argv[0];
output = argv[1];
argc -= 2;
argv += 2;
if (!strcmp(argv[0], "--")) {
/* modern syntax with locations between "--" */
argc--;
argv++;
locations = &argv[0];
nr_locations = 0;
while (nr_locations < argc && strcmp(argv[nr_locations], "--"))
nr_locations++;
/* check we have an ending "--" */
if (nr_locations == argc || strcmp(argv[nr_locations], "--")) {
usage(callname, stderr);
exit(EXIT_FAILURE);
}
/* skip those locations and the ending "--" */
argc -= nr_locations+1;
argv += nr_locations+1;
} else {
/* old syntax with a single location without "--" before and after */
locations = &argv[0];
nr_locations = 1;
argc--;
argv++;
}
if (argc < 1) {
usage(callname, stderr);
exit(EXIT_FAILURE);
}
if (!strcmp(argv[0], "info")) {
if (argc < 2 || (!replaceinfos && argc < 3)) {
usage(callname, stderr);
exit(EXIT_FAILURE);
}
infoname = argv[1];
infovalue = argc >= 3 ? argv[2] : NULL;
} else if (!strcmp(argv[0], "misc")) {
if (argc < 2) {
usage(callname, stderr);
exit(EXIT_FAILURE);
}
miscname = argv[1];
} else if (!strcmp(argv[0], "distances")) {
if (argc < 2) {
usage(callname, stderr);
exit(EXIT_FAILURE);
}
distancesfilename = argv[1];
if (argc >= 3) {
distancesflags = hwloc_utils_parse_distances_add_flags(argv[2]);
if(distancesflags == (unsigned long)-1)
goto out;
}
} else if (!strcmp(argv[0], "memattr")) {
if (argc < 3) {
usage(callname, stderr);
exit(EXIT_FAILURE);
}
if (argc == 3) {
maname = argv[1];
maflags = hwloc_utils_parse_memattr_flags(argv[2]);
} else {
mavname = argv[1];
mavvalue = strtoull(argv[3], NULL, 0);
if (strcmp(argv[2], "none")) {
if (!strncmp(argv[2], "0x", 2)) {
/* parse a cpuset */
mavicpuset = hwloc_bitmap_alloc();
if (!mavicpuset) {
fprintf(stderr, "Failed to allocate cpuset for memattr initiator\n");
goto out;
}
hwloc_bitmap_sscanf(mavicpuset, argv[2]);
} else {
/* parse an object */
maviobjstr = argv[2];
}
}
}
} else if (!strcmp(argv[0], "cpukind")) {
if (argc < 4) {
usage(callname, stderr);
exit(EXIT_FAILURE);
}
ckcpuset = hwloc_bitmap_alloc();
if (!ckcpuset) {
fprintf(stderr, "Failed to allocate cpuset for cpukind\n");
goto out;
}
hwloc_bitmap_sscanf(ckcpuset, argv[1]);
ckefficiency = atoi(argv[2]);
ckflags = atoi(argv[3]);
if (argc == 5) {
fprintf(stderr, "cpukind with info name without info value\n");
goto out;
}
if (argc >= 6 && *argv[4] && *argv[5]) {
ckiname = argv[4];
ckivalue = argv[5];
}
} else if (!strcmp(argv[0], "none")) {
/* do nothing (maybe clear) */
} else {
fprintf(stderr, "Unrecognized annotation type: %s\n", argv[0]);
usage(callname, stderr);
exit(EXIT_FAILURE);
}
if (replaceinfos && !infoname) {
fprintf(stderr, "--ri missing a info name\n");
usage(callname, stderr);
exit(EXIT_FAILURE);
}
hwloc_topology_init(&topology);
hwloc_topology_set_all_types_filter(topology, HWLOC_TYPE_FILTER_KEEP_ALL);
hwloc_topology_set_flags(topology, HWLOC_TOPOLOGY_FLAG_INCLUDE_DISALLOWED | HWLOC_TOPOLOGY_FLAG_IMPORT_SUPPORT);
err = hwloc_topology_set_xml(topology, input);
if (err < 0)
goto out_with_topology;
putenv((char *) "HWLOC_XML_USERDATA_NOT_DECODED=1");
hwloc_topology_set_userdata_import_callback(topology, hwloc_utils_userdata_import_cb);
hwloc_topology_set_userdata_export_callback(topology, hwloc_utils_userdata_export_cb);
err = hwloc_topology_load(topology);
if (err < 0)
goto out_with_topology;
topodepth = hwloc_topology_get_depth(topology);
if (cleardistances) {
hwloc_distances_remove(topology);
}
if (distancesfilename) {
/* ignore locations */
add_distances(topology, topodepth);
} else if (maname) {
hwloc_memattr_id_t id;
err = hwloc_memattr_register(topology, maname, maflags, &id);
if (err < 0) {
fprintf(stderr, "Failed to register new memattr (%s)\n", strerror(errno));
goto out_with_topology;
}
} else if (ckcpuset) {
struct hwloc_info_s info;
info.name = ckiname;
info.value = ckivalue;
if (hwloc_cpukinds_register(topology, ckcpuset, ckefficiency,
ckiname ? 1 : 0, ckiname ? &info : NULL,
ckflags) < 0) {
fprintf(stderr, "Failed to register CPU kind (%s)\n", strerror(errno));
}
} else {
int i;
unsigned long mavflags = 0;
if (mavname) {
mavid = hwloc_utils_parse_memattr_name(topology, mavname);
if (mavid == (hwloc_memattr_id_t) -1) {
fprintf(stderr, "Failed to find memattr by name %s\n", mavname);
goto out_with_topology;
}
hwloc_memattr_get_flags(topology, mavid, &mavflags);
}
if (maviobjstr && (mavflags & HWLOC_MEMATTR_FLAG_NEED_INITIATOR)) {
int ignored_multiple;
maviobj = get_unique_obj(topology, topodepth, maviobjstr, &ignored_multiple);
if (!maviobj) {
fprintf(stderr, "Failed to find memattr initiator object %s\n", maviobjstr);
goto out_with_topology;
}
if (ignored_multiple) {
fprintf(stderr, "Only the first object specified is used as a memattr initiator.\n");
}
}
for(i=0; i<nr_locations; i++) {
char *location = locations[i];
if (!strcmp(location, "all")) {
apply_recursive(topology, hwloc_get_root_obj(topology));
} else if (!strcmp(location, "root")) {
apply(topology, hwloc_get_root_obj(topology));
} else {
size_t typelen;
typelen = strspn(location, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
if (typelen && (location[typelen] == ':' || location[typelen] == '=' || location[typelen] == '[')) {
struct hwloc_calc_location_context_s lcontext;
lcontext.topology = topology;
lcontext.topodepth = topodepth;
lcontext.only_hbm = -1;
lcontext.logical = 1;
lcontext.verbose = 0;
err = hwloc_calc_process_location(&lcontext, location, typelen,
hwloc_calc_process_location_annotate_cb, NULL);
}
}
}
}
err = hwloc_topology_export_xml(topology, output, 0);
if (err < 0)
goto out;
hwloc_utils_userdata_free_recursive(hwloc_get_root_obj(topology));
hwloc_topology_destroy(topology);
exit(EXIT_SUCCESS);
out_with_topology:
hwloc_utils_userdata_free_recursive(hwloc_get_root_obj(topology));
hwloc_topology_destroy(topology);
out:
hwloc_bitmap_free(mavicpuset);
hwloc_bitmap_free(ckcpuset);
exit(EXIT_FAILURE);
}