Skip to content

Commit

Permalink
Add fus metric for GC native compiler argument
Browse files Browse the repository at this point in the history
#KT-70022 Verification Pending
  • Loading branch information
nav-nav authored and qodana-bot committed Sep 25, 2024
1 parent 6534ca9 commit 4384d14
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.condition.OS
import java.nio.file.Files
import java.nio.file.Path
import kotlin.io.path.appendText
import kotlin.io.path.deleteIfExists
import kotlin.io.path.writeText
import kotlin.streams.toList
Expand Down Expand Up @@ -396,6 +397,32 @@ class FusStatisticsIT : KGPBaseTest() {
}
}

@DisplayName("native compiler arguments")
@GradleTest
@NativeGradlePluginTests
fun testNativeCompilerArguments(gradleVersion: GradleVersion) {
nativeProject("native-incremental-simple", gradleVersion) {
buildGradleKts.appendText(
"""
|
|kotlin {
| tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinNativeCompile>().configureEach {
| kotlinOptions {
| freeCompilerArgs += listOf("-Xbinary=gc=noop")
| }
| }
|}
""".trimMargin())

build("linkDebugExecutableHost", "-Pkotlin.session.logger.root.path=$projectPath") {
assertFileContains(
fusStatisticsPath,
"ENABLED_NOOP_GC=true",
)
}
}
}

