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