Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-27 09:45:45

0001 // SPDX-License-Identifier: LGPL-3.0-or-later
0002 // Copyright (C) 2024, EICrecon contributors
0003 
0004 #pragma once
0005 
0006 #include <JANA/JEventProcessor.h>
0007 #include <spdlog/logger.h>
0008 #include <compare>
0009 #include <cstddef>
0010 #include <map>
0011 #include <memory>
0012 #include <mutex>
0013 #include <ostream>
0014 #include <set>
0015 #include <string>
0016 #include <vector>
0017 
0018 class JEventProcessorCallGraph : public JEventProcessor {
0019 private:
0020   enum node_type { kDefault, kProcessor, kFactory, kCache, kSource };
0021   enum graph_type { kSingleGraph, kPluginGraph, kOverviewGraph };
0022 
0023   class CallLink {
0024   public:
0025     std::string caller_name;
0026     std::string caller_tag;
0027     std::string callee_name;
0028     std::string callee_tag;
0029 
0030     bool operator<(const CallLink& link) const {
0031       if (this->caller_name != link.caller_name)
0032         return this->caller_name < link.caller_name;
0033       if (this->callee_name != link.callee_name)
0034         return this->callee_name < link.callee_name;
0035       if (this->caller_tag != link.caller_tag)
0036         return this->caller_tag < link.caller_tag;
0037       return this->callee_tag < link.callee_tag;
0038     }
0039   };
0040 
0041   class CallStats {
0042   public:
0043     CallStats(void) {
0044       from_cache_ms         = 0;
0045       from_source_ms        = 0;
0046       from_factory_ms       = 0;
0047       data_not_available_ms = 0;
0048       Nfrom_cache           = 0;
0049       Nfrom_source          = 0;
0050       Nfrom_factory         = 0;
0051       Ndata_not_available   = 0;
0052     }
0053     double from_cache_ms;
0054     double from_source_ms;
0055     double from_factory_ms;
0056     double data_not_available_ms;
0057     unsigned int Nfrom_cache;
0058     unsigned int Nfrom_source;
0059     unsigned int Nfrom_factory;
0060     unsigned int Ndata_not_available;
0061   };
0062 
0063   class FactoryCallStats {
0064   public:
0065     FactoryCallStats(void) {
0066       type           = kDefault;
0067       time_waited_on = 0.0;
0068       time_waiting   = 0.0;
0069       Nfrom_factory  = 0;
0070       Nfrom_source   = 0;
0071       Nfrom_cache    = 0;
0072     }
0073     node_type type;
0074     double time_waited_on; // time other factories spent waiting on this factory
0075     double time_waiting;   // time this factory spent waiting on other factories
0076     unsigned int Nfrom_factory;
0077     unsigned int Nfrom_source;
0078     unsigned int Nfrom_cache;
0079   };
0080 
0081 public:
0082   JEventProcessorCallGraph() : JEventProcessor() { SetTypeName("JEventProcessorCallGraph"); };
0083 
0084   void Init() override;
0085   void BeginRun(const std::shared_ptr<const JEvent>& /* event */) override {};
0086   void Process(const std::shared_ptr<const JEvent>& event) override;
0087   void EndRun() override {};
0088   void Finish() override;
0089 
0090 private:
0091   std::shared_ptr<spdlog::logger> m_log;
0092   std::mutex mutex;
0093   bool factory_mapping_built = false;
0094   std::size_t event_count    = 0;
0095 
0096   std::map<CallLink, CallStats> call_links;
0097   std::map<std::string, FactoryCallStats> factory_stats;
0098   std::map<std::string, std::string> nametag_to_plugin; // Maps nametag to plugin name
0099   std::map<std::string, std::string>
0100       nametag_to_factory_id; // Maps nametag to factory ID (for grouping)
0101   std::map<std::string, std::vector<std::string>> factory_outputs; // Maps factory ID to output tags
0102 
0103   // Configuration parameters
0104   std::string output_filename;
0105   bool enable_splitting;
0106 
0107   // Group-related parameters
0108   std::map<std::string, std::string> user_group_colors; // Group name -> color
0109   std::map<std::string, std::string> nametag_to_group;  // Nametag -> group name
0110   struct GroupEntry {
0111     std::string group_name;
0112     std::string entry;                 // As written in the parameter
0113     std::vector<std::string> nametags; // Its keys in nametag_to_group
0114   };
0115   std::vector<GroupEntry> user_group_entries; // To warn about entries that match nothing
0116 
0117   // Helper methods
0118   std::string MakeTimeString(double time_in_ms);
0119   std::string MakeNametag(const std::string& name, const std::string& tag);
0120   std::string GetBaseFilename();
0121   std::string SanitizeForFilename(const std::string& name);
0122   std::string EscapeForDot(const std::string& s);
0123   std::string EscapeForHtml(const std::string& s);
0124   double ClampPercent(double percent);
0125   std::string GetFactoryNodeName(const std::string& nametag);
0126   std::string ResolveGroupName(const std::string& nametag);
0127   node_type GetNodeType(const std::string& name, const std::string& tag);
0128   std::string GetNodeColorFromPercent(double percent);
0129   std::string GetNodeShape(node_type type);
0130   double GetSelfTime(const FactoryCallStats& fstats);
0131   double GetTotalSelfTime();
0132 
0133   // DOT file generation methods
0134   void WriteGraphLabel(std::ostream& ofs, const std::string& title, graph_type type);
0135   void WriteDotFile();
0136   void WriteSingleDotFile(const std::string& filename);
0137   void WriteSplitDotFiles();
0138   void WritePluginGraphs(const std::map<std::string, std::set<std::string>>& plugin_groups);
0139   void WritePluginDotFile(const std::string& plugin_name, const std::set<std::string>& nodes);
0140   void WriteOverallDotFile(const std::map<std::string, std::set<std::string>>& plugin_groups);
0141   std::map<std::string, std::set<std::string>> SplitGraphByPlugin();
0142 };