1 /*
   2  * Copyright (c) 2016, 2018, 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 
  26 package jdk.jfr.internal;
  27 
  28 import jdk.jfr.internal.SecuritySupport.SafePath;
  29 import jdk.internal.misc.Unsafe;
  30 
  31 /**
  32  * Options that control Flight Recorder.
  33  *
  34  * Can be set using JFR.configure
  35  *
  36  */
  37 public final class Options {
  38 
  39     private final static JVM jvm = JVM.getJVM();
  40     private final static long WAIT_INTERVAL = 1000; // ms;
  41 
  42     private final static long MIN_MAX_CHUNKSIZE = 1024 * 1024;
  43 
  44     private static final long DEFAULT_GLOBAL_BUFFER_COUNT = 20;
  45     private static final long DEFAULT_GLOBAL_BUFFER_SIZE = 524288;
  46     private static final long DEFAULT_MEMORY_SIZE = DEFAULT_GLOBAL_BUFFER_COUNT * DEFAULT_GLOBAL_BUFFER_SIZE;
  47     private static long DEFAULT_THREAD_BUFFER_SIZE;
  48     private static final int DEFAULT_STACK_DEPTH = 64;
  49     private static final boolean DEFAULT_SAMPLE_THREADS = true;
  50     private static final long DEFAULT_MAX_CHUNK_SIZE = 12 * 1024 * 1024;
  51     private static final SafePath DEFAULT_DUMP_PATH = SecuritySupport.USER_HOME;
  52 
  53     private static long memorySize;
  54     private static long globalBufferSize;
  55     private static long globalBufferCount;
  56     private static long threadBufferSize;
  57     private static int stackDepth;
  58     private static boolean sampleThreads;
  59     private static long maxChunkSize;
  60     private static SafePath dumpPath;
  61 
  62     static {
  63         final long pageSize = Unsafe.getUnsafe().pageSize();
  64         DEFAULT_THREAD_BUFFER_SIZE = pageSize > 8 * 1024 ? pageSize : 8 * 1024;
  65         reset();
  66     }
  67 
  68     public static synchronized void setMaxChunkSize(long max) {
  69         if (max < MIN_MAX_CHUNKSIZE) {
  70             throw new IllegalArgumentException("Max chunk size must be at least " + MIN_MAX_CHUNKSIZE);
  71         }
  72         jvm.setFileNotification(max);
  73         maxChunkSize = max;
  74     }
  75 
  76     public static synchronized long getMaxChunkSize() {
  77         return maxChunkSize;
  78     }
  79 
  80     public static synchronized void setMemorySize(long memSize) {
  81         jvm.setMemorySize(memSize);
  82         memorySize = memSize;
  83     }
  84 
  85     public static synchronized long getMemorySize() {
  86         return memorySize;
  87     }
  88 
  89     public static synchronized void setThreadBufferSize(long threadBufSize) {
  90         jvm.setThreadBufferSize(threadBufSize);
  91         threadBufferSize = threadBufSize;
  92     }
  93 
  94     public static synchronized long getThreadBufferSize() {
  95         return threadBufferSize;
  96     }
  97 
  98     public static synchronized long getGlobalBufferSize() {
  99         return globalBufferSize;
 100     }
 101 
 102     public static synchronized void setGlobalBufferCount(long globalBufCount) {
 103         jvm.setGlobalBufferCount(globalBufCount);
 104         globalBufferCount = globalBufCount;
 105     }
 106 
 107     public static synchronized long getGlobalBufferCount() {
 108         return globalBufferCount;
 109     }
 110 
 111     public static synchronized void setGlobalBufferSize(long globalBufsize) {
 112         jvm.setGlobalBufferSize(globalBufsize);
 113         globalBufferSize = globalBufsize;
 114     }
 115 
 116     public static synchronized void setDumpPath(SafePath path) {
 117         dumpPath = path;
 118     }
 119 
 120     public static synchronized SafePath getDumpPath() {
 121         return dumpPath;
 122     }
 123 
 124     public static synchronized void setStackDepth(Integer stackTraceDepth) {
 125         jvm.setStackDepth(stackTraceDepth);
 126         stackDepth = stackTraceDepth;
 127     }
 128 
 129     public static synchronized int getStackDepth() {
 130         return stackDepth;
 131     }
 132 
 133     public static synchronized void setSampleThreads(Boolean sample) {
 134         jvm.setSampleThreads(sample);
 135         sampleThreads = sample;
 136     }
 137 
 138     public static synchronized boolean getSampleThreads() {
 139         return sampleThreads;
 140     }
 141 
 142     private static synchronized void reset() {
 143         setMaxChunkSize(DEFAULT_MAX_CHUNK_SIZE);
 144         setMemorySize(DEFAULT_MEMORY_SIZE);
 145         setGlobalBufferSize(DEFAULT_GLOBAL_BUFFER_SIZE);
 146         setGlobalBufferCount(DEFAULT_GLOBAL_BUFFER_COUNT);
 147         setDumpPath(DEFAULT_DUMP_PATH);
 148         setSampleThreads(DEFAULT_SAMPLE_THREADS);
 149         setStackDepth(DEFAULT_STACK_DEPTH);
 150         setThreadBufferSize(DEFAULT_THREAD_BUFFER_SIZE);
 151     }
 152 
 153     static synchronized long getWaitInterval() {
 154         return WAIT_INTERVAL;
 155     }
 156 
 157     static void ensureInitialized() {
 158         // trigger clinit which will setup JVM defaults.
 159     }
 160 
 161 
 162 }