Skip to content

Commit

Permalink
[GR-25759] [GR-25774] Make unit tests resilient to the encoded graph …
Browse files Browse the repository at this point in the history
…cache being purged by idle compiler threads.

PullRequest: graal/7024
  • Loading branch information
mukel committed Aug 25, 2020
2 parents 64460fa + 612d2af commit c9b84a4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -805,10 +805,12 @@ private void compile(String classPath, LibGraalParams libgraal) throws IOExcepti
float selector = Math.max(0, startAtCompile);
float selectorStep = maxCompiles < allCompiles ? (float) allCompiles / maxCompiles : 1.0f;
int repeat = Options.Repeat.getValue(harnessOptions);
long taskCount = 0;
for (Map.Entry<HotSpotResolvedJavaMethod, Integer> e : toBeCompiled.entrySet()) {
if (compilationNum >= startAtCompile && compilationNum < stopAtCompile) {
if (Math.round(selector) == compilationNum) {
for (int i = 0; i < repeat; i++) {
taskCount++;
threadPool.submit(new Runnable() {
@Override
public void run() {
Expand All @@ -825,7 +827,6 @@ public void run() {
int wakeups = 0;
long lastCompletedTaskCount = 0;
int statsInterval = Options.StatsInterval.getValue(harnessOptions);
long taskCount = threadPool.getTaskCount();
long completedTaskCount;
do {
completedTaskCount = threadPool.getCompletedTaskCount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,15 @@ public void testCacheInvalidation() {
encodedGraphCacheContains(truffleCompiler, testMethod));

// Retry again, the encoded graph is re-parsed without the (invalidated) assumption.
// Compilation succeeds.
callTarget = compileAST(rootTestNode);

assertTrue("Re-parsed graph is in the cache",
encodedGraphCacheContains(truffleCompiler, testMethod));
boolean[] graphWasCached = {false};
for (int attempts = 0; attempts < 10 && !graphWasCached[0]; attempts++) {
// Compilation succeeds.
callTarget = compileAST(rootTestNode);
// But the cache can be purged anytime, retry if the graph is not cached.
graphWasCached[0] = encodedGraphCacheContains(truffleCompiler, testMethod);
}

Assert.assertTrue("Re-parsed graph was cached", graphWasCached[0]);
Assert.assertEquals(42, (int) callTarget.call());
}

Expand All @@ -218,27 +221,39 @@ public void testCacheIsDisabled() {

@Test
public void testCacheIsEnabled() {
testHelper(100, 100_000, compiler -> {
assertTrue("InvalidationTestNode.execute is cached",
encodedGraphCacheContains(compiler, testMethod));
});
boolean[] cacheContainsTargetGraph = {false};
for (int attempts = 0; attempts < 10 && !cacheContainsTargetGraph[0]; attempts++) {
testHelper(100, 100_000, compiler -> {
cacheContainsTargetGraph[0] = encodedGraphCacheContains(compiler, testMethod);
});
}
Assert.assertTrue("InvalidationTestNode.execute is cached", cacheContainsTargetGraph[0]);
}

@Test
public void testCacheCapacity() {
testHelper(1, 100_000, compiler -> {
EconomicMap<?, ?> cache = compiler.getPartialEvaluator().getOrCreateEncodedGraphCache();
// A single compilation should cache more than 1 graph.
Assert.assertEquals("Cache can hold at most 1 element", 1, cache.size());
});
boolean[] cacheHolds1Element = {false};
for (int attempts = 0; attempts < 10 && !cacheHolds1Element[0]; attempts++) {
testHelper(1, 100_000, compiler -> {
EconomicMap<?, ?> cache = compiler.getPartialEvaluator().getOrCreateEncodedGraphCache();
// The cache can have at most 1 element, but it can be purged anytime.
Assert.assertTrue("Cache holds at most 1 element", cache.size() <= 1);
cacheHolds1Element[0] = (cache.size() == 1); // can be empty
});
}
Assert.assertTrue("Cache holds exactly 1 element", cacheHolds1Element[0]);
}

@Test
public void testUnboundedCacheCapacity() {
testHelper(-1, 100_000, compiler -> {
EconomicMap<?, ?> cache = compiler.getPartialEvaluator().getOrCreateEncodedGraphCache();
Assert.assertFalse("Cache has some elements", cache.isEmpty());
});
boolean[] nonEmptyGraphCache = {false};
for (int attempts = 0; attempts < 10 && !nonEmptyGraphCache[0]; attempts++) {
testHelper(-1, 100_000, compiler -> {
EconomicMap<?, ?> cache = compiler.getPartialEvaluator().getOrCreateEncodedGraphCache();
nonEmptyGraphCache[0] = !cache.isEmpty();
});
}
Assert.assertTrue("Unbounded cache was populated", nonEmptyGraphCache[0]);
}

@Test
Expand Down

0 comments on commit c9b84a4

Please sign in to comment.