Skip to content

Commit bc87760

Browse files
committed
DPL: refactor dumping of performanceMetrics.json
* Add test. * Allow specifying the file on the command line * Make the function take a std::stream rather than opening the file itself.
1 parent 512d5bc commit bc87760

File tree

6 files changed

+290
-20
lines changed

6 files changed

+290
-20
lines changed

Framework/Core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ add_executable(o2-test-framework-core
240240
test/test_OverrideLabels.cxx
241241
test/test_O2DataModelHelpers.cxx
242242
test/test_RootConfigParamHelpers.cxx
243+
test/test_ResourcesMonitoringHelpers.cxx
243244
test/test_Services.cxx
244245
test/test_StringHelpers.cxx
245246
test/test_StaticFor.cxx

Framework/Core/include/Framework/DriverInfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ struct DriverInfo {
149149
std::string uniqueWorkflowId = "";
150150
/// Metrics gathering interval
151151
unsigned short resourcesMonitoringInterval = 0;
152+
/// Where to dump the metrics
153+
std::string resourcesMonitoringFilename = "performanceMetrics.json";
152154
/// Metrics gathering dump to disk interval
153155
unsigned short resourcesMonitoringDumpInterval = 0;
154156
/// Port used by the websocket control. 0 means not initialised.

Framework/Core/src/ResourcesMonitoringHelper.cxx

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ boost::property_tree::ptree fillNodeWithValue(const DeviceMetricsInfo& deviceMet
5555
bool ResourcesMonitoringHelper::dumpMetricsToJSON(const std::vector<DeviceMetricsInfo>& metrics,
5656
const DeviceMetricsInfo& driverMetrics,
5757
const std::vector<DeviceSpec>& specs,
58-
std::vector<std::regex> const& performanceMetricsRegex) noexcept
58+
std::vector<std::regex> const& performanceMetricsRegex,
59+
std::ostream& out) noexcept
5960
{
6061

6162
assert(metrics.size() == specs.size());
@@ -76,7 +77,7 @@ bool ResourcesMonitoringHelper::dumpMetricsToJSON(const std::vector<DeviceMetric
7677
auto same = [metricLabel](std::regex const& matcher) -> bool {
7778
return std::regex_match(metricLabel.begin(), metricLabel.end(), matcher);
7879
};
79-
//check if we are interested
80+
// check if we are interested
8081
if (std::find_if(std::begin(performanceMetricsRegex), std::end(performanceMetricsRegex), same) == performanceMetricsRegex.end()) {
8182
continue;
8283
}
@@ -85,7 +86,7 @@ bool ResourcesMonitoringHelper::dumpMetricsToJSON(const std::vector<DeviceMetric
8586
if (deviceMetrics.metrics[mi].filledMetrics == 0) {
8687
continue;
8788
}
88-
//if so
89+
// if so
8990

9091
boost::property_tree::ptree metricNode;
9192

@@ -122,7 +123,7 @@ bool ResourcesMonitoringHelper::dumpMetricsToJSON(const std::vector<DeviceMetric
122123
return std::regex_match(metricLabel.begin(), metricLabel.end(), matcher);
123124
};
124125

125-
//check if we are interested
126+
// check if we are interested
126127
if (std::find_if(std::begin(performanceMetricsRegex), std::end(performanceMetricsRegex), same) == performanceMetricsRegex.end()) {
127128
continue;
128129
}
@@ -133,7 +134,7 @@ bool ResourcesMonitoringHelper::dumpMetricsToJSON(const std::vector<DeviceMetric
133134
continue;
134135
}
135136

136-
//if so
137+
// if so
137138
boost::property_tree::ptree metricNode;
138139

139140
switch (driverMetrics.metrics[mi].type) {
@@ -161,14 +162,7 @@ bool ResourcesMonitoringHelper::dumpMetricsToJSON(const std::vector<DeviceMetric
161162

162163
root.add_child("driver", driverRoot);
163164

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();
165+
boost::property_tree::json_parser::write_json(out, root);
172166

173167
return true;
174168
}

Framework/Core/src/ResourcesMonitoringHelper.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,25 @@
1313
#define O2_FRAMEWORK_RESOURCESMONITORINGHELPER_H_
1414

1515
#include "Framework/DeviceMetricsInfo.h"
16-
#include "Monitoring/ProcessMonitor.h"
1716
#include "Framework/DeviceSpec.h"
1817

1918
#include <vector>
20-
#include <type_traits>
2119
#include <regex>
20+
#include <iosfwd>
2221

2322
namespace o2::framework
2423
{
25-
2624
struct ResourcesMonitoringHelper {
2725
/// Dump the metrics in @a metrics which match the names specified in @a metricsToDump
2826
/// @a specs are the DeviceSpecs associated to the metrics.
2927
static bool dumpMetricsToJSON(std::vector<DeviceMetricsInfo> const& metrics,
3028
DeviceMetricsInfo const& driverMetrics,
3129
std::vector<DeviceSpec> const& specs,
32-
std::vector<std::regex> const& metricsToDump) noexcept;
30+
std::vector<std::regex> const& metricsToDump,
31+
std::ostream& out) noexcept;
3332
static bool isResourcesMonitoringEnabled(unsigned short interval) noexcept { return interval > 0; }
3433
};
3534

36-
3735
} // namespace o2::framework
3836

3937
#endif // O2_FRAMEWORK_RESOURCESMONITORINGHELPER_H_

Framework/Core/src/runDataProcessing.cxx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,8 +1247,10 @@ void dumpMetricsCallback(uv_timer_t* handle)
12471247
auto* context = (DriverServerContext*)handle->data;
12481248

12491249
static auto performanceMetrics = getDumpableMetrics();
1250+
std::ofstream file(context->driver->resourcesMonitoringFilename, std::ios::out);
12501251
ResourcesMonitoringHelper::dumpMetricsToJSON(*(context->metrics),
1251-
context->driver->metrics, *(context->specs), performanceMetrics);
1252+
context->driver->metrics, *(context->specs), performanceMetrics,
1253+
file);
12521254
}
12531255

12541256
void dumpRunSummary(DriverServerContext& context, DriverInfo const& driverInfo, DeviceInfos const& infos, DeviceSpecs const& specs)
@@ -2035,6 +2037,7 @@ int runStateMachine(DataProcessorSpecs const& workflow,
20352037
"--fairmq-ipc-prefix",
20362038
"--readers",
20372039
"--resources-monitoring",
2040+
"--resources-monitoring-file",
20382041
"--resources-monitoring-dump-interval",
20392042
"--time-limit",
20402043
};
@@ -2268,7 +2271,7 @@ int runStateMachine(DataProcessorSpecs const& workflow,
22682271
if (driverInfo.resourcesMonitoringDumpInterval) {
22692272
uv_timer_stop(&metricDumpTimer);
22702273
}
2271-
LOG(info) << "Dumping performance metrics to performanceMetrics.json file";
2274+
LOGP(info, "Dumping performance metrics to {}.json file", driverInfo.resourcesMonitoringFilename);
22722275
dumpMetricsCallback(&metricDumpTimer);
22732276
}
22742277
dumpRunSummary(serverContext, driverInfo, infos, runningWorkflow.devices);
@@ -2916,6 +2919,7 @@ int doMain(int argc, char** argv, o2::framework::WorkflowSpec const& workflow,
29162919
("no-IPC", bpo::value<bool>()->zero_tokens()->default_value(false), "disable IPC topology optimization") // //
29172920
("o2-control,o2", bpo::value<std::string>()->default_value(""), "dump O2 Control workflow configuration under the specified name") //
29182921
("resources-monitoring", bpo::value<unsigned short>()->default_value(0), "enable cpu/memory monitoring for provided interval in seconds") //
2922+
("resources-monitoring-file", bpo::value<std::string>()->default_value("performanceMetrics.json"), "file where to dump the metrics") //
29192923
("resources-monitoring-dump-interval", bpo::value<unsigned short>()->default_value(0), "dump monitoring information to disk every provided seconds"); //
29202924
// some of the options must be forwarded by default to the device
29212925
executorOptions.add(DeviceSpecHelpers::getForwardedDeviceOptions());
@@ -3186,6 +3190,7 @@ int doMain(int argc, char** argv, o2::framework::WorkflowSpec const& workflow,
31863190
driverInfo.deployHostname = varmap["hostname"].as<std::string>();
31873191
driverInfo.resources = varmap["resources"].as<std::string>();
31883192
driverInfo.resourcesMonitoringInterval = varmap["resources-monitoring"].as<unsigned short>();
3193+
driverInfo.resourcesMonitoringFilename = varmap["resources-monitoring-file"].as<std::string>();
31893194
driverInfo.resourcesMonitoringDumpInterval = varmap["resources-monitoring-dump-interval"].as<unsigned short>();
31903195

31913196
// FIXME: should use the whole dataProcessorInfos, actually...

0 commit comments

Comments
 (0)