2 * Copyright 2013-2015, Michael Ellerman, IBM Corp.
3 * Licensed under GPLv2.
6 #define _GNU_SOURCE /* For CPU_ZERO etc. */
15 #include <sys/types.h>
20 static char auxv
[4096];
22 int read_auxv(char *buf
, ssize_t buf_size
)
27 fd
= open("/proc/self/auxv", O_RDONLY
);
33 num
= read(fd
, buf
, buf_size
);
41 printf("overflowed auxv buffer\n");
52 void *find_auxv_entry(int type
, char *auxv
)
56 p
= (ElfW(auxv_t
) *)auxv
;
58 while (p
->a_type
!= AT_NULL
) {
59 if (p
->a_type
== type
)
68 void *get_auxv_entry(int type
)
72 if (read_auxv(auxv
, sizeof(auxv
)))
75 p
= find_auxv_entry(type
, auxv
);
77 return (void *)p
->a_un
.a_val
;
82 int pick_online_cpu(void)
89 if (sched_getaffinity(0, sizeof(mask
), &mask
)) {
90 perror("sched_getaffinity");
94 /* We prefer a primary thread, but skip 0 */
95 for (cpu
= 8; cpu
< CPU_SETSIZE
; cpu
+= 8)
96 if (CPU_ISSET(cpu
, &mask
))
99 /* Search for anything, but in reverse */
100 for (cpu
= CPU_SETSIZE
- 1; cpu
>= 0; cpu
--)
101 if (CPU_ISSET(cpu
, &mask
))
104 printf("No cpus in affinity mask?!\n");