Skip to content

Commit

Permalink
Fix jcstress result not parsing default outcome (add parseOutcome() t…
Browse files Browse the repository at this point in the history
…o LitmusAutoOutcome)
  • Loading branch information
DLochmelis33 committed Aug 4, 2024
1 parent d8dd41f commit e6fbc8f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package org.jetbrains.litmuskt.autooutcomes

import org.jetbrains.litmuskt.LitmusOutcomeSpecScope

// TODO
/**
* "Z" is the name for Boolean outcomes in JCStress.
*/

// TODO: codegen

open class LitmusZZOutcome(
var r1: Boolean = false,
Expand All @@ -16,6 +20,10 @@ open class LitmusZZOutcome(
}

final override fun toList() = listOf(r1, r2)
final override fun parseOutcome(str: String): LitmusZZOutcome {
val rs = str.split(", ").map(String::toBooleanStrict)
return LitmusZZOutcome(rs[0], rs[1])
}
}

fun <S : LitmusZZOutcome> LitmusOutcomeSpecScope<S>.accept(r1: Boolean, r2: Boolean) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ open class LitmusIOutcome(
}

final override fun toList() = listOf(r1)
final override fun parseOutcome(str: String): LitmusIOutcome {
val rs = str.split(", ").map(String::toInt)
return LitmusIOutcome(rs[0])
}
}

fun <S : LitmusIOutcome> LitmusOutcomeSpecScope<S>.accept(r1: Int) =
Expand All @@ -36,6 +40,10 @@ open class LitmusIIOutcome(
}

final override fun toList() = listOf(r1, r2)
final override fun parseOutcome(str: String): LitmusIIOutcome {
val rs = str.split(", ").map(String::toInt)
return LitmusIIOutcome(rs[0], rs[1])
}
}

fun <S : LitmusIIOutcome> LitmusOutcomeSpecScope<S>.accept(r1: Int, r2: Int) =
Expand All @@ -60,6 +68,10 @@ open class LitmusIIIOutcome(
}

final override fun toList() = listOf(r1, r2, r3)
final override fun parseOutcome(str: String): LitmusIIIOutcome {
val rs = str.split(", ").map(String::toInt)
return LitmusIIIOutcome(rs[0], rs[1], rs[2])
}
}

fun <S : LitmusIIIOutcome> LitmusOutcomeSpecScope<S>.accept(r1: Int, r2: Int, r3: Int) =
Expand All @@ -85,6 +97,10 @@ open class LitmusIIIIOutcome(
}

final override fun toList() = listOf(r1, r2, r3, r4)
final override fun parseOutcome(str: String): LitmusIIIIOutcome {
val rs = str.split(", ").map(String::toInt)
return LitmusIIIIOutcome(rs[0], rs[1], rs[2], rs[3])
}
}

fun <S : LitmusIIIIOutcome> LitmusOutcomeSpecScope<S>.accept(r1: Int, r2: Int, r3: Int, r4: Int) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ sealed interface LitmusAutoOutcome {

// for JCStress interop
fun toList(): List<LitmusOutcome>
fun parseOutcome(str: String): LitmusAutoOutcome
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ open class LitmusLOutcome(
}

final override fun toList() = listOf(r1)
final override fun parseOutcome(str: String): LitmusLOutcome {
val rs = str.split(", ").map(String::toLong)
return LitmusLOutcome(rs[0])
}
}

fun <S : LitmusLOutcome> LitmusOutcomeSpecScope<S>.accept(r1: Long) =
Expand All @@ -36,6 +40,10 @@ open class LitmusLLOutcome(
}

final override fun toList() = listOf(r1, r2)
final override fun parseOutcome(str: String): LitmusLLOutcome {
val rs = str.split(", ").map(String::toLong)
return LitmusLLOutcome(rs[0], rs[1])
}
}

fun <S : LitmusLLOutcome> LitmusOutcomeSpecScope<S>.accept(r1: Long, r2: Long) =
Expand All @@ -60,6 +68,10 @@ open class LitmusLLLOutcome(
}

final override fun toList() = listOf(r1, r2, r3)
final override fun parseOutcome(str: String): LitmusLLLOutcome {
val rs = str.split(", ").map(String::toLong)
return LitmusLLLOutcome(rs[0], rs[1], rs[2])
}
}

