Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-07-13 08:37:12

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