Skip to content

Commit

Permalink
disable sleep.
Browse files Browse the repository at this point in the history
  • Loading branch information
aoli-al committed May 19, 2024
1 parent db3e978 commit 38182e4
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 18 deletions.
4 changes: 2 additions & 2 deletions benchmarks/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
val buildPath = layout.buildDirectory.get().asFile

dependencies {
implementation(fileTree(mapOf("dir" to "${buildPath}/libs/unzipped", "include" to listOf("*.jar"))))
implementation(fileTree(mapOf("dir" to "/Users/aoli/repos/dacapobench/benchmarks", "include" to listOf("*.jar"))))
implementation(project(":core"))
}

Expand Down Expand Up @@ -53,7 +53,7 @@ tasks.register<JavaExec>("run") {
else -> emptyList()
}
args = listOf("Harness", "main", "-a",
"$appName --size small", "-o", "${layout.buildDirectory.get().asFile}/$appName-report", "--logger", "csv", "--iter", "10000", "-s", "10000000") + extraArgs
"$appName --size small", "-o", "${layout.buildDirectory.get().asFile}/$appName-report", "--logger", "csv", "--iter", "10000", "-s", "90000000") + extraArgs
}

tasks.register<JavaExec>("replay") {
Expand Down
18 changes: 6 additions & 12 deletions core/src/main/kotlin/cmu/pasta/fray/core/GlobalContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -393,15 +393,10 @@ object GlobalContext {
}

fun lockTryLock(lock: Any) {
val t = Thread.currentThread().id
val objId = System.identityHashCode(lock)
registeredThreads[t]?.pendingOperation = LockLockOperation(objId)
registeredThreads[t]?.state = ThreadState.Enabled
scheduleNextOperation(true)
lockManager.lock(lock, t, false, false, false)
lockImpl(lock, false, false, false)
}

fun lockImpl(lock: Any, isMonitorLock: Boolean, canInterrupt: Boolean) {
fun lockImpl(lock: Any, isMonitorLock: Boolean, shouldBlock: Boolean, canInterrupt: Boolean) {
val t = Thread.currentThread().id
val objId = System.identityHashCode(lock)
val context = registeredThreads[t]!!
Expand All @@ -426,7 +421,7 @@ object GlobalContext {
// synchronized(lock) {
// lock.unlock();
// }
while (!lockManager.lock(lock, t, true, false, canInterrupt)) {
while (!lockManager.lock(lock, t, shouldBlock, false, canInterrupt) && shouldBlock) {
context.state = ThreadState.Paused

// We want to block current thread because we do
Expand All @@ -441,11 +436,11 @@ object GlobalContext {
}

fun monitorEnter(lock: Any) {
lockImpl(lock, true, false)
lockImpl(lock, true, true, false)
}

fun lockLock(lock: Any, canInterrupt: Boolean) {
lockImpl(lock, false, canInterrupt)
lockImpl(lock, false, true, canInterrupt)
}

fun reentrantReadWriteLockInit(readLock: ReadLock, writeLock: WriteLock) {
Expand Down Expand Up @@ -530,15 +525,14 @@ object GlobalContext {
context.state = ThreadState.Enabled
scheduleNextOperation(true)

while (!semaphoreManager.acquire(sem, permits, true, canInterrupt)) {
while (!semaphoreManager.acquire(sem, permits, shouldBlock, canInterrupt) && shouldBlock) {
context.state = ThreadState.Paused

scheduleNextOperation(true)
if (canInterrupt) {
context.checkInterrupt()
}
}
semaphoreManager.acquire(sem, permits, shouldBlock, canInterrupt)
}

fun semaphoreRelease(sem: Semaphore, permits: Int) {
Expand Down
13 changes: 13 additions & 0 deletions core/src/main/kotlin/cmu/pasta/fray/core/RuntimeDelegate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,19 @@ class RuntimeDelegate : Delegate() {
entered.set(false)
}

override fun onSemaphoreTryAcquire(sem: Semaphore, permits: Int) {
if (checkEntered()) {
skipFunctionEntered.set(1 + skipFunctionEntered.get())
return
}
try {
GlobalContext.semaphoreAcquire(sem, permits, false, true)
} finally {
skipFunctionEntered.set(skipFunctionEntered.get() + 1)
entered.set(false)
}
}

override fun onSemaphoreAcquire(sem: Semaphore, permits: Int) {
if (checkEntered()) {
skipFunctionEntered.set(1 + skipFunctionEntered.get())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class ApplicationCodeTransformer : ClassFileTransformer {
cv = ConditionInstrumenter(cv)
cv = SynchronizedMethodInstrumenter(cv, false)
cv = ClassConstructorInstrumenter(cv)
cv = SleepInstrumenter(cv)
val classVersionInstrumenter = ClassVersionInstrumenter(cv)
cv = ArrayOperationInstrumenter(classVersionInstrumenter)
classReader.accept(cv, ClassReader.EXPAND_FRAMES)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ class SemaphoreInstrumenter(cv: ClassVisitor) : ClassVisitorBase(cv, Semaphore::
return MethodExitVisitor(
mv, Runtime::onSemaphoreInit, access, name, descriptor, true, false, false)
}
if ((name == "acquire" || name == "acquireUninterruptibly") && descriptor == "()V") {
if ((name == "acquire" || name == "acquireUninterruptibly" || name == "tryAcquire") &&
descriptor.startsWith("()")) {
val eMv =
if (name == "acquire") {
MethodEnterVisitor(
mv, Runtime::onSemaphoreAcquire, access, name, descriptor, true, true)
} else {
} else if (name == "acquireUninterruptibly") {
MethodEnterVisitor(
mv,
Runtime::onSemaphoreAcquireUninterruptibly,
Expand All @@ -32,16 +33,20 @@ class SemaphoreInstrumenter(cv: ClassVisitor) : ClassVisitorBase(cv, Semaphore::
descriptor,
true,
true)
} else {
MethodEnterVisitor(
mv, Runtime::onSemaphoreTryAcquire, access, name, descriptor, true, true)
}
return MethodExitVisitor(
eMv, Runtime::onSemaphoreAcquireDone, access, name, descriptor, false, false, true)
}
if ((name == "acquire" || name == "acquireUninterruptibly") && descriptor == "(I)V") {
if ((name == "acquire" || name == "acquireUninterruptibly" || name == "tryAcquire") &&
descriptor.startsWith("(I)V")) {
val eMv =
if (name == "acquire") {
MethodEnterVisitor(
mv, Runtime::onSemaphoreAcquirePermits, access, name, descriptor, true, true)
} else {
} else if (name == "acquireUninterruptibly") {
MethodEnterVisitor(
mv,
Runtime::onSemaphoreAcquirePermitsUninterruptibly,
Expand All @@ -50,7 +55,11 @@ class SemaphoreInstrumenter(cv: ClassVisitor) : ClassVisitorBase(cv, Semaphore::
descriptor,
true,
true)
} else {
MethodEnterVisitor(
mv, Runtime::onSemaphoreTryAcquirePermits, access, name, descriptor, true, true)
}

return MethodExitVisitor(
eMv, Runtime::onSemaphoreAcquireDone, access, name, descriptor, false, false, true)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cmu.pasta.fray.instrumentation.visitors

import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.MethodVisitor
import org.objectweb.asm.Opcodes.ASM9
import org.objectweb.asm.commons.GeneratorAdapter

class SleepInstrumenter(cv: ClassVisitor) : ClassVisitor(ASM9, cv) {
override fun visitMethod(
access: Int,
name: String,
descriptor: String,
signature: String?,
exceptions: Array<out String>?
): MethodVisitor {
return object :
GeneratorAdapter(
ASM9,
super.visitMethod(access, name, descriptor, signature, exceptions),
access,
name,
descriptor) {
override fun visitMethodInsn(
opcode: Int,
owner: String,
name: String,
descriptor: String,
isInterface: Boolean
) {
if (owner == "java/lang/Thread" && name == "sleep") {
pop2()
} else {
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface)
}
}
}
}
}
3 changes: 3 additions & 0 deletions runtime/src/main/java/cmu/pasta/fray/runtime/Delegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ public void onSemaphoreInit(Semaphore sem) {
public void onSemaphoreAcquire(Semaphore sem, int permits) {
}

public void onSemaphoreTryAcquire(Semaphore sem, int permits) {
}

public void onSemaphoreAcquireUninterruptibly(Semaphore sem, int permits) {
}

Expand Down
8 changes: 8 additions & 0 deletions runtime/src/main/java/cmu/pasta/fray/runtime/Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ public static void onSemaphoreAcquire(Semaphore sem) {
DELEGATE.onSemaphoreAcquire(sem, 1);
}

public static void onSemaphoreTryAcquire(Semaphore sem) {
DELEGATE.onSemaphoreTryAcquire(sem, 1);
}

public static void onSemaphoreTryAcquirePermits(Semaphore sem, int permits) {
DELEGATE.onSemaphoreTryAcquire(sem, permits);
}

public static void onSemaphoreAcquireUninterruptibly(Semaphore sem) {
DELEGATE.onSemaphoreAcquireUninterruptibly(sem, 1);
}
Expand Down

0 comments on commit 38182e4

Please sign in to comment.