Skip to content

Worker Pool 防止饿死#51

Open
yousongyang wants to merge 2 commits into
owent:mainfrom
yousongyang:main
Open

Worker Pool 防止饿死#51
yousongyang wants to merge 2 commits into
owent:mainfrom
yousongyang:main

Conversation

@yousongyang
Copy link
Copy Markdown
Contributor

No description provided.

Copilot AI review requested due to automatic review settings May 19, 2026 03:23
Copy link
Copy Markdown
Owner

@owent owent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AICR problem for src/atframe/modules/worker_pool_module.cpp:282

Comment thread src/atframe/modules/worker_pool_module.cpp Outdated
@owent
Copy link
Copy Markdown
Owner

owent commented May 19, 2026

AI Code Review

Updated: 2026-05-20 01:42:30 UTC | Commit: 7423828

AI Code Review Summary

Worker 睡眠时间统计问题

Target: Worker Pool 防止饿死
Author: @yousongyang
Branch: main
Reviewers: @yousongyang


审查范围

审查 src/atframe/modules/worker_pool_module.cpp 中新增的每秒忙碌/等待时间统计功能及保护性睡眠机制。

发现问题

1. cpu_time_sleep_us_ 重复计算睡眠时间 (medium)

位置: Line 500

当常规睡眠和保护性睡眠在同一迭代中执行时,busy_end_time 变量未在保护性睡眠前更新,导致 line 500 计算的睡眠时长包含了已被 line 474 记录的常规睡眠时长,造成重复计算。

2. 保护性睡眠时间未记录到每秒统计 (low)

位置: Line 501 附近

每秒计数器 current_tick_second_waited_us_ 在 lines 482-483 被重置后,保护性睡眠的时长未被添加到该计数器,导致新一秒的等待时间统计不完整。

建议

在保护性睡眠前捕获当前时间戳,并基于该时间戳计算保护性睡眠时长,确保睡眠时间只被计算一次且被正确记录到相应的统计计数器中。

Problems (2)

# Severity Category Location Message
0 MEDIUM correctness src/atframe/modules/worker_pool_module.cpp:500 cpu_time_sleep_us_ 会重复计算睡眠时间。当常规睡眠(line 467)和保护性睡眠(line 493)在同一迭代中执行时,busy_end_time 始终是第一次工作结束的时间点。Line 474 已将第一次睡眠时长加到 cpu_time_sleep_us_,而 line 500 计算的 sleep_rep = sleep_end_time - busy_end_time 包含了第一次睡眠时长和保护性睡眠时长,导致第一次睡眠被重复计算。

触发场景:Worker 在一次迭代中,tick 工作时间短于 tick_interval(触发常规睡眠),且累计的 busy+waited 时间达到 1 秒(触发保护性睡眠)。

建议修复:在保护性睡眠前捕获当前时间(如 preserve_sleep_start = std::chrono::system_clock::now()),在 line 500 使用 sleep_end_time - preserve_sleep_start 计算实际保护性睡眠时长。
|
| 1 | LOW | correctness | src/atframe/modules/worker_pool_module.cpp:501 | 保护性睡眠的时间未记录到 current_tick_second_waited_us_。Lines 482-483 将每秒计数器重置为 0 后,保护性睡眠的时长只被添加到 cpu_time_sleep_us_(line 500),但 current_tick_second_waited_us_ 未被更新,导致新的一秒内的等待统计缺失这段睡眠时间。 |


  • Powered by AICodeReviewer*

Open Issues (2)

  1. [MEDIUM] correctnesssrc/atframe/modules/worker_pool_module.cpp:500 (new in 7423828)
  2. [LOW] correctnesssrc/atframe/modules/worker_pool_module.cpp:501 (new in 7423828)
Resolved (1)

The following previously reported issues are no longer present:

  • 415dc25b23d3b529 ✅ Resolved

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds worker-pool throttling intended to prevent starvation by tracking per-worker busy/wait time and inserting a preserved sleep window once accumulated tick time reaches one second.

Changes:

  • Adds per-worker counters for current-second busy and waited microseconds.
  • Adds a worker-set preserve interval defaulting to 8000µs.
  • Inserts an extra condition-variable wait after each accumulated second of worker activity.
Comments suppressed due to low confidence (1)

src/atframe/modules/worker_pool_module.cpp:481

  • This added line exceeds the repository's .clang-format ColumnLimit: 120; please wrap it or run clang-format to keep formatting consistent.
        auto wait_preserve_us = self->get_owner().configure_tick_preserve_microseconds_in_second.load(std::memory_order_acquire);

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/atframe/modules/worker_pool_module.cpp Outdated
Comment on lines +481 to +491
auto wait_preserve_us = self->get_owner().configure_tick_preserve_microseconds_in_second.load(std::memory_order_acquire);
if (wait_preserve_us <= 0) {
wait_preserve_us = 8000;
}

std::unique_lock<std::mutex> lk_cv(self->waker_lock_);
self->status_.store(static_cast<uint8_t>(worker_status::kSleeping), std::memory_order_release);
self->waker_cv_.wait_for(lk_cv, std::chrono::microseconds(wait_preserve_us));
self->status_.store(static_cast<uint8_t>(worker_status::kRunning), std::memory_order_release);

self->cpu_time_sleep_us_.fetch_add(wait_preserve_us, std::memory_order_release);
Comment thread src/atframe/modules/worker_pool_module.cpp Outdated
Comment thread src/atframe/modules/worker_pool_module.cpp Outdated
Copy link
Copy Markdown
Owner

@owent owent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AICR problem for src/atframe/modules/worker_pool_module.cpp:491

Comment thread src/atframe/modules/worker_pool_module.cpp Outdated
Copy link
Copy Markdown
Owner

@owent owent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AICR problem for src/atframe/modules/worker_pool_module.cpp:463

Comment thread src/atframe/modules/worker_pool_module.cpp Outdated
Copy link
Copy Markdown
Owner

@owent owent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MEDIUM
·
correctness

cpu_time_sleep_us_ 会重复计算睡眠时间。当常规睡眠(line 467)和保护性睡眠(line 493)在同一迭代中执行时,busy_end_time 始终是第一次工作结束的时间点。Line 474 已将第一次睡眠时长加到 cpu_time_sleep_us_,而 line 500 计算的 sleep_rep = sleep_end_time - busy_end_time 包含了第一次睡眠时长和保护性睡眠时长,导致第一次睡眠被重复计算。

触发场景:Worker 在一次迭代中,tick 工作时间短于 tick_interval(触发常规睡眠),且累计的 busy+waited 时间达到 1 秒(触发保护性睡眠)。

建议修复:在保护性睡眠前捕获当前时间(如 preserve_sleep_start = std::chrono::system_clock::now()),在 line 500 使用 sleep_end_time - preserve_sleep_start 计算实际保护性睡眠时长。

Location: src/atframe/modules/worker_pool_module.cpp:500

Copy link
Copy Markdown
Owner

@owent owent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOW
·
correctness

保护性睡眠的时间未记录到 current_tick_second_waited_us_。Lines 482-483 将每秒计数器重置为 0 后,保护性睡眠的时长只被添加到 cpu_time_sleep_us_(line 500),但 current_tick_second_waited_us_ 未被更新,导致新的一秒内的等待统计缺失这段睡眠时间。

Location: src/atframe/modules/worker_pool_module.cpp:501

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants