8062386: Different versions of nashorn use same code cache directory

Reviewed-by: lagergren, attila
This commit is contained in:
Hannes Wallnöfer 2014-11-05 17:07:26 +01:00
parent fe0da815c5
commit 043a07e654
4 changed files with 68 additions and 29 deletions

View File

@ -323,9 +323,11 @@ public final class OptimisticTypesPersistence {
* per-code-version directory. Normally, this will create the SHA-1 digest of the nashorn.jar. In case the classpath * 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 * 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. * 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 URL url = OptimisticTypesPersistence.class.getResource("");
final String protocol = url.getProtocol(); final String protocol = url.getProtocol();
if (protocol.equals("jar")) { if (protocol.equals("jar")) {

View File

@ -41,6 +41,7 @@ import java.security.PrivilegedExceptionAction;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.ServiceLoader; import java.util.ServiceLoader;
import jdk.nashorn.internal.codegen.OptimisticTypesPersistence;
import jdk.nashorn.internal.codegen.types.Type; import jdk.nashorn.internal.codegen.types.Type;
import jdk.nashorn.internal.runtime.logging.DebugLogger; import jdk.nashorn.internal.runtime.logging.DebugLogger;
import jdk.nashorn.internal.runtime.logging.Loggable; import jdk.nashorn.internal.runtime.logging.Loggable;
@ -102,7 +103,7 @@ public abstract class CodeStore implements Loggable {
} catch (final AccessControlException e) { } catch (final AccessControlException e) {
context.getLogger(CodeStore.class).warning("failed to load code store provider ", 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); store.initLogger(context);
return store; return store;
} }
@ -210,32 +211,34 @@ public abstract class CodeStore implements Loggable {
/** /**
* Constructor * Constructor
* *
* @param context the current context
* @throws IOException if there are read/write problems with the cache and cache directory * @throws IOException if there are read/write problems with the cache and cache directory
*/ */
public DirectoryCodeStore() throws IOException { public DirectoryCodeStore(final Context context) throws IOException {
this(Options.getStringProperty("nashorn.persistent.code.cache", "nashorn_code_cache"), false, DEFAULT_MIN_SIZE); this(context, Options.getStringProperty("nashorn.persistent.code.cache", "nashorn_code_cache"), false, DEFAULT_MIN_SIZE);
} }
/** /**
* Constructor * Constructor
* *
* @param context the current context
* @param path directory to store code in * @param path directory to store code in
* @param readOnly is this a read only code store * @param readOnly is this a read only code store
* @param minSize minimum file size for caching scripts * @param minSize minimum file size for caching scripts
* @throws IOException if there are read/write problems with the cache and cache directory * @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 { public DirectoryCodeStore(final Context context, final String path, final boolean readOnly, final int minSize) throws IOException {
this.dir = checkDirectory(path, readOnly); this.dir = checkDirectory(path, context.getEnv(), readOnly);
this.readOnly = readOnly; this.readOnly = readOnly;
this.minSize = minSize; 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 { try {
return AccessController.doPrivileged(new PrivilegedExceptionAction<File>() { return AccessController.doPrivileged(new PrivilegedExceptionAction<File>() {
@Override @Override
public File run() throws IOException { public File run() throws IOException {
final File dir = new File(path).getAbsoluteFile(); final File dir = new File(path, getVersionDir(env)).getAbsoluteFile();
if (readOnly) { if (readOnly) {
if (!dir.exists() || !dir.isDirectory()) { if (!dir.exists() || !dir.isDirectory()) {
throw new IOException("Not a directory: " + dir.getPath()); throw new IOException("Not a directory: " + dir.getPath());
@ -257,6 +260,15 @@ public abstract class CodeStore implements Loggable {
} }
} }
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 @Override
public StoredScript load(final Source source, final String functionKey) { public StoredScript load(final Source source, final String functionKey) {
if (source.getLength() < minSize) { if (source.getLength() < minSize) {

View File

@ -1150,9 +1150,8 @@ public final class Context {
StoredScript storedScript = null; StoredScript storedScript = null;
FunctionNode functionNode = null; FunctionNode functionNode = null;
// We only use the code store here if optimistic types are disabled. With optimistic types, // We only use the code store here if optimistic types are disabled. With optimistic types, initial compilation
// code is stored per function in RecompilableScriptFunctionData. // just creates a thin wrapper, and actual code is stored per function in RecompilableScriptFunctionData.
// TODO: This should really be triggered by lazy compilation, not optimistic types.
final boolean useCodeStore = env._persistent_cache && !env._parse_only && !env._optimistic_types; final boolean useCodeStore = env._persistent_cache && !env._parse_only && !env._optimistic_types;
final String cacheKey = useCodeStore ? CodeStore.getCacheKey(0, null) : null; final String cacheKey = useCodeStore ? CodeStore.getCacheKey(0, null) : null;

View File

@ -95,23 +95,15 @@ public class CodeStoreAndPathTest {
final String codeCache = "build/nashorn_code_cache"; final String codeCache = "build/nashorn_code_cache";
final String oldUserDir = System.getProperty("user.dir"); final String oldUserDir = System.getProperty("user.dir");
private static final String[] ENGINE_OPTIONS = new String[]{"--persistent-code-cache", "--optimistic-types=false", "--lazy-compilation=false"}; 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"};
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);
}
@Test @Test
public void pathHandlingTest() { public void pathHandlingTest() {
System.setProperty("nashorn.persistent.code.cache", codeCache); System.setProperty("nashorn.persistent.code.cache", codeCache);
final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); 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 expectedCodeCachePath = FileSystems.getDefault().getPath(oldUserDir + File.separator + codeCache);
final Path actualCodeCachePath = FileSystems.getDefault().getPath(System.getProperty( final Path actualCodeCachePath = FileSystems.getDefault().getPath(System.getProperty(
@ -128,9 +120,8 @@ public class CodeStoreAndPathTest {
public void changeUserDirTest() throws ScriptException, IOException { public void changeUserDirTest() throws ScriptException, IOException {
System.setProperty("nashorn.persistent.code.cache", codeCache); System.setProperty("nashorn.persistent.code.cache", codeCache);
final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS); final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_NOOPT);
final Path codeCachePath = FileSystems.getDefault().getPath(System.getProperty( final Path codeCachePath = getCodeCachePath(false);
"nashorn.persistent.code.cache")).toAbsolutePath();
final String newUserDir = "build/newUserDir"; final String newUserDir = "build/newUserDir";
// Now changing current working directory // Now changing current working directory
System.setProperty("user.dir", System.getProperty("user.dir") + File.separator + newUserDir); System.setProperty("user.dir", System.getProperty("user.dir") + File.separator + newUserDir);
@ -149,9 +140,8 @@ public class CodeStoreAndPathTest {
public void codeCacheTest() throws ScriptException, IOException { public void codeCacheTest() throws ScriptException, IOException {
System.setProperty("nashorn.persistent.code.cache", codeCache); System.setProperty("nashorn.persistent.code.cache", codeCache);
final NashornScriptEngineFactory fac = new NashornScriptEngineFactory(); final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS); final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_NOOPT);
final Path codeCachePath = FileSystems.getDefault().getPath(System.getProperty( final Path codeCachePath = getCodeCachePath(false);
"nashorn.persistent.code.cache")).toAbsolutePath();
e.eval(code1); e.eval(code1);
e.eval(code2); e.eval(code2);
e.eval(code3);// less than minimum size for storing e.eval(code3);// less than minimum size for storing
@ -159,4 +149,40 @@ public class CodeStoreAndPathTest {
final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath); final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath);
checkCompiledScripts(stream, 2); 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
// adding code1 and code2.
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);
}
} }