Skip to content
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 realtime scheduling calls to std.os.linux (issue #19671) #19675

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 47 additions & 0 deletions lib/std/os/linux.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1876,6 +1876,53 @@ pub fn fremovexattr(fd: usize, name: [*:0]const u8) usize {
return syscall2(.fremovexattr, fd, @intFromPtr(name));
}

pub const sched_param = extern struct {
sched_priority: i32,
};

pub const SCHED = struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this not be an enum, rather than a struct?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with it being an enum is that the RESET_ON_FORK is not an independent value, but a flag that is ORed with one of the other values.

Copy link
Contributor

@rohlem rohlem Apr 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the recommended thing to do would be to make it a packed struct, something like this (untested):

pub const SCHED = packed struct(i32) {
  pub const Mode = enum(u3) {
    /// normal multi-user scheduling
    pub const OTHER = 0;
    /// FIFO realtime scheduling
    pub const FIFO = 1;
    /// Round-robin realtime scheduling
    pub const RR = 2;
    /// For "batch" style execution of processes
    pub const BATCH = 3;
    /// Low latency scheduling
    pub const ISO = 4;
    /// For running very low priority background jobs
    pub const IDLE = 5;
    /// Sporadic task model deadline scheduling
    pub const DEADLINE = 6;
  };
  mode: Mode, //bits [0, 2]
  _3: u27 = 0, //bits [3, 29]
  /// set to true to stop children from inheriting policies
  RESET_ON_FORK: bool = false, //bit 30
  _31: u1 = 0, //bit 31
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I will update my code accordingly.

/// normal multi-user scheduling
pub const OTHER = 0;
/// FIFO realtime scheduling
pub const FIFO = 1;
/// Round-robin realtime scheduling
pub const RR = 2;
/// For "batch" style execution of processes
pub const BATCH = 3;
/// Low latency scheduling
pub const ISO = 4;
/// For running very low priority background jobs
pub const IDLE = 5;
/// Sporadic task model deadline scheduling
pub const DEADLINE = 6;
/// OR with other values to stop children from inheriting policies
pub const RESET_ON_FORK = 0x40000000;
};

pub fn sched_setparam(pid: pid_t, param: *const sched_param) usize {
return syscall2(.sched_setparam, @as(usize, @bitCast(@as(isize, pid))), @intFromPtr(param));
}

pub fn sched_getparam(pid: pid_t, param: *sched_param) usize {
return syscall2(.sched_getparam, @as(usize, @bitCast(@as(isize, pid))), @intFromPtr(param));
}

pub fn sched_setscheduler(pid: pid_t, policy: u32, param: *const sched_param) usize {
return syscall3(.sched_setscheduler, @as(usize, @bitCast(@as(isize, pid))), policy, @intFromPtr(param));
}

pub fn sched_getscheduler(pid: pid_t) usize {
return syscall1(.sched_getscheduler, @as(usize, @bitCast(@as(isize, pid))));
}

pub fn sched_get_priority_max(policy: u32) isize {
return @bitCast(syscall1(.sched_get_priority_max, policy));
}

pub fn sched_get_priority_min(policy: u32) isize {
return @bitCast(syscall1(.sched_get_priority_min, policy));
}

pub fn sched_yield() usize {
return syscall0(.sched_yield);
}
Expand Down