Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:01:41

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 JArrowProcessingController;
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/Engine/JPerfSummary.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 
0071 
0072     // Controlling processing
0073 
0074     void Initialize(void);
0075     void Run(bool wait_until_finished = true);
0076     void Scale(int nthreads);
0077     void Stop(bool wait_until_idle = false);
0078     void Resume() {};  // TODO: Do we need this?
0079     void Inspect();
0080     void Quit(bool skip_join = false);
0081     void SetExitCode(int exitCode);
0082     int GetExitCode();
0083     void HandleSigint();
0084 
0085 
0086     // Performance/status monitoring
0087 
0088     bool IsInitialized(void){return m_initialized;}
0089     bool IsQuitting(void) { return m_quitting; }
0090     bool IsDrainingQueues(void) { return m_draining_queues; }
0091 
0092     void SetTicker(bool ticker_on = true);
0093     bool IsTickerEnabled();
0094     void SetTimeoutEnabled(bool enabled = true);
0095     bool IsTimeoutEnabled();
0096     void PrintStatus();
0097     void PrintFinalReport();
0098     uint64_t GetNThreads();
0099     uint64_t GetNEventsProcessed();
0100     float GetIntegratedRate();
0101     float GetInstantaneousRate();
0102 
0103     const JComponentSummary& GetComponentSummary();
0104 
0105     // Parameter config
0106 
0107     JParameterManager* GetJParameterManager() { return m_params.get(); }
0108 
0109     template<typename T>
0110     T GetParameterValue(std::string name);
0111 
0112     template <typename T>
0113     JParameter* GetParameter(std::string name, T& val);
0114 
0115     template<typename T>
0116     JParameter* SetParameterValue(std::string name, T val);
0117 
0118     template <typename T>
0119     JParameter* SetDefaultParameter(std::string name, T& val, std::string description="");
0120 
0121     template <typename T>
0122     T RegisterParameter(std::string name, const T default_val, std::string description="");
0123 
0124     // Locating services
0125 
0126     /// Use this in EventSources, Factories, or EventProcessors. Do not call this
0127     /// from InitPlugin(), as not all JServices may have been loaded yet.
0128     /// When initializing a Service, use acquire_services() instead.
0129     template <typename T>
0130     std::shared_ptr<T> GetService();
0131 
0132     /// Call this from InitPlugin.
0133     template <typename T>
0134     void ProvideService(std::shared_ptr<T> service);
0135 
0136 
0137 private:
0138 
0139     JLogger m_logger;
0140 
0141     std::unique_ptr<JServiceLocator> m_service_locator;
0142 
0143     std::shared_ptr<JParameterManager> m_params;
0144     std::shared_ptr<JPluginLoader> m_plugin_loader;
0145     std::shared_ptr<JComponentManager> m_component_manager;
0146     std::shared_ptr<JArrowProcessingController> m_processing_controller;
0147 
0148     bool m_inspecting = false;
0149     bool m_quitting = false;
0150     bool m_draining_queues = false;
0151     bool m_skip_join = false;
0152     std::atomic_bool m_initialized {false};
0153     std::atomic_bool m_services_available {false};
0154     bool m_ticker_on = true;
0155     bool m_timeout_on = true;
0156     bool m_extended_report = false;
0157     int  m_exit_code = (int) ExitCode::Success;
0158     int  m_desired_nthreads;
0159     std::atomic_int m_sigint_count {0};
0160 
0161     std::mutex m_status_mutex;
0162     int m_ticker_interval_ms = 1000;
0163     std::chrono::time_point<std::chrono::high_resolution_clock> m_last_measurement;
0164     std::unique_ptr<const JPerfSummary> m_perf_summary;
0165 
0166     void update_status();
0167 };
0168 
0169 
0170 // This routine is used to bootstrap plugins. It is done outside
0171 // of the JApplication class to ensure it sees the global variables
0172 // that the rest of the plugin's InitPlugin routine sees.
0173 inline void InitJANAPlugin(JApplication* app) {
0174     // Make sure global pointers are pointing to the
0175     // same ones being used by executable
0176     japp = app;
0177 }
0178 
0179