-
-
Notifications
You must be signed in to change notification settings - Fork 20
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
Add support for SCHED_DEADLINE flags #15
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,6 +103,17 @@ impl RealtimeThreadSchedulePolicy { | |
} | ||
} | ||
|
||
/// Flags for controlling Deadline scheduling behavior. | ||
#[derive(Debug, Clone, Copy, Ord, PartialEq, Eq, PartialOrd, Hash)] | ||
pub enum DeadlineFlags { | ||
/// Children created by fork will not inhered privileged scheduling policies. | ||
ResetOnFork = 0x01, | ||
/// The thread may reclaim bandwidth that is unused by another realtime thread. | ||
Reclaim = 0x02, | ||
/// Request to be sent SIGXCPU when this thread overruns its deadline. | ||
DeadlineOverrun = 0x04, | ||
} | ||
|
||
/// Normal (usual) schedule policies | ||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] | ||
pub enum NormalThreadSchedulePolicy { | ||
|
@@ -220,7 +231,7 @@ impl ThreadPriority { | |
_ => Ok(0), | ||
}, | ||
#[cfg(target_os = "linux")] | ||
ThreadPriority::Deadline(_, _, _) => Err(Error::Priority( | ||
ThreadPriority::Deadline(_, _, _, _) => Err(Error::Priority( | ||
"Deadline is non-POSIX and cannot be converted.", | ||
)), | ||
}; | ||
|
@@ -313,8 +324,8 @@ pub fn set_thread_schedule_policy( | |
// SCHED_DEADLINE policy requires its own syscall | ||
#[cfg(target_os = "linux")] | ||
ThreadSchedulePolicy::Realtime(RealtimeThreadSchedulePolicy::Deadline) => { | ||
let (runtime, deadline, period) = match priority { | ||
ThreadPriority::Deadline(r, d, p) => (r, d, p), | ||
let (runtime, deadline, period, flags) = match priority { | ||
ThreadPriority::Deadline(r, d, p, f) => (r, d, p, f), | ||
_ => { | ||
return Err(Error::Priority( | ||
"Deadline policy given without deadline priority.", | ||
|
@@ -325,20 +336,17 @@ pub fn set_thread_schedule_policy( | |
let sched_attr = SchedAttr { | ||
size: std::mem::size_of::<SchedAttr>() as u32, | ||
sched_policy: policy.to_posix() as u32, | ||
|
||
sched_flags: match flags { | ||
None => 0, | ||
Some(flags) => flags as u64, | ||
}, | ||
sched_runtime: runtime as u64, | ||
sched_deadline: deadline as u64, | ||
sched_period: period as u64, | ||
|
||
..Default::default() | ||
}; | ||
libc::syscall( | ||
libc::SYS_sched_setattr, | ||
tid, | ||
&sched_attr as *const _, | ||
// we are not setting SCHED_FLAG_RECLAIM nor SCHED_FLAG_DL_OVERRUN | ||
0, | ||
) as i32 | ||
libc::syscall(libc::SYS_sched_setattr, tid, &sched_attr as *const _, 0) as i32 | ||
} | ||
_ => libc::pthread_setschedparam( | ||
native, | ||
|
@@ -543,7 +551,12 @@ mod tests { | |
|
||
assert!(set_thread_priority_and_policy( | ||
0, // current thread | ||
ThreadPriority::Deadline(1 * 10_u64.pow(6), 10 * 10_u64.pow(6), 100 * 10_u64.pow(6)), | ||
ThreadPriority::Deadline( | ||
1 * 10_u64.pow(6), | ||
10 * 10_u64.pow(6), | ||
100 * 10_u64.pow(6), | ||
None | ||
), | ||
ThreadSchedulePolicy::Realtime(RealtimeThreadSchedulePolicy::Deadline) | ||
) | ||
.is_ok()); | ||
|
@@ -569,4 +582,48 @@ mod tests { | |
assert_eq!(sched_attr.sched_period, 100 * 10_u64.pow(6)); | ||
} | ||
} | ||
|
||
#[test] | ||
#[cfg(target_os = "linux")] | ||
fn set_deadline_flags() { | ||
// allow the identity operation for clarity | ||
#![allow(clippy::identity_op)] | ||
|
||
assert!(set_thread_priority_and_policy( | ||
0, // current thread | ||
ThreadPriority::Deadline( | ||
1 * 10_u64.pow(6), | ||
10 * 10_u64.pow(6), | ||
100 * 10_u64.pow(6), | ||
Some(DeadlineFlags::DeadlineOverrun) | ||
), | ||
ThreadSchedulePolicy::Realtime(RealtimeThreadSchedulePolicy::Deadline) | ||
) | ||
.is_ok()); | ||
|
||
// now we check the return values | ||
unsafe { | ||
let mut sched_attr = SchedAttr::default(); | ||
let ret = libc::syscall( | ||
libc::SYS_sched_getattr, | ||
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. We have already used this syscall twice. Perhaps, there is also use of it for others in the library? Can we extract it into a |
||
0, // current thread | ||
&mut sched_attr as *mut _, | ||
std::mem::size_of::<SchedAttr>() as u32, | ||
0, // flags must be 0 | ||
); | ||
|
||
assert!(ret >= 0); | ||
assert_eq!( | ||
sched_attr.sched_policy, | ||
RealtimeThreadSchedulePolicy::Deadline.to_posix() as u32 | ||
); | ||
assert_eq!(sched_attr.sched_runtime, 1 * 10_u64.pow(6)); | ||
assert_eq!(sched_attr.sched_deadline, 10 * 10_u64.pow(6)); | ||
assert_eq!(sched_attr.sched_period, 100 * 10_u64.pow(6)); | ||
assert_eq!( | ||
sched_attr.sched_flags, | ||
DeadlineFlags::DeadlineOverrun as u64 | ||
) | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 you please also add a link to any online "man" where this is also documented? Just in case one needs more information.