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) noexcept
59{
60
61 assert(metrics.size() == specs.size());
62
63 if (metrics.empty()) {
64 return false;
65 }
66
67 boost::property_tree::ptree root;
68 for (unsigned int idx = 0; idx < metrics.size(); ++idx) {
69
70 const auto& deviceMetrics = metrics[idx];
71 boost::property_tree::ptree deviceRoot;
72
73 for (size_t mi = 0; mi < deviceMetrics.metricLabels.size(); mi++) {
74 std::string_view metricLabel{deviceMetrics.metricLabels[mi].label, deviceMetrics.metricLabels[mi].size};
75
76 auto same = [metricLabel](std::regex const& matcher) -> bool {
77 return std::regex_match(metricLabel.begin(), metricLabel.end(), matcher);
78 };
79 //check if we are interested
80 if (std::find_if(std::begin(performanceMetricsRegex), std::end(performanceMetricsRegex), same) == performanceMetricsRegex.end()) {
81 continue;
82 }
83 auto storeIdx = deviceMetrics.metrics[mi].storeIdx;
84
85 if (deviceMetrics.metrics[mi].filledMetrics == 0) {
86 continue;
87 }
88 //if so
89
90 boost::property_tree::ptree metricNode;
91
92 switch (deviceMetrics.metrics[mi].type) {
93 case MetricType::Int:
94 metricNode = fillNodeWithValue(deviceMetrics, deviceMetrics.intMetrics, deviceMetrics.intTimestamps, mi, storeIdx);
95 break;
96
97 case MetricType::Float:
98 metricNode = fillNodeWithValue(deviceMetrics, deviceMetrics.floatMetrics, deviceMetrics.floatTimestamps, mi, storeIdx);
99 break;
100
101 case MetricType::String:
102 metricNode = fillNodeWithValue(deviceMetrics, deviceMetrics.stringMetrics, deviceMetrics.stringTimestamps, mi, storeIdx);
103 break;
104
105 case MetricType::Uint64:
106 metricNode = fillNodeWithValue(deviceMetrics, deviceMetrics.uint64Metrics, deviceMetrics.uint64Timestamps, mi, storeIdx);
107 break;
108
109 default:
110 continue;
111 }
112 deviceRoot.add_child(std::string(metricLabel), metricNode);
113 }
114
115 root.add_child(specs[idx].id, deviceRoot);
116 }
117
118 boost::property_tree::ptree driverRoot;
119 for (size_t mi = 0; mi < driverMetrics.metricLabels.size(); mi++) {
120 std::string_view const metricLabel{driverMetrics.metricLabels[mi].label, driverMetrics.metricLabels[mi].size};
121 auto same = [metricLabel](std::regex const& matcher) -> bool {
122 return std::regex_match(metricLabel.begin(), metricLabel.end(), matcher);
123 };
124
125 //check if we are interested
126 if (std::find_if(std::begin(performanceMetricsRegex), std::end(performanceMetricsRegex), same) == performanceMetricsRegex.end()) {
127 continue;
128 }
129
130 auto storeIdx = driverMetrics.metrics[mi].storeIdx;
131 // and if data is there
132 if (driverMetrics.metrics[mi].filledMetrics == 0) {
133 continue;
134 }
135
136 //if so
137 boost::property_tree::ptree metricNode;
138
139 switch (driverMetrics.metrics[mi].type) {
140 case MetricType::Int:
141 metricNode = fillNodeWithValue(driverMetrics, driverMetrics.intMetrics, driverMetrics.intTimestamps, mi, storeIdx);
142 break;
143
144 case MetricType::Float:
145 metricNode = fillNodeWithValue(driverMetrics, driverMetrics.floatMetrics, driverMetrics.floatTimestamps, mi, storeIdx);
146 break;
147
148 case MetricType::String:
149 metricNode = fillNodeWithValue(driverMetrics, driverMetrics.stringMetrics, driverMetrics.stringTimestamps, mi, storeIdx);
150 break;
151
152 case MetricType::Uint64:
153 metricNode = fillNodeWithValue(driverMetrics, driverMetrics.uint64Metrics, driverMetrics.uint64Timestamps, mi, storeIdx);
154 break;
155
156 default:
157 continue;
158 }
159 driverRoot.add_child(std::string{metricLabel}, metricNode);
160 }
161
162 root.add_child("driver", driverRoot);
163
164 std::ofstream file("performanceMetrics.json", std::ios::out);
165 if (file.is_open()) {
166 boost::property_tree::json_parser::write_json(file, root);
167 } else {
168 return false;
169 }
170
171 file.close();
172
173 return true;
174}
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.
Definition TFIDInfo.h:20
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) noexcept