SST  15.1.0
StructuralSimulationToolkit
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 <cstdint>
24 #include <map>
25 #include <string>
26 
27 namespace SST::Profile {
28 
30 {
31 public:
32  SST_ELI_REGISTER_PROFILETOOL_DERIVED_API(SST::Profile::ClockHandlerProfileTool, SST::Profile::ProfileTool, Params&)
33 
34  SST_ELI_DOCUMENT_PARAMS(
35  { "level", "Level at which to track profile (global, type, component, subcomponent)", "type" },
36  )
37 
38  enum class Profile_Level { Global, Type, Component, Subcomponent };
39 
40  ClockHandlerProfileTool(const std::string& name, Params& params);
41 
42  // Default implementations of attach point functions for profile
43  // tools that don't use them
44  void beforeHandler(uintptr_t UNUSED(key), const Cycle_t& UNUSED(cycle)) override {}
45  void afterHandler(uintptr_t UNUSED(key), const bool& UNUSED(remove)) override {}
46 
47 protected:
48  std::string getKeyForHandler(const AttachPointMetaData& mdata);
49 
50  Profile_Level profile_level_;
51 };
52 
53 
54 /**
55  Profile tool that will count the number of times a handler is
56  called
57  */
59 {
60 public:
61  SST_ELI_REGISTER_PROFILETOOL(
64  "sst",
65  "profile.handler.clock.count",
66  SST_ELI_ELEMENT_VERSION(0, 1, 0),
67  "Profiler that will count calls to handler functions"
68  )
69 
70  ClockHandlerProfileToolCount(const std::string& name, Params& params);
71 
72  virtual ~ClockHandlerProfileToolCount() {}
73 
74  uintptr_t registerHandler(const AttachPointMetaData& mdata) override;
75 
76  void beforeHandler(uintptr_t key, const Cycle_t& cycle) override;
77 
78  void outputData(FILE* fp) override;
79 
80 private:
81  std::map<std::string, uint64_t> counts_;
82 };
83 
84 /**
85  Profile tool that will count the number of times a handler is
86  called
87  */
88 template <typename T>
90 {
91  struct clock_data_t
92  {
93  uint64_t time;
94  uint64_t count;
95 
96  clock_data_t() :
97  time(0),
98  count(0)
99  {}
100  };
101 
102 public:
103  ClockHandlerProfileToolTime(const std::string& name, Params& params);
104 
105  virtual ~ClockHandlerProfileToolTime() {}
106 
107  uintptr_t registerHandler(const AttachPointMetaData& mdata) override;
108 
109  void beforeHandler(uintptr_t UNUSED(key), const Cycle_t& UNUSED(cycle)) override { start_time_ = T::now(); }
110 
111  void afterHandler(uintptr_t key, const bool& UNUSED(remove)) override
112  {
113  auto total_time = T::now() - start_time_;
114  clock_data_t* entry = reinterpret_cast<clock_data_t*>(key);
115  entry->time += std::chrono::duration_cast<std::chrono::nanoseconds>(total_time).count();
116  entry->count++;
117  }
118 
119  void outputData(FILE* fp) override;
120 
121 private:
122  typename T::time_point start_time_;
123  std::map<std::string, clock_data_t> times_;
124 };
125 
126 } // namespace SST::Profile
127 
128 #endif // SST_CORE_PROFILE_CLOCKHANDLERPROFILETOOL_H
uintptr_t registerHandler(const AttachPointMetaData &mdata) override
Function that will be called when a handler is registered with the tool implementing the attach point...
Definition: clockHandlerProfileTool.cc:75
Profile tool that will count the number of times a handler is called.
Definition: clockHandlerProfileTool.h:89
Profile tool that will count the number of times a handler is called.
Definition: clockHandlerProfileTool.h:58
Main component object for the simulation.
Definition: component.h:30
Definition: link.h:37
ProfileTool is a class loadable through the factory which allows dynamic addition of profiling capabi...
Definition: profiletool.h:31
uintptr_t registerHandler(const AttachPointMetaData &mdata) override
Function that will be called when a handler is registered with the tool implementing the attach point...
Definition: clockHandlerProfileTool.cc:105
Attach Point to get notified when a handler starts and stops.
Definition: ssthandler.h:117
Parameter store.
Definition: params.h:63
Definition: clockHandlerProfileTool.h:29
Struct used as a base class for all AttachPoint metadata passed to registration functions.
Definition: sst_types.h:80