1 /*
   2  * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package jdk.nashorn.internal.codegen;
  26 
  27 import java.io.BufferedInputStream;
  28 import java.io.BufferedOutputStream;
  29 import java.io.DataInputStream;
  30 import java.io.DataOutputStream;
  31 import java.io.File;
  32 import java.io.FileInputStream;
  33 import java.io.FileOutputStream;
  34 import java.io.IOException;
  35 import java.io.InputStream;
  36 import java.io.PrintWriter;
  37 import java.io.StringWriter;
  38 import java.net.URL;
  39 import java.nio.file.Files;
  40 import java.nio.file.Path;
  41 import java.security.AccessController;
  42 import java.security.MessageDigest;
  43 import java.security.PrivilegedAction;
  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);
 101     // In-process locks to make sure we don't have a cross-thread race condition manipulating any file.
 102     private static final Object[] locks = cacheDir == null ? null : createLockArray();
 103     // Only report one read/write error every minute
 104     private static final long ERROR_REPORT_THRESHOLD = 60000L;
 105 
 106     private static volatile long lastReportedError;
 107     private static final AtomicBoolean scheduledCleanup;
 108     private static final Timer cleanupTimer;
 109     static {
 110         if (baseCacheDir == null || MAX_FILES == UNLIMITED_FILES) {
 111             scheduledCleanup = null;
 112             cleanupTimer = null;
 113         } else {
 114             scheduledCleanup = new AtomicBoolean();
 115             cleanupTimer = new Timer(true);
 116         }
 117     }
 118     /**
 119      * Retrieves an opaque descriptor for the persistence location for a given function. It should be passed
 120      * to {@link #load(Object)} and {@link #store(Object, Map)} methods.
 121      * @param source the source where the function comes from
 122      * @param functionId the unique ID number of the function within the source
 123      * @param paramTypes the types of the function parameters (as persistence is per parameter type
 124      * specialization).
 125      * @return an opaque descriptor for the persistence location. Can be null if persistence is disabled.
 126      */
 127     public static Object getLocationDescriptor(final Source source, final int functionId, final Type[] paramTypes) {
 128         if(cacheDir == null) {
 129             return null;
 130         }
 131         final StringBuilder b = new StringBuilder(48);
 132         // Base64-encode the digest of the source, and append the function id.
 133         b.append(source.getDigest()).append('-').append(functionId);
 134         // Finally, if this is a parameter-type specialized version of the function, add the parameter types
 135         // to the file name.
 136         if(paramTypes != null && paramTypes.length > 0) {
 137             b.append('-');
 138             for(final Type t: paramTypes) {
 139                 b.append(Type.getShortSignatureDescriptor(t));
 140             }
 141         }
 142         return new LocationDescriptor(new File(cacheDir, b.toString()));
 143     }
 144 
 145     private static final class LocationDescriptor {
 146         private final File file;
 147 
 148         LocationDescriptor(final File file) {
 149             this.file = file;
 150         }
 151     }
 152 
 153 
 154     /**
 155      * Stores the map of optimistic types for a given function.
 156      * @param locationDescriptor the opaque persistence location descriptor, retrieved by calling
 157      * {@link #getLocationDescriptor(Source, int, Type[])}.
 158      * @param optimisticTypes the map of optimistic types.
 159      */
 160     @SuppressWarnings("resource")
 161     public static void store(final Object locationDescriptor, final Map<Integer, Type> optimisticTypes) {
 162         if(locationDescriptor == null || optimisticTypes.isEmpty()) {
 163             return;
 164         }
 165         final File file = ((LocationDescriptor)locationDescriptor).file;
 166 
 167         AccessController.doPrivileged(new PrivilegedAction<Void>() {
 168             @Override
 169             public Void run() {
 170                 synchronized(getFileLock(file)) {
 171                     if (!file.exists()) {
 172                         // If the file already exists, we aren't increasing the number of cached files, so
 173                         // don't schedule cleanup.
 174                         scheduleCleanup();
 175                     }
 176                     try (final FileOutputStream out = new FileOutputStream(file)) {
 177                         out.getChannel().lock(); // lock exclusive
 178                         final DataOutputStream dout = new DataOutputStream(new BufferedOutputStream(out));
 179                         Type.writeTypeMap(optimisticTypes, dout);
 180                         dout.flush();
 181                     } catch(final Exception e) {
 182                         reportError("write", file, e);
 183                     }
 184                 }
 185                 return null;
 186             }
 187         });
 188     }
 189 
 190     /**
 191      * Loads the map of optimistic types for a given function.
 192      * @param locationDescriptor the opaque persistence location descriptor, retrieved by calling
 193      * {@link #getLocationDescriptor(Source, int, Type[])}.
 194      * @return the map of optimistic types, or null if persisted type information could not be retrieved.
 195      */
 196     @SuppressWarnings("resource")
 197     public static Map<Integer, Type> load(final Object locationDescriptor) {
 198         if (locationDescriptor == null) {
 199             return null;
 200         }
 201         final File file = ((LocationDescriptor)locationDescriptor).file;
 202         return AccessController.doPrivileged(new PrivilegedAction<Map<Integer, Type>>() {
 203             @Override
 204             public Map<Integer, Type> run() {
 205                 try {
 206                     if(!file.isFile()) {
 207                         return null;
 208                     }
 209                     synchronized(getFileLock(file)) {
 210                         try (final FileInputStream in = new FileInputStream(file)) {
 211                             in.getChannel().lock(0, Long.MAX_VALUE, true); // lock shared
 212                             final DataInputStream din = new DataInputStream(new BufferedInputStream(in));
 213                             return Type.readTypeMap(din);
 214                         }
 215                     }
 216                 } catch (final Exception e) {
 217                     reportError("read", file, e);
 218                     return null;
 219                 }
 220             }
 221         });
 222     }
 223 
 224     private static void reportError(final String msg, final File file, final Exception e) {
 225         final long now = System.currentTimeMillis();
 226         if(now - lastReportedError > ERROR_REPORT_THRESHOLD) {
 227             reportError(String.format("Failed to %s %s", msg, file), e);
 228             lastReportedError = now;
 229         }
 230     }
 231 
 232     /**
 233      * Logs an error message with warning severity (reasoning being that we're reporting an error that'll disable the
 234      * type info cache, but it's only logged as a warning because that doesn't prevent Nashorn from running, it just
 235      * disables a performance-enhancing cache).
 236      * @param msg the message to log
 237      * @param e the exception that represents the error.
 238      */
 239     private static void reportError(final String msg, final Exception e) {
 240         getLogger().warning(msg, "\n", exceptionToString(e));
 241     }
 242 
 243     /**
 244      * A helper that prints an exception stack trace into a string. We have to do this as if we just pass the exception
 245      * to {@link DebugLogger#warning(Object...)}, it will only log the exception message and not the stack, making
 246      * problems harder to diagnose.
 247      * @param e the exception
 248      * @return the string representation of {@link Exception#printStackTrace()} output.
 249      */
 250     private static String exceptionToString(final Exception e) {
 251         final StringWriter sw = new StringWriter();
 252         final PrintWriter pw = new PrintWriter(sw, false);
 253         e.printStackTrace(pw);
 254         pw.flush();
 255         return sw.toString();
 256     }
 257 
 258     private static File createBaseCacheDir() {
 259         if(MAX_FILES == 0 || Options.getBooleanProperty("nashorn.typeInfo.disabled")) {
 260             return null;
 261         }
 262         try {
 263             return createBaseCacheDirPrivileged();
 264         } catch(final Exception e) {
 265             reportError("Failed to create cache dir", e);
 266             return null;
 267         }
 268     }
 269 
 270     private static File createBaseCacheDirPrivileged() {
 271         return AccessController.doPrivileged(new PrivilegedAction<File>() {
 272             @Override
 273             public File run() {
 274                 final String explicitDir = System.getProperty("nashorn.typeInfo.cacheDir");
 275                 final File dir;
 276                 if(explicitDir != null) {
 277                     dir = new File(explicitDir);
 278                 } else {
 279                     // When no directory is explicitly specified, get an operating system specific cache
 280                     // directory, and create "com.oracle.java.NashornTypeInfo" in it.
 281                     final File systemCacheDir = getSystemCacheDir();
 282                     dir = new File(systemCacheDir, DEFAULT_CACHE_SUBDIR_NAME);
 283                     if (isSymbolicLink(dir)) {
 284                         return null;
 285                     }
 286                 }
 287                 return dir;
 288             }
 289         });
 290     }
 291 
 292     private static File createCacheDir(final File baseDir) {
 293         if (baseDir == null) {
 294             return null;
 295         }
 296         try {
 297             return createCacheDirPrivileged(baseDir);
 298         } catch(final Exception e) {
 299             reportError("Failed to create cache dir", e);
 300             return null;
 301         }
 302     }
 303 
 304     private static File createCacheDirPrivileged(final File baseDir) {
 305         return AccessController.doPrivileged(new PrivilegedAction<File>() {
 306             @Override
 307             public File run() {
 308                 final String versionDirName;
 309                 try {
 310                     versionDirName = getVersionDirName();
 311                 } catch(final Exception e) {
 312                     reportError("Failed to calculate version dir name", e);
 313                     return null;
 314                 }
 315                 final File versionDir = new File(baseDir, versionDirName);
 316                 if (isSymbolicLink(versionDir)) {
 317                     return null;
 318                 }
 319                 versionDir.mkdirs();
 320                 if (versionDir.isDirectory()) {
 321                     getLogger().info("Optimistic type persistence directory is " + versionDir);
 322                     return versionDir;
 323                 }
 324                 getLogger().warning("Could not create optimistic type persistence directory " + versionDir);
 325                 return null;
 326             }
 327         });
 328     }
 329 
 330     /**
 331      * Returns an operating system specific root directory for cache files.
 332      * @return an operating system specific root directory for cache files.
 333      */
 334     private static File getSystemCacheDir() {
 335         final String os = System.getProperty("os.name", "generic");
 336         if("Mac OS X".equals(os)) {
 337             // Mac OS X stores caches in ~/Library/Caches
 338             return new File(new File(System.getProperty("user.home"), "Library"), "Caches");
 339         } else if(os.startsWith("Windows")) {
 340             // On Windows, temp directory is the best approximation of a cache directory, as its contents
 341             // persist across reboots and various cleanup utilities know about it. java.io.tmpdir normally
 342             // points to a user-specific temp directory, %HOME%\LocalSettings\Temp.
 343             return new File(System.getProperty("java.io.tmpdir"));
 344         } else {
 345             // In other cases we're presumably dealing with a UNIX flavor (Linux, Solaris, etc.); "~/.cache"
 346             return new File(System.getProperty("user.home"), ".cache");
 347         }
 348     }
 349 
 350     /**
 351      * In order to ensure that changes in Nashorn code don't cause corruption in the data, we'll create a
 352      * per-code-version directory. Normally, this will create the SHA-1 digest of the nashorn.jar. In case the classpath
 353      * for nashorn is local directory (e.g. during development), this will create the string "dev-" followed by the
 354      * timestamp of the most recent .class file.
 355      *
 356      * @return digest of currently running nashorn
 357      * @throws Exception if digest could not be created
 358      */
 359     public static String getVersionDirName() throws Exception {
 360         // NOTE: getResource("") won't work if the JAR file doesn't have directory entries (and JAR files in JDK distro
 361         // don't, or at least it's a bad idea counting on it). Alternatively, we could've tried
 362         // getResource("OptimisticTypesPersistence.class") but behavior of getResource with regard to its willingness
 363         // to hand out URLs to .class files is also unspecified. Therefore, the most robust way to obtain an URL to our
 364         // package is to have a small non-class anchor file and start out from its URL.
 365         final URL url = OptimisticTypesPersistence.class.getResource("anchor.properties");
 366         final String protocol = url.getProtocol();
 367         if (protocol.equals("jar")) {
 368             // Normal deployment: nashorn.jar
 369             final String jarUrlFile = url.getFile();
 370             final String filePath = jarUrlFile.substring(0, jarUrlFile.indexOf('!'));
 371             final URL file = new URL(filePath);
 372             try (final InputStream in = file.openStream()) {
 373                 final byte[] buf = new byte[128*1024];
 374                 final MessageDigest digest = MessageDigest.getInstance("SHA-1");
 375                 for(;;) {
 376                     final int l = in.read(buf);
 377                     if(l == -1) {
 378                         return Base64.getUrlEncoder().withoutPadding().encodeToString(digest.digest());
 379                     }
 380                     digest.update(buf, 0, l);
 381                 }
 382             }
 383         } else if(protocol.equals("file")) {
 384             // Development
 385             final String fileStr = url.getFile();
 386             final String className = OptimisticTypesPersistence.class.getName();
 387             final int packageNameLen = className.lastIndexOf('.');
 388             final String dirStr = fileStr.substring(0, fileStr.length() - packageNameLen - 1);
 389             final File dir = new File(dirStr);
 390             return "dev-" + new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date(getLastModifiedClassFile(
 391                     dir, 0L)));
 392         } else {
 393             throw new AssertionError();
 394         }
 395     }
 396 
 397     private static long getLastModifiedClassFile(final File dir, final long max) {
 398         long currentMax = max;
 399         for(final File f: dir.listFiles()) {
 400             if(f.getName().endsWith(".class")) {
 401                 final long lastModified = f.lastModified();
 402                 if (lastModified > currentMax) {
 403                     currentMax = lastModified;
 404                 }
 405             } else if (f.isDirectory()) {
 406                 final long lastModified = getLastModifiedClassFile(f, currentMax);
 407                 if (lastModified > currentMax) {
 408                     currentMax = lastModified;
 409                 }
 410             }
 411         }
 412         return currentMax;
 413     }
 414 
 415     /**
 416      * Returns true if the specified file is a symbolic link, and also logs a warning if it is.
 417      * @param file the file
 418      * @return true if file is a symbolic link, false otherwise.
 419      */
 420     private static boolean isSymbolicLink(final File file) {
 421         if (Files.isSymbolicLink(file.toPath())) {
 422             getLogger().warning("Directory " + file + " is a symlink");
 423             return true;
 424         }
 425         return false;
 426     }
 427 
 428     private static Object[] createLockArray() {
 429         final Object[] lockArray = new Object[Runtime.getRuntime().availableProcessors() * 2];
 430         for (int i = 0; i < lockArray.length; ++i) {
 431             lockArray[i] = new Object();
 432         }
 433         return lockArray;
 434     }
 435 
 436     private static Object getFileLock(final File file) {
 437         return locks[(file.hashCode() & Integer.MAX_VALUE) % locks.length];
 438     }
 439 
 440     private static DebugLogger getLogger() {
 441         try {
 442             return Context.getContext().getLogger(RecompilableScriptFunctionData.class);
 443         } catch (final Exception e) {
 444             e.printStackTrace();
 445             return DebugLogger.DISABLED_LOGGER;
 446         }
 447     }
 448 
 449     private static void scheduleCleanup() {
 450         if (MAX_FILES != UNLIMITED_FILES && scheduledCleanup.compareAndSet(false, true)) {
 451             cleanupTimer.schedule(new TimerTask() {
 452                 @Override
 453                 public void run() {
 454                     scheduledCleanup.set(false);
 455                     try {
 456                         doCleanup();
 457                     } catch (final IOException e) {
 458                         // Ignore it. While this is unfortunate, we don't have good facility for reporting
 459                         // this, as we're running in a thread that has no access to Context, so we can't grab
 460                         // a DebugLogger.
 461                     }
 462                 }
 463             }, TimeUnit.SECONDS.toMillis(CLEANUP_DELAY));
 464         }
 465     }
 466 
 467     private static void doCleanup() throws IOException {
 468         final Path[] files = getAllRegularFilesInLastModifiedOrder();
 469         final int nFiles = files.length;
 470         final int filesToDelete = Math.max(0, nFiles - MAX_FILES);
 471         int filesDeleted = 0;
 472         for (int i = 0; i < nFiles && filesDeleted < filesToDelete; ++i) {
 473             try {
 474                 Files.deleteIfExists(files[i]);
 475                 // Even if it didn't exist, we increment filesDeleted; it existed a moment earlier; something
 476                 // else deleted it for us; that's okay with us.
 477                 filesDeleted++;
 478             } catch (final Exception e) {
 479                 // does not increase filesDeleted
 480             }
 481             files[i] = null; // gc eligible
 482         }
 483     }
 484 
 485     private static Path[] getAllRegularFilesInLastModifiedOrder() throws IOException {
 486         try (final Stream<Path> filesStream = Files.walk(baseCacheDir.toPath())) {
 487             // TODO: rewrite below once we can use JDK8 syntactic constructs
 488             return filesStream
 489             .filter(new Predicate<Path>() {
 490                 @Override
 491                 public boolean test(final Path path) {
 492                     return !Files.isDirectory(path);
 493                 }
 494             })
 495             .map(new Function<Path, PathAndTime>() {
 496                 @Override
 497                 public PathAndTime apply(final Path path) {
 498                     return new PathAndTime(path);
 499                 }
 500             })
 501             .sorted()
 502             .map(new Function<PathAndTime, Path>() {
 503                 @Override
 504                 public Path apply(final PathAndTime pathAndTime) {
 505                     return pathAndTime.path;
 506                 }
 507             })
 508             .toArray(new IntFunction<Path[]>() { // Replace with Path::new
 509                 @Override
 510                 public Path[] apply(final int length) {
 511                     return new Path[length];
 512                 }
 513             });
 514         }
 515     }
 516 
 517     private static class PathAndTime implements Comparable<PathAndTime> {
 518         private final Path path;
 519         private final long time;
 520 
 521         PathAndTime(final Path path) {
 522             this.path = path;
 523             this.time = getTime(path);
 524         }
 525 
 526         @Override
 527         public int compareTo(final PathAndTime other) {
 528             return Long.compare(time, other.time);
 529         }
 530 
 531         private static long getTime(final Path path) {
 532             try {
 533                 return Files.getLastModifiedTime(path).toMillis();
 534             } catch (final IOException e) {
 535                 // All files for which we can't retrieve the last modified date will be considered oldest.
 536                 return -1L;
 537             }
 538         }
 539     }
 540 
 541     private static int getMaxFiles() {
 542         final String str = Options.getStringProperty("nashorn.typeInfo.maxFiles", null);
 543         if (str == null) {
 544             return DEFAULT_MAX_FILES;
 545         } else if ("unlimited".equals(str)) {
 546             return UNLIMITED_FILES;
 547         }
 548         return Math.max(0, Integer.parseInt(str));
 549     }
 550 }