SST  15.1.0
StructuralSimulationToolkit
componentProfileTool.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_COMPONENTPROFILETOOL_H
13 #define SST_CORE_PROFILE_COMPONENTPROFILETOOL_H
14 
15 #include "sst/core/eli/elementinfo.h"
16 #include "sst/core/profile/profiletool.h"
17 #include "sst/core/sst_types.h"
18 #include "sst/core/warnmacros.h"
19 
20 #include <chrono>
21 #include <cstdint>
22 #include <map>
23 #include <string>
24 #include <utility>
25 #include <vector>
26 
27 namespace SST {
28 class BaseComponent;
29 }
30 
31 namespace SST::Profile {
32 
33 // Base class for profiling tools designed to profile in Components
34 // and SubComponents. For these types of profiling tools, you can
35 // trace at various levels:
36 
37 // 1 - Global: all profiling will be consolidated into one global
38 // profile
39 
40 // 2 - Type: all profiling will be consolidated into one profile per
41 // (Sub)Component type.
42 
43 // 3 - Component: profiling will be consolidated at the Component
44 // level and all SubComponent data will be consolidated with its
45 // parent component
46 
47 // 4 - SubComponent: profiling will be consolidated at the
48 // SubComponent level
50 {
51 public:
52  SST_ELI_REGISTER_PROFILETOOL_DERIVED_API(SST::Profile::ComponentProfileTool, SST::Profile::ProfileTool, Params&)
53 
54  SST_ELI_DOCUMENT_PARAMS(
55  { "level", "Level at which to track profile (global, type, component, subcomponent)", "type" },
56  { "track_points", "Determines whether independent profiling points are tracked", "true" },
57  )
58 
59  enum class Profile_Level { Global, Type, Component, Subcomponent };
60 
61  ComponentProfileTool(const std::string& name, Params& params);
62 
63 
64  virtual uintptr_t registerProfilePoint(
65  const std::string& point, ComponentId_t id, const std::string& name, const std::string& type) = 0;
66  std::string getKeyForCodeSegment(
67  const std::string& point, ComponentId_t id, const std::string& name, const std::string& type);
68 
69 protected:
70  Profile_Level profile_level_;
71 
72 private:
73  bool track_points_;
74 };
75 
76 
77 // Profiler API designed to profile code segments in Components and
78 // SubComponents
80 {
81  friend class BaseComponent;
82 
83 public:
85 
86  ComponentCodeSegmentProfileTool(const std::string& name, Params& params);
87 
88  virtual void codeSegmentStart(uintptr_t UNUSED(key)) {}
89  virtual void codeSegmentEnd(uintptr_t UNUSED(key)) {}
90 
92  {
93  public:
94  ProfilePoint() {}
95  ~ProfilePoint() {}
96 
97  inline void codeSegmentStart()
98  {
99  for ( auto& x : tools ) {
100  x.first->codeSegmentStart(x.second);
101  }
102  }
103  inline void codeSegmentEnd()
104  {
105  for ( auto& x : tools ) {
106  x.first->codeSegmentEnd(x.second);
107  }
108  }
109 
110  void registerProfilePoint(ComponentCodeSegmentProfileTool* tool, const std::string& point, ComponentId_t id,
111  const std::string& name, const std::string& type)
112  {
113  uintptr_t key = tool->registerProfilePoint(point, id, name, type);
114  tools.push_back(std::make_pair(tool, key));
115  }
116 
117  private:
118  std::vector<std::pair<ComponentCodeSegmentProfileTool*, uintptr_t>> tools;
119  };
120 };
121 
122 /**
123  Profile tool that will count the number of times a handler is
124  called
125  */
127 {
128 
129 public:
130  SST_ELI_REGISTER_PROFILETOOL(
133  "sst",
134  "profile.component.codesegment.count",
135  SST_ELI_ELEMENT_VERSION(0, 1, 0),
136  "Profiler that will count times through a marked code segment"
137  )
138 
139  ComponentCodeSegmentProfileToolCount(const std::string& name, Params& params);
140 
142 
143  uintptr_t registerProfilePoint(
144  const std::string& point, ComponentId_t id, const std::string& name, const std::string& type) override;
145 
146  void codeSegmentStart(uintptr_t key) override;
147 
148  void outputData(FILE* fp) override;
149 
150 private:
151  std::map<std::string, uint64_t> counts_;
152 };
153 
154 /**
155  Profile tool that will count the number of times a handler is
156  called
157  */
158 template <typename T>
160 {
161  struct segment_data_t
162  {
163  uint64_t time;
164  uint64_t count;
165 
166  segment_data_t() :
167  time(0),
168  count(0)
169  {}
170  };
171 
172 public:
173  ComponentCodeSegmentProfileToolTime(const std::string& name, Params& params);
174 
176 
177  uintptr_t registerProfilePoint(
178  const std::string& point, ComponentId_t id, const std::string& name, const std::string& type) override;
179 
180  void codeSegmentStart(uintptr_t UNUSED(key)) override { start_time_ = T::now(); }
181 
182  void codeSegmentEnd(uintptr_t key) override
183  {
184  auto total_time = T::now() - start_time_;
185  segment_data_t* entry = reinterpret_cast<segment_data_t*>(key);
186  entry->time += std::chrono::duration_cast<std::chrono::nanoseconds>(total_time).count();
187  entry->count++;
188  }
189 
190  void outputData(FILE* fp) override;
191 
192 private:
193  typename T::time_point start_time_;
194  std::map<std::string, segment_data_t> times_;
195 };
196 
197 } // namespace SST::Profile
198 
199 #endif // SST_CORE_PROFILE_COMPONENTPROFILETOOL_H
Profile tool that will count the number of times a handler is called.
Definition: componentProfileTool.h:126
Main component object for the simulation.
Definition: component.h:30
Definition: link.h:37
Definition: action.cc:18
ProfileTool is a class loadable through the factory which allows dynamic addition of profiling capabi...
Definition: profiletool.h:31
Main component object for the simulation.
Definition: baseComponent.h:64
Definition: componentProfileTool.h:49
Profile tool that will count the number of times a handler is called.
Definition: componentProfileTool.h:159
Definition: componentProfileTool.h:79
Parameter store.
Definition: params.h:63