Skip to content

Commit

Permalink
Instrument sleep statements. (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
aoli-al authored Sep 2, 2024
1 parent 2467497 commit 5455c6e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.pastalab.fray.core

import java.time.Duration
import java.util.*
import java.util.concurrent.CountDownLatch
import java.util.concurrent.ForkJoinPool
Expand Down Expand Up @@ -780,4 +781,10 @@ class RuntimeDelegate(val context: RunContext) : org.pastalab.fray.runtime.Deleg
entered.set(false)
return probe
}

override fun onThreadSleepDuration(duration: Duration?) {}

override fun onThreadSleepMillis(millis: Long) {}

override fun onThreadSleepMillisNanos(millis: Long, nanos: Int) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package org.pastalab.fray.instrumentation.base.visitors
import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.MethodVisitor
import org.objectweb.asm.Opcodes.ASM9
import org.objectweb.asm.Type
import org.objectweb.asm.commons.GeneratorAdapter
import org.pastalab.fray.runtime.Runtime

class SleepInstrumenter(cv: ClassVisitor) : ClassVisitor(ASM9, cv) {
override fun visitMethod(
Expand All @@ -28,7 +30,19 @@ class SleepInstrumenter(cv: ClassVisitor) : ClassVisitor(ASM9, cv) {
isInterface: Boolean
) {
if (owner == "java/lang/Thread" && name == "sleep") {
pop2()
if (descriptor == "(J)V") {
invokeStatic(
Type.getObjectType(Runtime::class.java.name.replace(".", "/")),
Utils.kFunctionToASMMethod(Runtime::onThreadSleepMillis))
} else if (descriptor == "(JI)V") {
invokeStatic(
Type.getObjectType(Runtime::class.java.name.replace(".", "/")),
Utils.kFunctionToASMMethod(Runtime::onThreadSleepMillisNanos))
} else {
invokeStatic(
Type.getObjectType(Runtime::class.java.name.replace(".", "/")),
Utils.kFunctionToASMMethod(Runtime::onThreadSleepDuration))
}
} else {
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface)
}
Expand Down
13 changes: 13 additions & 0 deletions runtime/src/main/java/org/pastalab/fray/runtime/Delegate.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.pastalab.fray.runtime;

import java.time.Duration;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ForkJoinPool;
Expand Down Expand Up @@ -281,5 +282,17 @@ public ForkJoinPool onForkJoinPoolCommonPool(ForkJoinPool pool) {
public int onThreadLocalRandomGetProbe(int probe) {
return probe;
}

public void onThreadSleepMillis(long millis) throws InterruptedException {
Thread.sleep(millis);
}

public void onThreadSleepDuration(Duration duration) throws InterruptedException {
Thread.sleep(duration);
}

public void onThreadSleepMillisNanos(long millis, int nanos) throws InterruptedException {
Thread.sleep(millis, nanos);
}
}

12 changes: 12 additions & 0 deletions runtime/src/main/java/org/pastalab/fray/runtime/Runtime.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.pastalab.fray.runtime;

import java.time.Duration;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ForkJoinPool;
Expand Down Expand Up @@ -372,4 +373,15 @@ public static int onThreadLocalRandomGetProbe(int probe) {
return DELEGATE.onThreadLocalRandomGetProbe(probe);
}

public static void onThreadSleepMillis(long millis) throws InterruptedException {
DELEGATE.onThreadSleepMillis(millis);
}

public static void onThreadSleepDuration(Duration duration) throws InterruptedException {
DELEGATE.onThreadSleepDuration(duration);
}

public static void onThreadSleepMillisNanos(long millis, int nanos) throws InterruptedException {
DELEGATE.onThreadSleepMillisNanos(millis, nanos);
}
}

0 comments on commit 5455c6e

Please sign in to comment.