Skip to content

Commit

Permalink
Huge refactor to resolve cyclic dependency + get jcstress to work
Browse files Browse the repository at this point in the history
  • Loading branch information
DLochmelis33 committed Jan 22, 2024
1 parent 586f237 commit 9798a48
Show file tree
Hide file tree
Showing 49 changed files with 193 additions and 91 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ build/
temp/
*.hprof
gitignored/
local.properties
51 changes: 51 additions & 0 deletions cli/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
plugins {
kotlin("multiplatform")
}

kotlin {
val nativeTargets = listOf(
linuxX64(),
// linuxArm64(), // 1) no machine currently available 2) CLI library does not support
macosX64(),
macosArm64(),
)
nativeTargets.forEach { target ->
target.binaries {
executable {
entryPoint = "main"
}
}
}
jvm {
withJava()
}

sourceSets {
commonMain {
dependencies {
implementation(project(":core"))
implementation("com.github.ajalt.clikt:clikt:4.2.1")
}
}
jvmMain {
dependencies {
implementation(project(":jcstress-wrapper"))
}
}
}
}

val jcsDir: File get() = File(System.getenv("JCS_DIR") ?: error("JCS_DIR envvar is not set"))

//tasks.named("jvmRun") {
// dependsOn(":core:copyLibToJCStress")
// dependsOn(":jcstress-wrapper:copyLibToJCStress")
//}

