Skip to content
7 changes: 7 additions & 0 deletions doc/admin-guide/files/records.yaml.en.rst
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,13 @@ Thread Variables

This option only has an affect when |TS| has been compiled with ``--enable-hwloc``.

.. ts:cv:: CONFIG proxy.config.exec_thread.watchdog.timeout_ms INT 1000
:units: milliseconds

Set the timeout for the exec thread watchdog in milliseconds. If an exec thread
does not heartbeat within this time period, the watchdog will log a warning message.
If this value is zero, the watchdog is disabled.

.. ts:cv:: CONFIG proxy.config.system.file_max_pct FLOAT 0.9
Set the maximum number of file handles for the traffic_server process as a percentage of the fs.file-max proc value in Linux. The default is 90%.
Expand Down
3 changes: 3 additions & 0 deletions include/iocore/eventsystem/EThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "iocore/eventsystem/PriorityEventQueue.h"
#include "iocore/eventsystem/ProtectedQueue.h"
#include "tsutil/Histogram.h"
#include "iocore/eventsystem/Watchdog.h"

#if TS_USE_HWLOC
struct hwloc_obj;
Expand Down Expand Up @@ -584,6 +585,8 @@ class EThread : public Thread

Metrics metrics;

Watchdog::Heartbeat heartbeat_state;

private:
void cons_common();
};
Expand Down
60 changes: 60 additions & 0 deletions include/iocore/eventsystem/Watchdog.h
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
1 change: 1 addition & 0 deletions src/iocore/eventsystem/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ add_library(
ConfigProcessor.cc
RecRawStatsImpl.cc
RecProcess.cc
Watchdog.cc
)
add_library(ts::inkevent ALIAS inkevent)

Expand Down
7 changes: 7 additions & 0 deletions src/iocore/eventsystem/UnixEThread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,15 @@ EThread::execute_regular()
ink_hrtime post_drain = ink_get_hrtime();
ink_hrtime drain_queue = post_drain - loop_start_time;

// watchdog kick - pre-sleep
this->heartbeat_state.last_sleep.store(std::chrono::steady_clock::now(), std::memory_order_relaxed);

tail_cb->waitForActivity(sleep_time);

// watchdog kick - post-wake
this->heartbeat_state.last_wake.store(std::chrono::steady_clock::now(), std::memory_order_relaxed);
this->heartbeat_state.seq.fetch_add(1, std::memory_order_relaxed);

// loop cleanup
loop_finish_time = ink_get_hrtime();
// @a delta can be negative due to time of day adjustments (which apparently happen quite frequently). I
Expand Down
100 changes: 100 additions & 0 deletions src/iocore/eventsystem/Watchdog.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/** @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), _watchdog_thread{std::bind_front(&Monitor::monitor_loop, this)}, _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_assert(timeout_ms.count() > 0);
}

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];
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
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
7 changes: 6 additions & 1 deletion src/records/RecordsConfig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1529,7 +1529,12 @@ static constexpr RecordElement RecordsConfig[] =
{RECT_CONFIG, "proxy.config.io_uring.wq_workers_unbounded", RECD_INT, "0", RECU_NULL, RR_NULL, RECC_NULL, nullptr, RECA_NULL},
{RECT_CONFIG, "proxy.config.aio.mode", RECD_STRING, "auto", RECU_DYNAMIC, RR_NULL, RECC_STR, "(auto|io_uring|thread)", RECA_NULL},
#endif

//###########
//#
//# Thread watchdog
//#
//###########
{RECT_CONFIG, "proxy.config.exec_thread.watchdog.timeout_ms", RECD_INT, "1000", RECU_RESTART_TS, RR_NULL, RECC_INT, "[0-10000]", RECA_NULL}
};
// clang-format on

Expand Down
14 changes: 14 additions & 0 deletions src/traffic_server/traffic_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "iocore/aio/AIO.h"
#include "iocore/cache/Store.h"
#include "iocore/eventsystem/Watchdog.h"
#include "tscore/TSSystemState.h"
#include "tscore/Version.h"
#include "tscore/ink_platform.h"
Expand Down Expand Up @@ -213,6 +214,8 @@ int cmd_block = 0;
// -1: cache is already initialized, don't delay.
int delay_listen_for_cache = 0;

std::unique_ptr<Watchdog::Monitor> watchdog = nullptr;

ArgumentDescription argument_descriptions[] = {
{"net_threads", 'n', "Number of Net Threads", "I", &num_of_net_threads, "PROXY_NET_THREADS", nullptr },
{"udp_threads", 'U', "Number of UDP Threads", "I", &num_of_udp_threads, "PROXY_UDP_THREADS", nullptr },
Expand Down Expand Up @@ -266,6 +269,9 @@ struct AutoStopCont : public Continuation {
int
mainEvent(int /* event */, Event * /* e */)
{
// Stop the watchdog before shutting threads down
watchdog.reset();

TSSystemState::stop_ssl_handshaking();

APIHook *hook = g_lifecycle_hooks->get(TS_LIFECYCLE_SHUTDOWN_HOOK);
Expand Down Expand Up @@ -2131,6 +2137,14 @@ main(int /* argc ATS_UNUSED */, const char **argv)
RecRegisterConfigUpdateCb("proxy.config.dump_mem_info_frequency", init_memory_tracker, nullptr);
init_memory_tracker(nullptr, RECD_NULL, RecData(), nullptr);

// Start the watchdog
int watchdog_timeout_ms = RecGetRecordInt("proxy.config.exec_thread.watchdog.timeout_ms").value_or(1000);
if (watchdog_timeout_ms > 0) {
watchdog = std::make_unique<Watchdog::Monitor>(eventProcessor.thread_group[ET_NET]._thread,
static_cast<size_t>(eventProcessor.thread_group[ET_NET]._count),
std::chrono::milliseconds{watchdog_timeout_ms});
Comment on lines +2143 to +2145
Copy link

Copilot AI Nov 7, 2025

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. Since heartbeat_state members are initialized to sentinel values (time_point::min() and seq{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.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

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:

if (last_sleep == std::chrono::steady_clock::time_point::min()) {
        // initial value sentinel - event loop hasn't started
        continue;
      }

}

{
auto s{RecGetRecordStringAlloc("proxy.config.diags.debug.client_ip")};
if (auto p{ats_as_c_str(s)}; p) {
Expand Down