From 247c2ecd8e17ab02996e45cfa1be254b04161210 Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Tue, 6 Apr 2021 12:29:36 -0400 Subject: [PATCH] Move getcpu.h to 'lib/lttng-ust/' 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 Signed-off-by: Mathieu Desnoyers --- src/common/Makefile.am | 1 - src/common/counter/counter-api.h | 2 +- src/common/ringbuffer/smp.h | 2 +- src/lib/lttng-ust/Makefile.am | 3 +- src/lib/lttng-ust/getcpu.c | 76 +++++++++++++++++++ .../ringbuffer => lib/lttng-ust}/getcpu.h | 25 ++++-- src/lib/lttng-ust/lttng-context-cpu-id.c | 2 +- src/lib/lttng-ust/lttng-getcpu.c | 55 -------------- src/lib/lttng-ust/lttng-ust-comm.c | 4 +- 9 files changed, 103 insertions(+), 67 deletions(-) create mode 100644 src/lib/lttng-ust/getcpu.c rename src/{common/ringbuffer => lib/lttng-ust}/getcpu.h (73%) delete mode 100644 src/lib/lttng-ust/lttng-getcpu.c diff --git a/src/common/Makefile.am b/src/common/Makefile.am index 619276ed..2ab4bfdc 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -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 \ diff --git a/src/common/counter/counter-api.h b/src/common/counter/counter-api.h index 81bb551b..3465d5dc 100644 --- a/src/common/counter/counter-api.h +++ b/src/common/counter/counter-api.h @@ -16,7 +16,7 @@ #include #include #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/smp.h b/src/common/ringbuffer/smp.h index 028a66f7..8a96d3d7 100644 --- a/src/common/ringbuffer/smp.h +++ b/src/common/ringbuffer/smp.h @@ -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, diff --git a/src/lib/lttng-ust/Makefile.am b/src/lib/lttng-ust/Makefile.am index df0ae8d0..3548a9e6 100644 --- a/src/lib/lttng-ust/Makefile.am +++ b/src/lib/lttng-ust/Makefile.am @@ -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 index 00000000..483f8131 --- /dev/null +++ b/src/lib/lttng-ust/getcpu.c @@ -0,0 +1,76 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-only + * + * Copyright (C) 2014 Mathieu Desnoyers + */ + +#define _LGPL_SOURCE +#include +#include +#include +#include "common/logging.h" +#include +#include + +#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/common/ringbuffer/getcpu.h b/src/lib/lttng-ust/getcpu.h similarity index 73% rename from src/common/ringbuffer/getcpu.h rename to src/lib/lttng-ust/getcpu.h index 52c74413..cac34017 100644 --- a/src/common/ringbuffer/getcpu.h +++ b/src/lib/lttng-ust/getcpu.h @@ -11,10 +11,21 @@ #include #include -void lttng_ust_getcpu_init(void) +#include + + +/* + * Initialize the getcpu plugin if it's present. + */ +void lttng_ust_getcpu_plugin_init(void) __attribute__((visibility("hidden"))); -extern int (*lttng_get_cpu)(void) +/* + * 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 @@ -92,12 +103,16 @@ int lttng_ust_get_cpu_internal(void) static inline int lttng_ust_get_cpu(void) { - int (*getcpu)(void) = CMM_LOAD_SHARED(lttng_get_cpu); + int (*lttng_ust_get_cpu_current)(void) = CMM_LOAD_SHARED(lttng_ust_get_cpu_sym); - if (caa_likely(!getcpu)) { + /* + * 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 getcpu(); + return lttng_ust_get_cpu_current(); } } diff --git a/src/lib/lttng-ust/lttng-context-cpu-id.c b/src/lib/lttng-ust/lttng-context-cpu-id.c index 900b3c2e..9a70ca52 100644 --- a/src/lib/lttng-ust/lttng-context-cpu-id.c +++ b/src/lib/lttng-ust/lttng-context-cpu-id.c @@ -19,7 +19,7 @@ #include #include #include -#include "common/ringbuffer/getcpu.h" +#include "lib/lttng-ust/getcpu.h" #include #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 index 8d2c944c..00000000 --- a/src/lib/lttng-ust/lttng-getcpu.c +++ /dev/null @@ -1,55 +0,0 @@ -/* - * SPDX-License-Identifier: LGPL-2.1-only - * - * Copyright (C) 2014 Mathieu Desnoyers - */ - -#define _LGPL_SOURCE -#include -#include -#include -#include "common/logging.h" -#include -#include -#include - -#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(); -} diff --git a/src/lib/lttng-ust/lttng-ust-comm.c b/src/lib/lttng-ust/lttng-ust-comm.c index 995c78ff..219ef5d2 100644 --- a/src/lib/lttng-ust/lttng-ust-comm.c +++ b/src/lib/lttng-ust/lttng-ust-comm.c @@ -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(); -- 2.34.1