Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1 | From 5de7c318804a7b1edce8562d4891b4c74aac0677 Mon Sep 17 00:00:00 2001 |
| 2 | From: Michael Jeanson <mjeanson@efficios.com> |
| 3 | Date: Wed, 20 Mar 2019 11:07:35 -0400 |
| 4 | Subject: [PATCH] compat: work around broken _SC_NPROCESSORS_CONF on MUSL libc |
| 5 | |
| 6 | On MUSL libc the _SC_NPROCESSORS_CONF sysconf will report the number of |
| 7 | CPUs allocated to the task based on the affinity mask instead of the |
| 8 | total number of CPUs configured on the system. |
| 9 | |
| 10 | Upstream-Status: Accepted [1] [5de7c318804a7b1edce8562d4891b4c74aac0677] |
| 11 | [1] https://lists.lttng.org/pipermail/lttng-dev/2019-March/028616.html |
| 12 | |
| 13 | Signed-off-by: Michael Jeanson <mjeanson@efficios.com> |
| 14 | Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 15 | --- |
| 16 | libringbuffer/smp.c | 66 +++++++++++++++++++++++++++++++++++++++++++++ |
| 17 | 1 file changed, 66 insertions(+) |
| 18 | |
| 19 | diff --git a/libringbuffer/smp.c b/libringbuffer/smp.c |
| 20 | index 9e7114be..656a75da 100644 |
| 21 | --- a/libringbuffer/smp.c |
| 22 | +++ b/libringbuffer/smp.c |
| 23 | @@ -2,6 +2,7 @@ |
| 24 | * libringbuffer/smp.c |
| 25 | * |
| 26 | * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 27 | + * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com> |
| 28 | * |
| 29 | * This library is free software; you can redistribute it and/or |
| 30 | * modify it under the terms of the GNU Lesser General Public |
| 31 | @@ -26,6 +27,7 @@ |
| 32 | |
| 33 | int __num_possible_cpus; |
| 34 | |
| 35 | +#if (defined(__GLIBC__) || defined( __UCLIBC__)) |
| 36 | void _get_num_possible_cpus(void) |
| 37 | { |
| 38 | int result; |
| 39 | @@ -43,3 +45,67 @@ void _get_num_possible_cpus(void) |
| 40 | return; |
| 41 | __num_possible_cpus = result; |
| 42 | } |
| 43 | + |
| 44 | +#else |
| 45 | + |
| 46 | +/* |
| 47 | + * The MUSL libc implementation of the _SC_NPROCESSORS_CONF sysconf does not |
| 48 | + * return the number of configured CPUs in the system but relies on the cpu |
| 49 | + * affinity mask of the current task. |
| 50 | + * |
| 51 | + * So instead we use a strategy similar to GLIBC's, counting the cpu |
| 52 | + * directories in "/sys/devices/system/cpu" and fallback on the value from |
| 53 | + * sysconf if it fails. |
| 54 | + */ |
| 55 | + |
| 56 | +#include <dirent.h> |
| 57 | +#include <limits.h> |
| 58 | +#include <stdlib.h> |
| 59 | +#include <string.h> |
| 60 | +#include <sys/types.h> |
| 61 | + |
| 62 | +#define __max(a,b) ((a)>(b)?(a):(b)) |
| 63 | + |
| 64 | +void _get_num_possible_cpus(void) |
| 65 | +{ |
| 66 | + int result, count = 0; |
| 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 | + * Count the number of directories named "cpu" followed by and |
| 76 | + * integer. This is the same strategy as glibc uses. |
| 77 | + */ |
| 78 | + while ((entry = readdir(cpudir))) { |
| 79 | + if (entry->d_type == DT_DIR && |
| 80 | + strncmp(entry->d_name, "cpu", 3) == 0) { |
| 81 | + |
| 82 | + char *endptr; |
| 83 | + unsigned long cpu_num; |
| 84 | + |
| 85 | + cpu_num = strtoul(entry->d_name + 3, &endptr, 10); |
| 86 | + if ((cpu_num < ULONG_MAX) && (endptr != entry->d_name + 3) |
| 87 | + && (*endptr == '\0')) { |
| 88 | + count++; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | +end: |
| 94 | + /* |
| 95 | + * Get the sysconf value as a fallback. Keep the highest number. |
| 96 | + */ |
| 97 | + result = __max(sysconf(_SC_NPROCESSORS_CONF), count); |
| 98 | + |
| 99 | + /* |
| 100 | + * If both methods failed, don't store the value. |
| 101 | + */ |
| 102 | + if (result < 1) |
| 103 | + return; |
| 104 | + __num_possible_cpus = result; |
| 105 | +} |
| 106 | +#endif |
| 107 | -- |
| 108 | 2.17.1 |
| 109 | |