| 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 <dirent.h> |
| 27 | #include <errno.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <limits.h> |
| 30 | #include <pthread.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <unistd.h> |
| 33 | #include <string.h> |
| 34 | #include <sys/types.h> |
| 35 | |
| 36 | #include "smp.h" |
| 37 | #include "usterr-signal-safe.h" |
| 38 | |
| 39 | #define __max(a,b) ((a)>(b)?(a):(b)) |
| 40 | |
| 41 | int __num_possible_cpus; |
| 42 | |
| 43 | /* |
| 44 | * As a fallback to parsing the CPU mask in "/sys/devices/system/cpu/possible", |
| 45 | * iterate on all the folders in "/sys/devices/system/cpu" that start with |
| 46 | * "cpu" followed by an integer, keep the highest CPU id encountered during |
| 47 | * this iteration and add 1 to get a number of CPUs. |
| 48 | * |
| 49 | * Then get the value from sysconf(_SC_NPROCESSORS_CONF) as a fallback and |
| 50 | * return the highest one. |
| 51 | * |
| 52 | * On Linux, using the value from sysconf can be unreliable since the way it |
| 53 | * counts CPUs varies between C libraries and even between versions of the same |
| 54 | * library. If we used it directly, getcpu() could return a value greater than |
| 55 | * this sysconf, in which case the arrays indexed by processor would overflow. |
| 56 | * |
| 57 | * As another example, the MUSL libc implementation of the _SC_NPROCESSORS_CONF |
| 58 | * sysconf does not return the number of configured CPUs in the system but |
| 59 | * relies on the cpu affinity mask of the current task. |
| 60 | * |
| 61 | * Returns 0 or less on error. |
| 62 | */ |
| 63 | int get_num_possible_cpus_fallback(void) |
| 64 | { |
| 65 | long max_cpuid = -1; |
| 66 | |
| 67 | DIR *cpudir; |
| 68 | struct dirent *entry; |
| 69 | |
| 70 | cpudir = opendir("/sys/devices/system/cpu"); |
| 71 | if (cpudir == NULL) |
| 72 | goto end; |
| 73 | |
| 74 | /* |
| 75 | * Iterate on all directories named "cpu" followed by an integer. |
| 76 | */ |
| 77 | while ((entry = readdir(cpudir))) { |
| 78 | if (entry->d_type == DT_DIR && |
| 79 | strncmp(entry->d_name, "cpu", 3) == 0) { |
| 80 | |
| 81 | char *endptr; |
| 82 | long cpu_id; |
| 83 | |
| 84 | cpu_id = strtol(entry->d_name + 3, &endptr, 10); |
| 85 | if ((cpu_id < LONG_MAX) && (endptr != entry->d_name + 3) |
| 86 | && (*endptr == '\0')) { |
| 87 | if (cpu_id > max_cpuid) |
| 88 | max_cpuid = cpu_id; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if (closedir(cpudir)) |
| 94 | PERROR("closedir"); |
| 95 | |
| 96 | /* |
| 97 | * If the max CPU id is out of bound, set it to -1 so it results in a |
| 98 | * CPU num of 0. |
| 99 | */ |
| 100 | if (max_cpuid < 0 || max_cpuid > INT_MAX) |
| 101 | max_cpuid = -1; |
| 102 | |
| 103 | end: |
| 104 | /* |
| 105 | * Get the sysconf value as a last resort. Keep the highest number. |
| 106 | */ |
| 107 | return __max(sysconf(_SC_NPROCESSORS_CONF), max_cpuid + 1); |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * Get the CPU possible mask string from sysfs. |
| 112 | * |
| 113 | * buf: the buffer where the mask will be read. |
| 114 | * max_bytes: the maximum number of bytes to write in the buffer. |
| 115 | * |
| 116 | * Returns the number of bytes read or -1 on error. |
| 117 | */ |
| 118 | int get_possible_cpu_mask_from_sysfs(char *buf, size_t max_bytes) |
| 119 | { |
| 120 | ssize_t bytes_read = 0; |
| 121 | size_t total_bytes_read = 0; |
| 122 | int fd = -1, ret = -1; |
| 123 | |
| 124 | if (buf == NULL) |
| 125 | goto end; |
| 126 | |
| 127 | fd = open("/sys/devices/system/cpu/possible", O_RDONLY); |
| 128 | if (fd < 0) |
| 129 | goto end; |
| 130 | |
| 131 | do { |
| 132 | bytes_read = read(fd, buf + total_bytes_read, |
| 133 | max_bytes - total_bytes_read); |
| 134 | |
| 135 | if (bytes_read < 0) { |
| 136 | if (errno == EINTR) { |
| 137 | continue; /* retry operation */ |
| 138 | } else { |
| 139 | goto end; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | total_bytes_read += bytes_read; |
| 144 | assert(total_bytes_read <= max_bytes); |
| 145 | } while (max_bytes > total_bytes_read && bytes_read > 0); |
| 146 | |
| 147 | /* |
| 148 | * Make sure the mask read is a null terminated string. |
| 149 | */ |
| 150 | if (total_bytes_read < max_bytes) |
| 151 | buf[total_bytes_read] = '\0'; |
| 152 | else |
| 153 | buf[max_bytes - 1] = '\0'; |
| 154 | |
| 155 | if (total_bytes_read > INT_MAX) |
| 156 | goto end; |
| 157 | |
| 158 | ret = (int) total_bytes_read; |
| 159 | |
| 160 | end: |
| 161 | if (fd >= 0 && close(fd) < 0) |
| 162 | PERROR("close"); |
| 163 | |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * Get the number of CPUs from the possible cpu mask. |
| 169 | * |
| 170 | * pmask: the mask to parse. |
| 171 | * len: the len of the mask excluding '\0'. |
| 172 | * |
| 173 | * Returns the number of possible CPUs from the mask or 0 on error. |
| 174 | */ |
| 175 | int get_num_possible_cpus_from_mask(const char *pmask, size_t len) |
| 176 | { |
| 177 | ssize_t i; |
| 178 | unsigned long cpu_index; |
| 179 | char *endptr; |
| 180 | |
| 181 | /* We need at least one char to read */ |
| 182 | if (len < 1) |
| 183 | goto error; |
| 184 | |
| 185 | /* Start from the end to read the last CPU index. */ |
| 186 | for (i = len - 1; i > 0; i--) { |
| 187 | /* Break when we hit the first separator. */ |
| 188 | if ((pmask[i] == ',') || (pmask[i] == '-')) { |
| 189 | i++; |
| 190 | break; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | cpu_index = strtoul(&pmask[i], &endptr, 10); |
| 195 | |
| 196 | if ((&pmask[i] != endptr) && (cpu_index < INT_MAX)) |
| 197 | return (int) cpu_index + 1; |
| 198 | |
| 199 | error: |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | void _get_num_possible_cpus(void) |
| 204 | { |
| 205 | int ret; |
| 206 | char buf[LTTNG_UST_CPUMASK_SIZE]; |
| 207 | |
| 208 | /* Get the possible cpu mask from sysfs, fallback to sysconf. */ |
| 209 | ret = get_possible_cpu_mask_from_sysfs((char *) &buf, LTTNG_UST_CPUMASK_SIZE); |
| 210 | if (ret <= 0) |
| 211 | goto fallback; |
| 212 | |
| 213 | /* Parse the possible cpu mask, on failure fallback to sysconf. */ |
| 214 | ret = get_num_possible_cpus_from_mask((char *) &buf, ret); |
| 215 | if (ret > 0) |
| 216 | goto end; |
| 217 | |
| 218 | fallback: |
| 219 | /* Fallback to sysconf. */ |
| 220 | ret = get_num_possible_cpus_fallback(); |
| 221 | |
| 222 | end: |
| 223 | /* If all methods failed, don't store the value. */ |
| 224 | if (ret < 1) |
| 225 | return; |
| 226 | |
| 227 | __num_possible_cpus = ret; |
| 228 | } |