Skip to content

Implement chrt - #712

Open
mmclinton wants to merge 3 commits into
uutils:mainfrom
mmclinton:chrt-implement
Open

mmclinton wants to merge 3 commits into
uutils:mainfrom
mmclinton:chrt-implement

Conversation

@mmclinton

Copy link
Copy Markdown
Contributor

Closes #641.

chrt shows or changes the scheduling policy and priority of a task: it reads them with -p PID, sets them on a running task with a policy option and -p, or runs a command under a policy. This is all of chrt(1) as of util-linux 2.42: the seven policies -o, -f, -r, -b, -i, -d and -e, -R, the timing options -T, -P and -D, -a, -m, -p, -v, -h and -V. It is Linux only; the other targets compile and exit 1 with a message. It adds no dependency, and its six unsafe blocks are all FFI: sched_get_priority_min, sched_get_priority_max, and four calls through syscall(), because libc has no sched_setattr or sched_getattr wrapper, musl's sched_setscheduler fails without making the call, and the raw getpriority has no valid -1 to confuse with an error.

I compared it against chrt(1) from util-linux 2.42.2 on stdout, exit status and the scheduling attributes the target ends up with.

Differences from util-linux

clap reads the command line, not getopt. -m waits for the whole line to parse, so a later -h, -V or parse error wins over it. -T=5 is read as -T 5 where chrt(1) rejects =5.

The Rust runtime. An inherited ignored SIGPIPE is lost before main runs, so chrt and the command it runs start with the default action. A closed standard descriptor is reopened on /dev/null, so the command finds it open.

Non-UTF-8 bytes in a diagnostic are echoed as U+FFFD.

Tests

73 integration tests, plus 4 unit tests.

A realtime set that succeeds needs CAP_SYS_NICE, which CI does not have, so for SCHED_FIFO, SCHED_RR and SCHED_DEADLINE only the refusal is tested. SCHED_EXT and -T on SCHED_OTHER and SCHED_BATCH need Linux 6.12, so their tests check the kernel first and return early on an older one.

chrt shows or changes the scheduling policy and priority of a task: it
reads them with -p PID, sets them with a policy option and -p, or runs
a command under a policy. It covers chrt(1) as of util-linux 2.42: the
seven policies -o -f -r -b -i -d -e, -R, the timing options -T -P -D,
and -a -m -p -v. It is Linux only; other targets build and exit 1 with
a message.

The checks run in chrt(1)'s order, because the first failure is the one
reported: under --pid the pid is parsed before anything else, and a -f
or -r anywhere on the line demands a priority even when a later policy
option wins.

OTHER, BATCH and DEADLINE are set with sched_setattr(2), the rest with
sched_setscheduler(2), as chrt(1) does. sched_attr carries a nice
value, so the task's own is read with getpriority(2) and passed back:
passing 0 would reset a niced task, which needs privilege.
sched_setattr, sched_getattr, sched_setscheduler and getpriority go
through syscall(): libc has no wrapper for the first two, musl's
sched_setscheduler fails without making the call, and the raw
getpriority has no valid -1 to confuse with an error.

clap reads the command line where chrt(1) uses getopt, which makes two
differences: -T=5 is read as -T 5 where chrt(1) rejects =5, and -m
waits for the whole line to parse, so a later -h, -V or parse error
wins over it. The Rust runtime makes two more before main runs: an
inherited ignored SIGPIPE is lost, so chrt and the command it runs
start with the default action, and a closed standard descriptor is
reopened on /dev/null, so the command finds it open.
Every set is checked by reading the target back with a raw
sched_getattr(2) that shares nothing with the port, never by what chrt
prints. Each test spawns its own target from a known policy and nice
value rather than inheriting the runner's.

A target with several threads comes from running this test binary
again on an ignored helper test, which spawns sleeping threads, so -a
has a stable set to walk without a new dependency.

A realtime set that succeeds needs CAP_SYS_NICE, which CI does not
have, so for FIFO, RR and DEADLINE only the refusal is tested. SCHED_EXT
and -T on OTHER and BATCH need Linux 6.12, so their tests check the
kernel first and return early on an older one.
cargo test from the workspace root runs only the root package's tests,
so the unit tests in uu_chrt would not run in CI.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement chrt

1 participant