1 /* 2 * Copyright (c) 2011, 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. 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 26 * @key stress gc 27 * 28 * @summary converted from VM Testbase gc/gctests/OneeFinalizerTest. 29 * VM Testbase keywords: [gc, stress, stressopt, nonconcurrent, jrockit] 30 * VM Testbase readme: 31 * DESCRIPTION 32 * Regression test that verifies that only one finalizer gets called. 33 * 34 * COMMENTS 35 * This test was ported from JRockit test suite. 36 * 37 * @library /vmTestbase 38 * /test/lib 39 * @run driver jdk.test.lib.FileInstaller . . 40 * @run main/othervm 41 * -XX:-UseGCOverheadLimit 42 * -Xlog:gc:gc.log 43 * gc.gctests.OneeFinalizerTest.OneeFinalizerTest 44 */ 45 46 package gc.gctests.OneeFinalizerTest; 47 48 import nsk.share.TestFailure; 49 import nsk.share.gc.GC; 50 import nsk.share.gc.GCTestBase; 51 import nsk.share.gc.gp.GarbageUtils; 52 import nsk.share.test.Stresser; 53 54 /** 55 * Test that verifies that finalize() method is invoking only once. 56 */ 57 public class OneeFinalizerTest extends GCTestBase { 58 59 private GlobalSafeCounter[] finalizerCounters = null; 60 61 /** 62 * Helper class used for counting number of calls to finalizers. 63 */ 64 protected class GlobalSafeCounter { 65 66 private int counter; 67 68 /** 69 * Constructor that inits the global counter to 0. 70 */ 71 protected GlobalSafeCounter() { 72 counter = 0; 73 } 74 75 /** 76 * Reset the global counter to 0. 77 */ 78 protected final void resetCounter() { 79 synchronized (this) { 80 counter = 0; 81 } 82 } 83 84 /** 85 * Increase the global counter by 1. 86 */ 87 protected final void increaseCounter() { 88 synchronized (this) { 89 counter++; 90 } 91 } 92 93 /** 94 * Retrieve the global counter value. 95 * 96 * @return value of the global counter 97 */ 98 protected final int getCounterValue() { 99 int value; 100 101 synchronized (this) { 102 value = counter; 103 } 104 105 return value; 106 } 107 } 108 109 /** 110 * Helper class the implements finalize(), and that increments 111 * the global counters for each finalize() invokation. 112 */ 113 protected class FinalizedObject { 114 115 private final int counterIndex; 116 117 /** 118 * Constructor for the helper object which implements finalize(). 119 * 120 * @param index Index for the counter in the global array, that 121 * corresponds to this object. 122 */ 123 protected FinalizedObject(int index) { 124 counterIndex = index; 125 } 126 127 /** 128 * Increases the global counter for this object when finalize() 129 * gets called (to make sure each finalizer gets called onee). 130 */ 131 @Override 132 protected final void finalize() { 133 finalizerCounters[counterIndex].increaseCounter(); 134 } 135 } 136 137 private void initOneeFinalizerTest(int numberOfObjects) { 138 // NOTE: Set to null in case it's been used before (to prevent OOM) 139 finalizerCounters = null; 140 finalizerCounters = new GlobalSafeCounter[numberOfObjects]; 141 142 for (int i = 0; i < numberOfObjects; i++) { 143 finalizerCounters[i] = new GlobalSafeCounter(); 144 } 145 } 146 147 /** 148 * Tests that the finalize() method on each FinalizedObject instance 149 * has been called exactly one time. 150 */ 151 @Override 152 public void run() { 153 154 155 int numberOfObjects = 2000; 156 157 initOneeFinalizerTest(numberOfObjects); 158 159 FinalizedObject[] testObjects = new FinalizedObject[numberOfObjects]; 160 161 // creates garbage 162 for (int i = 0; i < numberOfObjects; i++) { 163 testObjects[i] = new FinalizedObject(i); 164 } 165 166 if (testObjects[0].hashCode() == 212_85_06) { 167 System.out.println("Bingo!!!"); 168 } 169 170 testObjects = null; 171 172 Stresser stresser = new Stresser(runParams.getStressOptions()); 173 stresser.start(0); 174 /* force finalization */ 175 GarbageUtils.eatMemory(stresser); 176 if (!stresser.continueExecution()) { 177 // may be we didn't eat all memory and didn't provoke GC 178 System.out.println("Passed without check"); 179 return; 180 } 181 System.gc(); 182 System.runFinalization(); 183 System.gc(); 184 System.runFinalization(); 185 System.gc(); 186 187 int numberOfFinalizersRunMoreThanOnce = 0; 188 int numberOfFinalizersNotRun = 0; 189 190 for (int i = 0; i < numberOfObjects; i++) { 191 int counter = finalizerCounters[i].getCounterValue(); 192 if (counter > 1) { 193 numberOfFinalizersRunMoreThanOnce++; 194 System.err.println("Object #" + i + " counter = " + counter); 195 } else if (counter == 0) { 196 System.err.println("WARNING: Finalizer not run for object #" + i); 197 numberOfFinalizersNotRun++; 198 } 199 } 200 201 if (numberOfFinalizersNotRun > 0) { 202 System.err.println("WARNING: " + numberOfFinalizersNotRun + " finalizers not run"); 203 } 204 205 if (numberOfFinalizersRunMoreThanOnce != 0) { 206 throw new TestFailure("OneeFinalizerTest failed. " + numberOfFinalizersRunMoreThanOnce + " errors"); 207 } 208 System.out.println("Test passed."); 209 } 210 211 public static void main(String[] args) { 212 GC.runTest(new OneeFinalizerTest(), args); 213 } 214 }