Belle II Software  release-05-01-25
ZMQLogger.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2019 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Nils Braun *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <variant>
13 #include <chrono>
14 #include <vector>
15 #include <map>
16 #include <unordered_map>
17 #include <numeric>
18 #include <string>
19 
20 namespace Belle2 {
37  class ZMQLogger {
38  public:
40  virtual std::string getMonitoringJSON() const;
41 
43  template<class AClass>
44  void log(const std::string& key, const AClass& value);
46  void increment(const std::string& key);
48  void decrement(const std::string& key);
50  template<size_t MAX_SIZE = 100>
51  void average(const std::string& key, double value);
53  template<size_t AVERAGE_SIZE = 2000>
54  void timeit(const std::string& key);
56  void logTime(const std::string& key);
57 
58  private:
60  std::map<std::string, std::variant<long, double, std::string>> m_monitoring;
62  std::unordered_map<std::string, std::tuple<std::vector<double>, size_t>> m_averages;
64  std::unordered_map<std::string, std::tuple<unsigned long, std::chrono::system_clock::time_point>> m_timeCounters;
65 
67  struct toJSON {
69  std::string operator()(long value);
71  std::string operator()(double value);
73  std::string operator()(const std::string& value);
74  };
75 
77  struct Incrementor {
79  void operator()(long& value);
81  void operator()(double& value);
83  void operator()(std::string&);
84  };
85 
87  struct Decrementor {
89  void operator()(long& value);
91  void operator()(double& value);
93  void operator()(std::string&);
94  };
95  };
96 
97  template<class AClass>
98  void ZMQLogger::log(const std::string& key, const AClass& value)
99  {
100  m_monitoring[key] = value;
101  }
102 
103  template<size_t MAX_SIZE>
104  void ZMQLogger::average(const std::string& key, double value)
105  {
106  auto& [averages, index] = m_averages[key];
107 
108  if (averages.size() == MAX_SIZE) {
109  averages[index] = value;
110  index = (index + 1) % MAX_SIZE;
111  } else {
112  averages.push_back(value);
113  }
114 
115  log(key, std::accumulate(averages.begin(), averages.end(), 0.0) / averages.size());
116  }
117 
118  template<size_t AVERAGE_SIZE>
119  void ZMQLogger::timeit(const std::string& key)
120  {
121  auto& [calls, timestamp] = m_timeCounters[key];
122  if (calls % AVERAGE_SIZE == 0) {
123  auto current = std::chrono::high_resolution_clock::now();
124  double elapsed = std::chrono::duration_cast<std::chrono::duration<double>>(
125  current - timestamp).count();
126 
127  log(key, AVERAGE_SIZE / elapsed);
128 
129  timestamp = current;
130  calls = 0;
131 
132  auto displayTime = std::chrono::system_clock::to_time_t(current);
133  log(key + "_last_measurement", std::ctime(&displayTime));
134  }
135  calls++;
136  }
138 }
Belle2::ZMQLogger::m_timeCounters
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:72
Belle2::ZMQLogger::toJSON::operator()
std::string operator()(long value)
just stringify longs
Definition: ZMQLogger.cc:51
Belle2::ZMQLogger::timeit
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:127
Belle2::ZMQLogger::Decrementor
Visitor Helper for decrementing a numerical value.
Definition: ZMQLogger.h:95
Belle2::ZMQLogger::logTime
void logTime(const std::string &key)
Store the current time as a string under the given key.
Definition: ZMQLogger.cc:44
Belle2::ZMQLogger::Decrementor::operator()
void operator()(long &value)
-1 for longs
Definition: ZMQLogger.cc:81
Belle2::ZMQLogger::decrement
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:39
Belle2::ZMQLogger::m_averages
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:70
Belle2::ZMQLogger::log
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:106
Belle2::ZMQLogger::increment
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:34
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::ZMQLogger::Incrementor::operator()
void operator()(long &value)
+1 for longs
Definition: ZMQLogger.cc:66
Belle2::ZMQLogger::average
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:112
Belle2::ZMQLogger::Incrementor
Visitor Helper for incrementing a numerical value.
Definition: ZMQLogger.h:85
Belle2::ZMQLogger::getMonitoringJSON
virtual std::string getMonitoringJSON() const
Convert the stored monitoring values to a JSON string ready for sending out via a message.
Definition: ZMQLogger.cc:17
Belle2::ZMQLogger::m_monitoring
std::map< std::string, std::variant< long, double, std::string > > m_monitoring
Internal storage of all stored values.
Definition: ZMQLogger.h:68