Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 1 | #include <chrono> |
| 2 | #include <phosphor-logging/log.hpp> |
| 3 | #include "watchdog.hpp" |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 4 | namespace phosphor |
| 5 | { |
| 6 | namespace watchdog |
| 7 | { |
| 8 | using namespace std::chrono; |
| 9 | using namespace std::chrono_literals; |
| 10 | using namespace phosphor::logging; |
| 11 | |
Vishwanatha Subbanna | 3473d70 | 2017-05-30 16:38:50 +0530 | [diff] [blame] | 12 | // systemd service to kick start a target. |
| 13 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 14 | constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1"; |
| 15 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
| 16 | |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 17 | // Enable or disable watchdog |
| 18 | bool Watchdog::enabled(bool value) |
| 19 | { |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 20 | if (this->enabled() != value) |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 21 | { |
| 22 | if (value) |
| 23 | { |
| 24 | // Start ONESHOT timer. Timer handles all in usec |
| 25 | auto usec = duration_cast<microseconds>( |
| 26 | milliseconds(this->interval())); |
| 27 | // Update new expiration |
| 28 | timer.start(usec); |
| 29 | |
| 30 | // Enable timer |
| 31 | timer.setEnabled<std::true_type>(); |
| 32 | |
| 33 | log<level::INFO>("watchdog: enabled and started", |
| 34 | entry("INTERVAL=%llu", this->interval())); |
| 35 | } |
| 36 | else |
| 37 | { |
| 38 | timer.setEnabled<std::false_type>(); |
Patrick Venture | 09eebe3 | 2017-08-11 15:23:17 -0700 | [diff] [blame] | 39 | timer.clearExpired(); |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 40 | log<level::INFO>("watchdog: disabled"); |
| 41 | } |
| 42 | } |
| 43 | return WatchdogInherits::enabled(value); |
| 44 | } |
| 45 | |
| 46 | // Get the remaining time before timer expires. |
| 47 | // If the timer is disabled, returns 0 |
| 48 | uint64_t Watchdog::timeRemaining() const |
| 49 | { |
| 50 | uint64_t timeRemain = 0; |
| 51 | |
William A. Kennington III | 1c9515f | 2018-02-27 18:54:36 -0800 | [diff] [blame] | 52 | // timer may have already expired and disabled |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 53 | if (timerEnabled()) |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 54 | { |
William A. Kennington III | 1c9515f | 2018-02-27 18:54:36 -0800 | [diff] [blame] | 55 | // the one-shot timer does not expire yet |
| 56 | auto expiry = duration_cast<milliseconds>( |
| 57 | timer.getRemaining()); |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 58 | |
William A. Kennington III | 1c9515f | 2018-02-27 18:54:36 -0800 | [diff] [blame] | 59 | // convert to msec per interface expectation. |
| 60 | auto timeNow = duration_cast<milliseconds>( |
| 61 | Timer::getCurrentTime()); |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 62 | |
William A. Kennington III | 1c9515f | 2018-02-27 18:54:36 -0800 | [diff] [blame] | 63 | // Its possible that timer may have expired by now. |
| 64 | // So need to cross verify. |
| 65 | timeRemain = (expiry > timeNow) ? |
| 66 | (expiry - timeNow).count() : 0; |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 67 | } |
| 68 | return timeRemain; |
| 69 | } |
| 70 | |
| 71 | // Reset the timer to a new expiration value |
| 72 | uint64_t Watchdog::timeRemaining(uint64_t value) |
| 73 | { |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 74 | if (!timerEnabled()) |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 75 | { |
William A. Kennington III | 5e3f877 | 2018-02-27 18:59:58 -0800 | [diff] [blame] | 76 | // We don't need to update the timer because it is off |
| 77 | return 0; |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 78 | } |
William A. Kennington III | 5e3f877 | 2018-02-27 18:59:58 -0800 | [diff] [blame] | 79 | |
| 80 | // Update new expiration |
| 81 | auto usec = duration_cast<microseconds>(milliseconds(value)); |
| 82 | timer.start(usec); |
| 83 | |
| 84 | // Update Base class data. |
| 85 | return WatchdogInherits::timeRemaining(value); |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 86 | } |
| 87 | |
Vishwanatha Subbanna | 8c5a229 | 2017-05-30 15:34:23 +0530 | [diff] [blame] | 88 | // Optional callback function on timer expiration |
| 89 | void Watchdog::timeOutHandler() |
| 90 | { |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 91 | auto action = expireAction(); |
| 92 | auto target = actionTargets.find(action); |
Vishwanatha Subbanna | 3473d70 | 2017-05-30 16:38:50 +0530 | [diff] [blame] | 93 | |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 94 | if (target == actionTargets.end()) |
| 95 | { |
| 96 | log<level::INFO>("watchdog: Timed out with no target", |
| 97 | entry("ACTION=%s", convertForMessage(action).c_str())); |
| 98 | return; |
Vishwanatha Subbanna | 3473d70 | 2017-05-30 16:38:50 +0530 | [diff] [blame] | 99 | } |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 100 | |
| 101 | auto method = bus.new_method_call(SYSTEMD_SERVICE, |
| 102 | SYSTEMD_ROOT, |
| 103 | SYSTEMD_INTERFACE, |
| 104 | "StartUnit"); |
| 105 | method.append(target->second); |
| 106 | method.append("replace"); |
| 107 | |
| 108 | log<level::INFO>("watchdog: Timed out", |
| 109 | entry("ACTION=%s", convertForMessage(action).c_str()), |
| 110 | entry("TARGET=%s", target->second.c_str())); |
| 111 | bus.call_noreply(method); |
Vishwanatha Subbanna | 8c5a229 | 2017-05-30 15:34:23 +0530 | [diff] [blame] | 112 | } |
| 113 | |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 114 | } // namespace watchdog |
| 115 | } // namepsace phosphor |