Skip to content

Commit 351f801

Browse files
committed
Finalize BuildFusService parameters to avoid CME
#KTI-1611 (cherry picked from commit aa0aa60)
1 parent 275a1b5 commit 351f801

File tree

8 files changed

+91
-23
lines changed

8 files changed

+91
-23
lines changed

libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/internal/stdlibDependencyManagement.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ import org.jetbrains.kotlin.gradle.plugin.sources.KotlinDependencyScope
2222
import org.jetbrains.kotlin.gradle.plugin.sources.android.AndroidBaseSourceSetName
2323
import org.jetbrains.kotlin.gradle.plugin.sources.android.AndroidVariantType
2424
import org.jetbrains.kotlin.gradle.plugin.sources.android.androidSourceSetInfoOrNull
25-
import org.jetbrains.kotlin.gradle.plugin.sources.internal
2625
import org.jetbrains.kotlin.gradle.plugin.sources.sourceSetDependencyConfigurationByScope
27-
import org.jetbrains.kotlin.gradle.targets.js.KotlinWasmTargetType
28-
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTarget
26+
import org.jetbrains.kotlin.gradle.plugin.statistics.KotlinStdlibConfigurationMetrics
2927
import org.jetbrains.kotlin.gradle.targets.js.npm.SemVer
3028
import org.jetbrains.kotlin.gradle.utils.forAllTargets
3129
import org.jetbrains.kotlin.gradle.utils.withType
@@ -136,6 +134,8 @@ private fun KotlinTarget.addStdlibDependency(
136134
val stdlibModule = compilation.platformType.stdlibPlatformType(this, kotlinSourceSet, stdlibVersion >= kotlin1920Version)
137135
?: return@withDependencies
138136

137+
KotlinStdlibConfigurationMetrics.collectMetrics(project, requestedStdlibVersion)
138+
139139
dependencySet.addLater(
140140
coreLibrariesVersion.map {
141141
dependencies.kotlinDependency(stdlibModule, it)

libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/registerKotlinPluginExtensions.kt

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.resources.publication.SetUpMultipl
2525
import org.jetbrains.kotlin.gradle.plugin.mpp.resources.RegisterMultiplatformResourcesPublicationExtensionAction
2626
import org.jetbrains.kotlin.gradle.plugin.sources.KotlinMultiplatformSourceSetSetupAction
2727
import org.jetbrains.kotlin.gradle.plugin.sources.LanguageSettingsSetupAction
28+
import org.jetbrains.kotlin.gradle.plugin.statistics.FinalizeConfigurationFusMetricAction
2829
import org.jetbrains.kotlin.gradle.plugin.statistics.MultiplatformBuildStatsReportSetupAction
2930
import org.jetbrains.kotlin.gradle.scripting.internal.ScriptingGradleSubpluginSetupAction
3031
import org.jetbrains.kotlin.gradle.targets.*
@@ -55,6 +56,7 @@ internal fun Project.registerKotlinPluginExtensions() {
5556
register(project, CustomizeKotlinDependenciesSetupAction)
5657
register(project, AddKotlinPlatformIntegersSupportLibrary)
5758
register(project, SetupKotlinNativePlatformDependenciesForLegacyImport)
59+
register(project, FinalizeConfigurationFusMetricAction)
5860

5961

6062
if (isJvm || isMultiplatform) {

libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/statistics/BuildFusService.kt

+5-12
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ abstract class BuildFusService : BuildService<BuildFusService.Parameters>, AutoC
5656
}
5757

5858
interface Parameters : BuildServiceParameters {
59+
val generalConfigurationMetrics: Property<MetricContainer>
5960
val configurationMetrics: ListProperty<MetricContainer>
6061
val useBuildFinishFlowAction: Property<Boolean>
6162
val buildStatisticsConfiguration: Property<KotlinBuildStatsConfiguration>
@@ -94,13 +95,9 @@ abstract class BuildFusService : BuildService<BuildFusService.Parameters>, AutoC
9495
val isConfigurationCacheRequested = project.isConfigurationCacheRequested
9596
val isProjectIsolationRequested = project.isProjectIsolationRequested
9697

97-
project.gradle.sharedServices.registrations.findByName(serviceName)?.let {
98-
(it.parameters as Parameters).configurationMetrics.add(
99-
project.provider {
100-
KotlinProjectConfigurationMetrics.collectMetrics(project)
101-
}
98+
ProjectConfigurationFusService.registerIfAbsent(project)
10299

103-
)
100+
project.gradle.sharedServices.registrations.findByName(serviceName)?.let {
104101
@Suppress("UNCHECKED_CAST")
105102
return (it.service as Provider<BuildFusService>)
106103
}
@@ -116,7 +113,7 @@ abstract class BuildFusService : BuildService<BuildFusService.Parameters>, AutoC
116113
// when this OperationCompletionListener is called services can be already closed for Gradle 8,
117114
// so there is a change that no VariantImplementationFactory will be found
118115
return gradle.sharedServices.registerIfAbsent(serviceName, BuildFusService::class.java) { spec ->
119-
spec.parameters.configurationMetrics.add(project.provider {
116+
spec.parameters.generalConfigurationMetrics.set(project.provider {
120117
//isProjectIsolationEnabled isConfigurationCacheRequested and isProjectIsolationRequested should be calculated beforehand
121118
// because since Gradle 8.0 provider's calculation is made in BuildFinishFlowAction
122119
// and VariantImplementationFactories is not initialized at that moment
@@ -131,11 +128,6 @@ abstract class BuildFusService : BuildService<BuildFusService.Parameters>, AutoC
131128
isConfigurationCacheRequested
132129
)
133130
})
134-
spec.parameters.configurationMetrics.add(
135-
project.provider {
136-
collectProjectConfigurationTimeMetrics(project)
137-
}
138-
)
139131
spec.parameters.useBuildFinishFlowAction.set(GradleVersion.current().baseVersion >= GradleVersion.version("8.1"))
140132
spec.parameters.buildStatisticsConfiguration.set(KotlinBuildStatsConfiguration(project))
141133
}.also { buildService ->
@@ -182,6 +174,7 @@ abstract class BuildFusService : BuildService<BuildFusService.Parameters>, AutoC
182174
internal fun recordBuildFinished(buildFailed: Boolean) {
183175
BuildFinishMetrics.collectMetrics(log, buildFailed, buildStartTime, projectEvaluatedTime, fusMetricsConsumer)
184176
parameters.configurationMetrics.orElse(emptyList()).get().forEach { it.addToConsumer(fusMetricsConsumer) }
177+
parameters.generalConfigurationMetrics.orNull?.addToConsumer(fusMetricsConsumer)
185178
parameters.buildStatisticsConfiguration.orNull?.also {
186179
val loggerService = KotlinBuildStatsLoggerService(it)
187180
loggerService.initSessionLogger(buildId)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
4+
*/
5+
6+
package org.jetbrains.kotlin.gradle.plugin.statistics
7+
8+
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginLifecycle
9+
import org.jetbrains.kotlin.gradle.plugin.KotlinProjectSetupCoroutine
10+
import org.jetbrains.kotlin.gradle.plugin.await
11+
12+
13+
internal val FinalizeConfigurationFusMetricAction = KotlinProjectSetupCoroutine {
14+
KotlinPluginLifecycle.Stage.ReadyForExecution.await()
15+
16+
val projectConfigurationService =
17+
project.gradle.sharedServices.registrations.findByName(ProjectConfigurationFusService.getServiceName(project))?.also {
18+
val parameters = it.parameters as ProjectConfigurationFusService.Parameters
19+
parameters.configurationMetrics.finalizeValue()
20+
}
21+
22+
project.gradle.sharedServices.registrations.findByName(BuildFusService.serviceName)?.also {
23+
val parameters = it.parameters as BuildFusService.Parameters
24+
parameters.generalConfigurationMetrics.finalizeValue()
25+
projectConfigurationService?.service?.orNull?.also {
26+
parameters.configurationMetrics.add((it.parameters as ProjectConfigurationFusService.Parameters).configurationMetrics)
27+
}
28+
}
29+
}

libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/statistics/FusMetrics.kt

+8
Original file line numberDiff line numberDiff line change
@@ -280,4 +280,12 @@ internal object NativeLinkTaskMetrics : FusMetrics {
280280
it.put(BooleanMetrics.KOTLIN_INCREMENTAL_NATIVE_ENABLED, project.isKonanIncrementalCompilationEnabled())
281281
}
282282
}
283+
}
284+
285+
internal object KotlinStdlibConfigurationMetrics : FusMetrics {
286+
internal fun collectMetrics(project: Project, requestedStdlibVersion: String, ) {
287+
project.addConfigurationMetrics {
288+
it.put(StringMetrics.KOTLIN_STDLIB_VERSION, requestedStdlibVersion)
289+
}
290+
}
283291
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
4+
*/
5+
6+
package org.jetbrains.kotlin.gradle.plugin.statistics
7+
8+
import org.gradle.api.Project
9+
import org.gradle.api.provider.Property
10+
import org.gradle.api.provider.Provider
11+
import org.gradle.api.services.BuildService
12+
import org.gradle.api.services.BuildServiceParameters
13+
14+
abstract class ProjectConfigurationFusService : BuildService<ProjectConfigurationFusService.Parameters>, AutoCloseable {
15+
interface Parameters : BuildServiceParameters {
16+
val configurationMetrics: Property<MetricContainer>
17+
}
18+
19+
companion object {
20+
fun getServiceName(project: Project): String =
21+
"${project.path}_${ProjectConfigurationFusService::class.simpleName}_${ProjectConfigurationFusService::class.java.classLoader.hashCode()}"
22+
23+
//register a new service for every project to collect configuration metrics
24+
fun registerIfAbsent(project: Project): Provider<ProjectConfigurationFusService> {
25+
return project.gradle.sharedServices.registerIfAbsent(
26+
getServiceName(project),
27+
ProjectConfigurationFusService::class.java
28+
) { spec ->
29+
spec.parameters.configurationMetrics.set(project.provider {
30+
KotlinProjectConfigurationMetrics.collectMetrics(project)
31+
})
32+
}
33+
}
34+
}
35+
36+
override fun close() {
37+
}
38+
}

libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/statistics/kotlinBuildStatisticsUtils.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ private fun collectAppliedPluginsStatistics(
172172
configurationTimeMetrics: MetricContainer,
173173
) {
174174
for (plugin in ObservablePlugins.values()) {
175-
project.plugins.withId(plugin.title) {
175+
if (project.plugins.hasPlugin(plugin.title)) {
176176
configurationTimeMetrics.put(plugin.metric, true)
177177
}
178178
}

libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/ir/KotlinJsIrTargetPreset.kt

+5-7
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,11 @@ open class KotlinJsIrTargetPreset(
3030
override fun instantiateTarget(name: String): KotlinJsIrTarget {
3131
return project.objects.newInstance(KotlinJsIrTarget::class.java, project, platformType).apply {
3232
this.isMpp = this@KotlinJsIrTargetPreset.isMpp
33-
project.runProjectConfigurationHealthCheckWhenEvaluated {
34-
KotlinJsIrTargetMetrics.collectMetrics(
35-
isBrowserConfigured = isBrowserConfigured,
36-
isNodejsConfigured = isNodejsConfigured,
37-
project
38-
)
39-
}
33+
KotlinJsIrTargetMetrics.collectMetrics(
34+
isBrowserConfigured = isBrowserConfigured,
35+
isNodejsConfigured = isNodejsConfigured,
36+
project
37+
)
4038
}
4139
}
4240

0 commit comments

Comments
 (0)