Skip to content
Permalink
Browse files
8256156: JFR: Allow 'jfr' tool to show metadata without a recording
Reviewed-by: egahlin
  • Loading branch information
Yi Yang authored and egahlin committed Mar 13, 2021
1 parent 0b68ced commit 86e4c755f90a40330cfb72a7ff11bf4236831be2
Show file tree
Hide file tree
Showing 5 changed files with 363 additions and 133 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -31,14 +31,21 @@
import java.io.IOException;
import java.io.PrintStream;
import java.io.RandomAccessFile;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;

import jdk.jfr.EventType;

abstract class Command {
public final static String title = "Tool for working with Flight Recorder files (.jfr)";
@@ -236,7 +243,7 @@ final protected Path getJFRInputFile(Deque<String> options) throws UserSyntaxExc
}
}

private void ensureAccess(Path path) throws UserDataException {
final protected void ensureAccess(Path path) throws UserDataException {
try (RandomAccessFile rad = new RandomAccessFile(path.toFile(), "r")) {
if (rad.length() == 0) {
throw new UserDataException("file is empty '" + path + "'");
@@ -303,4 +310,108 @@ public List<String> getNames() {
names.addAll(getAliases());
return names;
}
}

public static void checkCommonError(Deque<String> options, String typo, String correct) throws UserSyntaxException {
if (typo.equals(options.peek())) {
throw new UserSyntaxException("unknown option " + typo + ", did you mean " + correct + "?");
}
}

final protected static char quoteCharacter() {
return File.pathSeparatorChar == ';' ? '"' : '\'';
}

private static <T> Predicate<T> recurseIfPossible(Predicate<T> filter) {
return x -> filter != null && filter.test(x);
}

private static String acronomify(String multipleWords) {
boolean newWord = true;
String acronym = "";
for (char c : multipleWords.toCharArray()) {
if (newWord) {
if (Character.isAlphabetic(c) && Character.isUpperCase(c)) {
acronym += c;
}
}
newWord = Character.isWhitespace(c);
}
return acronym;
}

private static boolean match(String text, String filter) {
if (filter.length() == 0) {
// empty filter string matches if string is empty
return text.length() == 0;
}
if (filter.charAt(0) == '*') { // recursive check
filter = filter.substring(1);
for (int n = 0; n <= text.length(); n++) {
if (match(text.substring(n), filter))
return true;
}
} else if (text.length() == 0) {
// empty string and non-empty filter does not match
return false;
} else if (filter.charAt(0) == '?') {
// eat any char and move on
return match(text.substring(1), filter.substring(1));
} else if (filter.charAt(0) == text.charAt(0)) {
// eat chars and move on
return match(text.substring(1), filter.substring(1));
}
return false;
}

private static List<String> explodeFilter(String filter) throws UserSyntaxException {
List<String> list = new ArrayList<>();
for (String s : filter.split(",")) {
s = s.trim();
if (!s.isEmpty()) {
list.add(s);
}
}
return list;
}

final protected static Predicate<EventType> addCategoryFilter(String filterText, Predicate<EventType> eventFilter) throws UserSyntaxException {
List<String> filters = explodeFilter(filterText);
Predicate<EventType> newFilter = recurseIfPossible(eventType -> {
for (String category : eventType.getCategoryNames()) {
for (String filter : filters) {
if (match(category, filter)) {
return true;
}
if (category.contains(" ") && acronomify(category).equals(filter)) {
return true;
}
}
}
return false;
});
return eventFilter == null ? newFilter : eventFilter.or(newFilter);
}

final protected static Predicate<EventType> addEventFilter(String filterText, final Predicate<EventType> eventFilter) throws UserSyntaxException {
List<String> filters = explodeFilter(filterText);
Predicate<EventType> newFilter = recurseIfPossible(eventType -> {
for (String filter : filters) {
String fullEventName = eventType.getName();
if (match(fullEventName, filter)) {
return true;
}
String eventName = fullEventName.substring(fullEventName.lastIndexOf(".") + 1);
if (match(eventName, filter)) {
return true;
}
}
return false;
});
return eventFilter == null ? newFilter : eventFilter.or(newFilter);
}

final protected static <T, X> Predicate<T> addCache(final Predicate<T> filter, Function<T, X> cacheFunction) {
Map<X, Boolean> cache = new HashMap<>();
return t -> cache.computeIfAbsent(cacheFunction.apply(t), x -> filter.test(t));
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -75,6 +75,8 @@ public static void main(String... args) {
System.out.println();
System.out.println(" jfr metadata recording.jfr");
System.out.println();
System.out.println(" jfr metadata --categories GC,Detailed");
System.out.println();
System.out.println("For more information about available commands, use 'jfr help'");
System.exit(EXIT_OK);
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -26,15 +26,25 @@
package jdk.jfr.internal.tool;

import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.List;
import java.util.function.Predicate;

import jdk.jfr.EventType;
import jdk.jfr.FlightRecorder;
import jdk.jfr.consumer.RecordingFile;
import jdk.jfr.internal.PlatformEventType;
import jdk.jfr.internal.PrivateAccess;
import jdk.jfr.internal.Type;
import jdk.jfr.internal.TypeLibrary;
import jdk.jfr.internal.consumer.JdkJfrConsumer;

final class Metadata extends Command {
@@ -91,52 +101,165 @@ int groupValue(Type t) {
}
}


@Override
public String getName() {
return "metadata";
}

@Override
public List<String> getOptionSyntax() {
return Collections.singletonList("<file>");
List<String> list = new ArrayList<>();
list.add("[--categories <filter>]");
list.add("[--events <filter>]");
list.add("[<file>]");
return list;
}

@Override
public String getDescription() {
protected String getTitle() {
return "Display event metadata, such as labels, descriptions and field layout";
}

@Override
public String getDescription() {
return getTitle() + ". See 'jfr help metadata' for details.";
}

@Override
public void displayOptionUsage(PrintStream stream) {
char q = quoteCharacter();
stream.println(" --categories <filter> Select events matching a category name.");
stream.println(" The filter is a comma-separated list of names,");
stream.println(" simple and/or qualified, and/or quoted glob patterns");
stream.println();
stream.println(" --events <filter> Select events matching an event name.");
stream.println(" The filter is a comma-separated list of names,");
stream.println(" simple and/or qualified, and/or quoted glob patterns");
stream.println();
stream.println(" <file> Location of the recording file (.jfr)");
stream.println();
stream.println("If the <file> parameter is omitted, metadata from the JDK where");
stream.println("the " + q + "jfr" + q + " tool is located will be used");
stream.println();
stream.println();
stream.println("Example usage:");
stream.println();
stream.println(" jfr metadata");
stream.println();
stream.println(" jfr metadata --events jdk.ThreadStart recording.jfr");
stream.println();
stream.println(" jfr metadata --events CPULoad,GarbageCollection");
stream.println();
stream.println(" jfr metadata --categories " + q + "GC,JVM,Java*" + q);
stream.println();
stream.println(" jfr metadata --events " + q + "Thread*" + q);
stream.println();
}

@Override
public void execute(Deque<String> options) throws UserSyntaxException, UserDataException {
Path file = getJFRInputFile(options);
Path file = getOptionalJFRInputFile(options);

boolean showIds = false;
boolean foundEventFilter = false;
boolean foundCategoryFilter = false;
Predicate<EventType> filter = null;
int optionCount = options.size();
while (optionCount > 0) {
if (acceptOption(options, "--ids")) {
// internal option, doest not export to users
if (acceptSingleOption(options, "--ids")) {
showIds = true;
}
if (acceptFilterOption(options, "--events")) {
if (foundEventFilter) {
throw new UserSyntaxException("use --events event1,event2,event3 to include multiple events");
}
foundEventFilter = true;
String filterStr = options.remove();
warnForWildcardExpansion("--events", filterStr);
filter = addEventFilter(filterStr, filter);
}
if (acceptFilterOption(options, "--categories")) {
if (foundCategoryFilter) {
throw new UserSyntaxException("use --categories category1,category2 to include multiple categories");
}
foundCategoryFilter = true;
String filterStr = options.remove();
warnForWildcardExpansion("--categories", filterStr);
filter = addCategoryFilter(filterStr, filter);
}
if (optionCount == options.size()) {
// No progress made
checkCommonError(options, "--event", "--events");
checkCommonError(options, "--category", "--categories");
throw new UserSyntaxException("unknown option " + options.peek());
}
optionCount = options.size();
}

try (PrintWriter pw = new PrintWriter(System.out)) {
try (PrintWriter pw = new PrintWriter(System.out, false, Charset.forName("UTF-8"))) {
PrettyWriter prettyWriter = new PrettyWriter(pw);
prettyWriter.setShowIds(showIds);
try (RecordingFile rf = new RecordingFile(file)) {
List<Type> types = PRIVATE_ACCESS.readTypes(rf);
Collections.sort(types, new TypeComparator());
for (Type type : types) {
if (filter != null) {
filter = addCache(filter, type -> type.getId());
}

List<Type> types = findTypes(file);
Collections.sort(types, new TypeComparator());
for (Type type : types) {
if (filter != null) {
// If --events or --categories, only operate on events
if (Type.SUPER_TYPE_EVENT.equals(type.getSuperType())) {
EventType et = PrivateAccess.getInstance().newEventType((PlatformEventType) type);
if (filter.test(et)) {
prettyWriter.printType(type);
}
}
} else {
prettyWriter.printType(type);
}
prettyWriter.flush(true);
} catch (IOException ioe) {
couldNotReadError(file, ioe);
}
prettyWriter.flush(true);
pw.flush();
}
}

private List<Type> findTypes(Path file) throws UserDataException {
// Determine whether reading from recording file or reading from the JDK where
// the jfr tool is located will be used
if (file == null) {
// Force initialization
FlightRecorder.getFlightRecorder().getEventTypes();
return TypeLibrary.getInstance().getTypes();
}
try (RecordingFile rf = new RecordingFile(file)) {
return PRIVATE_ACCESS.readTypes(rf);
} catch (IOException ioe) {
couldNotReadError(file, ioe);
}
return null; // Can't reach
}

private Path getOptionalJFRInputFile(Deque<String> options) throws UserDataException {
if (!options.isEmpty()) {
String file = options.getLast();
if (!file.startsWith("--")) {
Path tmp = Paths.get(file).toAbsolutePath();
if (tmp.toString().endsWith(".jfr")) {
ensureAccess(tmp);
options.removeLast();
return tmp;
}
}
}
return null;
}

private static boolean acceptSingleOption(Deque<String> options, String expected) {
if (expected.equals(options.peek())) {
options.remove();
return true;
}
return false;
}
}

0 comments on commit 86e4c75

Please sign in to comment.