-
Notifications
You must be signed in to change notification settings - Fork 16
kqueue adaptive pump #151
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
kqueue adaptive pump #151
Changes from all commits
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -173,6 +173,18 @@ class BOOST_COROSIO_DECL kqueue_scheduler final | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Called at the start of each posted completion handler to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| grant a fresh budget for speculative inline completions. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Operates in two modes depending on whether another thread | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| absorbed queued work from the previous dispatch cycle: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - **Adaptive** (default): the effective cap ramps up when | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| the previous cycle fully consumed its budget (doubles up | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| to 16) and ramps down to the floor (2) when budget was | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| only partially consumed, tracking actual inline demand. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - **Unassisted**: entered when no other thread was available | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| to signal (unlock_and_signal_one returned false). Applies | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| a fixed conservative cap (4) to amortize scheduling | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| overhead for small buffers while avoiding bursty I/O that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fills socket buffers and stalls large transfers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void reset_inline_budget() const noexcept; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -308,8 +320,11 @@ class BOOST_COROSIO_DECL kqueue_scheduler final | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Mutex must be held. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @param lock The held mutex lock. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @return `true` if at least one waiter was signaled, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| `false` if no waiters existed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void unlock_and_signal_one(std::unique_lock<std::mutex>& lock) const; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bool unlock_and_signal_one(std::unique_lock<std::mutex>& lock) const; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** Clear the signaled state before waiting. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -343,7 +358,6 @@ class BOOST_COROSIO_DECL kqueue_scheduler final | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::unique_lock<std::mutex>& lock, long timeout_us) const; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int kq_fd_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int max_inline_budget_ = 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mutable std::mutex mutex_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mutable std::condition_variable cond_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mutable op_queue completed_ops_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -391,12 +405,16 @@ struct BOOST_COROSIO_SYMBOL_VISIBLE scheduler_context | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| op_queue private_queue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::int64_t private_outstanding_work; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int inline_budget; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int inline_budget_max; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bool unassisted; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| scheduler_context(kqueue_scheduler const* k, scheduler_context* n) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : key(k) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| , next(n) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| , private_outstanding_work(0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| , inline_budget(0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| , inline_budget_max(2) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| , unassisted(false) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -468,7 +486,24 @@ inline void | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| kqueue_scheduler::reset_inline_budget() const noexcept | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (auto* ctx = kqueue::find_context(this)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ctx->inline_budget = max_inline_budget_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Cap when no other thread absorbed queued work. A moderate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // cap (4) amortizes scheduling for small buffers while avoiding | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // bursty I/O that fills socket buffers and stalls large transfers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (ctx->unassisted) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ctx->inline_budget_max = 4; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ctx->inline_budget = 4; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Ramp up when previous cycle fully consumed budget. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Reset on partial consumption (EAGAIN hit or peer got scheduled). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (ctx->inline_budget == 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ctx->inline_budget_max = (std::min)(ctx->inline_budget_max * 2, 16); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else if (ctx->inline_budget < ctx->inline_budget_max) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ctx->inline_budget_max = 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ctx->inline_budget = ctx->inline_budget_max; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| inline bool | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1073,7 +1108,7 @@ kqueue_scheduler::maybe_unlock_and_signal_one( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| inline void | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| inline bool | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| kqueue_scheduler::unlock_and_signal_one( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| std::unique_lock<std::mutex>& lock) const | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1082,6 +1117,7 @@ kqueue_scheduler::unlock_and_signal_one( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lock.unlock(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (have_waiters) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cond_.notify_one(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return have_waiters; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| inline void | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1378,10 +1414,15 @@ kqueue_scheduler::do_one( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Handle operation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (op != nullptr) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!completed_ops_.empty()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| unlock_and_signal_one(lock); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bool more = !completed_ops_.empty(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (more) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ctx->unassisted = !unlock_and_signal_one(lock); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ctx->unassisted = false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lock.unlock(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
1414
to
+1425
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. Potential null dereference of
🛡️ Suggested defensive guard bool more = !completed_ops_.empty();
if (more)
- ctx->unassisted = !unlock_and_signal_one(lock);
+ {
+ bool signaled = unlock_and_signal_one(lock);
+ if (ctx)
+ ctx->unassisted = !signaled;
+ }
else
{
- ctx->unassisted = false;
+ if (ctx)
+ ctx->unassisted = false;
lock.unlock();
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| work_cleanup on_exit{this, &lock, ctx}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (void)on_exit; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.