private fun TestProject.applyDokka() {
buildGradle.replaceText(
"plugins {",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6423,7 +6423,7 @@ public final class org/jetbrains/kotlin/gradle/targets/native/tasks/artifact/Kot
public fun registerAssembleTask (Lorg/gradle/api/Project;)V
}

public abstract class org/jetbrains/kotlin/gradle/targets/native/tasks/artifact/KotlinNativeLinkArtifactTask : org/gradle/api/DefaultTask, org/jetbrains/kotlin/gradle/internal/UsesClassLoadersCachingBuildService, org/jetbrains/kotlin/gradle/report/UsesBuildMetricsService, org/jetbrains/kotlin/gradle/targets/native/UsesKonanPropertiesBuildService, org/jetbrains/kotlin/gradle/targets/native/toolchain/UsesKotlinNativeBundleBuildService, org/jetbrains/kotlin/gradle/tasks/KotlinToolTask {
public abstract class org/jetbrains/kotlin/gradle/targets/native/tasks/artifact/KotlinNativeLinkArtifactTask : org/gradle/api/DefaultTask, org/jetbrains/kotlin/gradle/internal/UsesClassLoadersCachingBuildService, org/jetbrains/kotlin/gradle/plugin/statistics/UsesBuildFusService, org/jetbrains/kotlin/gradle/report/UsesBuildMetricsService, org/jetbrains/kotlin/gradle/targets/native/UsesKonanPropertiesBuildService, org/jetbrains/kotlin/gradle/targets/native/toolchain/UsesKotlinNativeBundleBuildService, org/jetbrains/kotlin/gradle/tasks/KotlinToolTask {
public fun <init> (Lorg/jetbrains/kotlin/konan/target/KonanTarget;Lorg/jetbrains/kotlin/konan/target/CompilerOutputKind;Lorg/gradle/api/model/ObjectFactory;Lorg/gradle/api/file/ProjectLayout;)V
public final fun getAllWarningsAsErrors ()Z
public abstract fun getBaseName ()Lorg/gradle/api/provider/Property;
Expand Down Expand Up @@ -6868,7 +6868,7 @@ public abstract class org/jetbrains/kotlin/gradle/tasks/KotlinNativeCompile : or
public fun setupCompilerArgs (Lorg/jetbrains/kotlin/cli/common/arguments/K2NativeCompilerArguments;ZZ)V
}

public abstract class org/jetbrains/kotlin/gradle/tasks/KotlinNativeLink : org/jetbrains/kotlin/gradle/tasks/AbstractKotlinCompileTool, org/jetbrains/kotlin/gradle/internal/UsesClassLoadersCachingBuildService, org/jetbrains/kotlin/gradle/report/UsesBuildMetricsService, org/jetbrains/kotlin/gradle/targets/native/UsesKonanPropertiesBuildService, org/jetbrains/kotlin/gradle/targets/native/toolchain/UsesKotlinNativeBundleBuildService, org/jetbrains/kotlin/gradle/tasks/KotlinToolTask {
public abstract class org/jetbrains/kotlin/gradle/tasks/KotlinNativeLink : org/jetbrains/kotlin/gradle/tasks/AbstractKotlinCompileTool, org/jetbrains/kotlin/gradle/internal/UsesClassLoadersCachingBuildService, org/jetbrains/kotlin/gradle/plugin/statistics/UsesBuildFusService, org/jetbrains/kotlin/gradle/report/UsesBuildMetricsService, org/jetbrains/kotlin/gradle/targets/native/UsesKonanPropertiesBuildService, org/jetbrains/kotlin/gradle/targets/native/toolchain/UsesKotlinNativeBundleBuildService, org/jetbrains/kotlin/gradle/tasks/KotlinToolTask {
public fun <init> (Lorg/jetbrains/kotlin/gradle/plugin/mpp/NativeBinary;Lorg/gradle/api/model/ObjectFactory;)V
public final fun compile ()V
public synthetic fun createCompilerArguments (Lorg/jetbrains/kotlin/gradle/plugin/KotlinCompilerArgumentsProducer$CreateCompilerArgumentsContext;)Lorg/jetbrains/kotlin/cli/common/arguments/CommonToolArguments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.jetbrains.kotlin.gradle.internal.ClassLoadersCachingBuildService
import org.jetbrains.kotlin.gradle.internal.KOTLIN_MODULE_GROUP
import org.jetbrains.kotlin.gradle.plugin.KLIB_COMMONIZER_CLASSPATH_CONFIGURATION_NAME
import org.jetbrains.kotlin.gradle.plugin.getKotlinPluginVersion
import org.jetbrains.kotlin.gradle.plugin.statistics.BuildFusService
import org.jetbrains.kotlin.gradle.plugin.usageByName
import org.jetbrains.kotlin.gradle.utils.*

Expand Down Expand Up @@ -71,7 +72,8 @@ internal fun ObjectFactory.KotlinNativeCommonizerToolRunner(
kotlinNativeCommonizerToolSpec(
toolClasspath,
toolJvmArgs,
)
),
property(BuildFusService::class.java)
)

private fun ObjectFactory.kotlinNativeCommonizerToolSpec(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,27 @@ internal object CompilerArgumentMetrics : FusMetrics {

}

internal object NativeArgumentMetrics : FusMetrics {
fun collectMetrics(compilerArguments: List<String>, metricsConsumer: StatisticsValuesConsumer) {
val arguments = K2NativeCompilerArguments()
parseCommandLineArguments(compilerArguments, arguments)

arguments.binaryOptions
?.filter { it.startsWith("gc=") }
?.map { it.substring("gc=".length) }
?.mapNotNull {
//Values are connected to [org.jetbrains.kotlin.backend.konan.GC], but the class can't be access from here
when (it) {
"noop" -> BooleanMetrics.ENABLED_NOOP_GC
"stwms" -> BooleanMetrics.ENABLED_STWMS_GC
"pmcs" -> BooleanMetrics.ENABLED_PMCS_GC
"cms" -> BooleanMetrics.ENABLED_CMS_GC
else -> null
}
}?.forEach { metricsConsumer.report(it, true) }
}
}

internal object NativeCompilerOptionMetrics : FusMetrics {
fun collectMetrics(compilerOptions: KotlinNativeCompilerOptions, metricsConsumer: StatisticsValuesConsumer) {
metricsConsumer.report(BooleanMetrics.KOTLIN_PROGRESSIVE_MODE, compilerOptions.progressiveMode.get())
Expand Down Expand Up @@ -191,7 +212,7 @@ internal object CompileKotlinTaskMetrics : FusMetrics {
internal fun collectMetrics(
name: String,
compilerOptions: KotlinCommonCompilerOptions,
metricsContainer: StatisticsValuesConsumer
metricsContainer: StatisticsValuesConsumer,
) {
metricsContainer.report(BooleanMetrics.KOTLIN_PROGRESSIVE_MODE, compilerOptions.progressiveMode.get())
compilerOptions.apiVersion.orNull?.also { v ->
Expand Down Expand Up @@ -251,7 +272,7 @@ internal object UrlRepoConfigurationMetrics : FusMetrics {
internal fun collectMetrics(
length: Long,
downloadDuration: Long,
metricsConsumer: StatisticsValuesConsumer
metricsConsumer: StatisticsValuesConsumer,
) {
metricsConsumer.report(NumericalMetrics.ARTIFACTS_DOWNLOAD_SPEED, length * 1000 / downloadDuration)
}
Expand Down Expand Up @@ -291,7 +312,7 @@ internal object NativeLinkTaskMetrics : FusMetrics {
}

internal object KotlinStdlibConfigurationMetrics : FusMetrics {
internal fun collectMetrics(project: Project, requestedStdlibVersion: String, ) {
internal fun collectMetrics(project: Project, requestedStdlibVersion: String) {
project.addConfigurationMetrics {
it.put(StringMetrics.KOTLIN_STDLIB_VERSION, requestedStdlibVersion)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider
import org.jetbrains.kotlin.gradle.plugin.cocoapods.asValidFrameworkName
import org.jetbrains.kotlin.gradle.plugin.mpp.*
import org.jetbrains.kotlin.gradle.plugin.mpp.apple.useXcodeMessageStyle
import org.jetbrains.kotlin.gradle.plugin.statistics.UsesBuildFusService
import org.jetbrains.kotlin.gradle.report.UsesBuildMetricsService
import org.jetbrains.kotlin.gradle.targets.native.UsesKonanPropertiesBuildService
import org.jetbrains.kotlin.gradle.targets.native.internal.*
Expand Down Expand Up @@ -66,7 +67,9 @@ constructor(
UsesBuildMetricsService,
UsesClassLoadersCachingBuildService,
KotlinToolTask<KotlinCommonCompilerToolOptions>,
UsesKotlinNativeBundleBuildService {
UsesKotlinNativeBundleBuildService,
UsesBuildFusService
{

@Deprecated("Visibility will be lifted to private in the future releases")
@get:Internal
Expand All @@ -86,6 +89,7 @@ constructor(
@Suppress("DEPRECATION")
@get:Internal
internal val nativeDependencies = compilation.nativeDependencies

@Suppress("DEPRECATION")
@get:Internal
internal val nativeDistributionDependencies = compilation.nativeDistributionDependencies
Expand Down Expand Up @@ -405,7 +409,8 @@ constructor(
useEmbeddableCompilerJar,
actualNativeHomeDirectory,
runnerJvmArgs,
konanPropertiesService
konanPropertiesService,
buildFusService
)

@TaskAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.Companion.kotlinPro
import org.jetbrains.kotlin.gradle.plugin.cocoapods.asValidFrameworkName
import org.jetbrains.kotlin.gradle.plugin.mpp.*
import org.jetbrains.kotlin.gradle.plugin.mpp.apple.useXcodeMessageStyle
import org.jetbrains.kotlin.gradle.plugin.statistics.BuildFusService
import org.jetbrains.kotlin.gradle.plugin.statistics.NativeCompilerOptionMetrics
import org.jetbrains.kotlin.gradle.plugin.statistics.UsesBuildFusService
import org.jetbrains.kotlin.gradle.report.*
Expand Down Expand Up @@ -457,7 +458,8 @@ internal constructor(
useEmbeddableCompilerJar,
actualNativeHomeDirectory,
runnerJvmArgs,
konanPropertiesService
konanPropertiesService,
buildFusService
)

// endregion.
Expand Down Expand Up @@ -558,7 +560,8 @@ internal constructor(
if (isMetadataCompilation) {
null
} else {
objectFactory.fileCollection().from(KonanDistribution(konanDistribution.get()).platformLibsDir.resolve(konanTarget.name).listLibraryFiles())
objectFactory.fileCollection()
.from(KonanDistribution(konanDistribution.get()).platformLibsDir.resolve(konanTarget.name).listLibraryFiles())
}

private fun File.listLibraryFiles(): List<File> = listFiles().orEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider
import org.jetbrains.kotlin.gradle.plugin.mpp.BITCODE_EMBEDDING_DEPRECATION_MESSAGE
import org.jetbrains.kotlin.gradle.plugin.mpp.BitcodeEmbeddingMode
import org.jetbrains.kotlin.gradle.plugin.mpp.apple.useXcodeMessageStyle
import org.jetbrains.kotlin.gradle.plugin.statistics.UsesBuildFusService
import org.jetbrains.kotlin.gradle.report.GradleBuildMetricsReporter
import org.jetbrains.kotlin.gradle.report.UsesBuildMetricsService
import org.jetbrains.kotlin.gradle.targets.native.UsesKonanPropertiesBuildService
Expand Down Expand Up @@ -61,7 +62,8 @@ abstract class KotlinNativeLinkArtifactTask @Inject constructor(
UsesKotlinNativeBundleBuildService,
UsesClassLoadersCachingBuildService,
UsesKonanPropertiesBuildService,
KotlinToolTask<KotlinCommonCompilerToolOptions> {
KotlinToolTask<KotlinCommonCompilerToolOptions>,
UsesBuildFusService {

@get:Input
abstract val baseName: Property<String>
Expand Down Expand Up @@ -233,7 +235,8 @@ abstract class KotlinNativeLinkArtifactTask @Inject constructor(
shouldUseEmbeddableCompilerJar,
actualNativeHomeDirectory,
runnerJvmArgs,
konanPropertiesService
konanPropertiesService,
buildFusService
)

@TaskAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import org.jetbrains.kotlin.compilerRunner.KotlinCompilerArgumentsLogLevel
import org.jetbrains.kotlin.gradle.internal.ClassLoadersCachingBuildService
import org.jetbrains.kotlin.gradle.internal.ParentClassLoaderProvider
import org.jetbrains.kotlin.gradle.logging.gradleLogLevel
import org.jetbrains.kotlin.gradle.plugin.statistics.BuildFusService
import org.jetbrains.kotlin.gradle.plugin.statistics.NativeArgumentMetrics
import org.jetbrains.kotlin.konan.target.HostManager
import java.io.IOException
import java.lang.reflect.InvocationTargetException
Expand All @@ -33,6 +35,7 @@ internal abstract class KotlinNativeToolRunner @Inject constructor(
private val metricsReporterProvider: Provider<BuildMetricsReporter<GradleBuildTime, GradleBuildPerformanceMetric>>,
private val classLoadersCachingBuildServiceProvider: Provider<ClassLoadersCachingBuildService>,
private val toolSpec: ToolSpec,
private val fusMetricsConsumer: Provider<BuildFusService>,
private val execOperations: ExecOperations,
) {
private val logger = Logging.getLogger(toolSpec.displayName.get())
Expand All @@ -42,6 +45,9 @@ internal abstract class KotlinNativeToolRunner @Inject constructor(

fun runTool(args: ToolArguments) {
metricsReporter.measure(GradleBuildTime.RUN_COMPILATION_IN_WORKER) {
fusMetricsConsumer.orNull?.let { metricsConsumer ->
NativeArgumentMetrics.collectMetrics(args.arguments, metricsConsumer.getFusMetricsConsumer())
}
if (args.shouldRunInProcessMode) {
runInProcess(args)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.jetbrains.kotlin.build.report.metrics.BuildMetricsReporter
import org.jetbrains.kotlin.build.report.metrics.GradleBuildPerformanceMetric
import org.jetbrains.kotlin.build.report.metrics.GradleBuildTime
import org.jetbrains.kotlin.gradle.internal.ClassLoadersCachingBuildService
import org.jetbrains.kotlin.gradle.plugin.statistics.BuildFusService
import org.jetbrains.kotlin.gradle.targets.native.KonanPropertiesBuildService
import org.jetbrains.kotlin.gradle.utils.listProperty
import org.jetbrains.kotlin.gradle.utils.property
Expand All @@ -28,7 +29,8 @@ internal fun ObjectFactory.KotlinNativeCInteropRunner(
): KotlinNativeToolRunner = newInstance(
metricsReporter,
classLoadersCachingBuildService,
kotlinToolSpec(isUseEmbeddableCompilerJar, actualNativeHomeDirectory, jvmArgs, useXcodeMessageStyle, konanPropertiesBuildService)
kotlinToolSpec(isUseEmbeddableCompilerJar, actualNativeHomeDirectory, jvmArgs, useXcodeMessageStyle, konanPropertiesBuildService),
property(BuildFusService::class.java)
)

private fun ObjectFactory.kotlinToolSpec(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
package org.jetbrains.kotlin.internal.compilerRunner.native

import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.jetbrains.kotlin.build.report.metrics.BuildMetricsReporter
import org.jetbrains.kotlin.build.report.metrics.GradleBuildPerformanceMetric
import org.jetbrains.kotlin.build.report.metrics.GradleBuildTime
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
import org.jetbrains.kotlin.gradle.internal.ClassLoadersCachingBuildService
import org.jetbrains.kotlin.gradle.plugin.statistics.BuildFusService
import org.jetbrains.kotlin.gradle.targets.native.KonanPropertiesBuildService
import org.jetbrains.kotlin.gradle.utils.listProperty
import org.jetbrains.kotlin.gradle.utils.newInstance
Expand All @@ -27,6 +29,7 @@ internal fun ObjectFactory.KotlinNativeCompilerRunner(
actualNativeHomeDirectory: Provider<File>,
jvmArgs: Provider<List<String>>,
konanPropertiesBuildService: Provider<KonanPropertiesBuildService>,
buildFusService: Property<BuildFusService?>,
): KotlinNativeToolRunner = newInstance<KotlinNativeToolRunner>(
metricsReporter,
classLoadersCachingBuildService,
Expand All @@ -37,7 +40,8 @@ internal fun ObjectFactory.KotlinNativeCompilerRunner(
actualNativeHomeDirectory,
jvmArgs,
konanPropertiesBuildService,
)
),
buildFusService
)

private fun ObjectFactory.kotlinToolSpec(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.jetbrains.kotlin.build.report.metrics.GradleBuildPerformanceMetric
import org.jetbrains.kotlin.build.report.metrics.GradleBuildTime
import org.jetbrains.kotlin.gradle.internal.ClassLoadersCachingBuildService
import org.jetbrains.kotlin.gradle.internal.properties.NativeProperties
import org.jetbrains.kotlin.gradle.plugin.statistics.BuildFusService
import org.jetbrains.kotlin.gradle.targets.native.KonanPropertiesBuildService
import org.jetbrains.kotlin.gradle.utils.listProperty
import org.jetbrains.kotlin.gradle.utils.newInstance
Expand All @@ -26,7 +27,8 @@ internal fun ObjectFactory.KotlinNativeLibraryGenerationRunner(
): KotlinNativeToolRunner = newInstance(
metricsReporter,
classLoadersCachingBuildService,
kotlinToolSpec(useXcodeMessageStyle, nativeProperties, konanPropertiesBuildService)
kotlinToolSpec(useXcodeMessageStyle, nativeProperties, konanPropertiesBuildService),
property(BuildFusService::class.java)
)

private fun ObjectFactory.kotlinToolSpec(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ enum class BooleanMetrics(val type: BooleanOverridePolicy, val anonymization: Bo

WASM_IR_INCREMENTAL(OR, SAFE),

//Garbage collector
ENABLED_NOOP_GC(OR, SAFE),
ENABLED_STWMS_GC(OR, SAFE),
ENABLED_PMCS_GC(OR, SAFE),
ENABLED_CMS_GC(OR, SAFE),

//Build reports
FILE_BUILD_REPORT(OR, SAFE),
BUILD_SCAN_BUILD_REPORT(OR, SAFE),
Expand Down Expand Up @@ -95,6 +101,6 @@ enum class BooleanMetrics(val type: BooleanOverridePolicy, val anonymization: Bo
;

companion object {
const val VERSION = 11
const val VERSION = 12
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private const val STRING_METRICS_RELATIVE_PATH = "$SOURCE_CODE_RELATIVE_PATH/Str
private const val NUMERICAL_METRICS_RELATIVE_PATH = "$SOURCE_CODE_RELATIVE_PATH/NumericalMetrics.kt"

private val STRING_METRICS_EXPECTED_VERSION_AND_HASH = Pair(2, "636529439256b809cd4fb99e20310505")
private val BOOLEAN_METRICS_EXPECTED_VERSION_AND_HASH = Pair(11, "90b586ea9ab1f5bf387c185248166cfd")
private val BOOLEAN_METRICS_EXPECTED_VERSION_AND_HASH = Pair(12, "a61a9ab2114ae61520005245a90af27f")
private val NUMERICAL_METRICS_EXPECTED_VERSION_AND_HASH = Pair(2, "d8c1a1f4fb7227fbe8247320bf3370ca")
private val SOURCE_FOLDER_EXPECTED_VERSION_AND_HASH =
Pair(
Expand Down

0 comments on commit 4384d14

Please sign in to comment.