Skip to content

Commit

Permalink
Merge pull request #15621 from JasonFengJ9/fixlock
Browse files Browse the repository at this point in the history
Ensure lock contentions for CRIU single thread mode tests
  • Loading branch information
tajila committed Aug 8, 2022
2 parents 41430ff + 4179a01 commit 81e4fd9
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 97 deletions.
Expand Up @@ -27,16 +27,15 @@
import java.nio.file.attribute.PosixFilePermissions;
import java.io.File;
import java.io.IOException;
import java.util.Date;

import org.eclipse.openj9.criu.*;

public class CRIUTestUtils {
public static void deleteCheckpointDirectory(Path path) {
try {
if (path.toFile().exists()) {
Files.walk(path)
.map(Path::toFile)
.forEach(File::delete);
Files.walk(path).map(Path::toFile).forEach(File::delete);

Files.deleteIfExists(path);
}
Expand Down Expand Up @@ -70,10 +69,7 @@ public static void checkPointJVM(CRIUSupport criu, Path path, boolean deleteDir)
if (criu == null) {
criu = new CRIUSupport(path);
}
criu.setLeaveRunning(false)
.setShellJob(true)
.setFileLocks(true)
.checkpointJVM();
criu.setLeaveRunning(false).setShellJob(true).setFileLocks(true).checkpointJVM();
} catch (RestoreException e) {
e.printStackTrace();
}
Expand All @@ -84,4 +80,10 @@ public static void checkPointJVM(CRIUSupport criu, Path path, boolean deleteDir)
System.err.println("CRIU is not enabled");
}
}

public static void showThreadCurrentTime(String logStr) {
System.out.println(logStr + ", current thread name: " + Thread.currentThread().getName() + ", " + new Date()
+ ", System.currentTimeMillis(): " + System.currentTimeMillis() + ", System.nanoTime(): "
+ System.nanoTime());
}
}
Expand Up @@ -30,73 +30,60 @@

