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