// code above does not sync
tasks.whenTaskAdded {
if (name == "jvmRun") {
dependsOn(":core:copyLibToJCStress")
dependsOn(":jcstress-wrapper:run")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import komem.litmus.generated.LitmusTestRegistry
import kotlin.time.Duration

abstract class CliCommon : CliktCommand(
name = "litmuskt"
name = "litmuskt",
printHelpOnEmptyArgs = true,
) {
companion object {
const val DEFAULT_BATCH_SIZE = 1_000_000
Expand Down Expand Up @@ -56,6 +57,8 @@ abstract class CliCommon : CliktCommand(
protected abstract val barrierProducer: BarrierProducer
// TODO: we don't talk about memshuffler for now

// TODO: dry run = simply list tests

override fun run() {
echo("selected tests: \n" + tests.joinToString("\n") { " - " + LitmusTestRegistry.resolveName(it) })
echo("in total: ${tests.size} tests")
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package komem.litmus

import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.required
import com.github.ajalt.clikt.parameters.options.*
import com.github.ajalt.clikt.parameters.types.choice
import com.github.ajalt.clikt.parameters.types.path
import jcstressDirectory
import komem.litmus.barriers.JvmSpinBarrier
import komem.litmus.jcstress.JCStressRunner

Expand All @@ -15,15 +13,17 @@ class CliJvm : CliCommon() {
override val barrierProducer = ::JvmSpinBarrier
override val affinityMapSchedule = listOf(null)

private val jcstressDirectory by option("-j", "--jcsdir")
.path(canBeFile = false)
private val allowJCStressReruns by option("--allow-jcs-reruns")
.flag()
// TODO: freeJCStressArgs
private val jcstressFreeArgs by option("-j", "--jcsargs")
.convert { it.split(" ") }
.default(emptyList())

override fun run() = if (runner == JCStressRunner) jcstressRun() else super.run()

private fun jcstressRun() {
println("haha: $jcstressFreeArgs")

val paramsList = variateRunParams(
batchSizeSchedule = batchSizeSchedule,
affinityMapSchedule = affinityMapSchedule,
Expand Down Expand Up @@ -54,7 +54,12 @@ class CliJvm : CliCommon() {
params.batchSize == DEFAULT_BATCH_SIZE &&
params.syncPeriod == DEFAULT_SYNC_EVERY
) null else params // jcstress defaults are different
JCStressRunner.runJCStress(nonDefaultParams, tests, jcstressDirectory)
JCStressRunner.runJCStress(
nonDefaultParams,
tests,
jcstressDirectory,
jcstressFreeArgs,
)
}
}
}
File renamed without changes.
26 changes: 6 additions & 20 deletions litmus/build.gradle.kts → core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ kotlin {
)

jvm {
withSourcesJar()
withJava()
jvmToolchain(8)
}

Expand Down Expand Up @@ -56,7 +56,6 @@ kotlin {
commonMain {
dependencies {
implementation("org.jetbrains.kotlinx:atomicfu:0.20.2")
implementation("com.github.ajalt.clikt:clikt:4.2.1")
}
kotlin.srcDir(layout.buildDirectory.dir("generated/ksp/metadata/commonMain/kotlin/")) // ksp
}
Expand All @@ -82,22 +81,6 @@ kotlin {
}
}

//val setupCinterop by tasks.register("setupCinterop") {
// group = "interop"
// doFirst {
// val interopFolder = project.projectDir.resolve("src/nativeInterop")
// if (!interopFolder.resolve("kaffinity.def").exists()) {
// exec {
// executable = interopFolder.resolve("setup.sh").absolutePath
// args = listOf(interopFolder.absolutePath)
// }
// }
// }
//}
//
//tasks.matching { it.name.contains("cinterop") && it.name.contains("Linux") }
// .forEach { it.dependsOn(setupCinterop) }

val bitcodeInternal by tasks.register("bitcodeInternal") {
val tempDir = projectDir.resolve("temp/bitcode")
doLast {
Expand Down Expand Up @@ -135,8 +118,11 @@ tasks.whenTaskAdded {
}
}

val jcsDir: File get() = File(System.getenv("JCS_DIR") ?: error("JCS_DIR envvar is not set"))

tasks.register<Copy>("copyLibToJCStress") {
dependsOn("jvmJar")
from(layout.buildDirectory.file("libs/litmus-jvm-$version.jar"))
into(projectDir.resolve("../jcstress/libs/"))
from(layout.buildDirectory.file("libs/core-jvm-$version.jar"))
rename { "litmusktJvm-1.0.jar" }
into(jcsDir.resolve("libs/komem/litmus/litmusktJvm/1.0/"))
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package komem.litmus

import komem.litmus.generated.LitmusTestRegistry

data class LitmusTest<S>(
val stateProducer: () -> S,
val threadFunctions: List<S.() -> Unit>,
Expand Down Expand Up @@ -47,3 +49,6 @@ class LitmusTestScope<S>(

fun <S> litmusTest(stateProducer: () -> S, setup: LitmusTestScope<S>.() -> Unit) =
LitmusTestScope(stateProducer).apply(setup).build()

val LitmusTest<*>.name get() = LitmusTestRegistry.resolveName(this)
val LitmusTest<*>.javaClassName get() = name.replace('.', '_')
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package komem.litmus.jcstress

import komem.litmus.LitmusResult
import komem.litmus.LitmusRunParams
import komem.litmus.LitmusRunner
import komem.litmus.LitmusTest
import komem.litmus.*
import java.nio.file.Path
import kotlin.io.path.Path
import kotlin.system.exitProcess

/**
Expand All @@ -18,12 +14,11 @@ object JCStressRunner : LitmusRunner() {
fun runJCStress(
params: LitmusRunParams?,
tests: Collection<LitmusTest<*>>,
jcsDirectory: Path? = null
jcstressDirectory: Path,
jcstressFreeArgs: List<String>,
) {
val jcstressDirectory = jcsDirectory ?: Path("../jcstress")
for (test in tests) generateWrapperFile(test, jcstressDirectory)

val mvn = ProcessBuilder("mvn", "verify")
val mvn = ProcessBuilder("mvn", "install", "verify")
.directory(jcstressDirectory.toFile())
.redirectOutput(ProcessBuilder.Redirect.INHERIT)
.redirectError(ProcessBuilder.Redirect.INHERIT)
Expand All @@ -40,7 +35,7 @@ object JCStressRunner : LitmusRunner() {
"java",
"-jar",
"target/jcstress.jar",
*jcsParams,
*(jcsParams + jcstressFreeArgs),
"-t",
tests.joinToString("|") { it.javaClassName },
)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions jcstress-wrapper/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
plugins {
kotlin("jvm")
application
}

application {
mainClass = "MainKt"
}

dependencies {
implementation(project(":core"))
implementation(kotlin("reflect"))
}
18 changes: 18 additions & 0 deletions jcstress-wrapper/src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import komem.litmus.generateWrapperFile
import komem.litmus.generated.LitmusTestRegistry
import kotlin.io.path.Path

fun main() {
var successCnt = 0
val allTests = LitmusTestRegistry.all()
for (test in allTests) {
val success = generateWrapperFile(test, jcstressDirectory)
if (success) successCnt++
}
if (successCnt != allTests.size) {
System.err.println("WARNING: generated wrappers for $successCnt out of ${allTests.size} known tests")
}
}

val jcstressDirectory
get() = Path(System.getenv("JCS_DIR") ?: error("JCS_DIR envvar is not set"))
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package komem.litmus.jcstress
package komem.litmus

import komem.litmus.LitmusAutoOutcome
import komem.litmus.LitmusOutcomeType
import komem.litmus.LitmusTest
import komem.litmus.generated.LitmusTestRegistry
import java.nio.file.Path
import kotlin.io.path.createParentDirectories
import kotlin.io.path.div
Expand All @@ -12,14 +8,17 @@ import kotlin.reflect.full.allSuperclasses
import kotlin.reflect.full.isSubclassOf
import kotlin.reflect.full.superclasses

val LitmusTest<*>.name get() = LitmusTestRegistry.resolveName(this)
val LitmusTest<*>.javaClassName get() = name.replace('.', '_')

fun generateWrapperFile(test: LitmusTest<*>, jcstressDirectory: Path) {
fun generateWrapperFile(test: LitmusTest<*>, jcstressDirectory: Path): Boolean {
val targetFile = jcstressDirectory / "src/main/java/komem/litmus/${test.javaClassName}.java"
targetFile.createParentDirectories()
val targetCode = generateWrapperCode(test)
val targetCode = try {
generateWrapperCode(test)
} catch (e: Throwable) {
System.err.println("WARNING: could not generate wrapper for ${test.name} because: ${e.message}")
return false
}
targetFile.writeText(targetCode)
return true
}

private fun generateWrapperCode(test: LitmusTest<*>): String {
Expand Down Expand Up @@ -137,4 +136,6 @@ public class ${test.javaClassName} {
""".trimIndent()
}

// TODO: arbiter padding is broken

private fun String.padded(padding: Int) = replace("\n", "\n" + " ".repeat(padding))
Loading

0 comments on commit 9798a48

Please sign in to comment.