Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-05 09:14:51

0001 
0002 // Copyright 2020, Jefferson Science Associates, LLC.
0003 // Subject to the terms in the LICENSE file found in the top-level directory.
0004 
0005 #pragma once
0006 
0007 #include <string>
0008 #include <atomic>
0009 #include <mutex>
0010 #include <memory>
0011 #include <chrono>
0012 
0013 class JEventProcessor;
0014 class JEventSource;
0015 class JEventSourceGenerator;
0016 class JFactoryGenerator;
0017 class JFactorySet;
0018 class JComponentManager;
0019 class JPluginLoader;
0020 class JExecutionEngine;
0021 class JEventUnfolder;
0022 class JServiceLocator;
0023 class JParameter;
0024 class JParameterManager;
0025 class JApplication;
0026 extern JApplication* japp;
0027 
0028 #include <JANA/Components/JComponentSummary.h>
0029 #include <JANA/JLogger.h>
0030 
0031 
0032 //////////////////////////////////////////////////////////////////////////////////////////////////
0033 /// JANA application class
0034 ///
0035 /// The JApplication class serves as a central access point for getting to most things
0036 /// in the JANA application. It owns the JThreadManager, JParameterManager, etc.
0037 /// It is also responsible for making sure all of the plugins are attached and other
0038 /// user specified configurations for the run are implemented before starting the processing
0039 /// of the data. User code (e.g. plugins) will generally register things like event sources
0040 /// and processors with the JApplication so they can be called up later at the appropriate time.
0041 //////////////////////////////////////////////////////////////////////////////////////////////////
0042 class JApplication {
0043 
0044 public:
0045 
0046     /// These exit codes are what JANA uses internally. However they are fundamentally a suggestion --
0047     /// the user code is likely to use arbitrary exit codes.
0048     enum class ExitCode {Success=0, UnhandledException, Timeout, Segfault=139};
0049 
0050     explicit JApplication(JParameterManager* params = nullptr);
0051     explicit JApplication(JLogger::Level verbosity);
0052     ~JApplication();
0053 
0054 
0055     // Loading plugins
0056 
0057     void AddPlugin(std::string plugin_name);
0058     void AddPluginPath(std::string path);
0059 
0060 
0061     // Building a JProcessingTopology
0062 
0063     void Add(std::string event_source_name);
0064     void Add(JEventSourceGenerator* source_generator);
0065     void Add(JFactoryGenerator* factory_generator);
0066     void Add(JEventSource* event_source);
0067     void Add(JEventProcessor* processor);
0068     void Add(JEventUnfolder* unfolder);
0069 
0070 
0071     // Controlling processing
0072 
0073     void Initialize(void);
0074     void Run(bool wait_until_stopped=true, bool finish=true);
0075     void Scale(int nthreads);
0076     void Stop(bool wait_until_stopped=false, bool finish=true);
0077     void Quit(bool skip_join = false);
0078     void SetExitCode(int exitCode);
0079     int GetExitCode();
0080 
0081     // Performance/status monitoring
0082     void PrintStatus();
0083     bool IsInitialized(void){return m_initialized;}
0084     bool IsQuitting(void) { return m_quitting; }
0085     bool IsDrainingQueues();
0086 
0087     void SetTicker(bool ticker_on = true);
0088     bool IsTickerEnabled();
0089     void SetTimeoutEnabled(bool enabled = true);
0090     bool IsTimeoutEnabled();
0091     uint64_t GetNThreads();
0092     uint64_t GetNEventsProcessed();
0093     float GetIntegratedRate();
0094     float GetInstantaneousRate();
0095 
0096     const JComponentSummary& GetComponentSummary();
0097 
0098     // Parameter config
0099 
0100     JParameterManager* GetJParameterManager() { return m_params.get(); }
0101 
0102     template<typename T>
0103     T GetParameterValue(std::string name);
0104 
0105     template <typename T>
0106     JParameter* GetParameter(std::string name, T& val);
0107 
0108     template<typename T>
0109     JParameter* SetParameterValue(std::string name, T val);
0110 
0111     template <typename T>
0112     JParameter* SetDefaultParameter(std::string name, T& val, std::string description="");
0113 
0114     template <typename T>
0115     T RegisterParameter(std::string name, const T default_val, std::string description="");
0116 
0117     // Locating services
0118 
0119     /// Use this in EventSources, Factories, or EventProcessors. Do not call this
0120     /// from InitPlugin(), as not all JServices may have been loaded yet.
0121     /// When initializing a Service, use acquire_services() instead.
0122     template <typename T>
0123     std::shared_ptr<T> GetService();
0124 
0125     /// Call this from InitPlugin.
0126     template <typename T>
0127     void ProvideService(std::shared_ptr<T> service);
0128 
0129 
0130 private:
0131 
0132     JLogger m_logger;
0133 
0134     std::unique_ptr<JServiceLocator> m_service_locator;
0135 
0136     std::shared_ptr<JParameterManager> m_params;
0137     std::shared_ptr<JPluginLoader> m_plugin_loader;
0138     std::shared_ptr<JComponentManager> m_component_manager;
0139     std::shared_ptr<JExecutionEngine> m_execution_engine;
0140 
0141     bool m_inspect = false;
0142     bool m_quitting = false;
0143     bool m_skip_join = false;
0144     std::atomic_bool m_initialized {false};
0145     std::atomic_bool m_services_available {false};
0146     int  m_exit_code = (int) ExitCode::Success;
0147     int  m_desired_nthreads;
0148 
0149     // For instantaneous rate calculations
0150     std::mutex m_inst_rate_mutex;
0151     std::chrono::steady_clock::time_point m_last_measurement_time = std::chrono::steady_clock::now();
0152     size_t m_last_event_count = 0;
0153 };
0154 
0155 
0156 // This routine is used to bootstrap plugins. It is done outside
0157 // of the JApplication class to ensure it sees the global variables
0158 // that the rest of the plugin's InitPlugin routine sees.
0159 inline void InitJANAPlugin(JApplication* app) {
0160     // Make sure global pointers are pointing to the
0161     // same ones being used by executable
0162     japp = app;
0163 }
0164 
0165