Path: blob/master/test/hotspot/jtreg/serviceability/tmtools/jstat/GarbageProducerTest.java
41153 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.lang.management.ManagementFactory;24import utils.GarbageProducer;25import common.TmTool;26import utils.JstatResults;2728/**29* Base class for jstat testing which uses GarbageProducer to allocate garbage.30*/31public class GarbageProducerTest {3233// Iterations of measurement to get consistent value of counters and jstat.34private final static int ITERATIONS = 10;35private final static float TARGET_MEMORY_USAGE = 0.7f;36private final static float MEASUREMENT_TOLERANCE = 0.05f;37private final GarbageProducer garbageProducer;38private final TmTool<? extends JstatResults> jstatTool;3940public GarbageProducerTest(TmTool<? extends JstatResults> tool) {41garbageProducer = new GarbageProducer(TARGET_MEMORY_USAGE);42// We will be running jstat tool43jstatTool = tool;44}4546public void run() throws Exception {47// Run once and get the results asserting that they are reasonable48JstatResults measurement1 = jstatTool.measure();49measurement1.assertConsistency();50// Eat metaspace and heap then run the tool again and get the results asserting that they are reasonable51System.gc();52garbageProducer.allocateMetaspaceAndHeap();53// Collect garbage. Also update VM statistics54System.gc();55int i = 0;56long collectionCountBefore = getCollectionCount();57JstatResults measurement2 = jstatTool.measure();58do {59System.out.println("Measurement #" + i);60long currentCounter = getCollectionCount();61// Check if GC cycle occured during measurement62if (currentCounter == collectionCountBefore) {63measurement2.assertConsistency();64checkOldGenMeasurement(measurement2);65return;66} else {67System.out.println("GC happened during measurement.");68}69collectionCountBefore = getCollectionCount();70measurement2 = jstatTool.measure();7172} while (i++ < ITERATIONS);73// Checking will be performed without consistency guarantee.74checkOldGenMeasurement(measurement2);75}7677private void checkOldGenMeasurement(JstatResults measurement2) {78float oldGenAllocationRatio = garbageProducer.getOldGenAllocationRatio() - MEASUREMENT_TOLERANCE;79// Assert that space has been utilized accordingly80JstatResults.assertSpaceUtilization(measurement2, TARGET_MEMORY_USAGE, oldGenAllocationRatio);81}8283private static long getCollectionCount() {84return ManagementFactory.getGarbageCollectorMXBeans().stream()85.mapToLong(b -> b.getCollectionCount())86.sum();87}88}899091