-
Notifications
You must be signed in to change notification settings - Fork 168
/
topology-synthetic.c
1576 lines (1367 loc) · 44.9 KB
/
topology-synthetic.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
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
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright © 2009 CNRS
* Copyright © 2009-2022 Inria. All rights reserved.
* Copyright © 2009-2010 Université Bordeaux
* Copyright © 2009-2011 Cisco Systems, Inc. All rights reserved.
* See COPYING in top-level directory.
*/
#include "private/autogen/config.h"
#include "hwloc.h"
#include "private/private.h"
#include "private/misc.h"
#include "private/debug.h"
#include <limits.h>
#include <assert.h>
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
struct hwloc_synthetic_attr_s {
hwloc_obj_type_t type;
unsigned depth; /* For caches/groups */
hwloc_obj_cache_type_t cachetype; /* For caches */
hwloc_uint64_t memorysize; /* For caches/memory */
};
struct hwloc_synthetic_indexes_s {
/* the indexes= attribute before parsing */
const char *string;
unsigned long string_length;
/* the array of explicit indexes after parsing */
unsigned *array;
/* used while filling the topology */
unsigned next; /* id of the next object for that level */
};
struct hwloc_synthetic_level_data_s {
unsigned arity;
unsigned long totalwidth;
struct hwloc_synthetic_attr_s attr;
struct hwloc_synthetic_indexes_s indexes;
struct hwloc_synthetic_attached_s {
struct hwloc_synthetic_attr_s attr;
struct hwloc_synthetic_attached_s *next;
} *attached;
};
struct hwloc_synthetic_backend_data_s {
/* synthetic backend parameters */
char *string;
unsigned long numa_attached_nr;
struct hwloc_synthetic_indexes_s numa_attached_indexes;
#define HWLOC_SYNTHETIC_MAX_DEPTH 128
struct hwloc_synthetic_level_data_s level[HWLOC_SYNTHETIC_MAX_DEPTH];
};
struct hwloc_synthetic_intlv_loop_s {
unsigned step;
unsigned nb;
unsigned level_depth;
};
static void
hwloc_synthetic_process_indexes(struct hwloc_synthetic_backend_data_s *data,
struct hwloc_synthetic_indexes_s *indexes,
unsigned long total,
int verbose)
{
const char *attr = indexes->string;
unsigned long length = indexes->string_length;
unsigned *array = NULL;
size_t i;
if (!attr)
return;
array = calloc(total, sizeof(*array));
if (!array) {
if (verbose)
fprintf(stderr, "Failed to allocate synthetic index array of size %lu\n", total);
goto out;
}
i = strspn(attr, "0123456789,");
if (i == length) {
/* explicit array of indexes */
for(i=0; i<total; i++) {
const char *next;
unsigned idx = strtoul(attr, (char **) &next, 10);
if (next == attr) {
if (verbose)
fprintf(stderr, "Failed to read synthetic index #%lu at '%s'\n", (unsigned long) i, attr);
goto out_with_array;
}
array[i] = idx;
if (i != total-1) {
if (*next != ',') {
if (verbose)
fprintf(stderr, "Missing comma after synthetic index #%lu at '%s'\n", (unsigned long) i, attr);
goto out_with_array;
}
attr = next+1;
} else {
attr = next;
}
}
indexes->array = array;
} else {
/* interleaving */
unsigned nr_loops = 1, cur_loop;
unsigned minstep = total;
unsigned long nbs = 1;
unsigned j, mul;
const char *tmp;
struct hwloc_synthetic_intlv_loop_s *loops;
tmp = attr;
while (tmp) {
tmp = strchr(tmp, ':');
if (!tmp || tmp >= attr+length)
break;
nr_loops++;
tmp++;
}
/* nr_loops colon-separated fields, but we may need one more at the end */
loops = malloc((nr_loops+1) * sizeof(*loops));
if (!loops)
goto out_with_array;
if (*attr >= '0' && *attr <= '9') {
/* interleaving as x*y:z*t:... */
unsigned step, nb;
tmp = attr;
cur_loop = 0;
while (tmp) {
char *tmp2, *tmp3;
step = (unsigned) strtol(tmp, &tmp2, 0);
if (tmp2 == tmp || *tmp2 != '*') {
if (verbose)
fprintf(stderr, "Failed to read synthetic index interleaving loop '%s' without number before '*'\n", tmp);
free(loops);
goto out_with_array;
}
if (!step) {
if (verbose)
fprintf(stderr, "Invalid interleaving loop with step 0 at '%s'\n", tmp);
free(loops);
goto out_with_array;
}
tmp2++;
nb = (unsigned) strtol(tmp2, &tmp3, 0);
if (tmp3 == tmp2 || (*tmp3 && *tmp3 != ':' && *tmp3 != ')' && *tmp3 != ' ')) {
if (verbose)
fprintf(stderr, "Failed to read synthetic index interleaving loop '%s' without number between '*' and ':'\n", tmp);
free(loops);
goto out_with_array;
}
if (!nb) {
if (verbose)
fprintf(stderr, "Invalid interleaving loop with number 0 at '%s'\n", tmp2);
free(loops);
goto out_with_array;
}
loops[cur_loop].step = step;
loops[cur_loop].nb = nb;
if (step < minstep)
minstep = step;
nbs *= nb;
cur_loop++;
if (*tmp3 == ')' || *tmp3 == ' ')
break;
tmp = (const char*) (tmp3+1);
}
} else {
/* interleaving as type1:type2:... */
hwloc_obj_type_t type;
union hwloc_obj_attr_u attrs;
int err;
/* find level depths for each interleaving loop */
tmp = attr;
cur_loop = 0;
while (tmp) {
err = hwloc_type_sscanf(tmp, &type, &attrs, sizeof(attrs));
if (err < 0) {
if (verbose)
fprintf(stderr, "Failed to read synthetic index interleaving loop type '%s'\n", tmp);
free(loops);
goto out_with_array;
}
if (type == HWLOC_OBJ_MISC || type == HWLOC_OBJ_BRIDGE || type == HWLOC_OBJ_PCI_DEVICE || type == HWLOC_OBJ_OS_DEVICE) {
if (verbose)
fprintf(stderr, "Misc object type disallowed in synthetic index interleaving loop type '%s'\n", tmp);
free(loops);
goto out_with_array;
}
for(i=0; ; i++) {
if (!data->level[i].arity) {
loops[cur_loop].level_depth = (unsigned)-1;
break;
}
if (type != data->level[i].attr.type)
continue;
if (type == HWLOC_OBJ_GROUP
&& attrs.group.depth != (unsigned) -1
&& attrs.group.depth != data->level[i].attr.depth)
continue;
loops[cur_loop].level_depth = (unsigned)i;
break;
}
if (loops[cur_loop].level_depth == (unsigned)-1) {
if (verbose)
fprintf(stderr, "Failed to find level for synthetic index interleaving loop type '%s'\n",
tmp);
free(loops);
goto out_with_array;
}
tmp = strchr(tmp, ':');
if (!tmp || tmp > attr+length)
break;
tmp++;
cur_loop++;
}
/* compute actual loop step/nb */
for(cur_loop=0; cur_loop<nr_loops; cur_loop++) {
unsigned mydepth = loops[cur_loop].level_depth;
unsigned prevdepth = 0;
unsigned step, nb;
for(i=0; i<nr_loops; i++) {
if (loops[i].level_depth == mydepth && i != cur_loop) {
if (verbose)
fprintf(stderr, "Invalid duplicate interleaving loop type in synthetic index '%s'\n", attr);
free(loops);
goto out_with_array;
}
if (loops[i].level_depth < mydepth
&& loops[i].level_depth > prevdepth)
prevdepth = loops[i].level_depth;
}
step = total / data->level[mydepth].totalwidth; /* number of objects below us */
nb = data->level[mydepth].totalwidth / data->level[prevdepth].totalwidth; /* number of us within parent */
loops[cur_loop].step = step;
loops[cur_loop].nb = nb;
assert(nb);
assert(step);
if (step < minstep)
minstep = step;
nbs *= nb;
}
}
assert(nbs);
if (nbs != total) {
/* one loop of total/nbs steps is missing, add it if it's just the smallest one */
if (minstep == total/nbs) {
loops[nr_loops].step = 1;
loops[nr_loops].nb = total/nbs;
nr_loops++;
} else {
if (verbose)
fprintf(stderr, "Invalid index interleaving total width %lu instead of %lu\n", nbs, total);
free(loops);
goto out_with_array;
}
}
/* generate the array of indexes */
mul = 1;
for(i=0; i<nr_loops; i++) {
unsigned step = loops[i].step;
unsigned nb = loops[i].nb;
for(j=0; j<total; j++)
array[j] += ((j / step) % nb) * mul;
mul *= nb;
}
free(loops);
/* check that we have the right values (cannot pass total, cannot give duplicate 0) */
for(j=0; j<total; j++) {
if (array[j] >= total) {
if (verbose)
fprintf(stderr, "Invalid index interleaving generates out-of-range index %u\n", array[j]);
goto out_with_array;
}
if (!array[j] && j) {
if (verbose)
fprintf(stderr, "Invalid index interleaving generates duplicate index values\n");
goto out_with_array;
}
}
indexes->array = array;
}
return;
out_with_array:
free(array);
out:
return;
}
static hwloc_uint64_t
hwloc_synthetic_parse_memory_attr(const char *attr, const char **endp)
{
const char *endptr;
hwloc_uint64_t size;
size = strtoull(attr, (char **) &endptr, 0);
if (!hwloc_strncasecmp(endptr, "TB", 2)) {
size *= 1000ULL*1000ULL*1000ULL*1000ULL;
endptr += 2;
} else if (!hwloc_strncasecmp(endptr, "TiB", 3)) {
size <<= 40;
endptr += 3;
} else if (!hwloc_strncasecmp(endptr, "GB", 2)) {
size *= 1000ULL*1000ULL*1000ULL;
endptr += 2;
} else if (!hwloc_strncasecmp(endptr, "GiB", 3)) {
size <<= 30;
endptr += 3;
} else if (!hwloc_strncasecmp(endptr, "MB", 2)) {
size *= 1000ULL*1000ULL;
endptr += 2;
} else if (!hwloc_strncasecmp(endptr, "MiB", 3)) {
size <<= 20;
endptr += 3;
} else if (!hwloc_strncasecmp(endptr, "kB", 2)) {
size *= 1000ULL;
endptr += 2;
} else if (!hwloc_strncasecmp(endptr, "kiB", 3)) {
size <<= 10;
endptr += 3;
}
*endp = endptr;
return size;
}
static int
hwloc_synthetic_parse_attrs(const char *attrs, const char **next_posp,
struct hwloc_synthetic_attr_s *sattr,
struct hwloc_synthetic_indexes_s *sind,
int verbose)
{
hwloc_obj_type_t type = sattr->type;
const char *next_pos;
hwloc_uint64_t memorysize = 0;
const char *index_string = NULL;
size_t index_string_length = 0;
next_pos = (const char *) strchr(attrs, ')');
if (!next_pos) {
if (verbose)
fprintf(stderr, "Missing attribute closing bracket in synthetic string doesn't have a number of objects at '%s'\n", attrs);
errno = EINVAL;
return -1;
}
while (')' != *attrs) {
int iscache = hwloc__obj_type_is_cache(type);
if (iscache && !strncmp("size=", attrs, 5)) {
memorysize = hwloc_synthetic_parse_memory_attr(attrs+5, &attrs);
} else if (!iscache && !strncmp("memory=", attrs, 7)) {
memorysize = hwloc_synthetic_parse_memory_attr(attrs+7, &attrs);
} else if (!strncmp("indexes=", attrs, 8)) {
index_string = attrs+8;
attrs += 8;
index_string_length = strcspn(attrs, " )");
attrs += index_string_length;
} else {
if (verbose)
fprintf(stderr, "Unknown attribute at '%s'\n", attrs);
errno = EINVAL;
return -1;
}
if (' ' == *attrs)
attrs++;
else if (')' != *attrs) {
if (verbose)
fprintf(stderr, "Missing parameter separator at '%s'\n", attrs);
errno = EINVAL;
return -1;
}
}
sattr->memorysize = memorysize;
if (index_string) {
if (sind->string && verbose)
fprintf(stderr, "Overwriting duplicate indexes attribute with last occurence\n");
sind->string = index_string;
sind->string_length = (unsigned long)index_string_length;
}
*next_posp = next_pos+1;
return 0;
}
/* frees level until arity = 0 */
static void
hwloc_synthetic_free_levels(struct hwloc_synthetic_backend_data_s *data)
{
unsigned i;
for(i=0; i<HWLOC_SYNTHETIC_MAX_DEPTH; i++) {
struct hwloc_synthetic_level_data_s *curlevel = &data->level[i];
struct hwloc_synthetic_attached_s **pprev = &curlevel->attached;
while (*pprev) {
struct hwloc_synthetic_attached_s *cur = *pprev;
*pprev = cur->next;
free(cur);
}
free(curlevel->indexes.array);
if (!curlevel->arity)
break;
}
free(data->numa_attached_indexes.array);
}
/* Read from description a series of integers describing a symmetrical
topology and update the hwloc_synthetic_backend_data_s accordingly. On
success, return zero. */
static int
hwloc_backend_synthetic_init(struct hwloc_synthetic_backend_data_s *data,
const char *description)
{
const char *pos, *next_pos;
unsigned long item, count;
unsigned i;
int type_count[HWLOC_OBJ_TYPE_MAX];
unsigned unset;
int verbose = 0;
const char *env = getenv("HWLOC_SYNTHETIC_VERBOSE");
int err;
unsigned long totalarity = 1;
if (env)
verbose = atoi(env);
data->numa_attached_nr = 0;
data->numa_attached_indexes.array = NULL;
/* default values before we add root attributes */
data->level[0].totalwidth = 1;
data->level[0].attr.type = HWLOC_OBJ_MACHINE;
data->level[0].indexes.string = NULL;
data->level[0].indexes.array = NULL;
data->level[0].attr.memorysize = 0;
data->level[0].attached = NULL;
type_count[HWLOC_OBJ_MACHINE] = 1;
if (*description == '(') {
err = hwloc_synthetic_parse_attrs(description+1, &description, &data->level[0].attr, &data->level[0].indexes, verbose);
if (err < 0)
return err;
}
data->numa_attached_indexes.string = NULL;
data->numa_attached_indexes.array = NULL;
for (pos = description, count = 1; *pos; pos = next_pos) {
hwloc_obj_type_t type = HWLOC_OBJ_TYPE_NONE;
union hwloc_obj_attr_u attrs;
/* initialize parent arity to 0 so that the levels are not infinite */
data->level[count-1].arity = 0;
while (*pos == ' ' || *pos == '\n')
pos++;
if (!*pos)
break;
if (*pos == '[') {
/* attached */
struct hwloc_synthetic_attached_s *attached, **pprev;
char *attr;
pos++;
if (hwloc_type_sscanf(pos, &type, &attrs, sizeof(attrs)) < 0) {
if (verbose)
fprintf(stderr, "Synthetic string with unknown attached object type at '%s'\n", pos);
errno = EINVAL;
goto error;
}
if (type != HWLOC_OBJ_NUMANODE) {
if (verbose)
fprintf(stderr, "Synthetic string with disallowed attached object type at '%s'\n", pos);
errno = EINVAL;
goto error;
}
data->numa_attached_nr += data->level[count-1].totalwidth;
attached = malloc(sizeof(*attached));
if (attached) {
attached->attr.type = type;
attached->attr.memorysize = 0;
/* attached->attr.depth and .cachetype unused */
attached->next = NULL;
pprev = &data->level[count-1].attached;
while (*pprev)
pprev = &((*pprev)->next);
*pprev = attached;
}
next_pos = strchr(pos, ']');
if (!next_pos) {
if (verbose)
fprintf(stderr,"Synthetic string doesn't have a closing `]' after attached object type at '%s'\n", pos);
errno = EINVAL;
goto error;
}
attr = strchr(pos, '(');
if (attr && attr < next_pos && attached) {
const char *dummy;
err = hwloc_synthetic_parse_attrs(attr+1, &dummy, &attached->attr, &data->numa_attached_indexes, verbose);
if (err < 0)
goto error;
}
next_pos++;
continue;
}
/* normal level */
/* reset defaults */
data->level[count].indexes.string = NULL;
data->level[count].indexes.array = NULL;
data->level[count].attached = NULL;
if (*pos < '0' || *pos > '9') {
if (hwloc_type_sscanf(pos, &type, &attrs, sizeof(attrs)) < 0) {
if (!strncmp(pos, "Tile", 4) || !strncmp(pos, "Module", 6)) {
/* possible future types */
type = HWLOC_OBJ_GROUP;
} else {
/* FIXME: allow generic "Cache" string? would require to deal with possibly duplicate cache levels */
if (verbose)
fprintf(stderr, "Synthetic string with unknown object type at '%s'\n", pos);
errno = EINVAL;
goto error;
}
}
if (type == HWLOC_OBJ_MACHINE || type == HWLOC_OBJ_MISC || type == HWLOC_OBJ_BRIDGE || type == HWLOC_OBJ_PCI_DEVICE || type == HWLOC_OBJ_OS_DEVICE) {
if (verbose)
fprintf(stderr, "Synthetic string with disallowed object type at '%s'\n", pos);
errno = EINVAL;
goto error;
}
next_pos = strchr(pos, ':');
if (!next_pos) {
if (verbose)
fprintf(stderr,"Synthetic string doesn't have a `:' after object type at '%s'\n", pos);
errno = EINVAL;
goto error;
}
pos = next_pos + 1;
}
data->level[count].attr.type = type;
data->level[count].attr.depth = (unsigned) -1;
data->level[count].attr.cachetype = (hwloc_obj_cache_type_t) -1;
if (hwloc__obj_type_is_cache(type)) {
/* these are always initialized */
data->level[count].attr.depth = attrs.cache.depth;
data->level[count].attr.cachetype = attrs.cache.type;
} else if (type == HWLOC_OBJ_GROUP) {
/* could be -1 but will be set below */
data->level[count].attr.depth = attrs.group.depth;
}
/* number of normal children */
item = strtoul(pos, (char **)&next_pos, 0);
if (next_pos == pos) {
if (verbose)
fprintf(stderr,"Synthetic string doesn't have a number of objects at '%s'\n", pos);
errno = EINVAL;
goto error;
}
if (!item) {
if (verbose)
fprintf(stderr,"Synthetic string with disallow 0 number of objects at '%s'\n", pos);
errno = EINVAL;
goto error;
}
totalarity *= item;
data->level[count].totalwidth = totalarity;
data->level[count].indexes.string = NULL;
data->level[count].indexes.array = NULL;
data->level[count].attr.memorysize = 0;
if (*next_pos == '(') {
err = hwloc_synthetic_parse_attrs(next_pos+1, &next_pos, &data->level[count].attr, &data->level[count].indexes, verbose);
if (err < 0)
goto error;
}
if (count + 1 >= HWLOC_SYNTHETIC_MAX_DEPTH) {
if (verbose)
fprintf(stderr,"Too many synthetic levels, max %d\n", HWLOC_SYNTHETIC_MAX_DEPTH);
errno = EINVAL;
goto error;
}
if (item > UINT_MAX) {
if (verbose)
fprintf(stderr,"Too big arity, max %u\n", UINT_MAX);
errno = EINVAL;
goto error;
}
data->level[count-1].arity = (unsigned)item;
count++;
}
if (data->level[count-1].attr.type != HWLOC_OBJ_TYPE_NONE && data->level[count-1].attr.type != HWLOC_OBJ_PU) {
if (verbose)
fprintf(stderr, "Synthetic string cannot use non-PU type for last level\n");
errno = EINVAL;
return -1;
}
data->level[count-1].attr.type = HWLOC_OBJ_PU;
for(i=HWLOC_OBJ_TYPE_MIN; i<HWLOC_OBJ_TYPE_MAX; i++) {
type_count[i] = 0;
}
for(i=count-1; i>0; i--) {
hwloc_obj_type_t type = data->level[i].attr.type;
if (type != HWLOC_OBJ_TYPE_NONE) {
type_count[type]++;
}
}
/* sanity checks */
if (!type_count[HWLOC_OBJ_PU]) {
if (verbose)
fprintf(stderr, "Synthetic string missing ending number of PUs\n");
errno = EINVAL;
return -1;
} else if (type_count[HWLOC_OBJ_PU] > 1) {
if (verbose)
fprintf(stderr, "Synthetic string cannot have several PU levels\n");
errno = EINVAL;
return -1;
}
if (type_count[HWLOC_OBJ_PACKAGE] > 1) {
if (verbose)
fprintf(stderr, "Synthetic string cannot have several package levels\n");
errno = EINVAL;
return -1;
}
if (type_count[HWLOC_OBJ_DIE] > 1) {
if (verbose)
fprintf(stderr, "Synthetic string cannot have several die levels\n");
errno = EINVAL;
return -1;
}
if (type_count[HWLOC_OBJ_NUMANODE] > 1) {
if (verbose)
fprintf(stderr, "Synthetic string cannot have several NUMA node levels\n");
errno = EINVAL;
return -1;
}
if (type_count[HWLOC_OBJ_NUMANODE] && data->numa_attached_nr) {
if (verbose)
fprintf(stderr,"Synthetic string cannot have NUMA nodes both as a level and attached\n");
errno = EINVAL;
return -1;
}
if (type_count[HWLOC_OBJ_CORE] > 1) {
if (verbose)
fprintf(stderr, "Synthetic string cannot have several core levels\n");
errno = EINVAL;
return -1;
}
/* deal with missing intermediate levels */
unset = 0;
for(i=1; i<count-1; i++) {
if (data->level[i].attr.type == HWLOC_OBJ_TYPE_NONE)
unset++;
}
if (unset && unset != count-2) {
if (verbose)
fprintf(stderr, "Synthetic string cannot mix unspecified and specified types for levels\n");
errno = EINVAL;
return -1;
}
if (unset) {
/* we want in priority: numa, package, core, up to 3 caches, groups */
unsigned _count = count;
unsigned neednuma = 0;
unsigned needpack = 0;
unsigned needcore = 0;
unsigned needcaches = 0;
unsigned needgroups = 0;
/* 2 levels for machine and PU */
_count -= 2;
neednuma = (_count >= 1 && !data->numa_attached_nr);
_count -= neednuma;
needpack = (_count >= 1);
_count -= needpack;
needcore = (_count >= 1);
_count -= needcore;
needcaches = (_count > 4 ? 4 : _count);
_count -= needcaches;
needgroups = _count;
/* we place them in order: groups, package, numa, caches, core */
for(i = 0; i < needgroups; i++) {
unsigned depth = 1 + i;
data->level[depth].attr.type = HWLOC_OBJ_GROUP;
type_count[HWLOC_OBJ_GROUP]++;
}
if (needpack) {
unsigned depth = 1 + needgroups;
data->level[depth].attr.type = HWLOC_OBJ_PACKAGE;
type_count[HWLOC_OBJ_PACKAGE] = 1;
}
if (neednuma) {
unsigned depth = 1 + needgroups + needpack;
data->level[depth].attr.type = HWLOC_OBJ_NUMANODE;
type_count[HWLOC_OBJ_NUMANODE] = 1;
}
if (needcaches) {
/* priority: l2, l1, l3, l1i */
/* order: l3, l2, l1, l1i */
unsigned l3depth = 1 + needgroups + needpack + neednuma;
unsigned l2depth = l3depth + (needcaches >= 3);
unsigned l1depth = l2depth + 1;
unsigned l1idepth = l1depth + 1;
if (needcaches >= 3) {
data->level[l3depth].attr.type = HWLOC_OBJ_L3CACHE;
data->level[l3depth].attr.depth = 3;
data->level[l3depth].attr.cachetype = HWLOC_OBJ_CACHE_UNIFIED;
type_count[HWLOC_OBJ_L3CACHE] = 1;
}
data->level[l2depth].attr.type = HWLOC_OBJ_L2CACHE;
data->level[l2depth].attr.depth = 2;
data->level[l2depth].attr.cachetype = HWLOC_OBJ_CACHE_UNIFIED;
type_count[HWLOC_OBJ_L2CACHE] = 1;
if (needcaches >= 2) {
data->level[l1depth].attr.type = HWLOC_OBJ_L1CACHE;
data->level[l1depth].attr.depth = 1;
data->level[l1depth].attr.cachetype = HWLOC_OBJ_CACHE_DATA;
type_count[HWLOC_OBJ_L1CACHE] = 1;
}
if (needcaches >= 4) {
data->level[l1idepth].attr.type = HWLOC_OBJ_L1ICACHE;
data->level[l1idepth].attr.depth = 1;
data->level[l1idepth].attr.cachetype = HWLOC_OBJ_CACHE_INSTRUCTION;
type_count[HWLOC_OBJ_L1ICACHE] = 1;
}
}
if (needcore) {
unsigned depth = 1 + needgroups + needpack + neednuma + needcaches;
data->level[depth].attr.type = HWLOC_OBJ_CORE;
type_count[HWLOC_OBJ_CORE] = 1;
}
}
/* enforce a NUMA level */
if (!type_count[HWLOC_OBJ_NUMANODE] && !data->numa_attached_nr) {
/* insert a NUMA level below the automatic machine root */
if (verbose)
fprintf(stderr, "Inserting a NUMA level with a single object at depth 1\n");
/* move existing levels by one */
memmove(&data->level[2], &data->level[1], count*sizeof(struct hwloc_synthetic_level_data_s));
data->level[1].attr.type = HWLOC_OBJ_NUMANODE;
data->level[1].indexes.string = NULL;
data->level[1].indexes.array = NULL;
data->level[1].attr.memorysize = 0;
data->level[1].totalwidth = data->level[0].totalwidth;
/* update arity to insert a single NUMA node per parent */
data->level[1].arity = data->level[0].arity;
data->level[0].arity = 1;
count++;
}
for (i=0; i<count; i++) {
struct hwloc_synthetic_level_data_s *curlevel = &data->level[i];
hwloc_obj_type_t type = curlevel->attr.type;
if (type == HWLOC_OBJ_GROUP) {
if (curlevel->attr.depth == (unsigned)-1)
curlevel->attr.depth = type_count[HWLOC_OBJ_GROUP]--;
} else if (hwloc__obj_type_is_cache(type)) {
if (!curlevel->attr.memorysize) {
if (1 == curlevel->attr.depth)
/* 32KiB in L1 */
curlevel->attr.memorysize = 32*1024;
else
/* *4 at each level, starting from 1MiB for L2, unified */
curlevel->attr.memorysize = 256ULL*1024 << (2*curlevel->attr.depth);
}
} else if (type == HWLOC_OBJ_NUMANODE && !curlevel->attr.memorysize) {
/* 1GiB in memory nodes. */
curlevel->attr.memorysize = 1024*1024*1024;
}
hwloc_synthetic_process_indexes(data, &data->level[i].indexes, data->level[i].totalwidth, verbose);
}
hwloc_synthetic_process_indexes(data, &data->numa_attached_indexes, data->numa_attached_nr, verbose);
data->string = strdup(description);
data->level[count-1].arity = 0;
return 0;
error:
hwloc_synthetic_free_levels(data);
return -1;
}
static void
hwloc_synthetic_set_attr(struct hwloc_synthetic_attr_s *sattr,
hwloc_obj_t obj)
{
switch (obj->type) {
case HWLOC_OBJ_GROUP:
obj->attr->group.kind = HWLOC_GROUP_KIND_SYNTHETIC;
obj->attr->group.subkind = sattr->depth-1;
break;
case HWLOC_OBJ_MACHINE:
break;
case HWLOC_OBJ_NUMANODE:
obj->attr->numanode.local_memory = sattr->memorysize;
obj->attr->numanode.page_types_len = 1;
obj->attr->numanode.page_types = malloc(sizeof(*obj->attr->numanode.page_types));
memset(obj->attr->numanode.page_types, 0, sizeof(*obj->attr->numanode.page_types));
obj->attr->numanode.page_types[0].size = 4096;
obj->attr->numanode.page_types[0].count = sattr->memorysize / 4096;
break;
case HWLOC_OBJ_PACKAGE:
case HWLOC_OBJ_DIE:
break;
case HWLOC_OBJ_L1CACHE:
case HWLOC_OBJ_L2CACHE:
case HWLOC_OBJ_L3CACHE:
case HWLOC_OBJ_L4CACHE:
case HWLOC_OBJ_L5CACHE:
case HWLOC_OBJ_L1ICACHE:
case HWLOC_OBJ_L2ICACHE:
case HWLOC_OBJ_L3ICACHE:
obj->attr->cache.depth = sattr->depth;
obj->attr->cache.linesize = 64;
obj->attr->cache.type = sattr->cachetype;
obj->attr->cache.size = sattr->memorysize;
break;
case HWLOC_OBJ_CORE:
break;
case HWLOC_OBJ_PU:
break;
default:
/* Should never happen */
assert(0);
break;
}
}
static unsigned
hwloc_synthetic_next_index(struct hwloc_synthetic_indexes_s *indexes, hwloc_obj_type_t type)
{
unsigned os_index = indexes->next++;
if (indexes->array)
os_index = indexes->array[os_index];
else if (hwloc__obj_type_is_cache(type) || type == HWLOC_OBJ_GROUP)
/* don't enforce useless os_indexes for Caches and Groups */
os_index = HWLOC_UNKNOWN_INDEX;
return os_index;
}
static void
hwloc_synthetic_insert_attached(struct hwloc_topology *topology,
struct hwloc_synthetic_backend_data_s *data,
struct hwloc_synthetic_attached_s *attached,
hwloc_bitmap_t set)
{
hwloc_obj_t child;
unsigned attached_os_index;
if (!attached)
return;
assert(attached->attr.type == HWLOC_OBJ_NUMANODE);
attached_os_index = hwloc_synthetic_next_index(&data->numa_attached_indexes, HWLOC_OBJ_NUMANODE);
child = hwloc_alloc_setup_object(topology, attached->attr.type, attached_os_index);
child->cpuset = hwloc_bitmap_dup(set);
child->nodeset = hwloc_bitmap_alloc();
hwloc_bitmap_set(child->nodeset, attached_os_index);
hwloc_synthetic_set_attr(&attached->attr, child);
hwloc__insert_object_by_cpuset(topology, NULL, child, "synthetic:attached");
hwloc_synthetic_insert_attached(topology, data, attached->next, set);
}
/*
* Recursively build objects whose cpu start at first_cpu
* - level gives where to look in the type, arity and id arrays
* - the id array is used as a variable to get unique IDs for a given level.
* - generated memory should be added to *memory_kB.
* - generated cpus should be added to parent_cpuset.
* - next cpu number to be used should be returned.
*/
static void
hwloc__look_synthetic(struct hwloc_topology *topology,
struct hwloc_synthetic_backend_data_s *data,
int level,
hwloc_bitmap_t parent_cpuset)
{
hwloc_obj_t obj;
unsigned i;
struct hwloc_synthetic_level_data_s *curlevel = &data->level[level];
hwloc_obj_type_t type = curlevel->attr.type;
hwloc_bitmap_t set;
unsigned os_index;
assert(hwloc__obj_type_is_normal(type) || type == HWLOC_OBJ_NUMANODE);
assert(type != HWLOC_OBJ_MACHINE);
os_index = hwloc_synthetic_next_index(&curlevel->indexes, type);
set = hwloc_bitmap_alloc();
if (!curlevel->arity) {
hwloc_bitmap_set(set, os_index);
} else {
for (i = 0; i < curlevel->arity; i++)
hwloc__look_synthetic(topology, data, level + 1, set);
}
hwloc_bitmap_or(parent_cpuset, parent_cpuset, set);
if (hwloc_filter_check_keep_object_type(topology, type)) {
obj = hwloc_alloc_setup_object(topology, type, os_index);
obj->cpuset = hwloc_bitmap_dup(set);
if (type == HWLOC_OBJ_NUMANODE) {
obj->nodeset = hwloc_bitmap_alloc();
hwloc_bitmap_set(obj->nodeset, os_index);
}
hwloc_synthetic_set_attr(&curlevel->attr, obj);
hwloc__insert_object_by_cpuset(topology, NULL, obj, "synthetic");
}
hwloc_synthetic_insert_attached(topology, data, curlevel->attached, set);
hwloc_bitmap_free(set);
}
static int
hwloc_look_synthetic(struct hwloc_backend *backend, struct hwloc_disc_status *dstatus)
{
/*
* This backend enforces !topology->is_thissystem by default.
*/
struct hwloc_topology *topology = backend->topology;
struct hwloc_synthetic_backend_data_s *data = backend->private_data;
hwloc_bitmap_t cpuset = hwloc_bitmap_alloc();
unsigned i;
assert(dstatus->phase == HWLOC_DISC_PHASE_GLOBAL);