diff options
author | William A. Kennington III <wak@google.com> | 2018-09-12 18:30:09 -0700 |
---|---|---|
committer | William A. Kennington III <wak@google.com> | 2018-10-05 09:14:28 +0000 |
commit | f505fc0674d5cf14a8b1903cb894e4a171e28400 (patch) | |
tree | de00af8a494be633c9127a3fb8a91c04f2a0da9d | |
parent | 7036c569c8de530165a3ee029cdeaa20fb9e9a41 (diff) | |
download | phosphor-watchdog-f505fc0674d5cf14a8b1903cb894e4a171e28400.tar.gz phosphor-watchdog-f505fc0674d5cf14a8b1903cb894e4a171e28400.zip |
Convert to using sdeventplus
This gets rid of the ad-hoc timer class in favor of using the timer
source built into sdeventplus.
Tested:
Unit tests pass and everything still builds. Manually verified the
functionality is still in tact on a zaius machine.
Change-Id: I90619f0fe5a9fdfcecd24a49de672c0c99dc95e9
Signed-off-by: William A. Kennington III <wak@google.com>
-rw-r--r-- | Makefile.am | 12 | ||||
-rw-r--r-- | configure.ac | 2 | ||||
-rw-r--r-- | mainapp.cpp | 36 | ||||
-rw-r--r-- | test/Makefile.am | 10 | ||||
-rw-r--r-- | test/timer_test.cpp | 72 | ||||
-rw-r--r-- | test/timer_test.hpp | 39 | ||||
-rw-r--r-- | test/watchdog_test.cpp | 20 | ||||
-rw-r--r-- | test/watchdog_test.hpp | 17 | ||||
-rw-r--r-- | timer.cpp | 134 | ||||
-rw-r--r-- | timer.hpp | 147 | ||||
-rw-r--r-- | watchdog.cpp | 44 | ||||
-rw-r--r-- | watchdog.hpp | 21 |
12 files changed, 62 insertions, 492 deletions
diff --git a/Makefile.am b/Makefile.am index 806a0aa..2de74ce 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,21 +1,19 @@ sbin_PROGRAMS = phosphor-watchdog -noinst_HEADERS = timer.hpp \ - watchdog.hpp +noinst_HEADERS = watchdog.hpp phosphor_watchdog_SOURCES = \ argument.cpp \ - timer.cpp \ watchdog.cpp \ mainapp.cpp -phosphor_watchdog_LDFLAGS = $(SYSTEMD_LIBS) \ - ${PHOSPHOR_LOGGING_LIBS} \ +phosphor_watchdog_LDFLAGS = ${PHOSPHOR_LOGGING_LIBS} \ ${SDBUSPLUS_LIBS} \ + ${SDEVENTPLUS_LIBS} \ ${PHOSPHOR_DBUS_INTERFACES_LIBS} -phosphor_watchdog_CXXFLAGS = $(SYSTEMD_CFLAGS)\ - ${PHOSPHOR_LOGGING_CFLAGS} \ +phosphor_watchdog_CXXFLAGS = ${PHOSPHOR_LOGGING_CFLAGS} \ ${SDBUSPLUS_CFLAGS} \ + ${SDEVENTPLUS_CFLAGS} \ ${PHOSPHOR_DBUS_INTERFACES_CFLAGS} SUBDIRS = test diff --git a/configure.ac b/configure.ac index 6c5ac70..cd4ae00 100644 --- a/configure.ac +++ b/configure.ac @@ -19,9 +19,9 @@ AX_APPEND_COMPILE_FLAGS([-Wall -Werror], [CXXFLAGS]) LT_INIT # Check for needed modules -PKG_CHECK_MODULES([SYSTEMD], [libsystemd >= 221], [], [AC_MSG_ERROR(["systemd required and not found"])]) PKG_CHECK_MODULES([PHOSPHOR_LOGGING], [phosphor-logging], [], [AC_MSG_ERROR([Could not find phosphor-logging...openbmc/phosphor-logging package required])]) PKG_CHECK_MODULES([SDBUSPLUS], [sdbusplus],, [AC_MSG_ERROR([Could not find sdbusplus...openbmc/sdbusplus package required])]) +PKG_CHECK_MODULES([SDEVENTPLUS], [sdeventplus],, [AC_MSG_ERROR([Could not find sdeventplus...openbmc/sdeventplus package required])]) PKG_CHECK_MODULES([PHOSPHOR_DBUS_INTERFACES], [phosphor-dbus-interfaces], [], [AC_MSG_ERROR([Could not find phosphor-dbus-interfaces...openbmc/phosphor-dbus-interfaces package required])]) PKG_CHECK_MODULES([GTEST_MAIN], [gtest_main], [], [ diff --git a/mainapp.cpp b/mainapp.cpp index 29cd7e2..b98b108 100644 --- a/mainapp.cpp +++ b/mainapp.cpp @@ -196,29 +196,22 @@ int main(int argc, char* argv[]) fallback->always = true; } - sd_event* event = nullptr; - auto r = sd_event_default(&event); - if (r < 0) + try { - log<level::ERR>("Error creating a default sd_event handler"); - return r; - } - phosphor::watchdog::EventPtr eventP{event}; - event = nullptr; + // Get a default event loop + auto event = sdeventplus::Event::get_default(); - // Get a handle to system dbus. - auto bus = sdbusplus::bus::new_default(); + // Get a handle to system dbus. + auto bus = sdbusplus::bus::new_default(); - // Add systemd object manager. - sdbusplus::server::manager::manager(bus, path.c_str()); + // Add systemd object manager. + sdbusplus::server::manager::manager(bus, path.c_str()); - // Attach the bus to sd_event to service user requests - bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL); + // Attach the bus to sd_event to service user requests + bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); - try - { // Create a watchdog object - Watchdog watchdog(bus, path.c_str(), eventP, std::move(actionTargetMap), + Watchdog watchdog(bus, path.c_str(), event, std::move(actionTargetMap), std::move(fallback)); // Claim the bus @@ -227,13 +220,8 @@ int main(int argc, char* argv[]) // Loop until our timer expires and we don't want to continue while (continueAfterTimeout || !watchdog.timerExpired()) { - // -1 denotes wait for ever - r = sd_event_run(eventP.get(), (uint64_t)-1); - if (r < 0) - { - log<level::ERR>("Error waiting for events"); - elog<InternalFailure>(); - } + // Run and never timeout + event.run(std::nullopt); } } catch (InternalFailure& e) diff --git a/test/Makefile.am b/test/Makefile.am index 48b3559..1c44162 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -5,12 +5,12 @@ TESTS = $(check_PROGRAMS) # Build/add utest to test suite check_PROGRAMS = argument_test \ - timer_test \ watchdog_test utestCPPFLAGS = $(GTEST_MAIN_CFLAGS) \ $(AM_CPPFLAGS) \ $(SDBUSPLUS_CFLAGS) \ + $(SDEVENTPLUS_CFLAGS) \ $(PHOSPHOR_LOGGING_CFLAGS) \ $(PHOSPHOR_DBUS_INTERFACES_CFLAGS) @@ -20,6 +20,7 @@ utestLDFLAGS = $(GTEST_MAIN_LIBS) \ $(PTHREAD_LIBS) \ $(OESDK_TESTCASE_FLAGS) \ $(SDBUSPLUS_LIBS) \ + $(SDEVENTPLUS_LIBS) \ $(PHOSPHOR_LOGGING_LIBS) \ $(PHOSPHOR_DBUS_INTERFACES_LIBS) @@ -27,14 +28,9 @@ argument_test_CPPFLAGS = ${utestCPPFLAGS} argument_test_CXXFLAGS = ${utestCXXFLAGS} argument_test_LDFLAGS = ${utestLDFLAGS} -timer_test_CPPFLAGS = ${utestCPPFLAGS} -timer_test_CXXFLAGS = ${utestCXXFLAGS} -timer_test_LDFLAGS = ${utestLDFLAGS} - watchdog_test_CPPFLAGS = ${utestCPPFLAGS} watchdog_test_CXXFLAGS = ${utestCXXFLAGS} watchdog_test_LDFLAGS = ${utestLDFLAGS} argument_test_SOURCES = ../argument.cpp argument_test.cpp -timer_test_SOURCES = ../timer.cpp timer_test.cpp -watchdog_test_SOURCES = ../timer.cpp ../watchdog.cpp watchdog_test.cpp +watchdog_test_SOURCES = ../watchdog.cpp watchdog_test.cpp diff --git a/test/timer_test.cpp b/test/timer_test.cpp deleted file mode 100644 index ea00228..0000000 --- a/test/timer_test.cpp +++ /dev/null @@ -1,72 +0,0 @@ -#include <chrono> -#include <timer_test.hpp> - -using namespace std::chrono; -using namespace std::chrono_literals; - -/** @brief Starts the timer and expects it to - * expire in configured time and expects the - * deault callback handler to kick-in - */ -TEST_F(TimerTest, testTimerForExpirationDefaultTimeoutHandler) -{ - // Expect timer to expire in 2 seconds - auto expireTime = seconds(2s); - - phosphor::watchdog::Timer timer(eventP); - - // Set the expiration and enable the timer - timer.start(duration_cast<milliseconds>(expireTime)); - timer.setEnabled<std::true_type>(); - - // Waiting 2 seconds to expect expiration - int count = 0; - while (count < expireTime.count() && !timer.expired()) - { - // Returns -0- on timeout and positive number on dispatch - auto sleepTime = duration_cast<microseconds>(seconds(1)); - if (!sd_event_run(eventP.get(), sleepTime.count())) - { - count++; - } - } - EXPECT_TRUE(timer.expired()); - EXPECT_EQ(expireTime.count() - 1, count); - - // Make sure secondary callback was not called. - EXPECT_FALSE(expired); -} - -/** @brief Starts the timer and expects it to expire - * in configured time and expects the secondary - * callback to be called into along with default. - */ -TEST_F(TimerTest, testTimerForExpirationSecondCallBack) -{ - // Expect timer to expire in 2 seconds - auto expireTime = seconds(2s); - - phosphor::watchdog::Timer timer( - eventP, std::bind(&TimerTest::timeOutHandler, this)); - - // Set the expiration and enable the timer - timer.start(duration_cast<milliseconds>(expireTime)); - timer.setEnabled<std::true_type>(); - - // Waiting 2 seconds to expect expiration - int count = 0; - while (count < expireTime.count() && !timer.expired()) - { - // Returns -0- on timeout and positive number on dispatch - auto sleepTime = duration_cast<microseconds>(seconds(1)); - if (!sd_event_run(eventP.get(), sleepTime.count())) - { - count++; - } - } - EXPECT_TRUE(timer.expired()); - EXPECT_EQ(expireTime.count() - 1, count); - - // This gets set as part of secondary callback - EXPECT_TRUE(expired); -} diff --git a/test/timer_test.hpp b/test/timer_test.hpp deleted file mode 100644 index cab9086..0000000 --- a/test/timer_test.hpp +++ /dev/null @@ -1,39 +0,0 @@ -#include <iostream> -#include <timer.hpp> - -#include <gtest/gtest.h> - -// Base class for testing Timer -class TimerTest : public testing::Test -{ - public: - // systemd event handler - sd_event* events; - - // Need this so that events can be initialized. - int rc; - - // Tells if the watchdog timer expired. - bool expired = false; - - // Gets called as part of each TEST_F construction - TimerTest() : rc(sd_event_default(&events)), eventP(events) - { - // Check for successful creation of - // event handler and bus handler - EXPECT_GE(rc, 0); - - // Its already wrapped in eventP - events = nullptr; - } - - // unique_ptr for sd_event - phosphor::watchdog::EventPtr eventP; - - // Handler called by timer expiration - inline void timeOutHandler() - { - std::cout << "Time out handler called" << std::endl; - expired = true; - } -}; diff --git a/test/watchdog_test.cpp b/test/watchdog_test.cpp index aa0ae0f..06b43b8 100644 --- a/test/watchdog_test.cpp +++ b/test/watchdog_test.cpp @@ -1,7 +1,7 @@ #include "watchdog_test.hpp" -#include <chrono> #include <memory> +#include <thread> #include <utility> using namespace phosphor::watchdog; @@ -15,9 +15,8 @@ seconds WdogTest::waitForWatchdog(seconds timeLimit) { previousTimeRemaining = wdog->timeRemaining(); - // Returns -0- on timeout and positive number on dispatch - auto sleepTime = 1s; - if (!sd_event_run(eventP.get(), microseconds(sleepTime).count())) + constexpr auto sleepTime = 1s; + if (event.run(sleepTime) == 0) { ret += sleepTime; } @@ -128,6 +127,10 @@ TEST_F(WdogTest, enableWdogAndResetTo5Seconds) // Sleep for 1 second std::this_thread::sleep_for(1s); + // Timer should still be running unexpired + EXPECT_FALSE(wdog->timerExpired()); + EXPECT_TRUE(wdog->timerEnabled()); + // Next timer will expire in 5 seconds from now. auto expireTime = 5s; auto expireTimeMs = milliseconds(expireTime).count(); @@ -137,9 +140,6 @@ TEST_F(WdogTest, enableWdogAndResetTo5Seconds) EXPECT_EQ(expireTime - 1s, waitForWatchdog(expireTime)); EXPECT_TRUE(wdog->timerExpired()); EXPECT_FALSE(wdog->timerEnabled()); - - // Make sure secondary callback was not called. - EXPECT_FALSE(expired); } /** @brief Make sure the Interval can be updated directly. @@ -218,7 +218,7 @@ TEST_F(WdogTest, enableWdogWithFallbackTillEnd) .action = Watchdog::Action::PowerOff, .interval = static_cast<uint64_t>(fallbackIntervalMs), }; - wdog = std::make_unique<Watchdog>(bus, TEST_PATH, eventP, + wdog = std::make_unique<Watchdog>(bus, TEST_PATH, event, Watchdog::ActionTargetMap(), std::move(fallback)); EXPECT_EQ(primaryInterval, milliseconds(wdog->interval(primaryIntervalMs))); @@ -301,7 +301,7 @@ TEST_F(WdogTest, enableWdogWithFallbackReEnable) .interval = static_cast<uint64_t>(fallbackIntervalMs), .always = false, }; - wdog = std::make_unique<Watchdog>(bus, TEST_PATH, eventP, + wdog = std::make_unique<Watchdog>(bus, TEST_PATH, event, Watchdog::ActionTargetMap(), std::move(fallback)); EXPECT_EQ(primaryInterval, milliseconds(wdog->interval(primaryIntervalMs))); @@ -355,7 +355,7 @@ TEST_F(WdogTest, enableWdogWithFallbackAlways) .interval = static_cast<uint64_t>(fallbackIntervalMs), .always = true, }; - wdog = std::make_unique<Watchdog>(bus, TEST_PATH, eventP, + wdog = std::make_unique<Watchdog>(bus, TEST_PATH, event, Watchdog::ActionTargetMap(), std::move(fallback)); EXPECT_EQ(primaryInterval, milliseconds(wdog->interval(primaryIntervalMs))); diff --git a/test/watchdog_test.hpp b/test/watchdog_test.hpp index 50fa8f1..4c798a7 100644 --- a/test/watchdog_test.hpp +++ b/test/watchdog_test.hpp @@ -1,30 +1,33 @@ #include <chrono> #include <memory> -#include <timer_test.hpp> +#include <sdbusplus/bus.hpp> +#include <sdeventplus/event.hpp> #include <watchdog.hpp> +#include <gtest/gtest.h> + using namespace std::chrono; using namespace std::chrono_literals; // Test Watchdog functionality -class WdogTest : public TimerTest +class WdogTest : public ::testing::Test { public: // Gets called as part of each TEST_F construction WdogTest() : + event(sdeventplus::Event::get_default()), bus(sdbusplus::bus::new_default()), wdog(std::make_unique<phosphor::watchdog::Watchdog>(bus, TEST_PATH, - eventP)), + event)), defaultInterval(milliseconds(wdog->interval())), defaultDrift(30) { - // Check for successful creation of - // event handler and bus handler - EXPECT_GE(rc, 0); - // Initially the watchdog would be disabled EXPECT_FALSE(wdog->enabled()); } + // sdevent Event handle + sdeventplus::Event event; + // sdbusplus handle sdbusplus::bus::bus bus; diff --git a/timer.cpp b/timer.cpp deleted file mode 100644 index 3dfe560..0000000 --- a/timer.cpp +++ /dev/null @@ -1,134 +0,0 @@ -#include "timer.hpp" - -#include <systemd/sd-event.h> - -#include <chrono> -#include <phosphor-logging/elog-errors.hpp> -#include <phosphor-logging/elog.hpp> -#include <phosphor-logging/log.hpp> -#include <xyz/openbmc_project/Common/error.hpp> -namespace phosphor -{ -namespace watchdog -{ - -// For throwing exception -using namespace phosphor::logging; -using InternalFailure = - sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; - -// Initializes the timer object -void Timer::initialize() -{ - // This can not be called more than once. - if (eventSource.get()) - { - log<level::ERR>("Timer already initialized"); - elog<InternalFailure>(); - } - - // Add infinite expiration time - decltype(eventSource.get()) sourcePtr = nullptr; - auto r = sd_event_add_time(event.get(), &sourcePtr, - CLOCK_MONOTONIC, // Time base - UINT64_MAX, // Expire time - way long time - 0, // Use default event accuracy - timeoutHandler, // Callback handler on timeout - this); // User data - eventSource.reset(sourcePtr); - - if (r < 0) - { - log<level::ERR>("Timer initialization failed"); - elog<InternalFailure>(); - } - - // Disable the timer for now - setEnabled<std::false_type>(); -} - -// callback handler on timeout -int Timer::timeoutHandler(sd_event_source* eventSource, uint64_t usec, - void* userData) -{ - using namespace phosphor::logging; - - log<level::INFO>("Timer Expired"); - - auto timer = static_cast<Timer*>(userData); - timer->expire = true; - - // Call an optional callback function - if (timer->userCallBack) - { - timer->userCallBack(); - } - return 0; -} - -// Gets the time from steady_clock -std::chrono::microseconds Timer::getCurrentTime() -{ - using namespace std::chrono; - auto usec = steady_clock::now().time_since_epoch(); - return duration_cast<microseconds>(usec); -} - -// Sets the expiration time and arms the timer -void Timer::start(std::chrono::microseconds usec) -{ - using namespace std::chrono; - - // Get the current MONOTONIC time and add the delta - auto expireTime = getCurrentTime() + usec; - - // Set the time - auto r = sd_event_source_set_time(eventSource.get(), expireTime.count()); - if (r < 0) - { - log<level::ERR>( - "Error setting the expiration time", - entry("MSEC=%llu", duration_cast<milliseconds>(usec).count())); - elog<InternalFailure>(); - } -} - -// Returns current timer enablement type -int Timer::getEnabled() const -{ - int enabled{}; - auto r = sd_event_source_get_enabled(eventSource.get(), &enabled); - if (r < 0) - { - log<level::ERR>("Error getting current timer type enablement state"); - elog<InternalFailure>(); - } - return enabled; -} - -// Enables / disables the timer -void Timer::setEnabled(int type) -{ - auto r = sd_event_source_set_enabled(eventSource.get(), type); - if (r < 0) - { - log<level::ERR>("Error setting the timer type", entry("TYPE=%d", type)); - elog<InternalFailure>(); - } -} - -// Returns time remaining before expiration -std::chrono::microseconds Timer::getRemaining() const -{ - uint64_t next = 0; - auto r = sd_event_source_get_time(eventSource.get(), &next); - if (r < 0) - { - log<level::ERR>("Error fetching remaining time to expire"); - elog<InternalFailure>(); - } - return std::chrono::microseconds(next); -} - -} // namespace watchdog -} // namespace phosphor diff --git a/timer.hpp b/timer.hpp deleted file mode 100644 index 28222a2..0000000 --- a/timer.hpp +++ /dev/null @@ -1,147 +0,0 @@ -#pragma once - -#include <systemd/sd-event.h> - -#include <chrono> -#include <functional> -#include <memory> -namespace phosphor -{ -namespace watchdog -{ - -/* Need a custom deleter for freeing up sd_event */ -struct EventDeleter -{ - void operator()(sd_event* event) const - { - event = sd_event_unref(event); - } -}; -using EventPtr = std::unique_ptr<sd_event, EventDeleter>; - -/* Need a custom deleter for freeing up sd_event_source */ -struct EventSourceDeleter -{ - void operator()(sd_event_source* eventSource) const - { - eventSource = sd_event_source_unref(eventSource); - } -}; -using EventSourcePtr = std::unique_ptr<sd_event_source, EventSourceDeleter>; - -/** @class Timer - * @brief Manages starting timers and handling timeouts - */ -class Timer -{ - public: - Timer() = delete; - ~Timer() = default; - Timer(const Timer&) = delete; - Timer& operator=(const Timer&) = delete; - Timer(Timer&&) = delete; - Timer& operator=(Timer&&) = delete; - - /** @brief Constructs timer object - * - * @param[in] event - sd_event unique pointer - * @param[in] userCallBack - Optional function callback - * for timer expiration - */ - Timer(EventPtr& event, std::function<void()> userCallBack = nullptr) : - event(event), userCallBack(userCallBack) - { - // Initialize the timer - initialize(); - } - - void clearExpired(void) - { - expire = false; - } - - /** @brief Tells whether the timer is expired or not */ - inline auto expired() const - { - return expire; - } - - /** @brief Returns the current Timer enablement type */ - int getEnabled() const; - - /** @brief Enables / disables the timer. - * <T> is an integral constant boolean - */ - template <typename T> - void setEnabled() - { - constexpr auto type = T::value ? SD_EVENT_ONESHOT : SD_EVENT_OFF; - return setEnabled(type); - } - - /** @brief Returns time remaining in usec before expiration - * which is an offset to current steady clock - */ - std::chrono::microseconds getRemaining() const; - - /** @brief Starts the timer with specified expiration value. - * std::steady_clock is used for base time. - * - * @param[in] usec - Microseconds from the current time - * before expiration. - * - * @return None. - * - * @error Throws exception - */ - void start(std::chrono::microseconds usec); - - /** @brief Gets the current time from steady clock */ - static std::chrono::microseconds getCurrentTime(); - - private: - /** @brief Reference to sd_event unique pointer */ - EventPtr& event; - - /** @brief event source */ - EventSourcePtr eventSource; - - /** @brief Set to true when the timeoutHandler is called into */ - bool expire = false; - - /** @brief Optional function to call on timer expiration - * This is called from timeout handler. - */ - std::function<void()> userCallBack; - - /** @brief Initializes the timer object with infinite - * expiration time and sets up the callback handler - * - * @return None. - * - * @error Throws exception - */ - void initialize(); - - /** @brief Callback function when timer goes off - * - * @param[in] eventSource - Source of the event - * @param[in] usec - time in microseconds - * @param[in] userData - User data pointer - * - */ - static int timeoutHandler(sd_event_source* eventSource, uint64_t usec, - void* userData); - - /** @brief Enables / disables the timer - * - * @param[in] type - Timer type. - * This implementation uses only SD_EVENT_OFF - * and SD_EVENT_ONESHOT - */ - void setEnabled(int type); -}; - -} // namespace watchdog -} // namespace phosphor diff --git a/watchdog.cpp b/watchdog.cpp index 92134ff..aed0443 100644 --- a/watchdog.cpp +++ b/watchdog.cpp @@ -47,18 +47,10 @@ bool Watchdog::enabled(bool value) } else if (!this->enabled()) { - // Start ONESHOT timer. Timer handles all in usec - auto usec = duration_cast<microseconds>(milliseconds(this->interval())); - - // Update new expiration - timer.clearExpired(); - timer.start(usec); - - // Enable timer - timer.setEnabled<std::true_type>(); - + auto interval_ms = this->interval(); + timer.restart(milliseconds(interval_ms)); log<level::INFO>("watchdog: enabled and started", - entry("INTERVAL=%llu", this->interval())); + entry("INTERVAL=%llu", interval_ms)); } return WatchdogInherits::enabled(value); @@ -68,22 +60,13 @@ bool Watchdog::enabled(bool value) // If the timer is disabled, returns 0 uint64_t Watchdog::timeRemaining() const { - uint64_t timeRemain = 0; - // timer may have already expired and disabled - if (timerEnabled()) + if (!timerEnabled()) { - // the one-shot timer does not expire yet - auto expiry = duration_cast<milliseconds>(timer.getRemaining()); - - // convert to msec per interface expectation. - auto timeNow = duration_cast<milliseconds>(Timer::getCurrentTime()); - - // Its possible that timer may have expired by now. - // So need to cross verify. - timeRemain = (expiry > timeNow) ? (expiry - timeNow).count() : 0; + return 0; } - return timeRemain; + + return duration_cast<milliseconds>(timer.getRemaining()).count(); } // Reset the timer to a new expiration value @@ -103,8 +86,7 @@ uint64_t Watchdog::timeRemaining(uint64_t value) } // Update new expiration - auto usec = duration_cast<microseconds>(milliseconds(value)); - timer.start(usec); + timer.setRemaining(milliseconds(value)); // Update Base class data. return WatchdogInherits::timeRemaining(value); @@ -159,19 +141,13 @@ void Watchdog::tryFallbackOrDisable() if (fallback && (fallback->always || this->enabled())) { auto interval_ms = fallback->interval; - auto interval_us = - duration_cast<microseconds>(milliseconds(interval_ms)); - - timer.clearExpired(); - timer.start(interval_us); - timer.setEnabled<std::true_type>(); - + timer.restart(milliseconds(interval_ms)); log<level::INFO>("watchdog: falling back", entry("INTERVAL=%llu", interval_ms)); } else if (timerEnabled()) { - timer.setEnabled<std::false_type>(); + timer.setEnabled(false); log<level::INFO>("watchdog: disabled"); } diff --git a/watchdog.hpp b/watchdog.hpp index c9821db..34b0411 100644 --- a/watchdog.hpp +++ b/watchdog.hpp @@ -1,11 +1,11 @@ #pragma once -#include "timer.hpp" - #include <functional> #include <optional> #include <sdbusplus/bus.hpp> #include <sdbusplus/server/object.hpp> +#include <sdeventplus/event.hpp> +#include <sdeventplus/utility/timer.hpp> #include <unordered_map> #include <utility> #include <xyz/openbmc_project/State/Watchdog/server.hpp> @@ -53,13 +53,14 @@ class Watchdog : public WatchdogInherits /** @brief Constructs the Watchdog object * - * @param[in] bus - DBus bus to attach to. - * @param[in] objPath - Object path to attach to. - * @param[in] event - reference to sd_event unique pointer - * @param[in] actionTargetMap - map of systemd targets called on timeout + * @param[in] bus - DBus bus to attach to. + * @param[in] objPath - Object path to attach to. + * @param[in] event - reference to sdeventplus::Event loop + * @param[in] actionTargets - map of systemd targets called on timeout * @param[in] fallback */ - Watchdog(sdbusplus::bus::bus& bus, const char* objPath, EventPtr& event, + Watchdog(sdbusplus::bus::bus& bus, const char* objPath, + const sdeventplus::Event& event, ActionTargetMap&& actionTargetMap = {}, std::optional<Fallback>&& fallback = std::nullopt) : WatchdogInherits(bus, objPath), @@ -118,13 +119,13 @@ class Watchdog : public WatchdogInherits /** @brief Tells if the referenced timer is expired or not */ inline auto timerExpired() const { - return timer.expired(); + return timer.hasExpired(); } /** @brief Tells if the timer is running or not */ inline bool timerEnabled() const { - return timer.getEnabled() != SD_EVENT_OFF; + return timer.isEnabled(); } private: @@ -138,7 +139,7 @@ class Watchdog : public WatchdogInherits std::optional<Fallback> fallback; /** @brief Contained timer object */ - Timer timer; + sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer; /** @brief Optional Callback handler on timer expirartion */ void timeOutHandler(); |