Repository navigation
Implement chrt - #712
Open
mmclinton wants to merge 3 commits into
Open
Implement chrt#712mmclinton wants to merge 3 commits into
chrt#712mmclinton wants to merge 3 commits into
Conversation
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.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #641.
chrtshows 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,-dand-e,-R, the timing options-T,-Pand-D,-a,-m,-p,-v,-hand-V. It is Linux only; the other targets compile and exit 1 with a message. It adds no dependency, and its sixunsafeblocks are all FFI:sched_get_priority_min,sched_get_priority_max, and four calls throughsyscall(), because libc has nosched_setattrorsched_getattrwrapper, musl'ssched_setschedulerfails without making the call, and the rawgetpriorityhas 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.
-mwaits for the whole line to parse, so a later-h,-Vor parse error wins over it.-T=5is read as-T 5where chrt(1) rejects=5.The Rust runtime. An inherited ignored SIGPIPE is lost before
mainruns, 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-Ton SCHED_OTHER and SCHED_BATCH need Linux 6.12, so their tests check the kernel first and return early on an older one.