Skip to content

Commit

Permalink
[K/N][build] Use semaphores to set platform libs building parallelism.
Browse files Browse the repository at this point in the history
* Instead of optionally using `WorkAction` inside `KonanInteropTask`,
  always use `WorkAction`s.
* Configure parallelism explicitly by attaching a `BuildService`
  to the created tasks.
* Also do the same for platform libs cache building tasks.

^KT-70990
  • Loading branch information
projedi authored and qodana-bot committed Sep 24, 2024
1 parent 3689805 commit e448d67
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.services.ServiceReference
import org.gradle.api.tasks.*
import org.gradle.kotlin.dsl.property
import org.gradle.process.ExecOperations
import org.gradle.workers.WorkAction
import org.gradle.workers.WorkParameters
Expand Down Expand Up @@ -48,9 +47,6 @@ abstract class KonanInteropTask @Inject constructor(
@get:Input
abstract val extraOpts: ListProperty<String>

@get:Internal
val enableParallel: Property<Boolean> = project.objects.property<Boolean>().convention(false)

@get:InputFile
abstract val defFile: RegularFileProperty

Expand Down Expand Up @@ -113,15 +109,11 @@ abstract class KonanInteropTask @Inject constructor(

addAll(extraOpts.get())
}
if (enableParallel.get()) {
val workQueue = workerExecutor.noIsolation()
interchangeBox[this.path] = interopRunner
workQueue.submit(RunTool::class.java) {
taskName = path
this.args = args
}
} else {
interopRunner.run(args)
val workQueue = workerExecutor.noIsolation()
interchangeBox[this.path] = interopRunner
workQueue.submit(RunTool::class.java) {
taskName = path
this.args = args
}
}
}
Expand Down
20 changes: 19 additions & 1 deletion kotlin-native/platformLibs/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ fun defFileToLibName(target: String, name: String) = "$target-$name"

private fun interopTaskName(libName: String, targetName: String) = "compileKonan${libName.capitalized}${targetName.capitalized}"

private abstract class CompilePlatformLibsSemaphore : BuildService<BuildServiceParameters.None>
private abstract class CachePlatformLibsSemaphore : BuildService<BuildServiceParameters.None>

private val compilePlatformLibsSemaphore = gradle.sharedServices.registerIfAbsent("compilePlatformLibsSemaphore", CompilePlatformLibsSemaphore::class.java) {
if (kotlinBuildProperties.limitPlatformLibsCompilationConcurrency) {
maxParallelUsages.set(1)
}
}

private val cachePlatformLibsSemaphore = gradle.sharedServices.registerIfAbsent("cachePlatformLibsSemaphore", CachePlatformLibsSemaphore::class.java) {
if (kotlinBuildProperties.limitPlatformLibsCacheBuildingConcurrency) {
maxParallelUsages.set(1)
}
}

// endregion

if (HostManager.host == KonanTarget.MACOS_ARM64) {
Expand Down Expand Up @@ -77,7 +92,8 @@ enabledTargets(platformManager).forEach { target ->
this.compilerOpts.addAll(
"-fmodules-cache-path=${project.layout.buildDirectory.dir("clangModulesCache").get().asFile}"
)
this.enableParallel.set(project.getBooleanProperty("kotlin.native.platformLibs.parallel") ?: true)

usesService(compilePlatformLibsSemaphore)
}

val klibInstallTask = tasks.register(libName, Sync::class.java) {
Expand All @@ -103,6 +119,8 @@ enabledTargets(platformManager).forEach { target ->
dependsOn(it)
dependsOn("${it}Cache")
}

usesService(cachePlatformLibsSemaphore)
}
cacheTasks.add(cacheTask)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,20 @@ val KotlinBuildProperties.limitTestTasksConcurrency: Boolean
get() = getBoolean("kotlin.build.limitTestTasksConcurrency", true)

val KotlinBuildProperties.konanDataDir: String?
get() = getOrNull("konan.data.dir") as String?
get() = getOrNull("konan.data.dir") as String?

/**
* If `true`, `:kotlin-native:platformLibs` will compile platform libraries klibs without parallelism.
*/
val KotlinBuildProperties.limitPlatformLibsCompilationConcurrency: Boolean
get() = !getBoolean("kotlin.native.platformLibs.parallel", true)


/**
* If `true`, `:kotlin-native:platformLibs` will build platform libraries caches without parallelism.
*/
val KotlinBuildProperties.limitPlatformLibsCacheBuildingConcurrency: Boolean
get() {
// if platform libs compilation parallelism is disabled, also disable parallel cache building by default.
return !getBoolean("kotlin.native.platformLibs.parallelCaches", !limitPlatformLibsCompilationConcurrency)
}

0 comments on commit e448d67

Please sign in to comment.