Move the getcpu plugin implementation to liblttn-ust-common
authorMichael Jeanson <mjeanson@efficios.com>
Fri, 23 Apr 2021 00:39:28 +0000 (20:39 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Fri, 23 Apr 2021 19:49:17 +0000 (15:49 -0400)
The getcpu plugin is used by both liblttng-ust and liblttng-ust-ctl, move
it to liblttng-ust-common.

Change-Id: Ifdef881188e744ffd2c19670959612aaca8d73d9
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-api.h
src/common/getcpu.h [new file with mode: 0644]
src/common/ringbuffer/frontend_api.h
src/lib/lttng-ust-common/Makefile.am
src/lib/lttng-ust-common/getcpu.c [new file with mode: 0644]
src/lib/lttng-ust-common/getcpu.h [new file with mode: 0644]
src/lib/lttng-ust-common/ust-common.c
src/lib/lttng-ust/Makefile.am
src/lib/lttng-ust/getcpu.c [deleted file]
src/lib/lttng-ust/getcpu.h [deleted file]
src/lib/lttng-ust/lttng-context-cpu-id.c
src/lib/lttng-ust/lttng-ust-comm.c

index f4c134a704685a0358eec114834c91d4878ec090..4692e24c5329f89421d9591571fad71af64c4469 100644 (file)
@@ -14,6 +14,7 @@ noinst_HEADERS = \
        creds.h \
        err-ptr.h \
        events.h \
+       getcpu.h \
        hash.h \
        jhash.h \
        logging.h \
index 3465d5dc98a18084cb7e34f5ca0932ade1fc8e5e..404ec9b5aacd0a443dd0ae6b7578f9232e2e6f36 100644 (file)
@@ -16,7 +16,7 @@
 #include <urcu/compiler.h>
 #include <urcu/uatomic.h>
 #include "common/bitmap.h"
-#include "lib/lttng-ust/getcpu.h"
+#include "common/getcpu.h"
 
 /*
  * Using unsigned arithmetic because overflow is defined.
diff --git a/src/common/getcpu.h b/src/common/getcpu.h
new file mode 100644 (file)
index 0000000..3bf9076
--- /dev/null
@@ -0,0 +1,115 @@
+/*
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ */
+
+#ifndef _UST_COMMON_GETCPU_H
+#define _UST_COMMON_GETCPU_H
+
+#include <urcu/compiler.h>
+#include <urcu/system.h>
+#include <urcu/arch.h>
+
+#include <lttng/ust-getcpu.h>
+
+/*
+ * 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.
+ *
+ * This is an ABI symbol of liblttng-ust-common accessed by other libraries
+ * through the static inline function in this file. It is initialised in the
+ * liblttng-ust-common constructor.
+ */
+extern int (*lttng_ust_get_cpu_sym)(void);
+
+#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 36370042d98cc1f67f68b091aeb588c8b24385d8..879461f37f1fa872b54285475f7d99c03a4f1c8b 100644 (file)
@@ -15,7 +15,7 @@
 
 #include <urcu/compiler.h>
 
-#include "lib/lttng-ust/getcpu.h"
+#include "common/getcpu.h"
 #include "frontend.h"
 
 /**
index 334fc0d1261d18c7ef89f05b47d371cf037989be..caeea2be5de74e54d7d67ddc0a4e45e9a429cbe1 100644 (file)
@@ -7,6 +7,8 @@ liblttng_ust_common_la_SOURCES = \
        clock.h \
        fd-tracker.c \
        fd-tracker.h \
+       getcpu.c \
+       getcpu.h \
        ust-common.c \
        lttng-ust-urcu.c \
        lttng-ust-urcu-pointer.c
diff --git a/src/lib/lttng-ust-common/getcpu.c b/src/lib/lttng-ust-common/getcpu.c
new file mode 100644 (file)
index 0000000..e80bcf1
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ */
+
+#define _LGPL_SOURCE
+#include <dlfcn.h>
+#include <stdlib.h>
+#include <urcu/system.h>
+#include <urcu/arch.h>
+
+#include "common/getcpu.h"
+#include "common/getenv.h"
+#include "common/logging.h"
+
+#include "lib/lttng-ust-common/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-common/getcpu.h b/src/lib/lttng-ust-common/getcpu.h
new file mode 100644 (file)
index 0000000..de1d610
--- /dev/null
@@ -0,0 +1,17 @@
+/*
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ */
+
+#ifndef _LTTNG_UST_COMMON_GETCPU_H
+#define _LTTNG_UST_COMMON_GETCPU_H
+
+/*
+ * Initialize the getcpu plugin if it's present.
+ */
+void lttng_ust_getcpu_plugin_init(void)
+       __attribute__((visibility("hidden")));
+
+
+#endif /* _LTTNG_UST_COMMON_GETCPU_H */
index e6c81ba65445b24d2d6510d6779a0e4abbd9e28f..1aee7ac7a7a99f4b47ce557ea68c9ab09eb80481 100644 (file)
@@ -12,6 +12,7 @@
 
 #include "lib/lttng-ust-common/fd-tracker.h"
 #include "lib/lttng-ust-common/clock.h"
+#include "lib/lttng-ust-common/getcpu.h"
 
 /*
  * The liblttng-ust-common constructor, initialize the internal shared state.
@@ -30,6 +31,11 @@ void lttng_ust_common_ctor(void)
         * Initialize the potential user-provided clock plugin.
         */
        lttng_ust_clock_init();
+
+       /*
+        * Initialize the potential user-provided getcpu plugin.
+        */
+       lttng_ust_getcpu_plugin_init();
 }
 
 void lttng_ust_common_alloc_tls(void)
