Remove duplicated 'smp' code
authorMichael Jeanson <mjeanson@efficios.com>
Thu, 8 Apr 2021 16:04:20 +0000 (12:04 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Fri, 16 Apr 2021 15:40:38 +0000 (11:40 -0400)
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 <mjeanson@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
13 files changed:
src/common/Makefile.am
src/common/counter/counter.c
src/common/counter/smp.c [deleted file]
src/common/counter/smp.h [deleted file]
src/common/ringbuffer/frontend.h
src/common/ringbuffer/frontend_api.h
src/common/ringbuffer/ring_buffer_backend.c
src/common/ringbuffer/ring_buffer_frontend.c
src/common/ringbuffer/smp.c [deleted file]
src/common/ringbuffer/smp.h [deleted file]
src/common/smp.c [new file with mode: 0644]
src/common/smp.h [new file with mode: 0644]
src/lib/lttng-ust-ctl/ustctl.c

index 078ec9eefdbf99e611c675e9e797567488052ee7..a53e4ce750404f3dbe7e9c59f64a0bc2049e18fc 100644 (file)
@@ -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
index 0393bed30ab2fe81f9a44c41b695a08bd6e82c34..b71b43ae7c3828b2c317d4fd10d8036e827aa7bc 100644 (file)
@@ -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.c b/src/common/counter/smp.c
deleted file mode 100644 (file)
index edec4a4..0000000
+++ /dev/null
@@ -1,97 +0,0 @@
-/*
- * SPDX-License-Identifier: LGPL-2.1-only
- *
- * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
- */
-
-#define _LGPL_SOURCE
-
-#include <unistd.h>
-#include <pthread.h>
-#include "smp.h"
-
-int __lttng_counter_num_possible_cpus;
-
-#if (defined(__GLIBC__) || defined( __UCLIBC__))
-void _lttng_counter_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;
-       __lttng_counter_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 <dirent.h>
-#include <limits.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-
-#define __max(a,b) ((a)>(b)?(a):(b))
-
-void _lttng_counter_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;
-       __lttng_counter_num_possible_cpus = result;
-}
-#endif
diff --git a/src/common/counter/smp.h b/src/common/counter/smp.h
deleted file mode 100644 (file)
index 38eae48..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * SPDX-License-Identifier: LGPL-2.1-only
- *
- * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- */
-
-#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 */
index cc9d80d5f9a55870579d5c869e645a909582b78c..8867418081300457fcc88e7bd4cd2cbb47c6dfb4 100644 (file)
@@ -17,7 +17,7 @@
 #include <urcu/compiler.h>
 #include <urcu/uatomic.h>
 
-#include "smp.h"
+#include "common/smp.h"
 
 /* Internal helpers */
 #include "frontend_internal.h"
index 4cf7031b5a9e8fb1958265b2c897511b160bb06d..36370042d98cc1f67f68b091aeb588c8b24385d8 100644 (file)
@@ -15,6 +15,7 @@
 
 #include <urcu/compiler.h>
 
+#include "lib/lttng-ust/getcpu.h"
 #include "frontend.h"
 
 /**
index 5a9987428d59a1b8bd6a3e6830b2fbf42a6a555b..27c335d50129aa3594897d1957fe4a237fc6078d 100644 (file)
@@ -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"
 
index 5a9117f6ef053736e12952874cdbc0a3e44cb1c4..d6f5365c8eb1e3629d4a4c829414d13bb00ea8da 100644 (file)
@@ -55,7 +55,7 @@
 #include <lttng/ust-utils.h>
 #include <lttng/ust-ringbuffer-context.h>
 
-#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 (file)
index 39bd555..0000000
+++ /dev/null
@@ -1,96 +0,0 @@
-/*
- * SPDX-License-Identifier: LGPL-2.1-only
- *
- * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
- */
-
-#define _LGPL_SOURCE
-#include <unistd.h>
-#include <pthread.h>
-#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 <dirent.h>
-#include <limits.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-
-#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 (file)
index 8a96d3d..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * SPDX-License-Identifier: LGPL-2.1-only
- *
- * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- */
-
-#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/smp.c b/src/common/smp.c
new file mode 100644 (file)
index 0000000..95f6dd1
--- /dev/null
@@ -0,0 +1,113 @@
+/*
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
+ */
+
+#define _LGPL_SOURCE
+#include <unistd.h>
+#include <pthread.h>
+
+#include <urcu/compiler.h>
+
+#include "common/smp.h"
+
+static int num_possible_cpus_cache;
+
+#if (defined(__GLIBC__) || defined( __UCLIBC__))
+static 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_cache = 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 <dirent.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+
+#define __max(a,b) ((a)>(b)?(a):(b))
+
+static 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_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 (file)
index 0000000..563fe62
--- /dev/null
@@ -0,0 +1,21 @@
+/*
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ */
+
+#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 */
index 4b0e3f76b2e56e3bb8332c1da1d5115ce3a022b7..817f89e426da57f5f91408b076b84eb543928c8a 100644 (file)
@@ -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 *
This page took 0.034643 seconds and 4 git commands to generate.