-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[EXPERIMENTAL]sched/hrtimer: hrtimer state machine improvement for SMP cases #17642
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
base: master
Are you sure you want to change the base?
Conversation
742782b to
f475319
Compare
f475319 to
7c27ef8
Compare
7c27ef8 to
6ba659c
Compare
b1ed47e to
c91948a
Compare
c91948a to
dc9a2b7
Compare
dc9a2b7 to
bc161b5
Compare
b0a43d1 to
411f1b0
Compare
49822a8 to
e9940ec
Compare
9358ac8 to
efdbd6e
Compare
efdbd6e to
51cae3d
Compare
51cae3d to
31322b7
Compare
Allow running/armed hrtimer to be restarted to fix hrtimer bug: apache#17567 Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
Add hrtimer_set() to allow users to change hrtimer callback after hrtimer is started or cancelled Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
Update the hrtimer documentation to describe the hrtimer state machine,
which is introduced to handle safe cancellation and execution in SMP
environments.
Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
31322b7 to
5df81dd
Compare
Enable the timer start functions when hrtimer is enabled. This allows hrtimer to set timer expirations with nanosecond resolution. Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
5df81dd to
68bb59c
Compare
| uint64_t expired; /* Absolute expiration time (ns) */ | ||
| hrtimer_node_t node; /* RB-tree node for sorted insertion */ | ||
| hrtimer_cb func; /* Expiration callback function */ | ||
| FAR void *arg; /* Argument passed to callback */ |
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.
let's use container_of to restore the context and remove arg to save the memory?
|
|
||
| /* If the inserted timer is now the earliest, start hardware timer */ | ||
|
|
||
| if (&hrtimer->node == RB_MIN(hrtimer_tree_s, &g_hrtimer_tree)) |
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.
can we check RB_LEFT is null
| hrtimer_cb func; /* Expiration callback function */ | ||
| FAR void *arg; /* Argument passed to callback */ | ||
| uint64_t expired; /* Absolute expiration time (ns) */ | ||
| uint8_t cpus; /* Number of cpus that are running the timer */ |
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.
it's better to save the running hrtimer to global variable like:
https://github.com/apache/nuttx/blob/master/sched/wqueue/wqueue.h#L67
https://github.com/apache/nuttx/pull/17675/changes#diff-3da39fd3cc9900688d3090fe48941b8197f3730e9a2c71d34e21e1eee1eeafa7R91
| */ | ||
|
|
||
| typedef uint32_t (*hrtimer_cb)(FAR struct hrtimer_s *hrtimer); | ||
| typedef uint32_t (*hrtimer_cb)(FAR void *arg); |
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.
why change
|
|
||
| #include <stdint.h> | ||
| #include <sys/tree.h> | ||
| #include <errno.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.
why include the unused header file
| */ | ||
|
|
||
| typedef uint32_t (*hrtimer_cb)(FAR struct hrtimer_s *hrtimer); | ||
| typedef uint32_t (*hrtimer_cb)(FAR void *arg); |
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.
return value need uint64_t
| /* Invoke the timer callback */ | ||
|
|
||
| period = hrtimer->func(hrtimer); | ||
| period = func(arg); |
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.
expired += func(arg) and remove period
|
|
||
| /* Re-arm periodic timer if not canceled or re-armed concurrently */ | ||
|
|
||
| if (period > 0 && hrtimer->expired == expired) |
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.
hrtimer->expired != UINT64_MAX && hrtimer->expired != expired
| * | ||
| ****************************************************************************/ | ||
|
|
||
| int hrtimer_set(FAR hrtimer_t *hrtimer, |
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.
why need? it's seldom to initialize func with the different value in the lifetime of hrtimer
| ****************************************************************************/ | ||
|
|
||
| #ifdef CONFIG_SCHED_TICKLESS | ||
| #if defined(CONFIG_HRTIMER) || defined(CONFIG_SCHED_TICKLESS) |
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.
move to patch #17573
Summary
This PR is also included in #17573. Since #17573 covers both overall scheduler support with hrtimer and related hrtimer improvements for SMP mode, and since hrtimer is an independent module, this change is submitted separately to focus on hrtimer improvements and avoid conflicts with other scheduler-related enhancements such as #17640.
This PR refines the hrtimer state machine to allow ARMED and RUNNING hrtimers to be safely restarted, and fixes several corner-case issues in SMP mode.
The updated hrtimer state machine is shown below and is also documented in the hrtimer module documentation:
Impact
Improments for the hrtimer module
Testing
Test 1 passed (integrated in ostest):
- test implementation:
test log on rv-virt:smp64:
test 2 passed (provided by @Fix-Point )
test implementation
test passed log on rv-virt:smp64
test 3 passed (provided by @Fix-Point )
test implementation
test passed log on rv-virt:smp64
test 4 passed (provided by @Fix-Point )
test implementation
test passed log on rv-virt:smp64