1 /* 2 * Copyright (c) 2007, 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. 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 /* 26 * @test 27 * @key randomness 28 * 29 * @summary converted from VM Testbase nsk/monitoring/GarbageCollectorMXBean/CollectionCounters/CollectionCounters001. 30 * VM Testbase keywords: [monitoring] 31 * 32 * @library /vmTestbase 33 * /test/lib 34 * @run main/othervm 35 * nsk.monitoring.GarbageCollectorMXBean.CollectionCounters.CollectionCounters001.CollectionCounters001 36 * -testMode=directly 37 */ 38 39 package nsk.monitoring.GarbageCollectorMXBean.CollectionCounters.CollectionCounters001; 40 41 import java.util.List; 42 import java.lang.management.*; 43 import nsk.share.TestFailure; 44 import nsk.share.test.*; 45 import nsk.monitoring.share.*; 46 import nsk.share.gc.Algorithms; 47 import nsk.share.runner.RunParams; 48 import nsk.share.runner.RunParamsAware; 49 50 /** 51 * Test counters from GarbageCollectorMXBean. 52 * 53 * In this test, we do some operations on heap and check that total 54 * counters from all GarbageCollectorBeans do not decrease and actually 55 * increase in some situations. 56 * 57 * This also checks that counters increase after MemoryMXBean.gc(). 58 * 59 * Note: we assume that System.gc() increases collection count and 60 * time. It may be false with -XX:+DisableExplicitGC. 61 */ 62 public class CollectionCounters001 extends MonitoringTestBase implements RunParamsAware, Initializable { 63 private List<GarbageCollectorMXBean> gcBeans; 64 private MemoryMXBean memory; 65 Stresser stresser; 66 RunParams runParams; 67 private long collectionCount; 68 private long collectionTime; 69 private long collectionCountOld; 70 private long collectionTimeOld; 71 initialize()72 public void initialize() { 73 gcBeans = monitoringFactory.getGarbageCollectorMXBeans(); 74 memory = monitoringFactory.getMemoryMXBean(); 75 } 76 runOne(ExecutionController stresser)77 private void runOne(ExecutionController stresser) { 78 updateCounters(); 79 validate(); 80 Algorithms.eatMemory(stresser); 81 if(stresser.continueExecution()) { 82 updateCounters(); 83 validateNonTrivial(); 84 System.gc(); 85 updateCounters(); 86 validateNonTrivial(); 87 memory.gc(); 88 updateCounters(); 89 validateNonTrivial(); 90 } 91 } 92 run()93 public void run() { 94 stresser = new Stresser(runParams.getStressOptions()); 95 stresser.start(runParams.getIterations()); 96 while (stresser.iteration()) { 97 runOne(stresser); 98 } 99 } 100 validate()101 private void validate() { 102 if (collectionCount < 0) 103 throw new TestFailure("collectionCount negative: " + collectionCount); 104 if (collectionTime < 0) 105 throw new TestFailure("collectionTime negative: " + collectionTime); 106 if (collectionCount < collectionCountOld) 107 throw new TestFailure("collectionCount decreased: " + collectionCount + " -> " + collectionCountOld); 108 if (collectionTime < collectionTimeOld) 109 throw new TestFailure("collectionTime decreased: " + collectionTime + " -> " + collectionTimeOld); 110 } 111 validateNonTrivial()112 private void validateNonTrivial() { 113 if (collectionCount < 0) 114 throw new TestFailure("collectionCount negative: " + collectionCount); 115 if (collectionTime < 0) 116 throw new TestFailure("collectionTime negative: " + collectionTime); 117 if (collectionCount <= collectionCountOld) 118 throw new TestFailure("collectionCount not increased: " + collectionCount + " -> " + collectionCountOld); 119 if (collectionTime < collectionTimeOld) 120 throw new TestFailure("collection time became smaller: " + collectionTime + " -> " + collectionTimeOld); 121 } 122 updateCounters()123 private void updateCounters() { 124 collectionCountOld = collectionCount; 125 collectionTimeOld = collectionTime; 126 collectionCount = 0; 127 collectionTime = 0; 128 for (GarbageCollectorMXBean gcBean : gcBeans) { 129 collectionCount += gcBean.getCollectionCount(); 130 collectionTime += gcBean.getCollectionTime(); 131 } 132 } 133 main(String[] args)134 public static void main(String[] args) { 135 Monitoring.runTest(new CollectionCounters001(), args); 136 } 137 138 @Override setRunParams(RunParams runParams)139 public void setRunParams(RunParams runParams) { 140 this.runParams = runParams; 141 } 142 } 143