From d90c6314c57eb8b3b0a4ba3b3b59ccbefde979e7 Mon Sep 17 00:00:00 2001 From: Giulio Eulisse <10544+ktf@users.noreply.github.com> Date: Wed, 23 Jun 2021 14:35:57 +0200 Subject: [PATCH 1/2] DPL: add api to easily load and act on a plugin --- Framework/Core/CMakeLists.txt | 1 + Framework/Core/include/Framework/Plugins.h | 16 +++++++ Framework/Core/src/Plugins.cxx | 54 ++++++++++++++++++++++ Framework/Core/src/runDataProcessing.cxx | 2 + 4 files changed, 73 insertions(+) create mode 100644 Framework/Core/src/Plugins.cxx diff --git a/Framework/Core/CMakeLists.txt b/Framework/Core/CMakeLists.txt index d946f3c477998..bf0cf5669549a 100644 --- a/Framework/Core/CMakeLists.txt +++ b/Framework/Core/CMakeLists.txt @@ -83,6 +83,7 @@ o2_add_library(Framework src/O2ControlLabels.cxx src/OutputSpec.cxx src/PropertyTreeHelpers.cxx + src/Plugins.cxx src/RCombinedDS.cxx src/ReadoutAdapter.cxx src/ResourcesMonitoringHelper.cxx diff --git a/Framework/Core/include/Framework/Plugins.h b/Framework/Core/include/Framework/Plugins.h index a72de955194aa..448ee4626d204 100644 --- a/Framework/Core/include/Framework/Plugins.h +++ b/Framework/Core/include/Framework/Plugins.h @@ -13,6 +13,9 @@ #include "Framework/AlgorithmSpec.h" #include +#include +#include +#include namespace o2::framework { @@ -40,6 +43,14 @@ struct DPLPluginHandle { DPLPluginHandle* previous; }; +// Struct to hold live plugin information which the plugin itself cannot +// know and that is owned by the framework. +struct PluginInfo { + uv_lib_t* dso = nullptr; + std::string name; + DPLPluginHandle* instance = nullptr; +}; + #define DEFINE_DPL_PLUGIN(NAME, KIND) \ extern "C" { \ DPLPluginHandle* dpl_plugin_callback(DPLPluginHandle* previous) \ @@ -75,7 +86,12 @@ struct PluginManager { } return nullptr; } + /// Load a DSO called @a dso and insert its handle in @a infos + /// On successfull completion @a onSuccess is called passing + /// the DPLPluginHandle provided by the library. + static void load(std::vector& infos, const char* dso, std::function& onSuccess); }; + } // namespace o2::framework #endif // O2_FRAMEWORK_PLUGINS_H_ diff --git a/Framework/Core/src/Plugins.cxx b/Framework/Core/src/Plugins.cxx new file mode 100644 index 0000000000000..835f89260daad --- /dev/null +++ b/Framework/Core/src/Plugins.cxx @@ -0,0 +1,54 @@ +// Copyright CERN and copyright holders of ALICE O2. This software is +// distributed under the terms of the GNU General Public License v3 (GPL +// Version 3), copied verbatim in the file "COPYING". +// +// See http://alice-o2.web.cern.ch/license for full licensing information. +// +// In applying this license CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. +#include "Framework/Plugins.h" +#include "Framework/Logger.h" +#include +#include +#include + +namespace o2::framework +{ + +void PluginManager::load(std::vector& libs, const char* dso, std::function& onSuccess) +{ + auto plugin = std::find_if(libs.begin(), libs.end(), [dso](PluginInfo& info) { return info.name == dso; }); + if (plugin != libs.end()) { + return onSuccess(plugin->instance); + } + uv_lib_t* supportLib = (uv_lib_t*)malloc(sizeof(uv_lib_t)); + int result = 0; +#ifdef __APPLE__ + char const* extension = "dylib"; +#else + char const* extension = "so"; +#endif + std::string filename = fmt::format("lib{}.{}", dso, extension); + result = uv_dlopen(filename.c_str(), supportLib); + if (result == -1) { + LOG(FATAL) << uv_dlerror(supportLib); + return; + } + void* callback = nullptr; + DPLPluginHandle* (*dpl_plugin_callback)(DPLPluginHandle*); + + result = uv_dlsym(supportLib, "dpl_plugin_callback", (void**)&dpl_plugin_callback); + if (result == -1) { + LOG(FATAL) << uv_dlerror(supportLib); + return; + } + if (dpl_plugin_callback == nullptr) { + LOGP(FATAL, "Could not find the {} plugin.", dso); + return; + } + DPLPluginHandle* pluginInstance = dpl_plugin_callback(nullptr); + libs.push_back({supportLib, dso}); + onSuccess(pluginInstance); +} +} // namespace o2::framework diff --git a/Framework/Core/src/runDataProcessing.cxx b/Framework/Core/src/runDataProcessing.cxx index 08d6dc3b446d5..155d4317dcd6e 100644 --- a/Framework/Core/src/runDataProcessing.cxx +++ b/Framework/Core/src/runDataProcessing.cxx @@ -2143,6 +2143,8 @@ int doMain(int argc, char** argv, o2::framework::WorkflowSpec const& workflow, { O2_SIGNPOST_INIT(); std::vector currentArgs; + std::vector plugins; + for (size_t ai = 1; ai < argc; ++ai) { currentArgs.push_back(argv[ai]); } From 2bc48ff662e4f8521ce5e2928b79a3b977d032eb Mon Sep 17 00:00:00 2001 From: Giulio Eulisse <10544+ktf@users.noreply.github.com> Date: Sun, 27 Jun 2021 08:34:17 +0200 Subject: [PATCH 2/2] Update Plugins.cxx --- Framework/Core/src/Plugins.cxx | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Framework/Core/src/Plugins.cxx b/Framework/Core/src/Plugins.cxx index 835f89260daad..614f67add02cb 100644 --- a/Framework/Core/src/Plugins.cxx +++ b/Framework/Core/src/Plugins.cxx @@ -1,8 +1,9 @@ -// Copyright CERN and copyright holders of ALICE O2. This software is -// distributed under the terms of the GNU General Public License v3 (GPL -// Version 3), copied verbatim in the file "COPYING". +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. +// All rights not expressly granted are reserved. // -// See http://alice-o2.web.cern.ch/license for full licensing information. +// This software is distributed under the terms of the GNU General Public +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". // // In applying this license CERN does not waive the privileges and immunities // granted to it by virtue of its status as an Intergovernmental Organization