|
1 | 1 | /* |
2 | | - * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
26 | 26 | package jdk.jfr.internal.tool; |
27 | 27 |
|
28 | 28 | import java.io.IOException; |
| 29 | +import java.io.PrintStream; |
29 | 30 | import java.io.PrintWriter; |
| 31 | +import java.nio.charset.Charset; |
30 | 32 | import java.nio.file.Path; |
| 33 | +import java.nio.file.Paths; |
| 34 | +import java.util.ArrayList; |
31 | 35 | import java.util.Collections; |
32 | 36 | import java.util.Comparator; |
33 | 37 | import java.util.Deque; |
34 | 38 | import java.util.List; |
| 39 | +import java.util.function.Predicate; |
35 | 40 |
|
| 41 | +import jdk.jfr.EventType; |
| 42 | +import jdk.jfr.FlightRecorder; |
36 | 43 | import jdk.jfr.consumer.RecordingFile; |
| 44 | +import jdk.jfr.internal.PlatformEventType; |
| 45 | +import jdk.jfr.internal.PrivateAccess; |
37 | 46 | import jdk.jfr.internal.Type; |
| 47 | +import jdk.jfr.internal.TypeLibrary; |
38 | 48 | import jdk.jfr.internal.consumer.JdkJfrConsumer; |
39 | 49 |
|
40 | 50 | final class Metadata extends Command { |
@@ -91,52 +101,165 @@ int groupValue(Type t) { |
91 | 101 | } |
92 | 102 | } |
93 | 103 |
|
94 | | - |
95 | 104 | @Override |
96 | 105 | public String getName() { |
97 | 106 | return "metadata"; |
98 | 107 | } |
99 | 108 |
|
100 | 109 | @Override |
101 | 110 | public List<String> getOptionSyntax() { |
102 | | - return Collections.singletonList("<file>"); |
| 111 | + List<String> list = new ArrayList<>(); |
| 112 | + list.add("[--categories <filter>]"); |
| 113 | + list.add("[--events <filter>]"); |
| 114 | + list.add("[<file>]"); |
| 115 | + return list; |
103 | 116 | } |
104 | 117 |
|
105 | 118 | @Override |
106 | | - public String getDescription() { |
| 119 | + protected String getTitle() { |
107 | 120 | return "Display event metadata, such as labels, descriptions and field layout"; |
108 | 121 | } |
109 | 122 |
|
| 123 | + @Override |
| 124 | + public String getDescription() { |
| 125 | + return getTitle() + ". See 'jfr help metadata' for details."; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void displayOptionUsage(PrintStream stream) { |
| 130 | + char q = quoteCharacter(); |
| 131 | + stream.println(" --categories <filter> Select events matching a category name."); |
| 132 | + stream.println(" The filter is a comma-separated list of names,"); |
| 133 | + stream.println(" simple and/or qualified, and/or quoted glob patterns"); |
| 134 | + stream.println(); |
| 135 | + stream.println(" --events <filter> Select events matching an event name."); |
| 136 | + stream.println(" The filter is a comma-separated list of names,"); |
| 137 | + stream.println(" simple and/or qualified, and/or quoted glob patterns"); |
| 138 | + stream.println(); |
| 139 | + stream.println(" <file> Location of the recording file (.jfr)"); |
| 140 | + stream.println(); |
| 141 | + stream.println("If the <file> parameter is omitted, metadata from the JDK where"); |
| 142 | + stream.println("the " + q + "jfr" + q + " tool is located will be used"); |
| 143 | + stream.println(); |
| 144 | + stream.println(); |
| 145 | + stream.println("Example usage:"); |
| 146 | + stream.println(); |
| 147 | + stream.println(" jfr metadata"); |
| 148 | + stream.println(); |
| 149 | + stream.println(" jfr metadata --events jdk.ThreadStart recording.jfr"); |
| 150 | + stream.println(); |
| 151 | + stream.println(" jfr metadata --events CPULoad,GarbageCollection"); |
| 152 | + stream.println(); |
| 153 | + stream.println(" jfr metadata --categories " + q + "GC,JVM,Java*" + q); |
| 154 | + stream.println(); |
| 155 | + stream.println(" jfr metadata --events " + q + "Thread*" + q); |
| 156 | + stream.println(); |
| 157 | + } |
| 158 | + |
110 | 159 | @Override |
111 | 160 | public void execute(Deque<String> options) throws UserSyntaxException, UserDataException { |
112 | | - Path file = getJFRInputFile(options); |
| 161 | + Path file = getOptionalJFRInputFile(options); |
113 | 162 |
|
114 | 163 | boolean showIds = false; |
| 164 | + boolean foundEventFilter = false; |
| 165 | + boolean foundCategoryFilter = false; |
| 166 | + Predicate<EventType> filter = null; |
115 | 167 | int optionCount = options.size(); |
116 | 168 | while (optionCount > 0) { |
117 | | - if (acceptOption(options, "--ids")) { |
| 169 | + // internal option, doest not export to users |
| 170 | + if (acceptSingleOption(options, "--ids")) { |
118 | 171 | showIds = true; |
119 | 172 | } |
| 173 | + if (acceptFilterOption(options, "--events")) { |
| 174 | + if (foundEventFilter) { |
| 175 | + throw new UserSyntaxException("use --events event1,event2,event3 to include multiple events"); |
| 176 | + } |
| 177 | + foundEventFilter = true; |
| 178 | + String filterStr = options.remove(); |
| 179 | + warnForWildcardExpansion("--events", filterStr); |
| 180 | + filter = addEventFilter(filterStr, filter); |
| 181 | + } |
| 182 | + if (acceptFilterOption(options, "--categories")) { |
| 183 | + if (foundCategoryFilter) { |
| 184 | + throw new UserSyntaxException("use --categories category1,category2 to include multiple categories"); |
| 185 | + } |
| 186 | + foundCategoryFilter = true; |
| 187 | + String filterStr = options.remove(); |
| 188 | + warnForWildcardExpansion("--categories", filterStr); |
| 189 | + filter = addCategoryFilter(filterStr, filter); |
| 190 | + } |
120 | 191 | if (optionCount == options.size()) { |
121 | 192 | // No progress made |
| 193 | + checkCommonError(options, "--event", "--events"); |
| 194 | + checkCommonError(options, "--category", "--categories"); |
122 | 195 | throw new UserSyntaxException("unknown option " + options.peek()); |
123 | 196 | } |
124 | 197 | optionCount = options.size(); |
125 | 198 | } |
126 | 199 |
|
127 | | - try (PrintWriter pw = new PrintWriter(System.out)) { |
| 200 | + try (PrintWriter pw = new PrintWriter(System.out, false, Charset.forName("UTF-8"))) { |
128 | 201 | PrettyWriter prettyWriter = new PrettyWriter(pw); |
129 | 202 | prettyWriter.setShowIds(showIds); |
130 | | - try (RecordingFile rf = new RecordingFile(file)) { |
131 | | - List<Type> types = PRIVATE_ACCESS.readTypes(rf); |
132 | | - Collections.sort(types, new TypeComparator()); |
133 | | - for (Type type : types) { |
| 203 | + if (filter != null) { |
| 204 | + filter = addCache(filter, type -> type.getId()); |
| 205 | + } |
| 206 | + |
| 207 | + List<Type> types = findTypes(file); |
| 208 | + Collections.sort(types, new TypeComparator()); |
| 209 | + for (Type type : types) { |
| 210 | + if (filter != null) { |
| 211 | + // If --events or --categories, only operate on events |
| 212 | + if (Type.SUPER_TYPE_EVENT.equals(type.getSuperType())) { |
| 213 | + EventType et = PrivateAccess.getInstance().newEventType((PlatformEventType) type); |
| 214 | + if (filter.test(et)) { |
| 215 | + prettyWriter.printType(type); |
| 216 | + } |
| 217 | + } |
| 218 | + } else { |
134 | 219 | prettyWriter.printType(type); |
135 | 220 | } |
136 | | - prettyWriter.flush(true); |
137 | | - } catch (IOException ioe) { |
138 | | - couldNotReadError(file, ioe); |
139 | 221 | } |
| 222 | + prettyWriter.flush(true); |
| 223 | + pw.flush(); |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + private List<Type> findTypes(Path file) throws UserDataException { |
| 228 | + // Determine whether reading from recording file or reading from the JDK where |
| 229 | + // the jfr tool is located will be used |
| 230 | + if (file == null) { |
| 231 | + // Force initialization |
| 232 | + FlightRecorder.getFlightRecorder().getEventTypes(); |
| 233 | + return TypeLibrary.getInstance().getTypes(); |
| 234 | + } |
| 235 | + try (RecordingFile rf = new RecordingFile(file)) { |
| 236 | + return PRIVATE_ACCESS.readTypes(rf); |
| 237 | + } catch (IOException ioe) { |
| 238 | + couldNotReadError(file, ioe); |
| 239 | + } |
| 240 | + return null; // Can't reach |
| 241 | + } |
| 242 | + |
| 243 | + private Path getOptionalJFRInputFile(Deque<String> options) throws UserDataException { |
| 244 | + if (!options.isEmpty()) { |
| 245 | + String file = options.getLast(); |
| 246 | + if (!file.startsWith("--")) { |
| 247 | + Path tmp = Paths.get(file).toAbsolutePath(); |
| 248 | + if (tmp.toString().endsWith(".jfr")) { |
| 249 | + ensureAccess(tmp); |
| 250 | + options.removeLast(); |
| 251 | + return tmp; |
| 252 | + } |
| 253 | + } |
| 254 | + } |
| 255 | + return null; |
| 256 | + } |
| 257 | + |
| 258 | + private static boolean acceptSingleOption(Deque<String> options, String expected) { |
| 259 | + if (expected.equals(options.peek())) { |
| 260 | + options.remove(); |
| 261 | + return true; |
140 | 262 | } |
| 263 | + return false; |
141 | 264 | } |
142 | 265 | } |
0 commit comments