1 /* 2 * Copyright (c) 2015, 2018, 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 package jdk.jfr.jcmd; 27 28 import java.io.File; 29 import java.io.IOException; 30 import java.util.ArrayList; 31 import java.util.Collections; 32 import java.util.HashMap; 33 import java.util.List; 34 import java.util.Map; 35 36 import jdk.jfr.Enabled; 37 import jdk.jfr.Recording; 38 import jdk.jfr.consumer.RecordedEvent; 39 import jdk.jfr.consumer.RecordedObject; 40 import jdk.jfr.consumer.RecordingFile; 41 import jdk.jfr.internal.test.WhiteBox; 42 import jdk.test.lib.jfr.EventNames; 43 44 /* 45 * @test 46 * @summary Start a recording with or without path-to-gc-roots 47 * @modules jdk.jfr/jdk.jfr.internal.test 48 * @library /test/lib /test/jdk 49 * @key jfr 50 * 51 * @run main/othervm -XX:TLABSize=2k jdk.jfr.jcmd.TestJcmdDumpPathToGCRoots 52 */ 53 public class TestJcmdDumpPathToGCRoots { 54 55 private static final int OBJECT_COUNT = 100_000; 56 public static List<Object[]> leak = new ArrayList<>(OBJECT_COUNT); 57 58 public static void main(String[] args) throws Exception { 59 WhiteBox.setWriteAllObjectSamples(true); 60 61 String settingName = EventNames.OldObjectSample + "#" + "cutoff"; 62 63 // dump parameter trumps previous setting 64 testDump("path-to-gc-roots=true", Collections.singletonMap(settingName, "infinity"), true); 65 testDump("path-to-gc-roots=true", Collections.singletonMap(settingName, "0 ns"), true); 66 testDump("path-to-gc-roots=true", Collections.emptyMap(), true); 67 68 testDump("path-to-gc-roots=false", Collections.singletonMap(settingName, "infinity"), false); 69 testDump("path-to-gc-roots=false", Collections.singletonMap(settingName, "0 ns"), false); 70 testDump("path-to-gc-roots=false", Collections.emptyMap(), false); 71 72 testDump("", Collections.singletonMap(settingName, "infinity"), true); 73 testDump("", Collections.singletonMap(settingName, "0 ns"), false); 74 testDump("", Collections.emptyMap(), false); 75 } 76 77 private static void testDump(String pathToGcRoots, Map<String, String> settings, boolean expectedChains) throws Exception { 78 try (Recording r = new Recording()) { 79 Map<String, String> p = new HashMap<>(settings); 80 p.put(EventNames.OldObjectSample + "#" + Enabled.NAME, "true"); 81 r.setName("dodo"); 82 r.setSettings(p); 83 r.setToDisk(true); 84 r.start(); 85 clearLeak(); 86 System.out.println("Recording id: " + r.getId()); 87 System.out.println("Settings: " + settings.toString()); 88 System.out.println("Command: JFR.dump " + pathToGcRoots); 89 System.out.println("Chains expected: " + expectedChains); 90 buildLeak(); 91 System.gc(); 92 System.gc(); 93 File recording = new File("TestJcmdDumpPathToGCRoots" + r.getId() + ".jfr"); 94 recording.delete(); 95 JcmdHelper.jcmd("JFR.dump", "name=dodo", pathToGcRoots, "filename=" + recording.getAbsolutePath()); 96 r.setSettings(Collections.emptyMap()); 97 List<RecordedEvent> events = RecordingFile.readAllEvents(recording.toPath()); 98 if (events.isEmpty()) { 99 throw new Exception("No events found in recoding"); 100 } 101 boolean chains = hasChains(events); 102 if (expectedChains && !chains) { 103 System.out.println(events); 104 throw new Exception("Expected chains but found none"); 105 } 106 if (!expectedChains && chains) { 107 System.out.println(events); 108 throw new Exception("Didn't expect chains but found some"); 109 } 110 } 111 } 112 113 private static void clearLeak() { 114 leak.clear(); 115 } 116 117 private static boolean hasChains(List<RecordedEvent> events) throws IOException { 118 for (RecordedEvent e : events) { 119 RecordedObject ro = e.getValue("object"); 120 if (ro.getValue("referrer") != null) { 121 return true; 122 } 123 } 124 return false; 125 } 126 127 private static void buildLeak() { 128 for (int i = 0; i < OBJECT_COUNT;i ++) { 129 leak.add(new Object[0]); 130 } 131 } 132 }