| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright 2020 Daniel Wagner <[email protected]> |
| 4 | * Copyright 2020 John Kacur <[email protected]> |
| 5 | */ |
| 6 | |
| 7 | #include <sys/types.h> |
| 8 | #include <unistd.h> |
| 9 | #include <errno.h> |
| 10 | #include <sched.h> |
| 11 | #include <pthread.h> |
| 12 | |
| 13 | #include "rt-error.h" |
| 14 | #include "rt-numa.h" |
| 15 | |
| 16 | /* |
| 17 | * numa_available() must be called before any other calls to the numa library |
| 18 | * returns 1 if numa is available, or 0 if numa is not available |
| 19 | */ |
| 20 | int numa_initialize(void) |
| 21 | { |
| 22 | static int is_initialized; // Only call numa_available once |
| 23 | static int numa; |
| 24 | |
| 25 | if (is_initialized == 1) |
| 26 | return numa; |
| 27 | |
| 28 | if (numa_available() != -1) |
| 29 | numa = 1; |
| 30 | |
| 31 | is_initialized = 1; |
| 32 | |
| 33 | return numa; |
| 34 | } |
| 35 | |
| 36 | int get_available_cpus(struct bitmask *cpumask) |
| 37 | { |
| 38 | cpu_set_t cpuset; |
| 39 | int ret; |
| 40 | |
| 41 | if (cpumask) |
| 42 | return numa_bitmask_weight(cpumask); |
| 43 | |
| 44 | CPU_ZERO(&cpuset); |
| 45 | |
| 46 | ret = sched_getaffinity(0, sizeof(cpu_set_t), &cpuset); |
| 47 | if (ret < 0) |
| 48 | fatal("sched_getaffinity failed: %m\n"); |
| 49 | |
| 50 | return CPU_COUNT(&cpuset); |
| 51 | } |
| 52 | |
| 53 | int cpu_for_thread_sp(int thread_num, int max_cpus, struct bitmask *cpumask) |
| 54 | { |
| 55 | int m, cpu, i, num_cpus; |
| 56 | |
| 57 | num_cpus = numa_bitmask_weight(cpumask); |
| 58 | |
| 59 | if (num_cpus == 0) |
| 60 | fatal("No allowable cpus to run on\n"); |
| 61 | |
| 62 | m = thread_num % num_cpus; |
| 63 | |
| 64 | /* there are num_cpus bits set, we want position of m'th one */ |
| 65 | for (i = 0, cpu = 0; i < max_cpus; i++) { |
| 66 | if (numa_bitmask_isbitset(cpumask, i)) { |
| 67 | if (cpu == m) |
| 68 | return i; |
| 69 | cpu++; |
| 70 | } |
| 71 | } |
| 72 | warn("Bug in cpu mask handling code.\n"); |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | /* cpu_for_thread AFFINITY_USEALL */ |
| 77 | int cpu_for_thread_ua(int thread_num, int max_cpus) |
| 78 | { |
| 79 | int res, num_cpus, i, m, cpu; |
| 80 | cpu_set_t cpuset; |
| 81 | |
| 82 | CPU_ZERO(&cpuset); |
| 83 | |
| 84 | res = sched_getaffinity(0, sizeof(cpu_set_t), &cpuset); |
| 85 | if (res != 0) |
| 86 | fatal("sched_getaffinity failed: %s\n", strerror(res)); |
| 87 | |
| 88 | num_cpus = CPU_COUNT(&cpuset); |
| 89 | m = thread_num % num_cpus; |
| 90 | |
| 91 | for (i = 0, cpu = 0; i < max_cpus; i++) { |
| 92 | if (CPU_ISSET(i, &cpuset)) { |
| 93 | if (cpu == m) |
| 94 | return i; |
| 95 | cpu++; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | warn("Bug in cpu mask handling code.\n"); |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * After this function is called, affinity_mask is the intersection of |
| 105 | * the user supplied affinity mask and the affinity mask from the run |
| 106 | * time environment |
| 107 | */ |
| 108 | static void use_current_cpuset(int max_cpus, struct bitmask *cpumask) |
| 109 | { |
| 110 | struct bitmask *curmask; |
| 111 | int i; |
| 112 | |
| 113 | curmask = numa_allocate_cpumask(); |
| 114 | numa_sched_getaffinity(getpid(), curmask); |
| 115 | |
| 116 | /* |
| 117 | * Clear bits that are not set in both the cpuset from the |
| 118 | * environment, and in the user specified affinity. |
| 119 | */ |
| 120 | for (i = 0; i < max_cpus; i++) { |
| 121 | if ((!numa_bitmask_isbitset(cpumask, i)) || |
| 122 | (!numa_bitmask_isbitset(curmask, i))) |
| 123 | numa_bitmask_clearbit(cpumask, i); |
| 124 | } |
| 125 | |
| 126 | numa_bitmask_free(curmask); |
| 127 | } |
| 128 | |
| 129 | int parse_cpumask(char *str, int max_cpus, struct bitmask **cpumask) |
| 130 | { |
| 131 | struct bitmask *mask; |
| 132 | |
| 133 | mask = numa_parse_cpustring_all(str); |
| 134 | if (!mask) |
| 135 | return -ENOMEM; |
| 136 | |
| 137 | if (numa_bitmask_weight(mask) == 0) { |
| 138 | numa_bitmask_free(mask); |
| 139 | *cpumask = NULL; |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | if (strchr(str, '!') != NULL || strchr(str, '+') != NULL) |
| 144 | use_current_cpuset(max_cpus, mask); |
| 145 | *cpumask = mask; |
| 146 | |
| 147 | return 0; |
| 148 | } |