From 1caf3501111e8386bb344ed68c66d6fbce8514d4 Mon Sep 17 00:00:00 2001 From: Bin Wang Date: Sun, 15 Sep 2024 12:33:41 -0400 Subject: [PATCH 1/9] Add fractional and subtraction for jobs flag Resolve https://github.com/com-lihaoyi/mill/issues/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. --- runner/src/mill/runner/MillCliConfig.scala | 17 +++++++---- runner/src/mill/runner/MillMain.scala | 33 ++++++++++++++++++---- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/runner/src/mill/runner/MillCliConfig.scala b/runner/src/mill/runner/MillCliConfig.scala index 7d7a8c39f023..0b337b18bc3a 100644 --- a/runner/src/mill/runner/MillCliConfig.scala +++ b/runner/src/mill/runner/MillCliConfig.scala @@ -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.""" @@ -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(), @@ -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, @@ -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, diff --git a/runner/src/mill/runner/MillMain.scala b/runner/src/mill/runner/MillMain.scala index 56733cbc571a..98a82b55f98d 100644 --- a/runner/src/mill/runner/MillMain.scala +++ b/runner/src/mill/runner/MillMain.scala @@ -3,7 +3,7 @@ package mill.runner import java.io.{FileOutputStream, PipedInputStream, PrintStream} import java.util.Locale import scala.jdk.CollectionConverters._ -import scala.util.Properties +import scala.util.{Failure, Properties, Success, Try} import mill.java9rtexport.Export import mill.api.{MillException, SystemStreams, WorkspaceRoot, internal} import mill.bsp.{BspContext, BspServerResult} @@ -169,6 +169,7 @@ 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) val (success, nextStateCache) = { if (config.repl.value) { @@ -179,15 +180,14 @@ object MillMain { logger.error("A target must be provided.") (false, stateCache) + } else if (maybeThreadCount.isFailure) { + logger.error(maybeThreadCount.failed.get.getMessage) + (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.get) if (mill.main.client.Util.isJava9OrAbove) { val rt = config.home / Export.rtJarName @@ -275,6 +275,27 @@ object MillMain { } } + private def parseThreadCount(threadCountRaw: Option[String]): Try[Int] = { + val cores = Runtime.getRuntime.availableProcessors() + 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) + } + } + def getLogger( streams: SystemStreams, config: MillCliConfig, From 1de500eeafa620161cba0a512edcfc3fb3791f75 Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Mon, 16 Sep 2024 07:40:43 +0800 Subject: [PATCH 2/9] format --- runner/src/mill/runner/MillMain.scala | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/runner/src/mill/runner/MillMain.scala b/runner/src/mill/runner/MillMain.scala index 98a82b55f98d..8e20da473f30 100644 --- a/runner/src/mill/runner/MillMain.scala +++ b/runner/src/mill/runner/MillMain.scala @@ -278,17 +278,18 @@ object MillMain { private def parseThreadCount(threadCountRaw: Option[String]): Try[Int] = { val cores = Runtime.getRuntime.availableProcessors() def err(detail: String) = new Exception( - s"Invalid value \"${threadCountRaw.getOrElse("")}\" for flag -j/--jobs: $detail") + 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) + .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 - _) + .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"))) + .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 From 4f07945895c099874b8692d4155d26c0aa001aad Mon Sep 17 00:00:00 2001 From: Bin Wang Date: Sun, 15 Sep 2024 21:07:15 -0400 Subject: [PATCH 3/9] change try to either and make cores a param --- runner/src/mill/runner/MillMain.scala | 41 +++++++++++++-------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/runner/src/mill/runner/MillMain.scala b/runner/src/mill/runner/MillMain.scala index 8e20da473f30..13f7482fd2b7 100644 --- a/runner/src/mill/runner/MillMain.scala +++ b/runner/src/mill/runner/MillMain.scala @@ -3,7 +3,7 @@ package mill.runner import java.io.{FileOutputStream, PipedInputStream, PrintStream} import java.util.Locale import scala.jdk.CollectionConverters._ -import scala.util.{Failure, Properties, Success, Try} +import scala.util.Properties import mill.java9rtexport.Export import mill.api.{MillException, SystemStreams, WorkspaceRoot, internal} import mill.bsp.{BspContext, BspServerResult} @@ -169,7 +169,7 @@ 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) + val maybeThreadCount = parseThreadCount(config.threadCountRaw, Runtime.getRuntime.availableProcessors()) val (success, nextStateCache) = { if (config.repl.value) { @@ -180,14 +180,15 @@ object MillMain { logger.error("A target must be provided.") (false, stateCache) - } else if (maybeThreadCount.isFailure) { - logger.error(maybeThreadCount.failed.get.getMessage) + } else if (maybeThreadCount.isLeft) { + logger.error(maybeThreadCount.swap.toOption.get) (false, stateCache) + } else { val userSpecifiedProperties = userSpecifiedProperties0 ++ config.extraSystemProperties - val threadCount = Some(maybeThreadCount.get) + val threadCount = Some(maybeThreadCount.toOption.get) if (mill.main.client.Util.isJava9OrAbove) { val rt = config.home / Export.rtJarName @@ -275,25 +276,21 @@ object MillMain { } } - private def parseThreadCount(threadCountRaw: Option[String]): Try[Int] = { - val cores = Runtime.getRuntime.availableProcessors() - def err(detail: String) = new Exception( - s"Invalid value \"${threadCountRaw.getOrElse("")}\" for flag -j/--jobs: $detail" - ) + private 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 => 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"))) + 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 => Failure(err("Calculated cores to use is a negative number.")) - case 0 => Success(cores) // use all cores if 0 - case x => Success(x) + case x if x <= 0 => Left(err("Calculated cores to use should be a positive number.")) + case x => Right(x) } } From 756c0c0ef376e9e8261dc018a2743e5654a73a4a Mon Sep 17 00:00:00 2001 From: Bin Wang Date: Sun, 15 Sep 2024 21:57:04 -0400 Subject: [PATCH 4/9] add tests --- runner/src/mill/runner/MillMain.scala | 2 +- .../test/src/mill/runner/MillMainTests.scala | 76 +++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 runner/test/src/mill/runner/MillMainTests.scala diff --git a/runner/src/mill/runner/MillMain.scala b/runner/src/mill/runner/MillMain.scala index 13f7482fd2b7..b08a75c61daf 100644 --- a/runner/src/mill/runner/MillMain.scala +++ b/runner/src/mill/runner/MillMain.scala @@ -276,7 +276,7 @@ object MillMain { } } - private def parseThreadCount(threadCountRaw: Option[String], availableCores: Int): Either[String, Int] = { + 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) diff --git a/runner/test/src/mill/runner/MillMainTests.scala b/runner/test/src/mill/runner/MillMainTests.scala new file mode 100644 index 000000000000..78673bf05940 --- /dev/null +++ b/runner/test/src/mill/runner/MillMainTests.scala @@ -0,0 +1,76 @@ +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") + 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") + } + + } + + } +} From d3129be035d678aa97423c5fd6aa8474a84fc9d1 Mon Sep 17 00:00:00 2001 From: Bin Wang Date: Sun, 15 Sep 2024 22:10:24 -0400 Subject: [PATCH 5/9] format file --- runner/src/mill/runner/MillMain.scala | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/runner/src/mill/runner/MillMain.scala b/runner/src/mill/runner/MillMain.scala index b08a75c61daf..a7d139acf363 100644 --- a/runner/src/mill/runner/MillMain.scala +++ b/runner/src/mill/runner/MillMain.scala @@ -169,7 +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 maybeThreadCount = + parseThreadCount(config.threadCountRaw, Runtime.getRuntime.availableProcessors()) val (success, nextStateCache) = { if (config.repl.value) { @@ -276,8 +277,12 @@ 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" + 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 From cf0511adce8263555c54b7c80efccc1109b9c19a Mon Sep 17 00:00:00 2001 From: Bin Wang Date: Sun, 15 Sep 2024 22:50:41 -0400 Subject: [PATCH 6/9] format files --- .../test/src/mill/runner/MillMainTests.scala | 77 ++++++++++++------- 1 file changed, 48 insertions(+), 29 deletions(-) diff --git a/runner/test/src/mill/runner/MillMainTests.scala b/runner/test/src/mill/runner/MillMainTests.scala index 78673bf05940..7883f84e85cc 100644 --- a/runner/test/src/mill/runner/MillMainTests.scala +++ b/runner/test/src/mill/runner/MillMainTests.scala @@ -21,16 +21,17 @@ object MillMainTests extends TestSuite { 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.") + 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") { @@ -41,33 +42,51 @@ object MillMainTests extends TestSuite { 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") - 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\"") + assertParseErr( + MillMain.parseThreadCount(Some("0.09C"), 10), + "Calculated cores to use should be a positive number" + ) + 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\"") + 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") + 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" + ) } } From 25e67611e6ed91de4b7f0a98f567a3d566c25c5a Mon Sep 17 00:00:00 2001 From: Bin Wang Date: Sun, 15 Sep 2024 23:50:43 -0400 Subject: [PATCH 7/9] round up to 1 if calculated double is smaller than 1 --- runner/src/mill/runner/MillMain.scala | 6 +++++- runner/test/src/mill/runner/MillMainTests.scala | 5 +---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/runner/src/mill/runner/MillMain.scala b/runner/src/mill/runner/MillMain.scala index a7d139acf363..20b2372c6eec 100644 --- a/runner/src/mill/runner/MillMain.scala +++ b/runner/src/mill/runner/MillMain.scala @@ -287,7 +287,11 @@ object MillMain { 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) + .map(_ * availableCores) + .map { + case x if x > 0 && x < 1 => 1 + case x => x.toInt + } case Some(s"C-${n}") => n.toIntOption .toRight(err("Failed to find a int number after \"C-\".")) .map(availableCores - _) diff --git a/runner/test/src/mill/runner/MillMainTests.scala b/runner/test/src/mill/runner/MillMainTests.scala index 7883f84e85cc..3c0e4fab0a07 100644 --- a/runner/test/src/mill/runner/MillMainTests.scala +++ b/runner/test/src/mill/runner/MillMainTests.scala @@ -41,11 +41,8 @@ object MillMainTests extends TestSuite { 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)) + assert(MillMain.parseThreadCount(Some("0.09C"), 10) == Right(1)) - assertParseErr( - MillMain.parseThreadCount(Some("0.09C"), 10), - "Calculated cores to use should be a positive number" - ) assertParseErr( MillMain.parseThreadCount(Some("-0.5C"), 10), "Calculated cores to use should be a positive number" From 619118aff154ce1e71e1dbfb6384a6856b97e073 Mon Sep 17 00:00:00 2001 From: Bin Wang Date: Mon, 16 Sep 2024 00:10:02 -0400 Subject: [PATCH 8/9] better handle computed 0 and neg num --- runner/src/mill/runner/MillCliConfig.scala | 2 ++ runner/src/mill/runner/MillMain.scala | 9 +++---- .../test/src/mill/runner/MillMainTests.scala | 27 ++++--------------- 3 files changed, 10 insertions(+), 28 deletions(-) diff --git a/runner/src/mill/runner/MillCliConfig.scala b/runner/src/mill/runner/MillCliConfig.scala index 0b337b18bc3a..21c93a72bc10 100644 --- a/runner/src/mill/runner/MillCliConfig.scala +++ b/runner/src/mill/runner/MillCliConfig.scala @@ -70,6 +70,8 @@ class MillCliConfig private ( 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. + If calculated result is less than 1, will round up to 1. This doesn't include pass in 0 directly. + Use 1 to disable parallel and 0 to use as much threads as available processors.""" ) val threadCountRaw: Option[String], diff --git a/runner/src/mill/runner/MillMain.scala b/runner/src/mill/runner/MillMain.scala index 20b2372c6eec..a6a89891690e 100644 --- a/runner/src/mill/runner/MillMain.scala +++ b/runner/src/mill/runner/MillMain.scala @@ -285,20 +285,17 @@ object MillMain { s"Invalid value \"${threadCountRaw.getOrElse("")}\" for flag -j/--jobs: $detail" (threadCountRaw match { case None => Right(availableCores) + case Some("0") => Right(availableCores) case Some(s"${n}C") => n.toDoubleOption .toRight(err("Failed to find a float number before \"C\".")) - .map(_ * availableCores) - .map { - case x if x > 0 && x < 1 => 1 - case x => x.toInt - } + .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 if x < 1 => Right(1) case x => Right(x) } } diff --git a/runner/test/src/mill/runner/MillMainTests.scala b/runner/test/src/mill/runner/MillMainTests.scala index 3c0e4fab0a07..7c8f7d71a4a0 100644 --- a/runner/test/src/mill/runner/MillMainTests.scala +++ b/runner/test/src/mill/runner/MillMainTests.scala @@ -24,14 +24,8 @@ object MillMainTests extends TestSuite { 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." - ) + assert(MillMain.parseThreadCount(Some("0"), 10) == Right(10)) + assert(MillMain.parseThreadCount(Some("-1"), 10) == Right(1)) } test("parse fraction number") { @@ -42,11 +36,7 @@ object MillMainTests extends TestSuite { assert(MillMain.parseThreadCount(Some("1.0C"), 10) == Right(10)) assert(MillMain.parseThreadCount(Some("1.5C"), 10) == Right(15)) assert(MillMain.parseThreadCount(Some("0.09C"), 10) == Right(1)) - - assertParseErr( - MillMain.parseThreadCount(Some("-0.5C"), 10), - "Calculated cores to use should be a positive number" - ) + assert(MillMain.parseThreadCount(Some("-0.5C"), 10) == Right(1)) assertParseErr( MillMain.parseThreadCount(Some("0.5.4C"), 10), "Failed to find a float number before \"C\"" @@ -55,15 +45,8 @@ object MillMainTests extends TestSuite { 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." - ) + assert(MillMain.parseThreadCount(Some("C-10"), 10) == Right(1)) + assert(MillMain.parseThreadCount(Some("C-11"), 10) == Right(1)) assertParseErr( MillMain.parseThreadCount(Some("C-1.1"), 10), From fb8a2238a59f3edca919be0e07a67f13766fe3cc Mon Sep 17 00:00:00 2001 From: Bin Wang Date: Mon, 16 Sep 2024 00:43:34 -0400 Subject: [PATCH 9/9] cleanup map --- runner/src/mill/runner/MillMain.scala | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/runner/src/mill/runner/MillMain.scala b/runner/src/mill/runner/MillMain.scala index a6a89891690e..c60b62ddcf9d 100644 --- a/runner/src/mill/runner/MillMain.scala +++ b/runner/src/mill/runner/MillMain.scala @@ -294,10 +294,7 @@ object MillMain { .map(availableCores - _) case Some(n) => n.toIntOption .toRight(err("Failed to find a int number")) - }).flatMap { - case x if x < 1 => Right(1) - case x => Right(x) - } + }).map { x => if (x < 1) 1 else x } } def getLogger(