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
17 changes: 12 additions & 5 deletions runner/src/mill/runner/MillCliConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,17 @@ class MillCliConfig private (
name = "jobs",
short = 'j',
doc =
"""Allow processing N targets in parallel.
"""Allow processing N targets in parallel. It can has 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.

Use 1 to disable parallel and 0 to use as much threads as available processors."""
)
val threadCountRaw: Option[Int],
val threadCountRaw: Option[String],
@arg(
name = "import",
doc = """Additional ivy dependencies to load into mill, e.g. plugins."""
Expand Down Expand Up @@ -175,7 +182,7 @@ object MillCliConfig {
debugLog: Flag = Flag(),
keepGoing: Flag = Flag(),
extraSystemProperties: Map[String, String] = Map(),
threadCountRaw: Option[Int] = None,
threadCountRaw: Option[String] = None,
imports: Seq[String] = Seq(),
interactive: Flag = Flag(),
help: Flag = Flag(),
Expand Down Expand Up @@ -224,7 +231,7 @@ object MillCliConfig {
debugLog: Flag,
keepGoing: Flag,
extraSystemProperties: Map[String, String],
threadCountRaw: Option[Int],
threadCountRaw: Option[String],
imports: Seq[String],
interactive: Flag,
help: Flag,
Expand Down Expand Up @@ -273,7 +280,7 @@ object MillCliConfig {
debugLog: Flag,
keepGoing: Flag,
extraSystemProperties: Map[String, String],
threadCountRaw: Option[Int],
threadCountRaw: Option[String],
imports: Seq[String],
interactive: Flag,
help: Flag,
Expand Down
34 changes: 29 additions & 5 deletions runner/src/mill/runner/MillMain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ object MillMain {

// special BSP mode, in which we spawn a server and register the current evaluator when-ever we start to eval a dedicated command
val bspMode = config.bsp.value && config.leftoverArgs.value.isEmpty
val maybeThreadCount =
parseThreadCount(config.threadCountRaw, Runtime.getRuntime.availableProcessors())

val (success, nextStateCache) = {
if (config.repl.value) {
Expand All @@ -179,15 +181,15 @@ object MillMain {
logger.error("A target must be provided.")
(false, stateCache)

} else if (maybeThreadCount.isLeft) {
logger.error(maybeThreadCount.swap.toOption.get)
(false, stateCache)

} else {
val userSpecifiedProperties =
userSpecifiedProperties0 ++ config.extraSystemProperties

val threadCount = config.threadCountRaw match {
case None => None
case Some(0) => None
case Some(n) => Some(n)
}
val threadCount = Some(maybeThreadCount.toOption.get)

if (mill.main.client.Util.isJava9OrAbove) {
val rt = config.home / Export.rtJarName
Expand Down Expand Up @@ -275,6 +277,28 @@ object MillMain {
}
}

private[runner] def parseThreadCount(
threadCountRaw: Option[String],
availableCores: Int
): Either[String, Int] = {
def err(detail: String) =
s"Invalid value \"${threadCountRaw.getOrElse("")}\" for flag -j/--jobs: $detail"
(threadCountRaw match {
case None => Right(availableCores)
case Some(s"${n}C") => n.toDoubleOption
.toRight(err("Failed to find a float number before \"C\"."))
.map(m => (m * availableCores).toInt)
case Some(s"C-${n}") => n.toIntOption
.toRight(err("Failed to find a int number after \"C-\"."))
.map(availableCores - _)
case Some(n) => n.toIntOption
.toRight(err("Failed to find a int number"))
}).flatMap {
case x if x <= 0 => Left(err("Calculated cores to use should be a positive number."))
case x => Right(x)
}
}

def getLogger(
streams: SystemStreams,
config: MillCliConfig,
Expand Down
95 changes: 95 additions & 0 deletions runner/test/src/mill/runner/MillMainTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package mill.runner

import utest._

object MillMainTests extends TestSuite {

private def assertParseErr(result: Either[String, Int], msg: String): Unit = {
assert(result.isLeft)
assert(result.swap.toOption.get.contains(msg))
}

def tests: Tests = Tests {

test("Parsing --jobs/-j flag") {

test("parse none") {
assert(MillMain.parseThreadCount(None, 10) == Right(10))
}

test("parse int number") {
assert(MillMain.parseThreadCount(Some("1"), 10) == Right(1))
assert(MillMain.parseThreadCount(Some("11"), 10) == Right(11))

assertParseErr(MillMain.parseThreadCount(Some("1.0"), 10), "Failed to find a int number")
assertParseErr(MillMain.parseThreadCount(Some("1.1"), 10), "Failed to find a int number")
assertParseErr(MillMain.parseThreadCount(Some("0.1"), 10), "Failed to find a int number")
assertParseErr(
MillMain.parseThreadCount(Some("0"), 10),
"Calculated cores to use should be a positive number."
)
assertParseErr(
MillMain.parseThreadCount(Some("-1"), 10),
"Calculated cores to use should be a positive number."
)
}

test("parse fraction number") {
assert(MillMain.parseThreadCount(Some("0.5C"), 10) == Right(5))
assert(MillMain.parseThreadCount(Some("0.54C"), 10) == Right(5))
assert(MillMain.parseThreadCount(Some("0.59C"), 10) == Right(5))
assert(MillMain.parseThreadCount(Some(".5C"), 10) == Right(5))
assert(MillMain.parseThreadCount(Some("1.0C"), 10) == Right(10))
assert(MillMain.parseThreadCount(Some("1.5C"), 10) == Right(15))

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

assertParseErr(
MillMain.parseThreadCount(Some("-0.5C"), 10),
"Calculated cores to use should be a positive number"
)
assertParseErr(
MillMain.parseThreadCount(Some("0.5.4C"), 10),
"Failed to find a float number before \"C\""
)
}

test("parse subtraction") {
assert(MillMain.parseThreadCount(Some("C-1"), 10) == Right(9))

assertParseErr(
MillMain.parseThreadCount(Some("C-10"), 10),
"Calculated cores to use should be a positive number."
)
assertParseErr(
MillMain.parseThreadCount(Some("C-11"), 10),
"Calculated cores to use should be a positive number."
)

assertParseErr(
MillMain.parseThreadCount(Some("C-1.1"), 10),
"Failed to find a int number after \"C-\""
)
assertParseErr(
MillMain.parseThreadCount(Some("11-C"), 10),
"Failed to find a float number before \"C\""
)
}

test("parse invalid input") {
assertParseErr(
MillMain.parseThreadCount(Some("CCCC"), 10),
"Failed to find a float number before \"C\""
)
assertParseErr(
MillMain.parseThreadCount(Some("abcdefg"), 10),
"Failed to find a int number"
)
}

}

}
}
Loading