1 /*
   2  * Copyright (c) 2016, 2019, 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.dcmd;
  27 
  28 
  29 
  30 import jdk.jfr.internal.LogLevel;
  31 import jdk.jfr.internal.LogTag;
  32 import jdk.jfr.internal.Logger;
  33 import jdk.jfr.internal.Options;
  34 import jdk.jfr.internal.Repository;
  35 import jdk.jfr.internal.SecuritySupport.SafePath;
  36 
  37 /**
  38  * JFR.configure - invoked from native
  39  *
  40  */
  41 //Instantiated by native
  42 final class DCmdConfigure extends AbstractDCmd {
  43     /**
  44      * Execute JFR.configure.
  45      *
  46      * @param repositoryPath the path
  47      * @param dumpPath path to dump to on fatal error (oom)
  48      * @param stackDepth depth of stack traces
  49      * @param globalBufferCount number of global buffers
  50      * @param globalBufferSize size of global buffers
  51      * @param threadBufferSize size of thread buffer for events
  52      * @param maxChunkSize threshold at which a new chunk is created in the disk repository
  53      * @param sampleThreads if thread sampling should be enabled
  54      *
  55      * @return result
  56 
  57      * @throws DCmdException
  58      *             if the dump could not be completed
  59      */
  60     public String execute
  61     (
  62             String repositoryPath,
  63             String dumpPath,
  64             Integer stackDepth,
  65             Long globalBufferCount,
  66             Long globalBufferSize,
  67             Long threadBufferSize,
  68             Long memorySize,
  69             Long maxChunkSize,
  70             Boolean sampleThreads
  71 
  72     ) throws DCmdException {
  73         boolean updated = false;
  74         if (repositoryPath != null) {
  75             try {
  76                 SafePath s = new SafePath(repositoryPath);
  77                 Repository.getRepository().setBasePath(s);
  78                 Logger.log(LogTag.JFR, LogLevel.INFO, "Base repository path set to " + repositoryPath);
  79             } catch (Exception e) {
  80                 throw new DCmdException("Could not use " + repositoryPath + " as repository. " + e.getMessage(), e);
  81             }
  82             printRepositoryPath();
  83             updated = true;
  84         }
  85 
  86         if (dumpPath != null)  {
  87             Options.setDumpPath(new SafePath(dumpPath));
  88             Logger.log(LogTag.JFR, LogLevel.INFO, "Emergency dump path set to " + dumpPath);
  89             printDumpPath();
  90             updated = true;
  91         }
  92 
  93         if (stackDepth != null)  {
  94             Options.setStackDepth(stackDepth);
  95             Logger.log(LogTag.JFR, LogLevel.INFO, "Stack depth set to " + stackDepth);
  96             printStackDepth();
  97             updated = true;
  98         }
  99 
 100         if (globalBufferCount != null)  {
 101             Options.setGlobalBufferCount(globalBufferCount);
 102             Logger.log(LogTag.JFR, LogLevel.INFO, "Global buffer count set to " + globalBufferCount);
 103             printGlobalBufferCount();
 104             updated = true;
 105         }
 106 
 107         if (globalBufferSize != null)  {
 108             Options.setGlobalBufferSize(globalBufferSize);
 109             Logger.log(LogTag.JFR, LogLevel.INFO, "Global buffer size set to " + globalBufferSize);
 110             printGlobalBufferSize();
 111             updated = true;
 112         }
 113 
 114         if (threadBufferSize != null)  {
 115             Options.setThreadBufferSize(threadBufferSize);
 116             Logger.log(LogTag.JFR, LogLevel.INFO, "Thread buffer size set to " + threadBufferSize);
 117             printThreadBufferSize();
 118             updated = true;
 119         }
 120 
 121         if (memorySize != null) {
 122             Options.setMemorySize(memorySize);
 123             Logger.log(LogTag.JFR, LogLevel.INFO, "Memory size set to " + memorySize);
 124             printMemorySize();
 125             updated = true;
 126         }
 127 
 128         if (maxChunkSize != null)  {
 129             Options.setMaxChunkSize(maxChunkSize);
 130             Logger.log(LogTag.JFR, LogLevel.INFO, "Max chunk size set to " + maxChunkSize);
 131             printMaxChunkSize();
 132             updated = true;
 133         }
 134 
 135         if (sampleThreads != null)  {
 136             Options.setSampleThreads(sampleThreads);
 137             Logger.log(LogTag.JFR, LogLevel.INFO, "Sample threads set to " + sampleThreads);
 138             printSampleThreads();
 139             updated = true;
 140         }
 141 
 142         if (!updated) {
 143             println("Current configuration:");
 144             println();
 145             printRepositoryPath();
 146             printStackDepth();
 147             printGlobalBufferCount();
 148             printGlobalBufferSize();
 149             printThreadBufferSize();
 150             printMemorySize();
 151             printMaxChunkSize();
 152             printSampleThreads();
 153         }
 154         return getResult();
 155     }
 156 
 157     private void printRepositoryPath() {
 158         print("Repository path: ");
 159         printPath(Repository.getRepository().getRepositoryPath());
 160         println();
 161     }
 162 
 163     private void printDumpPath() {
 164         print("Dump path: ");
 165         printPath(Options.getDumpPath());
 166         println();
 167     }
 168 
 169     private void printSampleThreads() {
 170         println("Sample threads: " + Options.getSampleThreads());
 171     }
 172 
 173     private void printStackDepth() {
 174         println("Stack depth: " +  Options.getStackDepth());
 175     }
 176 
 177     private void printGlobalBufferCount() {
 178         println("Global buffer count: " +  Options.getGlobalBufferCount());
 179     }
 180 
 181     private void printGlobalBufferSize() {
 182         print("Global buffer size: ");
 183         printBytes(Options.getGlobalBufferSize(), " ");
 184         println();
 185     }
 186 
 187     private void printThreadBufferSize() {
 188         print("Thread buffer size: ");
 189         printBytes(Options.getThreadBufferSize(), " ");
 190         println();
 191     }
 192 
 193     private void printMemorySize() {
 194         print("Memory size: ");
 195         printBytes(Options.getMemorySize(), " ");
 196         println();
 197     }
 198 
 199     private void printMaxChunkSize() {
 200         print("Max chunk size: ");
 201         printBytes(Options.getMaxChunkSize(), " ");
 202         println();
 203     }
 204 }