From 5191e7379adf83e4511136f184c02eef935a89eb Mon Sep 17 00:00:00 2001 From: Lukas Stadler <lukas.stadler@oracle.com> Date: Mon, 12 Feb 2018 13:42:51 +0100 Subject: [PATCH] create SHARED_PARENT_RW contexts in the same Engine --- .../truffle/r/nodes/test/ChimneySweeping.java | 5 +- .../r/nodes/test/DefaultArgsExtractor.java | 3 +- .../oracle/truffle/r/nodes/test/TestBase.java | 4 +- .../com/oracle/truffle/r/test/TestBase.java | 14 ++-- .../r/test/builtins/TestBuiltin_asmatrix.java | 5 +- .../truffle/r/test/generate/FastRSession.java | 67 ++++++++----------- .../r/test/generate/GnuROneShotRSession.java | 4 +- .../truffle/r/test/generate/RSession.java | 4 +- .../test/library/base/TestSimpleVectors.java | 9 ++- .../r/test/library/fastr/TestInterop.java | 9 +-- 10 files changed, 62 insertions(+), 62 deletions(-) diff --git a/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/ChimneySweeping.java b/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/ChimneySweeping.java index 4ba8401d17..10f273d4c7 100644 --- a/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/ChimneySweeping.java +++ b/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/ChimneySweeping.java @@ -53,6 +53,7 @@ import com.oracle.truffle.r.runtime.RError; import com.oracle.truffle.r.runtime.RSource; import com.oracle.truffle.r.runtime.ResourceHandlerFactory; import com.oracle.truffle.r.runtime.builtins.RBuiltinKind; +import com.oracle.truffle.r.runtime.context.RContext.ContextKind; import com.oracle.truffle.r.runtime.data.RList; import com.oracle.truffle.r.runtime.data.RMissing; import com.oracle.truffle.r.runtime.data.RNull; @@ -224,7 +225,7 @@ class ChimneySweeping extends SingleBuiltinDiagnostics { @Override SingleBuiltinDiagnostics init() throws Throwable { super.init(); - try (Context context = diagSuite.fastRSession.createContext()) { + try (Context context = diagSuite.fastRSession.createContext(ContextKind.SHARE_PARENT_RW)) { execInContext(context, () -> { this.castNodes = builtinFactory.getCastNodes(); print(0, "\n*** Chimney-sweeping of '" + builtinName + "' (" + builtinFactory.getBuiltinMetaClass().getName() + ") ***"); @@ -488,7 +489,7 @@ class ChimneySweeping extends SingleBuiltinDiagnostics { String output; try { - output = diagSuite.fastRSession.eval(null, call, null, false); + output = diagSuite.fastRSession.eval(null, call, ContextKind.SHARE_PARENT_RW, false); } catch (Throwable t) { output = "ERROR: " + t.getMessage(); } diff --git a/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/DefaultArgsExtractor.java b/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/DefaultArgsExtractor.java index de6153e10d..e9854ddb6e 100644 --- a/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/DefaultArgsExtractor.java +++ b/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/DefaultArgsExtractor.java @@ -31,6 +31,7 @@ import java.util.function.Consumer; import com.oracle.truffle.r.nodes.casts.Samples; import com.oracle.truffle.r.runtime.RDeparse; import com.oracle.truffle.r.runtime.RSource; +import com.oracle.truffle.r.runtime.context.RContext.ContextKind; import com.oracle.truffle.r.runtime.data.RLanguage; import com.oracle.truffle.r.runtime.data.RNull; import com.oracle.truffle.r.runtime.data.RPairList; @@ -56,7 +57,7 @@ class DefaultArgsExtractor { } Map<String, Samples<?>> extractDefaultArgs(String functionName) { - final Context context = fastRSession.createContext(); + final Context context = fastRSession.createContext(ContextKind.SHARE_PARENT_RW); HashMap<String, Samples<?>> samplesMap = new HashMap<>(); try { diff --git a/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/TestBase.java b/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/TestBase.java index 3ae46c2727..34be425167 100644 --- a/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/TestBase.java +++ b/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/TestBase.java @@ -24,6 +24,8 @@ package com.oracle.truffle.r.nodes.test; import com.oracle.truffle.r.runtime.RSource; import com.oracle.truffle.r.runtime.context.RContext; +import com.oracle.truffle.r.runtime.context.RContext.ContextKind; + import org.junit.AfterClass; import org.junit.BeforeClass; @@ -45,7 +47,7 @@ public class TestBase { public static void setupClass() { FastRSession session = FastRSession.create(); testVMContext = session.getContext(); - context = session.createContext(); + context = session.createContext(ContextKind.SHARE_PARENT_RW); } @AfterClass diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/TestBase.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/TestBase.java index 42217d82d1..70ac71adbb 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/TestBase.java +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/TestBase.java @@ -35,6 +35,7 @@ import java.util.function.Predicate; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.graalvm.polyglot.Engine; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -46,13 +47,11 @@ import com.oracle.truffle.r.runtime.FastROptions; import com.oracle.truffle.r.runtime.RInternalError; import com.oracle.truffle.r.runtime.ResourceHandlerFactory; import com.oracle.truffle.r.runtime.Utils; -import com.oracle.truffle.r.runtime.context.ChildContextInfo; import com.oracle.truffle.r.runtime.context.RContext; import com.oracle.truffle.r.runtime.context.RContext.ContextKind; import com.oracle.truffle.r.test.generate.FastRSession; import com.oracle.truffle.r.test.generate.GnuROneShotRSession; import com.oracle.truffle.r.test.generate.TestOutputManager; -import org.graalvm.polyglot.Engine; /** * Base class for all unit tests. The unit tests are actually arranged as a collection of @@ -639,7 +638,7 @@ public class TestBase { private boolean evalAndCompare(String[] inputs, TestTrait... traitsList) { WhiteList[] whiteLists = TestTrait.collect(traitsList, WhiteList.class); TestTraitsSet traits = new TestTraitsSet(traitsList); - ChildContextInfo contextInfo = traits.context.contains(Context.NonShared) ? fastROutputManager.fastRSession.createContextInfo(ContextKind.SHARE_NOTHING) : null; + ContextKind contextKind = traits.context.contains(Context.NonShared) ? ContextKind.SHARE_NOTHING : ContextKind.SHARE_PARENT_RW; int index = 1; boolean allOk = true; boolean skipFastREval = traits.isIgnored || generatingExpected(); @@ -648,7 +647,7 @@ public class TestBase { if (skipFastREval) { ignoredInputCount++; } else { - String result = fastREval(input, contextInfo, traits.context.contains(Context.LongTimeout)); + String result = fastREval(input, contextKind, traits.context.contains(Context.LongTimeout)); CheckResult checkResult = checkResult(whiteLists, input, traits.preprocessOutput(expected), traits.preprocessOutput(result), traits); result = checkResult.result; @@ -981,12 +980,13 @@ public class TestBase { * Evaluate {@code input} in FastR, returning all (virtual) console output that was produced. If * {@code nonShared} then this must evaluate in a new, non-shared, {@link RContext}. */ - protected String fastREval(String input, ChildContextInfo contextInfo, boolean longTimeout) { + protected String fastREval(String input, ContextKind contextKind, boolean longTimeout) { + assert contextKind != null; microTestInfo.expression = input; String result; try { beforeEval(); - result = fastROutputManager.fastRSession.eval(this, input, contextInfo, longTimeout); + result = fastROutputManager.fastRSession.eval(this, input, contextKind, longTimeout); } catch (Throwable e) { String clazz; if (e instanceof RInternalError && e.getCause() != null) { @@ -1025,7 +1025,7 @@ public class TestBase { if (generatingExpected()) { return expectedOutputManager.getRSession().eval(null, system2Command, null, true); } else { - return fastROutputManager.fastRSession.eval(null, system2Command, null, true); + return fastROutputManager.fastRSession.eval(null, system2Command, ContextKind.SHARE_PARENT_RW, true); } } diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_asmatrix.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_asmatrix.java index c5201e71ff..11480f6850 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_asmatrix.java +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_asmatrix.java @@ -13,6 +13,7 @@ package com.oracle.truffle.r.test.builtins; import org.junit.Assert; import org.junit.Test; +import com.oracle.truffle.r.runtime.context.RContext.ContextKind; import com.oracle.truffle.r.test.TestBase; // Checkstyle: stop line length check @@ -41,12 +42,12 @@ public class TestBuiltin_asmatrix extends TestBase { @Test public void testIgnoredMatrixExpression1() { - Assert.assertEquals(expectedOut1, fastREval("{ matrix(1i,10,10) }", null, false)); + Assert.assertEquals(expectedOut1, fastREval("{ matrix(1i,10,10) }", ContextKind.SHARE_PARENT_RW, false)); } @Test public void testIgnoredMatrixExpression2() { - Assert.assertEquals(expectedOut2, fastREval("{ matrix(c(1i,NA),10,10) }", null, false)); + Assert.assertEquals(expectedOut2, fastREval("{ matrix(c(1i,NA),10,10) }", ContextKind.SHARE_PARENT_RW, false)); } @Test diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/generate/FastRSession.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/generate/FastRSession.java index 22ac16e2b7..c789f90385 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/generate/FastRSession.java +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/generate/FastRSession.java @@ -22,9 +22,15 @@ */ package com.oracle.truffle.r.test.generate; +import static org.junit.Assert.fail; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.lang.reflect.AccessibleObject; +import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.CharsetDecoder; @@ -34,6 +40,17 @@ import java.util.HashMap; import java.util.Map; import java.util.Timer; import java.util.TimerTask; +import java.util.concurrent.Callable; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.graalvm.polyglot.Context; +import org.graalvm.polyglot.Engine; +import org.graalvm.polyglot.Instrument; +import org.graalvm.polyglot.PolyglotException; +import org.graalvm.polyglot.Source; +import org.graalvm.polyglot.Value; +import org.graalvm.polyglot.proxy.ProxyExecutable; import com.oracle.truffle.api.debug.Debugger; import com.oracle.truffle.api.debug.SuspendedCallback; @@ -53,21 +70,6 @@ import com.oracle.truffle.r.runtime.context.RContext; import com.oracle.truffle.r.runtime.context.RContext.ContextKind; import com.oracle.truffle.r.test.TestBase; import com.oracle.truffle.r.test.engine.interop.VectorMRTest; -import java.io.IOException; -import java.lang.reflect.AccessibleObject; -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.util.concurrent.Callable; -import java.util.logging.Level; -import java.util.logging.Logger; -import org.graalvm.polyglot.Context; -import org.graalvm.polyglot.Engine; -import org.graalvm.polyglot.Instrument; -import org.graalvm.polyglot.PolyglotException; -import org.graalvm.polyglot.Source; -import org.graalvm.polyglot.Value; -import org.graalvm.polyglot.proxy.ProxyExecutable; -import static org.junit.Assert.fail; public final class FastRSession implements RSession { @@ -86,6 +88,7 @@ public final class FastRSession implements RSession { private final ByteArrayOutputStream output = new ByteArrayOutputStream(); private final TestByteArrayInputStream input = new TestByteArrayInputStream(); + private Engine mainEngine; private Context mainContext; private RContext mainRContext; @@ -124,27 +127,14 @@ public final class FastRSession implements RSession { return null; } - public ChildContextInfo checkContext(ChildContextInfo contextInfoArg) { - ChildContextInfo contextInfo; - if (contextInfoArg == null) { - contextInfo = createContextInfo(ContextKind.SHARE_PARENT_RW); - } else { - contextInfo = contextInfoArg; - } - return contextInfo; - } - - public ChildContextInfo createContextInfo(ContextKind contextKind) { + public Context createContext(ContextKind contextKind) { RStartParams params = new RStartParams(RCmdOptions.parseArguments(Client.R, new String[]{"R", "--vanilla", "--slave", "--silent", "--no-restore"}, false), false); Map<String, String> env = new HashMap<>(); env.put("TZ", "GMT"); - ChildContextInfo ctx = ChildContextInfo.create(params, env, contextKind, mainRContext, input, output, output); + ChildContextInfo ctx = ChildContextInfo.create(params, env, contextKind, contextKind == ContextKind.SHARE_NOTHING ? null : mainRContext, input, output, output); RContext.childInfo = ctx; - return ctx; - } - public Context createContext() { - return Context.newBuilder("R").in(input).out(output).err(output).build(); + return Context.newBuilder("R").engine(mainEngine).build(); } private FastRSession() { @@ -163,7 +153,8 @@ public final class FastRSession implements RSession { RStartParams params = new RStartParams(RCmdOptions.parseArguments(Client.R, new String[]{"R", "--vanilla", "--slave", "--silent", "--no-restore"}, false), false); ChildContextInfo info = ChildContextInfo.create(params, null, ContextKind.SHARE_NOTHING, null, input, output, output); RContext.childInfo = info; - mainContext = createContext(); + mainEngine = Engine.newBuilder().in(input).out(output).err(output).build(); + mainContext = Context.newBuilder("R").engine(mainEngine).build(); mainRContext = mainContext.eval(GET_CONTEXT).asHostObject(); } finally { try { @@ -187,8 +178,8 @@ public final class FastRSession implements RSession { private String readLine() { /* - * We cannot use an InputStreamReader because it buffers characters internally, whereas - * readLine() should not buffer across newlines. + * We cannot use an InputStreamReader because it buffers characters internally, whereas readLine() + * should not buffer across newlines. */ ByteBuffer bytes = ByteBuffer.allocate(16); @@ -219,13 +210,13 @@ public final class FastRSession implements RSession { } @Override - public String eval(TestBase testClass, String expression, ChildContextInfo contextInfo, boolean longTimeout) throws Throwable { + public String eval(TestBase testClass, String expression, ContextKind contextKind, boolean longTimeout) throws Throwable { + assert contextKind != null; Timer timer = null; output.reset(); input.setContents(expression); try { - checkContext(contextInfo); - Context evalContext = createContext(); + Context evalContext = createContext(contextKind); // set up some interop objects used by fastr-specific tests: if (testClass != null) { testClass.addPolyglotSymbols(evalContext); @@ -284,7 +275,7 @@ public final class FastRSession implements RSession { } } - private Throwable getWrappedThrowable(PolyglotException e) { + private static Throwable getWrappedThrowable(PolyglotException e) { Object f = getField(e, "impl"); return (Throwable) getField(f, "exception"); } diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/generate/GnuROneShotRSession.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/generate/GnuROneShotRSession.java index 56b29da808..5533c299b1 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/generate/GnuROneShotRSession.java +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/generate/GnuROneShotRSession.java @@ -30,7 +30,7 @@ import java.util.concurrent.TimeUnit; import com.oracle.truffle.r.runtime.ProcessOutputManager; import com.oracle.truffle.r.runtime.REnvVars; -import com.oracle.truffle.r.runtime.context.ChildContextInfo; +import com.oracle.truffle.r.runtime.context.RContext.ContextKind; import com.oracle.truffle.r.test.TestBase; /** @@ -91,7 +91,7 @@ public class GnuROneShotRSession implements RSession { } @Override - public String eval(TestBase testBase, String expression, ChildContextInfo contextInfo, boolean longTimeout) throws Throwable { + public String eval(TestBase testBase, String expression, ContextKind contextKind, boolean longTimeout) throws Throwable { if (expression.contains("library(") && !TestBase.generatingExpected()) { System.out.println("=============================================="); System.out.println("LIBRARY LOADING WHEN NOT GENERATING EXPECTED OUTPUT"); diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/generate/RSession.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/generate/RSession.java index 6783accf75..0796d64b58 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/generate/RSession.java +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/generate/RSession.java @@ -22,7 +22,7 @@ */ package com.oracle.truffle.r.test.generate; -import com.oracle.truffle.r.runtime.context.ChildContextInfo; +import com.oracle.truffle.r.runtime.context.RContext.ContextKind; import com.oracle.truffle.r.test.TestBase; /** @@ -42,7 +42,7 @@ public interface RSession { * This result will always be non-null or an exception will be thrown in, say, a timeout * occurring. */ - String eval(TestBase testClass, String expression, ChildContextInfo contextInfo, boolean longTimeout) throws Throwable; + String eval(TestBase testClass, String expression, ContextKind contextKind, boolean longTimeout) throws Throwable; /** * A name to identify the session. diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/base/TestSimpleVectors.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/base/TestSimpleVectors.java index 31ec7d55f0..fca629270d 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/base/TestSimpleVectors.java +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/base/TestSimpleVectors.java @@ -13,6 +13,7 @@ package com.oracle.truffle.r.test.library.base; import org.junit.Assert; import org.junit.Test; +import com.oracle.truffle.r.runtime.context.RContext.ContextKind; import com.oracle.truffle.r.test.TestBase; public class TestSimpleVectors extends TestBase { @@ -1071,19 +1072,21 @@ public class TestSimpleVectors extends TestBase { @Test public void testIgnored1() { Assert.assertEquals(TEST_IGNORED1_EXPECTED_VAL, - fastREval("{ x <- c(a=as.raw(10), b=as.raw(11), c=as.raw(12)) ; x[10] }", null, false)); + fastREval("{ x <- c(a=as.raw(10), b=as.raw(11), c=as.raw(12)) ; x[10] }", ContextKind.SHARE_PARENT_RW, false)); } @Test public void testIgnored2() { Assert.assertEquals(TEST_IGNORED2_EXPECTED_VAL, - fastREval("{ x <- c(a=as.raw(10),b=as.raw(11),c=as.raw(12),d=as.raw(13)) ; f <- function(s) { x[s] } ; f(TRUE) ; f(1L) ; f(as.character(NA)) }", null, false)); + fastREval("{ x <- c(a=as.raw(10),b=as.raw(11),c=as.raw(12),d=as.raw(13)) ; f <- function(s) { x[s] } ; f(TRUE) ; f(1L) ; f(as.character(NA)) }", ContextKind.SHARE_PARENT_RW, + false)); } @Test public void testIgnored3() { Assert.assertEquals(TEST_IGNORED3_EXPECTED_VAL, - fastREval("{ x <- c(a=as.raw(10),b=as.raw(11),c=as.raw(12),d=as.raw(13)) ; f <- function(s) { x[c(s,s)] } ; f(TRUE) ; f(1L) ; f(as.character(NA)) }", null, false)); + fastREval("{ x <- c(a=as.raw(10),b=as.raw(11),c=as.raw(12),d=as.raw(13)) ; f <- function(s) { x[c(s,s)] } ; f(TRUE) ; f(1L) ; f(as.character(NA)) }", + ContextKind.SHARE_PARENT_RW, false)); } @Test diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/fastr/TestInterop.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/fastr/TestInterop.java index 9600176435..59975e1a8e 100644 --- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/fastr/TestInterop.java +++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/fastr/TestInterop.java @@ -30,6 +30,7 @@ import org.junit.Test; import com.oracle.truffle.api.interop.java.JavaInterop; import com.oracle.truffle.r.runtime.conn.SeekableMemoryByteChannel; +import com.oracle.truffle.r.runtime.context.RContext.ContextKind; import com.oracle.truffle.r.test.TestBase; import java.io.File; import org.junit.After; @@ -108,12 +109,12 @@ public class TestInterop extends TestBase { @Test public void testHelp() { - assertHelpResult(fastREval("?as.external.byte", null, false), "==== R Help on ‘as.external.byte’ ====", "converted to a byte", "byteClass$valueOf(javaByte)"); - assertHelpResult(fastREval("help(as.external.byte)", null, false), "==== R Help on ‘as.external.byte’ ====", "converted to a byte", "byteClass$valueOf(javaByte)"); - assertHelpResult(fastREval("example(as.external.byte)", null, false), null, "byteClass$valueOf(javaByte)", "[1] 123"); + assertHelpResult(fastREval("?as.external.byte", ContextKind.SHARE_PARENT_RW, false), "==== R Help on ‘as.external.byte’ ====", "converted to a byte", "byteClass$valueOf(javaByte)"); + assertHelpResult(fastREval("help(as.external.byte)", ContextKind.SHARE_PARENT_RW, false), "==== R Help on ‘as.external.byte’ ====", "converted to a byte", "byteClass$valueOf(javaByte)"); + assertHelpResult(fastREval("example(as.external.byte)", ContextKind.SHARE_PARENT_RW, false), null, "byteClass$valueOf(javaByte)", "[1] 123"); } - private void assertHelpResult(String result, String startsWith, String... contains) { + private static void assertHelpResult(String result, String startsWith, String... contains) { if (startsWith != null) { assertTrue(result.startsWith(startsWith)); } -- GitLab