1 /*
   2  * Copyright (c) 2012, 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 package jdk.jfr.internal.dcmd;
  26 
  27 import java.time.Duration;
  28 import java.util.ArrayList;
  29 import java.util.Collection;
  30 import java.util.Collections;
  31 import java.util.Comparator;
  32 import java.util.List;
  33 import java.util.Map;
  34 import java.util.StringJoiner;
  35 
  36 import jdk.jfr.EventType;
  37 import jdk.jfr.Recording;
  38 import jdk.jfr.SettingDescriptor;
  39 import jdk.jfr.internal.LogLevel;
  40 import jdk.jfr.internal.LogTag;
  41 import jdk.jfr.internal.Logger;
  42 
  43 /**
  44  * JFR.check - invoked from native
  45  *
  46  */
  47 final class DCmdCheck extends AbstractDCmd {
  48     /**
  49      * Execute JFR.check
  50      *
  51      * @param recordingText name or id of the recording to check, or
  52      *        <code>null</code> to show a list of all recordings.
  53      *
  54      * @param verbose if event settings should be included.
  55      *
  56      * @return result output
  57      *
  58      * @throws DCmdException if the check could not be completed.
  59      */
  60     public String execute(String recordingText, Boolean verbose) throws DCmdException {
  61         executeInternal(recordingText, verbose);
  62         return getResult();
  63     }
  64 
  65     private void executeInternal(String name, Boolean verbose) throws DCmdException {
  66         if (LogTag.JFR_DCMD.shouldLog(LogLevel.DEBUG)) {
  67             Logger.log(LogTag.JFR_DCMD, LogLevel.DEBUG, "Executing DCmdCheck: name=" + name + ", verbose=" + verbose);
  68         }
  69 
  70         if (verbose == null) {
  71             verbose = Boolean.FALSE;
  72         }
  73 
  74         if (name != null) {
  75             printRecording(findRecording(name), verbose);
  76             return;
  77         }
  78 
  79         List<Recording> recordings = getRecordings();
  80         if (!verbose && recordings.isEmpty()) {
  81             println("No available recordings.");
  82             println();
  83             println("Use jcmd " + getPid() + " JFR.start to start a recording.");
  84             return;
  85         }
  86         boolean first = true;
  87         for (Recording recording : recordings) {
  88             // Print separation between recordings,
  89             if (!first) {
  90                 println();
  91                 if (Boolean.TRUE.equals(verbose)) {
  92                     println();
  93                 }
  94             }
  95             first = false;
  96             printRecording(recording, verbose);
  97         }
  98     }
  99 
 100     private void printRecording(Recording recording, boolean verbose) {
 101         printGeneral(recording);
 102         if (verbose) {
 103             println();
 104             printSetttings(recording);
 105         }
 106     }
 107 
 108     private void printGeneral(Recording recording) {
 109         print("Recording " + recording.getId() + ": name=" + recording.getName());
 110 
 111         Duration duration = recording.getDuration();
 112         if (duration != null) {
 113             print(" duration=");
 114             printTimespan(duration, "");
 115         }
 116 
 117         long maxSize = recording.getMaxSize();
 118         if (maxSize != 0) {
 119             print(" maxsize=");
 120             printBytes(maxSize, "");
 121         }
 122         Duration maxAge = recording.getMaxAge();
 123         if (maxAge != null) {
 124             print(" maxage=");
 125             printTimespan(maxAge, "");
 126         }
 127 
 128         print(" (" + recording.getState().toString().toLowerCase() + ")");
 129         println();
 130     }
 131 
 132     private void printSetttings(Recording recording) {
 133         Map<String, String> settings = recording.getSettings();
 134         for (EventType eventType : sortByEventPath(getFlightRecorder().getEventTypes())) {
 135             StringJoiner sj = new StringJoiner(",", "[", "]");
 136             sj.setEmptyValue("");
 137             for (SettingDescriptor s : eventType.getSettingDescriptors()) {
 138                 String settingsPath = eventType.getName() + "#" + s.getName();
 139                 if (settings.containsKey(settingsPath)) {
 140                     sj.add(s.getName() + "=" + settings.get(settingsPath));
 141                 }
 142             }
 143             String settingsText = sj.toString();
 144             if (!settingsText.isEmpty()) {
 145                 print(" %s (%s)", eventType.getLabel(), eventType.getName());
 146                 println();
 147                 println("   " + settingsText);
 148             }
 149         }
 150     }
 151 
 152     private static List<EventType> sortByEventPath(Collection<EventType> events) {
 153         List<EventType> sorted = new ArrayList<>();
 154         sorted.addAll(events);
 155         Collections.sort(sorted, new Comparator<EventType>() {
 156             @Override
 157             public int compare(EventType e1, EventType e2) {
 158                 return e1.getName().compareTo(e2.getName());
 159             }
 160         });
 161         return sorted;
 162     }
 163 }