44 import java.text.SimpleDateFormat;
45 import java.util.Base64;
46 import java.util.Date;
47 import java.util.Map;
48 import java.util.Timer;
49 import java.util.TimerTask;
50 import java.util.concurrent.TimeUnit;
51 import java.util.concurrent.atomic.AtomicBoolean;
52 import java.util.function.Function;
53 import java.util.function.IntFunction;
54 import java.util.function.Predicate;
55 import java.util.stream.Stream;
56 import jdk.nashorn.internal.codegen.types.Type;
57 import jdk.nashorn.internal.runtime.Context;
58 import jdk.nashorn.internal.runtime.RecompilableScriptFunctionData;
59 import jdk.nashorn.internal.runtime.Source;
60 import jdk.nashorn.internal.runtime.logging.DebugLogger;
61 import jdk.nashorn.internal.runtime.options.Options;
62
63 /**
64 * Static utility that encapsulates persistence of type information for functions compiled with optimistic
65 * typing. With this feature enabled, when a JavaScript function is recompiled because it gets deoptimized,
66 * the type information for deoptimization is stored in a cache file. If the same function is compiled in a
67 * subsequent JVM invocation, the type information is used for initial compilation, thus allowing the system
68 * to skip a lot of intermediate recompilations and immediately emit a version of the code that has its
69 * optimistic types at (or near) the steady state.
70 * </p><p>
71 * Normally, the type info persistence feature is disabled. When the {@code nashorn.typeInfo.maxFiles} system
72 * property is specified with a value greater than 0, it is enabled and operates in an operating-system
73 * specific per-user cache directory. You can override the directory by specifying it in the
74 * {@code nashorn.typeInfo.cacheDir} directory. The maximum number of files is softly enforced by a task that
75 * cleans up the directory periodically on a separate thread. It is run after some delay after a new file is
76 * added to the cache. The default delay is 20 seconds, and can be set using the
77 * {@code nashorn.typeInfo.cleanupDelaySeconds} system property. You can also specify the word
78 * {@code unlimited} as the value for {@code nashorn.typeInfo.maxFiles} in which case the type info cache is
79 * allowed to grow without limits.
80 */
81 public final class OptimisticTypesPersistence {
82 // Default is 0, for disabling the feature when not specified. A reasonable default when enabled is
83 // dependent on the application; setting it to e.g. 20000 is probably good enough for most uses and will
84 // usually cap the cache directory to about 80MB presuming a 4kB filesystem allocation unit. There is one
85 // file per JavaScript function.
86 private static final int DEFAULT_MAX_FILES = 0;
87 // Constants for signifying that the cache should not be limited
88 private static final int UNLIMITED_FILES = -1;
89 // Maximum number of files that should be cached on disk. The maximum will be softly enforced.
90 private static final int MAX_FILES = getMaxFiles();
91 // Number of seconds to wait between adding a new file to the cache and running a cleanup process
92 private static final int DEFAULT_CLEANUP_DELAY = 20;
93 private static final int CLEANUP_DELAY = Math.max(0, Options.getIntProperty(
94 "nashorn.typeInfo.cleanupDelaySeconds", DEFAULT_CLEANUP_DELAY));
95 // The name of the default subdirectory within the system cache directory where we store type info.
96 private static final String DEFAULT_CACHE_SUBDIR_NAME = "com.oracle.java.NashornTypeInfo";
97 // The directory where we cache type info
98 private static final File baseCacheDir = createBaseCacheDir();
99 private static final File cacheDir = createCacheDir(baseCacheDir);
|
44 import java.text.SimpleDateFormat;
45 import java.util.Base64;
46 import java.util.Date;
47 import java.util.Map;
48 import java.util.Timer;
49 import java.util.TimerTask;
50 import java.util.concurrent.TimeUnit;
51 import java.util.concurrent.atomic.AtomicBoolean;
52 import java.util.function.Function;
53 import java.util.function.IntFunction;
54 import java.util.function.Predicate;
55 import java.util.stream.Stream;
56 import jdk.nashorn.internal.codegen.types.Type;
57 import jdk.nashorn.internal.runtime.Context;
58 import jdk.nashorn.internal.runtime.RecompilableScriptFunctionData;
59 import jdk.nashorn.internal.runtime.Source;
60 import jdk.nashorn.internal.runtime.logging.DebugLogger;
61 import jdk.nashorn.internal.runtime.options.Options;
62
63 /**
64 * <p>Static utility that encapsulates persistence of type information for functions compiled with optimistic
65 * typing. With this feature enabled, when a JavaScript function is recompiled because it gets deoptimized,
66 * the type information for deoptimization is stored in a cache file. If the same function is compiled in a
67 * subsequent JVM invocation, the type information is used for initial compilation, thus allowing the system
68 * to skip a lot of intermediate recompilations and immediately emit a version of the code that has its
69 * optimistic types at (or near) the steady state.
70 * </p><p>
71 * Normally, the type info persistence feature is disabled. When the {@code nashorn.typeInfo.maxFiles} system
72 * property is specified with a value greater than 0, it is enabled and operates in an operating-system
73 * specific per-user cache directory. You can override the directory by specifying it in the
74 * {@code nashorn.typeInfo.cacheDir} directory. The maximum number of files is softly enforced by a task that
75 * cleans up the directory periodically on a separate thread. It is run after some delay after a new file is
76 * added to the cache. The default delay is 20 seconds, and can be set using the
77 * {@code nashorn.typeInfo.cleanupDelaySeconds} system property. You can also specify the word
78 * {@code unlimited} as the value for {@code nashorn.typeInfo.maxFiles} in which case the type info cache is
79 * allowed to grow without limits.
80 * </p>
81 */
82 public final class OptimisticTypesPersistence {
83 // Default is 0, for disabling the feature when not specified. A reasonable default when enabled is
84 // dependent on the application; setting it to e.g. 20000 is probably good enough for most uses and will
85 // usually cap the cache directory to about 80MB presuming a 4kB filesystem allocation unit. There is one
86 // file per JavaScript function.
87 private static final int DEFAULT_MAX_FILES = 0;
88 // Constants for signifying that the cache should not be limited
89 private static final int UNLIMITED_FILES = -1;
90 // Maximum number of files that should be cached on disk. The maximum will be softly enforced.
91 private static final int MAX_FILES = getMaxFiles();
92 // Number of seconds to wait between adding a new file to the cache and running a cleanup process
93 private static final int DEFAULT_CLEANUP_DELAY = 20;
94 private static final int CLEANUP_DELAY = Math.max(0, Options.getIntProperty(
95 "nashorn.typeInfo.cleanupDelaySeconds", DEFAULT_CLEANUP_DELAY));
96 // The name of the default subdirectory within the system cache directory where we store type info.
97 private static final String DEFAULT_CACHE_SUBDIR_NAME = "com.oracle.java.NashornTypeInfo";
98 // The directory where we cache type info
99 private static final File baseCacheDir = createBaseCacheDir();
100 private static final File cacheDir = createCacheDir(baseCacheDir);
|