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