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

Added support for negative --jobs parameter, counting backwards from max CPUs. #9221

Closed
wants to merge 1 commit into from

Conversation

orlp
Copy link
Contributor

@orlp orlp commented Mar 1, 2021

Closes #9217.
Closes #9219.

@rust-highfive
Copy link

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @alexcrichton (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 1, 2021
@alexcrichton
Copy link
Member

Thanks for the PR! This seems like a neat little feature which doesn't really have a ton of impact on Cargo itself in terms of overall design. One thing I'm curious about, though, is if you're aware of any precedent for this? We try to mirror the -j semantics of build tools like ninja/make, but I'm not familiar with any other build tools that have this sort of interpretation of the -j flag.

@orlp
Copy link
Contributor Author

orlp commented Mar 2, 2021

@alexcrichton

I know ninja defined -j 0 as 'unlimited parallelism', but doesn't support -j < 0. I'm not personally aware of any other tools with a similar parameter that support a negative number.

@alexcrichton
Copy link
Member

Ah ok thanks for the clarification. Again this seems like a nice feature, but I'm a bit wary blazing the trail here when very established other build systems haven't implemented something like this.

We discussed this at the Cargo meeting yesterday and one worry was that -j -1 can be ambiguous if you don't already know what it means. We weren't entirely sure how to smooth over that, but it's also somewhat of a minor concern. One question we did have, did you envision this being passed on the CLI or through configuration more frequently? We could presumably make a more verbose syntax in .cargo/config.toml with inline tables and such rather than just a simple integer.

@orlp
Copy link
Contributor Author

orlp commented Mar 3, 2021

@alexcrichton I envision both being used, honestly. I don't know which would be more frequent. I am honestly not that familiar with .cargo/config.toml, so I'll refrain from commenting on how its config should look like.

One worry was that -j -1 can be ambiguous if you don't already know what it means

Yeah I would prefer it if there was at least a mention of it in the help.

-j, --jobs <N>                   Number of parallel jobs, defaults to # of CPUs

I tried but couldn't think of a good short unambiguous description that fits in ~70 cols (the longest line in the options currently).

Either way, even if someone assumed that -j -1 would mean 'all processors' (the only other reasonable interpretation IMO), they're only off by 1 at the worst, so really not the end of the world.

let jobs = jobs.or(cfg.jobs).unwrap_or(::num_cpus::get() as u32);
let jobs = match jobs.or(cfg.jobs) {
None => ::num_cpus::get() as u32,
Some(j) if j < 0 => (::num_cpus::get() as i32 + j).max(1) as u32,
Copy link
Member

Choose a reason for hiding this comment

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

I think I'd personally prefer that this return an error if the result is less than one rather than clamping to one

Copy link
Contributor Author

@orlp orlp Mar 5, 2021

Choose a reason for hiding this comment

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

@alexcrichton I think it's better if not, suppose that a build script contains -j -2 but is then run on a dual-core machine, now your build breaks. Also, -j 1000 doesn't give an error either.

Copy link
Member

Choose a reason for hiding this comment

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

Hm this is something I'm worried about though in that it's sort of hidden behavior. Using -j100 when you have 2 cpus just means "spawn up to 100 things at the same time" which is always possible, you're basically asking the kernel to do tons of scheduling for you. If the request ends up being "spawn at most -N" tasks I'm not sure Cargo can reasonbly handle that and I would prefer we be conservative and return an error.

If in the future, though, the script case is common enough and the error is just getting in everyone's way we could change it to clamp to 1

@alexcrichton alexcrichton added the T-cargo Team: Cargo label Mar 5, 2021
@alexcrichton
Copy link
Member

Ok this seems reasonable enough to me to go ahead and merge. We'll be trailblazers here in that there's not really any precedent for this, but I don't think that's the end of the world.

@rfcbot fcp merge

@rfcbot
Copy link
Collaborator

rfcbot commented Mar 5, 2021

Team member @alexcrichton has proposed to merge this. The next step is review by the rest of the tagged team members:

Concerns:

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period An FCP proposal has started, but not yet signed off. disposition-merge FCP with intent to merge labels Mar 5, 2021
@ehuss
Copy link
Contributor

ehuss commented Mar 8, 2021

@rfcbot concern docs

This needs some documentation updates. Can you make the following updates?

@orlp
Copy link
Contributor Author

orlp commented Mar 8, 2021

@ehuss The config docs I have no problem with, I can do that. I did try to think of a way to specify the command line doc but I could not find a concise wording I was happy with. My concern is that the command line docs are generally very terse and the best description I could come up with was still significantly longer than all others.

So suggestions are definitely welcome.

@ehuss
Copy link
Contributor

ehuss commented Mar 10, 2021

I wouldn't worry too much about the clap CLI help string. Maybe something like (negative is relative to max), but I would also be fine with not adding anything. Up to you!

@bors
Copy link
Collaborator

bors commented Apr 16, 2021

☔ The latest upstream changes (presumably #9367) made this pull request unmergeable. Please resolve the merge conflicts.

@ehuss ehuss added S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 20, 2021
@ehuss
Copy link
Contributor

ehuss commented Jul 1, 2021

Ping @orlp, are you still interested in moving forward with this? I think the documentation was the only thing that needed updating.

@dtolnay
Copy link
Member

dtolnay commented Oct 9, 2021

How feasible would it be to accept a very basic expression grammar for -j? As in -j cpus-2 or -j 'cpus / 2', or possibly -j n-1 / -j N-1. I think I would find this more appealing and self explanatory for readers than a negative number.

@ehuss
Copy link
Contributor

ehuss commented Dec 14, 2021

I'm going to close due to inactivity. We are still interested in some solution here. Feel free to reopen if you have a chance to work on it again.

@ehuss ehuss closed this Dec 14, 2021
@rfcbot rfcbot removed proposed-final-comment-period An FCP proposal has started, but not yet signed off. disposition-merge FCP with intent to merge labels Dec 14, 2021
bors added a commit that referenced this pull request Aug 1, 2022
Support for negative --jobs parameter, counting backwards from max CPUs

Fixes #9217.

Continuation of #9221.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author. T-cargo Team: Cargo
Projects
None yet
7 participants