forked from open-mpi/hwloc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
topology-linux.c
7825 lines (6905 loc) · 249 KB
/
topology-linux.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-2023 Inria. All rights reserved.
* Copyright © 2009-2013, 2015, 2020 Université Bordeaux
* Copyright © 2009-2018 Cisco Systems, Inc. All rights reserved.
* Copyright © 2015 Intel, Inc. All rights reserved.
* Copyright © 2010 IBM
* See COPYING in top-level directory.
*/
#include "private/autogen/config.h"
#include "hwloc.h"
#include "hwloc/linux.h"
#include "private/misc.h"
#include "private/private.h"
#include "private/misc.h"
#include "private/debug.h"
#include <limits.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HWLOC_HAVE_LIBUDEV
#include <libudev.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <sched.h>
#include <pthread.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <mntent.h>
#include <stddef.h>
#include <endian.h>
struct hwloc_linux_backend_data_s {
char *root_path; /* NULL if unused */
int root_fd; /* The file descriptor for the file system root, used when browsing, e.g., Linux' sysfs and procfs. */
int is_real_fsroot; /* Boolean saying whether root_fd points to the real filesystem root of the system */
#ifdef HWLOC_HAVE_LIBUDEV
struct udev *udev; /* Global udev context */
#endif
const char *dumped_hwdata_dirname;
enum {
HWLOC_LINUX_ARCH_X86, /* x86 32 or 64bits, including k1om (KNC) */
HWLOC_LINUX_ARCH_IA64,
HWLOC_LINUX_ARCH_ARM,
HWLOC_LINUX_ARCH_POWER,
HWLOC_LINUX_ARCH_S390,
HWLOC_LINUX_ARCH_LOONGARCH,
HWLOC_LINUX_ARCH_UNKNOWN
} arch;
int is_knl;
int is_amd_with_CU;
int is_fake_numa_uniform; /* 0 if not fake, -1 if fake non-uniform, N if fake=<N>U */
int use_numa_distances;
int use_numa_distances_for_cpuless;
int use_numa_initiators;
struct utsname utsname; /* fields contain \0 when unknown */
int fallback_nbprocessors; /* only used in hwloc_linux_fallback_pu_level(), maybe be <= 0 (error) earlier */
unsigned pagesize;
int need_global_infos;
};
/***************************
* Misc Abstraction layers *
***************************/
#if !(defined HWLOC_HAVE_SCHED_SETAFFINITY) && (defined HWLOC_HAVE_SYSCALL)
/* libc doesn't have support for sched_setaffinity, make system call
* ourselves: */
# ifndef __NR_sched_setaffinity
# ifdef __i386__
# define __NR_sched_setaffinity 241
# elif defined(__x86_64__)
# define __NR_sched_setaffinity 203
# elif defined(__ia64__)
# define __NR_sched_setaffinity 1231
# elif defined(__hppa__)
# define __NR_sched_setaffinity 211
# elif defined(__alpha__)
# define __NR_sched_setaffinity 395
# elif defined(__s390__)
# define __NR_sched_setaffinity 239
# elif defined(__sparc__)
# define __NR_sched_setaffinity 261
# elif defined(__m68k__)
# define __NR_sched_setaffinity 311
# elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) || defined(__powerpc64__) || defined(__ppc64__)
# define __NR_sched_setaffinity 222
# elif defined(__aarch64__)
# define __NR_sched_setaffinity 122
# elif defined(__riscv)
# define __NR_sched_setaffinity 122
# elif defined(__arm__)
# define __NR_sched_setaffinity 241
# elif defined(__cris__)
# define __NR_sched_setaffinity 241
/*# elif defined(__mips__)
# define __NR_sched_setaffinity TODO (32/64/nabi) */
# else
# warning "don't know the syscall number for sched_setaffinity on this architecture, will not support binding"
# define sched_setaffinity(pid, lg, mask) (errno = ENOSYS, -1)
# endif
# endif
# ifndef sched_setaffinity
# define sched_setaffinity(pid, lg, mask) syscall(__NR_sched_setaffinity, pid, lg, mask)
# endif
# ifndef __NR_sched_getaffinity
# ifdef __i386__
# define __NR_sched_getaffinity 242
# elif defined(__x86_64__)
# define __NR_sched_getaffinity 204
# elif defined(__ia64__)
# define __NR_sched_getaffinity 1232
# elif defined(__hppa__)
# define __NR_sched_getaffinity 212
# elif defined(__alpha__)
# define __NR_sched_getaffinity 396
# elif defined(__s390__)
# define __NR_sched_getaffinity 240
# elif defined(__sparc__)
# define __NR_sched_getaffinity 260
# elif defined(__m68k__)
# define __NR_sched_getaffinity 312
# elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) || defined(__powerpc64__) || defined(__ppc64__)
# define __NR_sched_getaffinity 223
# elif defined(__aarch64__)
# define __NR_sched_getaffinity 123
# elif defined(__riscv)
# define __NR_sched_getaffinity 123
# elif defined(__arm__)
# define __NR_sched_getaffinity 242
# elif defined(__cris__)
# define __NR_sched_getaffinity 242
/*# elif defined(__mips__)
# define __NR_sched_getaffinity TODO (32/64/nabi) */
# else
# warning "don't know the syscall number for sched_getaffinity on this architecture, will not support getting binding"
# define sched_getaffinity(pid, lg, mask) (errno = ENOSYS, -1)
# endif
# endif
# ifndef sched_getaffinity
# define sched_getaffinity(pid, lg, mask) (syscall(__NR_sched_getaffinity, pid, lg, mask) < 0 ? -1 : 0)
# endif
#endif
/* numa syscalls are only in libnuma, but libnuma devel headers aren't widely installed.
* just redefine these syscalls to avoid requiring libnuma devel headers just because of these missing syscalls.
* __NR_foo should be defined in headers in all modern platforms.
* Just redefine the basic ones on important platform when not to hard to detect/define.
*/
#ifndef MPOL_DEFAULT
# define MPOL_DEFAULT 0
#endif
#ifndef MPOL_PREFERRED
# define MPOL_PREFERRED 1
#endif
#ifndef MPOL_BIND
# define MPOL_BIND 2
#endif
#ifndef MPOL_INTERLEAVE
# define MPOL_INTERLEAVE 3
#endif
#ifndef MPOL_LOCAL
# define MPOL_LOCAL 4
#endif
#ifndef MPOL_PREFERRED_MANY
# define MPOL_PREFERRED_MANY 5
#endif
#ifndef MPOL_F_ADDR
# define MPOL_F_ADDR (1<<1)
#endif
#ifndef MPOL_MF_STRICT
# define MPOL_MF_STRICT (1<<0)
#endif
#ifndef MPOL_MF_MOVE
# define MPOL_MF_MOVE (1<<1)
#endif
#ifndef __NR_mbind
# ifdef __i386__
# define __NR_mbind 274
# elif defined(__x86_64__)
# define __NR_mbind 237
# elif defined(__ia64__)
# define __NR_mbind 1259
# elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) || defined(__powerpc64__) || defined(__ppc64__)
# define __NR_mbind 259
# elif defined(__sparc__)
# define __NR_mbind 353
# elif defined(__aarch64__)
# define __NR_mbind 235
# elif defined(__riscv)
# define __NR_mbind 235
# elif defined(__arm__)
# define __NR_mbind 319
# endif
#endif
static __hwloc_inline long hwloc_mbind(void *addr __hwloc_attribute_unused,
unsigned long len __hwloc_attribute_unused,
int mode __hwloc_attribute_unused,
const unsigned long *nodemask __hwloc_attribute_unused,
unsigned long maxnode __hwloc_attribute_unused,
unsigned flags __hwloc_attribute_unused)
{
#if (defined __NR_mbind) && (defined HWLOC_HAVE_SYSCALL)
return syscall(__NR_mbind, (long) addr, len, mode, (long)nodemask, maxnode, flags);
#else
#warning Couldn't find __NR_mbind syscall number, memory binding won't be supported
errno = ENOSYS;
return -1;
#endif
}
#ifndef __NR_set_mempolicy
# ifdef __i386__
# define __NR_set_mempolicy 276
# elif defined(__x86_64__)
# define __NR_set_mempolicy 239
# elif defined(__ia64__)
# define __NR_set_mempolicy 1261
# elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) || defined(__powerpc64__) || defined(__ppc64__)
# define __NR_set_mempolicy 261
# elif defined(__sparc__)
# define __NR_set_mempolicy 305
# elif defined(__aarch64__)
# define __NR_set_mempolicy 237
# elif defined(__riscv)
# define __NR_set_mempolicy 237
# elif defined(__arm__)
# define __NR_set_mempolicy 321
# endif
#endif
static __hwloc_inline long hwloc_set_mempolicy(int mode __hwloc_attribute_unused,
const unsigned long *nodemask __hwloc_attribute_unused,
unsigned long maxnode __hwloc_attribute_unused)
{
#if (defined __NR_set_mempolicy) && (defined HWLOC_HAVE_SYSCALL)
return syscall(__NR_set_mempolicy, mode, nodemask, maxnode);
#else
#warning Couldn't find __NR_set_mempolicy syscall number, memory binding won't be supported
errno = ENOSYS;
return -1;
#endif
}
#ifndef __NR_get_mempolicy
# ifdef __i386__
# define __NR_get_mempolicy 275
# elif defined(__x86_64__)
# define __NR_get_mempolicy 238
# elif defined(__ia64__)
# define __NR_get_mempolicy 1260
# elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) || defined(__powerpc64__) || defined(__ppc64__)
# define __NR_get_mempolicy 260
# elif defined(__sparc__)
# define __NR_get_mempolicy 304
# elif defined(__aarch64__)
# define __NR_get_mempolicy 236
# elif defined(__riscv)
# define __NR_get_mempolicy 236
# elif defined(__arm__)
# define __NR_get_mempolicy 320
# endif
#endif
static __hwloc_inline long hwloc_get_mempolicy(int *mode __hwloc_attribute_unused,
unsigned long *nodemask __hwloc_attribute_unused,
unsigned long maxnode __hwloc_attribute_unused,
void *addr __hwloc_attribute_unused,
int flags __hwloc_attribute_unused)
{
#if (defined __NR_get_mempolicy) && (defined HWLOC_HAVE_SYSCALL)
return syscall(__NR_get_mempolicy, mode, nodemask, maxnode, addr, flags);
#else
#warning Couldn't find __NR_get_mempolicy syscall number, memory binding won't be supported
errno = ENOSYS;
return -1;
#endif
}
#ifndef __NR_migrate_pages
# ifdef __i386__
# define __NR_migrate_pages 204
# elif defined(__x86_64__)
# define __NR_migrate_pages 256
# elif defined(__ia64__)
# define __NR_migrate_pages 1280
# elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) || defined(__powerpc64__) || defined(__ppc64__)
# define __NR_migrate_pages 258
# elif defined(__sparc__)
# define __NR_migrate_pages 302
# elif defined(__aarch64__)
# define __NR_migrate_pages 238
# elif defined(__riscv)
# define __NR_migrate_pages 238
# elif defined(__arm__)
# define __NR_migrate_pages 400
# endif
#endif
static __hwloc_inline long hwloc_migrate_pages(int pid __hwloc_attribute_unused,
unsigned long maxnode __hwloc_attribute_unused,
const unsigned long *oldnodes __hwloc_attribute_unused,
const unsigned long *newnodes __hwloc_attribute_unused)
{
#if (defined __NR_migrate_pages) && (defined HWLOC_HAVE_SYSCALL)
return syscall(__NR_migrate_pages, pid, maxnode, oldnodes, newnodes);
#else
#warning Couldn't find __NR_migrate_pages syscall number, memory migration won't be supported
errno = ENOSYS;
return -1;
#endif
}
#ifndef __NR_move_pages
# ifdef __i386__
# define __NR_move_pages 317
# elif defined(__x86_64__)
# define __NR_move_pages 279
# elif defined(__ia64__)
# define __NR_move_pages 1276
# elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) || defined(__powerpc64__) || defined(__ppc64__)
# define __NR_move_pages 301
# elif defined(__sparc__)
# define __NR_move_pages 307
# elif defined(__aarch64__)
# define __NR_move_pages 239
# elif defined(__riscv)
# define __NR_move_pages 239
# elif defined(__arm__)
# define __NR_move_pages 344
# endif
#endif
static __hwloc_inline long hwloc_move_pages(int pid __hwloc_attribute_unused,
unsigned long count __hwloc_attribute_unused,
void **pages __hwloc_attribute_unused,
const int *nodes __hwloc_attribute_unused,
int *status __hwloc_attribute_unused,
int flags __hwloc_attribute_unused)
{
#if (defined __NR_move_pages) && (defined HWLOC_HAVE_SYSCALL)
return syscall(__NR_move_pages, pid, count, pages, nodes, status, flags);
#else
#warning Couldn't find __NR_move_pages syscall number, getting memory location won't be supported
errno = ENOSYS;
return -1;
#endif
}
/* Added for ntohl() */
#include <arpa/inet.h>
#ifdef HAVE_OPENAT
/* Use our own filesystem functions if we have openat */
static const char *
hwloc_checkat(const char *path, int fsroot_fd)
{
const char *relative_path = path;
if (fsroot_fd >= 0)
/* Skip leading slashes. */
for (; *relative_path == '/'; relative_path++);
return relative_path;
}
static int
hwloc_openat(const char *path, int fsroot_fd)
{
const char *relative_path;
relative_path = hwloc_checkat(path, fsroot_fd);
if (!relative_path)
return -1;
return openat (fsroot_fd, relative_path, O_RDONLY);
}
static FILE *
hwloc_fopenat(const char *path, const char *mode, int fsroot_fd)
{
int fd;
if (strcmp(mode, "r")) {
errno = ENOTSUP;
return NULL;
}
fd = hwloc_openat (path, fsroot_fd);
if (fd == -1)
return NULL;
return fdopen(fd, mode);
}
static int
hwloc_accessat(const char *path, int mode, int fsroot_fd)
{
const char *relative_path;
relative_path = hwloc_checkat(path, fsroot_fd);
if (!relative_path)
return -1;
return faccessat(fsroot_fd, relative_path, mode, 0);
}
static int
hwloc_fstatat(const char *path, struct stat *st, int flags, int fsroot_fd)
{
const char *relative_path;
relative_path = hwloc_checkat(path, fsroot_fd);
if (!relative_path)
return -1;
return fstatat(fsroot_fd, relative_path, st, flags);
}
static DIR*
hwloc_opendirat(const char *path, int fsroot_fd)
{
int dir_fd;
const char *relative_path;
relative_path = hwloc_checkat(path, fsroot_fd);
if (!relative_path)
return NULL;
dir_fd = openat(fsroot_fd, relative_path, O_RDONLY | O_DIRECTORY);
if (dir_fd < 0)
return NULL;
return fdopendir(dir_fd);
}
static int
hwloc_readlinkat(const char *path, char *buf, size_t buflen, int fsroot_fd)
{
const char *relative_path;
relative_path = hwloc_checkat(path, fsroot_fd);
if (!relative_path)
return -1;
return readlinkat(fsroot_fd, relative_path, buf, buflen);
}
#endif /* HAVE_OPENAT */
/* Static inline version of fopen so that we can use openat if we have
it, but still preserve compiler parameter checking */
static __hwloc_inline int
hwloc_open(const char *p, int d __hwloc_attribute_unused)
{
#ifdef HAVE_OPENAT
return hwloc_openat(p, d);
#else
return open(p, O_RDONLY);
#endif
}
static __hwloc_inline FILE *
hwloc_fopen(const char *p, const char *m, int d __hwloc_attribute_unused)
{
#ifdef HAVE_OPENAT
return hwloc_fopenat(p, m, d);
#else
return fopen(p, m);
#endif
}
/* Static inline version of access so that we can use openat if we have
it, but still preserve compiler parameter checking */
static __hwloc_inline int
hwloc_access(const char *p, int m, int d __hwloc_attribute_unused)
{
#ifdef HAVE_OPENAT
return hwloc_accessat(p, m, d);
#else
return access(p, m);
#endif
}
static __hwloc_inline int
hwloc_stat(const char *p, struct stat *st, int d __hwloc_attribute_unused)
{
#ifdef HAVE_OPENAT
return hwloc_fstatat(p, st, 0, d);
#else
return stat(p, st);
#endif
}
/* Static inline version of opendir so that we can use openat if we have
it, but still preserve compiler parameter checking */
static __hwloc_inline DIR *
hwloc_opendir(const char *p, int d __hwloc_attribute_unused)
{
#ifdef HAVE_OPENAT
return hwloc_opendirat(p, d);
#else
return opendir(p);
#endif
}
static __hwloc_inline int
hwloc_readlink(const char *p, char *l, size_t ll, int d __hwloc_attribute_unused)
{
ssize_t err;
/* readlink doesn't put the ending \0. read ll-1 and add it. */
#ifdef HAVE_OPENAT
err = hwloc_readlinkat(p, l, ll-1, d);
#else
err = readlink(p, l, ll-1);
#endif
if (err >= 0)
l[err] = '\0';
return err;
}
/*****************************************
******* Helpers for reading files *******
*****************************************/
/* Read up to length-1 bytes in path and add an ending \0.
* Return read bytes (without counting the ending \0), 0 for empty file, or -1 on error.
*/
static __hwloc_inline ssize_t
hwloc_read_path_by_length(const char *path, char *string, size_t length, int fsroot_fd)
{
int fd;
ssize_t ret;
fd = hwloc_open(path, fsroot_fd);
if (fd < 0)
return -1;
ret = read(fd, string, length-1); /* read -1 to put the ending \0 */
close(fd);
if (ret <= 0)
return -1;
string[ret] = 0;
return ret;
}
static __hwloc_inline int
hwloc_read_path_as_int(const char *path, int *value, int fsroot_fd)
{
char string[11];
if (hwloc_read_path_by_length(path, string, sizeof(string), fsroot_fd) <= 0)
return -1;
*value = atoi(string);
return 0;
}
static __hwloc_inline int
hwloc_read_path_as_uint(const char *path, unsigned *value, int fsroot_fd)
{
char string[11];
if (hwloc_read_path_by_length(path, string, sizeof(string), fsroot_fd) <= 0)
return -1;
*value = (unsigned) strtoul(string, NULL, 10);
return 0;
}
static __hwloc_inline int
hwloc_read_path_as_uint64(const char *path, uint64_t *value, int fsroot_fd)
{
char string[22];
if (hwloc_read_path_by_length(path, string, sizeof(string), fsroot_fd) <= 0)
return -1;
*value = (uint64_t) strtoull(string, NULL, 10);
return 0;
}
/* Read everything from fd and save it into a newly allocated buffer
* returned in bufferp. Use sizep as a default buffer size, and return
* the actually needed size in sizep.
*/
static __hwloc_inline int
hwloc__read_fd(int fd, char **bufferp, size_t *sizep)
{
char *buffer;
size_t toread, filesize, totalread;
ssize_t ret;
toread = filesize = *sizep;
/* Alloc and read +1 so that we get EOF on 2^n without reading once more */
buffer = malloc(filesize+1);
if (!buffer)
return -1;
ret = read(fd, buffer, toread+1);
if (ret < 0) {
free(buffer);
return -1;
}
totalread = (size_t) ret;
if (totalread < toread + 1)
/* Normal case, a single read got EOF */
goto done;
/* Unexpected case, must extend the buffer and read again.
* Only occurs on first invocation and if the kernel ever uses multiple page for a single mask.
*/
do {
char *tmp;
toread = filesize;
filesize *= 2;
tmp = realloc(buffer, filesize+1);
if (!tmp) {
free(buffer);
return -1;
}
buffer = tmp;
ret = read(fd, buffer+toread+1, toread);
if (ret < 0) {
free(buffer);
return -1;
}
totalread += ret;
} while ((size_t) ret == toread);
done:
buffer[totalread] = '\0';
*bufferp = buffer;
*sizep = filesize;
return 0;
}
/* kernel cpumaps are composed of an array of 32bits cpumasks */
#define KERNEL_CPU_MASK_BITS 32
#define KERNEL_CPU_MAP_LEN (KERNEL_CPU_MASK_BITS/4+2)
static __hwloc_inline int
hwloc__read_path_as_cpumask(const char *path, hwloc_bitmap_t set, int fsroot_fd)
{
static size_t _filesize = 0; /* will be dynamically initialized to hwloc_get_pagesize(), and increased later if needed */
size_t filesize;
unsigned long *maps;
unsigned long map;
int nr_maps = 0;
static int _nr_maps_allocated = 8; /* Only compute the power-of-two above the kernel cpumask size once.
* Actually, it may increase multiple times if first read cpumaps start with zeroes.
*/
int nr_maps_allocated = _nr_maps_allocated;
char *buffer, *tmpbuf;
int fd, err;
int i;
fd = hwloc_open(path, fsroot_fd);
if (fd < 0)
goto out;
/* Kernel sysfs files are usually at most one page. 4kB may contain 455 32-bit
* masks (followed by comma), enough for 14k PUs. So allocate a page by default for now.
*
* If we ever need a larger buffer, we'll realloc() the buffer during the first
* invocation of this function so that others directly allocate the right size
* (all cpumask files have the exact same size).
*/
filesize = _filesize;
if (!filesize)
filesize = hwloc_getpagesize();
err = hwloc__read_fd(fd, &buffer, &filesize);
close(fd);
if (err < 0)
goto out;
/* Only update the static value with the final one,
* to avoid sharing intermediate values that we modify,
* in case there's ever multiple concurrent calls.
*/
_filesize = filesize;
maps = malloc(nr_maps_allocated * sizeof(*maps));
if (!maps)
goto out_with_buffer;
/* reset to zero first */
hwloc_bitmap_zero(set);
/* parse the whole mask */
tmpbuf = buffer;
while (sscanf(tmpbuf, "%lx", &map) == 1) {
/* read one kernel cpu mask and the ending comma */
if (nr_maps == nr_maps_allocated) {
unsigned long *tmp = realloc(maps, 2*nr_maps_allocated * sizeof(*maps));
if (!tmp)
goto out_with_maps;
maps = tmp;
nr_maps_allocated *= 2;
}
tmpbuf = strchr(tmpbuf, ',');
if (!tmpbuf) {
maps[nr_maps++] = map;
break;
} else
tmpbuf++;
if (!map && !nr_maps)
/* ignore the first map if it's empty */
continue;
maps[nr_maps++] = map;
}
free(buffer);
/* convert into a set */
#if KERNEL_CPU_MASK_BITS == HWLOC_BITS_PER_LONG
for(i=0; i<nr_maps; i++)
hwloc_bitmap_set_ith_ulong(set, i, maps[nr_maps-1-i]);
#else
for(i=0; i<(nr_maps+1)/2; i++) {
unsigned long mask;
mask = maps[nr_maps-2*i-1];
if (2*i+1<nr_maps)
mask |= maps[nr_maps-2*i-2] << KERNEL_CPU_MASK_BITS;
hwloc_bitmap_set_ith_ulong(set, i, mask);
}
#endif
free(maps);
/* Only update the static value with the final one,
* to avoid sharing intermediate values that we modify,
* in case there's ever multiple concurrent calls.
*/
if (nr_maps_allocated > _nr_maps_allocated)
_nr_maps_allocated = nr_maps_allocated;
return 0;
out_with_maps:
free(maps);
out_with_buffer:
free(buffer);
out:
return -1;
}
static __hwloc_inline hwloc_bitmap_t
hwloc__alloc_read_path_as_cpumask(const char *maskpath, int fsroot_fd)
{
hwloc_bitmap_t set;
int err;
set = hwloc_bitmap_alloc();
if (!set)
return NULL;
err = hwloc__read_path_as_cpumask(maskpath, set, fsroot_fd);
if (err < 0) {
hwloc_bitmap_free(set);
return NULL;
} else
return set;
}
int
hwloc_linux_read_path_as_cpumask(const char *maskpath, hwloc_bitmap_t set)
{
return hwloc__read_path_as_cpumask(maskpath, set, -1);
}
/* on failure, the content of set is undefined */
static __hwloc_inline int
hwloc__read_path_as_cpulist(const char *path, hwloc_bitmap_t set, int fsroot_fd)
{
/* Kernel sysfs files are usually at most one page.
* But cpulists can be of very different sizes depending on the fragmentation,
* so don't bother remember the actual read size between invocations.
* We don't have many invocations anyway.
*/
size_t filesize = hwloc_getpagesize();
char *buffer, *current, *comma, *tmp;
int prevlast, nextfirst, nextlast; /* beginning/end of enabled-segments */
int fd, err;
fd = hwloc_open(path, fsroot_fd);
if (fd < 0)
return -1;
err = hwloc__read_fd(fd, &buffer, &filesize);
close(fd);
if (err < 0)
return -1;
hwloc_bitmap_fill(set);
current = buffer;
prevlast = -1;
while (1) {
/* save a pointer to the next comma and erase it to simplify things */
comma = strchr(current, ',');
if (comma)
*comma = '\0';
/* find current enabled-segment bounds */
nextfirst = strtoul(current, &tmp, 0);
if (*tmp == '-')
nextlast = strtoul(tmp+1, NULL, 0);
else
nextlast = nextfirst;
if (prevlast+1 <= nextfirst-1)
hwloc_bitmap_clr_range(set, prevlast+1, nextfirst-1);
/* switch to next enabled-segment */
prevlast = nextlast;
if (!comma)
break;
current = comma+1;
}
hwloc_bitmap_clr_range(set, prevlast+1, -1);
free(buffer);
return 0;
}
/* on failure, the content of set is undefined */
static __hwloc_inline hwloc_bitmap_t
hwloc__alloc_read_path_as_cpulist(const char *maskpath, int fsroot_fd)
{
hwloc_bitmap_t set;
int err;
set = hwloc_bitmap_alloc_full();
if (!set)
return NULL;
err = hwloc__read_path_as_cpulist(maskpath, set, fsroot_fd);
if (err < 0) {
hwloc_bitmap_free(set);
return NULL;
} else
return set;
}
/*****************************
******* CpuBind Hooks *******
*****************************/
int
hwloc_linux_set_tid_cpubind(hwloc_topology_t topology __hwloc_attribute_unused, pid_t tid __hwloc_attribute_unused, hwloc_const_bitmap_t hwloc_set __hwloc_attribute_unused)
{
/* The resulting binding is always strict */
#if defined(HWLOC_HAVE_CPU_SET_S) && !defined(HWLOC_HAVE_OLD_SCHED_SETAFFINITY)
cpu_set_t *plinux_set;
unsigned cpu;
int last;
size_t setsize;
int err;
last = hwloc_bitmap_last(hwloc_set);
if (last == -1) {
errno = EINVAL;
return -1;
}
setsize = CPU_ALLOC_SIZE(last+1);
plinux_set = CPU_ALLOC(last+1);
if (!plinux_set)
return -1;
CPU_ZERO_S(setsize, plinux_set);
hwloc_bitmap_foreach_begin(cpu, hwloc_set)
CPU_SET_S(cpu, setsize, plinux_set);
hwloc_bitmap_foreach_end();
err = sched_setaffinity(tid, setsize, plinux_set);
CPU_FREE(plinux_set);
return err;
#elif defined(HWLOC_HAVE_CPU_SET)
cpu_set_t linux_set;
unsigned cpu;
CPU_ZERO(&linux_set);
hwloc_bitmap_foreach_begin(cpu, hwloc_set)
CPU_SET(cpu, &linux_set);
hwloc_bitmap_foreach_end();
#ifdef HWLOC_HAVE_OLD_SCHED_SETAFFINITY
return sched_setaffinity(tid, &linux_set);
#else /* HWLOC_HAVE_OLD_SCHED_SETAFFINITY */
return sched_setaffinity(tid, sizeof(linux_set), &linux_set);
#endif /* HWLOC_HAVE_OLD_SCHED_SETAFFINITY */
#elif defined(HWLOC_HAVE_SYSCALL)
unsigned long mask = hwloc_bitmap_to_ulong(hwloc_set);
#ifdef HWLOC_HAVE_OLD_SCHED_SETAFFINITY
return sched_setaffinity(tid, (void*) &mask);
#else /* HWLOC_HAVE_OLD_SCHED_SETAFFINITY */
return sched_setaffinity(tid, sizeof(mask), (void*) &mask);
#endif /* HWLOC_HAVE_OLD_SCHED_SETAFFINITY */
#else /* !SYSCALL */
errno = ENOSYS;
return -1;
#endif /* !SYSCALL */
}
#if defined(HWLOC_HAVE_CPU_SET_S) && !defined(HWLOC_HAVE_OLD_SCHED_SETAFFINITY)
/*
* On some kernels, sched_getaffinity requires the output size to be larger
* than the kernel cpu_set size (defined by CONFIG_NR_CPUS).
* Try sched_affinity on ourself until we find a nr_cpus value that makes
* the kernel happy.
*/
static int
hwloc_linux_find_kernel_nr_cpus(hwloc_topology_t topology)
{
static int _nr_cpus = -1;
int nr_cpus = _nr_cpus;
hwloc_bitmap_t possible_bitmap;
if (nr_cpus != -1)
/* already computed */
return nr_cpus;
if (topology->levels[0][0]->complete_cpuset)
/* start with a nr_cpus that may contain the whole topology */
nr_cpus = hwloc_bitmap_last(topology->levels[0][0]->complete_cpuset) + 1;
if (nr_cpus <= 0)
/* start from scratch, the topology isn't ready yet (complete_cpuset is missing (-1) or empty (0))*/
nr_cpus = 1;
/* reading /sys/devices/system/cpu/kernel_max would be easier (single value to parse instead of a list),
* but its value may be way too large (5119 on CentOS7).
* /sys/devices/system/cpu/possible is better because it matches the current hardware.
*/
possible_bitmap = hwloc__alloc_read_path_as_cpulist("/sys/devices/system/cpu/possible", -1); /* binding only supported in real fsroot, no need for data->root_fd */
if (possible_bitmap) {
int max_possible = hwloc_bitmap_last(possible_bitmap);
hwloc_debug_bitmap("possible CPUs are %s\n", possible_bitmap);
if (nr_cpus < max_possible + 1)
nr_cpus = max_possible + 1;
hwloc_bitmap_free(possible_bitmap);
}
while (1) {
cpu_set_t *set = CPU_ALLOC(nr_cpus);
size_t setsize = CPU_ALLOC_SIZE(nr_cpus);
int err;
if (!set)
return -1; /* caller will return an error, and we'll try again later */
err = sched_getaffinity(0, setsize, set); /* always works, unless setsize is too small */
CPU_FREE(set);
nr_cpus = setsize * 8; /* that's the value that was actually tested */
if (!err)
/* Found it. Only update the static value with the final one,
* to avoid sharing intermediate values that we modify,
* in case there's ever multiple concurrent calls.
*/
return _nr_cpus = nr_cpus;
nr_cpus *= 2;
}
}
#endif
int
hwloc_linux_get_tid_cpubind(hwloc_topology_t topology __hwloc_attribute_unused, pid_t tid __hwloc_attribute_unused, hwloc_bitmap_t hwloc_set __hwloc_attribute_unused)
{
int err __hwloc_attribute_unused;
#if defined(HWLOC_HAVE_CPU_SET_S) && !defined(HWLOC_HAVE_OLD_SCHED_SETAFFINITY)
cpu_set_t *plinux_set;
unsigned cpu;
int last;
size_t setsize;
int kernel_nr_cpus;
/* find the kernel nr_cpus so as to use a large enough cpu_set size */
kernel_nr_cpus = hwloc_linux_find_kernel_nr_cpus(topology);
if (kernel_nr_cpus < 0)
return -1;
setsize = CPU_ALLOC_SIZE(kernel_nr_cpus);
plinux_set = CPU_ALLOC(kernel_nr_cpus);
if (!plinux_set)