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 | |
William A. Kennington III | 1726df6 | 2018-04-23 10:28:28 -0700 | [diff] [blame^] | 17 | void Watchdog::resetTimeRemaining(bool enableWatchdog) |
| 18 | { |
| 19 | timeRemaining(interval()); |
| 20 | if (enableWatchdog) |
| 21 | { |
| 22 | enabled(true); |
| 23 | } |
| 24 | } |
| 25 | |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 26 | // Enable or disable watchdog |
| 27 | bool Watchdog::enabled(bool value) |
| 28 | { |
William A. Kennington III | 825f498 | 2018-02-27 19:10:56 -0800 | [diff] [blame] | 29 | if (!value) |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 30 | { |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 31 | // Make sure we accurately reflect our enabled state to the |
| 32 | // tryFallbackOrDisable() call |
| 33 | WatchdogInherits::enabled(value); |
| 34 | |
| 35 | // Attempt to fallback or disable our timer if needed |
| 36 | tryFallbackOrDisable(); |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 37 | |
William A. Kennington III | 825f498 | 2018-02-27 19:10:56 -0800 | [diff] [blame] | 38 | return false; |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 39 | } |
William A. Kennington III | 825f498 | 2018-02-27 19:10:56 -0800 | [diff] [blame] | 40 | else if (!this->enabled()) |
| 41 | { |
| 42 | // Start ONESHOT timer. Timer handles all in usec |
| 43 | auto usec = duration_cast<microseconds>( |
| 44 | milliseconds(this->interval())); |
| 45 | |
| 46 | // Update new expiration |
| 47 | timer.clearExpired(); |
| 48 | timer.start(usec); |
| 49 | |
| 50 | // Enable timer |
| 51 | timer.setEnabled<std::true_type>(); |
| 52 | |
| 53 | log<level::INFO>("watchdog: enabled and started", |
| 54 | entry("INTERVAL=%llu", this->interval())); |
| 55 | } |
| 56 | |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 57 | return WatchdogInherits::enabled(value); |
| 58 | } |
| 59 | |
| 60 | // Get the remaining time before timer expires. |
| 61 | // If the timer is disabled, returns 0 |
| 62 | uint64_t Watchdog::timeRemaining() const |
| 63 | { |
| 64 | uint64_t timeRemain = 0; |
| 65 | |
William A. Kennington III | 1c9515f | 2018-02-27 18:54:36 -0800 | [diff] [blame] | 66 | // timer may have already expired and disabled |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 67 | if (timerEnabled()) |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 68 | { |
William A. Kennington III | 1c9515f | 2018-02-27 18:54:36 -0800 | [diff] [blame] | 69 | // the one-shot timer does not expire yet |
| 70 | auto expiry = duration_cast<milliseconds>( |
| 71 | timer.getRemaining()); |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 72 | |
William A. Kennington III | 1c9515f | 2018-02-27 18:54:36 -0800 | [diff] [blame] | 73 | // convert to msec per interface expectation. |
| 74 | auto timeNow = duration_cast<milliseconds>( |
| 75 | Timer::getCurrentTime()); |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 76 | |
William A. Kennington III | 1c9515f | 2018-02-27 18:54:36 -0800 | [diff] [blame] | 77 | // Its possible that timer may have expired by now. |
| 78 | // So need to cross verify. |
| 79 | timeRemain = (expiry > timeNow) ? |
| 80 | (expiry - timeNow).count() : 0; |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 81 | } |
| 82 | return timeRemain; |
| 83 | } |
| 84 | |
| 85 | // Reset the timer to a new expiration value |
| 86 | uint64_t Watchdog::timeRemaining(uint64_t value) |
| 87 | { |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 88 | if (!timerEnabled()) |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 89 | { |
William A. Kennington III | 5e3f877 | 2018-02-27 18:59:58 -0800 | [diff] [blame] | 90 | // We don't need to update the timer because it is off |
| 91 | return 0; |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 92 | } |
William A. Kennington III | 5e3f877 | 2018-02-27 18:59:58 -0800 | [diff] [blame] | 93 | |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 94 | if (!this->enabled()) |
| 95 | { |
| 96 | // Having a timer but not displaying an enabled value means we |
| 97 | // are inside of the fallback |
| 98 | value = fallback->interval; |
| 99 | } |
| 100 | |
William A. Kennington III | 5e3f877 | 2018-02-27 18:59:58 -0800 | [diff] [blame] | 101 | // Update new expiration |
| 102 | auto usec = duration_cast<microseconds>(milliseconds(value)); |
| 103 | timer.start(usec); |
| 104 | |
| 105 | // Update Base class data. |
| 106 | return WatchdogInherits::timeRemaining(value); |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 107 | } |
| 108 | |
Vishwanatha Subbanna | 8c5a229 | 2017-05-30 15:34:23 +0530 | [diff] [blame] | 109 | // Optional callback function on timer expiration |
| 110 | void Watchdog::timeOutHandler() |
| 111 | { |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 112 | Action action = expireAction(); |
| 113 | if (!this->enabled()) |
| 114 | { |
| 115 | action = fallback->action; |
| 116 | } |
Vishwanatha Subbanna | 3473d70 | 2017-05-30 16:38:50 +0530 | [diff] [blame] | 117 | |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 118 | auto target = actionTargets.find(action); |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 119 | if (target == actionTargets.end()) |
| 120 | { |
| 121 | log<level::INFO>("watchdog: Timed out with no target", |
| 122 | entry("ACTION=%s", convertForMessage(action).c_str())); |
William A. Kennington III | 825f498 | 2018-02-27 19:10:56 -0800 | [diff] [blame] | 123 | } |
| 124 | else |
| 125 | { |
| 126 | auto method = bus.new_method_call(SYSTEMD_SERVICE, |
| 127 | SYSTEMD_ROOT, |
| 128 | SYSTEMD_INTERFACE, |
| 129 | "StartUnit"); |
| 130 | method.append(target->second); |
| 131 | method.append("replace"); |
| 132 | |
| 133 | log<level::INFO>("watchdog: Timed out", |
| 134 | entry("ACTION=%s", convertForMessage(action).c_str()), |
| 135 | entry("TARGET=%s", target->second.c_str())); |
| 136 | bus.call_noreply(method); |
Vishwanatha Subbanna | 3473d70 | 2017-05-30 16:38:50 +0530 | [diff] [blame] | 137 | } |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 138 | |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 139 | tryFallbackOrDisable(); |
William A. Kennington III | 825f498 | 2018-02-27 19:10:56 -0800 | [diff] [blame] | 140 | } |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 141 | |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 142 | void Watchdog::tryFallbackOrDisable() |
William A. Kennington III | 825f498 | 2018-02-27 19:10:56 -0800 | [diff] [blame] | 143 | { |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 144 | // We only re-arm the watchdog if we were already enabled and have |
| 145 | // a possible fallback |
William A. Kennington III | 2235219 | 2018-02-27 18:51:44 -0800 | [diff] [blame] | 146 | if (fallback && (fallback->always || this->enabled())) |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 147 | { |
| 148 | auto interval_ms = fallback->interval; |
| 149 | auto interval_us = duration_cast<microseconds>(milliseconds(interval_ms)); |
| 150 | |
| 151 | timer.clearExpired(); |
| 152 | timer.start(interval_us); |
| 153 | timer.setEnabled<std::true_type>(); |
| 154 | |
| 155 | log<level::INFO>("watchdog: falling back", |
| 156 | entry("INTERVAL=%llu", interval_ms)); |
| 157 | } |
| 158 | else if (timerEnabled()) |
William A. Kennington III | 825f498 | 2018-02-27 19:10:56 -0800 | [diff] [blame] | 159 | { |
| 160 | timer.setEnabled<std::false_type>(); |
| 161 | |
| 162 | log<level::INFO>("watchdog: disabled"); |
| 163 | } |
| 164 | |
| 165 | // Make sure we accurately reflect our enabled state to the |
| 166 | // dbus interface. |
| 167 | WatchdogInherits::enabled(false); |
Vishwanatha Subbanna | 8c5a229 | 2017-05-30 15:34:23 +0530 | [diff] [blame] | 168 | } |
| 169 | |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 170 | } // namespace watchdog |
| 171 | } // namepsace phosphor |