2 * Copyright 2013-2015, Michael Ellerman, IBM Corp.
3 * Licensed under GPLv2.
6 #define _GNU_SOURCE /* For CPU_ZERO etc. */
16 #include <sys/sysinfo.h>
17 #include <sys/types.h>
18 #include <sys/utsname.h>
23 static char auxv[4096];
25 int read_auxv(char *buf, ssize_t buf_size)
30 fd = open("/proc/self/auxv", O_RDONLY);
36 num = read(fd, buf, buf_size);
44 printf("overflowed auxv buffer\n");
55 void *find_auxv_entry(int type, char *auxv)
59 p = (ElfW(auxv_t) *)auxv;
61 while (p->a_type != AT_NULL) {
62 if (p->a_type == type)
71 void *get_auxv_entry(int type)
75 if (read_auxv(auxv, sizeof(auxv)))
78 p = find_auxv_entry(type, auxv);
80 return (void *)p->a_un.a_val;
85 int pick_online_cpu(void)
91 ncpus = get_nprocs_conf();
92 size = CPU_ALLOC_SIZE(ncpus);
93 mask = CPU_ALLOC(ncpus);
99 CPU_ZERO_S(size, mask);
101 if (sched_getaffinity(0, size, mask)) {
102 perror("sched_getaffinity");
106 /* We prefer a primary thread, but skip 0 */
107 for (cpu = 8; cpu < ncpus; cpu += 8)
108 if (CPU_ISSET_S(cpu, size, mask))
111 /* Search for anything, but in reverse */
112 for (cpu = ncpus - 1; cpu >= 0; cpu--)
113 if (CPU_ISSET_S(cpu, size, mask))
116 printf("No cpus in affinity mask?!\n");
123 bool is_ppc64le(void)
135 return strcmp(uts.machine, "ppc64le") == 0;