Why would we want to limitedParallelism? Is due to the following question: if you use up all threads will operating system (OS) create cooperative environment for other processes that are running?.
Experiment aimed to roughly answer this
Experiment: load up CPU and try to use other apps in the meantime.
When I tried this on 16-core/32-thread AMD Ryzen 9 7945HX on Linux it seems at least Linux OS was creating a cooperative environment between the threads to not show perceivable slow down in other processes while the CPU was shown to be running at 100% per htop. Hence, as of now I don’t think it’s worth artificially lowering the parallelism and rather let OS take care of scheduling threads.
code-example loading up CPU for 30 seconds
GT-Sandbox-Snapshot: example loading up all the CPU cores for 30 seconds doing computation in a loop
Code
package com.glassthought.sandboximport kotlinx.coroutines.*import kotlin.system.measureTimeMillisfun main(): Unit = runBlocking(CoroutineName("main-runBlocking")) { val availableProcessors = Runtime.getRuntime().availableProcessors() println("Starting CPU stress test on $availableProcessors cores...") println("This will run for 30 seconds. System may become less responsive.") val duration = 30_000L // 30 seconds in milliseconds val elapsedTime = measureTimeMillis { // Create a list of jobs, one for each processor val jobs = List(availableProcessors) { coreIndex -> // Use Dispatchers.Default which has a thread pool sized to CPU cores // Each coroutine will pin to a thread and consume CPU launch(Dispatchers.Default + CoroutineName("cpu-loader-$coreIndex")) { println("Core $coreIndex: Starting intensive computation...") val startTime = System.currentTimeMillis() var counter = 0L var sum = 0.0 // Tight loop that runs for 30 seconds while (System.currentTimeMillis() - startTime < duration) { // Perform some CPU-intensive operations counter++ sum += Math.sqrt(counter.toDouble()) // Add some more operations to ensure CPU usage if (counter % 1000000 == 0L) { // Occasionally perform more complex calculations for (i in 1..100) { sum += Math.sin(sum) * Math.cos(counter.toDouble()) } } // Check for cancellation periodically (every million iterations) if (counter % 10000000 == 0L) { yield() // Allow coroutine cancellation if needed } } println("Core $coreIndex: Completed. Counter=$counter, Sum=$sum") } } // Wait for all jobs to complete jobs.joinAll() } println("\nCPU stress test completed in ${elapsedTime}ms") println("All $availableProcessors cores were loaded for approximately 30 seconds")}
Command to reproduce:
gt.sandbox.checkout.commit 552c189a3d508ae510c9 \&& cd "${GT_SANDBOX_REPO}" \&& cmd.run.announce "./gradlew run --quiet"