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