| 1 | /* |
| 2 | * libringbuffer/smp.c |
| 3 | * |
| 4 | * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com> |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; only |
| 10 | * version 2.1 of the License. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #define _GNU_SOURCE |
| 23 | #define _LGPL_SOURCE |
| 24 | #include <assert.h> |
| 25 | #include <ctype.h> |
| 26 | #include <errno.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <limits.h> |
| 29 | #include <pthread.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <unistd.h> |
| 32 | |
| 33 | #include "smp.h" |
| 34 | #include "usterr-signal-safe.h" |
| 35 | |
| 36 | int __num_possible_cpus; |
| 37 | |
| 38 | #if (defined(__GLIBC__) || defined( __UCLIBC__)) |
| 39 | int get_num_possible_cpus_fallback(void) |
| 40 | { |
| 41 | /* On Linux, when some processors are offline |
| 42 | * _SC_NPROCESSORS_CONF counts the offline |
| 43 | * processors, whereas _SC_NPROCESSORS_ONLN |
| 44 | * does not. If we used _SC_NPROCESSORS_ONLN, |
| 45 | * getcpu() could return a value greater than |
| 46 | * this sysconf, in which case the arrays |
| 47 | * indexed by processor would overflow. |
| 48 | */ |
| 49 | return sysconf(_SC_NPROCESSORS_CONF); |
| 50 | } |
| 51 | |
| 52 | #else |
| 53 | |
| 54 | /* |
| 55 | * The MUSL libc implementation of the _SC_NPROCESSORS_CONF sysconf does not |
| 56 | * return the number of configured CPUs in the system but relies on the cpu |
| 57 | * affinity mask of the current task. |
| 58 | * |
| 59 | * So instead we use a strategy similar to GLIBC's, counting the cpu |
| 60 | * directories in "/sys/devices/system/cpu" and fallback on the value from |
| 61 | * sysconf if it fails. |
| 62 | */ |
| 63 | |
| 64 | #include <dirent.h> |
| 65 | #include <limits.h> |
| 66 | #include <stdlib.h> |
| 67 | #include <string.h> |
| 68 | #include <sys/types.h> |
| 69 | |
| 70 | #define __max(a,b) ((a)>(b)?(a):(b)) |
| 71 | |
| 72 | int get_num_possible_cpus_fallback(void) |
| 73 | { |
| 74 | int count = 0; |
| 75 | DIR *cpudir; |
| 76 | struct dirent *entry; |
| 77 | |
| 78 | cpudir = opendir("/sys/devices/system/cpu"); |
| 79 | if (cpudir == NULL) |
| 80 | goto end; |
| 81 | |
| 82 | /* |
| 83 | * Count the number of directories named "cpu" followed by and |
| 84 | * integer. This is the same strategy as glibc uses. |
| 85 | */ |
| 86 | while ((entry = readdir(cpudir))) { |
| 87 | if (entry->d_type == DT_DIR && |
| 88 | strncmp(entry->d_name, "cpu", 3) == 0) { |
| 89 | |
| 90 | char *endptr; |
| 91 | unsigned long cpu_num; |
| 92 | |
| 93 | cpu_num = strtoul(entry->d_name + 3, &endptr, 10); |
| 94 | if ((cpu_num < ULONG_MAX) && (endptr != entry->d_name + 3) |
| 95 | && (*endptr == '\0')) { |
| 96 | count++; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | end: |
| 102 | /* |
| 103 | * Get the sysconf value as a fallback. Keep the highest number. |
| 104 | */ |
| 105 | return __max(sysconf(_SC_NPROCESSORS_CONF), count); |
| 106 | } |
| 107 | #endif |
| 108 | |
| 109 | /* |
| 110 | * Get the CPU possible mask string from sysfs. |
| 111 | * |
| 112 | * buf: the buffer where the mask will be read. |
| 113 | * max_bytes: the maximum number of bytes to write in the buffer. |
| 114 | * |
| 115 | * Returns the number of bytes read or -1 on error. |
| 116 | */ |
| 117 | int get_possible_cpu_mask_from_sysfs(char *buf, size_t max_bytes) |
| 118 | { |
| 119 | ssize_t bytes_read = 0; |
| 120 | size_t total_bytes_read = 0; |
| 121 | int fd = -1, ret = -1; |
| 122 | |
| 123 | if (buf == NULL) |
| 124 | goto end; |
| 125 | |
| 126 | fd = open("/sys/devices/system/cpu/possible", O_RDONLY); |
| 127 | if (fd < 0) |
| 128 | goto end; |
| 129 | |
| 130 | do { |
| 131 | bytes_read = read(fd, buf + total_bytes_read, |
| 132 | max_bytes - total_bytes_read); |
| 133 | |
| 134 | if (bytes_read < 0) { |
| 135 | if (errno == EINTR) { |
| 136 | continue; /* retry operation */ |
| 137 | } else { |
| 138 | goto end; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | total_bytes_read += bytes_read; |
| 143 | assert(total_bytes_read <= max_bytes); |
| 144 | } while (max_bytes > total_bytes_read && bytes_read > 0); |
| 145 | |
| 146 | /* |
| 147 | * Make sure the mask read is a null terminated string. |
| 148 | */ |
| 149 | if (total_bytes_read < max_bytes) |
| 150 | buf[total_bytes_read] = '\0'; |
| 151 | else |
| 152 | buf[max_bytes - 1] = '\0'; |
| 153 | |
| 154 | if (total_bytes_read > INT_MAX) |
| 155 | goto end; |
| 156 | |
| 157 | ret = (int) total_bytes_read; |
| 158 | |
| 159 | end: |
| 160 | if (fd >= 0 && close(fd) < 0) |
| 161 | PERROR("close"); |
| 162 | |
| 163 | return ret; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * Get the number of CPUs from the possible cpu mask. |
| 168 | * |
| 169 | * pmask: the mask to parse. |
| 170 | * len: the len of the mask excluding '\0'. |
| 171 | * |
| 172 | * Returns the number of possible CPUs from the mask or 0 on error. |
| 173 | */ |
| 174 | int get_num_possible_cpus_from_mask(const char *pmask, size_t len) |
| 175 | { |
| 176 | ssize_t i; |
| 177 | unsigned long cpu_index; |
| 178 | char *endptr; |
| 179 | |
| 180 | /* We need at least one char to read */ |
| 181 | if (len < 1) |
| 182 | goto error; |
| 183 | |
| 184 | /* Start from the end to read the last CPU index. */ |
| 185 | for (i = len - 1; i > 0; i--) { |
| 186 | /* Break when we hit the first separator. */ |
| 187 | if ((pmask[i] == ',') || (pmask[i] == '-')) { |
| 188 | i++; |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | cpu_index = strtoul(&pmask[i], &endptr, 10); |
| 194 | |
| 195 | /* |
| 196 | * If we read a CPU index, increment it by one to return a number of |
| 197 | * CPUs. |
| 198 | */ |
| 199 | if ((&pmask[i] != endptr) && (cpu_index < INT_MAX)) |
| 200 | return (int) cpu_index + 1; |
| 201 | |
| 202 | error: |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | void _get_num_possible_cpus(void) |
| 207 | { |
| 208 | int ret; |
| 209 | int buf_len = sysconf(_SC_PAGE_SIZE); |
| 210 | char buf[buf_len]; |
| 211 | |
| 212 | /* Get the possible cpu mask from sysfs, fallback to sysconf. */ |
| 213 | ret = get_possible_cpu_mask_from_sysfs((char *) &buf, buf_len); |
| 214 | if (ret <= 0) |
| 215 | goto fallback; |
| 216 | |
| 217 | /* Parse the possible cpu mask, on failure fallback to sysconf. */ |
| 218 | ret = get_num_possible_cpus_from_mask((char *) &buf, ret); |
| 219 | if (ret > 0) |
| 220 | goto end; |
| 221 | |
| 222 | fallback: |
| 223 | /* Fallback to sysconf. */ |
| 224 | ret = get_num_possible_cpus_fallback(); |
| 225 | |
| 226 | end: |
| 227 | /* If all methods failed, don't store the value. */ |
| 228 | if (ret < 1) |
| 229 | return; |
| 230 | |
| 231 | __num_possible_cpus = ret; |
| 232 | } |