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