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