|
| 1 | +/** @file |
| 2 | +
|
| 3 | + A watchdog for event loops |
| 4 | +
|
| 5 | + @section license License |
| 6 | +
|
| 7 | + Licensed to the Apache Software Foundation (ASF) under one |
| 8 | + or more contributor license agreements. See the NOTICE file |
| 9 | + distributed with this work for additional information |
| 10 | + regarding copyright ownership. The ASF licenses this file |
| 11 | + to you under the Apache License, Version 2.0 (the |
| 12 | + "License"); you may not use this file except in compliance |
| 13 | + with the License. You may obtain a copy of the License at |
| 14 | +
|
| 15 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | +
|
| 17 | + Unless required by applicable law or agreed to in writing, software |
| 18 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | + See the License for the specific language governing permissions and |
| 21 | + limitations under the License. |
| 22 | +
|
| 23 | + */ |
| 24 | + |
| 25 | +#include "iocore/eventsystem/Watchdog.h" |
| 26 | +#include "iocore/eventsystem/EThread.h" |
| 27 | +#include "tscore/Diags.h" |
| 28 | +#include "tscore/ink_assert.h" |
| 29 | +#include "tscore/ink_thread.h" |
| 30 | +#include "tsutil/DbgCtl.h" |
| 31 | + |
| 32 | +#include <atomic> |
| 33 | +#include <chrono> |
| 34 | +#include <thread> |
| 35 | +#include <functional> |
| 36 | + |
| 37 | +namespace Watchdog |
| 38 | +{ |
| 39 | + |
| 40 | +DbgCtl dbg_ctl_watchdog("watchdog"); |
| 41 | + |
| 42 | +Monitor::Monitor(EThread *threads[], size_t n_threads, std::chrono::milliseconds timeout_ms) |
| 43 | + : _threads(threads, threads + n_threads), _timeout{timeout_ms} |
| 44 | +{ |
| 45 | + // Precondition: timeout_ms must be > 0. A timeout of 0 indicates the watchdog is disabled |
| 46 | + // and the caller should not instantiate the Monitor (see traffic_server.cc). |
| 47 | + ink_release_assert(timeout_ms.count() > 0); |
| 48 | + _watchdog_thread = std::thread(std::bind_front(&Monitor::monitor_loop, this)); |
| 49 | +} |
| 50 | + |
| 51 | +Monitor::~Monitor() |
| 52 | +{ |
| 53 | + _shutdown.store(true, std::memory_order_release); |
| 54 | + _watchdog_thread.join(); |
| 55 | +} |
| 56 | + |
| 57 | +void |
| 58 | +Monitor::monitor_loop() const |
| 59 | +{ |
| 60 | + // Divide by a floating point 2 to avoid truncation to zero. |
| 61 | + auto sleep_time = _timeout / 2.0; |
| 62 | + ink_release_assert(sleep_time.count() > 0); |
| 63 | + Dbg(dbg_ctl_watchdog, "Starting watchdog with timeout %" PRIu64 " ms on %zu threads. sleep_time = %" PRIu64 " us", |
| 64 | + _timeout.count(), _threads.size(), std::chrono::duration_cast<std::chrono::microseconds>(sleep_time).count()); |
| 65 | + |
| 66 | + ink_set_thread_name("[WATCHDOG]"); |
| 67 | + |
| 68 | + while (!_shutdown.load(std::memory_order_acquire)) { |
| 69 | + std::chrono::time_point<std::chrono::steady_clock> now = std::chrono::steady_clock::now(); |
| 70 | + for (size_t i = 0; i < _threads.size(); ++i) { |
| 71 | + EThread *t = _threads[i]; |
| 72 | + // Relaxed load: each heartbeat field has a single writer (its EThread) so per-object coherence suffices. |
| 73 | + std::chrono::time_point<std::chrono::steady_clock> last_sleep = t->heartbeat_state.last_sleep.load(std::memory_order_relaxed); |
| 74 | + if (last_sleep == std::chrono::steady_clock::time_point::min()) { |
| 75 | + // initial value sentinel - event loop hasn't started |
| 76 | + continue; |
| 77 | + } |
| 78 | + // Same reasoning for relaxed load on wake timestamp. |
| 79 | + std::chrono::time_point<std::chrono::steady_clock> last_wake = t->heartbeat_state.last_wake.load(std::memory_order_relaxed); |
| 80 | + |
| 81 | + if (last_wake == std::chrono::steady_clock::time_point::min() || last_wake < last_sleep) { |
| 82 | + // not yet woken from last sleep |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + auto awake_duration = now - last_wake; |
| 87 | + if (awake_duration > _timeout) { |
| 88 | + // Monitor thread is the sole reader (and warned_seq writer), so relaxed accesses are race-free. |
| 89 | + uint64_t seq = t->heartbeat_state.seq.load(std::memory_order_relaxed); |
| 90 | + uint64_t warned_seq = t->heartbeat_state.warned_seq.load(std::memory_order_relaxed); |
| 91 | + if (warned_seq < seq) { |
| 92 | + // Warn once per loop iteration |
| 93 | + Warning("Watchdog: [ET_NET %zu] has been awake for %" PRIu64 " ms", i, |
| 94 | + std::chrono::duration_cast<std::chrono::milliseconds>(awake_duration).count()); |
| 95 | + t->heartbeat_state.warned_seq.store(seq, std::memory_order_relaxed); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + std::this_thread::sleep_for(sleep_time); |
| 101 | + } |
| 102 | + Dbg(dbg_ctl_watchdog, "Stopping watchdog"); |
| 103 | +} |
| 104 | +} // namespace Watchdog |
0 commit comments