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
2528using namespace AliceO2 ::roc::CommandLineUtilities;
2629using namespace AliceO2 ::roc;
2730using namespace AliceO2 ::InfoLogger;
31+ namespace pt = boost::property_tree;
2832namespace po = boost::program_options;
2933
3034class 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};
0 commit comments