Set lastRebootTime once at startup only
The lastRebootTime doesn't change once the daemon starts, so we only
need to fetch the info once and save the time instead of calculating it
everytime.
Change-Id: I5e665d637f122727be9bb8f0e7b731d75a567f02
Signed-off-by: Willy Tu <wltu@google.com>
diff --git a/bmc_state_manager.cpp b/bmc_state_manager.cpp
index 5071d89..9f67a31 100644
--- a/bmc_state_manager.cpp
+++ b/bmc_state_manager.cpp
@@ -4,13 +4,11 @@
#include "xyz/openbmc_project/Common/error.hpp"
#include <gpiod.h>
-#include <sys/sysinfo.h>
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/lg2.hpp>
#include <sdbusplus/exception.hpp>
-#include <cassert>
#include <filesystem>
#include <fstream>
#include <iostream>
@@ -246,17 +244,7 @@
uint64_t BMC::lastRebootTime() const
{
- using namespace std::chrono;
- struct sysinfo info;
-
- auto rc = sysinfo(&info);
- assert(rc == 0);
-
- // Since uptime is in seconds, also get the current time in seconds.
- auto now = time_point_cast<seconds>(system_clock::now());
- auto rebootTime = now - seconds(info.uptime);
-
- return duration_cast<milliseconds>(rebootTime.time_since_epoch()).count();
+ return rebootTime;
}
void BMC::discoverLastRebootCause()
diff --git a/bmc_state_manager.hpp b/bmc_state_manager.hpp
index e832451..d50d4f3 100644
--- a/bmc_state_manager.hpp
+++ b/bmc_state_manager.hpp
@@ -4,9 +4,13 @@
#include "xyz/openbmc_project/State/BMC/server.hpp"
#include <linux/watchdog.h>
+#include <sys/sysinfo.h>
#include <sdbusplus/bus.hpp>
+#include <cassert>
+#include <chrono>
+
namespace phosphor
{
namespace state
@@ -44,6 +48,19 @@
utils::subscribeToSystemdSignals(bus);
discoverInitialState();
discoverLastRebootCause();
+
+ using namespace std::chrono;
+ struct sysinfo info;
+
+ auto rc = sysinfo(&info);
+ assert(rc == 0);
+ // Since uptime is in seconds, also get the current time in seconds.
+ auto now = time_point_cast<seconds>(system_clock::now());
+ auto rebootTimeTs = now - seconds(info.uptime);
+ rebootTime =
+ duration_cast<milliseconds>(rebootTimeTs.time_since_epoch())
+ .count();
+
this->emit_object_added();
};
@@ -102,6 +119,11 @@
* @brief discover the last reboot cause of the bmc
**/
void discoverLastRebootCause();
+
+ /**
+ * @brief the lastRebootTime calcuated at startup.
+ **/
+ uint64_t rebootTime;
};
} // namespace manager