Move getcpu.h to 'lib/lttng-ust/'
authorMichael Jeanson <mjeanson@efficios.com>
Tue, 6 Apr 2021 16:29:36 +0000 (12:29 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 13 Apr 2021 18:25:12 +0000 (14:25 -0400)
Move getcpu.h from 'common/ringbuffer' to 'lib/lttng-ust', the
implementation is there and it provides some liblttng-ust symbols to
other components unrelated to the ringbuffer.

* Namespace internal symbols for clarity
* Add comments to the code.

Change-Id: I93da098dc9f90aa6324ad1ac36eaba1c660f0f64
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
src/common/Makefile.am
src/common/counter/counter-api.h
src/common/ringbuffer/getcpu.h [deleted file]
src/common/ringbuffer/smp.h
src/lib/lttng-ust/Makefile.am
src/lib/lttng-ust/getcpu.c [new file with mode: 0644]
src/lib/lttng-ust/getcpu.h [new file with mode: 0644]
src/lib/lttng-ust/lttng-context-cpu-id.c
src/lib/lttng-ust/lttng-getcpu.c [deleted file]
src/lib/lttng-ust/lttng-ust-comm.c

index 619276ede2009d95680ee190fc516388d036c2f8..2ab4bfdc6271e63c0fdf37050b87df8f16e4a4af 100644 (file)
@@ -90,7 +90,6 @@ libringbuffer_la_SOURCES = \
        ringbuffer/frontend.h \
        ringbuffer/frontend_internal.h \
        ringbuffer/frontend_types.h \
-       ringbuffer/getcpu.h \
        ringbuffer/nohz.h \
        ringbuffer/rb-init.h \
        ringbuffer/ring_buffer_backend.c \
index 81bb551badeb9e95d5d6050bb9336410df453c1e..3465d5dc98a18084cb7e34f5ca0932ade1fc8e5e 100644 (file)
@@ -16,7 +16,7 @@
 #include <urcu/compiler.h>
 #include <urcu/uatomic.h>
 #include "common/bitmap.h"
-#include "common/ringbuffer/getcpu.h"
+#include "lib/lttng-ust/getcpu.h"
 
 /*
  * Using unsigned arithmetic because overflow is defined.
diff --git a/src/common/ringbuffer/getcpu.h b/src/common/ringbuffer/getcpu.h
deleted file mode 100644 (file)
index 52c7441..0000000
+++ /dev/null
@@ -1,104 +0,0 @@
-/*
- * SPDX-License-Identifier: LGPL-2.1-only
- *
- * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- */
-
-#ifndef _LTTNG_GETCPU_H
-#define _LTTNG_GETCPU_H
-
-#include <urcu/compiler.h>
-#include <urcu/system.h>
-#include <urcu/arch.h>
-
-void lttng_ust_getcpu_init(void)
-       __attribute__((visibility("hidden")));
-
-extern int (*lttng_get_cpu)(void)
-       __attribute__((visibility("hidden")));
-
-#ifdef LTTNG_UST_DEBUG_VALGRIND
-
-/*
- * Fallback on cpu 0 if liblttng-ust is build with Valgrind support.
- * get_cpu() returns the current CPU number. It may change due to
- * migration, so it is only statistically accurate.
- */
-static inline
-int lttng_ust_get_cpu_internal(void)
-{
-       return 0;
-}
-
-#else
-
-/*
- * sched_getcpu.
- */
-#ifdef __linux__
-
-#if !HAVE_SCHED_GETCPU
-#include <sys/syscall.h>
-#define __getcpu(cpu, node, cache)     syscall(__NR_getcpu, cpu, node, cache)
-/*
- * If getcpu is not implemented in the kernel, use cpu 0 as fallback.
- */
-static inline
-int lttng_ust_get_cpu_internal(void)
-{
-       int cpu, ret;
-
-       ret = __getcpu(&cpu, NULL, NULL);
-       if (caa_unlikely(ret < 0))
-               return 0;
-       return cpu;
-}
-#else /* HAVE_SCHED_GETCPU */
-#include <sched.h>
-
-/*
- * If getcpu is not implemented in the kernel, use cpu 0 as fallback.
- */
-static inline
-int lttng_ust_get_cpu_internal(void)
-{
-       int cpu;
-
-       cpu = sched_getcpu();
-       if (caa_unlikely(cpu < 0))
-               return 0;
-       return cpu;
-}
-#endif /* HAVE_SCHED_GETCPU */
-
-#elif (defined(__FreeBSD__) || defined(__CYGWIN__))
-
-/*
- * FreeBSD and Cygwin do not allow query of CPU ID. Always use CPU
- * number 0, with the assocated performance degradation on SMP.
- */
-static inline
-int lttng_ust_get_cpu_internal(void)
-{
-       return 0;
-}
-
-#else
-#error "Please add support for your OS into liblttng-ust/compat.h."
-#endif
-
-#endif
-
-static inline
-int lttng_ust_get_cpu(void)
-{
-       int (*getcpu)(void) = CMM_LOAD_SHARED(lttng_get_cpu);
-
-       if (caa_likely(!getcpu)) {
-               return lttng_ust_get_cpu_internal();
-       } else {
-               return getcpu();
-       }
-}
-
-#endif /* _LTTNG_GETCPU_H */
index 028a66f7844aa47a791db7618469a387e7f999b2..8a96d3d7daeb363d4694b7641b1d54ba3562600c 100644 (file)
@@ -7,7 +7,7 @@
 #ifndef _LIBRINGBUFFER_SMP_H
 #define _LIBRINGBUFFER_SMP_H
 
-#include "getcpu.h"
+#include "lib/lttng-ust/getcpu.h"
 
 /*
  * 4kB of per-cpu data available. Enough to hold the control structures,
index df0ae8d09bda29cbac6875168a34df9f1bebe61f..3548a9e624299ddcc4c94421eff20e5fb1fb0455 100644 (file)
@@ -126,7 +126,8 @@ liblttng_ust_support_la_SOURCES = \
        lttng-counter-client.h \
        lttng-counter-client-percpu-32-modular.c \
        lttng-counter-client-percpu-64-modular.c \
-       lttng-clock.c lttng-getcpu.c
+       lttng-clock.c \
+       getcpu.c getcpu.h
 
 liblttng_ust_la_SOURCES =
 
diff --git a/src/lib/lttng-ust/getcpu.c b/src/lib/lttng-ust/getcpu.c
new file mode 100644 (file)
index 0000000..483f813
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ */
+
+#define _LGPL_SOURCE
+#include <error.h>
+#include <dlfcn.h>
+#include <stdlib.h>
+#include "common/logging.h"
+#include <urcu/system.h>
+#include <urcu/arch.h>
+
+#include "getenv.h"
+#include "lib/lttng-ust/getcpu.h"
+
+/* Function pointer to the current getcpu callback. */
+int (*lttng_ust_get_cpu_sym)(void);
+
+static
+void *getcpu_plugin_handle;
+
+/*
+ * Override the user provided getcpu implementation.
+ */
+int lttng_ust_getcpu_override(int (*getcpu)(void))
+{
+       CMM_STORE_SHARED(lttng_ust_get_cpu_sym, getcpu);
+       return 0;
+}
+
+/*
+ * Initialize the getcpu plugin if it's present.
+ */
+void lttng_ust_getcpu_plugin_init(void)
+{
+       const char *libname;
+       void (*getcpu_plugin_init)(void);
+
+       /* If a plugin is already loaded, do nothing. */
+       if (getcpu_plugin_handle)
+               return;
+
+       /*
+        * If the LTTNG_UST_GETCPU_PLUGIN environment variable is undefined, do
+        * nothing.
+        */
+       libname = lttng_ust_getenv("LTTNG_UST_GETCPU_PLUGIN");
+       if (!libname)
+               return;
+
+       /*
+        * Thy to dlopen the getcpu plugin shared object specified in
+        * LTTNG_UST_GETCPU_PLUGIN.
+        */
+       getcpu_plugin_handle = dlopen(libname, RTLD_NOW);
+       if (!getcpu_plugin_handle) {
+               PERROR("Cannot load LTTng UST getcpu override library %s",
+                       libname);
+               return;
+       }
+       dlerror();
+
+       /* Locate the getcpu plugin init function in the shared object. */
+       getcpu_plugin_init = (void (*)(void)) dlsym(getcpu_plugin_handle,
+               "lttng_ust_getcpu_plugin_init");
+       if (!getcpu_plugin_init) {
+               PERROR("Cannot find LTTng UST getcpu override library %s initialization function lttng_ust_getcpu_plugin_init()",
+                       libname);
+               return;
+       }
+
+       /* Run the user provided getcpu plugin init function. */
+       getcpu_plugin_init();
+}
diff --git a/src/lib/lttng-ust/getcpu.h b/src/lib/lttng-ust/getcpu.h
new file mode 100644 (file)
index 0000000..cac3401
--- /dev/null
@@ -0,0 +1,119 @@
+/*
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ */
+
+#ifndef _LTTNG_GETCPU_H
+#define _LTTNG_GETCPU_H
+
+#include <urcu/compiler.h>
+#include <urcu/system.h>
+#include <urcu/arch.h>
+
+#include <lttng/ust-getcpu.h>
+
+
+/*
+ * Initialize the getcpu plugin if it's present.
+ */
+void lttng_ust_getcpu_plugin_init(void)
+       __attribute__((visibility("hidden")));
+
+/*
+ * Function pointer to the user provided getcpu callback, can be set at library
+ * initialization by a dlopened plugin or at runtime by a user by calling
+ * lttng_ust_getcpu_override() from the public API.
+ */
+extern int (*lttng_ust_get_cpu_sym)(void)
+       __attribute__((visibility("hidden")));
+
+#ifdef LTTNG_UST_DEBUG_VALGRIND
+
+/*
+ * Fallback on cpu 0 if liblttng-ust is build with Valgrind support.
+ * get_cpu() returns the current CPU number. It may change due to
+ * migration, so it is only statistically accurate.
+ */
+static inline
+int lttng_ust_get_cpu_internal(void)
+{
+       return 0;
+}
+
+#else
+
+/*
+ * sched_getcpu.
+ */
+#ifdef __linux__
+
+#if !HAVE_SCHED_GETCPU
+#include <sys/syscall.h>
+#define __getcpu(cpu, node, cache)     syscall(__NR_getcpu, cpu, node, cache)
+/*
+ * If getcpu is not implemented in the kernel, use cpu 0 as fallback.
+ */
+static inline
+int lttng_ust_get_cpu_internal(void)
+{
+       int cpu, ret;
+
+       ret = __getcpu(&cpu, NULL, NULL);
+       if (caa_unlikely(ret < 0))
+               return 0;
+       return cpu;
+}
+#else /* HAVE_SCHED_GETCPU */
+#include <sched.h>
+
+/*
+ * If getcpu is not implemented in the kernel, use cpu 0 as fallback.
+ */
+static inline
+int lttng_ust_get_cpu_internal(void)
+{
+       int cpu;
+
+       cpu = sched_getcpu();
+       if (caa_unlikely(cpu < 0))
+               return 0;
+       return cpu;
+}
+#endif /* HAVE_SCHED_GETCPU */
+
+#elif (defined(__FreeBSD__) || defined(__CYGWIN__))
+
+/*
+ * FreeBSD and Cygwin do not allow query of CPU ID. Always use CPU
+ * number 0, with the assocated performance degradation on SMP.
+ */
+static inline
+int lttng_ust_get_cpu_internal(void)
+{
+       return 0;
+}
+
+#else
+#error "Please add support for your OS into liblttng-ust/compat.h."
+#endif
+
+#endif
+
+static inline
+int lttng_ust_get_cpu(void)
+{
+       int (*lttng_ust_get_cpu_current)(void) = CMM_LOAD_SHARED(lttng_ust_get_cpu_sym);
+
+       /*
+        * Fallback to the internal getcpu implementation if no override was
+        * provided the user.
+        */
+       if (caa_likely(!lttng_ust_get_cpu_current)) {
+               return lttng_ust_get_cpu_internal();
+       } else {
+               return lttng_ust_get_cpu_current();
+       }
+}
+
+#endif /* _LTTNG_GETCPU_H */
index 900b3c2e526bf1d76945c40d9e952250b70a9838..9a70ca526d19e4390aba56f759635dd74979ca6c 100644 (file)
@@ -19,7 +19,7 @@
 #include <limits.h>
 #include <lttng/ust-events.h>
 #include <lttng/ust-tracer.h>
-#include "common/ringbuffer/getcpu.h"
+#include "lib/lttng-ust/getcpu.h"
 #include <lttng/ringbuffer-context.h>
 
 #include "context-internal.h"
diff --git a/src/lib/lttng-ust/lttng-getcpu.c b/src/lib/lttng-ust/lttng-getcpu.c
deleted file mode 100644 (file)
index 8d2c944..0000000
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * SPDX-License-Identifier: LGPL-2.1-only
- *
- * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- */
-
-#define _LGPL_SOURCE
-#include <error.h>
-#include <dlfcn.h>
-#include <stdlib.h>
-#include "common/logging.h"
-#include <lttng/ust-getcpu.h>
-#include <urcu/system.h>
-#include <urcu/arch.h>
-
-#include "getenv.h"
-#include "common/ringbuffer/getcpu.h"
-
-int (*lttng_get_cpu)(void);
-
-static
-void *getcpu_handle;
-
-int lttng_ust_getcpu_override(int (*getcpu)(void))
-{
-       CMM_STORE_SHARED(lttng_get_cpu, getcpu);
-       return 0;
-}
-
-void lttng_ust_getcpu_init(void)
-{
-       const char *libname;
-       void (*libinit)(void);
-
-       if (getcpu_handle)
-               return;
-       libname = lttng_ust_getenv("LTTNG_UST_GETCPU_PLUGIN");
-       if (!libname)
-               return;
-       getcpu_handle = dlopen(libname, RTLD_NOW);
-       if (!getcpu_handle) {
-               PERROR("Cannot load LTTng UST getcpu override library %s",
-                       libname);
-               return;
-       }
-       dlerror();
-       libinit = (void (*)(void)) dlsym(getcpu_handle,
-               "lttng_ust_getcpu_plugin_init");
-       if (!libinit) {
-               PERROR("Cannot find LTTng UST getcpu override library %s initialization function lttng_ust_getcpu_plugin_init()",
-                       libname);
-               return;
-       }
-       libinit();
-}
index 995c78ff21b32c2c9043e8aa2a2c51cea72cb870..219ef5d21ca64a21c08457c86f8b822d73efc540 100644 (file)
@@ -50,7 +50,7 @@
 #include "common/ringbuffer/rb-init.h"
 #include "lttng-ust-statedump.h"
 #include "clock.h"
-#include "common/ringbuffer/getcpu.h"
+#include "lib/lttng-ust/getcpu.h"
 #include "getenv.h"
 #include "ust-events-internal.h"
 #include "context-internal.h"
@@ -2125,7 +2125,7 @@ void lttng_ust_init(void)
        lttng_ust_tp_init();
        lttng_ust_init_fd_tracker();
        lttng_ust_clock_init();
-       lttng_ust_getcpu_init();
+       lttng_ust_getcpu_plugin_init();
        lttng_ust_statedump_init();
        lttng_ust_ring_buffer_clients_init();
        lttng_ust_counter_clients_init();
This page took 0.033332 seconds and 4 git commands to generate.