CbmRoot
Loading...
Searching...
No Matches
CaMonitor.h
Go to the documentation of this file.
1/* Copyright (C) 2023 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Sergei Zharko [committer] */
4
9
10#pragma once // include this header only once per compilation unit
11
12#include "CaEnumArray.h"
13#include "CaMonitorData.h"
14
15#include <boost/serialization/access.hpp>
16#include <boost/serialization/string.hpp>
17#include <boost/serialization/vector.hpp>
18
19#include <algorithm>
20#include <iomanip>
21#include <sstream>
22#include <string>
23#include <string_view>
24#include <vector>
25
26
27namespace cbm::algo::ca
28{
34 template<class ECounterKey, class ETimerKey = EDummy>
35 class Monitor {
36 public:
38 template<typename T>
40
42 template<typename T>
44
47 Monitor(std::string_view name) : fsName(name) {}
48
50 Monitor() = default;
51
53 ~Monitor() = default;
54
56 Monitor(const Monitor&) = delete;
57
59 Monitor(Monitor&&) = delete;
60
62 Monitor& operator=(const Monitor&) = delete;
63
66
70 void AddMonitorData(const MonitorData<ECounterKey, ETimerKey>& data, bool parallel = false)
71 {
72 fMonitorData.AddMonitorData(data, parallel);
73 }
74
77 const std::string& GetCounterName(ECounterKey key) const { return faCounterNames[key]; }
78
81 int GetCounterValue(ECounterKey key) const { return fMonitorData.GetCounterValue(key); }
82
85
87 const std::string& GetName() const { return fsName; }
88
91 const Timer& GetTimer(ETimerKey key) const { return fMonitorData.GetTimer(key); }
92
95 const std::string& GetTimerName(ETimerKey key) const { return faTimerNames[key]; }
96
99 void IncrementCounter(ECounterKey key) { fMonitorData.IncrementCounter(key); };
100
104 void IncrementCounter(ECounterKey key, int num) { fMonitorData.IncrementCounter(key, num); }
105
107 void Reset() { fMonitorData.Reset(); }
108
111 void SetRatioKeys(const std::vector<ECounterKey>& vKeys) { fvCounterRatioKeys = vKeys; }
112
115 void SetCounterName(ECounterKey key, std::string_view name) { faCounterNames[key] = name; }
116
120
123 void SetName(std::string_view name) { fsName = name; }
124
127 void SetTimerName(ETimerKey key, std::string_view name) { faTimerNames[key] = name; }
128
131 void StartTimer(ETimerKey key) { fMonitorData.StartTimer(key); }
132
135 void StopTimer(ETimerKey key) { fMonitorData.StopTimer(key); }
136
138 std::string ToString() const;
139
141 template<typename Archive>
142 void serialize(Archive& ar, const unsigned int /*version*/)
143 {
144 ar& fMonitorData;
145 ar& faTimerNames;
146 ar& faCounterNames;
148 ar& fsName;
149 }
150
151 private:
155 std::vector<ECounterKey> fvCounterRatioKeys{};
156 std::string fsName;
157 };
158
159
160 // *****************************************
161 // ** Template function implementations **
162 // *****************************************
163
164 // ---------------------------------------------------------------------------------------------------------------------
165 //
166 template<class ECounterKey, class ETimerKey>
168 {
169 using std::left;
170 using std::right;
171 using std::setfill;
172 using std::setw;
173 std::stringstream msg;
174 constexpr size_t widthKey = 30;
175 constexpr size_t width = 24;
176 msg << "\n===== Monitor: " << fsName << "\n";
177 if constexpr (!std::is_same_v<ETimerKey, EDummy>) {
178 msg << "\n----- Timers:\n";
179 msg << setw(widthKey) << left << "Key" << ' ';
180 msg << setw(width) << left << "N Calls" << ' ';
181 msg << setw(width) << left << "Average [s]" << ' ';
182 msg << setw(width) << left << "Min [s]" << ' ';
183 msg << setw(width) << left << "Max [s]" << ' ';
184 msg << setw(width) << left << "Total [s]" << '\n';
185 msg << right;
186 for (int iKey = 0; iKey < fMonitorData.GetNofTimers(); ++iKey) {
187 const Timer& timer = fMonitorData.GetTimer(static_cast<ETimerKey>(iKey));
188 msg << setw(widthKey) << faTimerNames[iKey] << ' ';
189 msg << setw(width) << timer.GetNofCalls() << ' ';
190 msg << setw(width) << timer.GetAverage() << ' ';
191 msg << setw(width) << timer.GetMin() << ' ';
192 msg << setw(width) << timer.GetMax() << ' ';
193 msg << setw(width) << timer.GetTotal() << '\n';
194 }
195 }
196
197 msg << "\n----- Counters:\n";
198 msg << setw(widthKey) << left << "Key" << ' ';
199 msg << setw(width) << left << "Total" << ' ';
200 for (auto key : fvCounterRatioKeys) {
201 msg << setw(width) << left << std::string("per ") + faCounterNames[key] << ' ';
202 }
203 msg << '\n';
204 for (int iKey = 0; iKey < fMonitorData.GetNofCounters(); ++iKey) {
205 auto counterValue = fMonitorData.GetCounterValue(static_cast<ECounterKey>(iKey));
206 msg << setw(widthKey) << left << faCounterNames[iKey] << ' ';
207 msg << setw(width) << right << counterValue << ' ';
208 for (auto keyDen : fvCounterRatioKeys) {
209 auto ratio = static_cast<double>(counterValue) / fMonitorData.GetCounterValue(keyDen);
210 msg << setw(width) << right << ratio << ' ';
211 }
212 msg << '\n';
213 }
214 return msg.str();
215 }
216} // namespace cbm::algo::ca
Implementation of cbm::algo::ca::EnumArray class.
A block of data for cbm::algo::ca::Monitor.
Class of arrays, which can be accessed by an enum class entry as an index.
Monitor data block.
Definition Reco.h:80
Monitor class for the CA tracking.
Definition CaMonitor.h:35
TimerArray< std::string > faTimerNames
Array of timer names.
Definition CaMonitor.h:153
Monitor()=default
Default constructor.
const std::string & GetCounterName(ECounterKey key) const
Gets counter name.
Definition CaMonitor.h:77
void SetMonitorData(const MonitorData< ECounterKey, ETimerKey > &data)
Sets monitor data.
Definition CaMonitor.h:119
void IncrementCounter(ECounterKey key, int num)
Increments key counter by a number.
Definition CaMonitor.h:104
std::string ToString() const
Prints counters summary to string.
Definition CaMonitor.h:167
void IncrementCounter(ECounterKey key)
Increments key counter by 1.
Definition CaMonitor.h:99
void SetName(std::string_view name)
Sets name of the monitor.
Definition CaMonitor.h:123
void Reset()
Resets the counters.
Definition CaMonitor.h:107
CounterArray< std::string > faCounterNames
Array of counter names.
Definition CaMonitor.h:154
void SetCounterName(ECounterKey key, std::string_view name)
Sets name of counter.
Definition CaMonitor.h:115
void StartTimer(ETimerKey key)
Starts timer.
Definition CaMonitor.h:131
Monitor(std::string_view name)
Constructor.
Definition CaMonitor.h:47
std::string fsName
Name of the monitor.
Definition CaMonitor.h:156
Monitor & operator=(Monitor &&)=delete
Move assignment operator.
Monitor(const Monitor &)=delete
Copy constructor.
Monitor & operator=(const Monitor &)=delete
Copy assignment operator.
~Monitor()=default
Destructor.
MonitorData< ECounterKey, ETimerKey > fMonitorData
Monitor data.
Definition CaMonitor.h:152
void StopTimer(ETimerKey key)
Stops timer.
Definition CaMonitor.h:135
const std::string & GetName() const
Gets name of the monitor.
Definition CaMonitor.h:87
std::vector< ECounterKey > fvCounterRatioKeys
List of keys, which are used as denominators in ratios.
Definition CaMonitor.h:155
void serialize(Archive &ar, const unsigned int)
Definition CaMonitor.h:142
const MonitorData< ECounterKey, ETimerKey > & GetMonitorData() const
Gets monitor data.
Definition CaMonitor.h:84
void SetTimerName(ETimerKey key, std::string_view name)
Sets name of the timer.
Definition CaMonitor.h:127
friend class boost::serialization::access
Definition CaMonitor.h:140
void AddMonitorData(const MonitorData< ECounterKey, ETimerKey > &data, bool parallel=false)
Adds the other monitor data to this.
Definition CaMonitor.h:70
int GetCounterValue(ECounterKey key) const
Gets counter value.
Definition CaMonitor.h:81
const Timer & GetTimer(ETimerKey key) const
Gets timer.
Definition CaMonitor.h:91
const std::string & GetTimerName(ETimerKey key) const
Gets timer name.
Definition CaMonitor.h:95
Monitor(Monitor &&)=delete
Move constructor.
void SetRatioKeys(const std::vector< ECounterKey > &vKeys)
Sets keys of counters, which are used as denominators for ratios.
Definition CaMonitor.h:111
A timer class for the monitor.
Definition CaTimer.h:25
double GetTotal() const
Gets total time [s].
Definition CaTimer.h:77
double GetMin() const
Gets time of the shortest call [s].
Definition CaTimer.h:65
double GetAverage() const
Gets average time [s].
Definition CaTimer.h:59
int GetNofCalls() const
Gets number of calls.
Definition CaTimer.h:68
double GetMax() const
Gets time of the longest call [s].
Definition CaTimer.h:62
TODO: SZh 8.11.2022: add selection of parameterisation.
Definition CaBranch.h:14