public class TestSingleThreadModeCheckpointException {

private final static Path imagePath = Paths.get("cpData");
private static final Object lock = new Object();

public static void main(String[] args) {
new TestSingleThreadModeCheckpointException().testSingleThreadModeCheckpointException();
}

private void log(String msg) {
System.out.println(msg + ": " + this + " name: " + Thread.currentThread().getName());
}

Thread newThreadOwnMonitor() {
Thread doCheckpoint() {
Thread t = new Thread(new Runnable() {
public void run() {
log("newThreadOwnMonitor() before synchronized on " + lock);
synchronized (lock) {
for (;;) {
try {
log("newThreadOwnMonitor() before Thread.sleep()");
Thread.sleep(1000);
log("newThreadOwnMonitor() after Thread.sleep()");
} catch (InterruptedException e) {
log("newThreadOwnMonitor() interrupted");
break;
boolean result = false;
Path imagePath = Paths.get("cpData");
CRIUTestUtils.deleteCheckpointDirectory(imagePath);
CRIUTestUtils.createCheckpointDirectory(imagePath);
CRIUSupport criu = new CRIUSupport(imagePath);
criu.registerPreSnapshotHook(new Runnable() {
public void run() {
CRIUTestUtils.showThreadCurrentTime("PreSnapshotHook() before synchronized on " + lock);
synchronized (lock) {
CRIUTestUtils.showThreadCurrentTime("PreSnapshotHook() within synchronized on " + lock);
}
CRIUTestUtils.showThreadCurrentTime("PreSnapshotHook() after synchronized on " + lock);
}
});

try {
System.out.println("Pre-checkpoint");
CRIUTestUtils.checkPointJVM(criu, imagePath, false);
} catch (JVMCheckpointException jvmce) {
result = true;
}
if (result) {
System.out.println("TestSingleThreadModeCheckpointException: PASSED.");
} else {
System.out.println("TestSingleThreadModeCheckpointException: FAILED.");
}
log("newThreadOwnMonitor() after synchronized on " + lock);
}
});
return t;
}

void testSingleThreadModeCheckpointException() {
boolean result = false;
Thread tom = newThreadOwnMonitor();
tom.start();

CRIUTestUtils.deleteCheckpointDirectory(imagePath);
CRIUTestUtils.createCheckpointDirectory(imagePath);
CRIUSupport criu = new CRIUSupport(imagePath);
criu.registerPreSnapshotHook(new Runnable() {
public void run() {
log("PreSnapshotHook() before synchronized on " + lock);
synchronized (lock) {
log("PreSnapshotHook() within synchronized on " + lock);
}
log("PreSnapshotHook() after synchronized on " + lock);
CRIUTestUtils.showThreadCurrentTime("testSingleThreadModeCheckpointException() before synchronized on " + lock);
synchronized (lock) {
try {
// ensure the lock already taken before performing a checkpoint
CRIUTestUtils.showThreadCurrentTime("testSingleThreadModeCheckpointException() before doCheckpoint()");
Thread tpc = doCheckpoint();
tpc.start();
// set timeout 10s
tpc.join(10000);
CRIUTestUtils.showThreadCurrentTime("testSingleThreadModeCheckpointException() after doCheckpoint()");
} catch (InterruptedException e) {
}
});

try {
System.out.println("Pre-checkpoint");
CRIUTestUtils.checkPointJVM(criu, imagePath, false);
} catch (JVMCheckpointException jvmce) {
result = true;
}
if (result) {
System.out.println("TestSingleThreadModeCheckpointException: PASSED.");
} else {
System.out.println("TestSingleThreadModeCheckpointException: FAILED.");
}

tom.interrupt();
try {
tom.join();
} catch (InterruptedException e) {
}
CRIUTestUtils.showThreadCurrentTime("testSingleThreadModeCheckpointException() after synchronized on " + lock);
}
}
Expand Up @@ -30,64 +30,51 @@

public class TestSingleThreadModeRestoreException {

private final static Path imagePath = Paths.get("cpData");
private static final Object lock = new Object();

public static void main(String[] args) {
new TestSingleThreadModeRestoreException().testSingleThreadModeRestoreException();
}

private void log(String msg) {
System.out.println(msg + ": " + this + " name: " + Thread.currentThread().getName());
}

Thread newThreadOwnMonitor() {
Thread doCheckpoint() {
Thread t = new Thread(new Runnable() {
public void run() {
log("newThreadOwnMonitor() before synchronized on " + lock);
synchronized (lock) {
for (;;) {
try {
log("newThreadOwnMonitor() before Thread.sleep()");
Thread.sleep(1000);
log("newThreadOwnMonitor() after Thread.sleep()");
} catch (InterruptedException e) {
log("newThreadOwnMonitor() interrupted");
break;
Path imagePath = Paths.get("cpData");
CRIUTestUtils.deleteCheckpointDirectory(imagePath);
CRIUTestUtils.deleteCheckpointDirectory(imagePath);
CRIUTestUtils.createCheckpointDirectory(imagePath);
CRIUSupport criu = new CRIUSupport(imagePath);
criu.registerPostRestoreHook(new Runnable() {
public void run() {
CRIUTestUtils.showThreadCurrentTime("PreSnapshotHook() before synchronized on " + lock);
synchronized (lock) {
CRIUTestUtils.showThreadCurrentTime("PreSnapshotHook() within synchronized on " + lock);
}
CRIUTestUtils.showThreadCurrentTime("PreSnapshotHook() after synchronized on " + lock);
}
}
log("newThreadOwnMonitor() after synchronized on " + lock);
});

System.out.println("Pre-checkpoint");
CRIUTestUtils.checkPointJVM(criu, imagePath, false);
}
});
return t;
}

void testSingleThreadModeRestoreException() {
boolean result = false;
Thread tom = newThreadOwnMonitor();
tom.start();

CRIUTestUtils.deleteCheckpointDirectory(imagePath);
CRIUTestUtils.createCheckpointDirectory(imagePath);
CRIUSupport criu = new CRIUSupport(imagePath);
criu.registerPostRestoreHook(new Runnable() {
public void run() {
log("PreSnapshotHook() before synchronized on " + lock);
synchronized (lock) {
log("PreSnapshotHook() within synchronized on " + lock);
}
log("PreSnapshotHook() after synchronized on " + lock);
CRIUTestUtils.showThreadCurrentTime("testSingleThreadModeRestoreException() before synchronized on " + lock);
synchronized (lock) {
try {
// ensure the lock already taken before performing a checkpoint
CRIUTestUtils.showThreadCurrentTime("testSingleThreadModeRestoreException() before doCheckpoint()");
Thread tpc = doCheckpoint();
tpc.start();
// set timeout 10s
tpc.join(10000);
CRIUTestUtils.showThreadCurrentTime("testSingleThreadModeRestoreException() after doCheckpoint()");
} catch (InterruptedException e) {
}
});

System.out.println("Pre-checkpoint");
CRIUTestUtils.checkPointJVM(criu, imagePath, false);

tom.interrupt();
try {
tom.join();
} catch (InterruptedException e) {
}
CRIUTestUtils.showThreadCurrentTime("testSingleThreadModeRestoreException() after synchronized on " + lock);
}
}

0 comments on commit 81e4fd9

Please sign in to comment.