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.cmd;
  27 
  28 import java.io.IOException;
  29 import java.nio.file.Path;
  30 import java.nio.file.Paths;
  31 import java.time.Duration;
  32 import java.time.Instant;
  33 import java.util.ArrayList;
  34 import java.util.Collections;
  35 import java.util.Deque;
  36 import java.util.HashMap;
  37 import java.util.List;
  38 
  39 import jdk.jfr.EventType;
  40 import jdk.jfr.internal.MetadataDescriptor;
  41 import jdk.jfr.internal.consumer.ChunkHeader;
  42 import jdk.jfr.internal.consumer.RecordingInput;
  43 
  44 final class SummaryCommand extends Command {
  45 
  46     private static class Statistics {
  47         Statistics(String name) {
  48             this.name = name;
  49         }
  50 
  51         String name;
  52         long count;
  53         long size;
  54     }
  55 
  56     @Override
  57     public String getOptionSyntax() {
  58         return "<file>";
  59     }
  60 
  61     @Override
  62     public void displayOptionUsage() {
  63         println("  <file>   Location of the recording file (.jfr) to display information about");
  64     }
  65 
  66     @Override
  67     public String getName() {
  68         return "summary";
  69     }
  70 
  71     @Override
  72     public String getDescription() {
  73         return "Display general information about a recording file (.jfr)";
  74     }
  75 
  76     @Override
  77     public void execute(Deque<String> options) {
  78         if (options.isEmpty()) {
  79             userFailed("Missing file");
  80         }
  81         ensureMaxArgumentCount(options, 1);
  82         Path p = Paths.get(options.remove());
  83         ensureFileExist(p);
  84         ensureJFRFile(p);
  85         try {
  86             printInformation(p);
  87         } catch (IOException e) {
  88             throw new IllegalStateException("Unexpected error. " + e.getMessage());
  89         }
  90     }
  91 
  92     private void printInformation(Path p) throws IOException {
  93         long totalSize = 0;
  94         long totalDuration = 0;
  95         long chunks = 0;
  96 
  97         try (RecordingInput input = new RecordingInput(p.toFile())) {
  98             ChunkHeader first = new ChunkHeader(input);
  99             ChunkHeader ch = first;
 100             HashMap<Long, Statistics> stats = new HashMap<>();
 101             stats.put(0L, new Statistics("com.oracle.jdk.Metadata"));
 102             stats.put(1L, new Statistics("com.oracle.jdk.CheckPoint"));
 103             int minWidth = 0;
 104             while (true) {
 105                 long chunkEnd = ch.getEnd();
 106                 MetadataDescriptor md = ch.readMetadata();
 107 
 108                 for (EventType eventType : md.getEventTypes()) {
 109                     stats.computeIfAbsent(eventType.getId(), (e) -> new Statistics(eventType.getName()));
 110                     minWidth = Math.max(minWidth, eventType.getName().length());
 111                 }
 112 
 113                 totalSize += ch.getSize();
 114                 totalDuration += ch.getDuration();
 115                 chunks++;
 116                 input.position(ch.getEventStart());
 117                 while (input.position() < chunkEnd) {
 118 
 119                     long pos = input.position();
 120                     int size = input.readInt();
 121                     long eventTypeId = input.readLong();
 122                     Statistics s = stats.get(eventTypeId);
 123 
 124                     if (s != null) {
 125                         s.count++;
 126                         s.size += size;
 127                     }
 128                     input.position(pos + size);
 129                 }
 130                 if (ch.isLastChunk()) {
 131                     break;
 132                 }
 133                 ch = ch.nextHeader();
 134             }
 135             println();
 136             long epochSeconds = first.getStartNanos() / 1_000_000_000L;
 137             long adjustNanos = first.getStartNanos() - epochSeconds * 1_000_000_000L;
 138             println(" Version: " + first.getMajor() + "." + first.getMinor());
 139             println(" Chunks: " + chunks);
 140             println(" Size: " + totalSize + " bytes");
 141             println(" Start: " + Instant.ofEpochSecond(epochSeconds, adjustNanos));
 142             println(" Duration: " + Duration.ofNanos(totalDuration));
 143             println();
 144             println(" Start Ticks: " + first.getStartTicks());
 145             println(" Ticks / Second: " + first.getTicksPerSecond());
 146 
 147             List<Statistics> statsList = new ArrayList<>(stats.values());
 148             Collections.sort(statsList, (u, v) -> Long.compare(v.count, u.count));
 149             println();
 150             String header = "      Count  Size (bytes) ";
 151             String typeHeader = " Event Type";
 152             minWidth = Math.max(minWidth, typeHeader.length());
 153             println(typeHeader + pad(minWidth - typeHeader.length(), ' ') + header);
 154             println(pad(minWidth + header.length(), '='));
 155             for (Statistics s : statsList) {
 156                 System.out.printf(" %-" + minWidth + "s%10d  %12d\n", s.name, s.count, s.size);
 157             }
 158         }
 159     }
 160 
 161     private String pad(int count, char c) {
 162         StringBuilder sb = new StringBuilder();
 163         for (int i = 0; i < count; i++) {
 164             sb.append(c);
 165         }
 166         return sb.toString();
 167     }
 168 }