Skip to content

Commit 0553557

Browse files
committed
[roc-{metrics,pkt-monitor}] Add json output + packet rate
1 parent 25ff1ef commit 0553557

File tree

4 files changed

+117
-45
lines changed

4 files changed

+117
-45
lines changed

src/CommandLineUtilities/ProgramMetrics.cxx

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@
1919
#include "CommandLineUtilities/Options.h"
2020
#include "CommandLineUtilities/Program.h"
2121
#include "RocPciDevice.h"
22+
#include "Utilities/Util.h"
2223
#include <boost/format.hpp>
2324
#include <boost/optional/optional_io.hpp>
25+
#include <boost/property_tree/ptree.hpp>
26+
#include <boost/property_tree/json_parser.hpp>
2427

2528
using namespace AliceO2::roc::CommandLineUtilities;
2629
using namespace AliceO2::roc;
2730
using namespace AliceO2::InfoLogger;
31+
namespace pt = boost::property_tree;
2832
namespace po = boost::program_options;
2933

3034
class ProgramMetrics : public Program
@@ -33,72 +37,86 @@ class ProgramMetrics : public Program
3337
virtual Description getDescription()
3438
{
3539
return { "Metrics", "Return current RoC parameters",
36-
"roc-metrics --id -1\n"
37-
"roc-metrics --id 42:00.0\n" };
40+
"roc-metrics \n" };
3841
}
3942

4043
virtual void addOptions(boost::program_options::options_description& options)
4144
{
42-
Options::addOptionCardId(options);
45+
options.add_options()("json-out",
46+
po::bool_switch(&mOptions.jsonOut),
47+
"Toggle json-formatted output");
4348
options.add_options()("csv-out",
4449
po::bool_switch(&mOptions.csvOut),
4550
"Toggle csv-formatted output");
4651
}
4752

