diff --git a/kotlin-native/backend.native/tests/build.gradle b/kotlin-native/backend.native/tests/build.gradle index 628e01cfb91..3c5d1dd9cf8 100644 --- a/kotlin-native/backend.native/tests/build.gradle +++ b/kotlin-native/backend.native/tests/build.gradle @@ -9,10 +9,6 @@ import org.jetbrains.kotlin.* import org.jetbrains.kotlin.gradle.plugin.tasks.KonanCompileNativeBinary import org.jetbrains.kotlin.konan.target.Architecture import org.jetbrains.kotlin.konan.target.Family -import org.jetbrains.kotlin.konan.target.HostManager -import org.jetbrains.kotlin.konan.target.KonanTarget - -import java.util.regex.Pattern buildscript { repositories { @@ -969,17 +965,6 @@ createInterop("workerSignals") { it.extraOpts "-Xcompile-source", "$projectDir/interop/workerSignals/workerSignals.cpp" } -if (PlatformInfo.isAppleTarget(project)) { - createInterop("kt63423_dispose_on_main_stress") { - it.defFile 'interop/objc/kt63423_dispose_on_main_stress/objclib.def' - it.headers "$projectDir/interop/objc/kt63423_dispose_on_main_stress/objclib.h" - it.extraOpts "-Xcompile-source", "$projectDir/interop/objc/kt63423_dispose_on_main_stress/objclib.m" - it.extraOpts "-Xsource-compiler-option", "-DNS_FORMAT_ARGUMENT(A)=" - it.extraOpts "-Xsource-compiler-option", "-fobjc-arc" - it.extraOpts "-Xsource-compiler-option", "-ObjC++" - } -} - createInterop("exceptions_throwThroughBridge") { it.defFile "interop/exceptions/throwThroughBridge.def" it.extraOpts "-Xcompile-source", "$projectDir/interop/exceptions/throwThroughBridgeInterop.cpp" @@ -1118,21 +1103,6 @@ if (PlatformInfo.isAppleTarget(project)) { useGoldenData = true UtilsKt.dependsOnPlatformLibs(it) } - - - interopTest("interop_objc_kt63423_dispose_on_main_stress") { - // Test depends on macOS-specific AppKit - enabled = (project.testTarget == 'macos_x64' || project.testTarget == 'macos_arm64' || project.testTarget == null) - && !isNoopGC // requires some GC - && !isAggressiveGC // requires careful timing of GC - && !isWithStateChecker // TODO(KT-65261) - source = 'interop/objc/kt63423_dispose_on_main_stress/main.kt' - interop = "kt63423_dispose_on_main_stress" - flags = [ - '-opt-in=kotlin.native.internal.InternalForKotlinNative', // MemoryUsageInfo is internal - '-Xbinary=gcSchedulerType=manual', // This test requires very careful timing when GC can happen - ] - } } tasks.register("KT-50983", KonanDriverTest) { diff --git a/kotlin-native/backend.native/tests/interop/objc/kt63423_dispose_on_main_stress/main.kt b/kotlin-native/backend.native/tests/interop/objc/kt63423_dispose_on_main_stress/main.kt deleted file mode 100644 index 97449917fb1..00000000000 --- a/kotlin-native/backend.native/tests/interop/objc/kt63423_dispose_on_main_stress/main.kt +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ -@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class) - -import objclib.* - -import kotlin.native.internal.MemoryUsageInfo -import kotlin.test.* -import kotlinx.cinterop.* - -class PeakRSSChecker(private val rssDiffLimitBytes: Long) { - // On Linux, the child process might immediately commit the same amount of memory as the parent. - // So, measure difference between peak RSS measurements. - private val initialBytes = MemoryUsageInfo.peakResidentSetSizeBytes.also { - check(it != 0L) { "Error trying to obtain peak RSS. Check if current platform is supported" } - } - - fun check(): Long { - val diffBytes = MemoryUsageInfo.peakResidentSetSizeBytes - initialBytes - check(diffBytes <= rssDiffLimitBytes) { "Increased peak RSS by $diffBytes bytes which is more than $rssDiffLimitBytes" } - return diffBytes - } -} - -fun alloc(): Unit = autoreleasepool { - OnDestroyHook() - Unit -} - -fun waitDestruction() { - assertTrue(isMainThread()) - kotlin.native.internal.GC.collect() - spin() -} - -fun main() = startApp { - repeat(500000) { - alloc() - } - val peakRSSChecker = PeakRSSChecker(10_000_000L) // ~10MiB allowed difference for running finalizers - waitDestruction() - peakRSSChecker.check() -} \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/interop/objc/kt63423_dispose_on_main_stress/objclib.def b/kotlin-native/backend.native/tests/interop/objc/kt63423_dispose_on_main_stress/objclib.def deleted file mode 100644 index e6ac22942a9..00000000000 --- a/kotlin-native/backend.native/tests/interop/objc/kt63423_dispose_on_main_stress/objclib.def +++ /dev/null @@ -1,3 +0,0 @@ -language = Objective-C -headerFilter = **/objclib.h -linkerOpts = -framework AppKit \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/interop/objc/kt63423_dispose_on_main_stress/objclib.h b/kotlin-native/backend.native/tests/interop/objc/kt63423_dispose_on_main_stress/objclib.h deleted file mode 100644 index d2bacdc8b1d..00000000000 --- a/kotlin-native/backend.native/tests/interop/objc/kt63423_dispose_on_main_stress/objclib.h +++ /dev/null @@ -1,17 +0,0 @@ -#include - -@interface OnDestroyHook : NSObject -- (instancetype)init; -@end - -#ifdef __cplusplus -extern "C" { -#endif - -void startApp(void (^task)()); -BOOL isMainThread(); -void spin(); - -#ifdef __cplusplus -} -#endif \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/interop/objc/kt63423_dispose_on_main_stress/objclib.m b/kotlin-native/backend.native/tests/interop/objc/kt63423_dispose_on_main_stress/objclib.m deleted file mode 100644 index 0b6f1c6d405..00000000000 --- a/kotlin-native/backend.native/tests/interop/objc/kt63423_dispose_on_main_stress/objclib.m +++ /dev/null @@ -1,58 +0,0 @@ -#include "objclib.h" - -#include -#include -#include -#import -#import -#import - -std::map dictionary; - -@implementation OnDestroyHook -- (instancetype)init { - if (self = [super init]) { - dictionary[(uintptr_t)self] = true; - } - return self; -} - -- (void)dealloc { - dictionary[(uintptr_t)self] = false; -} - -@end - -extern "C" void startApp(void (^task)()) { - dispatch_async(dispatch_get_main_queue(), ^{ - // At this point all other scheduled main queue tasks were already executed. - // Executing via performBlock to allow a recursive run loop in `spin()`. - [[NSRunLoop currentRunLoop] performBlock:^{ - task(); - [NSApp terminate:NULL]; - }]; - }); - [[NSApplication sharedApplication] run]; -} - -extern "C" BOOL isMainThread() { - return [NSThread isMainThread]; -} - -extern "C" void spin() { - if ([NSRunLoop currentRunLoop] != [NSRunLoop mainRunLoop]) { - fprintf(stderr, "Must spin main run loop\n"); - exit(1); - } - while (true) { - [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; - bool done = true; - for (auto kvp : dictionary) { - if (kvp.second) { - done = false; - break; - } - } - if (done) return; - } -} diff --git a/native/native.tests/testData/standalone/kt63423_dispose_on_main_stress.kt b/native/native.tests/testData/standalone/kt63423_dispose_on_main_stress.kt new file mode 100644 index 00000000000..43fa8ad7f3a --- /dev/null +++ b/native/native.tests/testData/standalone/kt63423_dispose_on_main_stress.kt @@ -0,0 +1,145 @@ +/* + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ +// Test depends on macOS-specific AppKit +// DISABLE_NATIVE: isAppleTarget=false +// DISABLE_NATIVE: targetFamily=IOS +// DISABLE_NATIVE: targetFamily=TVOS +// DISABLE_NATIVE: targetFamily=WATCHOS +// requires some GC and its careful timing +// DISABLE_NATIVE: gcType=NOOP +// DISABLE_NATIVE: gcScheduler=AGGRESSIVE +// KT-65261: TODO +// DISABLE_NATIVE: useThreadStateChecker=ENABLED + +// FREE_CINTEROP_ARGS: -Xsource-compiler-option -ObjC++ +// FREE_COMPILER_ARGS: -Xbinary=gcSchedulerType=manual -opt-in=kotlin.native.internal.InternalForKotlinNative +// MODULE: cinterop +// FILE: objclib.def +language = Objective-C +headers = objclib.h +linkerOpts = -framework AppKit + +// FILE: objclib.h +#include + +@interface OnDestroyHook : NSObject +- (instancetype)init; +@end + +#ifdef __cplusplus +extern "C" { + #endif + + void startApp(void (^task)()); + BOOL isMainThread(); + void spin(); + + #ifdef __cplusplus +} +#endif + +// FILE: objclib.m +#include "objclib.h" + +#include +#include +#include +#import +#import +#import + +std::map dictionary; + +@implementation OnDestroyHook +- (instancetype)init { + if (self = [super init]) { + dictionary[(uintptr_t)self] = true; + } + return self; +} + +- (void)dealloc { + dictionary[(uintptr_t)self] = false; +} + +@end + +extern "C" void startApp(void (^task)()) { + dispatch_async(dispatch_get_main_queue(), ^{ + // At this point all other scheduled main queue tasks were already executed. + // Executing via performBlock to allow a recursive run loop in `spin()`. + [[NSRunLoop currentRunLoop] performBlock:^{ + task(); + [NSApp terminate:NULL]; + }]; + }); + [[NSApplication sharedApplication] run]; +} + +extern "C" BOOL isMainThread() { + return [NSThread isMainThread]; +} + +extern "C" void spin() { + if ([NSRunLoop currentRunLoop] != [NSRunLoop mainRunLoop]) { + fprintf(stderr, "Must spin main run loop\n"); + exit(1); + } + while (true) { + [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; + bool done = true; + for (auto kvp : dictionary) { + if (kvp.second) { + done = false; + break; + } + } + if (done) return; + } +} + +// MODULE: main(cinterop) +// FILE: main.kt +@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class, kotlinx.cinterop.ExperimentalForeignApi::class) + +import objclib.* + +import kotlin.native.internal.MemoryUsageInfo +import kotlin.test.* +import kotlinx.cinterop.* + +class PeakRSSChecker(private val rssDiffLimitBytes: Long) { + // On Linux, the child process might immediately commit the same amount of memory as the parent. + // So, measure difference between peak RSS measurements. + private val initialBytes = MemoryUsageInfo.peakResidentSetSizeBytes.also { + check(it != 0L) { "Error trying to obtain peak RSS. Check if current platform is supported" } + } + + fun check(): Long { + val diffBytes = MemoryUsageInfo.peakResidentSetSizeBytes - initialBytes + check(diffBytes <= rssDiffLimitBytes) { "Increased peak RSS by $diffBytes bytes which is more than $rssDiffLimitBytes" } + return diffBytes + } +} + +fun alloc(): Unit = autoreleasepool { + OnDestroyHook() + Unit +} + +fun waitDestruction() { + assertTrue(isMainThread()) + kotlin.native.internal.GC.collect() + spin() +} + +fun main() = startApp { + repeat(500000) { + alloc() + } + val peakRSSChecker = PeakRSSChecker(10_000_000L) // ~10MiB allowed difference for running finalizers + waitDestruction() + peakRSSChecker.check() +} \ No newline at end of file diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeStandaloneTestGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeStandaloneTestGenerated.java index 3bc38bd52ac..9553ec0325a 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeStandaloneTestGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeStandaloneTestGenerated.java @@ -46,6 +46,12 @@ public class FirNativeStandaloneTestGenerated extends AbstractNativeBlackBoxTest runTest("native/native.tests/testData/standalone/kt56048.kt"); } + @Test + @TestMetadata("kt63423_dispose_on_main_stress.kt") + public void testKt63423_dispose_on_main_stress() { + runTest("native/native.tests/testData/standalone/kt63423_dispose_on_main_stress.kt"); + } + @Nested @TestMetadata("native/native.tests/testData/standalone/console") @TestDataPath("$PROJECT_ROOT") diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeStandaloneTestGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeStandaloneTestGenerated.java index 2a1f9cb6050..c3f878a99db 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeStandaloneTestGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeStandaloneTestGenerated.java @@ -43,6 +43,12 @@ public class NativeStandaloneTestGenerated extends AbstractNativeBlackBoxTest { runTest("native/native.tests/testData/standalone/kt56048.kt"); } + @Test + @TestMetadata("kt63423_dispose_on_main_stress.kt") + public void testKt63423_dispose_on_main_stress() { + runTest("native/native.tests/testData/standalone/kt63423_dispose_on_main_stress.kt"); + } + @Nested @TestMetadata("native/native.tests/testData/standalone/console") @TestDataPath("$PROJECT_ROOT") diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/test/blackbox/support/group/ExtTestCaseGroupProvider.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/test/blackbox/support/group/ExtTestCaseGroupProvider.kt index 1e195521ba6..5651fc3b5b9 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/test/blackbox/support/group/ExtTestCaseGroupProvider.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/test/blackbox/support/group/ExtTestCaseGroupProvider.kt @@ -948,6 +948,8 @@ private val CACHE_MODE_NAMES = CacheMode.Alias.entries.map { it.name } private val TEST_MODE_NAMES = TestMode.entries.map { it.name } private val OPTIMIZATION_MODE_NAMES = OptimizationMode.entries.map { it.name } private val GC_TYPE_NAMES = GCType.entries.map { it.name } +private val GC_SCHEDULER_NAMES = GCScheduler.entries.map { it.name } +private val THREAD_STATE_CHECKER_NAMES = ThreadStateChecker.entries.map { it.name } private val FAMILY_NAMES = Family.entries.map { it.name } private val ARCHITECTURE_NAMES = Architecture.entries.map { it.name } private val BOOLEAN_NAMES = listOf(true.toString(), false.toString()) @@ -975,6 +977,8 @@ private fun Settings.evaluate(directiveValues: List): Boolean { ClassLevelProperty.OPTIMIZATION_MODE.shortName -> get().name to OPTIMIZATION_MODE_NAMES ClassLevelProperty.TEST_TARGET.shortName -> get().testTarget.name to null ClassLevelProperty.GC_TYPE.shortName -> get().name to GC_TYPE_NAMES + ClassLevelProperty.GC_SCHEDULER.shortName -> get().name to GC_SCHEDULER_NAMES + ClassLevelProperty.USE_THREAD_STATE_CHECKER.shortName -> get().name to THREAD_STATE_CHECKER_NAMES TARGET_FAMILY -> get().testTarget.family.name to FAMILY_NAMES TARGET_ARCHITECTURE -> get().testTarget.architecture.name to ARCHITECTURE_NAMES IS_APPLE_TARGET -> get().testTarget.family.isAppleFamily.toString() to BOOLEAN_NAMES