Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Java 11 performance bug #316

Merged
merged 19 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ tasks {
fun Test.configureJvmTestCommon() {
maxParallelForks = 1
maxHeapSize = "6g"
val instrumentAllClassesInModelCheckingMode: String by project
if (instrumentAllClassesInModelCheckingMode.toBoolean()) {
systemProperty("lincheck.instrumentAllClassesInModelCheckingMode", "true")
val instrumentAllClasses: String by project
if (instrumentAllClasses.toBoolean()) {
systemProperty("lincheck.instrumentAllClasses", "true")
}
val extraArgs = mutableListOf<String>()
val withEventIdSequentialCheck: String by project
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ lastCopyrightYear=2023

jdkToolchainVersion=17
runAllTestsInSeparateJVMs=false
instrumentAllClassesInModelCheckingMode=false
instrumentAllClasses=false
eupp marked this conversation as resolved.
Show resolved Hide resolved
withEventIdSequentialCheck=false

kotlinVersion=1.9.21
Expand Down
51 changes: 45 additions & 6 deletions src/jvm/main/org/jetbrains/kotlinx/lincheck/LinChecker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,32 @@ class LinChecker (private val testClass: Class<*>, options: Options<*, *>?) {
* @throws LincheckAssertionError if the testing data structure is incorrect.
*/
fun check() {
val failure = checkImpl() ?: return
throw LincheckAssertionError(failure)
checkImpl { failure ->
if (failure != null) throw LincheckAssertionError(failure)
}
}

/**
* @return TestReport with information about concurrent test run.
* Runs Lincheck to check the tested class under given configurations.
*
* @param cont Optional continuation taking [LincheckFailure] as an argument.
* The continuation is run in the context when Lincheck java-agent is still attached.
* @return [LincheckFailure] if a failure is discovered, null otherwise.
*/
@Synchronized // never run Lincheck tests in parallel
internal fun checkImpl(): LincheckFailure? {
internal fun checkImpl(cont: LincheckFailureContinuation? = null): LincheckFailure? {
check(testConfigurations.isNotEmpty()) { "No Lincheck test configuration to run" }
lincheckVerificationStarted()
for (testCfg in testConfigurations) {
withLincheckJavaAgent(testCfg.instrumentationMode) {
val failure = testCfg.checkImpl()
if (failure != null) return failure
if (failure != null) {
if (cont != null) cont(failure)
return failure
}
}
}
if (cont != null) cont(null)
return null
}

Expand Down Expand Up @@ -203,6 +212,36 @@ fun <O : Options<O, *>> O.check(testClass: Class<*>) = LinChecker.check(testClas
*/
fun <O : Options<O, *>> O.check(testClass: KClass<*>) = this.check(testClass.java)

internal fun <O : Options<O, *>> O.checkImpl(testClass: Class<*>) = LinChecker(testClass, this).checkImpl()
/**
* Runs Lincheck to check the tested class under given configurations.
*
* @param testClass Tested class.
* @return [LincheckFailure] if a failure is discovered, null otherwise.
*/
internal fun <O : Options<O, *>> O.checkImpl(testClass: Class<*>): LincheckFailure? =
LinChecker(testClass, this).checkImpl()

/**
* Runs Lincheck to check the tested class under given configurations.
*
* Takes the [LincheckFailureContinuation] as an argument.
* This is required due to current limitations of our testing infrastructure.
* Some tests need to inspect the internals of the failure object
* (for example, the stack traces of exceptions thrown during the execution).
* However, because Lincheck dynamically installs java-agent and then uninstalls it,
* this process can invalidate some internal state of the failure object
* (for example, the source code mapping information in the stack traces is typically lost).
* To overcome this problem, we run the continuation in the context when Lincheck java-agent is still attached.
*
* @param testClass Tested class.
* @param cont Continuation taking [LincheckFailure] as an argument.
* The continuation is run in the context when Lincheck java-agent is still attached.
* @return [LincheckFailure] if a failure is discovered, null otherwise.
*/
internal fun <O : Options<O, *>> O.checkImpl(testClass: Class<*>, cont: LincheckFailureContinuation) {
LinChecker(testClass, this).checkImpl(cont)
}

internal typealias LincheckFailureContinuation = (LincheckFailure?) -> Unit

internal const val NO_OPERATION_ERROR_MESSAGE = "You must specify at least one operation to test. Please refer to the user guide: https://kotlinlang.org/docs/introduction.html"
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import org.objectweb.asm.commons.*
import org.jetbrains.kotlinx.lincheck.transformation.InstrumentationMode.*
import org.jetbrains.kotlinx.lincheck.transformation.transformers.*
import sun.nio.ch.lincheck.*
import kotlin.collections.HashSet

internal class LincheckClassVisitor(
private val instrumentationMode: InstrumentationMode,
Expand All @@ -26,8 +27,8 @@ internal class LincheckClassVisitor(
private val ideaPluginEnabled = ideaPluginEnabled()
private var classVersion = 0

private lateinit var fileName: String
private lateinit var className: String
private var fileName: String = ""
private var className: String = ""

override fun visitField(
access: Int,
Expand Down Expand Up @@ -73,7 +74,7 @@ internal class LincheckClassVisitor(
if (access and ACC_NATIVE != 0) return mv
if (instrumentationMode == STRESS) {
return if (methodName != "<clinit>" && methodName != "<init>") {
CoroutineCancellabilitySupportTransformer(mv, access, methodName, desc)
CoroutineCancellabilitySupportTransformer(mv, access, className, methodName, desc)
} else {
mv
}
Expand Down Expand Up @@ -103,7 +104,7 @@ internal class LincheckClassVisitor(
}
mv = JSRInlinerAdapter(mv, access, methodName, desc, signature, exceptions)
mv = TryCatchBlockSorter(mv, access, methodName, desc, signature, exceptions)
mv = CoroutineCancellabilitySupportTransformer(mv, access, methodName, desc)
mv = CoroutineCancellabilitySupportTransformer(mv, access, className, methodName, desc)
eupp marked this conversation as resolved.
Show resolved Hide resolved
if (access and ACC_SYNCHRONIZED != 0) {
mv = SynchronizedMethodTransformer(fileName, className, methodName, mv.newAdapter(), classVersion)
}
Expand Down Expand Up @@ -192,4 +193,6 @@ private class WrapMethodInIgnoredSectionTransformer(
}
visitInsn(opcode)
}
}
}

internal val coroutineCallingClasses = HashSet<String>()
ndkoval marked this conversation as resolved.
Show resolved Hide resolved
Loading