Project
Loading...
Searching...
No Matches
DataProcessingStats.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11#ifndef O2_FRAMEWORK_DATAPROCESSINGSTATS_H_
12#define O2_FRAMEWORK_DATAPROCESSINGSTATS_H_
13
14#include "DeviceState.h"
16#include <atomic>
17#include <cstdint>
18#include <array>
19#include <memory>
20#include <numeric>
21#include <mutex>
22#include <utility>
23
24namespace o2::framework
25{
26
80
83 // Parameters for the default behaviour
87
89
90 DataProcessingStats(std::function<void(int64_t& base, int64_t& offset)> getRealtimeBase,
91 std::function<int64_t(int64_t base, int64_t offset)> getTimestamp,
92 DefaultConfig config);
93
95 constexpr static unsigned short MAX_METRICS = 1 << 15;
96 constexpr static short MAX_CMDS = 64;
97
98 enum struct Op : char {
99 Nop,
100 Set,
104 Add,
105 Sub,
106 Max,
107 Min
108 };
109
110 // Kind of the metric. This is used to know how to interpret the value
111 enum struct Kind : char {
112 Int,
113 UInt64,
114 Double,
115 Rate,
118 Unknown,
119 };
120
121 // The scope for a given metric. DPL is used for the DPL Monitoring GUI,
122 // Online is used for the online monitoring.
123 enum struct Scope : char {
124 DPL,
125 Online
126 };
127
128 // This is what the user passes. Notice that there is no
129 // need to specify the timestamp, because we calculate it for them
130 // using the delta between the last update and the current time.
131 struct CommandSpec {
132 unsigned short id = 0;
135 };
136
137 // This is the structure to keep track of local updates to the stats.
138 // Each command will be queued in a buffer and then flushed to the
139 // global stats either when the buffer is full (after MAX_CMDS commands)
140 // or when the queue is flushed explicitly via the processQueue() method.
141 struct Command {
142 unsigned short id = 0; // StatsId of the metric to update
143 int64_t value = 0; // Value to update the metric with
144 int64_t timestamp = 0; // Timestamp of the update
145 Op op = Op::Nop; // Operation to perform to do the update
146 };
147
148 // This structure is used to keep track of the last updates
149 // for each of the metrics. This can be used to defer the need
150 // to flush the buffers to the remote end, so that we do not need to
151 // send metrics synchronously but we can do e.g. as a batch update.
152 // It also prevents that we send the same metric multiple times, because
153 // we keep track of the time of the last update.
154 struct UpdateInfo {
155 int64_t timestamp = 0; // When the update actually took place
156 int64_t lastPublished = 0; // When the update was last published
157 };
158
159 struct MetricSpec {
160 // Id of the metric. It must match the index in the metrics array.
161 // Name of the metric
162 std::string name = "";
163 // Wether or not the metric is enabled
164 bool enabled = true;
165 int metricId = -1;
176 uint64_t maxRefreshLatency = -1;
179 bool sendInitialValue = false;
180 };
181
182 void registerMetric(MetricSpec const& spec);
183
184 // Update some stats as specified by the @cmd cmd
185 void updateStats(CommandSpec cmd);
186
187 char const* findMetricNameById(ProcessingStatsId id) const;
190 void processCommandQueue();
191
192 void flushChangedMetrics(std::function<void(MetricSpec const&, int64_t, int64_t)> const& callback);
193
194 std::atomic<size_t> statesSize = 0;
195
196 std::array<Command, MAX_CMDS> cmds = {};
197 std::array<int64_t, MAX_METRICS> metrics = {};
198 std::array<bool, MAX_METRICS> updated = {};
199 std::array<std::string, MAX_METRICS> metricsNames = {};
200 std::array<UpdateInfo, MAX_METRICS> updateInfos = {};
201 std::array<MetricSpec, MAX_METRICS> metricSpecs = {};
202 std::array<int64_t, MAX_METRICS> lastPublishedMetrics = {};
203 std::vector<int> availableMetrics;
204 // for fast check for AVAILABLE_MANAGED_SHM metric which is only provided for readout-proxy
205 bool hasAvailSHMMetric = false;
206 // How many commands have been committed to the queue.
207 std::atomic<int> insertedCmds = 0;
208 // The insertion point for the next command.
209 std::atomic<int> nextCmd = 0;
210 // How many commands are currently in flight.
211 std::atomic<int> pendingCmds = 0;
214 // This is the mutex to protect the queue of commands.
215 std::mutex mMutex;
216
217 // Function to retrieve an aritrary base for the realtime clock.
218 std::function<void(int64_t& base, int64_t& offset)> getRealtimeBase;
219 // Function to retrieve the timestamp from the value returned by getRealtimeBase.
221 // The value of the uv_hrtime() at the last update.
223 // The value of the uv_now() at the last update.
225
226 // Invoke to make sure that the updatedMetricsTotal is updated.
236
237 // Telemetry for the metric updates and pushes
238 std::atomic<int64_t> updatedMetricsLapse = 0;
246};
247
248} // namespace o2::framework
249
250#endif // O2_FRAMEWORK_DATAPROCESSINGSTATS_H_
GLuint const GLchar * name
Definition glcorearb.h:781
GLenum GLenum GLsizei const GLuint GLboolean enabled
Definition glcorearb.h:2513
GLsizei GLenum const void GLuint GLsizei GLfloat * metrics
Definition glcorearb.h:5500
GLsizei const GLfloat * value
Definition glcorearb.h:819
GLintptr offset
Definition glcorearb.h:660
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
Defining ITS Vertex explicitly as messageable.
Definition Cartesian.h:288
ServiceKind
The kind of service we are asking for.
int64_t minPublishInterval
How many milliseconds must have passed since the last publishing.
int64_t defaultValue
The default value for the metric.
Helper struct to hold statistics about the data processing happening.
std::array< bool, MAX_METRICS > updated
char const * findMetricNameById(ProcessingStatsId id) const
void registerMetric(MetricSpec const &spec)
std::array< MetricSpec, MAX_METRICS > metricSpecs
std::function< int64_t(int64_t base, int64_t offset)> getTimestamp
@ SetIfPositive
Set the value to the specified value.
@ InstantaneousRate
Update the rate of the metric given the cumulative value since last time it got published.
@ Max
Subtract the value from the current value.
@ Min
Set the value to the maximum of the current value and the specified value.
@ CumulativeRate
Set the value to the specified value if it is positive.
@ Sub
Add the value to the current value.
@ Add
Update the rate of the metric given the amount since the last time.
std::array< std::string, MAX_METRICS > metricsNames
std::array< Command, MAX_CMDS > cmds
void flushChangedMetrics(std::function< void(MetricSpec const &, int64_t, int64_t)> const &callback)
std::array< int64_t, MAX_METRICS > lastPublishedMetrics
std::function< void(int64_t &base, int64_t &offset)> getRealtimeBase
static constexpr unsigned short MAX_METRICS
std::array< UpdateInfo, MAX_METRICS > updateInfos
static constexpr ServiceKind service_kind