-
Notifications
You must be signed in to change notification settings - Fork 16
Worker Pool 防止饿死 #51
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
Worker Pool 防止饿死 #51
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -279,6 +279,8 @@ class UTIL_SYMBOL_LOCAL worker_pool_module::worker : public std::enable_shared_f | |
| worker_tick_action_container_type tick_handles_; | ||
| std::recursive_mutex tick_handle_lock_; | ||
| std::atomic<std::chrono::microseconds::rep> current_tick_interval_us_; | ||
| std::atomic<std::chrono::microseconds::rep> current_tick_second_busy_us_; | ||
| std::atomic<std::chrono::microseconds::rep> current_tick_second_waited_us_; | ||
|
yousongyang marked this conversation as resolved.
Comment on lines
+282
to
+283
|
||
|
|
||
| std::atomic<std::chrono::microseconds::rep> cpu_time_busy_us_; | ||
| std::atomic<std::chrono::microseconds::rep> cpu_time_sleep_us_; | ||
|
|
@@ -299,6 +301,7 @@ struct UTIL_SYMBOL_LOCAL worker_pool_module::worker_set { | |
|
|
||
| std::atomic<int64_t> configure_tick_min_interval_microseconds; | ||
| std::atomic<int64_t> configure_tick_max_interval_microseconds; | ||
| std::atomic<int64_t> configure_tick_preserve_microseconds_in_second; | ||
|
|
||
| std::recursive_mutex worker_lock; | ||
| std::vector<std::shared_ptr<worker>> workers; | ||
|
|
@@ -453,6 +456,9 @@ void worker_pool_module::worker::start(const std::shared_ptr<worker>& self, cons | |
| } | ||
|
|
||
| // Maybe sleep until timeout or next event | ||
|
Comment on lines
458
to
461
|
||
| self->current_tick_second_busy_us_.fetch_add( | ||
| std::chrono::duration_cast<std::chrono::microseconds>(busy_end_time - start_time).count(), | ||
| std::memory_order_release); | ||
| if (busy_end_time - start_time < tick_interval) { | ||
| std::unique_lock<std::mutex> lk_cv(self->waker_lock_); | ||
|
|
||
|
|
@@ -465,6 +471,33 @@ void worker_pool_module::worker::start(const std::shared_ptr<worker>& self, cons | |
| auto sleep_rep = | ||
| std::chrono::duration_cast<std::chrono::microseconds>(sleep_end_time - busy_end_time).count(); | ||
| self->cpu_time_sleep_us_.fetch_add(sleep_rep, std::memory_order_release); | ||
| self->current_tick_second_waited_us_.fetch_add(sleep_rep, std::memory_order_release); | ||
| } | ||
| } | ||
|
|
||
|
Comment on lines
+478
to
+480
|
||
| if (self->current_tick_second_busy_us_.load(std::memory_order_acquire) + | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MEDIUM Location: Referenced code ( }
if (self->current_tick_second_busy_us_.load(std::memory_order_acquire) +
self->current_tick_second_waited_us_.load(std::memory_order_acquire) >=
1000000) {
self->current_tick_second_busy_us_.store(0, std::memory_order_release);
self->current_tick_second_waited_us_.store(0, std::memory_order_release);
auto wait_preserve_us =
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LOW Location: Referenced code ( }
if (self->current_tick_second_busy_us_.load(std::memory_order_acquire) +
self->current_tick_second_waited_us_.load(std::memory_order_acquire) >=
1000000) {Suggested fix:
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MEDIUM Location: Referenced code ( }
if (self->current_tick_second_busy_us_.load(std::memory_order_acquire) +
self->current_tick_second_waited_us_.load(std::memory_order_acquire) >=
1000000) {Suggested fix: |
||
| self->current_tick_second_waited_us_.load(std::memory_order_acquire) >= | ||
| 1000000) { | ||
| self->current_tick_second_busy_us_.store(0, std::memory_order_release); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LOW Location: Referenced code ( self->current_tick_second_waited_us_.load(std::memory_order_acquire) >=
1000000) {
self->current_tick_second_busy_us_.store(0, std::memory_order_release);
self->current_tick_second_waited_us_.store(0, std::memory_order_release);
auto wait_preserve_us =Suggested fix: |
||
| self->current_tick_second_waited_us_.store(0, std::memory_order_release); | ||
|
|
||
| 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::chrono::system_clock::time_point preserve_sleep_start = std::chrono::system_clock::now(); | ||
| 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); | ||
|
|
||
| std::chrono::system_clock::time_point sleep_end_time = std::chrono::system_clock::now(); | ||
| if (sleep_end_time > preserve_sleep_start) { | ||
| auto sleep_rep = | ||
| std::chrono::duration_cast<std::chrono::microseconds>(sleep_end_time - preserve_sleep_start).count(); | ||
| self->cpu_time_sleep_us_.fetch_add(sleep_rep, std::memory_order_release); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MEDIUM 触发场景:当 worker 的 busy + waited 时间累计超过 1 秒后触发 preserve sleep 时,该 sleep 时长不会被计入下一周期的 waited 累计值。 建议修复:在第 500 行的 Location: Referenced code ( auto sleep_rep =
std::chrono::duration_cast<std::chrono::microseconds>(sleep_end_time - preserve_sleep_start).count();
self->cpu_time_sleep_us_.fetch_add(sleep_rep, std::memory_order_release);
}
} |
||
| } | ||
| } | ||
| } | ||
|
|
@@ -578,6 +611,7 @@ worker_pool_module::worker_set::worker_set() | |
| current_expect_workers.store(2, std::memory_order_release); | ||
| configure_tick_min_interval_microseconds.store(4, std::memory_order_release); | ||
| configure_tick_max_interval_microseconds.store(128, std::memory_order_release); | ||
| configure_tick_preserve_microseconds_in_second.store(8000, std::memory_order_release); | ||
| } | ||
|
|
||
| LIBATAPP_MACRO_API worker_pool_module::worker_pool_module() | ||
|
|
||
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.
MEDIUM
·
correctness
新增的原子成员变量
current_tick_second_busy_us_和current_tick_second_waited_us_未在 diff 中显示初始化。std::atomic的默认构造函数对于整型类型会保留未初始化的值。如果worker类的构造函数没有显式初始化这些变量,它们将持有不确定的值,导致首次使用时的行为未定义。建议在构造函数中添加初始化,例如:
或使用成员初始化列表/默认成员初始化器。
Location:
src/atframe/modules/worker_pool_module.cpp:282-283Referenced code (
src/atframe/modules/worker_pool_module.cpp:282-283):