2 * Copyright 2013-2015, Michael Ellerman, IBM Corp.
3 * Licensed under GPLv2.
6 #define _GNU_SOURCE /* For CPU_ZERO etc. */
16 #include <sys/types.h>
17 #include <sys/utsname.h>
22 static char auxv
[4096];
24 int read_auxv(char *buf
, ssize_t buf_size
)
29 fd
= open("/proc/self/auxv", O_RDONLY
);
35 num
= read(fd
, buf
, buf_size
);
43 printf("overflowed auxv buffer\n");
54 void *find_auxv_entry(int type
, char *auxv
)
58 p
= (ElfW(auxv_t
) *)auxv
;
60 while (p
->a_type
!= AT_NULL
) {
61 if (p
->a_type
== type
)
70 void *get_auxv_entry(int type
)
74 if (read_auxv(auxv
, sizeof(auxv
)))
77 p
= find_auxv_entry(type
, auxv
);
79 return (void *)p
->a_un
.a_val
;
84 int pick_online_cpu(void)
91 if (sched_getaffinity(0, sizeof(mask
), &mask
)) {
92 perror("sched_getaffinity");
96 /* We prefer a primary thread, but skip 0 */
97 for (cpu
= 8; cpu
< CPU_SETSIZE
; cpu
+= 8)
98 if (CPU_ISSET(cpu
, &mask
))
101 /* Search for anything, but in reverse */
102 for (cpu
= CPU_SETSIZE
- 1; cpu
>= 0; cpu
--)
103 if (CPU_ISSET(cpu
, &mask
))
106 printf("No cpus in affinity mask?!\n");
110 bool is_ppc64le(void)
122 return strcmp(uts
.machine
, "ppc64le") == 0;