From bb72dfa904126379428f65f7d09545974bf51ba4 Mon Sep 17 00:00:00 2001 From: Valerio Pastore Date: Thu, 11 Jan 2024 19:54:42 +0100 Subject: [PATCH 1/3] adding doxygen --- include/Astri_Monitor.h | 116 ++++++++++++++++++++++++++++++++-------- 1 file changed, 94 insertions(+), 22 deletions(-) diff --git a/include/Astri_Monitor.h b/include/Astri_Monitor.h index b1ab645..0db9b2c 100644 --- a/include/Astri_Monitor.h +++ b/include/Astri_Monitor.h @@ -6,54 +6,126 @@ #include #include +/** + * @brief Namespace containing packet monitors for the ASTRI DAQ system. + */ namespace inaf::oasbo::PacketMonitors { + +/** + * @brief Class representing an ASTRI packet monitor. + * + * This class inherits from the BasePacketMonitor class and provides functionality + * to monitor ASTRI packets, calculate statistics, and print the statistics. + */ class AstriPacketMonitor: public BasePacketMonitor { protected: - AstriPacketMonitor(); - std::chrono::duration timeElps; - std::chrono::steady_clock::time_point startTime; - std::unordered_map lastSSCMap; - std::unordered_map lostsMap; - std::chrono::steady_clock::time_point lastNsTime; - double bitsLastNs; - size_t packsLastNs; - uint update_interval; + std::chrono::duration timeElps; /**< Elapsed time since monitoring started. */ + std::chrono::steady_clock::time_point startTime; /**< Start time of monitoring. */ + std::unordered_map lastSSCMap; /**< Map of last sequence numbers for each packet type. */ + std::unordered_map lostsMap; /**< Map of lost packets for each packet type. */ + std::chrono::steady_clock::time_point lastNsTime; /**< helper variable. */ + double bitsLastNs; /**< Number of bits received in the last N seconds. */ + size_t packsLastNs; /**< Number of packets received in the last N seconds. */ + uint update_interval; /**< Interval at which statistics are updated. */ + /** + * @brief Monitors a packet for loss and updates the lost packets map. + * + * @param pack The packet to monitor. + */ void monitPacketLost(PacketLib::BasePacket &pack); - static std::string tot_packets_key; - static std::string tot_MB_key; - static std::string tot_lost_key; - static std::string byte_rate_key; - static std::string mbit_rate_key; - static std::string packet_rate_key; + static std::string tot_packets_key; /**< Key for total packets statistic. */ + static std::string tot_MB_key; /**< Key for total MB statistic. */ + static std::string tot_lost_key; /**< Key for total lost packets statistic. */ + static std::string byte_rate_key; /**< Key for byte rate statistic. */ + static std::string mbit_rate_key; /**< Key for megabit rate statistic. */ + static std::string packet_rate_key; /**< Key for packet rate statistic. */ + friend class AstriPacketMonitorBuilder; public: + /** + * @brief Sets the update interval for statistics. + * + * @param interval The update interval in milliseconds. + */ void setUpdateInterval(int interval); + + /** + * @brief Gets the update interval for statistics. + * + * @return The update interval in milliseconds. + */ uint getUpdateInterval(){return update_interval;} + + /** + * @brief Monitors a packet and updates the statistics. + * + * @param pack The packet to monitor. + */ void monit(PacketLib::BasePacket &pack); + + /** + * @brief Resets the monitor and clears the statistics. + */ void reset(); + + /** + * @brief Prints the current statistics. + */ void printStats(); }; - - +/** + * @brief Builder class for creating an AstriPacketMonitor object. + */ class AstriPacketMonitorBuilder { protected: - AstriPacketMonitor *monitor; + AstriPacketMonitor *monitor; /**< Pointer to the AstriPacketMonitor object being built. */ public: - std::string config_target { "astrisimplemonitor" }; - std::string interval_key { "rateinterval" }; + std::string config_target { "astrisimplemonitor" }; /**< Configuration target for the monitor. */ + std::string interval_key { "rateinterval" }; /**< Key for the update interval configuration. */ + /** + * @brief Default constructor. + */ AstriPacketMonitorBuilder(); - AstriPacketMonitorBuilder(std::string ip, int port); + + /** + * @brief Destructor. + */ ~AstriPacketMonitorBuilder(); + + /** + * @brief Resets the builder to its initial state. + */ void reset(); + + /** + * @brief Configures the monitor from a BaseConfigurator object. + * + * @param conf The BaseConfigurator object to configure from. + * @return A pointer to the AstriPacketMonitorBuilder object. + */ AstriPacketMonitorBuilder* configFrom(Configurators::BaseConfigurator &conf); + + /** + * @brief Sets the update interval for the monitor. + * + * @param interval The update interval in seconds. + * @return A pointer to the AstriPacketMonitorBuilder object. + */ AstriPacketMonitorBuilder* setUpdateInterval(int interval); + + /** + * @brief Gets the built AstriPacketMonitor object. + * + * @return A pointer to the AstriPacketMonitor object. + */ AstriPacketMonitor* getMonitor(); }; -} + +} \ No newline at end of file -- GitLab From 3f0cac9b8a3575552c66af13982b6c574d5ffa3f Mon Sep 17 00:00:00 2001 From: valerio pastore Date: Thu, 11 Jan 2024 20:02:36 +0100 Subject: [PATCH 2/3] dev --- include/Astri_Monitor.h | 11 +++--- src/Astri_Monitor.cpp | 77 +++++++++++++++++++++-------------------- src/Builder.cpp | 16 ++++----- 3 files changed, 55 insertions(+), 49 deletions(-) diff --git a/include/Astri_Monitor.h b/include/Astri_Monitor.h index 0db9b2c..d74c648 100644 --- a/include/Astri_Monitor.h +++ b/include/Astri_Monitor.h @@ -1,4 +1,3 @@ - #pragma once #include @@ -20,6 +19,7 @@ namespace inaf::oasbo::PacketMonitors { class AstriPacketMonitor: public BasePacketMonitor { protected: + AstriPacketMonitor(); std::chrono::duration timeElps; /**< Elapsed time since monitoring started. */ std::chrono::steady_clock::time_point startTime; /**< Start time of monitoring. */ std::unordered_map lastSSCMap; /**< Map of last sequence numbers for each packet type. */ @@ -58,7 +58,9 @@ public: * * @return The update interval in milliseconds. */ - uint getUpdateInterval(){return update_interval;} + uint getUpdateInterval() { + return update_interval; + } /** * @brief Monitors a packet and updates the statistics. @@ -110,7 +112,8 @@ public: * @param conf The BaseConfigurator object to configure from. * @return A pointer to the AstriPacketMonitorBuilder object. */ - AstriPacketMonitorBuilder* configFrom(Configurators::BaseConfigurator &conf); + AstriPacketMonitorBuilder* configFrom( + Configurators::BaseConfigurator &conf); /** * @brief Sets the update interval for the monitor. @@ -128,4 +131,4 @@ public: AstriPacketMonitor* getMonitor(); }; -} \ No newline at end of file +} diff --git a/src/Astri_Monitor.cpp b/src/Astri_Monitor.cpp index 223afd1..e0873fe 100644 --- a/src/Astri_Monitor.cpp +++ b/src/Astri_Monitor.cpp @@ -1,24 +1,23 @@ - #include using namespace inaf::oasbo::PacketMonitors; -std::string AstriPacketMonitor::tot_packets_key{"tot_packets"}; -std::string AstriPacketMonitor::tot_MB_key{"tot_MB"}; -std::string AstriPacketMonitor::tot_lost_key{"tot_lost"}; -std::string AstriPacketMonitor::mbit_rate_key{"mbit_rate"}; -std::string AstriPacketMonitor::packet_rate_key{"packet_rate"}; - +std::string AstriPacketMonitor::tot_packets_key { "tot_packets" }; +std::string AstriPacketMonitor::tot_MB_key { "tot_MB" }; +std::string AstriPacketMonitor::tot_lost_key { "tot_lost" }; +std::string AstriPacketMonitor::mbit_rate_key { "mbit_rate" }; +std::string AstriPacketMonitor::packet_rate_key { "packet_rate" }; -AstriPacketMonitor::AstriPacketMonitor() : update_interval(5) { +AstriPacketMonitor::AstriPacketMonitor() : + update_interval(5) { reset(); } void AstriPacketMonitor::printStats() { std::cout << "------------------------------" << std::endl; - for(auto [k,v] : stats){ - std::cout << k << ": " << v << std::endl; + for (auto [k, v] : stats) { + std::cout << k << ": " << v << std::endl; } std::cout << "------------------------------" << std::endl; } @@ -35,11 +34,11 @@ void AstriPacketMonitor::reset() { lastSSCMap[key] = 0; for (auto [key, value] : lostsMap) lostsMap[key] = 0; - timeElps = std::chrono::steady_clock::duration::zero(); - startTime = std::chrono::steady_clock::time_point::min(); - lastNsTime = std::chrono::steady_clock::now(); - bitsLastNs = 0; - packsLastNs = 0; + timeElps = std::chrono::steady_clock::duration::zero(); + startTime = std::chrono::steady_clock::time_point::min(); + lastNsTime = std::chrono::steady_clock::now(); + bitsLastNs = 0; + packsLastNs = 0; } void AstriPacketMonitor::monit(PacketLib::BasePacket &pack) { @@ -49,27 +48,29 @@ void AstriPacketMonitor::monit(PacketLib::BasePacket &pack) { string type = to_string(pack["Packet Type"].value()); string subtype = to_string(pack["Packet SubType"].value()); string key = string(type).append(subtype).append("_tot"); - stats[key] = stats.find(key) != stats.end() ? to_string(stoi(stats[key]) + 1) : "1"; + stats[key] = + stats.find(key) != stats.end() ? + to_string(stoi(stats[key]) + 1) : "1"; //count tot MB double bytes = (pack.getHeaderSize() + pack.getPayloadSize() + pack.getTailSize()); - stats[tot_MB_key] = to_string(stod(stats[tot_MB_key]) + bytes/(1000000)); + stats[tot_MB_key] = to_string(stod(stats[tot_MB_key]) + bytes / (1000000)); // update byterate each "update_interval" seconds - bitsLastNs += bytes*8; - packsLastNs+=1; - auto now = chrono::steady_clock::now(); - if (now - lastNsTime > chrono::seconds(update_interval)) { - chrono::duration duration(now - lastNsTime); - double bitRateLastNs = bitsLastNs / duration.count(); - double packRateLastNs = packsLastNs / duration.count(); - double mbit_rate = bitRateLastNs/1000000; - stats[mbit_rate_key] = std::to_string(mbit_rate); - stats[packet_rate_key] = std::to_string(packRateLastNs); - lastNsTime = now; - bitsLastNs = 0; - packsLastNs = 0; - } + bitsLastNs += bytes * 8; + packsLastNs += 1; + auto now = chrono::steady_clock::now(); + if (now - lastNsTime > chrono::seconds(update_interval)) { + chrono::duration duration(now - lastNsTime); + double bitRateLastNs = bitsLastNs / duration.count(); + double packRateLastNs = packsLastNs / duration.count(); + double mbit_rate = bitRateLastNs / 1000000; + stats[mbit_rate_key] = std::to_string(mbit_rate); + stats[packet_rate_key] = std::to_string(packRateLastNs); + lastNsTime = now; + bitsLastNs = 0; + packsLastNs = 0; + } // Count the number of lost packets for each type and subtype, // and add them up to obtain the total number of lost packets monitPacketLost(pack); @@ -88,7 +89,9 @@ void AstriPacketMonitor::monitPacketLost(PacketLib::BasePacket &pack) { string subtype = to_string(pack["Packet SubType"].value()); string last_ssc_key = string(type).append(subtype); string losts_key = string(last_ssc_key).append("_lost"); - int last_ssc = lastSSCMap.find(last_ssc_key) != lastSSCMap.end() ? lastSSCMap[last_ssc_key] : 0; + int last_ssc = + lastSSCMap.find(last_ssc_key) != lastSSCMap.end() ? + lastSSCMap[last_ssc_key] : 0; int lostCount = 0; if (curr_ssc > last_ssc + 1) { lostCount = curr_ssc - last_ssc - 1; @@ -102,14 +105,14 @@ void AstriPacketMonitor::monitPacketLost(PacketLib::BasePacket &pack) { } -void AstriPacketMonitor::setUpdateInterval(int interval){ - if(interval < 0){ +void AstriPacketMonitor::setUpdateInterval(int interval) { + if (interval < 0) { this->update_interval = -interval; return; } - if(interval == 0){ - this->update_interval = 1; - return; + if (interval == 0) { + this->update_interval = 1; + return; } this->update_interval = interval; } diff --git a/src/Builder.cpp b/src/Builder.cpp index 02f62b9..884e390 100644 --- a/src/Builder.cpp +++ b/src/Builder.cpp @@ -13,18 +13,18 @@ void AstriPacketMonitorBuilder::reset() { this->monitor = new AstriPacketMonitor(); } -AstriPacketMonitorBuilder* AstriPacketMonitorBuilder::configFrom(Configurators::BaseConfigurator &conf) { +AstriPacketMonitorBuilder* AstriPacketMonitorBuilder::configFrom( + Configurators::BaseConfigurator &conf) { conf.readConfigFromSource(config_target); std::map params = conf.getConfig(); - std::string key = config_target+"_"+interval_key; - if (params.count(key) > 0){ + std::string key = config_target + "_" + interval_key; + if (params.count(key) > 0) { int interval = 0; - try{ + try { interval = std::stoi(params[key]); this->monitor->setUpdateInterval(interval); - } - catch (const std::invalid_argument& e){ + } catch (const std::invalid_argument &e) { // just keep the old value. } } @@ -32,12 +32,12 @@ AstriPacketMonitorBuilder* AstriPacketMonitorBuilder::configFrom(Configurators:: return this; } -AstriPacketMonitorBuilder* AstriPacketMonitorBuilder::setUpdateInterval(int interval) { +AstriPacketMonitorBuilder* AstriPacketMonitorBuilder::setUpdateInterval( + int interval) { monitor->setUpdateInterval(interval); return this; } - AstriPacketMonitor* AstriPacketMonitorBuilder::getMonitor() { AstriPacketMonitor *result = this->monitor; this->reset(); -- GitLab From db7ecbf313ed4e79ee77063cb47dd1d9ea40ddd0 Mon Sep 17 00:00:00 2001 From: valerio pastore Date: Fri, 12 Jan 2024 20:17:29 +0100 Subject: [PATCH 3/3] . --- deps/Base-DAQ | 2 +- include/Astri_Monitor.h | 4 ++-- src/Astri_Monitor.cpp | 5 ++--- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/deps/Base-DAQ b/deps/Base-DAQ index 8a0ea2d..a00f9a2 160000 --- a/deps/Base-DAQ +++ b/deps/Base-DAQ @@ -1 +1 @@ -Subproject commit 8a0ea2d0e699863df5fe1c91caf2d7b0855957be +Subproject commit a00f9a27afbf5f75dab7db2368b9b9b6fcb395e1 diff --git a/include/Astri_Monitor.h b/include/Astri_Monitor.h index d74c648..fcb20d4 100644 --- a/include/Astri_Monitor.h +++ b/include/Astri_Monitor.h @@ -34,7 +34,7 @@ protected: * * @param pack The packet to monitor. */ - void monitPacketLost(PacketLib::BasePacket &pack); + void monitPacketLost(Packets::BasePacket &pack); static std::string tot_packets_key; /**< Key for total packets statistic. */ static std::string tot_MB_key; /**< Key for total MB statistic. */ @@ -67,7 +67,7 @@ public: * * @param pack The packet to monitor. */ - void monit(PacketLib::BasePacket &pack); + void monit(Packets::BasePacket &pack); /** * @brief Resets the monitor and clears the statistics. diff --git a/src/Astri_Monitor.cpp b/src/Astri_Monitor.cpp index e0873fe..9d55a3a 100644 --- a/src/Astri_Monitor.cpp +++ b/src/Astri_Monitor.cpp @@ -1,4 +1,3 @@ - #include using namespace inaf::oasbo::PacketMonitors; @@ -41,7 +40,7 @@ void AstriPacketMonitor::reset() { packsLastNs = 0; } -void AstriPacketMonitor::monit(PacketLib::BasePacket &pack) { +void AstriPacketMonitor::monit(Packets::BasePacket &pack) { using namespace std; stats[tot_packets_key] = to_string(stoi(stats[tot_packets_key]) + 1); // count tot packets // counts tot for each type_subtype @@ -82,7 +81,7 @@ void AstriPacketMonitor::monit(PacketLib::BasePacket &pack) { stats[tot_lost_key] = to_string(tot_losts); } -void AstriPacketMonitor::monitPacketLost(PacketLib::BasePacket &pack) { +void AstriPacketMonitor::monitPacketLost(Packets::BasePacket &pack) { using namespace std; int curr_ssc = pack["Source Sequence Counter SSC"].value(); string type = to_string(pack["Packet Type"].value()); -- GitLab