Skip to content

Commit

Permalink
JComponentManager, JPluginLoader use new JService functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwbrei committed Apr 15, 2024
1 parent 05950b0 commit 6f60aae
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 73 deletions.
12 changes: 8 additions & 4 deletions src/libraries/JANA/JApplication.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ JApplication::JApplication(JLogger::Level verbosity) {
m_service_locator->provide(m_params);
ProvideService(m_params);
ProvideService(std::make_shared<JLoggingService>());
ProvideService(std::make_shared<JPluginLoader>(this));
ProvideService(std::make_shared<JComponentManager>(this));
ProvideService(std::make_shared<JPluginLoader>());
ProvideService(std::make_shared<JComponentManager>());
ProvideService(std::make_shared<JGlobalRootLock>());
ProvideService(std::make_shared<JTopologyBuilder>());

Expand All @@ -48,7 +48,7 @@ JApplication::JApplication(JParameterManager* params) {
ProvideService(m_params);
ProvideService(std::make_shared<JLoggingService>());
ProvideService(std::make_shared<JPluginLoader>());
ProvideService(std::make_shared<JComponentManager>(this));
ProvideService(std::make_shared<JComponentManager>());
ProvideService(std::make_shared<JGlobalRootLock>());
ProvideService(std::make_shared<JTopologyBuilder>());

Expand Down Expand Up @@ -119,6 +119,10 @@ void JApplication::Initialize() {
// Only run this once
if (m_initialized) return;

// Obtain final values of parameters and loggers
m_plugin_loader->InitPhase2();
m_component_manager->InitPhase2();

// Attach all plugins
m_plugin_loader->attach_plugins(m_component_manager.get());

Expand All @@ -137,7 +141,7 @@ void JApplication::Initialize() {
m_params->SetDefaultParameter("jana:ticker_interval", m_ticker_interval_ms, "Controls the ticker interval (in ms)");
m_params->SetDefaultParameter("jana:extended_report", m_extended_report, "Controls whether the ticker shows simple vs detailed performance metrics");

m_component_manager->initialize();
m_component_manager->resolve_event_sources();

/*
int engine_choice = 0;
Expand Down
60 changes: 23 additions & 37 deletions src/libraries/JANA/Services/JComponentManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
#include <JANA/JFactoryGenerator.h>
#include <JANA/JEventUnfolder.h>

JComponentManager::JComponentManager(JApplication* app) : m_app(app) {
}
JComponentManager::JComponentManager() {}

JComponentManager::~JComponentManager() {

Expand All @@ -30,6 +29,17 @@ JComponentManager::~JComponentManager() {
}
}

void JComponentManager::InitPhase2() {

// We don't set these in Init() because Init() gets called by the JApplication constructor and we want to give the user a chance to
// set them manually before they call JApplication::Init().
m_params().SetDefaultParameter("event_source_type", m_user_evt_src_typename, "Manually specifies which JEventSource should open the input file");
m_params().SetDefaultParameter("record_call_stack", m_enable_call_graph_recording, "Records a trace of who called each factory. Reduces performance but necessary for plugins such as janadot.");
m_params().SetDefaultParameter("jana:nevents", m_nevents, "Max number of events that sources can emit");
m_params().SetDefaultParameter("jana:nskip", m_nskip, "Number of events that sources should skip before starting emitting");
m_params().FilterParameters(m_default_tags, "DEFTAG:");
}

void JComponentManager::next_plugin(std::string plugin_name) {
// We defer resolving event sources until we have finished loading all plugins
m_current_plugin_name = plugin_name;
Expand All @@ -41,31 +51,36 @@ void JComponentManager::add(std::string event_source_name) {

void JComponentManager::add(JEventSourceGenerator *source_generator) {
source_generator->SetPluginName(m_current_plugin_name);
source_generator->SetJApplication(m_app);
source_generator->SetJApplication(GetApplication());
// source_generator->SetLogger(m_logging().get_logger(source_generator->GetLoggerName()));
m_src_gens.push_back(source_generator);
}

void JComponentManager::add(JFactoryGenerator *factory_generator) {
factory_generator->SetPluginName(m_current_plugin_name);
factory_generator->SetApplication(m_app);
factory_generator->SetApplication(GetApplication());
// factory_generator->SetLogger(m_logging().get_logger(factory_generator->GetLoggerName()));
m_fac_gens.push_back(factory_generator);
}

void JComponentManager::add(JEventSource *event_source) {
event_source->SetPluginName(m_current_plugin_name);
event_source->SetApplication(m_app);
event_source->SetApplication(GetApplication());
event_source->SetLogger(m_logging().get_logger(event_source->GetLoggerName()));
m_evt_srces.push_back(event_source);
}

void JComponentManager::add(JEventProcessor *processor) {
processor->SetPluginName(m_current_plugin_name);
processor->SetApplication(m_app);
processor->SetApplication(GetApplication());
processor->SetLogger(m_logging().get_logger(processor->GetLoggerName()));
m_evt_procs.push_back(processor);
}

void JComponentManager::add(JEventUnfolder* unfolder) {
unfolder->SetPluginName(m_current_plugin_name);
unfolder->SetApplication(m_app);
unfolder->SetApplication(GetApplication());
unfolder->SetLogger(m_logging().get_logger(unfolder->GetLoggerName()));
m_unfolders.push_back(unfolder);
}

Expand All @@ -76,47 +91,18 @@ void JComponentManager::configure_event(JEvent& event) {
event.GetJCallGraphRecorder()->SetEnabled(m_enable_call_graph_recording);
}

void JComponentManager::initialize() {
// We want to obtain parameters from here rather than in the constructor.
// If we set them here, plugins and test cases can set parameters right up until JApplication::Initialize()
// or Run() are called. Otherwise, the parameters have to be set before the
// JApplication is even constructed.
auto parms = m_app->GetJParameterManager();
parms->SetDefaultParameter("record_call_stack", m_enable_call_graph_recording, "Records a trace of who called each factory. Reduces performance but necessary for plugins such as janadot.");
parms->FilterParameters(m_default_tags, "DEFTAG:");

resolve_event_sources();

auto logging_svc = m_app->GetService<JLoggingService>();

for (JEventSource* source : m_evt_srces) {
source->SetLogger(logging_svc->get_logger(source->GetLoggerName()));
}
for (JEventProcessor* proc : m_evt_procs) {
proc->SetLogger(logging_svc->get_logger(proc->GetLoggerName()));
}
for (JEventUnfolder* unfolder : m_unfolders) {
unfolder->SetLogger(logging_svc->get_logger(unfolder->GetLoggerName()));
}
}


void JComponentManager::resolve_event_sources() {

m_app->SetDefaultParameter("event_source_type", m_user_evt_src_typename, "Manually specifies which JEventSource should open the input file");

m_user_evt_src_gen = resolve_user_event_source_generator();
for (auto& source_name : m_src_names) {
auto* generator = resolve_event_source(source_name);
auto source = generator->MakeJEventSource(source_name);
source->SetPluginName(generator->GetPluginName());
source->SetApplication(m_app);
source->SetApplication(GetApplication());
m_evt_srces.push_back(source);
}

m_app->SetDefaultParameter("jana:nevents", m_nevents, "Max number of events that sources can emit");
m_app->SetDefaultParameter("jana:nskip", m_nskip, "Number of events that sources should skip before starting emitting");

for (auto source : m_evt_srces) {
// If nskip/nevents are set individually on JEventSources, respect those. Otherwise use global values.
// Note that this is not what we usually want when we have multiple event sources. It would make more sense to
Expand Down
7 changes: 4 additions & 3 deletions src/libraries/JANA/Services/JComponentManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ class JEventUnfolder;
class JComponentManager : public JService {
public:

explicit JComponentManager(JApplication*);
explicit JComponentManager();
~JComponentManager() override;
void InitPhase2();

void next_plugin(std::string plugin_name);

Expand All @@ -30,7 +31,6 @@ class JComponentManager : public JService {
void add(JEventProcessor* processor);
void add(JEventUnfolder* unfolder);

void initialize();
void resolve_event_sources();
JEventSourceGenerator* resolve_user_event_source_generator() const;
JEventSourceGenerator* resolve_event_source(std::string source_name) const;
Expand All @@ -51,7 +51,8 @@ class JComponentManager : public JService {
// Processors need: { typename, pluginname, mutexgroup, status, evtcnt }
// Factories need: { typename, pluginname }

JApplication* m_app;
Service<JParameterManager> m_params {this};
Service<JLoggingService> m_logging {this};

std::string m_current_plugin_name;
std::vector<std::string> m_src_names;
Expand Down
43 changes: 20 additions & 23 deletions src/libraries/JANA/Services/JPluginLoader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include "JPluginLoader.h"
#include "JComponentManager.h"
#include "JLoggingService.h"
#include "JParameterManager.h"

#include <dlfcn.h>
Expand All @@ -15,6 +14,22 @@

class JApplication;

void JPluginLoader::InitPhase2() {

m_params().SetDefaultParameter("plugins", m_plugins_to_include, "Comma-separated list of plugins to load.");
m_params().SetDefaultParameter("plugins_to_ignore", m_plugins_to_exclude, "Comma-separated list of plugins to NOT load, even if they are specified in 'plugins'.");
m_params().SetDefaultParameter("jana:plugin_path", m_plugin_paths_str, "Colon-separated list of paths to search for plugins");
m_params().SetDefaultParameter("jana:debug_plugin_loading", m_verbose, "Trace the plugin search path and display any loading errors");

if (m_verbose) {
// The jana:debug_plugin_loading parameter is kept around for backwards compatibility
// at least for now
GetLogger().level = JLogger::Level::TRACE;
}
}



void JPluginLoader::add_plugin(std::string plugin_name) {
/// Add the specified plugin to the list of plugins to be
/// attached. This only records the name. The plugin is not
Expand Down Expand Up @@ -211,7 +226,7 @@ void JPluginLoader::attach_plugin(std::string soname) {
InitPlugin_t* initialize_proc = (InitPlugin_t*) dlsym(handle, "InitPlugin");
if (initialize_proc) {
LOG_INFO(m_logger) << "Initializing plugin \"" << soname << "\"" << LOG_END;
(*initialize_proc)(m_app);
(*initialize_proc)(GetApplication());
m_sohandles[soname] = handle;
} else {
dlclose(handle);
Expand All @@ -221,9 +236,9 @@ void JPluginLoader::attach_plugin(std::string soname) {
}


JPluginLoader::JPluginLoader(JApplication* app) : m_app(app) {}
JPluginLoader::JPluginLoader() {}

JPluginLoader::~JPluginLoader(){
JPluginLoader::~JPluginLoader() {

// Loop over open plugin handles.
// Call FinalizePlugin if it has one and close it in all cases.
Expand All @@ -234,29 +249,11 @@ JPluginLoader::~JPluginLoader(){
FinalizePlugin_t* finalize_proc = (FinalizePlugin_t*) dlsym(handle, "FinalizePlugin");
if (finalize_proc) {
LOG_INFO(m_logger) << "Finalizing plugin \"" << soname << "\"" << LOG_END;
(*finalize_proc)(m_app);
(*finalize_proc)(GetApplication());
}

// Close plugin handle
dlclose(handle);
}
}

void JPluginLoader::acquire_services(JServiceLocator* sl) {

auto params = sl->get<JParameterManager>();
params->SetDefaultParameter("plugins", m_plugins_to_include, "Comma-separated list of plugins to load.");
params->SetDefaultParameter("plugins_to_ignore", m_plugins_to_exclude, "Comma-separated list of plugins to NOT load, even if they are specified in 'plugins'.");
m_app->SetDefaultParameter("jana:plugin_path", m_plugin_paths_str, "Colon-separated list of paths to search for plugins");
params->SetDefaultParameter("jana:debug_plugin_loading", m_verbose, "Trace the plugin search path and display any loading errors");

m_logger = sl->get<JLoggingService>()->get_logger("JPluginLoader");
if (m_verbose) {
// The jana:debug_plugin_loading parameter is kept around for backwards compatibility
// at least for now
m_logger.level = JLogger::Level::TRACE;
}
}



10 changes: 4 additions & 6 deletions src/libraries/JANA/Services/JPluginLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#ifndef JANA2_JPLUGINLOADER_H
#define JANA2_JPLUGINLOADER_H

#include <JANA/Services/JLoggingService.h>
#include <JANA/Services/JParameterManager.h>
#include <JANA/JService.h>

#include <string>
#include <vector>
Expand All @@ -20,16 +20,17 @@ class JPluginLoader : public JService {

public:

JPluginLoader(JApplication* app);
JPluginLoader();
~JPluginLoader() override;
void acquire_services(JServiceLocator*) override;
void InitPhase2();

void add_plugin(std::string plugin_name);
void add_plugin_path(std::string path);
void attach_plugins(JComponentManager* jcm);
void attach_plugin(std::string plugin_name);

private:
Service<JParameterManager> m_params {this};

std::vector<std::string> m_plugins_to_include;
std::vector<std::string> m_plugins_to_exclude;
Expand All @@ -38,9 +39,6 @@ class JPluginLoader : public JService {
std::map<std::string, void*> m_sohandles; // key=plugin name val=dlopen handle

bool m_verbose = false;
JLogger m_logger;

JApplication* m_app = nullptr;

};

Expand Down

0 comments on commit 6f60aae

Please sign in to comment.