8062386: Different versions of nashorn use same code cache directory
authorhannesw
Wed, 05 Nov 2014 17:07:26 +0100
changeset 27366 c0fdaac978ea
parent 27365 6dd6e324d1c7
child 27367 47c1c541f45d
8062386: Different versions of nashorn use same code cache directory Reviewed-by: lagergren, attila
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/OptimisticTypesPersistence.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/CodeStore.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java
nashorn/test/src/jdk/nashorn/internal/runtime/CodeStoreAndPathTest.java
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/OptimisticTypesPersistence.java	Wed Nov 05 12:34:06 2014 +0100
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/OptimisticTypesPersistence.java	Wed Nov 05 17:07:26 2014 +0100
@@ -323,9 +323,11 @@
      * per-code-version directory. Normally, this will create the SHA-1 digest of the nashorn.jar. In case the classpath
      * for nashorn is local directory (e.g. during development), this will create the string "dev-" followed by the
      * timestamp of the most recent .class file.
-     * @return
+     *
+     * @return digest of currently running nashorn
+     * @throws Exception if digest could not be created
      */
-    private static String getVersionDirName() throws Exception {
+    public static String getVersionDirName() throws Exception {
         final URL url = OptimisticTypesPersistence.class.getResource("");
         final String protocol = url.getProtocol();
         if (protocol.equals("jar")) {
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/CodeStore.java	Wed Nov 05 12:34:06 2014 +0100
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/CodeStore.java	Wed Nov 05 17:07:26 2014 +0100
@@ -41,6 +41,7 @@
 import java.util.Iterator;
 import java.util.Map;
 import java.util.ServiceLoader;
+import jdk.nashorn.internal.codegen.OptimisticTypesPersistence;
 import jdk.nashorn.internal.codegen.types.Type;
 import jdk.nashorn.internal.runtime.logging.DebugLogger;
 import jdk.nashorn.internal.runtime.logging.Loggable;
@@ -102,7 +103,7 @@
         } catch (final AccessControlException e) {
             context.getLogger(CodeStore.class).warning("failed to load code store provider ", e);
         }
-        final CodeStore store = new DirectoryCodeStore();
+        final CodeStore store = new DirectoryCodeStore(context);
         store.initLogger(context);
         return store;
     }
@@ -210,32 +211,34 @@
         /**
          * Constructor
          *
+         * @param context the current context
          * @throws IOException if there are read/write problems with the cache and cache directory
          */
-        public DirectoryCodeStore() throws IOException {
-            this(Options.getStringProperty("nashorn.persistent.code.cache", "nashorn_code_cache"), false, DEFAULT_MIN_SIZE);
+        public DirectoryCodeStore(final Context context) throws IOException {
+            this(context, Options.getStringProperty("nashorn.persistent.code.cache", "nashorn_code_cache"), false, DEFAULT_MIN_SIZE);
         }
 
         /**
          * Constructor
          *
+         * @param context the current context
          * @param path    directory to store code in
          * @param readOnly is this a read only code store
          * @param minSize minimum file size for caching scripts
          * @throws IOException if there are read/write problems with the cache and cache directory
          */
-        public DirectoryCodeStore(final String path, final boolean readOnly, final int minSize) throws IOException {
-            this.dir = checkDirectory(path, readOnly);
+        public DirectoryCodeStore(final Context context, final String path, final boolean readOnly, final int minSize) throws IOException {
+            this.dir = checkDirectory(path, context.getEnv(), readOnly);
             this.readOnly = readOnly;
             this.minSize = minSize;
         }
 
-        private static File checkDirectory(final String path, final boolean readOnly) throws IOException {
+        private static File checkDirectory(final String path, final ScriptEnvironment env, final boolean readOnly) throws IOException {
             try {
                 return AccessController.doPrivileged(new PrivilegedExceptionAction<File>() {
                     @Override
                     public File run() throws IOException {
-                        final File dir = new File(path).getAbsoluteFile();
+                        final File dir = new File(path, getVersionDir(env)).getAbsoluteFile();
                         if (readOnly) {
                             if (!dir.exists() || !dir.isDirectory()) {
                                 throw new IOException("Not a directory: " + dir.getPath());
@@ -257,6 +260,15 @@
             }
         }
 
+        private static String getVersionDir(final ScriptEnvironment env) throws IOException {
+            try {
+                final String versionDir = OptimisticTypesPersistence.getVersionDirName();
+                return env._optimistic_types ? versionDir + "_opt" : versionDir;
+            } catch (final Exception e) {
+                throw new IOException(e);
+            }
+        }
+
         @Override
         public StoredScript load(final Source source, final String functionKey) {
             if (source.getLength() < minSize) {
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java	Wed Nov 05 12:34:06 2014 +0100
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java	Wed Nov 05 17:07:26 2014 +0100
@@ -1150,9 +1150,8 @@
 
         StoredScript storedScript = null;
         FunctionNode functionNode = null;
-        // We only use the code store here if optimistic types are disabled. With optimistic types,
-        // code is stored per function in RecompilableScriptFunctionData.
-        // TODO: This should really be triggered by lazy compilation, not optimistic types.
+        // We only use the code store here if optimistic types are disabled. With optimistic types, initial compilation
+        // just creates a thin wrapper, and actual code is stored per function in RecompilableScriptFunctionData.
         final boolean useCodeStore = env._persistent_cache && !env._parse_only && !env._optimistic_types;
         final String cacheKey = useCodeStore ? CodeStore.getCacheKey(0, null) : null;
 
--- a/nashorn/test/src/jdk/nashorn/internal/runtime/CodeStoreAndPathTest.java	Wed Nov 05 12:34:06 2014 +0100
+++ b/nashorn/test/src/jdk/nashorn/internal/runtime/CodeStoreAndPathTest.java	Wed Nov 05 17:07:26 2014 +0100
@@ -95,23 +95,15 @@
     final String codeCache = "build/nashorn_code_cache";
     final String oldUserDir = System.getProperty("user.dir");
 
-    private static final String[] ENGINE_OPTIONS = new String[]{"--persistent-code-cache", "--optimistic-types=false", "--lazy-compilation=false"};
-
-    public void checkCompiledScripts(final DirectoryStream<Path> stream, final int numberOfScripts) throws IOException {
-        int n = numberOfScripts;
-        for (@SuppressWarnings("unused") final Path file : stream) {
-            n--;
-        }
-        stream.close();
-        assertEquals(n, 0);
-    }
+    private static final String[] ENGINE_OPTIONS_OPT   = new String[]{"--persistent-code-cache", "--optimistic-types=true"};
+    private static final String[] ENGINE_OPTIONS_NOOPT = new String[]{"--persistent-code-cache", "--optimistic-types=false"};
 
     @Test
     public void pathHandlingTest() {
         System.setProperty("nashorn.persistent.code.cache", codeCache);
         final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
 
-        fac.getScriptEngine(ENGINE_OPTIONS);
+        fac.getScriptEngine(ENGINE_OPTIONS_NOOPT);
 
         final Path expectedCodeCachePath = FileSystems.getDefault().getPath(oldUserDir + File.separator + codeCache);
         final Path actualCodeCachePath = FileSystems.getDefault().getPath(System.getProperty(
@@ -128,9 +120,8 @@
     public void changeUserDirTest() throws ScriptException, IOException {
         System.setProperty("nashorn.persistent.code.cache", codeCache);
         final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
-        final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS);
-        final Path codeCachePath = FileSystems.getDefault().getPath(System.getProperty(
-                            "nashorn.persistent.code.cache")).toAbsolutePath();
+        final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_NOOPT);
+        final Path codeCachePath = getCodeCachePath(false);
         final String newUserDir = "build/newUserDir";
         // Now changing current working directory
         System.setProperty("user.dir", System.getProperty("user.dir") + File.separator + newUserDir);
@@ -149,9 +140,22 @@
     public void codeCacheTest() throws ScriptException, IOException {
         System.setProperty("nashorn.persistent.code.cache", codeCache);
         final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
-        final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS);
-        final Path codeCachePath = FileSystems.getDefault().getPath(System.getProperty(
-                            "nashorn.persistent.code.cache")).toAbsolutePath();
+        final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_NOOPT);
+        final Path codeCachePath = getCodeCachePath(false);
+        e.eval(code1);
+        e.eval(code2);
+        e.eval(code3);// less than minimum size for storing
+        // adding code1 and code2.
+        final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath);
+        checkCompiledScripts(stream, 2);
+    }
+
+    @Test
+    public void codeCacheTestOpt() throws ScriptException, IOException {
+        System.setProperty("nashorn.persistent.code.cache", codeCache);
+        final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
+        final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_OPT);
+        final Path codeCachePath = getCodeCachePath(true);
         e.eval(code1);
         e.eval(code2);
         e.eval(code3);// less than minimum size for storing
@@ -159,4 +163,26 @@
         final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath);
         checkCompiledScripts(stream, 2);
     }
+
+    private static Path getCodeCachePath(final boolean optimistic) {
+        final String codeCache = System.getProperty("nashorn.persistent.code.cache");
+        final Path codeCachePath = FileSystems.getDefault().getPath(codeCache).toAbsolutePath();
+        final String[] files = codeCachePath.toFile().list();
+        for (final String file : files) {
+            if (file.endsWith("_opt") == optimistic) {
+                return codeCachePath.resolve(file);
+            }
+        }
+        throw new AssertionError("Code cache path not found");
+    }
+
+    private static void checkCompiledScripts(final DirectoryStream<Path> stream, final int numberOfScripts) throws IOException {
+        int n = numberOfScripts;
+        for (@SuppressWarnings("unused") final Path file : stream) {
+            n--;
+        }
+        stream.close();
+        assertEquals(n, 0);
+    }
+
 }