SST 15.0
Structural Simulation Toolkit
clockHandlerProfileTool.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_CLOCKHANDLERPROFILETOOL_H
13#define SST_CORE_PROFILE_CLOCKHANDLERPROFILETOOL_H
14
15#include "sst/core/clock.h"
16#include "sst/core/eli/elementinfo.h"
17#include "sst/core/profile/profiletool.h"
18#include "sst/core/sst_types.h"
19#include "sst/core/ssthandler.h"
20#include "sst/core/warnmacros.h"
21
22#include <chrono>
23#include <map>
24#include <string>
25
26namespace SST::Profile {
27
28class ClockHandlerProfileTool : public ProfileTool, public Clock::HandlerBase::AttachPoint
29{
30public:
31 SST_ELI_REGISTER_PROFILETOOL_DERIVED_API(SST::Profile::ClockHandlerProfileTool, SST::Profile::ProfileTool, Params&)
32
33 SST_ELI_DOCUMENT_PARAMS(
34 { "level", "Level at which to track profile (global, type, component, subcomponent)", "type" },
35 )
36
37 enum class Profile_Level { Global, Type, Component, Subcomponent };
38
39 ClockHandlerProfileTool(const std::string& name, Params& params);
40
41 // Default implementations of attach point functions for profile
42 // tools that don't use them
43 void beforeHandler(uintptr_t UNUSED(key), const Cycle_t& UNUSED(cycle)) override {}
44 void afterHandler(uintptr_t UNUSED(key), const bool& UNUSED(remove)) override {}
45
46protected:
47 std::string getKeyForHandler(const AttachPointMetaData& mdata);
48
49 Profile_Level profile_level_;
50};
51
52
53/**
54 Profile tool that will count the number of times a handler is
55 called
56 */
57class ClockHandlerProfileToolCount : public ClockHandlerProfileTool
58{
59public:
60 SST_ELI_REGISTER_PROFILETOOL(
63 "sst",
64 "profile.handler.clock.count",
65 SST_ELI_ELEMENT_VERSION(0, 1, 0),
66 "Profiler that will count calls to handler functions"
67 )
68
69 ClockHandlerProfileToolCount(const std::string& name, Params& params);
70
72
73 uintptr_t registerHandler(const AttachPointMetaData& mdata) override;
74
75 void beforeHandler(uintptr_t key, const Cycle_t& cycle) override;
76
77 void outputData(FILE* fp) override;
78
79private:
80 std::map<std::string, uint64_t> counts_;
81};
82
83/**
84 Profile tool that will count the number of times a handler is
85 called
86 */
87template <typename T>
88class ClockHandlerProfileToolTime : public ClockHandlerProfileTool
89{
90 struct clock_data_t
91 {
92 uint64_t time;
93 uint64_t count;
94
95 clock_data_t() :
96 time(0),
97 count(0)
98 {}
99 };
100
101public:
102 ClockHandlerProfileToolTime(const std::string& name, Params& params);
103
104 virtual ~ClockHandlerProfileToolTime() {}
105
106 uintptr_t registerHandler(const AttachPointMetaData& mdata) override;
107
108 void beforeHandler(uintptr_t UNUSED(key), const Cycle_t& UNUSED(cycle)) override { start_time_ = T::now(); }
109
110 void afterHandler(uintptr_t key, const bool& UNUSED(remove)) override
111 {
112 auto total_time = T::now() - start_time_;
113 clock_data_t* entry = reinterpret_cast<clock_data_t*>(key);
114 entry->time += std::chrono::duration_cast<std::chrono::nanoseconds>(total_time).count();
115 entry->count++;
116 }
117
118 void outputData(FILE* fp) override;
119
120private:
121 typename T::time_point start_time_;
122 std::map<std::string, clock_data_t> times_;
123};
124
125} // namespace SST::Profile
126
127#endif // SST_CORE_PROFILE_CLOCKHANDLERPROFILETOOL_H
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 clockHandlerProfileTool.h:58
Definition clockHandlerProfileTool.h:29
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