X-Git-Url: http://git.lttng.org/?a=blobdiff_plain;ds=sidebyside;f=libringbuffer%2Fsmp.c;h=39bd5559094e75b6305e96f13e8002312f7861c5;hb=221309e3604c231925b2441e41e29c5fed80dc4d;hp=3f86ac86c954fc2685998ac7dcab58cdadb01ae2;hpb=a6352fd40a2090fd883a6c369144bf405c9e9ec4;p=lttng-ust.git diff --git a/libringbuffer/smp.c b/libringbuffer/smp.c index 3f86ac86..39bd5559 100644 --- a/libringbuffer/smp.c +++ b/libringbuffer/smp.c @@ -1,19 +1,18 @@ /* - * libust/smp.c + * SPDX-License-Identifier: LGPL-2.1-only * - * Copyright 2011 (c) - Mathieu Desnoyers - * - * Dual LGPL v2.1/GPL v2 license. + * Copyright (C) 2011-2012 Mathieu Desnoyers + * Copyright (C) 2019 Michael Jeanson */ +#define _LGPL_SOURCE #include -#include "ust/core.h" -#include "usterr.h" #include #include "smp.h" int __num_possible_cpus; +#if (defined(__GLIBC__) || defined( __UCLIBC__)) void _get_num_possible_cpus(void) { int result; @@ -31,3 +30,67 @@ void _get_num_possible_cpus(void) return; __num_possible_cpus = result; } + +#else + +/* + * 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. + * + * 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. + */ + +#include +#include +#include +#include +#include + +#define __max(a,b) ((a)>(b)?(a):(b)) + +void _get_num_possible_cpus(void) +{ + int result, count = 0; + DIR *cpudir; + struct dirent *entry; + + cpudir = opendir("/sys/devices/system/cpu"); + if (cpudir == NULL) + goto end; + + /* + * Count the number of directories named "cpu" followed by and + * integer. This is the same strategy as glibc uses. + */ + while ((entry = readdir(cpudir))) { + if (entry->d_type == DT_DIR && + strncmp(entry->d_name, "cpu", 3) == 0) { + + char *endptr; + unsigned long cpu_num; + + cpu_num = strtoul(entry->d_name + 3, &endptr, 10); + if ((cpu_num < ULONG_MAX) && (endptr != entry->d_name + 3) + && (*endptr == '\0')) { + count++; + } + } + } + +end: + /* + * Get the sysconf value as a fallback. Keep the highest number. + */ + result = __max(sysconf(_SC_NPROCESSORS_CONF), count); + + /* + * If both methods failed, don't store the value. + */ + if (result < 1) + return; + __num_possible_cpus = result; +} +#endif