SST  15.1.0
StructuralSimulationToolkit
eventHandlerProfileTool.h
1 // Copyright 2009-2025 NTESS. Under the terms
2 // of Contract DE-NA0003525 with NTESS, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2025, NTESS
6 // All rights reserved.
7 //
8 // This file is part of the SST software package. For license
9 // information, see the LICENSE file in the top level directory of the
10 // distribution.
11 
12 #ifndef SST_CORE_PROFILE_EVENTHANDLERPROFILETOOL_H
13 #define SST_CORE_PROFILE_EVENTHANDLERPROFILETOOL_H
14 
15 #include "sst/core/eli/elementinfo.h"
16 #include "sst/core/event.h"
17 #include "sst/core/link.h"
18 #include "sst/core/profile/profiletool.h"
19 #include "sst/core/sst_types.h"
20 #include "sst/core/ssthandler.h"
21 #include "sst/core/warnmacros.h"
22 
23 #include <chrono>
24 #include <cstdint>
25 #include <map>
26 #include <string>
27 
28 namespace SST::Profile {
29 
31 {
32 public:
33  SST_ELI_REGISTER_PROFILETOOL_DERIVED_API(SST::Profile::EventHandlerProfileTool, SST::Profile::ProfileTool, Params&)
34 
35  SST_ELI_DOCUMENT_PARAMS(
36  { "level", "Level at which to track profile (global, type, component, subcomponent)", "type" },
37  { "track_ports", "Controls whether to track by individual ports", "false" },
38  { "profile_sends", "Controls whether sends are profiled (due to location of profiling point in the code, turning on send profiling will incur relatively high overhead)", "false" },
39  { "profile_receives", "Controls whether receives are profiled", "true" },
40  )
41 
42  enum class Profile_Level { Global, Type, Component, Subcomponent };
43 
44  EventHandlerProfileTool(const std::string& name, Params& params);
45 
46 
47  bool profileSends() { return profile_sends_; }
48  bool profileReceives() { return profile_receives_; }
49 
50  // Default implementations of attach point functions for profile
51  // tools that don't use them
52  void beforeHandler(uintptr_t UNUSED(key), const Event* UNUSED(event)) override {}
53  void afterHandler(uintptr_t UNUSED(key)) override {}
54  void eventSent(uintptr_t UNUSED(key), Event*& UNUSED(ev)) override {}
55 
56 
57 protected:
58  std::string getKeyForHandler(const AttachPointMetaData& mdata);
59 
60  Profile_Level profile_level_;
61  bool track_ports_;
62 
63  bool profile_sends_;
64  bool profile_receives_;
65 };
66 
67 
68 /**
69  Profile tool that will count the number of times a handler is
70  called
71  */
73 {
74  struct event_data_t
75  {
76  uint64_t recv_count;
77  uint64_t send_count;
78 
79  event_data_t() :
80  recv_count(0),
81  send_count(0)
82  {}
83  };
84 
85 public:
86  SST_ELI_REGISTER_PROFILETOOL(
89  "sst",
90  "profile.handler.event.count",
91  SST_ELI_ELEMENT_VERSION(0, 1, 0),
92  "Profiler that will count calls to handler functions"
93  )
94 
95  EventHandlerProfileToolCount(const std::string& name, Params& params);
96 
97  virtual ~EventHandlerProfileToolCount() {}
98 
99  uintptr_t registerHandler(const AttachPointMetaData& mdata) override;
100  uintptr_t registerLinkAttachTool(const AttachPointMetaData& mdata) override;
101 
102  void beforeHandler(uintptr_t key, const SST::Event* event) override;
103  void eventSent(uintptr_t key, Event*& ev) override;
104 
105  void outputData(FILE* fp) override;
106 
107 private:
108  std::map<std::string, event_data_t> counts_;
109 };
110 
111 /**
112  Profile tool that will count the number of times a handler is
113  called
114  */
115 template <typename T>
117 {
118  struct event_data_t
119  {
120  uint64_t recv_time;
121  uint64_t recv_count;
122  uint64_t send_count;
123 
124  event_data_t() :
125  recv_time(0),
126  recv_count(0),
127  send_count(0)
128  {}
129  };
130 
131 public:
132  EventHandlerProfileToolTime(const std::string& name, Params& params);
133 
134  virtual ~EventHandlerProfileToolTime() {}
135 
136  uintptr_t registerHandler(const AttachPointMetaData& mdata) override;
137  uintptr_t registerLinkAttachTool(const AttachPointMetaData& mdata) override;
138 
139  void beforeHandler(uintptr_t UNUSED(key), const Event* UNUSED(event)) override { start_time_ = T::now(); }
140 
141  void afterHandler(uintptr_t key) override
142  {
143  auto total_time = T::now() - start_time_;
144  event_data_t* entry = reinterpret_cast<event_data_t*>(key);
145  entry->recv_time += std::chrono::duration_cast<std::chrono::nanoseconds>(total_time).count();
146  entry->recv_count++;
147  }
148 
149  void eventSent(uintptr_t key, Event*& UNUSED(ev)) override { reinterpret_cast<event_data_t*>(key)->send_count++; }
150 
151  void outputData(FILE* fp) override;
152 
153 private:
154  typename T::time_point start_time_;
155  std::map<std::string, event_data_t> times_;
156 };
157 
158 } // namespace SST::Profile
159 
160 #endif // SST_CORE_PROFILE_EVENTHANDLERPROFILETOOL_H
Main component object for the simulation.
Definition: component.h:30
Definition: link.h:37
uintptr_t registerLinkAttachTool(const AttachPointMetaData &mdata) override
Function that will be called when an attach point is registered with the tool implementing the attach...
Definition: eventHandlerProfileTool.cc:88
ProfileTool is a class loadable through the factory which allows dynamic addition of profiling capabi...
Definition: profiletool.h:31
Definition: eventHandlerProfileTool.h:30
uintptr_t registerHandler(const AttachPointMetaData &mdata) override
Function that will be called when a handler is registered with the tool implementing the attach point...
Definition: eventHandlerProfileTool.cc:82
Profile tool that will count the number of times a handler is called.
Definition: eventHandlerProfileTool.h:116
uintptr_t registerLinkAttachTool(const AttachPointMetaData &mdata) override
Function that will be called when an attach point is registered with the tool implementing the attach...
Definition: eventHandlerProfileTool.cc:137
Attach Point to get notified when a handler starts and stops.
Definition: ssthandler.h:117
Profile tool that will count the number of times a handler is called.
Definition: eventHandlerProfileTool.h:72
uintptr_t registerHandler(const AttachPointMetaData &mdata) override
Function that will be called when a handler is registered with the tool implementing the attach point...
Definition: eventHandlerProfileTool.cc:130
Parameter store.
Definition: params.h:63
Struct used as a base class for all AttachPoint metadata passed to registration functions.
Definition: sst_types.h:80
void eventSent(uintptr_t key, Event *&ev) override
Function that will be called when an event is sent on a link with registered attach points...
Definition: eventHandlerProfileTool.cc:100
Base class for Events - Items sent across links to communicate between components.
Definition: event.h:40