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