index 752b5177bae66210a8c225cb99f2df543c72feef..b101a81ea30c56a52adf9c35a2f8f203e30e8cd0 100644 (file)
@@ -84,8 +84,7 @@ liblttng_ust_support_la_SOURCES = \
        lttng-ring-buffer-metadata-client.c \
        lttng-counter-client.h \
        lttng-counter-client-percpu-32-modular.c \
-       lttng-counter-client-percpu-64-modular.c \
-       getcpu.c getcpu.h
+       lttng-counter-client-percpu-64-modular.c
 
 liblttng_ust_la_SOURCES =
 
diff --git a/src/lib/lttng-ust/getcpu.c b/src/lib/lttng-ust/getcpu.c
deleted file mode 100644 (file)
index 149491c..0000000
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * SPDX-License-Identifier: LGPL-2.1-only
- *
- * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- */
-
-#define _LGPL_SOURCE
-#include <dlfcn.h>
-#include <stdlib.h>
-#include "common/logging.h"
-#include <urcu/system.h>
-#include <urcu/arch.h>
-
-#include "common/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
deleted file mode 100644 (file)
index cac3401..0000000
+++ /dev/null
@@ -1,119 +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>
-
-#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 37e8327f28fc05daaa4ca03ca4ad8c0f2161f8d1..554e2c9b26fb5e5d18bd05602db50c3a884ad29d 100644 (file)
 #include <limits.h>
 #include <lttng/ust-events.h>
 #include <lttng/ust-tracer.h>
-#include "lib/lttng-ust/getcpu.h"
 #include <lttng/ust-ringbuffer-context.h>
 
+#include "common/getcpu.h"
+
 #include "context-internal.h"
 
 static
index db9fa8d990267735fbc967e1b9629165efc5a99e..ff7b5939ae05cade2d07d1338c73184e67fa8d3c 100644 (file)
@@ -51,7 +51,6 @@
 #include "common/ringbuffer/rb-init.h"
 #include "lttng-ust-statedump.h"
 #include "common/clock.h"
-#include "lib/lttng-ust/getcpu.h"
 #include "common/getenv.h"
 #include "lib/lttng-ust/events.h"
 #include "context-internal.h"
@@ -2118,7 +2117,6 @@ void lttng_ust_ctor(void)
        lttng_ust_common_ctor();
 
        lttng_ust_tp_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.032976 seconds and 4 git commands to generate.