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