[Java] Improve caching mechanism for hw specs in CompilerConfigurationUtils

IDEA-352162

GitOrigin-RevId: b2bd4b997a797a441932c2b67a71f08ae02448bf
This commit is contained in:
Georgii Ustinov
2024-08-07 14:15:26 +03:00
committed by intellij-monorepo-bot
parent c45d432165
commit f5029d7b21
3 changed files with 8 additions and 22 deletions

View File

@@ -325,7 +325,7 @@ public final class CompilerConfigurationImpl extends CompilerConfiguration imple
public boolean isParallelCompilationEnabled() {
return switch (getParallelCompilationOption()) {
case ENABLED -> true;
case AUTOMATIC -> CompilerConfigurationUtils.isParallelCompilationAllowedWithCurrentSpecs(myProject);
case AUTOMATIC -> CompilerConfigurationUtils.isParallelCompilationAllowedWithCurrentSpecs();
case DISABLED -> false;
};
}

View File

@@ -23,7 +23,7 @@ internal class CompilerSettingsCollector : ProjectUsagesCollector() {
usages.add(CLEAR_OUTPUT_DIRECTORY.metric(workspaceConfig.CLEAR_OUTPUT_DIRECTORY))
usages.add(MAKE_PROJECT_ON_SAVE.metric(workspaceConfig.MAKE_PROJECT_ON_SAVE))
usages.add(PARALLEL_COMPILATION_OPTION.metric(config.parallelCompilationOption))
usages.add(IS_PARALLEL_COMPILATION_ALLOWED_IN_AUTOMATIC_MODE.metric(CompilerConfigurationUtils.isParallelCompilationAllowedWithCurrentSpecs(project)))
usages.add(IS_PARALLEL_COMPILATION_ALLOWED_IN_AUTOMATIC_MODE.metric(CompilerConfigurationUtils.isParallelCompilationAllowedWithCurrentSpecs()))
usages.add(REBUILD_ON_DEPENDENCY_CHANGE.metric(workspaceConfig.REBUILD_ON_DEPENDENCY_CHANGE))
usages.add(COMPILE_AFFECTED_UNLOADED_MODULES_BEFORE_COMMIT.metric(workspaceConfig.COMPILE_AFFECTED_UNLOADED_MODULES_BEFORE_COMMIT))

View File

@@ -1,9 +1,6 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.compiler.server
import com.intellij.openapi.project.Project
import com.intellij.psi.util.CachedValueProvider
import com.intellij.psi.util.CachedValuesManager
import com.sun.management.OperatingSystemMXBean
import java.lang.management.ManagementFactory
import kotlin.math.roundToInt
@@ -13,29 +10,18 @@ object CompilerConfigurationUtils {
private const val MINIMUM_NUMBER_OF_CORES = 4
private const val MINIMUM_RAM_SIZE_IN_GIB = 8
private val NUMBER_OF_CORES = Runtime.getRuntime().availableProcessors()
private val RAM_SIZE_IN_GIB = getRAMSizeInGiB()
/**
* Checks if it is possible to compile in parallel based on the characteristics of the hardware - number of cores and available RAM
*/
@JvmStatic
fun isParallelCompilationAllowedWithCurrentSpecs(project: Project): Boolean {
val numberOfCores = getNumberOfCores(project)
if (numberOfCores < MINIMUM_NUMBER_OF_CORES) return false
val ramSizeInGiB = getRAMSizeInGiB(project) ?: return false
return ramSizeInGiB >= MINIMUM_RAM_SIZE_IN_GIB
fun isParallelCompilationAllowedWithCurrentSpecs(): Boolean {
return NUMBER_OF_CORES >= MINIMUM_NUMBER_OF_CORES && RAM_SIZE_IN_GIB != null && RAM_SIZE_IN_GIB >= MINIMUM_RAM_SIZE_IN_GIB
}
private fun getNumberOfCores(project: Project): Int = CachedValuesManager
.getManager(project).getCachedValue(project) {
CachedValueProvider.Result.create(Runtime.getRuntime().availableProcessors())
}
private fun getRAMSizeInGiB(project: Project): Int? = CachedValuesManager
.getManager(project).getCachedValue(project) {
CachedValueProvider.Result.create(getRAMSizeInGiBInner())
}
private fun getRAMSizeInGiBInner(): Int? {
private fun getRAMSizeInGiB(): Int? {
return try {
val bean = ManagementFactory.getOperatingSystemMXBean() as? OperatingSystemMXBean ?: return null
val ramInBytes = bean.totalMemorySize