Belle II Software development
ZMQLogger.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#pragma once
9
10#include <variant>
11#include <chrono>
12#include <vector>
13#include <map>
14#include <unordered_map>
15#include <numeric>
16#include <string>
17
18namespace Belle2 {
35 class ZMQLogger {
36 public:
38 virtual std::string getMonitoringJSON() const;
39
41 template<class AClass>
42 void log(const std::string& key, const AClass& value);
44 void increment(const std::string& key);
46 void decrement(const std::string& key);
48 template<size_t MAX_SIZE = 100>
49 void average(const std::string& key, double value);
51 template<size_t AVERAGE_SIZE = 2000>
52 void timeit(const std::string& key);
54 void logTime(const std::string& key);
55
56 private:
58 std::map<std::string, std::variant<long, double, std::string>> m_monitoring;
60 std::unordered_map<std::string, std::tuple<std::vector<double>, size_t>> m_averages;
62 std::unordered_map<std::string, std::tuple<unsigned long, std::chrono::system_clock::time_point>> m_timeCounters;
63
65 struct toJSON {
67 std::string operator()(long value);
69 std::string operator()(double value);
71 std::string operator()(const std::string& value);
72 };
73
75 struct Incrementor {
77 void operator()(long& value);
79 void operator()(double& value);
81 void operator()(std::string&);
82 };
83
85 struct Decrementor {
87 void operator()(long& value);
89 void operator()(double& value);
91 void operator()(std::string&);
92 };
93 };
94
95 template<class AClass>
96 void ZMQLogger::log(const std::string& key, const AClass& value)
97 {
98 m_monitoring[key] = value;
99 }
100
101 template<size_t MAX_SIZE>
102 void ZMQLogger::average(const std::string& key, double value)
103 {
104 auto& [averages, index] = m_averages[key];
105
106 if (averages.size() == MAX_SIZE) {
107 averages[index] = value;
108 index = (index + 1) % MAX_SIZE;
109 } else {
110 averages.push_back(value);
111 }
112
113 log(key, std::accumulate(averages.begin(), averages.end(), 0.0) / averages.size());
114 }
115
116 template<size_t AVERAGE_SIZE>
117 void ZMQLogger::timeit(const std::string& key)
118 {
119 auto& [calls, timestamp] = m_timeCounters[key];
120 if (calls % AVERAGE_SIZE == 0) {
121 auto current = std::chrono::high_resolution_clock::now();
122 double elapsed = std::chrono::duration_cast<std::chrono::duration<double>>(
123 current - timestamp).count();
124
125 log(key, AVERAGE_SIZE / elapsed);
126
127 timestamp = current;
128 calls = 0;
129
130 auto displayTime = std::chrono::system_clock::to_time_t(current);
131 log(key + "_last_measurement", std::ctime(&displayTime));
132 }
133 calls++;
134 }
136}
Base class for the ZMQ connection to help monitor and log certain values.
Definition: ZMQLogger.h:35
void decrement(const std::string &key)
Decrement the value with the given key (only numerical values). If not present, set to -1.
Definition: ZMQLogger.cc:37
virtual std::string getMonitoringJSON() const
Convert the stored monitoring values to a JSON string ready for sending out via a message.
Definition: ZMQLogger.cc:15
void logTime(const std::string &key)
Store the current time as a string under the given key.
Definition: ZMQLogger.cc:42
void increment(const std::string &key)
Increment the value with the given key (only numerical values). If not present, set to 1.
Definition: ZMQLogger.cc:32
std::unordered_map< std::string, std::tuple< std::vector< double >, size_t > > m_averages
Internal storage of the previous values when calculating averages.
Definition: ZMQLogger.h:60
std::map< std::string, std::variant< long, double, std::string > > m_monitoring
Internal storage of all stored values.
Definition: ZMQLogger.h:58
std::unordered_map< std::string, std::tuple< unsigned long, std::chrono::system_clock::time_point > > m_timeCounters
Internal storage how often the timeit function for a given key was called and when it has last reache...
Definition: ZMQLogger.h:62
void timeit(const std::string &key)
Measure the rate of calls with the same key every AVERAGE_SIZE calls (and also display the last time ...
Definition: ZMQLogger.h:117
void log(const std::string &key, const AClass &value)
Store a value under a certain key. Different types of values can be stored, namely long,...
Definition: ZMQLogger.h:96
void average(const std::string &key, double value)
Instead of storeing the double value directly under the given key, store the average of the last MAX_...
Definition: ZMQLogger.h:102
Abstract base class for different kinds of events.
Visitor Helper for decrementing a numerical value.
Definition: ZMQLogger.h:85
void operator()(long &value)
-1 for longs
Definition: ZMQLogger.cc:79
Visitor Helper for incrementing a numerical value.
Definition: ZMQLogger.h:75
void operator()(long &value)
+1 for longs
Definition: ZMQLogger.cc:64
Visitor Helper for converting a variant value into a JSON string.
Definition: ZMQLogger.h:65
std::string operator()(long value)
just stringify longs
Definition: ZMQLogger.cc:49