-
Notifications
You must be signed in to change notification settings - Fork 165
/
topology.c
5122 lines (4512 loc) · 166 KB
/
topology.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-2021 Inria. All rights reserved.
* Copyright © 2009-2012, 2020 Université Bordeaux
* Copyright © 2009-2011 Cisco Systems, Inc. All rights reserved.
* See COPYING in top-level directory.
*/
#include "private/autogen/config.h"
#define _ATFILE_SOURCE
#include <assert.h>
#include <sys/types.h>
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include <float.h>
#include "hwloc.h"
#include "private/private.h"
#include "private/debug.h"
#include "private/misc.h"
#ifdef HAVE_MACH_MACH_INIT_H
#include <mach/mach_init.h>
#endif
#ifdef HAVE_MACH_INIT_H
#include <mach_init.h>
#endif
#ifdef HAVE_MACH_MACH_HOST_H
#include <mach/mach_host.h>
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_SYS_SYSCTL_H
#include <sys/sysctl.h>
#endif
#ifdef HWLOC_WIN_SYS
#include <windows.h>
#endif
/*
* Define ZES_ENABLE_SYSMAN=1 early so that the LevelZero backend gets Sysman enabled.
* Use the constructor if supported and/or the Windows DllMain callback.
* Do it in the main hwloc library instead of the levelzero component because
* the latter could be loaded later as a plugin.
*
* L0 seems to be using getenv() to check this variable on Windows
* (at least in the Intel Compute-Runtime of March 2021),
* so use putenv() to set the variable.
*
* For the record, Get/SetEnvironmentVariable() is not exactly the same as getenv/putenv():
* - getenv() doesn't see what was set with SetEnvironmentVariable()
* - GetEnvironmentVariable() doesn't see putenv() in cygwin (while it does in MSVC and MinGW).
* Hence, if L0 ever switches from getenv() to GetEnvironmentVariable(),
* it will break in cygwin, we'll have to use both putenv() and SetEnvironmentVariable().
* Hopefully L0 will be provide a way to enable Sysman without env vars before it happens.
*/
#ifdef HWLOC_HAVE_ATTRIBUTE_CONSTRUCTOR
static void hwloc_constructor(void) __attribute__((constructor));
static void hwloc_constructor(void)
{
if (!getenv("ZES_ENABLE_SYSMAN"))
putenv((char *) "ZES_ENABLE_SYSMAN=1");
}
#endif
#ifdef HWLOC_WIN_SYS
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
{
if (fdwReason == DLL_PROCESS_ATTACH) {
if (!getenv("ZES_ENABLE_SYSMAN"))
putenv((char *) "ZES_ENABLE_SYSMAN=1");
}
return TRUE;
}
#endif
unsigned hwloc_get_api_version(void)
{
return HWLOC_API_VERSION;
}
int hwloc_topology_abi_check(hwloc_topology_t topology)
{
return topology->topology_abi != HWLOC_TOPOLOGY_ABI ? -1 : 0;
}
int hwloc_hide_errors(void)
{
static int hide = 1; /* only show critical errors by default. lstopo will show others */
static int checked = 0;
if (!checked) {
const char *envvar = getenv("HWLOC_HIDE_ERRORS");
if (envvar)
hide = atoi(envvar);
checked = 1;
}
return hide;
}
/* format the obj info to print in error messages */
static void
report_insert_error_format_obj(char *buf, size_t buflen, hwloc_obj_t obj)
{
char typestr[64];
char *cpusetstr;
char *nodesetstr = NULL;
hwloc_obj_type_snprintf(typestr, sizeof(typestr), obj, 0);
hwloc_bitmap_asprintf(&cpusetstr, obj->cpuset);
if (obj->nodeset) /* may be missing during insert */
hwloc_bitmap_asprintf(&nodesetstr, obj->nodeset);
if (obj->os_index != HWLOC_UNKNOWN_INDEX)
snprintf(buf, buflen, "%s (P#%u cpuset %s%s%s)",
typestr, obj->os_index, cpusetstr,
nodesetstr ? " nodeset " : "",
nodesetstr ? nodesetstr : "");
else
snprintf(buf, buflen, "%s (cpuset %s%s%s)",
typestr, cpusetstr,
nodesetstr ? " nodeset " : "",
nodesetstr ? nodesetstr : "");
free(cpusetstr);
free(nodesetstr);
}
static void report_insert_error(hwloc_obj_t new, hwloc_obj_t old, const char *msg, const char *reason)
{
static int reported = 0;
if (reason && !reported && hwloc_hide_errors() < 2) {
char newstr[512];
char oldstr[512];
report_insert_error_format_obj(newstr, sizeof(newstr), new);
report_insert_error_format_obj(oldstr, sizeof(oldstr), old);
fprintf(stderr, "****************************************************************************\n");
fprintf(stderr, "* hwloc %s received invalid information from the operating system.\n", HWLOC_VERSION);
fprintf(stderr, "*\n");
fprintf(stderr, "* Failed with: %s\n", msg);
fprintf(stderr, "* while inserting %s at %s\n", newstr, oldstr);
fprintf(stderr, "* coming from: %s\n", reason);
fprintf(stderr, "*\n");
fprintf(stderr, "* The following FAQ entry in the hwloc documentation may help:\n");
fprintf(stderr, "* What should I do when hwloc reports \"operating system\" warnings?\n");
fprintf(stderr, "* Otherwise please report this error message to the hwloc user's mailing list,\n");
#ifdef HWLOC_LINUX_SYS
fprintf(stderr, "* along with the files generated by the hwloc-gather-topology script.\n");
#else
fprintf(stderr, "* along with any relevant topology information from your platform.\n");
#endif
fprintf(stderr, "* \n");
fprintf(stderr, "* hwloc will now ignore this invalid topology information and continue.\n");
fprintf(stderr, "****************************************************************************\n");
reported = 1;
}
}
#if defined(HAVE_SYSCTLBYNAME)
int hwloc_get_sysctlbyname(const char *name, int64_t *ret)
{
union {
int32_t i32;
int64_t i64;
} n;
size_t size = sizeof(n);
if (sysctlbyname(name, &n, &size, NULL, 0))
return -1;
switch (size) {
case sizeof(n.i32):
*ret = n.i32;
break;
case sizeof(n.i64):
*ret = n.i64;
break;
default:
return -1;
}
return 0;
}
#endif
#if defined(HAVE_SYSCTL)
int hwloc_get_sysctl(int name[], unsigned namelen, int64_t *ret)
{
union {
int32_t i32;
int64_t i64;
} n;
size_t size = sizeof(n);
if (sysctl(name, namelen, &n, &size, NULL, 0))
return -1;
switch (size) {
case sizeof(n.i32):
*ret = n.i32;
break;
case sizeof(n.i64):
*ret = n.i64;
break;
default:
return -1;
}
return 0;
}
#endif
/* Return the OS-provided number of processors.
* Assumes topology->is_thissystem is true.
*/
#ifndef HWLOC_WIN_SYS /* The windows implementation is in topology-windows.c */
int
hwloc_fallback_nbprocessors(unsigned flags) {
int n;
if (flags & HWLOC_FALLBACK_NBPROCESSORS_INCLUDE_OFFLINE) {
/* try to get all CPUs for Linux and Solaris that can handle offline CPUs */
#if HAVE_DECL__SC_NPROCESSORS_CONF
n = sysconf(_SC_NPROCESSORS_CONF);
#elif HAVE_DECL__SC_NPROC_CONF
n = sysconf(_SC_NPROC_CONF);
#else
n = -1;
#endif
if (n != -1)
return n;
}
/* try getting only online CPUs, or whatever we can get */
#if HAVE_DECL__SC_NPROCESSORS_ONLN
n = sysconf(_SC_NPROCESSORS_ONLN);
#elif HAVE_DECL__SC_NPROC_ONLN
n = sysconf(_SC_NPROC_ONLN);
#elif HAVE_DECL__SC_NPROCESSORS_CONF
n = sysconf(_SC_NPROCESSORS_CONF);
#elif HAVE_DECL__SC_NPROC_CONF
n = sysconf(_SC_NPROC_CONF);
#elif defined(HAVE_HOST_INFO) && HAVE_HOST_INFO
struct host_basic_info info;
mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
host_info(mach_host_self(), HOST_BASIC_INFO, (integer_t*) &info, &count);
n = info.avail_cpus;
#elif defined(HAVE_SYSCTLBYNAME)
int64_t nn;
if (hwloc_get_sysctlbyname("hw.ncpu", &nn))
nn = -1;
n = nn;
#elif defined(HAVE_SYSCTL) && HAVE_DECL_CTL_HW && HAVE_DECL_HW_NCPU
static int name[2] = {CTL_HW, HW_NCPU};
int64_t nn;
if (hwloc_get_sysctl(name, sizeof(name)/sizeof(*name), &nn))
n = -1;
n = nn;
#else
#ifdef __GNUC__
#warning No known way to discover number of available processors on this system
#endif
n = -1;
#endif
return n;
}
int64_t
hwloc_fallback_memsize(void) {
int64_t size;
#if defined(HAVE_HOST_INFO) && HAVE_HOST_INFO
struct host_basic_info info;
mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
host_info(mach_host_self(), HOST_BASIC_INFO, (integer_t*) &info, &count);
size = info.memory_size;
#elif defined(HAVE_SYSCTL) && HAVE_DECL_CTL_HW && (HAVE_DECL_HW_REALMEM64 || HAVE_DECL_HW_MEMSIZE64 || HAVE_DECL_HW_PHYSMEM64 || HAVE_DECL_HW_USERMEM64 || HAVE_DECL_HW_REALMEM || HAVE_DECL_HW_MEMSIZE || HAVE_DECL_HW_PHYSMEM || HAVE_DECL_HW_USERMEM)
#if HAVE_DECL_HW_MEMSIZE64
static int name[2] = {CTL_HW, HW_MEMSIZE64};
#elif HAVE_DECL_HW_REALMEM64
static int name[2] = {CTL_HW, HW_REALMEM64};
#elif HAVE_DECL_HW_PHYSMEM64
static int name[2] = {CTL_HW, HW_PHYSMEM64};
#elif HAVE_DECL_HW_USERMEM64
static int name[2] = {CTL_HW, HW_USERMEM64};
#elif HAVE_DECL_HW_MEMSIZE
static int name[2] = {CTL_HW, HW_MEMSIZE};
#elif HAVE_DECL_HW_REALMEM
static int name[2] = {CTL_HW, HW_REALMEM};
#elif HAVE_DECL_HW_PHYSMEM
static int name[2] = {CTL_HW, HW_PHYSMEM};
#elif HAVE_DECL_HW_USERMEM
static int name[2] = {CTL_HW, HW_USERMEM};
#endif
if (hwloc_get_sysctl(name, sizeof(name)/sizeof(*name), &size))
size = -1;
#elif defined(HAVE_SYSCTLBYNAME)
if (hwloc_get_sysctlbyname("hw.memsize", &size) &&
hwloc_get_sysctlbyname("hw.realmem", &size) &&
hwloc_get_sysctlbyname("hw.physmem", &size) &&
hwloc_get_sysctlbyname("hw.usermem", &size))
size = -1;
#else
size = -1;
#endif
return size;
}
#endif /* !HWLOC_WIN_SYS */
/*
* Use the given number of processors to set a PU level.
*/
void
hwloc_setup_pu_level(struct hwloc_topology *topology,
unsigned nb_pus)
{
struct hwloc_obj *obj;
unsigned oscpu,cpu;
hwloc_debug("%s", "\n\n * CPU cpusets *\n\n");
for (cpu=0,oscpu=0; cpu<nb_pus; oscpu++)
{
obj = hwloc_alloc_setup_object(topology, HWLOC_OBJ_PU, oscpu);
obj->cpuset = hwloc_bitmap_alloc();
hwloc_bitmap_only(obj->cpuset, oscpu);
hwloc_debug_2args_bitmap("cpu %u (os %u) has cpuset %s\n",
cpu, oscpu, obj->cpuset);
hwloc__insert_object_by_cpuset(topology, NULL, obj, "core:pulevel");
cpu++;
}
}
/* Traverse children of a parent in a safe way: reread the next pointer as
* appropriate to prevent crash on child deletion: */
#define for_each_child_safe(child, parent, pchild) \
for (pchild = &(parent)->first_child, child = *pchild; \
child; \
/* Check whether the current child was not dropped. */ \
(*pchild == child ? pchild = &(child->next_sibling) : NULL), \
/* Get pointer to next child. */ \
child = *pchild)
#define for_each_memory_child_safe(child, parent, pchild) \
for (pchild = &(parent)->memory_first_child, child = *pchild; \
child; \
/* Check whether the current child was not dropped. */ \
(*pchild == child ? pchild = &(child->next_sibling) : NULL), \
/* Get pointer to next child. */ \
child = *pchild)
#define for_each_io_child_safe(child, parent, pchild) \
for (pchild = &(parent)->io_first_child, child = *pchild; \
child; \
/* Check whether the current child was not dropped. */ \
(*pchild == child ? pchild = &(child->next_sibling) : NULL), \
/* Get pointer to next child. */ \
child = *pchild)
#define for_each_misc_child_safe(child, parent, pchild) \
for (pchild = &(parent)->misc_first_child, child = *pchild; \
child; \
/* Check whether the current child was not dropped. */ \
(*pchild == child ? pchild = &(child->next_sibling) : NULL), \
/* Get pointer to next child. */ \
child = *pchild)
#ifdef HWLOC_DEBUG
/* Just for debugging. */
static void
hwloc_debug_print_object(int indent __hwloc_attribute_unused, hwloc_obj_t obj)
{
char type[64], idx[12], attr[1024], *cpuset = NULL;
hwloc_debug("%*s", 2*indent, "");
hwloc_obj_type_snprintf(type, sizeof(type), obj, 1);
if (obj->os_index != HWLOC_UNKNOWN_INDEX)
snprintf(idx, sizeof(idx), "#%u", obj->os_index);
else
*idx = '\0';
hwloc_obj_attr_snprintf(attr, sizeof(attr), obj, " ", 1);
hwloc_debug("%s%s%s%s%s", type, idx, *attr ? "(" : "", attr, *attr ? ")" : "");
if (obj->name)
hwloc_debug(" name \"%s\"", obj->name);
if (obj->subtype)
hwloc_debug(" subtype \"%s\"", obj->subtype);
if (obj->cpuset) {
hwloc_bitmap_asprintf(&cpuset, obj->cpuset);
hwloc_debug(" cpuset %s", cpuset);
free(cpuset);
}
if (obj->complete_cpuset) {
hwloc_bitmap_asprintf(&cpuset, obj->complete_cpuset);
hwloc_debug(" complete %s", cpuset);
free(cpuset);
}
if (obj->nodeset) {
hwloc_bitmap_asprintf(&cpuset, obj->nodeset);
hwloc_debug(" nodeset %s", cpuset);
free(cpuset);
}
if (obj->complete_nodeset) {
hwloc_bitmap_asprintf(&cpuset, obj->complete_nodeset);
hwloc_debug(" completeN %s", cpuset);
free(cpuset);
}
if (obj->arity)
hwloc_debug(" arity %u", obj->arity);
hwloc_debug("%s", "\n");
}
static void
hwloc_debug_print_objects(int indent __hwloc_attribute_unused, hwloc_obj_t obj)
{
if (hwloc_debug_enabled() >= 2) {
hwloc_obj_t child;
hwloc_debug_print_object(indent, obj);
for_each_child (child, obj)
hwloc_debug_print_objects(indent + 1, child);
for_each_memory_child (child, obj)
hwloc_debug_print_objects(indent + 1, child);
for_each_io_child (child, obj)
hwloc_debug_print_objects(indent + 1, child);
for_each_misc_child (child, obj)
hwloc_debug_print_objects(indent + 1, child);
}
}
#else /* !HWLOC_DEBUG */
#define hwloc_debug_print_object(indent, obj) do { /* nothing */ } while (0)
#define hwloc_debug_print_objects(indent, obj) do { /* nothing */ } while (0)
#endif /* !HWLOC_DEBUG */
void hwloc__free_infos(struct hwloc_info_s *infos, unsigned count)
{
unsigned i;
for(i=0; i<count; i++) {
free(infos[i].name);
free(infos[i].value);
}
free(infos);
}
int hwloc__add_info(struct hwloc_info_s **infosp, unsigned *countp, const char *name, const char *value)
{
unsigned count = *countp;
struct hwloc_info_s *infos = *infosp;
#define OBJECT_INFO_ALLOC 8
/* nothing allocated initially, (re-)allocate by multiple of 8 */
unsigned alloccount = (count + 1 + (OBJECT_INFO_ALLOC-1)) & ~(OBJECT_INFO_ALLOC-1);
if (count != alloccount) {
struct hwloc_info_s *tmpinfos = realloc(infos, alloccount*sizeof(*infos));
if (!tmpinfos)
/* failed to allocate, ignore this info */
goto out_with_array;
*infosp = infos = tmpinfos;
}
infos[count].name = strdup(name);
if (!infos[count].name)
goto out_with_array;
infos[count].value = strdup(value);
if (!infos[count].value)
goto out_with_name;
*countp = count+1;
return 0;
out_with_name:
free(infos[count].name);
out_with_array:
/* don't bother reducing the array */
return -1;
}
int hwloc__add_info_nodup(struct hwloc_info_s **infosp, unsigned *countp,
const char *name, const char *value,
int replace)
{
struct hwloc_info_s *infos = *infosp;
unsigned count = *countp;
unsigned i;
for(i=0; i<count; i++) {
if (!strcmp(infos[i].name, name)) {
if (replace) {
char *new = strdup(value);
if (!new)
return -1;
free(infos[i].value);
infos[i].value = new;
}
return 0;
}
}
return hwloc__add_info(infosp, countp, name, value);
}
int hwloc__move_infos(struct hwloc_info_s **dst_infosp, unsigned *dst_countp,
struct hwloc_info_s **src_infosp, unsigned *src_countp)
{
unsigned dst_count = *dst_countp;
struct hwloc_info_s *dst_infos = *dst_infosp;
unsigned src_count = *src_countp;
struct hwloc_info_s *src_infos = *src_infosp;
unsigned i;
#define OBJECT_INFO_ALLOC 8
/* nothing allocated initially, (re-)allocate by multiple of 8 */
unsigned alloccount = (dst_count + src_count + (OBJECT_INFO_ALLOC-1)) & ~(OBJECT_INFO_ALLOC-1);
if (dst_count != alloccount) {
struct hwloc_info_s *tmp_infos = realloc(dst_infos, alloccount*sizeof(*dst_infos));
if (!tmp_infos)
/* Failed to realloc, ignore the appended infos */
goto drop;
dst_infos = tmp_infos;
}
for(i=0; i<src_count; i++, dst_count++) {
dst_infos[dst_count].name = src_infos[i].name;
dst_infos[dst_count].value = src_infos[i].value;
}
*dst_infosp = dst_infos;
*dst_countp = dst_count;
free(src_infos);
*src_infosp = NULL;
*src_countp = 0;
return 0;
drop:
/* drop src infos, don't modify dst_infos at all */
for(i=0; i<src_count; i++) {
free(src_infos[i].name);
free(src_infos[i].value);
}
free(src_infos);
*src_infosp = NULL;
*src_countp = 0;
return -1;
}
int hwloc_obj_add_info(hwloc_obj_t obj, const char *name, const char *value)
{
return hwloc__add_info(&obj->infos, &obj->infos_count, name, value);
}
/* This function may be called with topology->tma set, it cannot free() or realloc() */
int hwloc__tma_dup_infos(struct hwloc_tma *tma,
struct hwloc_info_s **newip, unsigned *newcp,
struct hwloc_info_s *oldi, unsigned oldc)
{
struct hwloc_info_s *newi;
unsigned i, j;
newi = hwloc_tma_calloc(tma, oldc * sizeof(*newi));
if (!newi)
return -1;
for(i=0; i<oldc; i++) {
newi[i].name = hwloc_tma_strdup(tma, oldi[i].name);
newi[i].value = hwloc_tma_strdup(tma, oldi[i].value);
if (!newi[i].name || !newi[i].value)
goto failed;
}
*newip = newi;
*newcp = oldc;
return 0;
failed:
assert(!tma || !tma->dontfree); /* this tma cannot fail to allocate */
for(j=0; j<=i; j++) {
free(newi[i].name);
free(newi[i].value);
}
free(newi);
*newip = NULL;
return -1;
}
static void
hwloc__free_object_contents(hwloc_obj_t obj)
{
switch (obj->type) {
case HWLOC_OBJ_NUMANODE:
free(obj->attr->numanode.page_types);
break;
default:
break;
}
hwloc__free_infos(obj->infos, obj->infos_count);
free(obj->attr);
free(obj->children);
free(obj->subtype);
free(obj->name);
hwloc_bitmap_free(obj->cpuset);
hwloc_bitmap_free(obj->complete_cpuset);
hwloc_bitmap_free(obj->nodeset);
hwloc_bitmap_free(obj->complete_nodeset);
}
/* Free an object and all its content. */
void
hwloc_free_unlinked_object(hwloc_obj_t obj)
{
hwloc__free_object_contents(obj);
free(obj);
}
/* Replace old with contents of new object, and make new freeable by the caller.
* Requires reconnect (for siblings pointers and group depth),
* fixup of sets (only the main cpuset was likely compared before merging),
* and update of total_memory and group depth.
*/
static void
hwloc_replace_linked_object(hwloc_obj_t old, hwloc_obj_t new)
{
/* drop old fields */
hwloc__free_object_contents(old);
/* copy old tree pointers to new */
new->parent = old->parent;
new->next_sibling = old->next_sibling;
new->first_child = old->first_child;
new->memory_first_child = old->memory_first_child;
new->io_first_child = old->io_first_child;
new->misc_first_child = old->misc_first_child;
/* copy new contents to old now that tree pointers are OK */
memcpy(old, new, sizeof(*old));
/* clear new to that we may free it */
memset(new, 0,sizeof(*new));
}
/* Remove an object and its children from its parent and free them.
* Only updates next_sibling/first_child pointers,
* so may only be used during early discovery or during destroy.
*/
static void
unlink_and_free_object_and_children(hwloc_obj_t *pobj)
{
hwloc_obj_t obj = *pobj, child, *pchild;
for_each_child_safe(child, obj, pchild)
unlink_and_free_object_and_children(pchild);
for_each_memory_child_safe(child, obj, pchild)
unlink_and_free_object_and_children(pchild);
for_each_io_child_safe(child, obj, pchild)
unlink_and_free_object_and_children(pchild);
for_each_misc_child_safe(child, obj, pchild)
unlink_and_free_object_and_children(pchild);
*pobj = obj->next_sibling;
hwloc_free_unlinked_object(obj);
}
/* Free an object and its children without unlinking from parent.
*/
void
hwloc_free_object_and_children(hwloc_obj_t obj)
{
unlink_and_free_object_and_children(&obj);
}
/* Free an object, its next siblings and their children without unlinking from parent.
*/
void
hwloc_free_object_siblings_and_children(hwloc_obj_t obj)
{
while (obj)
unlink_and_free_object_and_children(&obj);
}
/* insert the (non-empty) list of sibling starting at firstnew as new children of newparent,
* and return the address of the pointer to the next one
*/
static hwloc_obj_t *
insert_siblings_list(hwloc_obj_t *firstp, hwloc_obj_t firstnew, hwloc_obj_t newparent)
{
hwloc_obj_t tmp;
assert(firstnew);
*firstp = tmp = firstnew;
tmp->parent = newparent;
while (tmp->next_sibling) {
tmp = tmp->next_sibling;
tmp->parent = newparent;
}
return &tmp->next_sibling;
}
/* Take the new list starting at firstnew and prepend it to the old list starting at *firstp,
* and mark the new children as children of newparent.
* May be used during early or late discovery (updates prev_sibling and sibling_rank).
* List firstnew must be non-NULL.
*/
static void
prepend_siblings_list(hwloc_obj_t *firstp, hwloc_obj_t firstnew, hwloc_obj_t newparent)
{
hwloc_obj_t *tmpp, tmp, last;
unsigned length;
/* update parent pointers and find the length and end of the new list */
for(length = 0, tmpp = &firstnew, last = NULL ; *tmpp; length++, last = *tmpp, tmpp = &((*tmpp)->next_sibling))
(*tmpp)->parent = newparent;
/* update sibling_rank */
for(tmp = *firstp; tmp; tmp = tmp->next_sibling)
tmp->sibling_rank += length; /* if it wasn't initialized yet, it'll be overwritten later */
/* place the existing list at the end of the new one */
*tmpp = *firstp;
if (*firstp)
(*firstp)->prev_sibling = last;
/* use the beginning of the new list now */
*firstp = firstnew;
}
/* Take the new list starting at firstnew and append it to the old list starting at *firstp,
* and mark the new children as children of newparent.
* May be used during early or late discovery (updates prev_sibling and sibling_rank).
*/
static void
append_siblings_list(hwloc_obj_t *firstp, hwloc_obj_t firstnew, hwloc_obj_t newparent)
{
hwloc_obj_t *tmpp, tmp, last;
unsigned length;
/* find the length and end of the existing list */
for(length = 0, tmpp = firstp, last = NULL ; *tmpp; length++, last = *tmpp, tmpp = &((*tmpp)->next_sibling));
/* update parent pointers and sibling_rank */
for(tmp = firstnew; tmp; tmp = tmp->next_sibling) {
tmp->parent = newparent;
tmp->sibling_rank += length; /* if it wasn't set yet, it'll be overwritten later */
}
/* place new list at the end of the old one */
*tmpp = firstnew;
if (firstnew)
firstnew->prev_sibling = last;
}
/* Remove an object from its parent and free it.
* Only updates next_sibling/first_child pointers,
* so may only be used during early discovery.
*
* Children are inserted in the parent.
* If children should be inserted somewhere else (e.g. when merging with a child),
* the caller should move them before calling this function.
*/
static void
unlink_and_free_single_object(hwloc_obj_t *pparent)
{
hwloc_obj_t old = *pparent;
hwloc_obj_t *lastp;
if (old->type == HWLOC_OBJ_MISC) {
/* Misc object */
/* no normal children */
assert(!old->first_child);
/* no memory children */
assert(!old->memory_first_child);
/* no I/O children */
assert(!old->io_first_child);
if (old->misc_first_child)
/* insert old misc object children as new siblings below parent instead of old */
lastp = insert_siblings_list(pparent, old->misc_first_child, old->parent);
else
lastp = pparent;
/* append old siblings back */
*lastp = old->next_sibling;
} else if (hwloc__obj_type_is_io(old->type)) {
/* I/O object */
/* no normal children */
assert(!old->first_child);
/* no memory children */
assert(!old->memory_first_child);
if (old->io_first_child)
/* insert old I/O object children as new siblings below parent instead of old */
lastp = insert_siblings_list(pparent, old->io_first_child, old->parent);
else
lastp = pparent;
/* append old siblings back */
*lastp = old->next_sibling;
/* append old Misc children to parent */
if (old->misc_first_child)
append_siblings_list(&old->parent->misc_first_child, old->misc_first_child, old->parent);
} else if (hwloc__obj_type_is_memory(old->type)) {
/* memory object */
/* no normal children */
assert(!old->first_child);
/* no I/O children */
assert(!old->io_first_child);
if (old->memory_first_child)
/* insert old memory object children as new siblings below parent instead of old */
lastp = insert_siblings_list(pparent, old->memory_first_child, old->parent);
else
lastp = pparent;
/* append old siblings back */
*lastp = old->next_sibling;
/* append old Misc children to parent */
if (old->misc_first_child)
append_siblings_list(&old->parent->misc_first_child, old->misc_first_child, old->parent);
} else {
/* Normal object */
if (old->first_child)
/* insert old object children as new siblings below parent instead of old */
lastp = insert_siblings_list(pparent, old->first_child, old->parent);
else
lastp = pparent;
/* append old siblings back */
*lastp = old->next_sibling;
/* append old memory, I/O and Misc children to parent
* old->parent cannot be NULL (removing root), misc children should have been moved by the caller earlier.
*/
if (old->memory_first_child)
append_siblings_list(&old->parent->memory_first_child, old->memory_first_child, old->parent);
if (old->io_first_child)
append_siblings_list(&old->parent->io_first_child, old->io_first_child, old->parent);
if (old->misc_first_child)
append_siblings_list(&old->parent->misc_first_child, old->misc_first_child, old->parent);
}
hwloc_free_unlinked_object(old);
}
/* This function may use a tma, it cannot free() or realloc() */
static int
hwloc__duplicate_object(struct hwloc_topology *newtopology,
struct hwloc_obj *newparent,
struct hwloc_obj *newobj,
struct hwloc_obj *src)
{
struct hwloc_tma *tma = newtopology->tma;
hwloc_obj_t *level;
unsigned level_width;
size_t len;
unsigned i;
hwloc_obj_t child, prev;
int err = 0;
/* either we're duplicating to an already allocated new root, which has no newparent,
* or we're duplicating to a non-yet allocated new non-root, which will have a newparent.
*/
assert(!newparent == !!newobj);
if (!newobj) {
newobj = hwloc_alloc_setup_object(newtopology, src->type, src->os_index);
if (!newobj)
return -1;
}
/* duplicate all non-object-pointer fields */
newobj->logical_index = src->logical_index;
newobj->depth = src->depth;
newobj->sibling_rank = src->sibling_rank;
newobj->type = src->type;
newobj->os_index = src->os_index;
newobj->gp_index = src->gp_index;
newobj->symmetric_subtree = src->symmetric_subtree;
if (src->name)
newobj->name = hwloc_tma_strdup(tma, src->name);
if (src->subtype)
newobj->subtype = hwloc_tma_strdup(tma, src->subtype);
newobj->userdata = src->userdata;
newobj->total_memory = src->total_memory;
memcpy(newobj->attr, src->attr, sizeof(*newobj->attr));
if (src->type == HWLOC_OBJ_NUMANODE && src->attr->numanode.page_types_len) {
len = src->attr->numanode.page_types_len * sizeof(struct hwloc_memory_page_type_s);
newobj->attr->numanode.page_types = hwloc_tma_malloc(tma, len);
memcpy(newobj->attr->numanode.page_types, src->attr->numanode.page_types, len);
}
newobj->cpuset = hwloc_bitmap_tma_dup(tma, src->cpuset);
newobj->complete_cpuset = hwloc_bitmap_tma_dup(tma, src->complete_cpuset);
newobj->nodeset = hwloc_bitmap_tma_dup(tma, src->nodeset);
newobj->complete_nodeset = hwloc_bitmap_tma_dup(tma, src->complete_nodeset);
hwloc__tma_dup_infos(tma, &newobj->infos, &newobj->infos_count, src->infos, src->infos_count);
/* find our level */
if (src->depth < 0) {
i = HWLOC_SLEVEL_FROM_DEPTH(src->depth);
level = newtopology->slevels[i].objs;
level_width = newtopology->slevels[i].nbobjs;
/* deal with first/last pointers of special levels, even if not really needed */
if (!newobj->logical_index)
newtopology->slevels[i].first = newobj;
if (newobj->logical_index == newtopology->slevels[i].nbobjs - 1)
newtopology->slevels[i].last = newobj;
} else {
level = newtopology->levels[src->depth];
level_width = newtopology->level_nbobjects[src->depth];
}
/* place us for real */
assert(newobj->logical_index < level_width);
level[newobj->logical_index] = newobj;
/* link to already-inserted cousins */
if (newobj->logical_index > 0 && level[newobj->logical_index-1]) {
newobj->prev_cousin = level[newobj->logical_index-1];
level[newobj->logical_index-1]->next_cousin = newobj;
}
if (newobj->logical_index < level_width-1 && level[newobj->logical_index+1]) {
newobj->next_cousin = level[newobj->logical_index+1];
level[newobj->logical_index+1]->prev_cousin = newobj;
}
/* prepare for children */
if (src->arity) {
newobj->children = hwloc_tma_malloc(tma, src->arity * sizeof(*newobj->children));
if (!newobj->children)
return -1;
}
newobj->arity = src->arity;
newobj->memory_arity = src->memory_arity;
newobj->io_arity = src->io_arity;
newobj->misc_arity = src->misc_arity;
/* actually insert children now */
for_each_child(child, src) {
err = hwloc__duplicate_object(newtopology, newobj, NULL, child);
if (err < 0)
goto out_with_children;
}
for_each_memory_child(child, src) {
err = hwloc__duplicate_object(newtopology, newobj, NULL, child);
if (err < 0)
return err;
}
for_each_io_child(child, src) {
err = hwloc__duplicate_object(newtopology, newobj, NULL, child);
if (err < 0)
goto out_with_children;
}
for_each_misc_child(child, src) {
err = hwloc__duplicate_object(newtopology, newobj, NULL, child);
if (err < 0)
goto out_with_children;
}
out_with_children:
/* link children if all of them where inserted */
if (!err) {
/* only next_sibling is set by insert_by_parent().
* sibling_rank was set above.
*/
if (newobj->arity) {
newobj->children[0]->prev_sibling = NULL;
for(i=1; i<newobj->arity; i++)
newobj->children[i]->prev_sibling = newobj->children[i-1];
newobj->last_child = newobj->children[newobj->arity-1];
}
if (newobj->memory_arity) {
child = newobj->memory_first_child;
prev = NULL;
while (child) {
child->prev_sibling = prev;
prev = child;
child = child->next_sibling;
}
}
if (newobj->io_arity) {
child = newobj->io_first_child;
prev = NULL;
while (child) {
child->prev_sibling = prev;
prev = child;
child = child->next_sibling;
}
}
if (newobj->misc_arity) {
child = newobj->misc_first_child;
prev = NULL;
while (child) {
child->prev_sibling = prev;
prev = child;
child = child->next_sibling;
}
}
}
/* some children insertion may have failed, but some children may have been inserted below us already.
* keep inserting ourself and let the caller clean the entire tree if we return an error.
*/
if (newparent) {
/* no need to check the children insert order here, the source topology