fun <S : LitmusLLLOutcome> LitmusOutcomeSpecScope<S>.accept(r1: Long, r2: Long, r3: Long) =
Expand All @@ -85,6 +97,10 @@ open class LitmusLLLLOutcome(
}

final override fun toList() = listOf(r1, r2, r3, r4)
final override fun parseOutcome(str: String): LitmusLLLLOutcome {
val rs = str.split(", ").map(String::toLong)
return LitmusLLLLOutcome(rs[0], rs[1], rs[2], rs[3])
}
}

fun <S : LitmusLLLLOutcome> LitmusOutcomeSpecScope<S>.accept(r1: Long, r2: Long, r3: Long, r4: Long) =
Expand Down
21 changes: 12 additions & 9 deletions jcstress-wrapper/src/main/kotlin/org/jetbrains/litmuskt/Codegen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fun generateWrapperFile(test: LitmusTest<*>, generatedSrc: Path): Boolean {
val targetCode = try {
generateWrapperCode(test)
} catch (e: Throwable) {
System.err.println("WARNING: could not generate wrapper for ${test.alias} because: ${e.message}")
System.err.println("WARNING: could not generate wrapper for ${test.alias} because:\n" + e.stackTraceToString())
return false
}
targetFile.writeText(targetCode)
Expand All @@ -37,12 +37,15 @@ private fun generateWrapperCode(test: LitmusTest<*>): String {
val outcomeTypeName = autoOutcomeClassList.first().simpleName!!
.removePrefix("Litmus")
.removeSuffix("Outcome")
val (outcomeVarType, outcomeVarCount) = when (outcomeTypeName) {
"I" -> "Integer" to 1
"II" -> "Integer" to 2
"III" -> "Integer" to 3
"IIII" -> "Integer" to 4
else -> error("unknown AutoOutcome type $outcomeTypeName")

val outcomeVarTypes = outcomeTypeName.map { c ->
when (c) {
'I' -> "Integer"
'L' -> "Long"
'Z' -> "Boolean"
// TODO: add others once they are created
else -> error("unrecognized outcome type '$c'")
}
}

val javaTestGetter: String = run {
Expand All @@ -56,8 +59,8 @@ private fun generateWrapperCode(test: LitmusTest<*>): String {
"""
@Arbiter
public void a($jcstressResultClassName r) {
List<$outcomeVarType> result = (List<$outcomeVarType>) (Object) ((LitmusAutoOutcome) fA.invoke(state)).toList();
${List(outcomeVarCount) { "r.r${it + 1} = result.get($it);" }.joinToString("\n ")}
List<Object> result = (List<Object>) (Object) ((LitmusAutoOutcome) fA.invoke(state)).toList();
${List(outcomeVarTypes.size) { "r.r${it + 1} = (${outcomeVarTypes[it]}) result.get($it);" }.joinToString("\n ")}
}
""".trim()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jetbrains.litmuskt

import org.jetbrains.litmuskt.autooutcomes.LitmusAutoOutcome
import org.jetbrains.litmuskt.barriers.JvmCyclicBarrier
import java.nio.file.Files
import java.nio.file.Path
Expand Down Expand Up @@ -109,9 +110,6 @@ class JCStressRunner(
if (Files.notExists(resultsFile)) return null
var lines = Files.lines(resultsFile).asSequence()

val allOutcomes = test.outcomeSpec.all
val outcomeStrings = allOutcomes.associateBy { it.toString().trim('(', ')') }

// get the number of observed outcomes
lines = lines.dropWhile { !it.contains("Observed States") }
val observedOutcomesLine = lines.splitFirst().let { (first, rest) -> lines = rest; first }
Expand All @@ -120,9 +118,10 @@ class JCStressRunner(
// skip to <tr> with outcomes
lines = lines.drop(3)
val linesOutcomes = lines.splitTake(observedSize).let { (first, rest) -> lines = rest; first }
val outcomeParser = test.stateProducer() as LitmusAutoOutcome
val outcomesOrdered = linesOutcomes.map {
val outcomeString = parseElementData(it)
outcomeStrings[outcomeString] ?: error("unrecognized outcome: $outcomeString")
outcomeParser.parseOutcome(outcomeString)
}.toList()

// lines with "bgColor" and "width" are the only ones with data
Expand Down

0 comments on commit e6fbc8f

Please sign in to comment.