From 74cc1f594e05b5a3524529f018d85efaad53f9a3 Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Thu, 8 Apr 2021 12:04:20 -0400 Subject: [PATCH] Remove duplicated 'smp' code libcounter contains a namespaced copy of the smp code from libringbuffer, remove it and move the original to libcommon. Also add comments and hide some implementation details following the ABI cleanup. Change-Id: Ie7d78d4f34312c5c788abc07d1806de3ce6fc04c Signed-off-by: Michael Jeanson Signed-off-by: Mathieu Desnoyers --- src/common/Makefile.am | 8 +- src/common/counter/counter.c | 26 +++--- src/common/counter/smp.h | 32 ------- src/common/ringbuffer/frontend.h | 2 +- src/common/ringbuffer/frontend_api.h | 1 + src/common/ringbuffer/ring_buffer_backend.c | 2 +- src/common/ringbuffer/ring_buffer_frontend.c | 2 +- src/common/ringbuffer/smp.c | 96 -------------------- src/common/ringbuffer/smp.h | 35 ------- src/common/{counter => }/smp.c | 30 ++++-- src/common/smp.h | 21 +++++ src/lib/lttng-ust-ctl/ustctl.c | 4 +- 12 files changed, 66 insertions(+), 193 deletions(-) delete mode 100644 src/common/counter/smp.h delete mode 100644 src/common/ringbuffer/smp.c delete mode 100644 src/common/ringbuffer/smp.h rename src/common/{counter => }/smp.c (77%) create mode 100644 src/common/smp.h diff --git a/src/common/Makefile.am b/src/common/Makefile.am index 078ec9ee..a53e4ce7 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -69,9 +69,7 @@ libcounter_la_SOURCES = \ counter/shm.c \ counter/shm.h \ counter/shm_internal.h \ - counter/shm_types.h \ - counter/smp.c \ - counter/smp.h + counter/shm_types.h libcounter_la_LIBADD = -lrt @@ -112,8 +110,6 @@ libringbuffer_la_SOURCES = \ ringbuffer/shm.h \ ringbuffer/shm_internal.h \ ringbuffer/shm_types.h \ - ringbuffer/smp.c \ - ringbuffer/smp.h \ ringbuffer/vatomic.h libringbuffer_la_LIBADD = \ @@ -146,6 +142,8 @@ libcommon_la_SOURCES = \ getenv.h \ logging.c \ logging.h \ + smp.c \ + smp.h \ strutils.c \ strutils.h \ patient.c diff --git a/src/common/counter/counter.c b/src/common/counter/counter.c index 0393bed3..b71b43ae 100644 --- a/src/common/counter/counter.c +++ b/src/common/counter/counter.c @@ -16,7 +16,7 @@ #include "common/align.h" #include "common/bitmap.h" -#include "smp.h" +#include "common/smp.h" #include "shm.h" static size_t lttng_counter_get_dimension_nr_elements(struct lib_counter_dimension *dimension) @@ -118,7 +118,7 @@ int lttng_counter_set_cpu_shm(struct lib_counter *counter, int cpu, int fd) struct lib_counter_config *config = &counter->config; struct lib_counter_layout *layout; - if (cpu < 0 || cpu >= lttng_counter_num_possible_cpus()) + if (cpu < 0 || cpu >= num_possible_cpus()) return -EINVAL; if (!(config->alloc & COUNTER_ALLOC_PER_CPU)) @@ -171,7 +171,7 @@ int validate_args(const struct lib_counter_config *config, int nr_counter_cpu_fds, const int *counter_cpu_fds) { - int nr_cpus = lttng_counter_num_possible_cpus(); + int nr_cpus = num_possible_cpus(); if (CAA_BITS_PER_LONG != 64 && config->counter_size == COUNTER_SIZE_64_BIT) { WARN_ON_ONCE(1); @@ -210,7 +210,7 @@ struct lib_counter *lttng_counter_create(const struct lib_counter_config *config size_t dimension, nr_elem = 1; int cpu, ret; int nr_handles = 0; - int nr_cpus = lttng_counter_num_possible_cpus(); + int nr_cpus = num_possible_cpus(); if (validate_args(config, nr_dimensions, max_nr_elem, global_sum_step, global_counter_fd, nr_counter_cpu_fds, @@ -234,7 +234,7 @@ struct lib_counter *lttng_counter_create(const struct lib_counter_config *config counter->percpu_counters = zmalloc(sizeof(struct lib_counter_layout) * nr_cpus); if (!counter->percpu_counters) goto error_alloc_percpu; - lttng_counter_for_each_possible_cpu(cpu) + for_each_possible_cpu(cpu) counter->percpu_counters[cpu].shm_fd = -1; } @@ -260,7 +260,7 @@ struct lib_counter *lttng_counter_create(const struct lib_counter_config *config goto layout_init_error; } if ((config->alloc & COUNTER_ALLOC_PER_CPU) && counter_cpu_fds) { - lttng_counter_for_each_possible_cpu(cpu) { + for_each_possible_cpu(cpu) { ret = lttng_counter_layout_init(counter, cpu, counter_cpu_fds[cpu]); if (ret) goto layout_init_error; @@ -309,7 +309,7 @@ int lttng_counter_get_cpu_shm(struct lib_counter *counter, int cpu, int *fd, siz struct lib_counter_layout *layout; int shm_fd; - if (cpu >= lttng_counter_num_possible_cpus()) + if (cpu >= num_possible_cpus()) return -1; layout = &counter->percpu_counters[cpu]; shm_fd = layout->shm_fd; @@ -335,13 +335,13 @@ int lttng_counter_read(const struct lib_counter_config *config, switch (config->alloc) { case COUNTER_ALLOC_PER_CPU: - if (cpu < 0 || cpu >= lttng_counter_num_possible_cpus()) + if (cpu < 0 || cpu >= num_possible_cpus()) return -EINVAL; layout = &counter->percpu_counters[cpu]; break; case COUNTER_ALLOC_PER_CPU | COUNTER_ALLOC_GLOBAL: if (cpu >= 0) { - if (cpu >= lttng_counter_num_possible_cpus()) + if (cpu >= num_possible_cpus()) return -EINVAL; layout = &counter->percpu_counters[cpu]; } else { @@ -430,7 +430,7 @@ int lttng_counter_aggregate(const struct lib_counter_config *config, break; case COUNTER_ALLOC_PER_CPU | COUNTER_ALLOC_GLOBAL: /* Fallthrough */ case COUNTER_ALLOC_PER_CPU: - lttng_counter_for_each_possible_cpu(cpu) { + for_each_possible_cpu(cpu) { int64_t old = sum; ret = lttng_counter_read(config, counter, dimension_indexes, @@ -469,13 +469,13 @@ int lttng_counter_clear_cpu(const struct lib_counter_config *config, switch (config->alloc) { case COUNTER_ALLOC_PER_CPU: - if (cpu < 0 || cpu >= lttng_counter_num_possible_cpus()) + if (cpu < 0 || cpu >= num_possible_cpus()) return -EINVAL; layout = &counter->percpu_counters[cpu]; break; case COUNTER_ALLOC_PER_CPU | COUNTER_ALLOC_GLOBAL: if (cpu >= 0) { - if (cpu >= lttng_counter_num_possible_cpus()) + if (cpu >= num_possible_cpus()) return -EINVAL; layout = &counter->percpu_counters[cpu]; } else { @@ -551,7 +551,7 @@ int lttng_counter_clear(const struct lib_counter_config *config, switch (config->alloc) { case COUNTER_ALLOC_PER_CPU: /* Fallthrough */ case COUNTER_ALLOC_PER_CPU | COUNTER_ALLOC_GLOBAL: - lttng_counter_for_each_possible_cpu(cpu) { + for_each_possible_cpu(cpu) { ret = lttng_counter_clear_cpu(config, counter, dimension_indexes, cpu); if (ret < 0) return ret; diff --git a/src/common/counter/smp.h b/src/common/counter/smp.h deleted file mode 100644 index 38eae489..00000000 --- a/src/common/counter/smp.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * SPDX-License-Identifier: LGPL-2.1-only - * - * Copyright (C) 2011-2012 Mathieu Desnoyers - */ - -#ifndef _LIBCOUNTER_SMP_H -#define _LIBCOUNTER_SMP_H - -/* - * 4kB of per-cpu data available. - */ -#define LTTNG_COUNTER_PER_CPU_MEM_SIZE 4096 - -extern int __lttng_counter_num_possible_cpus - __attribute__((visibility("hidden"))); - -extern void _lttng_counter_get_num_possible_cpus(void) - __attribute__((visibility("hidden"))); - -static inline -int lttng_counter_num_possible_cpus(void) -{ - if (!__lttng_counter_num_possible_cpus) - _lttng_counter_get_num_possible_cpus(); - return __lttng_counter_num_possible_cpus; -} - -#define lttng_counter_for_each_possible_cpu(cpu) \ - for ((cpu) = 0; (cpu) < lttng_counter_num_possible_cpus(); (cpu)++) - -#endif /* _LIBCOUNTER_SMP_H */ diff --git a/src/common/ringbuffer/frontend.h b/src/common/ringbuffer/frontend.h index cc9d80d5..88674180 100644 --- a/src/common/ringbuffer/frontend.h +++ b/src/common/ringbuffer/frontend.h @@ -17,7 +17,7 @@ #include #include -#include "smp.h" +#include "common/smp.h" /* Internal helpers */ #include "frontend_internal.h" diff --git a/src/common/ringbuffer/frontend_api.h b/src/common/ringbuffer/frontend_api.h index 4cf7031b..36370042 100644 --- a/src/common/ringbuffer/frontend_api.h +++ b/src/common/ringbuffer/frontend_api.h @@ -15,6 +15,7 @@ #include +#include "lib/lttng-ust/getcpu.h" #include "frontend.h" /** diff --git a/src/common/ringbuffer/ring_buffer_backend.c b/src/common/ringbuffer/ring_buffer_backend.c index 5a998742..27c335d5 100644 --- a/src/common/ringbuffer/ring_buffer_backend.c +++ b/src/common/ringbuffer/ring_buffer_backend.c @@ -18,7 +18,7 @@ #include "vatomic.h" #include "backend.h" #include "frontend.h" -#include "smp.h" +#include "common/smp.h" #include "shm.h" #include "common/align.h" diff --git a/src/common/ringbuffer/ring_buffer_frontend.c b/src/common/ringbuffer/ring_buffer_frontend.c index 5a9117f6..d6f5365c 100644 --- a/src/common/ringbuffer/ring_buffer_frontend.c +++ b/src/common/ringbuffer/ring_buffer_frontend.c @@ -55,7 +55,7 @@ #include #include -#include "smp.h" +#include "common/smp.h" #include "ringbuffer-config.h" #include "vatomic.h" #include "backend.h" diff --git a/src/common/ringbuffer/smp.c b/src/common/ringbuffer/smp.c deleted file mode 100644 index 39bd5559..00000000 --- a/src/common/ringbuffer/smp.c +++ /dev/null @@ -1,96 +0,0 @@ -/* - * SPDX-License-Identifier: LGPL-2.1-only - * - * Copyright (C) 2011-2012 Mathieu Desnoyers - * Copyright (C) 2019 Michael Jeanson - */ - -#define _LGPL_SOURCE -#include -#include -#include "smp.h" - -int __num_possible_cpus; - -#if (defined(__GLIBC__) || defined( __UCLIBC__)) -void _get_num_possible_cpus(void) -{ - int result; - - /* 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. - */ - result = sysconf(_SC_NPROCESSORS_CONF); - if (result == -1) - 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 diff --git a/src/common/ringbuffer/smp.h b/src/common/ringbuffer/smp.h deleted file mode 100644 index 8a96d3d7..00000000 --- a/src/common/ringbuffer/smp.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * SPDX-License-Identifier: LGPL-2.1-only - * - * Copyright (C) 2011-2012 Mathieu Desnoyers - */ - -#ifndef _LIBRINGBUFFER_SMP_H -#define _LIBRINGBUFFER_SMP_H - -#include "lib/lttng-ust/getcpu.h" - -/* - * 4kB of per-cpu data available. Enough to hold the control structures, - * but not ring buffers. - */ -#define PER_CPU_MEM_SIZE 4096 - -extern int __num_possible_cpus - __attribute__((visibility("hidden"))); - -extern void _get_num_possible_cpus(void) - __attribute__((visibility("hidden"))); - -static inline -int num_possible_cpus(void) -{ - if (!__num_possible_cpus) - _get_num_possible_cpus(); - return __num_possible_cpus; -} - -#define for_each_possible_cpu(cpu) \ - for ((cpu) = 0; (cpu) < num_possible_cpus(); (cpu)++) - -#endif /* _LIBRINGBUFFER_SMP_H */ diff --git a/src/common/counter/smp.c b/src/common/smp.c similarity index 77% rename from src/common/counter/smp.c rename to src/common/smp.c index edec4a4b..95f6dd14 100644 --- a/src/common/counter/smp.c +++ b/src/common/smp.c @@ -6,15 +6,17 @@ */ #define _LGPL_SOURCE - #include #include -#include "smp.h" -int __lttng_counter_num_possible_cpus; +#include + +#include "common/smp.h" + +static int num_possible_cpus_cache; #if (defined(__GLIBC__) || defined( __UCLIBC__)) -void _lttng_counter_get_num_possible_cpus(void) +static void _get_num_possible_cpus(void) { int result; @@ -29,7 +31,7 @@ void _lttng_counter_get_num_possible_cpus(void) result = sysconf(_SC_NPROCESSORS_CONF); if (result == -1) return; - __lttng_counter_num_possible_cpus = result; + num_possible_cpus_cache = result; } #else @@ -52,7 +54,7 @@ void _lttng_counter_get_num_possible_cpus(void) #define __max(a,b) ((a)>(b)?(a):(b)) -void _lttng_counter_get_num_possible_cpus(void) +static void _get_num_possible_cpus(void) { int result, count = 0; DIR *cpudir; @@ -92,6 +94,20 @@ end: */ if (result < 1) return; - __lttng_counter_num_possible_cpus = result; + num_possible_cpus_cache = result; } #endif + +/* + * Returns the total number of CPUs in the system. If the cache is not yet + * initialized, get the value from the system through sysconf and cache it. + * + * If the sysconf call fails, don't populate the cache and return 0. + */ +int num_possible_cpus(void) +{ + if (caa_unlikely(!num_possible_cpus_cache)) + _get_num_possible_cpus(); + + return num_possible_cpus_cache; +} diff --git a/src/common/smp.h b/src/common/smp.h new file mode 100644 index 00000000..563fe623 --- /dev/null +++ b/src/common/smp.h @@ -0,0 +1,21 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-only + * + * Copyright (C) 2011-2012 Mathieu Desnoyers + */ + +#ifndef _UST_COMMON_SMP_H +#define _UST_COMMON_SMP_H + +/* + * Returns the total number of CPUs in the system. If the cache is not yet + * initialized, get the value from the system through sysconf and cache it. + * + * If the sysconf call fails, don't populate the cache and return 0. + */ +int num_possible_cpus(void); + +#define for_each_possible_cpu(cpu) \ + for ((cpu) = 0; (cpu) < num_possible_cpus(); (cpu)++) + +#endif /* _UST_COMMON_SMP_H */ diff --git a/src/lib/lttng-ust-ctl/ustctl.c b/src/lib/lttng-ust-ctl/ustctl.c index 4b0e3f76..817f89e4 100644 --- a/src/lib/lttng-ust-ctl/ustctl.c +++ b/src/lib/lttng-ust-ctl/ustctl.c @@ -32,7 +32,7 @@ #include "lib/lttng-ust/lttng-tracer-core.h" #include "lib/lttng-ust/lttng-counter-client.h" -#include "common/counter/smp.h" +#include "common/smp.h" #include "common/counter/counter.h" /* @@ -2534,7 +2534,7 @@ int ustctl_regenerate_statedump(int sock, int handle) int ustctl_get_nr_cpu_per_counter(void) { - return lttng_counter_num_possible_cpus(); + return num_possible_cpus(); } struct ustctl_daemon_counter * -- 2.34.1