Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/monitoring/ThreadMXBean/ThreadMXBeanTestBase.java
40948 views
/*1* Copyright (c) 2011, 2020, 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*/2223package nsk.monitoring.ThreadMXBean;2425import java.util.*;26import nsk.share.test.*;27import nsk.monitoring.share.*;28import nsk.share.gc.Memory;2930public abstract class ThreadMXBeanTestBase extends MonitoringTestBase31implements Initializable {3233/**34* Maximum allocation object size. about 2 Mb35*/36private static final int MAX_OBJECT_SIZE = 2*1024*1024;37/**38* Allocations count for each MXBeanTestThread39*/40private static final int ALLOCATIONS = 100;41/**42* Minimum size of allocated objects for a single thread stressing GC: 1Mb43*/44protected static final int MIN_STRESS_ALLOCATION_AMOUNT = 1024*1024;45/**46* Percent of maximum difference of actual result from expected one47*/48protected static final int DELTA_PERCENT = 15;49/**50* Instance of com.sun.management.ThreadMXBean used by all tests51* Obtained in initialize() method52*/53protected com.sun.management.ThreadMXBean threadMXBean;54/**55* Stresser class instance used in StressTest56*/57protected Stresser stresser;58/**59* String indicating the GarbageProducer type used in test60*/61protected String garbageProducerId;62/**63* Defined array with allocation objects sizes64*/65protected static int[] allocArr = new int[ALLOCATIONS];6667/**68* Obtains instance of com.sun.management.ThreadMXBean69* and stores it in threadMXBean field70* If com.sun.management.ThreadMXBean API is not available,71* threadMXBean is set to null and appropriate message is72* prompted73*/74public void initialize() {75if (monitoringFactory.hasThreadMXBeanNew()) {76threadMXBean =77(com.sun.management.ThreadMXBean) monitoringFactory.getThreadMXBeanNew();78// ensure LocalRandom is loaded and has enough memory79LocalRandom.init();80for (int i = 0; i < ALLOCATIONS; i++) {81allocArr[i] = Memory.getArrayExtraSize() + Memory.getIntSize()82+ Memory.getReferenceSize() // don't be zero-length83+ LocalRandom.nextInt(MAX_OBJECT_SIZE);84}85} else {86log.info("com.sun.management.ThreadMXBean API is not available!");87}88}8990/**91* Obtains instance of Stresser and stores it in stresser field92* @param args Stresser arguments93*/94public void setStresser(String[] args) {95if (stresser == null)96stresser = new Stresser(args);97}9899/**100* Parses input String arrays searching for GarbageProducer101* settings. If found, sets garbageProducerID filed.102* @param args input arguments103* @return input arguments without GarbageProducer options104*/105public String[] setGarbageProducer(String[] args) {106if (garbageProducerId == null) {107ArrayList<String> list = new ArrayList<String>();108for (int i = 0; i < args.length; i++) {109if (args[i].equals("-gp")) {110garbageProducerId = args[++i];111} else {112list.add(args[i]);113}114}115return list.toArray(new String[] {});116} else {117return args;118}119}120121/**122* Starts all specified TestThread threads123* @param threads threads to start124*/125public BarrierHandler startThreads(MXBeanTestThread... threads) {126BarrierHandler handler = new BarrierHandler(threads);127for (MXBeanTestThread thread : threads) {128thread.setHandler(handler);129thread.start();130}131handler.start();132return handler;133}134135/**136* Returns expected memory size allocated by MXBeanTestThreads during137* stress test (allocateStress() method execution)138*139* @param array of MXBeanTestThreads140* @return expected amount of memory allocated by each StressTestThread141*/142public long[] getStressAllocatedBytes(MXBeanTestThread... threads) {143long[] result = new long[threads.length];144int counter = 0;145for (MXBeanTestThread thread : threads) {146result[counter++] = thread.getStressAllocatedBytes();147}148return result;149}150}151152153