src/jdk.jfr/share/classes/jdk/jfr/internal/tool/Summary.java
author mgronlun
Wed, 30 Oct 2019 19:43:52 +0100
changeset 58863 c16ac7a2eba4
parent 52850 f527b24990d7
permissions -rw-r--r--
8226511: Implement JFR Event Streaming Reviewed-by: egahlin, mseledtsov, mgronlun Contributed-by: erik.gahlin@oracle.com, mikhailo.seledtsov@oracle.com, markus.gronlund@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52850
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
     1
/*
58863
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 52850
diff changeset
     2
 * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
52850
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
     4
 *
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    10
 *
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    15
 * accompanied this code).
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    16
 *
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    20
 *
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    23
 * questions.
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    24
 */
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    25
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    26
package jdk.jfr.internal.tool;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    27
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    28
import java.io.IOException;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    29
import java.io.PrintStream;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    30
import java.nio.file.Path;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    31
import java.time.Instant;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    32
import java.time.ZoneOffset;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    33
import java.time.format.DateTimeFormatter;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    34
import java.util.ArrayList;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    35
import java.util.Collections;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    36
import java.util.Deque;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    37
import java.util.HashMap;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    38
import java.util.List;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    39
import java.util.Locale;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    40
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    41
import jdk.jfr.EventType;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    42
import jdk.jfr.internal.MetadataDescriptor;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    43
import jdk.jfr.internal.Type;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    44
import jdk.jfr.internal.consumer.ChunkHeader;
58863
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 52850
diff changeset
    45
import jdk.jfr.internal.consumer.FileAccess;
52850
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    46
import jdk.jfr.internal.consumer.RecordingInput;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    47
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    48
final class Summary extends Command {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    49
    private final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withLocale(Locale.UK).withZone(ZoneOffset.UTC);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    50
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    51
    @Override
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    52
    public String getName() {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    53
        return "summary";
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    54
    }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    55
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    56
    private static class Statistics {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    57
        Statistics(String name) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    58
            this.name = name;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    59
        }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    60
        String name;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    61
        long count;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    62
        long size;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    63
    }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    64
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    65
    @Override
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    66
    public List<String> getOptionSyntax() {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    67
        return Collections.singletonList("<file>");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    68
    }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    69
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    70
    @Override
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    71
    public void displayOptionUsage(PrintStream stream) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    72
        stream.println("  <file>   Location of the recording file (.jfr) to display information about");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    73
    }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    74
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    75
    @Override
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    76
    public String getDescription() {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    77
        return "Display general information about a recording file (.jfr)";
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    78
    }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    79
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    80
    @Override
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    81
    public void execute(Deque<String> options) throws UserSyntaxException, UserDataException {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    82
        ensureMaxArgumentCount(options, 1);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    83
        Path p = getJFRInputFile(options);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    84
        try {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    85
            printInformation(p);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    86
        } catch (IOException e) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    87
            couldNotReadError(p, e);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    88
        }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    89
    }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    90
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    91
    private void printInformation(Path p) throws IOException {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    92
        long totalDuration = 0;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    93
        long chunks = 0;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    94
58863
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 52850
diff changeset
    95
        try (RecordingInput input = new RecordingInput(p.toFile(), FileAccess.UNPRIVILIGED)) {
52850
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    96
            ChunkHeader first = new ChunkHeader(input);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    97
            ChunkHeader ch = first;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    98
            String eventPrefix = Type.EVENT_NAME_PREFIX;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    99
            if (first.getMajor() == 1) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   100
                eventPrefix = "com.oracle.jdk.";
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   101
            }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   102
            HashMap<Long, Statistics> stats = new HashMap<>();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   103
            stats.put(0L, new Statistics(eventPrefix + "Metadata"));
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   104
            stats.put(1L, new Statistics(eventPrefix + "CheckPoint"));
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   105
            int minWidth = 0;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   106
            while (true) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   107
                long chunkEnd = ch.getEnd();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   108
                MetadataDescriptor md = ch.readMetadata();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   109
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   110
                for (EventType eventType : md.getEventTypes()) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   111
                    stats.computeIfAbsent(eventType.getId(), (e) -> new Statistics(eventType.getName()));
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   112
                    minWidth = Math.max(minWidth, eventType.getName().length());
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   113
                }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   114
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   115
                totalDuration += ch.getDurationNanos();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   116
                chunks++;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   117
                input.position(ch.getEventStart());
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   118
                while (input.position() < chunkEnd) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   119
                    long pos = input.position();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   120
                    int size = input.readInt();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   121
                    long eventTypeId = input.readLong();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   122
                    Statistics s = stats.get(eventTypeId);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   123
                    if (s != null) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   124
                        s.count++;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   125
                        s.size += size;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   126
                    }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   127
                    input.position(pos + size);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   128
                }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   129
                if (ch.isLastChunk()) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   130
                    break;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   131
                }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   132
                ch = ch.nextHeader();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   133
            }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   134
            println();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   135
            long epochSeconds = first.getStartNanos() / 1_000_000_000L;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   136
            long adjustNanos = first.getStartNanos() - epochSeconds * 1_000_000_000L;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   137
            println(" Version: " + first.getMajor() + "." + first.getMinor());
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   138
            println(" Chunks: " + chunks);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   139
            println(" Start: " + DATE_FORMAT.format(Instant.ofEpochSecond(epochSeconds, adjustNanos)) + " (UTC)");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   140
            println(" Duration: " + (totalDuration + 500_000_000) / 1_000_000_000 + " s");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   141
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   142
            List<Statistics> statsList = new ArrayList<>(stats.values());
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   143
            Collections.sort(statsList, (u, v) -> Long.compare(v.count, u.count));
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   144
            println();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   145
            String header = "      Count  Size (bytes) ";
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   146
            String typeHeader = " Event Type";
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   147
            minWidth = Math.max(minWidth, typeHeader.length());
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   148
            println(typeHeader + pad(minWidth - typeHeader.length(), ' ') + header);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   149
            println(pad(minWidth + header.length(), '='));
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   150
            for (Statistics s : statsList) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   151
                System.out.printf(" %-" + minWidth + "s%10d  %12d\n", s.name, s.count, s.size);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   152
            }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   153
        }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   154
    }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   155
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   156
    private String pad(int count, char c) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   157
        StringBuilder sb = new StringBuilder();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   158
        for (int i = 0; i < count; i++) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   159
            sb.append(c);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   160
        }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   161
        return sb.toString();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   162
    }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   163
}