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 void *get_auxv_entry(int type)
30 fd = open("/proc/self/auxv", O_RDONLY);
38 num = read(fd, auxv, sizeof(auxv));
44 if (num > sizeof(auxv)) {
45 printf("Overflowed auxv buffer\n");
49 p = (ElfW(auxv_t) *)auxv;
51 while (p->a_type != AT_NULL) {
52 if (p->a_type == type) {
53 result = (void *)p->a_un.a_val;
64 int pick_online_cpu(void)
70 ncpus = get_nprocs_conf();
71 size = CPU_ALLOC_SIZE(ncpus);
72 mask = CPU_ALLOC(ncpus);
78 CPU_ZERO_S(size, mask);
80 if (sched_getaffinity(0, size, mask)) {
81 perror("sched_getaffinity");
85 /* We prefer a primary thread, but skip 0 */
86 for (cpu = 8; cpu < ncpus; cpu += 8)
87 if (CPU_ISSET_S(cpu, size, mask))
90 /* Search for anything, but in reverse */
91 for (cpu = ncpus - 1; cpu >= 0; cpu--)
92 if (CPU_ISSET_S(cpu, size, mask))
95 printf("No cpus in affinity mask?!\n");