Skip to content

Commit cf92526

Browse files
authored
chore: add ability to identify fibers that run too long (#414)
Added fiber_run_warning_threshold_ms flag plus a method that can change the threshold at runtime. Signed-off-by: Roman Gershman <[email protected]>
1 parent e5d8057 commit cf92526

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

util/fibers/detail/fiber_interface.cc

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ ABSL_FLAG(uint32_t, fiber_safety_margin, 1024,
1616
"If > 0, ensures the stack each fiber has at least this margin. "
1717
"The check is done at fiber destruction time.");
1818

19+
ABSL_FLAG(uint32_t, fiber_run_warning_threshold_ms, 0,
20+
"If greater than 0, will warn if a fiber runs longer than this threshold. ");
21+
1922
namespace util {
2023
namespace fb2 {
2124
using namespace std;
@@ -61,14 +64,13 @@ class MainFiberImpl final : public FiberInterface {
6164
mutex g_scheduler_lock;
6265

6366
TL_FiberInitializer* g_fiber_thread_list = nullptr;
64-
uint64_t g_tsc_cycles_per_ms = 0;
67+
uint64_t g_tsc_cycles_per_ms = 0, g_ts_cycles_warn_threshold = -1;
6568

6669
} // namespace
6770

6871
PMR_NS::memory_resource* default_stack_resource = nullptr;
6972
size_t default_stack_size = 64 * 1024;
7073

71-
7274
__thread FiberInterface::TL FiberInterface::tl;
7375

7476
// Per thread initialization structure.
@@ -111,6 +113,9 @@ TL_FiberInitializer::TL_FiberInitializer() noexcept : sched(nullptr) {
111113
g_fiber_thread_list = this;
112114
if (g_tsc_cycles_per_ms == 0) {
113115
g_tsc_cycles_per_ms = CycleClock::Frequency() / 1000;
116+
uint32_t warn_ms = absl::GetFlag(FLAGS_fiber_run_warning_threshold_ms);
117+
if (warn_ms)
118+
g_ts_cycles_warn_threshold = g_tsc_cycles_per_ms * warn_ms;
114119
VLOG(1) << "TSC Frequency : " << g_tsc_cycles_per_ms << "/ms";
115120
}
116121
}
@@ -401,12 +406,16 @@ FiberInterface* FiberInterface::SwitchSetup() {
401406
fb_initializer.switch_delay_cycles += (tsc - cpu_tsc_);
402407

403408
// to_suspend points to the fiber that is active and is going to be suspended.
404-
uint64_t delta_cycles = tsc - to_suspend->cpu_tsc_;
405-
if (delta_cycles > g_tsc_cycles_per_ms) {
409+
uint64_t active_cycles = tsc - to_suspend->cpu_tsc_;
410+
if (active_cycles > g_tsc_cycles_per_ms) {
406411
fb_initializer.long_runtime_cnt++;
407412

408-
// improves precision, instead of "delta_cycles / (g_tsc_cycles_per_ms / 1000)"
409-
fb_initializer.long_runtime_usec += (delta_cycles * 1000) / g_tsc_cycles_per_ms;
413+
// improves precision, instead of "active_cycles / (g_tsc_cycles_per_ms / 1000)"
414+
fb_initializer.long_runtime_usec += (active_cycles * 1000) / g_tsc_cycles_per_ms;
415+
if (active_cycles >= g_ts_cycles_warn_threshold && to_suspend->type() == WORKER) {
416+
LOG_EVERY_T(WARNING, 1) << "Fiber " << to_suspend->name() << " ran for "
417+
<< (active_cycles / g_tsc_cycles_per_ms) << " ms";
418+
}
410419
}
411420
}
412421

@@ -473,6 +482,10 @@ uint64_t FiberLongRunSumUsec() noexcept {
473482
return detail::FbInitializer().long_runtime_usec;
474483
}
475484

485+
void SetFiberLongRunWarningThreshold(uint32_t warn_ms) {
486+
detail::g_ts_cycles_warn_threshold = warn_ms == 0 ? -1 : detail::g_tsc_cycles_per_ms * warn_ms;
487+
}
488+
476489
size_t WorkerFibersStackSize() {
477490
return detail::FbInitializer().sched->worker_stack_size();
478491
}

util/fibers/fibers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ uint64_t FiberLongRunCnt() noexcept;
131131
// Exposes total duration of fibers running for a "long" time (longer than 1ms).
132132
uint64_t FiberLongRunSumUsec() noexcept;
133133

134+
void SetFiberLongRunWarningThreshold(uint32_t warn_ms);
135+
134136
// Injects a custom memory resource for stack allocation. Can be called only once.
135137
// It is advised to call this function when a program starts.
136138
void SetDefaultStackResource(PMR_NS::memory_resource* mr, size_t default_size = 64 * 1024);

0 commit comments

Comments
 (0)