/
topology-xml.c
3115 lines (2772 loc) · 96.7 KB
/
topology-xml.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-2019 Inria. All rights reserved.
* Copyright © 2009-2011 Université Bordeaux
* Copyright © 2009-2018 Cisco Systems, Inc. All rights reserved.
* See COPYING in top-level directory.
*/
#include "private/autogen/config.h"
#include "hwloc.h"
#include "private/xml.h"
#include "private/private.h"
#include "private/misc.h"
#include "private/debug.h"
#include <math.h>
int
hwloc__xml_verbose(void)
{
static int checked = 0;
static int verbose = 0;
if (!checked) {
const char *env = getenv("HWLOC_XML_VERBOSE");
if (env)
verbose = atoi(env);
checked = 1;
}
return verbose;
}
static int
hwloc_nolibxml_import(void)
{
static int checked = 0;
static int nolibxml = 0;
if (!checked) {
const char *env = getenv("HWLOC_LIBXML");
if (env) {
nolibxml = !atoi(env);
} else {
env = getenv("HWLOC_LIBXML_IMPORT");
if (env)
nolibxml = !atoi(env);
}
checked = 1;
}
return nolibxml;
}
static int
hwloc_nolibxml_export(void)
{
static int checked = 0;
static int nolibxml = 0;
if (!checked) {
const char *env = getenv("HWLOC_LIBXML");
if (env) {
nolibxml = !atoi(env);
} else {
env = getenv("HWLOC_LIBXML_EXPORT");
if (env)
nolibxml = !atoi(env);
}
checked = 1;
}
return nolibxml;
}
#define BASE64_ENCODED_LENGTH(length) (4*(((length)+2)/3))
/*********************************
********* XML callbacks *********
*********************************/
/* set when registering nolibxml and libxml components.
* modifications protected by the components mutex.
* read by the common XML code in topology-xml.c to jump to the right XML backend.
*/
static struct hwloc_xml_callbacks *hwloc_nolibxml_callbacks = NULL, *hwloc_libxml_callbacks = NULL;
void
hwloc_xml_callbacks_register(struct hwloc_xml_component *comp)
{
if (!hwloc_nolibxml_callbacks)
hwloc_nolibxml_callbacks = comp->nolibxml_callbacks;
if (!hwloc_libxml_callbacks)
hwloc_libxml_callbacks = comp->libxml_callbacks;
}
void
hwloc_xml_callbacks_reset(void)
{
hwloc_nolibxml_callbacks = NULL;
hwloc_libxml_callbacks = NULL;
}
/************************************************
********* XML import (common routines) *********
************************************************/
#define _HWLOC_OBJ_CACHE_OLD (HWLOC_OBJ_TYPE_MAX+1) /* temporarily used when importing pre-v2.0 attribute-less cache types */
#define _HWLOC_OBJ_FUTURE (HWLOC_OBJ_TYPE_MAX+2) /* temporarily used when ignoring future types */
static void
hwloc__xml_import_object_attr(struct hwloc_topology *topology,
struct hwloc_xml_backend_data_s *data,
struct hwloc_obj *obj,
const char *name, const char *value,
hwloc__xml_import_state_t state)
{
if (!strcmp(name, "type")) {
/* already handled */
return;
}
else if (!strcmp(name, "os_index"))
obj->os_index = strtoul(value, NULL, 10);
else if (!strcmp(name, "gp_index")) {
obj->gp_index = strtoull(value, NULL, 10);
if (!obj->gp_index && hwloc__xml_verbose())
fprintf(stderr, "%s: unexpected zero gp_index, topology may be invalid\n", state->global->msgprefix);
if (obj->gp_index >= topology->next_gp_index)
topology->next_gp_index = obj->gp_index + 1;
} else if (!strcmp(name, "cpuset")) {
if (!obj->cpuset)
obj->cpuset = hwloc_bitmap_alloc();
hwloc_bitmap_sscanf(obj->cpuset, value);
} else if (!strcmp(name, "complete_cpuset")) {
if (!obj->complete_cpuset)
obj->complete_cpuset = hwloc_bitmap_alloc();
hwloc_bitmap_sscanf(obj->complete_cpuset, value);
} else if (!strcmp(name, "allowed_cpuset")) {
/* ignored except for root */
if (!obj->parent)
hwloc_bitmap_sscanf(topology->allowed_cpuset, value);
} else if (!strcmp(name, "nodeset")) {
if (!obj->nodeset)
obj->nodeset = hwloc_bitmap_alloc();
hwloc_bitmap_sscanf(obj->nodeset, value);
} else if (!strcmp(name, "complete_nodeset")) {
if (!obj->complete_nodeset)
obj->complete_nodeset = hwloc_bitmap_alloc();
hwloc_bitmap_sscanf(obj->complete_nodeset, value);
} else if (!strcmp(name, "allowed_nodeset")) {
/* ignored except for root */
if (!obj->parent)
hwloc_bitmap_sscanf(topology->allowed_nodeset, value);
} else if (!strcmp(name, "name")) {
if (obj->name)
free(obj->name);
obj->name = strdup(value);
} else if (!strcmp(name, "subtype")) {
if (obj->subtype)
free(obj->subtype);
obj->subtype = strdup(value);
}
else if (!strcmp(name, "cache_size")) {
unsigned long long lvalue = strtoull(value, NULL, 10);
if (hwloc__obj_type_is_cache(obj->type) || obj->type == _HWLOC_OBJ_CACHE_OLD || obj->type == HWLOC_OBJ_MEMCACHE)
obj->attr->cache.size = lvalue;
else if (hwloc__xml_verbose())
fprintf(stderr, "%s: ignoring cache_size attribute for non-cache object type\n",
state->global->msgprefix);
}
else if (!strcmp(name, "cache_linesize")) {
unsigned long lvalue = strtoul(value, NULL, 10);
if (hwloc__obj_type_is_cache(obj->type) || obj->type == _HWLOC_OBJ_CACHE_OLD || obj->type == HWLOC_OBJ_MEMCACHE)
obj->attr->cache.linesize = lvalue;
else if (hwloc__xml_verbose())
fprintf(stderr, "%s: ignoring cache_linesize attribute for non-cache object type\n",
state->global->msgprefix);
}
else if (!strcmp(name, "cache_associativity")) {
int lvalue = atoi(value);
if (hwloc__obj_type_is_cache(obj->type) || obj->type == _HWLOC_OBJ_CACHE_OLD || obj->type == HWLOC_OBJ_MEMCACHE)
obj->attr->cache.associativity = lvalue;
else if (hwloc__xml_verbose())
fprintf(stderr, "%s: ignoring cache_associativity attribute for non-cache object type\n",
state->global->msgprefix);
}
else if (!strcmp(name, "cache_type")) {
unsigned long lvalue = strtoul(value, NULL, 10);
if (hwloc__obj_type_is_cache(obj->type) || obj->type == _HWLOC_OBJ_CACHE_OLD || obj->type == HWLOC_OBJ_MEMCACHE) {
if (lvalue == HWLOC_OBJ_CACHE_UNIFIED
|| lvalue == HWLOC_OBJ_CACHE_DATA
|| lvalue == HWLOC_OBJ_CACHE_INSTRUCTION)
obj->attr->cache.type = (hwloc_obj_cache_type_t) lvalue;
else
fprintf(stderr, "%s: ignoring invalid cache_type attribute %lu\n",
state->global->msgprefix, lvalue);
} else if (hwloc__xml_verbose())
fprintf(stderr, "%s: ignoring cache_type attribute for non-cache object type\n",
state->global->msgprefix);
}
else if (!strcmp(name, "local_memory")) {
unsigned long long lvalue = strtoull(value, NULL, 10);
if (obj->type == HWLOC_OBJ_NUMANODE)
obj->attr->numanode.local_memory = lvalue;
else if (!obj->parent)
topology->machine_memory.local_memory = lvalue;
else if (hwloc__xml_verbose())
fprintf(stderr, "%s: ignoring local_memory attribute for non-NUMAnode non-root object\n",
state->global->msgprefix);
}
else if (!strcmp(name, "depth")) {
unsigned long lvalue = strtoul(value, NULL, 10);
if (hwloc__obj_type_is_cache(obj->type) || obj->type == _HWLOC_OBJ_CACHE_OLD || obj->type == HWLOC_OBJ_MEMCACHE) {
obj->attr->cache.depth = lvalue;
} else if (obj->type == HWLOC_OBJ_GROUP || obj->type == HWLOC_OBJ_BRIDGE) {
/* will be overwritten by the core */
} else if (hwloc__xml_verbose())
fprintf(stderr, "%s: ignoring depth attribute for object type without depth\n",
state->global->msgprefix);
}
else if (!strcmp(name, "kind")) {
unsigned long lvalue = strtoul(value, NULL, 10);
if (obj->type == HWLOC_OBJ_GROUP)
obj->attr->group.kind = lvalue;
else if (hwloc__xml_verbose())
fprintf(stderr, "%s: ignoring kind attribute for non-group object type\n",
state->global->msgprefix);
}
else if (!strcmp(name, "subkind")) {
unsigned long lvalue = strtoul(value, NULL, 10);
if (obj->type == HWLOC_OBJ_GROUP)
obj->attr->group.subkind = lvalue;
else if (hwloc__xml_verbose())
fprintf(stderr, "%s: ignoring subkind attribute for non-group object type\n",
state->global->msgprefix);
}
else if (!strcmp(name, "dont_merge")) {
unsigned long lvalue = strtoul(value, NULL, 10);
if (obj->type == HWLOC_OBJ_GROUP)
obj->attr->group.dont_merge = lvalue;
else if (hwloc__xml_verbose())
fprintf(stderr, "%s: ignoring dont_merge attribute for non-group object type\n",
state->global->msgprefix);
}
else if (!strcmp(name, "pci_busid")) {
switch (obj->type) {
case HWLOC_OBJ_PCI_DEVICE:
case HWLOC_OBJ_BRIDGE: {
unsigned domain, bus, dev, func;
if (sscanf(value, "%04x:%02x:%02x.%01x",
&domain, &bus, &dev, &func) != 4) {
if (hwloc__xml_verbose())
fprintf(stderr, "%s: ignoring invalid pci_busid format string %s\n",
state->global->msgprefix, value);
} else {
obj->attr->pcidev.domain = domain;
obj->attr->pcidev.bus = bus;
obj->attr->pcidev.dev = dev;
obj->attr->pcidev.func = func;
}
break;
}
default:
if (hwloc__xml_verbose())
fprintf(stderr, "%s: ignoring pci_busid attribute for non-PCI object\n",
state->global->msgprefix);
break;
}
}
else if (!strcmp(name, "pci_type")) {
switch (obj->type) {
case HWLOC_OBJ_PCI_DEVICE:
case HWLOC_OBJ_BRIDGE: {
unsigned classid, vendor, device, subvendor, subdevice, revision;
if (sscanf(value, "%04x [%04x:%04x] [%04x:%04x] %02x",
&classid, &vendor, &device, &subvendor, &subdevice, &revision) != 6) {
if (hwloc__xml_verbose())
fprintf(stderr, "%s: ignoring invalid pci_type format string %s\n",
state->global->msgprefix, value);
} else {
obj->attr->pcidev.class_id = classid;
obj->attr->pcidev.vendor_id = vendor;
obj->attr->pcidev.device_id = device;
obj->attr->pcidev.subvendor_id = subvendor;
obj->attr->pcidev.subdevice_id = subdevice;
obj->attr->pcidev.revision = revision;
}
break;
}
default:
if (hwloc__xml_verbose())
fprintf(stderr, "%s: ignoring pci_type attribute for non-PCI object\n",
state->global->msgprefix);
break;
}
}
else if (!strcmp(name, "pci_link_speed")) {
switch (obj->type) {
case HWLOC_OBJ_PCI_DEVICE:
case HWLOC_OBJ_BRIDGE: {
obj->attr->pcidev.linkspeed = (float) atof(value);
break;
}
default:
if (hwloc__xml_verbose())
fprintf(stderr, "%s: ignoring pci_link_speed attribute for non-PCI object\n",
state->global->msgprefix);
break;
}
}
else if (!strcmp(name, "bridge_type")) {
switch (obj->type) {
case HWLOC_OBJ_BRIDGE: {
unsigned upstream_type, downstream_type;
if (sscanf(value, "%u-%u", &upstream_type, &downstream_type) != 2) {
if (hwloc__xml_verbose())
fprintf(stderr, "%s: ignoring invalid bridge_type format string %s\n",
state->global->msgprefix, value);
} else {
obj->attr->bridge.upstream_type = (hwloc_obj_bridge_type_t) upstream_type;
obj->attr->bridge.downstream_type = (hwloc_obj_bridge_type_t) downstream_type;
};
break;
}
default:
if (hwloc__xml_verbose())
fprintf(stderr, "%s: ignoring bridge_type attribute for non-bridge object\n",
state->global->msgprefix);
break;
}
}
else if (!strcmp(name, "bridge_pci")) {
switch (obj->type) {
case HWLOC_OBJ_BRIDGE: {
unsigned domain, secbus, subbus;
if (sscanf(value, "%04x:[%02x-%02x]",
&domain, &secbus, &subbus) != 3) {
if (hwloc__xml_verbose())
fprintf(stderr, "%s: ignoring invalid bridge_pci format string %s\n",
state->global->msgprefix, value);
} else {
obj->attr->bridge.downstream.pci.domain = domain;
obj->attr->bridge.downstream.pci.secondary_bus = secbus;
obj->attr->bridge.downstream.pci.subordinate_bus = subbus;
}
break;
}
default:
if (hwloc__xml_verbose())
fprintf(stderr, "%s: ignoring bridge_pci attribute for non-bridge object\n",
state->global->msgprefix);
break;
}
}
else if (!strcmp(name, "osdev_type")) {
switch (obj->type) {
case HWLOC_OBJ_OS_DEVICE: {
unsigned osdev_type;
if (sscanf(value, "%u", &osdev_type) != 1) {
if (hwloc__xml_verbose())
fprintf(stderr, "%s: ignoring invalid osdev_type format string %s\n",
state->global->msgprefix, value);
} else
obj->attr->osdev.type = (hwloc_obj_osdev_type_t) osdev_type;
break;
}
default:
if (hwloc__xml_verbose())
fprintf(stderr, "%s: ignoring osdev_type attribute for non-osdev object\n",
state->global->msgprefix);
break;
}
}
else if (data->version_major < 2) {
/************************
* deprecated from 1.x
*/
if (!strcmp(name, "os_level")
|| !strcmp(name, "online_cpuset"))
{ /* ignored */ }
/*************************
* deprecated from 1.0
*/
else if (!strcmp(name, "dmi_board_vendor")) {
if (value[0])
hwloc_obj_add_info(obj, "DMIBoardVendor", value);
}
else if (!strcmp(name, "dmi_board_name")) {
if (value[0])
hwloc_obj_add_info(obj, "DMIBoardName", value);
}
else if (data->version_major < 1) {
/*************************
* deprecated from 0.9
*/
if (!strcmp(name, "memory_kB")) {
unsigned long long lvalue = strtoull(value, NULL, 10);
if (obj->type == _HWLOC_OBJ_CACHE_OLD)
obj->attr->cache.size = lvalue << 10;
else if (obj->type == HWLOC_OBJ_NUMANODE)
obj->attr->numanode.local_memory = lvalue << 10;
else if (!obj->parent)
topology->machine_memory.local_memory = lvalue << 10;
else if (hwloc__xml_verbose())
fprintf(stderr, "%s: ignoring memory_kB attribute for non-NUMAnode non-root object\n",
state->global->msgprefix);
}
else if (!strcmp(name, "huge_page_size_kB")) {
unsigned long lvalue = strtoul(value, NULL, 10);
if (obj->type == HWLOC_OBJ_NUMANODE || !obj->parent) {
struct hwloc_numanode_attr_s *memory = obj->type == HWLOC_OBJ_NUMANODE ? &obj->attr->numanode : &topology->machine_memory;
if (!memory->page_types) {
memory->page_types = malloc(sizeof(*memory->page_types));
memory->page_types_len = 1;
}
assert(memory->page_types);
memory->page_types[0].size = lvalue << 10;
} else if (hwloc__xml_verbose()) {
fprintf(stderr, "%s: ignoring huge_page_size_kB attribute for non-NUMAnode non-root object\n",
state->global->msgprefix);
}
}
else if (!strcmp(name, "huge_page_free")) {
unsigned long lvalue = strtoul(value, NULL, 10);
if (obj->type == HWLOC_OBJ_NUMANODE || !obj->parent) {
struct hwloc_numanode_attr_s *memory = obj->type == HWLOC_OBJ_NUMANODE ? &obj->attr->numanode : &topology->machine_memory;
if (!memory->page_types) {
memory->page_types = malloc(sizeof(*memory->page_types));
memory->page_types_len = 1;
}
assert(memory->page_types);
memory->page_types[0].count = lvalue;
} else if (hwloc__xml_verbose()) {
fprintf(stderr, "%s: ignoring huge_page_free attribute for non-NUMAnode non-root object\n",
state->global->msgprefix);
}
}
/* end of deprecated from 0.9 */
else goto unknown;
}
/* end of deprecated from 1.0 */
else goto unknown;
}
else {
unknown:
if (hwloc__xml_verbose())
fprintf(stderr, "%s: ignoring unknown object attribute %s\n",
state->global->msgprefix, name);
}
}
static int
hwloc__xml_import_info(struct hwloc_xml_backend_data_s *data,
hwloc_obj_t obj,
hwloc__xml_import_state_t state)
{
char *infoname = NULL;
char *infovalue = NULL;
while (1) {
char *attrname, *attrvalue;
if (state->global->next_attr(state, &attrname, &attrvalue) < 0)
break;
if (!strcmp(attrname, "name"))
infoname = attrvalue;
else if (!strcmp(attrname, "value"))
infovalue = attrvalue;
else
return -1;
}
if (infoname) {
/* empty strings are ignored by libxml */
if (data->version_major < 2 &&
(!strcmp(infoname, "Type") || !strcmp(infoname, "CoProcType"))) {
/* 1.x stored subtype in Type or CoProcType */
if (infovalue) {
if (obj->subtype)
free(obj->subtype);
obj->subtype = strdup(infovalue);
}
} else {
if (infovalue)
hwloc_obj_add_info(obj, infoname, infovalue);
}
}
return state->global->close_tag(state);
}
static int
hwloc__xml_import_pagetype(hwloc_topology_t topology __hwloc_attribute_unused, struct hwloc_numanode_attr_s *memory,
hwloc__xml_import_state_t state)
{
uint64_t size = 0, count = 0;
while (1) {
char *attrname, *attrvalue;
if (state->global->next_attr(state, &attrname, &attrvalue) < 0)
break;
if (!strcmp(attrname, "size"))
size = strtoull(attrvalue, NULL, 10);
else if (!strcmp(attrname, "count"))
count = strtoull(attrvalue, NULL, 10);
else
return -1;
}
if (size) {
unsigned idx = memory->page_types_len;
struct hwloc_memory_page_type_s *tmp;
tmp = realloc(memory->page_types, (idx+1)*sizeof(*memory->page_types));
if (tmp) { /* if failed to allocate, ignore this page_type entry */
memory->page_types = tmp;
memory->page_types_len = idx+1;
memory->page_types[idx].size = size;
memory->page_types[idx].count = count;
}
}
return state->global->close_tag(state);
}
static int
hwloc__xml_v1import_distances(struct hwloc_xml_backend_data_s *data,
hwloc_obj_t obj,
hwloc__xml_import_state_t state)
{
unsigned long reldepth = 0, nbobjs = 0;
float latbase = 0;
char *tag;
int ret;
while (1) {
char *attrname, *attrvalue;
if (state->global->next_attr(state, &attrname, &attrvalue) < 0)
break;
if (!strcmp(attrname, "nbobjs"))
nbobjs = strtoul(attrvalue, NULL, 10);
else if (!strcmp(attrname, "relative_depth"))
reldepth = strtoul(attrvalue, NULL, 10);
else if (!strcmp(attrname, "latency_base"))
latbase = (float) atof(attrvalue);
else
return -1;
}
if (nbobjs && reldepth && latbase) {
unsigned i;
float *matrix;
struct hwloc__xml_imported_v1distances_s *v1dist;
matrix = malloc(nbobjs*nbobjs*sizeof(float));
v1dist = malloc(sizeof(*v1dist));
if (!matrix || !v1dist) {
if (hwloc__xml_verbose())
fprintf(stderr, "%s: failed to allocate v1distance matrix for %lu objects\n",
state->global->msgprefix, nbobjs);
free(v1dist);
free(matrix);
return -1;
}
v1dist->kind = HWLOC_DISTANCES_KIND_FROM_OS|HWLOC_DISTANCES_KIND_MEANS_LATENCY;
/* TODO: we can't know for sure if it comes from the OS.
* On Linux/x86, it would be 10 on the diagonal.
* On Solaris/T5, 15 on the diagonal.
* Just check whether all values are integers, and that all values on the diagonal are minimal and identical?
*/
v1dist->nbobjs = nbobjs;
v1dist->floats = matrix;
for(i=0; i<nbobjs*nbobjs; i++) {
struct hwloc__xml_import_state_s childstate;
char *attrname, *attrvalue;
float val;
ret = state->global->find_child(state, &childstate, &tag);
if (ret <= 0 || strcmp(tag, "latency")) {
/* a latency child is needed */
free(matrix);
free(v1dist);
return -1;
}
ret = state->global->next_attr(&childstate, &attrname, &attrvalue);
if (ret < 0 || strcmp(attrname, "value")) {
free(matrix);
free(v1dist);
return -1;
}
val = (float) atof((char *) attrvalue);
matrix[i] = val * latbase;
ret = state->global->close_tag(&childstate);
if (ret < 0) {
free(matrix);
free(v1dist);
return -1;
}
state->global->close_child(&childstate);
}
if (nbobjs < 2) {
/* distances with a single object are useless, even if the XML isn't invalid */
assert(nbobjs == 1);
if (hwloc__xml_verbose())
fprintf(stderr, "%s: ignoring invalid distance matrix with only 1 object\n",
state->global->msgprefix);
free(matrix);
free(v1dist);
} else if (obj->parent) {
/* we currently only import distances attached to root.
* we can't save obj in v1dist because obj could be dropped during insert if ignored.
* we could save its complete_cpu/nodeset instead to find it back later.
* but it doesn't matter much since only NUMA distances attached to root matter.
*/
free(matrix);
free(v1dist);
} else {
/* queue the distance for real */
v1dist->prev = data->last_v1dist;
v1dist->next = NULL;
if (data->last_v1dist)
data->last_v1dist->next = v1dist;
else
data->first_v1dist = v1dist;
data->last_v1dist = v1dist;
}
}
return state->global->close_tag(state);
}
static int
hwloc__xml_import_userdata(hwloc_topology_t topology __hwloc_attribute_unused, hwloc_obj_t obj,
hwloc__xml_import_state_t state)
{
size_t length = 0;
int encoded = 0;
char *name = NULL; /* optional */
int ret;
while (1) {
char *attrname, *attrvalue;
if (state->global->next_attr(state, &attrname, &attrvalue) < 0)
break;
if (!strcmp(attrname, "length"))
length = strtoul(attrvalue, NULL, 10);
else if (!strcmp(attrname, "encoding"))
encoded = !strcmp(attrvalue, "base64");
else if (!strcmp(attrname, "name"))
name = attrvalue;
else
return -1;
}
if (!topology->userdata_import_cb) {
char *buffer;
size_t reallength = encoded ? BASE64_ENCODED_LENGTH(length) : length;
ret = state->global->get_content(state, &buffer, reallength);
if (ret < 0)
return -1;
} else if (topology->userdata_not_decoded) {
char *buffer, *fakename;
size_t reallength = encoded ? BASE64_ENCODED_LENGTH(length) : length;
ret = state->global->get_content(state, &buffer, reallength);
if (ret < 0)
return -1;
fakename = malloc(6 + 1 + (name ? strlen(name) : 4) + 1);
if (!fakename)
return -1;
sprintf(fakename, encoded ? "base64%c%s" : "normal%c%s", name ? ':' : '-', name ? name : "anon");
topology->userdata_import_cb(topology, obj, fakename, buffer, length);
free(fakename);
} else if (encoded && length) {
char *encoded_buffer;
size_t encoded_length = BASE64_ENCODED_LENGTH(length);
ret = state->global->get_content(state, &encoded_buffer, encoded_length);
if (ret < 0)
return -1;
if (ret) {
char *decoded_buffer = malloc(length+1);
if (!decoded_buffer)
return -1;
assert(encoded_buffer[encoded_length] == 0);
ret = hwloc_decode_from_base64(encoded_buffer, decoded_buffer, length+1);
if (ret != (int) length) {
free(decoded_buffer);
return -1;
}
topology->userdata_import_cb(topology, obj, name, decoded_buffer, length);
free(decoded_buffer);
}
} else { /* always handle length==0 in the non-encoded case */
char *buffer = (char *) "";
if (length) {
ret = state->global->get_content(state, &buffer, length);
if (ret < 0)
return -1;
}
topology->userdata_import_cb(topology, obj, name, buffer, length);
}
state->global->close_content(state);
return state->global->close_tag(state);
}
static void hwloc__xml_import_report_outoforder(hwloc_topology_t topology, hwloc_obj_t new, hwloc_obj_t old)
{
char *progname = hwloc_progname(topology);
const char *origversion = hwloc_obj_get_info_by_name(topology->levels[0][0], "hwlocVersion");
const char *origprogname = hwloc_obj_get_info_by_name(topology->levels[0][0], "ProcessName");
char *c1, *cc1, t1[64];
char *c2 = NULL, *cc2 = NULL, t2[64];
hwloc_bitmap_asprintf(&c1, new->cpuset);
hwloc_bitmap_asprintf(&cc1, new->complete_cpuset);
hwloc_obj_type_snprintf(t1, sizeof(t1), new, 0);
if (old->cpuset)
hwloc_bitmap_asprintf(&c2, old->cpuset);
if (old->complete_cpuset)
hwloc_bitmap_asprintf(&cc2, old->complete_cpuset);
hwloc_obj_type_snprintf(t2, sizeof(t2), old, 0);
fprintf(stderr, "****************************************************************************\n");
fprintf(stderr, "* hwloc has encountered an out-of-order XML topology load.\n");
fprintf(stderr, "* Object %s cpuset %s complete %s\n",
t1, c1, cc1);
fprintf(stderr, "* was inserted after object %s with %s and %s.\n",
t2, c2 ? c2 : "none", cc2 ? cc2 : "none");
fprintf(stderr, "* The error occured in hwloc %s inside process `%s', while\n",
HWLOC_VERSION,
progname ? progname : "<unknown>");
if (origversion || origprogname)
fprintf(stderr, "* the input XML was generated by hwloc %s inside process `%s'.\n",
origversion ? origversion : "(unknown version)",
origprogname ? origprogname : "<unknown>");
else
fprintf(stderr, "* the input XML was generated by an unspecified ancient hwloc release.\n");
fprintf(stderr, "* Please check that your input topology XML file is valid.\n");
fprintf(stderr, "* Set HWLOC_DEBUG_CHECK=1 in the environment to detect further issues.\n");
fprintf(stderr, "****************************************************************************\n");
free(c1);
free(cc1);
free(c2);
free(cc2);
free(progname);
}
static int
hwloc__xml_import_object(hwloc_topology_t topology,
struct hwloc_xml_backend_data_s *data,
hwloc_obj_t parent, hwloc_obj_t obj, int *gotignored,
hwloc__xml_import_state_t state)
{
int ignored = 0;
int childrengotignored = 0;
int attribute_less_cache = 0;
int numa_was_root = 0;
char *tag;
struct hwloc__xml_import_state_s childstate;
/* set parent now since it's used during import below or in subfunctions */
obj->parent = parent;
/* process attributes */
while (1) {
char *attrname, *attrvalue;
if (state->global->next_attr(state, &attrname, &attrvalue) < 0)
break;
if (!strcmp(attrname, "type")) {
if (hwloc_type_sscanf(attrvalue, &obj->type, NULL, 0) < 0) {
if (!strcasecmp(attrvalue, "Cache")) {
obj->type = _HWLOC_OBJ_CACHE_OLD; /* will be fixed below */
attribute_less_cache = 1;
} else if (!strcasecmp(attrvalue, "System")) {
if (!parent)
obj->type = HWLOC_OBJ_MACHINE;
else {
if (hwloc__xml_verbose())
fprintf(stderr, "%s: obsolete System object only allowed at root\n",
state->global->msgprefix);
goto error_with_object;
}
} else if (!strcasecmp(attrvalue, "Tile")) {
/* deal with possible future type */
obj->type = HWLOC_OBJ_GROUP;
obj->attr->group.kind = HWLOC_GROUP_KIND_INTEL_TILE;
} else if (!strcasecmp(attrvalue, "Module")) {
/* deal with possible future type */
obj->type = HWLOC_OBJ_GROUP;
obj->attr->group.kind = HWLOC_GROUP_KIND_INTEL_MODULE;
} else if (!strcasecmp(attrvalue, "MemCache")) {
/* ignore possible future type */
obj->type = _HWLOC_OBJ_FUTURE;
ignored = 1;
if (hwloc__xml_verbose())
fprintf(stderr, "%s: %s object not-supported, will be ignored\n",
state->global->msgprefix, attrvalue);
} else {
if (hwloc__xml_verbose())
fprintf(stderr, "%s: unrecognized object type string %s\n",
state->global->msgprefix, attrvalue);
goto error_with_object;
}
}
} else {
/* type needed first */
if (obj->type == HWLOC_OBJ_TYPE_NONE) {
if (hwloc__xml_verbose())
fprintf(stderr, "%s: object attribute %s found before type\n",
state->global->msgprefix, attrname);
goto error_with_object;
}
hwloc__xml_import_object_attr(topology, data, obj, attrname, attrvalue, state);
}
}
/* process non-object subnodes to get info attrs (as well as page_types, etc) */
while (1) {
int ret;
tag = NULL;
ret = state->global->find_child(state, &childstate, &tag);
if (ret < 0)
goto error;
if (!ret)
break;
if (!strcmp(tag, "object")) {
/* we'll handle children later */
break;
} else if (!strcmp(tag, "page_type")) {
if (obj->type == HWLOC_OBJ_NUMANODE) {
ret = hwloc__xml_import_pagetype(topology, &obj->attr->numanode, &childstate);
} else if (!parent) {
ret = hwloc__xml_import_pagetype(topology, &topology->machine_memory, &childstate);
} else {
if (hwloc__xml_verbose())
fprintf(stderr, "%s: invalid non-NUMAnode object child %s\n",
state->global->msgprefix, tag);
ret = -1;
}
} else if (!strcmp(tag, "info")) {
ret = hwloc__xml_import_info(data, obj, &childstate);
} else if (data->version_major < 2 && !strcmp(tag, "distances")) {
ret = hwloc__xml_v1import_distances(data, obj, &childstate);
} else if (!strcmp(tag, "userdata")) {
ret = hwloc__xml_import_userdata(topology, obj, &childstate);
} else {
if (hwloc__xml_verbose())
fprintf(stderr, "%s: invalid special object child %s\n",
state->global->msgprefix, tag);
ret = -1;
}
if (ret < 0)
goto error;
state->global->close_child(&childstate);
}
if (parent && obj->type == HWLOC_OBJ_MACHINE) {
/* replace non-root Machine with Groups */
obj->type = HWLOC_OBJ_GROUP;
}
if (parent && data->version_major >= 2) {
/* check parent/child types for 2.x */
if (hwloc__obj_type_is_normal(obj->type)) {
if (!hwloc__obj_type_is_normal(parent->type)) {
if (hwloc__xml_verbose())
fprintf(stderr, "normal object %s cannot be child of non-normal parent %s\n",
hwloc_obj_type_string(obj->type), hwloc_obj_type_string(parent->type));
goto error_with_object;
}
} else if (hwloc__obj_type_is_memory(obj->type)) {
if (hwloc__obj_type_is_io(parent->type) || HWLOC_OBJ_MISC == parent->type) {
if (hwloc__xml_verbose())
fprintf(stderr, "Memory object %s cannot be child of non-normal-or-memory parent %s\n",
hwloc_obj_type_string(obj->type), hwloc_obj_type_string(parent->type));
goto error_with_object;
}
} else if (hwloc__obj_type_is_io(obj->type)) {
if (hwloc__obj_type_is_memory(parent->type) || HWLOC_OBJ_MISC == parent->type) {
if (hwloc__xml_verbose())
fprintf(stderr, "I/O object %s cannot be child of non-normal-or-I/O parent %s\n",
hwloc_obj_type_string(obj->type), hwloc_obj_type_string(parent->type));
goto error_with_object;
}
}
} else if (parent && data->version_major < 2) {
/* check parent/child types for pre-v2.0 */
if (hwloc__obj_type_is_normal(obj->type) || HWLOC_OBJ_NUMANODE == obj->type) {
if (hwloc__obj_type_is_special(parent->type)) {
if (hwloc__xml_verbose())
fprintf(stderr, "v1.x normal v1.x object %s cannot be child of special parent %s\n",
hwloc_obj_type_string(obj->type), hwloc_obj_type_string(parent->type));
goto error_with_object;
}
} else if (hwloc__obj_type_is_io(obj->type)) {
if (HWLOC_OBJ_MISC == parent->type) {
if (hwloc__xml_verbose())
fprintf(stderr, "I/O object %s cannot be child of Misc parent\n",
hwloc_obj_type_string(obj->type));
goto error_with_object;
}
}
}
if (data->version_major < 2) {
/***************************
* 1.x specific checks
*/
/* attach pre-v2.0 children of NUMA nodes to normal parent */
if (parent && parent->type == HWLOC_OBJ_NUMANODE) {
parent = parent->parent;
assert(parent);
}
/* insert a group above pre-v2.0 NUMA nodes if needed */
if (obj->type == HWLOC_OBJ_NUMANODE) {
if (!parent) {
/* crazy case of NUMA node root (only possible when filtering Machine keep_structure in v1.x),
* reinsert a Machine object
*/
hwloc_obj_t machine = hwloc_alloc_setup_object(topology, HWLOC_OBJ_MACHINE, HWLOC_UNKNOWN_INDEX);
machine->cpuset = hwloc_bitmap_dup(obj->cpuset);
machine->complete_cpuset = hwloc_bitmap_dup(obj->cpuset);
machine->nodeset = hwloc_bitmap_dup(obj->nodeset);
machine->complete_nodeset = hwloc_bitmap_dup(obj->complete_nodeset);
topology->levels[0][0] = machine;
parent = machine;
numa_was_root = 1;
} else if (!hwloc_bitmap_isequal(obj->complete_cpuset, parent->complete_cpuset)) {
/* This NUMA node has a different locality from its parent.
* Don't attach it to this parent, or it well get its parent cpusets.
* Add an intermediate Group with the desired locality.
*/
int needgroup = 1;
hwloc_obj_t sibling;
sibling = parent->memory_first_child;
if (sibling && !sibling->subtype
&& !sibling->next_sibling
&& obj->subtype && !strcmp(obj->subtype, "MCDRAM")
&& hwloc_bitmap_iszero(obj->complete_cpuset)) {
/* this is KNL MCDRAM, we want to attach it near its DDR sibling */
needgroup = 0;
}
/* Ideally we would also detect similar cases on future non-KNL platforms with multiple local NUMA nodes.
* That's unlikely to occur with v1.x.
* And we have no way to be sure if this CPU-less node is desired or not.
*/
if (needgroup
&& hwloc_filter_check_keep_object_type(topology, HWLOC_OBJ_GROUP)) {
hwloc_obj_t group = hwloc_alloc_setup_object(topology, HWLOC_OBJ_GROUP, HWLOC_UNKNOWN_INDEX);
group->gp_index = 0; /* will be initialized at the end of the discovery once we know the max */
group->cpuset = hwloc_bitmap_dup(obj->cpuset);
group->complete_cpuset = hwloc_bitmap_dup(obj->cpuset);
group->nodeset = hwloc_bitmap_dup(obj->nodeset);
group->complete_nodeset = hwloc_bitmap_dup(obj->complete_nodeset);
group->attr->group.kind = HWLOC_GROUP_KIND_MEMORY;
hwloc_insert_object_by_parent(topology, parent, group);
parent = group;
}
}
}