| 1 | // SPDX-License-Identifier: GPL-2.0-or-latercxx . . $ `<file>/src/lib/rt-numa.c`/ |
| 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"cxx . . $ `<file>/src/include/rt-error.h`/ |
| 14 | #include "rt-numa.h"cxx . . $ `<file>/src/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_initializecxx . . $ numa_initialize(b126dc7c1de90089).(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_availablecxx . . $ numa_available(b126dc7c1de90089).() != -1) |
| 29 | numa = 1; |
| 30 | |
| 31 | is_initialized = 1; |
| 32 | |
| 33 | return numa; |
| 34 | } |
| 35 | |
| 36 | int get_available_cpuscxx . . $ get_available_cpus(e325be287bc55a3e).(struct bitmaskcxx . . $ bitmask# *cpumask) |
| 37 | { |
| 38 | cpu_set_tcxx . . $ cpu_set_t# cpuset; |
| 39 | int ret; |
| 40 | |
| 41 | if (cpumask) |
| 42 | return numa_bitmask_weightcxx . . $ numa_bitmask_weight(4b55de75830e9378).(cpumask); |
| 43 | |
| 44 | CPU_ZEROcxx . . $ `<external>/17522079545615609418/sched.h:96:10`!(&cpuset); |
| 45 | |
| 46 | ret = sched_getaffinitycxx . . $ sched_getaffinity(3d37575d37dd6057).(0, sizeof(cpu_set_tcxx . . $ cpu_set_t#), &cpuset); |
| 47 | if (ret < 0) |
| 48 | fatalcxx . . $ fatal(5313ecf19fd40d4d).("sched_getaffinity failed: %m\n"); |
| 49 | |
| 50 | return CPU_COUNTcxx . . $ `<external>/17522079545615609418/sched.h:97:10`!(&cpuset); |
| 51 | } |
| 52 | |
| 53 | int cpu_for_thread_spcxx . . $ cpu_for_thread_sp(5778b5fee399886d).(int thread_num, int max_cpus, struct bitmaskcxx . . $ bitmask# *cpumask) |
| 54 | { |
| 55 | int m, cpu, i, num_cpus; |
| 56 | |
| 57 | num_cpus = numa_bitmask_weightcxx . . $ numa_bitmask_weight(4b55de75830e9378).(cpumask); |
| 58 | |
| 59 | if (num_cpus == 0) |
| 60 | fatalcxx . . $ fatal(5313ecf19fd40d4d).("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_isbitsetcxx . . $ numa_bitmask_isbitset(9048e51cedd65d8e).(cpumask, i)) { |
| 67 | if (cpu == m) |
| 68 | return i; |
| 69 | cpu++; |
| 70 | } |
| 71 | } |
| 72 | warncxx . . $ warn(d5d9ff45db7b5d36).("Bug in cpu mask handling code.\n"); |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | /* cpu_for_thread AFFINITY_USEALL */ |
| 77 | int cpu_for_thread_uacxx . . $ cpu_for_thread_ua(9b79fb6aee4c0440).(int thread_num, int max_cpus) |
| 78 | { |
| 79 | int res, num_cpus, i, m, cpu; |
| 80 | cpu_set_tcxx . . $ cpu_set_t# cpuset; |
| 81 | |
| 82 | CPU_ZEROcxx . . $ `<external>/17522079545615609418/sched.h:96:10`!(&cpuset); |
| 83 | |
| 84 | res = sched_getaffinitycxx . . $ sched_getaffinity(3d37575d37dd6057).(0, sizeof(cpu_set_tcxx . . $ cpu_set_t#), &cpuset); |
| 85 | if (res != 0) |
| 86 | fatalcxx . . $ fatal(5313ecf19fd40d4d).("sched_getaffinity failed: %s\n", strerrorcxx . . $ strerror(ff224f080444ee45).(res)); |
| 87 | |
| 88 | num_cpus = CPU_COUNTcxx . . $ `<external>/17522079545615609418/sched.h:97:10`!(&cpuset); |
| 89 | m = thread_num % num_cpus; |
| 90 | |
| 91 | for (i = 0, cpu = 0; i < max_cpus; i++) { |
| 92 | if (CPU_ISSETcxx . . $ `<external>/17522079545615609418/sched.h:94:10`!(i, &cpuset)) { |
| 93 | if (cpu == m) |
| 94 | return i; |
| 95 | cpu++; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | warncxx . . $ warn(d5d9ff45db7b5d36).("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_cpusetcxx . . $ use_current_cpuset(121c2d1bef5a1373).(int max_cpus, struct bitmaskcxx . . $ bitmask# *cpumask) |
| 109 | { |
| 110 | struct bitmaskcxx . . $ bitmask# *curmask; |
| 111 | int i; |
| 112 | |
| 113 | curmask = numa_allocate_cpumaskcxx . . $ numa_allocate_cpumask(891d7968813f299e).(); |
| 114 | numa_sched_getaffinitycxx . . $ numa_sched_getaffinity(cf9f0dd4c417177b).(getpidcxx . . $ getpid(b126dc7c1de90089).(), 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_isbitsetcxx . . $ numa_bitmask_isbitset(9048e51cedd65d8e).(cpumask, i)) || |
| 122 | (!numa_bitmask_isbitsetcxx . . $ numa_bitmask_isbitset(9048e51cedd65d8e).(curmask, i))) |
| 123 | numa_bitmask_clearbitcxx . . $ numa_bitmask_clearbit(3111d0266c8957c8).(cpumask, i); |
| 124 | } |
| 125 | |
| 126 | numa_bitmask_freecxx . . $ numa_bitmask_free(d7c6455decaee94d).(curmask); |
| 127 | } |
| 128 | |
| 129 | int parse_cpumaskcxx . . $ parse_cpumask(3a199e04db700bfb).(char *str, int max_cpus, struct bitmaskcxx . . $ bitmask# **cpumask) |
| 130 | { |
| 131 | struct bitmaskcxx . . $ bitmask# *mask; |
| 132 | |
| 133 | mask = numa_parse_cpustring_allcxx . . $ numa_parse_cpustring_all(62218a8025ffedd2).(str); |
| 134 | if (!mask) |
| 135 | return -ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!; |
| 136 | |
| 137 | if (numa_bitmask_weightcxx . . $ numa_bitmask_weight(4b55de75830e9378).(mask) == 0) { |
| 138 | numa_bitmask_freecxx . . $ numa_bitmask_free(d7c6455decaee94d).(mask); |
| 139 | *cpumask = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | if (strchrcxx . . $ strchr(2050848944e00d54).(str, '!') != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || strchrcxx . . $ strchr(2050848944e00d54).(str, '+') != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 144 | use_current_cpusetcxx . . $ use_current_cpuset(121c2d1bef5a1373).(max_cpus, mask); |
| 145 | *cpumask = mask; |
| 146 | |
| 147 | return 0; |
| 148 | } |