SST 16.0.0
Structural Simulation Toolkit
eventHandlerProfileTool.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_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
28namespace SST::Util {
29class DataRecord;
30}
31
32namespace SST::Profile {
33
34class EventHandlerProfileTool : public ProfileTool, public Event::HandlerBase::AttachPoint, public Link::AttachPoint
35{
36public:
37 SST_ELI_REGISTER_PROFILETOOL_DERIVED_API(SST::Profile::EventHandlerProfileTool, SST::Profile::ProfileTool, Params&)
38
39 SST_ELI_DOCUMENT_PARAMS(
40 { "level", "Level at which to track profile (global, type, component, subcomponent)", "type" },
41 { "track_ports", "Controls whether to track by individual ports", "false" },
42 { "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" },
43 { "profile_receives", "Controls whether receives are profiled", "true" },
44 )
45
46 enum class Profile_Level { Global, Type, Component, Subcomponent };
47
48 EventHandlerProfileTool(const std::string& name, Params& params);
49
50
51 bool profileSends() { return profile_sends_; }
52 bool profileReceives() { return profile_receives_; }
53
54 // Default implementations of attach point functions for profile
55 // tools that don't use them
56 void beforeHandler(uintptr_t UNUSED(key), const Event* UNUSED(event)) override {}
57 void afterHandler(uintptr_t UNUSED(key)) override {}
58 void eventSent(uintptr_t UNUSED(key), Event*& UNUSED(ev)) override {}
59
60
61protected:
62 std::string getKeyForHandler(const AttachPointMetaData& mdata);
63
64 Profile_Level profile_level_;
65 bool track_ports_;
66
67 bool profile_sends_;
68 bool profile_receives_;
69};
70
71
72/**
73 Profile tool that will count the number of times a handler is
74 called
75 */
76class EventHandlerProfileToolCount : public EventHandlerProfileTool
77{
78 struct event_data_t
79 {
80 uint64_t recv_count;
81 uint64_t send_count;
82
83 event_data_t() :
84 recv_count(0),
85 send_count(0)
86 {}
87 };
88
89public:
90 SST_ELI_REGISTER_PROFILETOOL(
93 "sst",
94 "profile.handler.event.count",
95 SST_ELI_ELEMENT_VERSION(0, 1, 0),
96 "Profiler that will count calls to handler functions"
97 )
98
99 EventHandlerProfileToolCount(const std::string& name, Params& params);
100
102
103 uintptr_t registerHandler(const AttachPointMetaData& mdata) override;
104 uintptr_t registerLinkAttachTool(const AttachPointMetaData& mdata) override;
105
106 void beforeHandler(uintptr_t key, const SST::Event* event) override;
107 void eventSent(uintptr_t key, Event*& ev) override;
108
109 void outputData(SST::Util::DataRecord* record, RankInfo rank) override;
110
111private:
112 std::map<std::string, event_data_t> counts_;
113};
114
115/**
116 Profile tool that will count the number of times a handler is
117 called
118 */
119template <typename T>
120class EventHandlerProfileToolTime : public EventHandlerProfileTool
121{
122 struct event_data_t
123 {
124 uint64_t recv_time;
125 uint64_t recv_count;
126 uint64_t send_count;
127
128 event_data_t() :
129 recv_time(0),
130 recv_count(0),
131 send_count(0)
132 {}
133 };
134
135public:
136 EventHandlerProfileToolTime(const std::string& name, Params& params);
137
138 virtual ~EventHandlerProfileToolTime() {}
139
140 uintptr_t registerHandler(const AttachPointMetaData& mdata) override;
141 uintptr_t registerLinkAttachTool(const AttachPointMetaData& mdata) override;
142
143 void beforeHandler(uintptr_t UNUSED(key), const Event* UNUSED(event)) override { start_time_ = T::now(); }
144
145 void afterHandler(uintptr_t key) override
146 {
147 auto total_time = T::now() - start_time_;
148 event_data_t* entry = reinterpret_cast<event_data_t*>(key);
149 entry->recv_time += std::chrono::duration_cast<std::chrono::nanoseconds>(total_time).count();
150 entry->recv_count++;
151 }
152
153 void eventSent(uintptr_t key, Event*& UNUSED(ev)) override { reinterpret_cast<event_data_t*>(key)->send_count++; }
154
155 void outputData(SST::Util::DataRecord* record, RankInfo rank) override;
156
157private:
158 typename T::time_point start_time_;
159 std::map<std::string, event_data_t> times_;
160};
161
162} // namespace SST::Profile
163
164#endif // SST_CORE_PROFILE_EVENTHANDLERPROFILETOOL_H
Main component object for the simulation.
Definition component.h:32
Base class for Events - Items sent across links to communicate between components.
Definition event.h:41
Parameter store.
Definition params.h:65
Profile tool that will count the number of times a handler is called.
Definition eventHandlerProfileTool.h:77
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:89
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:101
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:133
Definition eventHandlerProfileTool.h:35
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
Struct used as a base class for all AttachPoint metadata passed to registration functions.
Definition sst_types.h:201