1 /*
2  * Copyright (c) 2018, 2020, 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.event.gc.collection;
27 
28 import static java.lang.System.gc;
29 import static java.lang.Thread.sleep;
30 import static java.util.Set.of;
31 import static java.util.stream.Collectors.joining;
32 import static java.util.stream.Collectors.toList;
33 import static java.util.stream.Collectors.toSet;
34 import static java.util.stream.IntStream.range;
35 import static jdk.test.lib.Asserts.assertEquals;
36 import static jdk.test.lib.Asserts.assertTrue;
37 import static jdk.test.lib.jfr.Events.fromRecording;
38 import static sun.hotspot.WhiteBox.getWhiteBox;
39 
40 import java.io.IOException;
41 import java.lang.ref.WeakReference;
42 import java.math.BigDecimal;
43 import java.util.ArrayList;
44 import java.util.Collection;
45 import java.util.List;
46 import java.util.Set;
47 
48 import gc.testlibrary.g1.MixedGCProvoker;
49 import jdk.jfr.Recording;
50 import jdk.test.lib.Asserts;
51 import jdk.test.lib.jfr.EventNames;
52 import sun.hotspot.WhiteBox;
53 
54 /**
55  * @test
56  * @key jfr
57  * @requires vm.hasJFR
58  * @requires vm.gc == "G1" | vm.gc == null
59  * @library /test/lib /test/jdk /test/hotspot/jtreg
60  * @build sun.hotspot.WhiteBox
61  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
62  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+AlwaysTenure
63  *      -Xms20M -Xmx20M -Xlog:gc=debug,gc+heap*=debug,gc+ergo*=debug,gc+start=debug
64  *      -XX:G1MixedGCLiveThresholdPercent=100 -XX:G1HeapWastePercent=0 -XX:G1HeapRegionSize=1m
65  *      -XX:+UseG1GC -XX:+UseStringDeduplication
66  *      -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
67  *      jdk.jfr.event.gc.collection.TestG1ParallelPhases
68  */
69 
70 public class TestG1ParallelPhases {
71     public static List<WeakReference<byte[]>> weakRefs;
72 
main(String[] args)73     public static void main(String[] args) throws IOException {
74         Recording recording = new Recording();
75         recording.enable(EventNames.GCPhaseParallel);
76         recording.start();
77 
78         // create more weak garbage than can fit in this heap (-Xmx20m), will force collection of weak references
79         weakRefs = range(1, 30)
80             .mapToObj(n -> new WeakReference<>(new byte[1_000_000]))
81             .collect(toList()); // force evaluation of lazy stream (all weak refs must be created)
82 
83         final var MEG = 1024 * 1024;
84         MixedGCProvoker.allocateAndProvokeMixedGC(MEG);
85         recording.stop();
86 
87         Set<String> usedPhases = fromRecording(recording).stream()
88             .map(e -> e.getValue("name").toString())
89             .collect(toSet());
90 
91         Set<String> allPhases = of(
92             "ExtRootScan",
93             "ThreadRoots",
94             "UniverseRoots",
95             "JNIRoots",
96             "ObjectSynchronizerRoots",
97             "ManagementRoots",
98             "VMGlobalRoots",
99             "CLDGRoots",
100             "JVMTIRoots",
101             "CMRefRoots",
102             "MergeER",
103             "MergeHCC",
104             "MergeRS",
105             "MergeLB",
106             "ScanHR",
107             "CodeRoots",
108             "ObjCopy",
109             "Termination",
110             "StringDedupQueueFixup",
111             "StringDedupTableFixup",
112             "RedirtyCards",
113             "ParFreeCSet",
114             "NonYoungFreeCSet",
115             "YoungFreeCSet",
116             "RebuildFreeList"
117         );
118 
119         // Some GC phases may or may not occur depending on environment. Filter them out
120         // since we can not reliably guarantee that they occur (or not).
121         Set<String> optPhases = of(
122             "OptScanHR",
123             "OptMergeRS",
124             "OptCodeRoots",
125             "OptObjCopy"
126         );
127         usedPhases.removeAll(optPhases);
128 
129         assertTrue(usedPhases.equals(allPhases), "Compare events expected and received"
130             + ", Not found phases: " + allPhases.stream().filter(p -> !usedPhases.contains(p)).collect(joining(", "))
131             + ", Not expected phases: " + usedPhases.stream().filter(p -> !allPhases.contains(p)).collect(joining(", ")));
132     }
133 }
134