8256156: JFR: Allow 'jfr' tool to show metadata without a recording

Reviewed-by: egahlin
This commit is contained in:
Yi Yang 2021-03-13 02:03:07 +00:00 committed by Erik Gahlin
parent 0b68ced027
commit 86e4c755f9
5 changed files with 363 additions and 133 deletions
src/jdk.jfr/share/classes/jdk/jfr/internal/tool
test/jdk/jdk/jfr/tool

@ -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,6 +31,7 @@ import java.io.IOError;
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;
@ -38,7 +39,13 @@ 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 @@ abstract class Command {
}
}
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 @@ abstract class Command {
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 final class Main {
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,7 +101,6 @@ final class Metadata extends Command {
}
}
@Override
public String getName() {
return "metadata";
@ -99,44 +108,158 @@ final class Metadata extends Command {
@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;
}
}

@ -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
@ -33,10 +33,7 @@ import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.ArrayList;
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;
@ -171,12 +168,6 @@ final class Print extends Command {
pw.flush();
}
private 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 + "?");
}
}
private static boolean acceptFormatterOption(Deque<String> options, EventPrintWriter eventWriter, String expected) throws UserSyntaxException {
if (expected.equals(options.peek())) {
if (eventWriter != null) {
@ -187,102 +178,4 @@ final class Print extends Command {
}
return false;
}
private 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));
}
private static <T> Predicate<T> recurseIfPossible(Predicate<T> filter) {
return x -> filter != null && filter.test(x);
}
private 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);
}
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 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);
}
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;
}
static char quoteCharacter() {
return File.pathSeparatorChar == ';' ? '"' : '\'';
}
}

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020, 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,11 +26,19 @@
package jdk.jfr.tool;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import jdk.jfr.Category;
import jdk.jfr.Event;
import jdk.jfr.EventType;
import jdk.jfr.FlightRecorder;
import jdk.jfr.Name;
import jdk.jfr.Registered;
import jdk.jfr.consumer.RecordingFile;
import jdk.test.lib.Asserts;
import jdk.test.lib.process.OutputAnalyzer;
/**
@ -44,14 +52,23 @@ import jdk.test.lib.process.OutputAnalyzer;
public class TestMetadata {
public static void main(String[] args) throws Throwable {
testUnfiltered();
testIllegalOption();
testNumberOfEventTypes();
FlightRecorder.register(MyEvent1.class);
FlightRecorder.register(MyEvent2.class);
FlightRecorder.register(MyEvent3.class);
String file = ExecuteHelper.createProfilingRecording().toAbsolutePath().toAbsolutePath().toString();
testEventFilter(file);
testWildcard(file);
}
static void testUnfiltered() throws Throwable {
Path f = ExecuteHelper.createProfilingRecording().toAbsolutePath();
String file = f.toAbsolutePath().toString();
OutputAnalyzer output = ExecuteHelper.jfr("metadata");
output.shouldContain("missing file");
output = ExecuteHelper.jfr("metadata", "--wrongOption", file);
output.shouldContain("unknown option --wrongOption");
output.shouldContain("extends jdk.jfr.Event");
output = ExecuteHelper.jfr("metadata", file);
try (RecordingFile rf = new RecordingFile(f)) {
@ -75,4 +92,88 @@ public class TestMetadata {
lineNumber++;
}
}
}
static void testIllegalOption() throws Throwable {
Path f = ExecuteHelper.createProfilingRecording().toAbsolutePath();
String file = f.toAbsolutePath().toString();
OutputAnalyzer output = ExecuteHelper.jfr("metadata", "--wrongOption", file);
output.shouldContain("unknown option --wrongOption");
output = ExecuteHelper.jfr("metadata", "--wrongOption2");
output.shouldContain("unknown option --wrongOption2");
}
static void testNumberOfEventTypes() throws Throwable {
OutputAnalyzer output = ExecuteHelper.jfr("metadata");
int count = 0;
for (String line : output.asLines()) {
if (line.contains("extends jdk.jfr.Event")) {
count++;
}
}
Asserts.assertEquals(count, FlightRecorder.getFlightRecorder().getEventTypes().size());
}
static void testEventFilter(String file) throws Throwable {
OutputAnalyzer output = ExecuteHelper.jfr("metadata", "--events", "MyEvent1,MyEvent2", file);
int count = 0;
for (String line : output.asLines()) {
if (line.contains("extends jdk.jfr.Event")) {
Asserts.assertTrue(line.contains("MyEvent1") || line.contains("MyEvent2"));
count++;
}
}
Asserts.assertEQ(count, 2);
output = ExecuteHelper.jfr("metadata", "--categories", "Customized", file);
count = 0;
for (String line : output.asLines()) {
if (line.contains("extends jdk.jfr.Event")) {
Asserts.assertTrue(line.contains("MyEvent1") || line.contains("MyEvent2") || line.contains("MyEvent3"));
count++;
}
}
Asserts.assertEQ(count, 3);
}
static void testWildcard(String file) throws Throwable {
OutputAnalyzer output = ExecuteHelper.jfr("metadata", "--events", "MyEv*", file);
int count = 0;
for (String line : output.asLines()) {
if (line.contains("extends jdk.jfr.Event")) {
count++;
Asserts.assertTrue(line.contains("MyEvent"));
}
}
Asserts.assertEQ(count, 3);
output = ExecuteHelper.jfr("metadata", "--categories", "Custo*", file);
count = 0;
for (String line : output.asLines()) {
if (line.startsWith("@Category")) {
Asserts.assertTrue(line.contains("Customized"));
}
if (line.contains("extends jdk.jfr.Event")) {
count++;
Asserts.assertTrue(line.contains("MyEvent"));
}
}
Asserts.assertEQ(count, 3);
}
@Registered(false)
@Category("Customized")
@Name("MyEvent1")
private static class MyEvent1 extends Event {
}
@Registered(false)
@Category("Customized")
@Name("MyEvent2")
private static class MyEvent2 extends Event {
}
@Registered(false)
@Category("Customized")
@Name("MyEvent3")
private static class MyEvent3 extends Event {
}
}