metrics: Fix port iterator after remove
Update the iterator after removing while looping through the hashmap.
Also cleaned up some memory leak issues with string_view.
Also changed to use steady_timer for monotonic time.
Google-Bug-Id: 271869660
Change-Id: I2363bbd451265d0e40ed44e690caab0b2b95fe00
Signed-off-by: Willy Tu <wltu@google.com>
diff --git a/recipes-google/metric-collector/metric-collector/0002-Metric-Collection-Daemon.patch b/recipes-google/metric-collector/metric-collector/0002-Metric-Collection-Daemon.patch
index ea2674d..af46e45 100644
--- a/recipes-google/metric-collector/metric-collector/0002-Metric-Collection-Daemon.patch
+++ b/recipes-google/metric-collector/metric-collector/0002-Metric-Collection-Daemon.patch
@@ -1,4 +1,4 @@
-From 33a6928788de30db57687ac96712ba719ba6b992 Mon Sep 17 00:00:00 2001
+From cf8375ff5286c7effaf9aad31b6a4532a3e67393 Mon Sep 17 00:00:00 2001
From: Edward Lee <edwarddl@google.com>
Date: Wed, 18 Jan 2023 21:37:49 +0000
Subject: [PATCH] Metric-Collection-Daemon
@@ -86,11 +86,11 @@
.../metricCollector.cpp | 392 ++++++++++++++++++
.../metricCollector.hpp | 79 ++++
.../phosphor-metric-collector.service.in | 11 +
- subprojects/metric-collection-daemon/port.cpp | 197 +++++++++
+ subprojects/metric-collection-daemon/port.cpp | 193 +++++++++
subprojects/metric-collection-daemon/port.hpp | 64 +++
.../metric-collection-daemon/utils.cpp | 179 ++++++++
.../metric-collection-daemon/utils.hpp | 35 ++
- 13 files changed, 1288 insertions(+)
+ 13 files changed, 1284 insertions(+)
create mode 120000 metric-collection-daemon
create mode 100644 subprojects/metric-collection-daemon/daemon.cpp
create mode 100644 subprojects/metric-collection-daemon/daemon.hpp
@@ -137,7 +137,7 @@
\ No newline at end of file
diff --git a/subprojects/metric-collection-daemon/daemon.cpp b/subprojects/metric-collection-daemon/daemon.cpp
new file mode 100644
-index 0000000..5fd9c0b
+index 0000000..3b526e6
--- /dev/null
+++ b/subprojects/metric-collection-daemon/daemon.cpp
@@ -0,0 +1,215 @@
@@ -230,7 +230,7 @@
+ try
+ {
+ const std::string& statPath = fmt::format("/proc/{}/stat", pid);
-+ std::string_view processStats = readFileIntoString(statPath);
++ const std::string& processStats = readFileIntoString(statPath);
+
+ const long ticksPerSec = getTicksPerSec();
+
@@ -484,7 +484,7 @@
+ install_dir: systemd.get_pkgconfig_variable('systemdsystemunitdir'))
diff --git a/subprojects/metric-collection-daemon/metricCollector.cpp b/subprojects/metric-collection-daemon/metricCollector.cpp
new file mode 100644
-index 0000000..5c44b2b
+index 0000000..e5e93a4
--- /dev/null
+++ b/subprojects/metric-collection-daemon/metricCollector.cpp
@@ -0,0 +1,392 @@
@@ -500,7 +500,7 @@
+#include <syslog.h>
+#include <unistd.h>
+
-+#include <boost/asio/deadline_timer.hpp>
++#include <boost/asio/steady_timer.hpp>
+#include <sdbusplus/asio/connection.hpp>
+#include <sdbusplus/asio/object_server.hpp>
+#include <sdbusplus/bus/match.hpp>
@@ -679,17 +679,17 @@
+ }
+
+ // Remove nonexistent ports
-+ for (auto it = bmcPorts.begin(); it != bmcPorts.end(); it++)
++ for (auto it = bmcPorts.begin(); it != bmcPorts.end();)
+ {
-+ auto portId = it->first;
-+ if (!allPortIds.contains(portId))
++ if (!allPortIds.contains(it->first))
+ {
+ removeAssociation(it->second.getObjectPath());
-+ bmcPorts.erase(it);
++ it = bmcPorts.erase(it);
+ continue;
+ }
+ // Update port info of existing ports
+ it->second.updatePortInfo();
++ ++it;
+ }
+
+ // Add new ports
@@ -834,10 +834,10 @@
+
+} // namespace
+
-+void metricCollectCallback(std::shared_ptr<boost::asio::deadline_timer> timer,
++void metricCollectCallback(std::shared_ptr<boost::asio::steady_timer> timer,
+ sdbusplus::bus_t& bus, MetricCollector& metricCol)
+{
-+ timer->expires_from_now(boost::posix_time::seconds(TIMER_INTERVAL));
++ timer->expires_from_now(std::chrono::seconds(TIMER_INTERVAL));
+ timer->async_wait([timer, &bus,
+ &metricCol](const boost::system::error_code& ec) {
+ if (ec == boost::asio::error::operation_aborted)
@@ -869,7 +869,7 @@
+
+ auto metricCol = std::make_shared<MetricCollector>(io, conn, objectServer);
+
-+ auto metricCollectTimer = std::make_shared<boost::asio::deadline_timer>(io);
++ auto metricCollectTimer = std::make_shared<boost::asio::steady_timer>(io);
+
+ // Start the timer
+ io.post([conn, &metricCollectTimer, &metricCol]() {
@@ -984,10 +984,10 @@
+WantedBy=multi-user.target
diff --git a/subprojects/metric-collection-daemon/port.cpp b/subprojects/metric-collection-daemon/port.cpp
new file mode 100644
-index 0000000..fceb225
+index 0000000..6eb847d
--- /dev/null
+++ b/subprojects/metric-collection-daemon/port.cpp
-@@ -0,0 +1,197 @@
+@@ -0,0 +1,193 @@
+#include "port.hpp"
+
+#include "utils.hpp"
@@ -1074,7 +1074,7 @@
+{
+ const std::string& speedPath =
+ fmt::format("/sys/class/net/{}/speed", interfaceName);
-+ std::string_view speedFile = readFileIntoString(speedPath);
++ const std::string& speedFile = readFileIntoString(speedPath);
+
+ double speed;
+ auto res = std::from_chars(speedFile.data(),
@@ -1082,7 +1082,7 @@
+
+ if (res.ec != std::errc())
+ {
-+ speed = 0;
++ return 0;
+ }
+
+ return speed / 1000;
@@ -1092,20 +1092,16 @@
+{
+ const std::string& dormantPath =
+ fmt::format("/sys/class/net/{}/dormant", interfaceName);
-+ std::string_view dormant = readFileIntoString(dormantPath);
+ // https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-class-net
-+
-+ return toSizeT(std::string(dormant)) == 0;
++ return toSizeT(readFileIntoString(dormantPath)) == 0;
+}
+
+bool Port::getLinkUp(std::string interfaceName)
+{
+ const std::string& linkUpPath =
+ fmt::format("/sys/class/net/{}/carrier", interfaceName);
-+ std::string_view linkUp = readFileIntoString(linkUpPath);
-+
+ // https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-class-net
-+ return toSizeT(std::string(linkUp)) != 0;
++ return toSizeT(readFileIntoString(linkUpPath)) != 0;
+}
+
+void Port::updatePortProperties()
@@ -1185,10 +1181,9 @@
+{
+ return rxDroppedPackets;
+}
-\ No newline at end of file
diff --git a/subprojects/metric-collection-daemon/port.hpp b/subprojects/metric-collection-daemon/port.hpp
new file mode 100644
-index 0000000..9b828d5
+index 0000000..2dbe89e
--- /dev/null
+++ b/subprojects/metric-collection-daemon/port.hpp
@@ -0,0 +1,64 @@
@@ -1256,10 +1251,9 @@
+ size_t rxDroppedPackets;
+ double speed;
+};
-\ No newline at end of file
diff --git a/subprojects/metric-collection-daemon/utils.cpp b/subprojects/metric-collection-daemon/utils.cpp
new file mode 100644
-index 0000000..4943002
+index 0000000..712d668
--- /dev/null
+++ b/subprojects/metric-collection-daemon/utils.cpp
@@ -0,0 +1,179 @@
@@ -1326,7 +1320,7 @@
+ return sysconf(_SC_CLK_TCK);
+}
+
-+std::string_view readFileIntoString(const std::string_view fileName)
++std::string readFileIntoString(const std::string_view fileName)
+{
+ std::stringstream ss;
+ std::ifstream ifs(fileName.data());
@@ -1382,7 +1376,7 @@
+std::array<size_t, 3> parseBootInfo()
+{
+ const std::string& bootInfoPath = "/var/google/bootinfo";
-+ std::string_view bootinfoFile = readFileIntoString(bootInfoPath);
++ const std::string& bootinfoFile = readFileIntoString(bootInfoPath);
+ std::array<size_t, 3> bootinfo{0, 0, 0};
+
+ // If file does not exist, then just set boot and crash counts to 0
@@ -1442,10 +1436,9 @@
+
+ return std::nullopt;
+}
-\ No newline at end of file
diff --git a/subprojects/metric-collection-daemon/utils.hpp b/subprojects/metric-collection-daemon/utils.hpp
new file mode 100644
-index 0000000..20a8029
+index 0000000..e4e2996
--- /dev/null
+++ b/subprojects/metric-collection-daemon/utils.hpp
@@ -0,0 +1,35 @@
@@ -1476,7 +1469,7 @@
+
+std::optional<int> isNumericPath(std::string_view path);
+long getTicksPerSec();
-+std::string_view readFileIntoString(const std::string_view fileName);
++std::string readFileIntoString(const std::string_view fileName);
+std::vector<std::string> split(std::string_view input, char delim);
+
+size_t toSizeT(std::string intString);
@@ -1484,6 +1477,6 @@
+std::array<size_t, 3> parseBootInfo();
+
+std::optional<std::string> getPortIdByNum(const int portNum);
-\ No newline at end of file
--
-2.39.2.637.g21b0678d19-goog
+2.40.0.348.gf938b09366-goog
+