X-Git-Url: http://git.lttng.org/?a=blobdiff_plain;f=src%2Fcommon%2Fsmp.c;h=8c742028039a10395b099202a741f490bc72fe2d;hb=fb0f6ca9385e1813311cfd917c130b40bec04970;hp=5bcbad8113d82b8971574912d09320db3d2631c3;hpb=217babc9b6b5dcc531fb6b8bc5fc6e2906a060be;p=lttng-ust.git diff --git a/src/common/smp.c b/src/common/smp.c index 5bcbad81..8c742028 100644 --- a/src/common/smp.c +++ b/src/common/smp.c @@ -25,36 +25,32 @@ #define __max(a,b) ((a)>(b)?(a):(b)) -static int num_possible_cpus_cache; +static int possible_cpus_array_len_cache; /* - * 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 + * Get the highest CPU id from sysfs. + * + * 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. - * - * 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. + * Returns the highest CPU id, or -1 on error. */ -int get_num_possible_cpus_fallback(void) +int get_max_cpuid_from_sysfs(void) +{ + return _get_max_cpuid_from_sysfs("/sys/devices/system/cpu"); +} + +int _get_max_cpuid_from_sysfs(const char *path) { long max_cpuid = -1; DIR *cpudir; struct dirent *entry; - cpudir = opendir("/sys/devices/system/cpu"); + assert(path); + + cpudir = opendir(path); if (cpudir == NULL) goto end; @@ -85,10 +81,35 @@ int get_num_possible_cpus_fallback(void) max_cpuid = -1; end: + return max_cpuid; +} + +/* + * 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. + * + * 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. + */ +int get_num_possible_cpus_fallback(void) +{ /* * Get the sysconf value as a last resort. Keep the highest number. */ - return __max(sysconf(_SC_NPROCESSORS_CONF), max_cpuid + 1); + return __max(sysconf(_SC_NPROCESSORS_CONF), get_max_cpuid_from_sysfs() + 1); } /* @@ -100,15 +121,32 @@ end: * Returns the number of bytes read or -1 on error. */ int get_possible_cpu_mask_from_sysfs(char *buf, size_t max_bytes) +{ + return get_cpu_mask_from_sysfs(buf, max_bytes, + "/sys/devices/system/cpu/possible"); +} + +/* + * Get a CPU mask string from sysfs. + * + * buf: the buffer where the mask will be read. + * max_bytes: the maximum number of bytes to write in the buffer. + * path: file path to read the mask from. + * + * Returns the number of bytes read or -1 on error. + */ +int get_cpu_mask_from_sysfs(char *buf, size_t max_bytes, const char *path) { ssize_t bytes_read = 0; size_t total_bytes_read = 0; int fd = -1, ret = -1; + assert(path); + if (buf == NULL) goto end; - fd = open("/sys/devices/system/cpu/possible", O_RDONLY); + fd = open(path, O_RDONLY); if (fd < 0) goto end; @@ -146,14 +184,14 @@ end: } /* - * Get the number of CPUs from the possible cpu mask. + * Get the highest CPU id from a CPU mask. * * pmask: the mask to parse. * len: the len of the mask excluding '\0'. * - * Returns the number of possible CPUs from the mask or 0 on error. + * Returns the highest CPU id from the mask or -1 on error. */ -int get_num_possible_cpus_from_mask(const char *pmask, size_t len) +int get_max_cpuid_from_mask(const char *pmask, size_t len) { ssize_t i; unsigned long cpu_index; @@ -179,13 +217,13 @@ int get_num_possible_cpus_from_mask(const char *pmask, size_t len) * CPUs. */ if ((&pmask[i] != endptr) && (cpu_index < INT_MAX)) - return (int) cpu_index + 1; + return (int) cpu_index; error: - return 0; + return -1; } -static void _get_num_possible_cpus(void) +static void update_possible_cpus_array_len_cache(void) { int ret; char buf[LTTNG_UST_CPUMASK_SIZE]; @@ -196,9 +234,12 @@ static void _get_num_possible_cpus(void) goto fallback; /* Parse the possible cpu mask, on failure fallback to sysconf. */ - ret = get_num_possible_cpus_from_mask((char *) &buf, ret); - if (ret > 0) + ret = get_max_cpuid_from_mask((char *) &buf, ret); + if (ret >= 0) { + /* Add 1 to convert from max cpuid to an array len. */ + ret++; goto end; + } fallback: /* Fallback to sysconf. */ @@ -209,20 +250,24 @@ end: if (ret < 1) return; - num_possible_cpus_cache = ret; + possible_cpus_array_len_cache = ret; } /* - * Returns the total number of CPUs in the system. If the cache is not yet - * initialized, get the value from "/sys/devices/system/cpu/possible" or - * fallback to sysconf and cache it. + * Returns the length of an array that could contain a per-CPU element for each + * possible CPU id for the lifetime of the process. + * + * We currently assume CPU ids are contiguous up the maximum CPU id. + * + * If the cache is not yet initialized, get the value from + * "/sys/devices/system/cpu/possible" or fallback to sysconf and cache it. * * If all methods fail, don't populate the cache and return 0. */ -int num_possible_cpus(void) +int get_possible_cpus_array_len(void) { - if (caa_unlikely(!num_possible_cpus_cache)) - _get_num_possible_cpus(); + if (caa_unlikely(!possible_cpus_array_len_cache)) + update_possible_cpus_array_len_cache(); - return num_possible_cpus_cache; + return possible_cpus_array_len_cache; }