Skip to content

taskset: add CPU affinity tool - #506

Open
jmr wants to merge 1 commit into
uutils:mainfrom
jmr:taskset
Open

jmr wants to merge 1 commit into
uutils:mainfrom
jmr:taskset

Conversation

@jmr

@jmr jmr commented Mar 24, 2026

Copy link
Copy Markdown
Contributor

Implements taskset(1) with hex mask and CPU list parsing/formatting, -p (pid mode), -a (all-tasks), and exec mode. Includes stride notation in format_cpu_list, comma-separated hex mask support from /proc//status, and unit + integration tests.

@codecov

codecov Bot commented Apr 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 0.00%. Comparing base (eae208f) to head (709e07b).
⚠️ Report is 60 commits behind head on main.

Additional details and impacted files
@@     Coverage Diff     @@
##   main   #506   +/-   ##
===========================
===========================

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Implements taskset(1) with hex mask and CPU list parsing/formatting, -p (pid
mode), -a (all-tasks), and exec mode. Includes stride notation in
format_cpu_list, comma-separated hex mask support from /proc/<pid>/status, and
unit + integration tests.
@jmr

jmr commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Ping. @sylvestre

// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

#![cfg_attr(not(target_os = "linux"), allow(dead_code, unused_imports))]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please don't silence this with allow, cfg the unused bits instead

println!("pid {}'s current {}: {}", p, label, fmt(&set));
}
}
[mask_str, pid_str, ..] => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

.. silently accepts taskset -p mask pid extra, util-linux rejects it, no?

} else {
vec![pid]
};
for p in pids {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

almost the same loop as the one above (pids + getaffinity + ESRCH), could be dedup, no?

// exec() replaces the current process image and never returns
// on success; if it returns at all, it's an error.
let err = std::process::Command::new(command).args(rest).exec();
return Err(err.map_err_context(|| format!("failed to execute {command}")));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

util-linux exits with 127 when the command is not found and 126 for other exec failures, not 1.
could you please match that and update test_command_not_found?


#[cfg(target_os = "linux")]
fn format_hex_mask(set: &CpuSet) -> String {
let max_cpu = (0..CpuSet::count()).rev().find(|&i| set.is_set(i).unwrap());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please avoid the unwrap() here and at line 312
format!("{:x}") on the nibble would be simpler

ranges.push(format!("{}-{}", a, b));
i = j + 1;
} else {
ranges.push(format!("{}-{}:{}", a, b, step));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

as far as i know, util-linux taskset -cp never prints stride notation: for 0,2,4 it prints 0,2,4, not 0-4:2.
could you please check against the real taskset and match it?

.args(&["-c", "0", "/usr/bin/true"])
.succeeds()
.no_output();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

these exec tests only check that it doesn't fail
could you please run something like cat /proc/self/status and assert that Cpus_allowed_list matches?
also please use true rather than /usr/bin/true, it isn't there on every system

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.

2 participants