Project
Loading...
Searching...
No Matches
ResourcesMonitoringHelper.cxx
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
14#define BOOST_BIND_GLOBAL_PLACEHOLDERS
15#include <boost/property_tree/json_parser.hpp>
16#include <fstream>
17#include <string_view>
18#include <algorithm>
19#include <cassert>
20#include <regex>
21
22using namespace o2::framework;
23
24template <typename T>
25inline static T retriveValue(T val)
26{
27 return val;
28}
29
30inline static std::string retriveValue(const std::reference_wrapper<const StringMetric> val)
31{
32 return std::string(val.get().data);
33}
34
35template <typename T, typename TIMESTAMPS>
36boost::property_tree::ptree fillNodeWithValue(const DeviceMetricsInfo& deviceMetrics,
37 const T& metricsStorage, const TIMESTAMPS& timestampsStorage, size_t labelIndex, size_t storeIndex)
38{
39 unsigned int loopRange = std::min(deviceMetrics.metrics[labelIndex].filledMetrics, metricsStorage[storeIndex].size());
40 boost::property_tree::ptree metricNode;
41
42 for (unsigned int idx = 0; idx < loopRange; ++idx) {
43 boost::property_tree::ptree values;
44 values.add("timestamp", timestampsStorage[storeIndex][idx]);
45 if constexpr (std::is_arithmetic_v<T>) {
46 values.add("value", std::to_string(retriveValue(std::cref(metricsStorage[storeIndex][idx]))));
47 } else {
48 values.add("value", retriveValue(std::cref(metricsStorage[storeIndex][idx])));
49 }
50 metricNode.push_back(std::make_pair("", values));
51 }
52 return metricNode;
53}
54
55bool ResourcesMonitoringHelper::dumpMetricsToJSON(const std::vector<DeviceMetricsInfo>& metrics,
56 const DeviceMetricsInfo& driverMetrics,
57 const std::vector<DeviceSpec>& specs,
58 std::vector<std::regex> const& performanceMetricsRegex,
59 std::ostream& out) noexcept
60{
61
62 assert(metrics.size() == specs.size());
63
64 if (metrics.empty()) {
65 return false;
66 }
67
68 boost::property_tree::ptree root;
69 for (unsigned int idx = 0; idx < metrics.size(); ++idx) {
70
71 const auto& deviceMetrics = metrics[idx];
72 boost::property_tree::ptree deviceRoot;
73
74 for (size_t mi = 0; mi < deviceMetrics.metricLabels.size(); mi++) {
75 std::string_view metricLabel{deviceMetrics.metricLabels[mi].label, deviceMetrics.metricLabels[mi].size};
76
77 auto same = [metricLabel](std::regex const& matcher) -> bool {
78 return std::regex_match(metricLabel.begin(), metricLabel.end(), matcher);
79 };
80 // check if we are interested
81 if (std::find_if(std::begin(performanceMetricsRegex), std::end(performanceMetricsRegex), same) == performanceMetricsRegex.end()) {
82 continue;
83 }
84 auto storeIdx = deviceMetrics.metrics[mi].storeIdx;
85
86 if (deviceMetrics.metrics[mi].filledMetrics == 0) {
87 continue;
88 }
89 // if so
90
91 boost::property_tree::ptree metricNode;
92
93 switch (deviceMetrics.metrics[mi].type) {
94 case MetricType::Int:
95 metricNode = fillNodeWithValue(deviceMetrics, deviceMetrics.intMetrics, deviceMetrics.intTimestamps, mi, storeIdx);
96 break;
97
98 case MetricType::Float:
99 metricNode = fillNodeWithValue(deviceMetrics, deviceMetrics.floatMetrics, deviceMetrics.floatTimestamps, mi, storeIdx);
100 break;
101
102 case MetricType::String:
103 metricNode = fillNodeWithValue(deviceMetrics, deviceMetrics.stringMetrics, deviceMetrics.stringTimestamps, mi, storeIdx);
104 break;
105
106 case MetricType::Uint64:
107 metricNode = fillNodeWithValue(deviceMetrics, deviceMetrics.uint64Metrics, deviceMetrics.uint64Timestamps, mi, storeIdx);
108 break;
109
110 default:
111 continue;
112 }
113 deviceRoot.add_child(std::string(metricLabel), metricNode);
114 }
115
116 root.add_child(specs[idx].id, deviceRoot);
117 }
118
119 boost::property_tree::ptree driverRoot;
120 for (size_t mi = 0; mi < driverMetrics.metricLabels.size(); mi++) {
121 std::string_view const metricLabel{driverMetrics.metricLabels[mi].label, driverMetrics.metricLabels[mi].size};
122 auto same = [metricLabel](std::regex const& matcher) -> bool {
123 return std::regex_match(metricLabel.begin(), metricLabel.end(), matcher);
124 };
125
126 // check if we are interested
127 if (std::find_if(std::begin(performanceMetricsRegex), std::end(performanceMetricsRegex), same) == performanceMetricsRegex.end()) {
128 continue;
129 }
130
131 auto storeIdx = driverMetrics.metrics[mi].storeIdx;
132 // and if data is there
133 if (driverMetrics.metrics[mi].filledMetrics == 0) {
134 continue;
135 }
136
137 // if so
138 boost::property_tree::ptree metricNode;
139
140 switch (driverMetrics.metrics[mi].type) {
141 case MetricType::Int:
142 metricNode = fillNodeWithValue(driverMetrics, driverMetrics.intMetrics, driverMetrics.intTimestamps, mi, storeIdx);
143 break;
144
145 case MetricType::Float:
146 metricNode = fillNodeWithValue(driverMetrics, driverMetrics.floatMetrics, driverMetrics.floatTimestamps, mi, storeIdx);
147 break;
148
149 case MetricType::String:
150 metricNode = fillNodeWithValue(driverMetrics, driverMetrics.stringMetrics, driverMetrics.stringTimestamps, mi, storeIdx);
151 break;
152
153 case MetricType::Uint64:
154 metricNode = fillNodeWithValue(driverMetrics, driverMetrics.uint64Metrics, driverMetrics.uint64Timestamps, mi, storeIdx);
155 break;
156
157 default:
158 continue;
159 }
160 driverRoot.add_child(std::string{metricLabel}, metricNode);
161 }
162
163 root.add_child("driver", driverRoot);
164
165 boost::property_tree::json_parser::write_json(out, root);
166
167 return true;
168}
boost::property_tree::ptree fillNodeWithValue(const DeviceMetricsInfo &deviceMetrics, const T &metricsStorage, const TIMESTAMPS &timestampsStorage, size_t labelIndex, size_t storeIndex)
GLsizei GLenum const void GLuint GLsizei GLfloat * metrics
Definition glcorearb.h:5500
GLenum GLsizei GLsizei GLint * values
Definition glcorearb.h:1576
GLuint GLfloat * val
Definition glcorearb.h:1582
Defining PrimaryVertex explicitly as messageable.
std::string to_string(gsl::span< T, Size > span)
Definition common.h:52
std::vector< MetricInfo > metrics
static bool dumpMetricsToJSON(std::vector< DeviceMetricsInfo > const &metrics, DeviceMetricsInfo const &driverMetrics, std::vector< DeviceSpec > const &specs, std::vector< std::regex > const &metricsToDump, std::ostream &out) noexcept