2 * Copyright 2013-2015, Michael Ellerman, IBM Corp.
3 * Licensed under GPLv2.
6 #define _GNU_SOURCE /* For CPU_ZERO etc. */
15 #include <sys/sysinfo.h>
16 #include <sys/types.h>
21 static char auxv[4096];
23 int read_auxv(char *buf, ssize_t buf_size)
28 fd = open("/proc/self/auxv", O_RDONLY);
34 num = read(fd, buf, buf_size);
42 printf("overflowed auxv buffer\n");
53 void *find_auxv_entry(int type, char *auxv)
57 p = (ElfW(auxv_t) *)auxv;
59 while (p->a_type != AT_NULL) {
60 if (p->a_type == type)
69 void *get_auxv_entry(int type)
73 if (read_auxv(auxv, sizeof(auxv)))
76 p = find_auxv_entry(type, auxv);
78 return (void *)p->a_un.a_val;
83 int pick_online_cpu(void)
89 ncpus = get_nprocs_conf();
90 size = CPU_ALLOC_SIZE(ncpus);
91 mask = CPU_ALLOC(ncpus);
97 CPU_ZERO_S(size, mask);
99 if (sched_getaffinity(0, size, mask)) {
100 perror("sched_getaffinity");
104 /* We prefer a primary thread, but skip 0 */
105 for (cpu = 8; cpu < ncpus; cpu += 8)
106 if (CPU_ISSET_S(cpu, size, mask))
109 /* Search for anything, but in reverse */
110 for (cpu = ncpus - 1; cpu >= 0; cpu--)
111 if (CPU_ISSET_S(cpu, size, mask))
114 printf("No cpus in affinity mask?!\n");