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