GNU Linux-libre 4.9.309-gnu1
[releases.git] / tools / testing / selftests / powerpc / utils.c
1 /*
2  * Copyright 2013-2015, Michael Ellerman, IBM Corp.
3  * Licensed under GPLv2.
4  */
5
6 #define _GNU_SOURCE     /* For CPU_ZERO etc. */
7
8 #include <elf.h>
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <link.h>
12 #include <sched.h>
13 #include <stdio.h>
14 #include <sys/stat.h>
15 #include <sys/sysinfo.h>
16 #include <sys/types.h>
17 #include <unistd.h>
18
19 #include "utils.h"
20
21 static char auxv[4096];
22
23 void *get_auxv_entry(int type)
24 {
25         ElfW(auxv_t) *p;
26         void *result;
27         ssize_t num;
28         int fd;
29
30         fd = open("/proc/self/auxv", O_RDONLY);
31         if (fd == -1) {
32                 perror("open");
33                 return NULL;
34         }
35
36         result = NULL;
37
38         num = read(fd, auxv, sizeof(auxv));
39         if (num < 0) {
40                 perror("read");
41                 goto out;
42         }
43
44         if (num > sizeof(auxv)) {
45                 printf("Overflowed auxv buffer\n");
46                 goto out;
47         }
48
49         p = (ElfW(auxv_t) *)auxv;
50
51         while (p->a_type != AT_NULL) {
52                 if (p->a_type == type) {
53                         result = (void *)p->a_un.a_val;
54                         break;
55                 }
56
57                 p++;
58         }
59 out:
60         close(fd);
61         return result;
62 }
63
64 int pick_online_cpu(void)
65 {
66         int ncpus, cpu = -1;
67         cpu_set_t *mask;
68         size_t size;
69
70         ncpus = get_nprocs_conf();
71         size = CPU_ALLOC_SIZE(ncpus);
72         mask = CPU_ALLOC(ncpus);
73         if (!mask) {
74                 perror("malloc");
75                 return -1;
76         }
77
78         CPU_ZERO_S(size, mask);
79
80         if (sched_getaffinity(0, size, mask)) {
81                 perror("sched_getaffinity");
82                 goto done;
83         }
84
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))
88                         goto done;
89
90         /* Search for anything, but in reverse */
91         for (cpu = ncpus - 1; cpu >= 0; cpu--)
92                 if (CPU_ISSET_S(cpu, size, mask))
93                         goto done;
94
95         printf("No cpus in affinity mask?!\n");
96
97 done:
98         CPU_FREE(mask);
99         return cpu;
100 }