48-
virtual void run(const boost::program_options::variables_map& map)
53+
virtual void run(const boost::program_options::variables_map& /*map*/)
4954
{
5055

51-
auto cardId = Options::getOptionCardId(map);
52-
std::vector<CardDescriptor> cardsFound;
53-
54-
try {
55-
cardsFound.push_back(RocPciDevice(cardId).getCardDescriptor());
56-
} catch (boost::exception& e) {
57-
std::cout << boost::diagnostic_information(e);
58-
return;
59-
}
60-
6156
std::ostringstream table;
62-
auto formatHeader = " %-3s %-6s %-10s %-10s %-19s %-20s %-19s %-8s %-17s %-17s\n";
63-
auto formatRow = " %-3s %-6s %-10s %-10s %-19s %-20s %-19s %-8s %-17s %-17s\n";
64-
auto header = (boost::format(formatHeader) % "#" % "Type" % "PCI Addr" % "Temp (C)" % "#Dropped Packets" % "CTP Clock (MHz)" % "Local Clock (MHz)" % "#links" % "#Wrapper 0 links" % "#Wrapper 1 links").str();
57+
auto formatHeader = " %-3s %-6s %-10s %-10s %-19s %-20s %-19s %-26s\n";
58+
auto formatRow = " %-3s %-6s %-10s %-10s %-19s %-20s %-19s %-26s\n";
59+
auto header = (boost::format(formatHeader) % "#" % "Type" % "PCI Addr" % "Temp (C)" % "#Dropped Packets" % "CTP Clock (MHz)" % "Local Clock (MHz)" % "Total Packets per second").str();
6560
auto lineFat = std::string(header.length(), '=') + '\n';
6661
auto lineThin = std::string(header.length(), '-') + '\n';
6762

6863
if (mOptions.csvOut) {
69-
auto csvHeader = "#,Type,PCI Addr,Temp (C),#Dropped Packets,CTP Clock (MHz),Local Clock (MHz),#links,#Wrapper 0 links, #Wrapper 1 links\n";
64+
auto csvHeader = "#,Type,PCI Addr,Temp (C),#Dropped Packets,CTP Clock (MHz),Local Clock (MHz),Total Packets per second\n";
7065
std::cout << csvHeader;
71-
} else {
66+
} else if (!mOptions.jsonOut) {
7267
table << lineFat << header << lineThin;
7368
}
7469

70+
auto cardsFound = AliceO2::roc::RocPciDevice::findSystemDevices();
71+
72+
pt::ptree root;
7573
int i = 0;
7674
for (const auto& card : cardsFound) {
75+
if (card.cardType == CardType::type::Crorc) {
76+
continue;
77+
}
78+
7779
Parameters params0 = Parameters::makeParameters(card.pciAddress, 0);
7880
auto bar0 = ChannelFactory().getBar(params0);
7981
Parameters params2 = Parameters::makeParameters(card.pciAddress, 2);
8082
auto bar2 = ChannelFactory().getBar(params2);
8183

8284
float temperature = bar2->getTemperature().value_or(0);
8385
int32_t dropped = bar2->getDroppedPackets(bar0->getEndpointNumber());
84-
float ctp_clock = bar2->getCTPClock() / 1e6;
85-
float local_clock = bar2->getLocalClock() / 1e6;
86-
int32_t links = bar2->getLinks();
87-
uint32_t links0 = bar2->getLinksPerWrapper(0);
88-
uint32_t links1 = bar2->getLinksPerWrapper(1);
89-
90-
if (mOptions.csvOut) {
91-
auto csvLine = std::to_string(i) + "," + CardType::toString(card.cardType) + "," + card.pciAddress.toString() + "," + std::to_string(temperature) + "," + std::to_string(dropped) + "," + std::to_string(ctp_clock) + "," + std::to_string(local_clock) + "," + std::to_string(links) + "," + std::to_string(links0) + "," + std::to_string(links1) + "\n";
86+
float ctpClock = bar2->getCTPClock() / 1e6;
87+
float localClock = bar2->getLocalClock() / 1e6;
88+
uint32_t totalPacketsPerSecond = bar2->getTotalPacketsPerSecond(0);
89+
90+
if (mOptions.jsonOut) {
91+
pt::ptree cardNode;
92+
93+
// add kv pairs for this card
94+
cardNode.put("type", CardType::toString(card.cardType));
95+
cardNode.put("pciAddress", card.pciAddress.toString());
96+
cardNode.put("temperature", Utilities::toPreciseString(temperature));
97+
cardNode.put("droppedPackets", std::to_string(dropped));
98+
cardNode.put("ctpClock", Utilities::toPreciseString(ctpClock));
99+
cardNode.put("localClock", Utilities::toPreciseString(localClock));
100+
cardNode.put("totalPacketsPerSecond", std::to_string(totalPacketsPerSecond));
101+
102+
// add the card node to the tree
103+
root.add_child(std::to_string(i), cardNode);
104+
} else if (mOptions.csvOut) {
105+
auto csvLine = std::to_string(i) + "," + CardType::toString(card.cardType) + "," + card.pciAddress.toString() + "," +
106+
std::to_string(temperature) + "," + std::to_string(dropped) + "," + std::to_string(ctpClock) + "," +
107+
std::to_string(localClock) + "," + std::to_string(totalPacketsPerSecond) + "\n";
92108
std::cout << csvLine;
93109
} else {
94-
auto format = boost::format(formatRow) % i % CardType::toString(card.cardType) % card.pciAddress.toString() % temperature % dropped % ctp_clock % local_clock % links % links0 % links1;
110+
auto format = boost::format(formatRow) % i % CardType::toString(card.cardType) % card.pciAddress.toString() % temperature % dropped % ctpClock % localClock % totalPacketsPerSecond;
95111

96112
table << format;
97113
}
98114
i++;
99115
}
100116

101-
if (!mOptions.csvOut) {
117+
if (mOptions.jsonOut) {
118+
pt::write_json(std::cout, root);
119+
} else if (!mOptions.csvOut) {
102120
auto lineFat = std::string(header.length(), '=') + '\n';
103121
table << lineFat;
104122
std::cout << table.str();
@@ -107,6 +125,7 @@ class ProgramMetrics : public Program
107125

108126
private:
109127
struct OptionsStruct {
128+
bool jsonOut = false;
110129
bool csvOut = false;
111130
} mOptions;
112131
};

src/CommandLineUtilities/ProgramPacketMonitor.cxx

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@
2121
#include "CommandLineUtilities/Options.h"
2222
#include "CommandLineUtilities/Program.h"
2323
#include <boost/format.hpp>
24+
#include <boost/property_tree/ptree.hpp>
25+
#include <boost/property_tree/json_parser.hpp>
2426

2527
using namespace AliceO2::roc::CommandLineUtilities;
2628
using namespace AliceO2::roc;
2729
using namespace AliceO2::InfoLogger;
30+
namespace pt = boost::property_tree;
2831
namespace po = boost::program_options;
2932

3033
class ProgramPacketMonitor : public Program
@@ -39,6 +42,9 @@ class ProgramPacketMonitor : public Program
3942
virtual void addOptions(boost::program_options::options_description& options)
4043
{
4144
Options::addOptionCardId(options);
45+
options.add_options()("json-out",
46+
po::bool_switch(&mOptions.jsonOut),
47+
"Toggle json-formatted output");
4248
options.add_options()("csv-out",
4349
po::bool_switch(&mOptions.csvOut),
4450
"Toggle csv-formatted output");
@@ -75,10 +81,15 @@ class ProgramPacketMonitor : public Program
7581
if (mOptions.csvOut) {
7682
auto csvHeader = "Link ID,Accepted,Rejected,Forced\n";
7783
std::cout << csvHeader;
78-
} else {
84+
} else if (!mOptions.jsonOut) {
7985
table << lineFat << header << lineThin;
8086
}
8187

88+
// initialize ptrees
89+
pt::ptree root;
90+
pt::ptree gbtLinks;
91+
pt::ptree ulLink;
92+
8293
/* TABLE */
8394
for (const auto& el : packetMonitoringInfo.linkPacketInfoMap) {
8495
int globalId = el.first;
@@ -91,15 +102,32 @@ class ProgramPacketMonitor : public Program
91102
if (globalId == 15) {
92103
auto uLHeader = (boost::format(formatHeader) % "ULL ID " % "Accepted" % "Rejected" % "Forced").str();
93104

94-
if (mOptions.csvOut) {
105+
if (mOptions.jsonOut) {
106+
107+
} else if (mOptions.csvOut) {
95108
auto uLHeader = "Link ID,Accepted,Rejected,Forced\n";
96109
std::cout << uLHeader;
97110
} else {
98111
table << lineFat << uLHeader << lineThin;
99112
}
100113
}
101114

102-
if (mOptions.csvOut) {
115+
if (mOptions.jsonOut) {
116+
pt::ptree linkNode;
117+
118+
// add kv pairs for this link
119+
linkNode.put("linkId", std::to_string(globalId));
120+
linkNode.put("accepted", std::to_string(accepted));
121+
linkNode.put("rejected", std::to_string(rejected));
122+
linkNode.put("forced", std::to_string(forced));
123+
124+
// append to the links (or UL link)
125+
if (globalId == 15) {
126+
ulLink.add_child(std::to_string(globalId), linkNode);
127+
} else {
128+
gbtLinks.add_child(std::to_string(globalId), linkNode);
129+
}
130+
} else if (mOptions.csvOut) {
103131
auto csvLine = std::to_string(globalId) + "," + std::to_string(accepted) + "," + std::to_string(rejected) + "," + std::to_string(forced) + "\n";
104132
std::cout << csvLine;
105133
} else {
@@ -108,6 +136,10 @@ class ProgramPacketMonitor : public Program
108136
}
109137
}
110138

139+
// add links nodes to the tree
140+
root.add_child("gbtLinks", gbtLinks);
141+
root.add_child("userLogicLink", ulLink);
142+
111143
/* PRINT */
112144
if (!mOptions.csvOut) {
113145
std::cout << table.str();
@@ -128,13 +160,26 @@ class ProgramPacketMonitor : public Program
128160
otherTable << lineFat << header << lineThin;
129161
}
130162

163+
pt::ptree ulLinks;
164+
131165
/* TABLE */
132166
for (const auto& el : packetMonitoringInfo.wrapperPacketInfoMap) {
133167
int wrapper = el.first;
134168
auto wrapperMonitoringInfoMap = el.second;
135169
uint32_t dropped = wrapperMonitoringInfoMap.dropped;
136170
uint32_t totalPacketsPerSec = wrapperMonitoringInfoMap.totalPacketsPerSec;
137171

172+
if (mOptions.jsonOut) {
173+
pt::ptree wrapperNode;
174+
175+
// add kv pairs for this wrapper
176+
wrapperNode.put("wrapperId", std::to_string(wrapper));
177+
wrapperNode.put("dropped", std::to_string(dropped));
178+
wrapperNode.put("totalPacketsPerSec", std::to_string(totalPacketsPerSec));
179+
180+
// add the wrapper node to the tree
181+
root.add_child("wrapper", wrapperNode);
182+
}
138183
if (mOptions.csvOut) {
139184
auto csvLine = std::to_string(wrapper) + "," + std::to_string(dropped) + "," + std::to_string(totalPacketsPerSec) + "\n";
140185
std::cout << csvLine;
@@ -145,7 +190,9 @@ class ProgramPacketMonitor : public Program
145190
}
146191

147192
/* BREAK + PRINT */
148-
if (!mOptions.csvOut) {
193+
if (mOptions.jsonOut) {
194+
pt::write_json(std::cout, root);
195+
} else if (!mOptions.csvOut) {
149196
auto lineFat = std::string(header.length(), '=') + '\n';
150197
otherTable << lineFat;
151198
std::cout << otherTable.str();
@@ -154,6 +201,7 @@ class ProgramPacketMonitor : public Program
154201

155202
private:
156203
struct OptionsStruct {
204+
bool jsonOut = false;
157205
bool csvOut = false;
158206
} mOptions;
159207
};

src/CommandLineUtilities/ProgramStatus.cxx

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "ReadoutCard/ChannelFactory.h"
2323
#include "CommandLineUtilities/Options.h"
2424
#include "CommandLineUtilities/Program.h"
25+
#include "Utilities/Util.h"
2526
#include <boost/format.hpp>
2627
#include <boost/property_tree/ptree.hpp>
2728
#include <boost/property_tree/json_parser.hpp>
@@ -55,14 +56,6 @@ class ProgramStatus : public Program
5556

5657
virtual void run(const boost::program_options::variables_map& map)
5758
{
58-
auto enforcePrecision = [](auto flo) {
59-
std::ostringstream precisionEnforcer;
60-
precisionEnforcer << std::fixed;
61-
precisionEnforcer << std::setprecision(2);
62-
precisionEnforcer << flo;
63-
return precisionEnforcer.str();
64-
};
65-
6659
std::ostringstream table;
6760
std::string formatHeader;
6861
std::string formatRow;
@@ -141,7 +134,7 @@ class ProgramStatus : public Program
141134

142135
// add kv pairs for this card
143136
linkNode.put("status", linkStatus);
144-
linkNode.put("opticalPower", enforcePrecision(opticalPower));
137+
linkNode.put("opticalPower", Utilities::toPreciseString(opticalPower));
145138

146139
// add the link node to the tree
147140
root.add_child(std::to_string(id), linkNode);
@@ -247,10 +240,10 @@ class ProgramStatus : public Program
247240
linkNode.put("gbtMux", gbtMux);
248241
linkNode.put("datapathMode", datapathMode);
249242
linkNode.put("datapath", enabled);
250-
linkNode.put("rxFreq", enforcePrecision(rxFreq));
251-
linkNode.put("txFreq", enforcePrecision(txFreq));
243+
linkNode.put("rxFreq", Utilities::toPreciseString(rxFreq));
244+
linkNode.put("txFreq", Utilities::toPreciseString(txFreq));
252245
linkNode.put("status", linkStatus);
253-
linkNode.put("opticalPower", enforcePrecision(opticalPower));
246+
linkNode.put("opticalPower", Utilities::toPreciseString(opticalPower));
254247

255248
// add the link node to the tree
256249
root.add_child(std::to_string(globalId), linkNode);

src/Utilities/Util.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define ALICEO2_SRC_READOUTCARD_UTILITIES_UTIL_H_
1919

2020
#include <cstdint>
21+
#include <iomanip>
2122
#include <iostream>
2223
#include <bitset>
2324

@@ -111,6 +112,17 @@ inline bool checkAlignment(void* address, uint64_t alignment)
111112
return (uint64_t(address) % alignment) == 0;
112113
}
113114

115+
// Converts a number to a string with enforced precision
116+
template <typename T>
117+
std::string toPreciseString(T flo, int precision = 2)
118+
{
119+
std::ostringstream precisionEnforcer;
120+
precisionEnforcer << std::fixed;
121+
precisionEnforcer << std::setprecision(precision);
122+
precisionEnforcer << flo;
123+
return precisionEnforcer.str();
124+
}
125+
114126
} // namespace Utilities
115127
} // namespace roc
116128
} // namespace AliceO2

0 commit comments

Comments
 (0)