-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
fix: changed the calculating logic of epoll timeout provided by TimerTaskManager #2794
fix: changed the calculating logic of epoll timeout provided by TimerTaskManager #2794
Conversation
WalkthroughThe Changes
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configuration File (
|
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.
Actionable comments posted: 1
Outside diff range, codebase verification and nitpick comments (2)
src/net/src/net_util.h (1)
62-62
: Inconsistent return type forExecTimerTask
function.The return type of the
ExecTimerTask
function has been changed toint32_t
in the implementation file (src/net/src/net_util.cc
) but remains asint
in the header file (src/net/src/net_util.h
). Please update the header file to maintain consistency.
src/net/src/net_util.cc: line 1
src/net/src/net_util.h: line 62
Analysis chain
Verify the impact of changing the return type to
int32_t
.The return type of
ExecTimerTask
has been changed fromint
toint32_t
. Ensure that the rest of the codebase correctly handles this new return type.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify all function calls to `ExecTimerTask` handle the new return type `int32_t`. # Test: Search for the function usage. Expect: Correct handling of the new return type. rg --type cpp -A 5 $'ExecTimerTask'Length of output: 1002
src/net/src/dispatch_thread.cc (1)
69-69
: Typo in thread name.The thread name "DispacherTimerTaskThread" in the provided code snippet appears to be a typo. The expected name "DispatcherTimerTaskThread" is not used elsewhere in the codebase.
- src/net/src/dispatch_thread.cc:
timer_task_thread_.set_thread_name("DispacherTimerTaskThread");
Analysis chain
Ensure consistency in thread naming.
The thread name has been changed to
"DispatcherTimerTaskThread"
. Ensure that this new name is used consistently throughout the codebase.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify consistent use of the new thread name. # Test: Search for the thread name usage. Expect: Consistent use of `"DispatcherTimerTaskThread"`. rg --type cpp -A 5 $'DispatcherTimerTaskThread'Length of output: 46
Script:
#!/bin/bash # Description: Verify consistent use of the new thread name. # Test: Search for the thread name usage in all file types. Expect: Consistent use of "DispatcherTimerTaskThread". rg -i 'Dispatcher\s*Timer\s*Task\s*Thread'Length of output: 44
Script:
#!/bin/bash # Description: Verify consistent use of the new thread name. # Test: Search for variations of the thread name usage in all file types. Expect: Consistent use of "DispatcherTimerTaskThread". rg -i 'Dispacher\s*Timer\s*Task\s*Thread|Dispatcher\s*Timer\s*Task\s*Thread'Length of output: 175
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (3)
- src/net/src/dispatch_thread.cc (1 hunks)
- src/net/src/net_util.cc (4 hunks)
- src/net/src/net_util.h (1 hunks)
Additional comments not posted (5)
src/net/src/net_util.h (2)
66-66
: LGTM!The
Empty
method has been correctly updated to be aconst
member function, ensuring it does not modify the state of the object.
76-79
: LGTM!The added comment clearly explains the absence of locks in the current version of
TimerTaskThread
and provides guidance for adding dynamic task management.src/net/src/net_util.cc (3)
47-47
: Verify the impact of changing the return type toint32_t
.The return type of
ExecTimerTask
has been changed fromint
toint32_t
. Ensure that the rest of the codebase correctly handles this new return type.
80-82
: Ensure correct calculation of the time gap.The calculation of
gap_between_now_and_next_task
ensures that it is non-negative. This is correct and prevents potential issues with negative timeout values.
50-62
: Ensure correct handling of expired tasks.The logic for executing expired tasks seems correct. Ensure that all potential edge cases (e.g., tasks expiring exactly at
now_in_ms
) are handled properly.
if (exec_queue_.empty()) { | ||
//to avoid wasting of cpu resources, epoll use 5000ms as timeout value when no task to exec | ||
return 5000; | ||
} |
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.
Consider reducing the default epoll timeout value.
The default timeout value of 5000ms might be too high and could lead to inefficiencies. Consider reducing it to a lower value.
- return 5000;
+ return 1000; // or a suitable lower value
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
if (exec_queue_.empty()) { | |
//to avoid wasting of cpu resources, epoll use 5000ms as timeout value when no task to exec | |
return 5000; | |
} | |
if (exec_queue_.empty()) { | |
//to avoid wasting of cpu resources, epoll use 5000ms as timeout value when no task to exec | |
return 1000; // or a suitable lower value | |
} |
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.
no, this 5000 will be returned only when the timer has no registered task inside. there's no need of reduce it to 1000 to use more cpu.
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.
@cheniujh, thank you for the clarification. Given that the 5000ms timeout is only used when there are no registered tasks, it makes sense to keep it as is to avoid unnecessary CPU usage. Your rationale is clear and well understood.
Learnings added
Learnt from: cheniujh
PR: OpenAtomFoundation/pika#2794
File: src/net/src/net_util.cc:76-79
Timestamp: 2024-07-10T10:05:58.987Z
Learning: When the TimerTaskManager in the Pika project has no registered tasks, it returns a timeout value of 5000ms to the epoll_wait function to avoid wasting CPU resources.
Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.
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.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- src/net/src/net_util.cc (4 hunks)
Files skipped from review as they are similar to previous changes (1)
- src/net/src/net_util.cc
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.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (2)
- src/net/src/net_util.cc (5 hunks)
- src/net/src/net_util.h (3 hunks)
Additional comments not posted (10)
src/net/src/net_util.h (5)
24-24
: LGTM!The introduction of
TimerTaskID
improves code readability and maintainability.
26-26
: LGTM!Changing the type of
task_id
toTimerTaskID
maintains consistency with the new type alias.
37-37
: LGTM!Changing the type of
id
toTimerTaskID
maintains consistency with the new type alias.
62-62
: LGTM!Updating the return type of
ExecTimerTask
toint64_t
allows for a larger range of return values, which is necessary for handling time intervals accurately.
65-65
: LGTM!Updating the
Empty
method to be aconst
member function ensures that it does not modify the state of the object, which is a good practice for methods that are intended to be read-only.src/net/src/net_util.cc (5)
47-47
: LGTM!Updating the return type of
ExecTimerTask
toint64_t
allows for a larger range of return values, which is necessary for handling time intervals accurately.
Line range hint
50-84
: LGTM!The revised logic within
ExecTimerTask
ensures that tasks are executed at the correct times and that the timeout value forepoll_wait
is accurately calculated.
47-47
: LGTM!The removal of the
RenewMinIntervalMs
method simplifies the code and is consistent with the updated logic inExecTimerTask
.
137-137
: LGTM!Explicitly casting the result of
ExecTimerTask
toint32_t
ensures that the timeout value passed toepoll_wait
is within the expected range for anint32_t
.
76-79
: Appropriate timeout value.The timeout value of 5000ms is appropriate to avoid unnecessary CPU usage when there are no tasks to execute.
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.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- src/net/src/net_util.h (3 hunks)
Files skipped from review as they are similar to previous changes (1)
- src/net/src/net_util.h
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.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (2)
- src/net/src/net_util.cc (5 hunks)
- src/net/src/net_util.h (4 hunks)
Files skipped from review as they are similar to previous changes (2)
- src/net/src/net_util.cc
- src/net/src/net_util.h
现在 |
Now |
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.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- src/net/src/net_util.cc (5 hunks)
Files skipped from review as they are similar to previous changes (1)
- src/net/src/net_util.cc
…TaskManager (#2794) * changed the returned timeout of TimerTaskManager --------- Co-authored-by: cheniujh <[email protected]>
…TaskManager (OpenAtomFoundation#2794) * changed the returned timeout of TimerTaskManager --------- Co-authored-by: cheniujh <[email protected]>
问题:
原本的TimerTaskManager(定时器)提供给epoll的timeout值为min_interval_ms,即注册在定时器内部所有定时任务中,执行间隔最小的任务的那个执行间隔。但是这样可能导致一个问题:假如有2个定时任务,一个每隔 20s执行一次,一个每隔30s执行一次,那这个定时器取的timeout就都是20s(min_time_interval), 也就是每隔20s才会检查一下有没有任务过期。这样就会导致那个30s间隔的定时任务每次都要延迟10s才能被执行到。
解决:
直接取下一个即将过期的定时任务的过期时间戳减去当前时间戳,得到的值交给epoll_wait作为timeout参数即可。这样逻辑更加简单直接,而且可以规避上面提到的间隔误差问题。
Question: Originally, the TimerTaskManager (timer) provided the timeout value for epoll as min_interval_ms, which is the execution interval of the task with the shortest interval among all the scheduled tasks within the timer. However, this approach can lead to a problem: if there are two scheduled tasks, one that executes every 20 seconds and another that executes every 30 seconds, the timer will take 20 seconds (min_time_interval) as the timeout value. This means it will check for expired tasks every 20 seconds. Consequently, the task with a 30-second interval will always be delayed by 10 seconds before it is executed.
Solution:
Directly take the difference between the expiration timestamp of the next task to expire and the current timestamp, and pass this value to epoll_wait as the timeout parameter. This approach is simpler and more direct, and it avoids the interval error problem mentioned above.
Summary by CodeRabbit
ExecTimerTask
.RenewMinIntervalMs
method to streamline code.TimerTaskManager
, resulting in better task handling and timeout calculation.