-
Notifications
You must be signed in to change notification settings - Fork 842
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
Open
moonchen
wants to merge
6
commits into
apache:master
Choose a base branch
from
moonchen:watchdog
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /** @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. | ||
|
|
||
| */ | ||
|
|
||
| #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,95 @@ | ||
| /** @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 "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), _watchdog_thread{std::bind_front(&Monitor::monitor_loop, this)}, _timeout{timeout_ms} | ||
| { | ||
| ink_assert(timeout_ms.count() > 0); | ||
| } | ||
|
|
||
| Monitor::~Monitor() | ||
| { | ||
| _shutdown.store(true); | ||
| _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()); | ||
|
|
||
| while (!_shutdown.load()) { | ||
| 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]; | ||
| 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; | ||
| } | ||
| 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) { | ||
| 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 | ||
| Error("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.
Should we make this optional, as in, if proxy.config.thread_watchdog == 0, we don't setup the watchdog ?
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.
Done.