1 /* 2 * Copyright (c) 2017, 2019, 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 /** 27 * This package contains classes for consuming Flight Recorder data. 28 * <p> 29 * In the following example, the program prints a histogram of all method samples in a recording. 30 * <pre> 31 * <code> 32 * public static void main(String[] args) { 33 * if (args.length != 0) { 34 * System.out.println("Must specify recording file."); 35 * return; 36 * } 37 * try (RecordingFile f = new RecordingFile(Paths.get(args[0]))) { 38 * Map{@literal <}String, SimpleEntry{@literal <}String, Integer{@literal >}{@literal >} histogram = new HashMap{@literal <}{@literal >}(); 39 * int total = 0; 40 * while (f.hasMoreEvents()) { 41 * RecordedEvent event = f.readEvent(); 42 * if (event.getEventType().getName().equals("com.oracle.jdk.ExecutionSample")) { 43 * RecordedStackTrace s = event.getStackTrace(); 44 * if (s != null) { 45 * RecordedFrame topFrame= s.getFrames().get(0); 46 * if (topFrame.isJavaFrame()) { 47 * RecordedMethod method = topFrame.getMethod(); 48 * String methodName = method.getType().getName() + "#" + method.getName() + " " + method.getDescriptor(); 49 * Entry entry = histogram.computeIfAbsent(methodName, u -{@literal >} new SimpleEntry{@literal <}String, Integer{@literal >}(methodName, 0)); 50 * entry.setValue(entry.getValue() + 1); 51 * total++; 52 * } 53 * } 54 * } 55 * } 56 * List{@literal <}SimpleEntry{@literal <}String, Integer{@literal >}{@literal >} entries = new ArrayList{@literal <}{@literal >}(histogram.values()); 57 * entries.sort((u, v) -{@literal >} v.getValue().compareTo(u.getValue())); 58 * for (SimpleEntry{@literal <}String, Integer{@literal >} c : entries) { 59 * System.out.printf("%2.0f%% %s\n", 100 * (float) c.getValue() / total, c.getKey()); 60 * } 61 * System.out.println("\nSample count: " + total); 62 * } catch (IOException ioe) { 63 * System.out.println("Error reading file " + args[0] + ". " + ioe.getMessage()); 64 * } 65 * } 66 * </code> 67 * </pre> 68 * <p> 69 * <b>Null-handling</b> 70 * <p> 71 * All methods define whether they accept or return {@code null} in the Javadoc. 72 * Typically this is expressed as {@code "not null"}. If a {@code null} 73 * parameter is used where it is not allowed, a 74 * {@code java.lang.NullPointerException} is thrown. If a {@code null} 75 * parameters is passed to a method that throws other exceptions, such as 76 * {@code java.io.IOException}, the {@code java.lang.NullPointerException} takes 77 * precedence, unless the Javadoc for the method explicitly states how 78 * {@code null} is handled, i.e. by throwing {@code java.lang.IllegalArgumentException}. 79 * 80 * @commercialFeature 81 * @since 9 82 */ 83 package jdk.jfr.consumer;