X-Git-Url: http://git.lttng.org/?a=blobdiff_plain;f=src%2Fcommon%2Fsmp.c;h=5bcbad8113d82b8971574912d09320db3d2631c3;hb=217babc9b6b5dcc531fb6b8bc5fc6e2906a060be;hp=a4346591c4595c0682e757ed700c724f6c74b99b;hpb=66dbdc3448a77043d0fd59f47b17e77a8d59fddb;p=lttng-ust.git diff --git a/src/common/smp.c b/src/common/smp.c index a4346591..5bcbad81 100644 --- a/src/common/smp.c +++ b/src/common/smp.c @@ -8,57 +8,49 @@ #define _LGPL_SOURCE #include #include +#include #include #include +#include #include #include #include +#include +#include #include -#include "common/align.h" #include "common/logging.h" #include "common/smp.h" -static int num_possible_cpus_cache; - -#if (defined(__GLIBC__) || defined( __UCLIBC__)) -int get_num_possible_cpus_fallback(void) -{ - /* On Linux, when some processors are offline - * _SC_NPROCESSORS_CONF counts the offline - * processors, whereas _SC_NPROCESSORS_ONLN - * does not. If we used _SC_NPROCESSORS_ONLN, - * getcpu() could return a value greater than - * this sysconf, in which case the arrays - * indexed by processor would overflow. - */ - return sysconf(_SC_NPROCESSORS_CONF); -} +#define __max(a,b) ((a)>(b)?(a):(b)) -#else +static int num_possible_cpus_cache; /* - * The MUSL libc implementation of the _SC_NPROCESSORS_CONF sysconf does not - * return the number of configured CPUs in the system but relies on the cpu - * affinity mask of the current task. + * As a fallback to parsing the CPU mask in "/sys/devices/system/cpu/possible", + * iterate on all the folders in "/sys/devices/system/cpu" that start with + * "cpu" followed by an integer, keep the highest CPU id encountered during + * this iteration and add 1 to get a number of CPUs. + * + * Then get the value from sysconf(_SC_NPROCESSORS_CONF) as a fallback and + * return the highest one. + * + * On Linux, using the value from sysconf can be unreliable since the way it + * counts CPUs varies between C libraries and even between versions of the same + * library. If we used it directly, getcpu() could return a value greater than + * this sysconf, in which case the arrays indexed by processor would overflow. * - * So instead we use a strategy similar to GLIBC's, counting the cpu - * directories in "/sys/devices/system/cpu" and fallback on the value from - * sysconf if it fails. + * As another example, the MUSL libc implementation of the _SC_NPROCESSORS_CONF + * sysconf does not return the number of configured CPUs in the system but + * relies on the cpu affinity mask of the current task. + * + * Returns 0 or less on error. */ - -#include -#include -#include -#include -#include - -#define __max(a,b) ((a)>(b)?(a):(b)) - int get_num_possible_cpus_fallback(void) { - int count = 0; + long max_cpuid = -1; + DIR *cpudir; struct dirent *entry; @@ -67,31 +59,37 @@ int get_num_possible_cpus_fallback(void) goto end; /* - * Count the number of directories named "cpu" followed by and - * integer. This is the same strategy as glibc uses. + * Iterate on all directories named "cpu" followed by an integer. */ while ((entry = readdir(cpudir))) { if (entry->d_type == DT_DIR && strncmp(entry->d_name, "cpu", 3) == 0) { char *endptr; - unsigned long cpu_num; + long cpu_id; - cpu_num = strtoul(entry->d_name + 3, &endptr, 10); - if ((cpu_num < ULONG_MAX) && (endptr != entry->d_name + 3) + cpu_id = strtol(entry->d_name + 3, &endptr, 10); + if ((cpu_id < LONG_MAX) && (endptr != entry->d_name + 3) && (*endptr == '\0')) { - count++; + if (cpu_id > max_cpuid) + max_cpuid = cpu_id; } } } + /* + * If the max CPU id is out of bound, set it to -1 so it results in a + * CPU num of 0. + */ + if (max_cpuid < 0 || max_cpuid > INT_MAX) + max_cpuid = -1; + end: /* - * Get the sysconf value as a fallback. Keep the highest number. + * Get the sysconf value as a last resort. Keep the highest number. */ - return __max(sysconf(_SC_NPROCESSORS_CONF), count); + return __max(sysconf(_SC_NPROCESSORS_CONF), max_cpuid + 1); } -#endif /* * Get the CPU possible mask string from sysfs. @@ -105,14 +103,14 @@ int get_possible_cpu_mask_from_sysfs(char *buf, size_t max_bytes) { ssize_t bytes_read = 0; size_t total_bytes_read = 0; - int fd = 0; + int fd = -1, ret = -1; if (buf == NULL) - return -1; + goto end; fd = open("/sys/devices/system/cpu/possible", O_RDONLY); if (fd < 0) - return -1; + goto end; do { bytes_read = read(fd, buf + total_bytes_read, @@ -122,7 +120,7 @@ int get_possible_cpu_mask_from_sysfs(char *buf, size_t max_bytes) if (errno == EINTR) { continue; /* retry operation */ } else { - return -1; + goto end; } } @@ -130,9 +128,6 @@ int get_possible_cpu_mask_from_sysfs(char *buf, size_t max_bytes) assert(total_bytes_read <= max_bytes); } while (max_bytes > total_bytes_read && bytes_read > 0); - if (close(fd)) - PERROR("close"); - /* * Make sure the mask read is a null terminated string. */ @@ -141,7 +136,13 @@ int get_possible_cpu_mask_from_sysfs(char *buf, size_t max_bytes) else buf[max_bytes - 1] = '\0'; - return total_bytes_read; + if (total_bytes_read > INT_MAX) + goto end; + ret = (int) total_bytes_read; +end: + if (fd >= 0 && close(fd) < 0) + PERROR("close"); + return ret; } /* @@ -187,11 +188,10 @@ error: static void _get_num_possible_cpus(void) { int ret; - int buf_len = LTTNG_UST_PAGE_SIZE; - char buf[buf_len]; + char buf[LTTNG_UST_CPUMASK_SIZE]; /* Get the possible cpu mask from sysfs, fallback to sysconf. */ - ret = get_possible_cpu_mask_from_sysfs((char *) &buf, buf_len); + ret = get_possible_cpu_mask_from_sysfs((char *) &buf, LTTNG_UST_CPUMASK_SIZE); if (ret <= 0) goto fallback;