1 /* 2 * Copyright (c) 2015, 2017, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test TestPrintReferences 26 * @bug 8136991 27 * @summary Validate the reference processing logging 28 * @key gc 29 * @library /test/lib 30 * @modules java.base/jdk.internal.misc 31 * java.management 32 */ 33 34 import java.lang.ref.SoftReference; 35 import java.util.ArrayList; 36 37 import jdk.test.lib.process.OutputAnalyzer; 38 import jdk.test.lib.process.ProcessTools; 39 40 public class TestPrintReferences { 41 public static void main(String[] args) throws Exception { 42 ProcessBuilder pb_enabled = ProcessTools.createJavaProcessBuilder("-Xlog:gc+phases+ref=debug", 43 "-XX:+UseG1GC", 44 "-Xmx10M", 45 GCTest.class.getName()); 46 OutputAnalyzer output = new OutputAnalyzer(pb_enabled.start()); 47 48 String indent_4 = " "; 49 String indent_6 = " "; 50 String indent_8 = " "; 51 String gcLogTimeRegex = ".* GC\\([0-9]+\\) "; 52 String countRegex = "[0-9]+"; 53 String timeRegex = "[0-9]+[.,][0-9]+ms"; 54 String totalRegex = gcLogTimeRegex + indent_4 + "Reference Processing: " + timeRegex + "\n"; 55 String balanceRegex = gcLogTimeRegex + indent_8 + "Balance queues: " + timeRegex + "\n"; 56 String softRefRegex = gcLogTimeRegex + indent_6 + "SoftReference: " + timeRegex + "\n"; 57 String weakRefRegex = gcLogTimeRegex + indent_6 + "WeakReference: " + timeRegex + "\n"; 58 String finalRefRegex = gcLogTimeRegex + indent_6 + "FinalReference: " + timeRegex + "\n"; 59 String phantomRefRegex = gcLogTimeRegex + indent_6 + "PhantomReference: " + timeRegex + "\n"; 60 String refDetailRegex = gcLogTimeRegex + indent_8 + "Phase2: " + timeRegex + "\n" + 61 gcLogTimeRegex + indent_8 + "Phase3: " + timeRegex + "\n" + 62 gcLogTimeRegex + indent_8 + "Discovered: " + countRegex + "\n" + 63 gcLogTimeRegex + indent_8 + "Cleared: " + countRegex + "\n"; 64 String softRefDetailRegex = gcLogTimeRegex + indent_8 + "Phase1: " + timeRegex + "\n" + refDetailRegex; 65 String enqueueRegex = gcLogTimeRegex + indent_4 + "Reference Enqueuing: " + timeRegex + "\n"; 66 String enqueueDetailRegex = gcLogTimeRegex + indent_6 + "Reference Counts: Soft: " + countRegex + 67 " Weak: " + countRegex + " Final: " + countRegex + " Phantom: " + countRegex + "\n"; 68 69 output.shouldMatch(/* Total Reference processing time */ 70 totalRegex + 71 /* SoftReference processing */ 72 softRefRegex + balanceRegex + softRefDetailRegex + 73 /* WeakReference processing */ 74 weakRefRegex + balanceRegex + refDetailRegex + 75 /* FinalReference processing */ 76 finalRefRegex + balanceRegex + refDetailRegex + 77 /* PhantomReference processing */ 78 phantomRefRegex + balanceRegex + refDetailRegex + 79 /* Total Enqueuing time */ 80 enqueueRegex + 81 /* Enqueued Stats */ 82 enqueueDetailRegex 83 ); 84 85 output.shouldHaveExitValue(0); 86 } 87 88 static class GCTest { 89 static final int M = 1024 * 1024; 90 91 public static void main(String [] args) { 92 93 ArrayList arrSoftRefs = new ArrayList(); 94 95 // Populate to triger GC and then Reference related logs will be printed. 96 for (int i = 0; i < 10; i++) { 97 byte[] tmp = new byte[M]; 98 99 arrSoftRefs.add(new SoftReference(tmp)); 100 } 101 } 102 } 103 }