-
Notifications
You must be signed in to change notification settings - Fork 841
watchdog: alert on ET_NET thread stalls beyond threshold #12524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a09bab8
Thread watchdog
moonchen 7fcbaf2
Use full path for Watchdog.h include
moonchen 7542f2b
Remove span
moonchen ec68544
Don't use jthread
moonchen 953e393
Add doc, off switch, and change config name
moonchen 7b2407e
Specify memory order, set thread name.
moonchen dd0186f
move _watchdog_thread initialization to avoid race
moonchen f4a5d46
Upgrade the assert in Monitor constructor
moonchen 1ef85ff
Add comments about watchdog.h and memory order
moonchen fb80253
Disable the watchdog by default
moonchen fa14241
update doc for default timeout
moonchen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /** @file | ||
|
|
||
| A watchdog for event loops | ||
|
|
||
| Each event thread advertises its current state through a lightweight | ||
| "heartbeat" struct: the thread publishes the timestamps for the most recent | ||
| sleep/wake pair along with a monotonically increasing sequence number. | ||
| `Watchdog::Monitor`, started from `traffic_server.cc`, runs in its own | ||
| `std::thread` and periodically scans those heartbeats; if a thread has been | ||
| awake longer than the configured timeout it emits a warning (timeout values | ||
| come from `proxy.config.exec_thread.watchdog.timeout_ms`, where 0 disables | ||
| the monitor). The monitor never touches event-system locks, keeping the | ||
| runtime overhead in the hot loop confined to a handful of atomic updates. | ||
|
|
||
| @section license License | ||
|
|
||
| Licensed to the Apache Software Foundation (ASF) under one | ||
| or more contributor license agreements. See the NOTICE file | ||
| distributed with this work for additional information | ||
| regarding copyright ownership. The ASF licenses this file | ||
| to you under the Apache License, Version 2.0 (the | ||
| "License"); you may not use this file except in compliance | ||
| with the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <atomic> | ||
| #include <chrono> | ||
| #include <vector> | ||
| #include <thread> | ||
|
|
||
| class EThread; | ||
|
|
||
| namespace Watchdog | ||
| { | ||
| struct Heartbeat { | ||
| std::atomic<std::chrono::time_point<std::chrono::steady_clock>> last_sleep{ | ||
| std::chrono::steady_clock::time_point::min()}; // set right before sleeping (e.g. before epoll_wait) | ||
| std::atomic<std::chrono::time_point<std::chrono::steady_clock>> last_wake{ | ||
| std::chrono::steady_clock::time_point::min()}; // set right after waking from sleep (e.g. epoll_wait returns) | ||
| std::atomic<uint64_t> seq{0}; // increment on each loop - used to deduplicate warnings | ||
| std::atomic<uint64_t> warned_seq{0}; // last seq we logged a warning about | ||
| }; | ||
|
|
||
| class Monitor | ||
| { | ||
| public: | ||
| explicit Monitor(EThread *threads[], size_t n_threads, std::chrono::milliseconds timeout_ms); | ||
| ~Monitor(); | ||
| Monitor() = delete; | ||
|
|
||
| private: | ||
| const std::vector<EThread *> _threads; | ||
| std::thread _watchdog_thread; | ||
| const std::chrono::milliseconds _timeout; | ||
| std::atomic<bool> _shutdown = false; | ||
| void monitor_loop() const; | ||
| }; | ||
|
|
||
| } // namespace Watchdog |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /** @file | ||
|
|
||
| A watchdog for event loops | ||
|
|
||
| @section license License | ||
|
|
||
| Licensed to the Apache Software Foundation (ASF) under one | ||
| or more contributor license agreements. See the NOTICE file | ||
| distributed with this work for additional information | ||
| regarding copyright ownership. The ASF licenses this file | ||
| to you under the Apache License, Version 2.0 (the | ||
| "License"); you may not use this file except in compliance | ||
| with the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| */ | ||
|
|
||
| #include "iocore/eventsystem/Watchdog.h" | ||
| #include "iocore/eventsystem/EThread.h" | ||
| #include "tscore/Diags.h" | ||
| #include "tscore/ink_assert.h" | ||
| #include "tscore/ink_thread.h" | ||
| #include "tsutil/DbgCtl.h" | ||
|
|
||
| #include <atomic> | ||
| #include <chrono> | ||
| #include <thread> | ||
| #include <functional> | ||
|
|
||
| namespace Watchdog | ||
| { | ||
|
|
||
| DbgCtl dbg_ctl_watchdog("watchdog"); | ||
|
|
||
| Monitor::Monitor(EThread *threads[], size_t n_threads, std::chrono::milliseconds timeout_ms) | ||
| : _threads(threads, threads + n_threads), _timeout{timeout_ms} | ||
| { | ||
| // Precondition: timeout_ms must be > 0. A timeout of 0 indicates the watchdog is disabled | ||
| // and the caller should not instantiate the Monitor (see traffic_server.cc). | ||
| ink_release_assert(timeout_ms.count() > 0); | ||
| _watchdog_thread = std::thread(std::bind_front(&Monitor::monitor_loop, this)); | ||
| } | ||
|
|
||
| Monitor::~Monitor() | ||
| { | ||
| _shutdown.store(true, std::memory_order_release); | ||
| _watchdog_thread.join(); | ||
| } | ||
|
|
||
| void | ||
| Monitor::monitor_loop() const | ||
| { | ||
| // Divide by a floating point 2 to avoid truncation to zero. | ||
| auto sleep_time = _timeout / 2.0; | ||
| ink_release_assert(sleep_time.count() > 0); | ||
| Dbg(dbg_ctl_watchdog, "Starting watchdog with timeout %" PRIu64 " ms on %zu threads. sleep_time = %" PRIu64 " us", | ||
| _timeout.count(), _threads.size(), std::chrono::duration_cast<std::chrono::microseconds>(sleep_time).count()); | ||
|
|
||
| ink_set_thread_name("[WATCHDOG]"); | ||
|
|
||
| while (!_shutdown.load(std::memory_order_acquire)) { | ||
| std::chrono::time_point<std::chrono::steady_clock> now = std::chrono::steady_clock::now(); | ||
| for (size_t i = 0; i < _threads.size(); ++i) { | ||
| EThread *t = _threads[i]; | ||
| // Relaxed load: each heartbeat field has a single writer (its EThread) so per-object coherence suffices. | ||
| std::chrono::time_point<std::chrono::steady_clock> last_sleep = t->heartbeat_state.last_sleep.load(std::memory_order_relaxed); | ||
| if (last_sleep == std::chrono::steady_clock::time_point::min()) { | ||
| // initial value sentinel - event loop hasn't started | ||
| continue; | ||
| } | ||
| // Same reasoning for relaxed load on wake timestamp. | ||
| std::chrono::time_point<std::chrono::steady_clock> last_wake = t->heartbeat_state.last_wake.load(std::memory_order_relaxed); | ||
|
|
||
| if (last_wake == std::chrono::steady_clock::time_point::min() || last_wake < last_sleep) { | ||
| // not yet woken from last sleep | ||
| continue; | ||
| } | ||
|
|
||
| auto awake_duration = now - last_wake; | ||
| if (awake_duration > _timeout) { | ||
| // Monitor thread is the sole reader (and warned_seq writer), so relaxed accesses are race-free. | ||
| uint64_t seq = t->heartbeat_state.seq.load(std::memory_order_relaxed); | ||
| uint64_t warned_seq = t->heartbeat_state.warned_seq.load(std::memory_order_relaxed); | ||
| if (warned_seq < seq) { | ||
| // Warn once per loop iteration | ||
| Warning("Watchdog: [ET_NET %zu] has been awake for %" PRIu64 " ms", i, | ||
| std::chrono::duration_cast<std::chrono::milliseconds>(awake_duration).count()); | ||
| t->heartbeat_state.warned_seq.store(seq, std::memory_order_relaxed); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| std::this_thread::sleep_for(sleep_time); | ||
| } | ||
| Dbg(dbg_ctl_watchdog, "Stopping watchdog"); | ||
| } | ||
| } // namespace Watchdog |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The watchdog is created immediately after
eventProcessor.start()is called (line 2132), but the ET_NET threads may not have fully initialized their heartbeat state yet. Sinceheartbeat_statemembers are initialized to sentinel values (time_point::min()andseq{0}), the watchdog should either wait for threads to be ready or the initialization order should be documented to prevent potential timing issues during startup.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The sentinel values are chosen this way to tolerate delays in the startup of ET_NET threads. See this code in watchdog.cc: