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 fractional and subtraction for jobs flag #3554

Merged
merged 11 commits into from
Sep 16, 2024

Conversation

wb14123
Copy link
Contributor

@wb14123 wb14123 commented Sep 15, 2024

Resolve #3482

Allow jobs param to have following formats:

  1. An integer indicates the parallel level.
  2. A float N followed by character "C" indicates uses (N * all available cores). e.g. "0.5C" uses half of the available cores.
  3. "C-" followed by an integer N indicates uses (all available cores - N). e.g. "C-1" leaves 1 core and uses all the other cores.

Resolve com-lihaoyi#3482

Allow jobs param to have following formats:

1. An integer indicates the parallel level.
2. A float N followed by character "C" indicates uses (N * all available cores).
   e.g. "0.5C" uses half of the available cores.
3. "C-" followed by an integer N indicates uses (all available cores - N).
   e.g. "C-1" leaves 1 core and uses all the other cores.
Comment on lines 280 to 298
def err(detail: String) = new Exception(
s"Invalid value \"${threadCountRaw.getOrElse("")}\" for flag -j/--jobs: $detail"
)
(threadCountRaw match {
case None => Success(cores)
case Some(s"${n}C") => n.toDoubleOption.map(Success(_))
.getOrElse(Failure(err("Failed to find a float number before \"C\".")))
.map(m => (m * cores).toInt)
case Some(s"C-${n}") => n.toIntOption.map(Success(_))
.getOrElse(Failure(err("Failed to find a int number after \"C-\".")))
.map(cores - _)
case Some(n) => n.toIntOption.map(Success(_))
.getOrElse(Failure(err("Failed to find a int number")))
}).flatMap {
case x if x < 0 => Failure(err("Calculated cores to use is a negative number."))
case 0 => Success(cores) // use all cores if 0
case x => Success(x)
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Few nits here:

  1. Can we extract this into a helper function that we can unit test with various combinations of detail: String and cores: Int? That will give us some confidence at-a-glance that the code is doing what we expect without tedious manual testing

  2. I don't think the current treatment of "0 = use all cores" makes sense for computed zeros. If someone runs -j0.4C on a 4 core machine, they probably don't intend to use all 4 cores. Let's treat literal 0 separately from computed numbers, and have computed numbers have a lower limit of 1 core (so 0 or -1 becomes 1)

  3. Lets return an Either[String, Int] since that's all the information you need later on at the callsite

@wb14123
Copy link
Contributor Author

wb14123 commented Sep 16, 2024

@lihaoyi addressed comments about the code. Where should I put the unit tests?

@lihaoyi
Copy link
Member

lihaoyi commented Sep 16, 2024

runner/test/src/mill/runner/ should work i think, run via mill runner.test

@wb14123
Copy link
Contributor Author

wb14123 commented Sep 16, 2024

@lihaoyi Added unit tests. Seems the auto lint check failed on previous commit again. Is there any command that I can auto format code or show lint errors?

@lihaoyi
Copy link
Member

lihaoyi commented Sep 16, 2024

@wb14123 ./mill -i mill.scalalib.scalafmt.ScalafmtModule/reformatAll __.sources does the formatting

@wb14123
Copy link
Contributor Author

wb14123 commented Sep 16, 2024

Hmm I formatted the files. Not sure why it failed again. Does the command above also format the test files?

@lihaoyi
Copy link
Member

lihaoyi commented Sep 16, 2024

@wb14123 it should. Could you try merging in latest main and running it again? Maybe there's some formatting inconsistencies introduced by the merge

Comment on lines 45 to 48
assertParseErr(
MillMain.parseThreadCount(Some("0.09C"), 10),
"Calculated cores to use should be a positive number"
)
Copy link
Member

Choose a reason for hiding this comment

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

For these computed cases (this one and C-10 and C-11 below), let's always just round up to 1 core minimum. That's probably what users want, so no point erroring out when we can just do it for them

@lihaoyi
Copy link
Member

lihaoyi commented Sep 16, 2024

Left one more comment, after that I think we're good to merge

@lihaoyi lihaoyi merged commit 1ede694 into com-lihaoyi:main Sep 16, 2024
24 checks passed
@lefou lefou added this to the 0.12.0 milestone Sep 16, 2024
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.

Allow fractional jobs to automatically scale by number of CPUs (200USD Bounty)
3 participants