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