-
Notifications
You must be signed in to change notification settings - Fork 168
/
hwloc-calc.h
849 lines (763 loc) · 24.5 KB
/
hwloc-calc.h
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
/*
* Copyright © 2009 CNRS
* Copyright © 2009-2023 Inria. All rights reserved.
* Copyright © 2009-2012 Université Bordeaux
* Copyright © 2011 Cisco Systems, Inc. All rights reserved.
* See COPYING in top-level directory.
*/
#ifndef HWLOC_CALC_H
#define HWLOC_CALC_H
#include "hwloc.h"
#include "private/misc.h" /* for HWLOC_OBJ_TYPE_NONE */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include <ctype.h>
#include <assert.h>
/* this is a global that doesn't change when walking hierarchy of locations, etc */
struct hwloc_calc_location_context_s {
hwloc_topology_t topology;
int topodepth;
int only_hbm; /* -1 for everything, 0 for only non-HBM, 1 for only HBM numa nodes */
int logical;
int verbose;
};
/* this a local that changes when going from one level to another in the hierarchy of locations, etc */
struct hwloc_calc_level {
int depth;
hwloc_obj_type_t type;
union hwloc_obj_attr_u attr;
};
typedef enum hwloc_calc_append_mode_e {
HWLOC_CALC_APPEND_ADD,
HWLOC_CALC_APPEND_CLR,
HWLOC_CALC_APPEND_AND,
HWLOC_CALC_APPEND_XOR
} hwloc_calc_append_mode_t;
static __hwloc_inline int
hwloc_calc_append_set(hwloc_bitmap_t set, hwloc_const_bitmap_t newset,
hwloc_calc_append_mode_t mode, int verbose)
{
char *s1, *s2;
hwloc_bitmap_asprintf(&s1, newset);
hwloc_bitmap_asprintf(&s2, set);
switch (mode) {
case HWLOC_CALC_APPEND_ADD:
if (verbose > 0)
fprintf(stderr, "adding %s to %s\n",
s1, s2);
hwloc_bitmap_or(set, set, newset);
break;
case HWLOC_CALC_APPEND_CLR:
if (verbose > 0)
fprintf(stderr, "clearing %s from %s\n",
s1, s2);
hwloc_bitmap_andnot(set, set, newset);
break;
case HWLOC_CALC_APPEND_AND:
if (verbose > 0)
fprintf(stderr, "and'ing %s from %s\n",
s1, s2);
hwloc_bitmap_and(set, set, newset);
break;
case HWLOC_CALC_APPEND_XOR:
if (verbose > 0)
fprintf(stderr, "xor'ing %s from %s\n",
s1, s2);
hwloc_bitmap_xor(set, set, newset);
break;
default:
assert(0);
}
free(s1);
free(s2);
return 0;
}
static __hwloc_inline unsigned
hwloc_calc_get_nbobjs_inside_sets_by_depth(struct hwloc_calc_location_context_s *lcontext,
hwloc_const_bitmap_t cpuset, hwloc_const_bitmap_t nodeset,
int depth)
{
hwloc_topology_t topology = lcontext->topology;
int only_hbm = lcontext->only_hbm;
hwloc_obj_t obj = NULL;
unsigned n = 0;
while ((obj = hwloc_get_next_obj_by_depth(topology, depth, obj)) != NULL) {
if (!hwloc_bitmap_iszero(obj->cpuset) && !hwloc_bitmap_intersects(obj->cpuset, cpuset))
continue;
if (!hwloc_bitmap_iszero(obj->nodeset) && !hwloc_bitmap_intersects(obj->nodeset, nodeset))
continue;
if (hwloc_bitmap_iszero(obj->cpuset) && hwloc_bitmap_iszero(obj->nodeset))
/* ignore objects with empty sets (both can be empty when outside of cgroup) */
continue;
if (only_hbm >= 0 && obj->type == HWLOC_OBJ_NUMANODE) {
/* filter on hbm */
int obj_is_hbm = obj->subtype && !strcmp(obj->subtype, "MCDRAM");
if (only_hbm != obj_is_hbm)
continue;
}
n++;
}
return n;
}
static __hwloc_inline hwloc_obj_t
hwloc_calc_get_obj_inside_sets_by_depth(struct hwloc_calc_location_context_s *lcontext,
hwloc_const_bitmap_t cpuset, hwloc_const_bitmap_t nodeset,
int depth, unsigned ind)
{
hwloc_topology_t topology = lcontext->topology;
int only_hbm = lcontext->only_hbm;
int logical = lcontext->logical;
hwloc_obj_t obj = NULL;
unsigned i = 0;
while ((obj = hwloc_get_next_obj_by_depth(topology, depth, obj)) != NULL) {
if (!hwloc_bitmap_iszero(obj->cpuset) && !hwloc_bitmap_intersects(obj->cpuset, cpuset))
continue;
if (!hwloc_bitmap_iszero(obj->nodeset) && !hwloc_bitmap_intersects(obj->nodeset, nodeset))
continue;
if (hwloc_bitmap_iszero(obj->cpuset) && hwloc_bitmap_iszero(obj->nodeset))
/* ignore objects with empty sets (both can be empty when outside of cgroup) */
continue;
if (only_hbm >= 0 && obj->type == HWLOC_OBJ_NUMANODE) {
/* filter on hbm */
int obj_is_hbm = obj->subtype && !strcmp(obj->subtype, "MCDRAM");
if (only_hbm != obj_is_hbm)
continue;
}
if (logical) {
if (i == ind)
return obj;
i++;
} else {
if (obj->os_index == ind)
return obj;
}
}
return NULL;
}
static __hwloc_inline int
hwloc_calc_parse_level(struct hwloc_calc_location_context_s *lcontext,
hwloc_topology_t topology,
const char *_typestring, size_t typelen,
struct hwloc_calc_level *level)
{
char typestring[20+1]; /* large enough to store all type names, even with a depth attribute */
char *endptr;
int err;
level->depth = HWLOC_TYPE_DEPTH_UNKNOWN;
if (typelen >= sizeof(typestring))
return -1;
snprintf(typestring, typelen+1, "%s", _typestring);
err = hwloc_type_sscanf(typestring, &level->type, &level->attr, sizeof(level->attr));
if (!err) {
/* parsed a correct type */
level->depth = hwloc_get_type_depth_with_attr(topology, level->type, &level->attr, sizeof(level->attr));
if (level->depth == HWLOC_TYPE_DEPTH_UNKNOWN
|| level->depth == HWLOC_TYPE_DEPTH_MULTIPLE)
return -1;
return 0;
}
if (!strcasecmp(typestring, "HBM") || !strcasecmp(typestring, "MCDRAM")) {
if (lcontext && lcontext->only_hbm == -1)
lcontext->only_hbm = 1;
level->type = HWLOC_OBJ_NUMANODE;
level->depth = HWLOC_TYPE_DEPTH_NUMANODE;
return 0;
}
/* couldn't parse the type, try a depth value */
level->depth = strtoul(typestring, &endptr, 0);
if (typestring[0] == '-' || *endptr || level->depth >= hwloc_topology_get_depth(topology)) {
level->depth = HWLOC_TYPE_DEPTH_UNKNOWN;
return -1;
}
level->type = HWLOC_OBJ_TYPE_NONE;
return 0;
}
static __hwloc_inline int
hwloc_calc_parse_depth_prefix(struct hwloc_calc_location_context_s *lcontext,
const char *string, size_t typelen,
hwloc_obj_type_t *typep)
{
hwloc_topology_t topology = lcontext->topology;
int topodepth = lcontext->topodepth;
int verbose = lcontext->verbose;
char typestring[20+1]; /* large enough to store all type names, even with a depth attribute */
hwloc_obj_type_t type;
union hwloc_obj_attr_u attr;
int depth;
char *end;
int err;
if (typelen >= sizeof(typestring)) {
if (verbose >= 0)
fprintf(stderr, "invalid type name %s\n", string);
return -1;
}
strncpy(typestring, string, typelen);
typestring[typelen] = '\0';
/* try to match a type name */
err = hwloc_type_sscanf(typestring, &type, &attr, sizeof(attr));
if (!err) {
*typep = type;
return hwloc_get_type_depth_with_attr(topology, type, &attr, sizeof(attr));
}
if (!strcasecmp(typestring, "HBM") || !strcasecmp(typestring, "MCDRAM")) {
if (lcontext->only_hbm == -1)
lcontext->only_hbm = 1;
*typep = HWLOC_OBJ_NUMANODE;
depth = HWLOC_TYPE_DEPTH_NUMANODE;
return depth;
}
/* try to match a numeric depth */
depth = strtol(string, &end, 0);
if (end != &string[typelen]) {
if (verbose >= 0)
fprintf(stderr, "invalid type name %s\n", string);
return -1;
}
if (depth >= topodepth) {
if (verbose >= 0)
fprintf(stderr, "ignoring invalid depth %d\n", depth);
return -1;
}
*typep = HWLOC_OBJ_TYPE_NONE;
return depth;
}
static __hwloc_inline int
hwloc_calc_parse_range(const char *_string,
int *firstp, int *amountp, int *stepp, int *wrapp,
const char **dotp,
int verbose)
{
char string[65];
size_t len;
char *dot, *end, *end2;
long first, last, amount;
int wrap;
dot = strchr(_string, '.');
*dotp = dot;
if (dot) {
len = dot - _string;
} else {
len = strlen(_string);
}
if (len >= sizeof(string)) {
if (verbose >= 0)
fprintf(stderr, "invalid range `%s', too long\n", _string);
return -1;
}
memcpy(string, _string, len);
string[len] = '\0';
if (!isdigit(*string)) {
if (!strncmp(string, "all", 3)) {
*firstp = 0;
*amountp = -1;
*stepp = 1;
*wrapp = 0;
return 0;
} else if (!strncmp(string, "odd", 3)) {
*firstp = 1;
*amountp = -1;
*stepp = 2;
*wrapp = 0;
return 0;
} else if (!strncmp(string, "even", 4)) {
*firstp = 0;
*amountp = -1;
*stepp = 2;
*wrapp = 0;
return 0;
} else {
if (verbose >= 0)
fprintf(stderr, "unrecognized range keyword `%s'\n", string);
return -1;
}
}
first = strtol(string, &end, 10);
amount = 1;
wrap = 0;
if (*end == '-') {
last = strtol(end+1, &end2, 10);
if (*end2) {
if (verbose >= 0)
fprintf(stderr, "invalid character at `%s' after range at `%s'\n", end2, string);
return -1;
} else if (end2 == end+1) {
/* X- */
amount = -1;
} else {
/* X-Y */
amount = last-first+1;
}
} else if (*end == ':') {
/* X:Y */
wrap = 1;
amount = strtol(end+1, &end2, 10);
if (*end2) {
if (verbose >= 0)
fprintf(stderr, "invalid character at `%s' after range at `%s'\n", end2, string);
return -1;
} else if (end2 == end+1) {
if (verbose >= 0)
fprintf(stderr, "missing width at `%s' in range at `%s'\n", end2, string);
return -1;
}
} else if (*end) {
if (verbose >= 0)
fprintf(stderr, "invalid character at `%s' after index at `%s'\n", end, string);
return -1;
}
*firstp = first;
*amountp = amount;
*stepp = 1;
*wrapp = wrap;
return 0;
}
static __hwloc_inline int
hwloc_calc_append_object_range(struct hwloc_calc_location_context_s *lcontext,
hwloc_const_bitmap_t rootcpuset, hwloc_const_bitmap_t rootnodeset, int depth,
const char *string, /* starts with indexes following the colon */
void (*cbfunc)(struct hwloc_calc_location_context_s *, void *, hwloc_obj_t), void *cbdata)
{
int verbose = lcontext->verbose;
hwloc_obj_t obj;
unsigned width;
const char *dot, *nextsep = NULL;
int nextdepth = -1;
int first, wrap, amount, step;
unsigned i,j;
int found = 0;
int err;
err = hwloc_calc_parse_range(string,
&first, &amount, &step, &wrap,
&dot,
verbose);
if (err < 0) {
if (verbose >= 0)
fprintf(stderr, "Failed to parse object index range %s\n", string);
return -1;
}
assert(amount != -1 || !wrap);
if (dot) {
/* parse the next string before calling ourself recursively */
size_t typelen;
hwloc_obj_type_t type;
const char *nextstring = dot+1;
typelen = strspn(nextstring, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
if (!typelen || nextstring[typelen] != ':') {
if (verbose >= 0)
fprintf(stderr, "hierarchical sublocation %s contains types not followed by colon and index range\n", nextstring);
return -1;
}
nextsep = &nextstring[typelen];
nextdepth = hwloc_calc_parse_depth_prefix(lcontext,
nextstring, typelen,
&type);
if (nextdepth == HWLOC_TYPE_DEPTH_UNKNOWN) {
if (verbose >= 0)
fprintf(stderr, "could not find level specified by location %s\n", nextstring);
return -1;
}
if (nextdepth == HWLOC_TYPE_DEPTH_MULTIPLE) {
if (verbose >= 0)
fprintf(stderr, "found multiple levels for location %s\n", nextstring);
return -1;
}
/* we need an object with a cpuset, that's depth>=0 or memory */
if (nextdepth < 0 && nextdepth != HWLOC_TYPE_DEPTH_NUMANODE) {
if (verbose >= 0)
fprintf(stderr, "hierarchical location %s only supported with normal object types\n", string);
return -1;
}
}
width = hwloc_calc_get_nbobjs_inside_sets_by_depth(lcontext, rootcpuset, rootnodeset, depth);
if (amount == -1)
amount = (width-first+step-1)/step;
for(i=first, j=0; j<(unsigned)amount; i+=step, j++) {
if (wrap && i>=width)
i = 0;
obj = hwloc_calc_get_obj_inside_sets_by_depth(lcontext, rootcpuset, rootnodeset, depth, i);
if (verbose > 0 || (!obj && verbose >= 0)) {
char *sc, *sn;
hwloc_bitmap_asprintf(&sc, rootcpuset);
hwloc_bitmap_asprintf(&sn, rootnodeset);
if (obj)
printf("using object #%u depth %d below cpuset %s nodeset %s\n",
i, depth, sc, sn);
else
fprintf(stderr, "object #%u depth %d below cpuset %s nodeset %s does not exist\n",
i, depth, sc, sn);
free(sc);
free(sn);
}
if (obj) {
found++;
if (dot) {
hwloc_calc_append_object_range(lcontext, obj->cpuset, obj->nodeset, nextdepth, nextsep+1, cbfunc, cbdata);
} else {
/* add to the temporary cpuset
* and let the caller add/clear/and/xor for the actual final cpuset depending on cmdline options
*/
cbfunc(lcontext, cbdata, obj);
}
}
}
if (!found && verbose >= 0)
fprintf(stderr, "failed to use any single object in index range %s\n", string);
return 0;
}
static __hwloc_inline int
hwloc_calc_append_iodev(struct hwloc_calc_location_context_s *lcontext,
void (*cbfunc)(struct hwloc_calc_location_context_s *, void *, hwloc_obj_t), void *cbdata,
hwloc_obj_t obj)
{
cbfunc(lcontext, cbdata, obj);
return 0;
}
static __hwloc_inline int
hwloc_calc_append_iodev_by_index(struct hwloc_calc_location_context_s *lcontext,
hwloc_obj_type_t type, int depth, const char *string,
void (*cbfunc)(struct hwloc_calc_location_context_s *, void *, hwloc_obj_t), void *cbdata)
{
hwloc_topology_t topology = lcontext->topology;
int verbose = lcontext->verbose;
hwloc_obj_t obj, prev = NULL;
int pcivendor = -1, pcidevice = -1;
hwloc_obj_osdev_type_t osdevtype = 0; /* none */
const char *current, *dot;
char *endp;
int first = 0, step = 1, amount = 1, wrap = 0; /* assume the index suffix is `:0' by default */
int err, i, max;
if (*string == '[') {
/* matching */
current = string+1;
if (type == HWLOC_OBJ_PCI_DEVICE) {
/* try to match by [vendor:device] */
pcivendor = strtoul(current, &endp, 16);
if (*endp != ':') {
if (verbose >= 0)
fprintf(stderr, "invalid PCI vendor:device matching specification %s\n", string);
return -1;
}
if (endp == current)
pcivendor = -1;
current = endp+1;
pcidevice = strtoul(current, &endp, 16);
if (*endp != ']') {
if (verbose >= 0)
fprintf(stderr, "invalid PCI vendor:device matching specification %s\n", string);
return -1;
}
if (endp == current)
pcidevice = -1;
current = endp+1;
if (*current != ':' && *current != '\0') {
if (verbose >= 0)
fprintf(stderr, "invalid PCI vendor:device matching specification %s\n", string);
return -1;
}
} else if (type == HWLOC_OBJ_OS_DEVICE) {
/* try to match by [osdevtype] */
hwloc_obj_type_t type2;
union hwloc_obj_attr_u attr;
endp = strchr(current, ']');
if (!endp) {
if (verbose >= 0)
fprintf(stderr, "invalid OS device subtype specification %s\n", string);
return -1;
}
*endp = 0;
err = hwloc_type_sscanf(current, &type2, &attr, sizeof(attr));
*endp = ']';
if (err < 0 || type2 != HWLOC_OBJ_OS_DEVICE) {
if (verbose >= 0)
fprintf(stderr, "invalid OS device subtype specification %s\n", string);
return -1;
}
osdevtype = attr.osdev.type;
current = endp+1;
if (*current != ':' && *current != '\0') {
if (verbose >= 0)
fprintf(stderr, "invalid OS device subtype specification %s\n", string);
return -1;
}
} else {
/* no matching for non-PCI devices */
if (verbose >= 0)
fprintf(stderr, "invalid matching specification %s\n", string);
return -1;
}
} else {
/* no matching */
current = string;
}
if (*current != '\0') {
current++;
err = hwloc_calc_parse_range(current,
&first, &amount, &step, &wrap,
&dot,
verbose);
if (dot) {
fprintf(stderr, "hierarchical location %s only supported with normal object types\n", string);
return -1;
}
if (err < 0) {
if (verbose >= 0)
fprintf(stderr, "Failed to parse object index range %s\n", current);
return -1;
}
}
max = hwloc_get_nbobjs_by_depth(topology, depth);
for(i=0; i < max*(wrap+1); i++) {
if (i == max && wrap) {
i = 0;
wrap = 0;
}
obj = hwloc_get_obj_by_depth(topology, depth, i);
assert(obj);
if (obj == prev) /* already used that object, stop wrapping around */
break;
if (type == HWLOC_OBJ_PCI_DEVICE) {
if (pcivendor != -1 && (int) obj->attr->pcidev.vendor_id != pcivendor)
continue;
if (pcidevice != -1 && (int) obj->attr->pcidev.device_id != pcidevice)
continue;
}
if (type == HWLOC_OBJ_OS_DEVICE) {
if ((obj->attr->osdev.type & osdevtype) != osdevtype)
continue;
}
if (first--)
continue;
/* ok, got one object */
if (verbose > 0)
printf("using matching PCI object #%d bus id %04x:%02x:%02x.%01x\n", i,
obj->attr->pcidev.domain, obj->attr->pcidev.bus, obj->attr->pcidev.dev, obj->attr->pcidev.func);
hwloc_calc_append_iodev(lcontext, cbfunc, cbdata, obj);
if (!prev)
prev = obj;
amount--;
if (!amount)
break;
first = step-1;
}
return 0;
}
static __hwloc_inline int
hwloc_calc_process_location(struct hwloc_calc_location_context_s *lcontext,
const char *arg, size_t typelen,
void (*cbfunc)(struct hwloc_calc_location_context_s *, void *, hwloc_obj_t), void *cbdata)
{
hwloc_topology_t topology = lcontext->topology;
int verbose = lcontext->verbose;
const char *sep = &arg[typelen];
hwloc_obj_type_t type = HWLOC_OBJ_TYPE_NONE;
int depth;
depth = hwloc_calc_parse_depth_prefix(lcontext,
arg, typelen,
&type);
if (depth == HWLOC_TYPE_DEPTH_UNKNOWN) {
if (verbose >= 0)
fprintf(stderr, "could not find level specified by location %s\n", arg);
return -1;
}
if (depth == HWLOC_TYPE_DEPTH_MULTIPLE) {
if (verbose >= 0)
fprintf(stderr, "found multiple levels for location %s\n", arg);
return -1;
}
if (depth < 0 && depth != HWLOC_TYPE_DEPTH_NUMANODE) {
/* special object without cpusets */
/* if we didn't find a depth but found a type, handle special cases */
hwloc_obj_t obj = NULL;
if (*sep == ':' || *sep == '[') {
return hwloc_calc_append_iodev_by_index(lcontext, type, depth, sep, cbfunc, cbdata);
} else if (*sep == '=' && type == HWLOC_OBJ_PCI_DEVICE) {
/* try to match a busid */
obj = hwloc_get_pcidev_by_busidstring(topology, sep+1);
if (obj)
return hwloc_calc_append_iodev(lcontext, cbfunc, cbdata, obj);
if (verbose >= 0)
fprintf(stderr, "invalid PCI device %s\n", sep+1);
return -1;
} else if (*sep == '=' && type == HWLOC_OBJ_OS_DEVICE) {
/* try to match a OS device name */
while ((obj = hwloc_get_next_osdev(topology, obj)) != NULL) {
if (!strcmp(obj->name, sep+1))
return hwloc_calc_append_iodev(lcontext, cbfunc, cbdata, obj);
}
if (verbose >= 0)
fprintf(stderr, "invalid OS device %s\n", sep+1);
return -1;
} else if (*sep == '=' && type == HWLOC_OBJ_MISC) {
/* try to match a Misc device name */
obj = hwloc_get_obj_by_type(topology, HWLOC_OBJ_MISC, 0);
while (obj) {
if (!strcmp(obj->name, sep+1))
return hwloc_calc_append_iodev(lcontext, cbfunc, cbdata, obj);
obj = obj->next_cousin;
}
if (verbose >= 0)
fprintf(stderr, "invalid Misc object %s\n", sep+1);
return -1;
} else
return -1;
}
/* look at indexes following this type/depth */
return hwloc_calc_append_object_range(lcontext,
hwloc_topology_get_complete_cpuset(topology),
hwloc_topology_get_complete_nodeset(topology),
depth, sep+1, cbfunc, cbdata);
}
struct hwloc_calc_set_context_s {
int nodeset_input;
int nodeset_output;
hwloc_bitmap_t output_set;
};
struct hwloc_calc_process_location_set_cbdata_s {
struct hwloc_calc_set_context_s *scontext;
hwloc_bitmap_t set;
};
static __hwloc_inline void
hwloc_calc_process_location_set_cb(struct hwloc_calc_location_context_s *lcontext, void *_data, hwloc_obj_t obj)
{
int verbose = lcontext->verbose;
struct hwloc_calc_process_location_set_cbdata_s *cbdata = _data;
hwloc_bitmap_t set = cbdata->set;
int nodeset_output = cbdata->scontext->nodeset_output;
/* walk up out of I/O objects */
while (obj && !obj->cpuset)
obj = obj->parent;
if (!obj)
/* do nothing */
return;
hwloc_calc_append_set(set,
nodeset_output ? obj->nodeset : obj->cpuset,
HWLOC_CALC_APPEND_ADD, verbose);
}
static __hwloc_inline int
hwloc_calc_process_location_as_set(struct hwloc_calc_location_context_s *lcontext,
struct hwloc_calc_set_context_s *scontext,
const char *arg)
{
hwloc_topology_t topology = lcontext->topology;
int verbose = lcontext->verbose;
int nodeset_output = scontext->nodeset_output;
int nodeset_input = scontext->nodeset_input;
hwloc_bitmap_t output_set = scontext->output_set;
hwloc_calc_append_mode_t mode = HWLOC_CALC_APPEND_ADD;
size_t typelen;
int err;
if (*arg == '~') {
mode = HWLOC_CALC_APPEND_CLR;
arg++;
} else if (*arg == 'x') {
mode = HWLOC_CALC_APPEND_AND;
arg++;
} else if (*arg == '^') {
mode = HWLOC_CALC_APPEND_XOR;
arg++;
}
if (!strcmp(arg, "all") || !strcmp(arg, "root"))
return hwloc_calc_append_set(output_set,
nodeset_output ? hwloc_topology_get_topology_nodeset(topology) : hwloc_topology_get_topology_cpuset(topology),
mode, verbose);
/* try to match a type/depth followed by a special character */
typelen = strspn(arg, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
if (typelen && (arg[typelen] == ':' || arg[typelen] == '=' || arg[typelen] == '[')) {
/* process type/depth */
struct hwloc_calc_process_location_set_cbdata_s cbdata;
cbdata.set = hwloc_bitmap_alloc();
cbdata.scontext = scontext;
err = hwloc_calc_process_location(lcontext, arg, typelen,
hwloc_calc_process_location_set_cb, &cbdata);
if (!err)
err = hwloc_calc_append_set(output_set, cbdata.set, mode, verbose);
hwloc_bitmap_free(cbdata.set);
} else {
/* try to match a cpuset */
char *tmp = (char*) arg;
hwloc_bitmap_t newset;
int taskset = ( strchr(tmp, ',') == NULL );
/* check the infinite prefix */
if (hwloc_strncasecmp(tmp, "0xf...f,", 7+!taskset) == 0) {
tmp += 7+!taskset;
if (0 == *tmp) {
err = -1;
goto out;
}
}
if (taskset) {
/* check that the remaining is 0x followed by a huge hexadecimal number */
if (hwloc_strncasecmp(tmp, "0x", 2) != 0) {
err = -1;
goto out;
}
tmp += 2;
if (0 == *tmp) {
err = -1;
goto out;
}
if (strlen(tmp) != strspn(tmp, "0123456789abcdefABCDEF")) {
err = -1;
goto out;
}
} else {
/* check that the remaining is a comma-separated list of hexadecimal integer with 0x as an optional prefix */
while (1) {
char *next = strchr(tmp, ',');
size_t len;
if (hwloc_strncasecmp(tmp, "0x", 2) == 0) {
tmp += 2;
if (',' == *tmp || 0 == *tmp) {
err = -1;
goto out;
}
}
len = next ? (size_t) (next-tmp) : strlen(tmp);
if (len != strspn(tmp, "0123456789abcdefABCDEF")) {
err = -1;
goto out;
}
if (!next)
break;
tmp = next+1;
}
}
newset = hwloc_bitmap_alloc();
if (taskset)
hwloc_bitmap_taskset_sscanf(newset, arg);
else
hwloc_bitmap_sscanf(newset, arg);
if (nodeset_output && !nodeset_input) {
hwloc_bitmap_t newnset = hwloc_bitmap_alloc();
hwloc_cpuset_to_nodeset(topology, newset, newnset);
err = hwloc_calc_append_set(output_set, newnset, mode, verbose);
hwloc_bitmap_free(newnset);
} else if (nodeset_input && !nodeset_output) {
hwloc_bitmap_t newcset = hwloc_bitmap_alloc();
hwloc_cpuset_from_nodeset(topology, newcset, newset);
err = hwloc_calc_append_set(output_set, newcset, mode, verbose);
hwloc_bitmap_free(newcset);
} else {
err = hwloc_calc_append_set(output_set, newset, mode, verbose);
}
hwloc_bitmap_free(newset);
}
out:
return err;
}
static __hwloc_inline void
hwloc_calc_locations_usage(FILE *where)
{
fprintf (where, " core:2-3 for the third and fourth cores\n");
fprintf (where, " node:1.pu:2 the third PU of the second NUMA node\n");
fprintf (where, " 0x12345678 a CPU set given a bitmask string\n");
fprintf (where, " os=eth0 the operating system device named eth0\n");
fprintf (where, " pci=0000:01:02.0 the PCI device with the given bus ID\n");
fprintf (where, " with prefix ~ to remove, ^ for xor and x for intersection\n");
fprintf (where, " (see Location Specification in hwloc(7) for details).\n");
}
#endif /* HWLOC_CALC_H */