8062386: Different versions of nashorn use same code cache directory

Wed, 05 Nov 2014 17:07:26 +0100

author
hannesw
date
Wed, 05 Nov 2014 17:07:26 +0100
changeset 1087
a119a11d49d8
parent 1086
d0b26e6f602c
child 1088
b49b6786afad

8062386: Different versions of nashorn use same code cache directory
Reviewed-by: lagergren, attila

src/jdk/nashorn/internal/codegen/OptimisticTypesPersistence.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/runtime/CodeStore.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/runtime/Context.java file | annotate | diff | comparison | revisions
test/src/jdk/nashorn/internal/runtime/CodeStoreAndPathTest.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/jdk/nashorn/internal/codegen/OptimisticTypesPersistence.java	Wed Nov 05 12:34:06 2014 +0100
     1.2 +++ b/src/jdk/nashorn/internal/codegen/OptimisticTypesPersistence.java	Wed Nov 05 17:07:26 2014 +0100
     1.3 @@ -323,9 +323,11 @@
     1.4       * per-code-version directory. Normally, this will create the SHA-1 digest of the nashorn.jar. In case the classpath
     1.5       * for nashorn is local directory (e.g. during development), this will create the string "dev-" followed by the
     1.6       * timestamp of the most recent .class file.
     1.7 -     * @return
     1.8 +     *
     1.9 +     * @return digest of currently running nashorn
    1.10 +     * @throws Exception if digest could not be created
    1.11       */
    1.12 -    private static String getVersionDirName() throws Exception {
    1.13 +    public static String getVersionDirName() throws Exception {
    1.14          final URL url = OptimisticTypesPersistence.class.getResource("");
    1.15          final String protocol = url.getProtocol();
    1.16          if (protocol.equals("jar")) {
     2.1 --- a/src/jdk/nashorn/internal/runtime/CodeStore.java	Wed Nov 05 12:34:06 2014 +0100
     2.2 +++ b/src/jdk/nashorn/internal/runtime/CodeStore.java	Wed Nov 05 17:07:26 2014 +0100
     2.3 @@ -41,6 +41,7 @@
     2.4  import java.util.Iterator;
     2.5  import java.util.Map;
     2.6  import java.util.ServiceLoader;
     2.7 +import jdk.nashorn.internal.codegen.OptimisticTypesPersistence;
     2.8  import jdk.nashorn.internal.codegen.types.Type;
     2.9  import jdk.nashorn.internal.runtime.logging.DebugLogger;
    2.10  import jdk.nashorn.internal.runtime.logging.Loggable;
    2.11 @@ -102,7 +103,7 @@
    2.12          } catch (final AccessControlException e) {
    2.13              context.getLogger(CodeStore.class).warning("failed to load code store provider ", e);
    2.14          }
    2.15 -        final CodeStore store = new DirectoryCodeStore();
    2.16 +        final CodeStore store = new DirectoryCodeStore(context);
    2.17          store.initLogger(context);
    2.18          return store;
    2.19      }
    2.20 @@ -210,32 +211,34 @@
    2.21          /**
    2.22           * Constructor
    2.23           *
    2.24 +         * @param context the current context
    2.25           * @throws IOException if there are read/write problems with the cache and cache directory
    2.26           */
    2.27 -        public DirectoryCodeStore() throws IOException {
    2.28 -            this(Options.getStringProperty("nashorn.persistent.code.cache", "nashorn_code_cache"), false, DEFAULT_MIN_SIZE);
    2.29 +        public DirectoryCodeStore(final Context context) throws IOException {
    2.30 +            this(context, Options.getStringProperty("nashorn.persistent.code.cache", "nashorn_code_cache"), false, DEFAULT_MIN_SIZE);
    2.31          }
    2.32  
    2.33          /**
    2.34           * Constructor
    2.35           *
    2.36 +         * @param context the current context
    2.37           * @param path    directory to store code in
    2.38           * @param readOnly is this a read only code store
    2.39           * @param minSize minimum file size for caching scripts
    2.40           * @throws IOException if there are read/write problems with the cache and cache directory
    2.41           */
    2.42 -        public DirectoryCodeStore(final String path, final boolean readOnly, final int minSize) throws IOException {
    2.43 -            this.dir = checkDirectory(path, readOnly);
    2.44 +        public DirectoryCodeStore(final Context context, final String path, final boolean readOnly, final int minSize) throws IOException {
    2.45 +            this.dir = checkDirectory(path, context.getEnv(), readOnly);
    2.46              this.readOnly = readOnly;
    2.47              this.minSize = minSize;
    2.48          }
    2.49  
    2.50 -        private static File checkDirectory(final String path, final boolean readOnly) throws IOException {
    2.51 +        private static File checkDirectory(final String path, final ScriptEnvironment env, final boolean readOnly) throws IOException {
    2.52              try {
    2.53                  return AccessController.doPrivileged(new PrivilegedExceptionAction<File>() {
    2.54                      @Override
    2.55                      public File run() throws IOException {
    2.56 -                        final File dir = new File(path).getAbsoluteFile();
    2.57 +                        final File dir = new File(path, getVersionDir(env)).getAbsoluteFile();
    2.58                          if (readOnly) {
    2.59                              if (!dir.exists() || !dir.isDirectory()) {
    2.60                                  throw new IOException("Not a directory: " + dir.getPath());
    2.61 @@ -257,6 +260,15 @@
    2.62              }
    2.63          }
    2.64  
    2.65 +        private static String getVersionDir(final ScriptEnvironment env) throws IOException {
    2.66 +            try {
    2.67 +                final String versionDir = OptimisticTypesPersistence.getVersionDirName();
    2.68 +                return env._optimistic_types ? versionDir + "_opt" : versionDir;
    2.69 +            } catch (final Exception e) {
    2.70 +                throw new IOException(e);
    2.71 +            }
    2.72 +        }
    2.73 +
    2.74          @Override
    2.75          public StoredScript load(final Source source, final String functionKey) {
    2.76              if (source.getLength() < minSize) {
     3.1 --- a/src/jdk/nashorn/internal/runtime/Context.java	Wed Nov 05 12:34:06 2014 +0100
     3.2 +++ b/src/jdk/nashorn/internal/runtime/Context.java	Wed Nov 05 17:07:26 2014 +0100
     3.3 @@ -1150,9 +1150,8 @@
     3.4  
     3.5          StoredScript storedScript = null;
     3.6          FunctionNode functionNode = null;
     3.7 -        // We only use the code store here if optimistic types are disabled. With optimistic types,
     3.8 -        // code is stored per function in RecompilableScriptFunctionData.
     3.9 -        // TODO: This should really be triggered by lazy compilation, not optimistic types.
    3.10 +        // We only use the code store here if optimistic types are disabled. With optimistic types, initial compilation
    3.11 +        // just creates a thin wrapper, and actual code is stored per function in RecompilableScriptFunctionData.
    3.12          final boolean useCodeStore = env._persistent_cache && !env._parse_only && !env._optimistic_types;
    3.13          final String cacheKey = useCodeStore ? CodeStore.getCacheKey(0, null) : null;
    3.14  
     4.1 --- a/test/src/jdk/nashorn/internal/runtime/CodeStoreAndPathTest.java	Wed Nov 05 12:34:06 2014 +0100
     4.2 +++ b/test/src/jdk/nashorn/internal/runtime/CodeStoreAndPathTest.java	Wed Nov 05 17:07:26 2014 +0100
     4.3 @@ -95,23 +95,15 @@
     4.4      final String codeCache = "build/nashorn_code_cache";
     4.5      final String oldUserDir = System.getProperty("user.dir");
     4.6  
     4.7 -    private static final String[] ENGINE_OPTIONS = new String[]{"--persistent-code-cache", "--optimistic-types=false", "--lazy-compilation=false"};
     4.8 -
     4.9 -    public void checkCompiledScripts(final DirectoryStream<Path> stream, final int numberOfScripts) throws IOException {
    4.10 -        int n = numberOfScripts;
    4.11 -        for (@SuppressWarnings("unused") final Path file : stream) {
    4.12 -            n--;
    4.13 -        }
    4.14 -        stream.close();
    4.15 -        assertEquals(n, 0);
    4.16 -    }
    4.17 +    private static final String[] ENGINE_OPTIONS_OPT   = new String[]{"--persistent-code-cache", "--optimistic-types=true"};
    4.18 +    private static final String[] ENGINE_OPTIONS_NOOPT = new String[]{"--persistent-code-cache", "--optimistic-types=false"};
    4.19  
    4.20      @Test
    4.21      public void pathHandlingTest() {
    4.22          System.setProperty("nashorn.persistent.code.cache", codeCache);
    4.23          final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
    4.24  
    4.25 -        fac.getScriptEngine(ENGINE_OPTIONS);
    4.26 +        fac.getScriptEngine(ENGINE_OPTIONS_NOOPT);
    4.27  
    4.28          final Path expectedCodeCachePath = FileSystems.getDefault().getPath(oldUserDir + File.separator + codeCache);
    4.29          final Path actualCodeCachePath = FileSystems.getDefault().getPath(System.getProperty(
    4.30 @@ -128,9 +120,8 @@
    4.31      public void changeUserDirTest() throws ScriptException, IOException {
    4.32          System.setProperty("nashorn.persistent.code.cache", codeCache);
    4.33          final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
    4.34 -        final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS);
    4.35 -        final Path codeCachePath = FileSystems.getDefault().getPath(System.getProperty(
    4.36 -                            "nashorn.persistent.code.cache")).toAbsolutePath();
    4.37 +        final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_NOOPT);
    4.38 +        final Path codeCachePath = getCodeCachePath(false);
    4.39          final String newUserDir = "build/newUserDir";
    4.40          // Now changing current working directory
    4.41          System.setProperty("user.dir", System.getProperty("user.dir") + File.separator + newUserDir);
    4.42 @@ -149,9 +140,8 @@
    4.43      public void codeCacheTest() throws ScriptException, IOException {
    4.44          System.setProperty("nashorn.persistent.code.cache", codeCache);
    4.45          final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
    4.46 -        final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS);
    4.47 -        final Path codeCachePath = FileSystems.getDefault().getPath(System.getProperty(
    4.48 -                            "nashorn.persistent.code.cache")).toAbsolutePath();
    4.49 +        final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_NOOPT);
    4.50 +        final Path codeCachePath = getCodeCachePath(false);
    4.51          e.eval(code1);
    4.52          e.eval(code2);
    4.53          e.eval(code3);// less than minimum size for storing
    4.54 @@ -159,4 +149,40 @@
    4.55          final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath);
    4.56          checkCompiledScripts(stream, 2);
    4.57      }
    4.58 +
    4.59 +    @Test
    4.60 +    public void codeCacheTestOpt() throws ScriptException, IOException {
    4.61 +        System.setProperty("nashorn.persistent.code.cache", codeCache);
    4.62 +        final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
    4.63 +        final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_OPT);
    4.64 +        final Path codeCachePath = getCodeCachePath(true);
    4.65 +        e.eval(code1);
    4.66 +        e.eval(code2);
    4.67 +        e.eval(code3);// less than minimum size for storing
    4.68 +        // adding code1 and code2.
    4.69 +        final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath);
    4.70 +        checkCompiledScripts(stream, 2);
    4.71 +    }
    4.72 +
    4.73 +    private static Path getCodeCachePath(final boolean optimistic) {
    4.74 +        final String codeCache = System.getProperty("nashorn.persistent.code.cache");
    4.75 +        final Path codeCachePath = FileSystems.getDefault().getPath(codeCache).toAbsolutePath();
    4.76 +        final String[] files = codeCachePath.toFile().list();
    4.77 +        for (final String file : files) {
    4.78 +            if (file.endsWith("_opt") == optimistic) {
    4.79 +                return codeCachePath.resolve(file);
    4.80 +            }
    4.81 +        }
    4.82 +        throw new AssertionError("Code cache path not found");
    4.83 +    }
    4.84 +
    4.85 +    private static void checkCompiledScripts(final DirectoryStream<Path> stream, final int numberOfScripts) throws IOException {
    4.86 +        int n = numberOfScripts;
    4.87 +        for (@SuppressWarnings("unused") final Path file : stream) {
    4.88 +            n--;
    4.89 +        }
    4.90 +        stream.close();
    4.91 +        assertEquals(n, 0);
    4.92 +    }
    4.93 +
    4.94  }

mercurial