--- old/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/OptimisticTypesPersistence.java 2020-04-15 18:47:15.000000000 +0530
+++ /dev/null 2020-04-15 18:47:15.000000000 +0530
@@ -1,602 +0,0 @@
-/*
- * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation. Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-package jdk.nashorn.internal.codegen;
-
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
-import java.io.DataInputStream;
-import java.io.DataOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.PrintWriter;
-import java.io.StringWriter;
-import java.io.UncheckedIOException;
-import java.net.URI;
-import java.net.URL;
-import java.nio.file.Files;
-import java.nio.file.FileSystem;
-import java.nio.file.FileSystems;
-import java.nio.file.Path;
-import java.security.AccessController;
-import java.security.MessageDigest;
-import java.security.PrivilegedAction;
-import java.text.SimpleDateFormat;
-import java.util.Base64;
-import java.util.Date;
-import java.util.Map;
-import java.util.Timer;
-import java.util.TimerTask;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.function.Consumer;
-import java.util.function.Function;
-import java.util.function.IntFunction;
-import java.util.function.Predicate;
-import java.util.stream.Stream;
-import jdk.nashorn.internal.codegen.types.Type;
-import jdk.nashorn.internal.runtime.Context;
-import jdk.nashorn.internal.runtime.RecompilableScriptFunctionData;
-import jdk.nashorn.internal.runtime.Source;
-import jdk.nashorn.internal.runtime.logging.DebugLogger;
-import jdk.nashorn.internal.runtime.options.Options;
-
-/**
- *
Static utility that encapsulates persistence of type information for functions compiled with optimistic
- * typing. With this feature enabled, when a JavaScript function is recompiled because it gets deoptimized,
- * the type information for deoptimization is stored in a cache file. If the same function is compiled in a
- * subsequent JVM invocation, the type information is used for initial compilation, thus allowing the system
- * to skip a lot of intermediate recompilations and immediately emit a version of the code that has its
- * optimistic types at (or near) the steady state.
- *
- * Normally, the type info persistence feature is disabled. When the {@code nashorn.typeInfo.maxFiles} system
- * property is specified with a value greater than 0, it is enabled and operates in an operating-system
- * specific per-user cache directory. You can override the directory by specifying it in the
- * {@code nashorn.typeInfo.cacheDir} directory. The maximum number of files is softly enforced by a task that
- * cleans up the directory periodically on a separate thread. It is run after some delay after a new file is
- * added to the cache. The default delay is 20 seconds, and can be set using the
- * {@code nashorn.typeInfo.cleanupDelaySeconds} system property. You can also specify the word
- * {@code unlimited} as the value for {@code nashorn.typeInfo.maxFiles} in which case the type info cache is
- * allowed to grow without limits.
- *
- */
-public final class OptimisticTypesPersistence {
- // Default is 0, for disabling the feature when not specified. A reasonable default when enabled is
- // dependent on the application; setting it to e.g. 20000 is probably good enough for most uses and will
- // usually cap the cache directory to about 80MB presuming a 4kB filesystem allocation unit. There is one
- // file per JavaScript function.
- private static final int DEFAULT_MAX_FILES = 0;
- // Constants for signifying that the cache should not be limited
- private static final int UNLIMITED_FILES = -1;
- // Maximum number of files that should be cached on disk. The maximum will be softly enforced.
- private static final int MAX_FILES = getMaxFiles();
- // Number of seconds to wait between adding a new file to the cache and running a cleanup process
- private static final int DEFAULT_CLEANUP_DELAY = 20;
- private static final int CLEANUP_DELAY = Math.max(0, Options.getIntProperty(
- "nashorn.typeInfo.cleanupDelaySeconds", DEFAULT_CLEANUP_DELAY));
- // The name of the default subdirectory within the system cache directory where we store type info.
- private static final String DEFAULT_CACHE_SUBDIR_NAME = "com.oracle.java.NashornTypeInfo";
- // The directory where we cache type info
- private static final File baseCacheDir = createBaseCacheDir();
- private static final File cacheDir = createCacheDir(baseCacheDir);
- // In-process locks to make sure we don't have a cross-thread race condition manipulating any file.
- private static final Object[] locks = cacheDir == null ? null : createLockArray();
- // Only report one read/write error every minute
- private static final long ERROR_REPORT_THRESHOLD = 60000L;
-
- private static volatile long lastReportedError;
- private static final AtomicBoolean scheduledCleanup;
- private static final Timer cleanupTimer;
- static {
- if (baseCacheDir == null || MAX_FILES == UNLIMITED_FILES) {
- scheduledCleanup = null;
- cleanupTimer = null;
- } else {
- scheduledCleanup = new AtomicBoolean();
- cleanupTimer = new Timer(true);
- }
- }
- /**
- * Retrieves an opaque descriptor for the persistence location for a given function. It should be passed
- * to {@link #load(Object)} and {@link #store(Object, Map)} methods.
- * @param source the source where the function comes from
- * @param functionId the unique ID number of the function within the source
- * @param paramTypes the types of the function parameters (as persistence is per parameter type
- * specialization).
- * @return an opaque descriptor for the persistence location. Can be null if persistence is disabled.
- */
- public static Object getLocationDescriptor(final Source source, final int functionId, final Type[] paramTypes) {
- if(cacheDir == null) {
- return null;
- }
- final StringBuilder b = new StringBuilder(48);
- // Base64-encode the digest of the source, and append the function id.
- b.append(source.getDigest()).append('-').append(functionId);
- // Finally, if this is a parameter-type specialized version of the function, add the parameter types
- // to the file name.
- if(paramTypes != null && paramTypes.length > 0) {
- b.append('-');
- for(final Type t: paramTypes) {
- b.append(Type.getShortSignatureDescriptor(t));
- }
- }
- return new LocationDescriptor(new File(cacheDir, b.toString()));
- }
-
- private static final class LocationDescriptor {
- private final File file;
-
- LocationDescriptor(final File file) {
- this.file = file;
- }
- }
-
-
- /**
- * Stores the map of optimistic types for a given function.
- * @param locationDescriptor the opaque persistence location descriptor, retrieved by calling
- * {@link #getLocationDescriptor(Source, int, Type[])}.
- * @param optimisticTypes the map of optimistic types.
- */
- @SuppressWarnings("resource")
- public static void store(final Object locationDescriptor, final Map optimisticTypes) {
- if(locationDescriptor == null || optimisticTypes.isEmpty()) {
- return;
- }
- final File file = ((LocationDescriptor)locationDescriptor).file;
-
- AccessController.doPrivileged(new PrivilegedAction() {
- @Override
- public Void run() {
- synchronized(getFileLock(file)) {
- if (!file.exists()) {
- // If the file already exists, we aren't increasing the number of cached files, so
- // don't schedule cleanup.
- scheduleCleanup();
- }
- try (final FileOutputStream out = new FileOutputStream(file)) {
- out.getChannel().lock(); // lock exclusive
- final DataOutputStream dout = new DataOutputStream(new BufferedOutputStream(out));
- Type.writeTypeMap(optimisticTypes, dout);
- dout.flush();
- } catch(final Exception e) {
- reportError("write", file, e);
- }
- }
- return null;
- }
- });
- }
-
- /**
- * Loads the map of optimistic types for a given function.
- * @param locationDescriptor the opaque persistence location descriptor, retrieved by calling
- * {@link #getLocationDescriptor(Source, int, Type[])}.
- * @return the map of optimistic types, or null if persisted type information could not be retrieved.
- */
- @SuppressWarnings("resource")
- public static Map load(final Object locationDescriptor) {
- if (locationDescriptor == null) {
- return null;
- }
- final File file = ((LocationDescriptor)locationDescriptor).file;
- return AccessController.doPrivileged(new PrivilegedAction