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