Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/monitoring/ThreadMXBean/MXBeanTestThread.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.ArrayList;26import java.util.List;27import nsk.share.gc.gp.GarbageProducer;28import nsk.share.gc.gp.GarbageUtils;29import nsk.share.test.Stresser;30import nsk.share.test.LocalRandom;313233public class MXBeanTestThread extends Thread {3435/**36* BarrierHandler instance for synchronization with management thread37*/38protected BarrierHandler handler;3940/**41* List where allocated objects are stored42*/43private List<Object> allocatedList;44/**45* Number of simultaneously running threads46*/47private static int threadCount;48/**49* Expected amount of memory allocated during stress test50*/51private long stressAllocatedBytes;52/**53* Maximum memory in bytes that one thread is allowed to use at once54*/55private long maxThreadMemory = 0;56/**57* GarbageProducer for objects creation58*/59private GarbageProducer gp;60/**61* Stresser instance for allocateStress()62*/63private Stresser stresser;6465public static void warmUp(String garbageProducerId) {66MXBeanTestThread warmUpThread = new MXBeanTestThread(garbageProducerId) {67@Override68public void doWork() {69allocate();70}71};72warmUpThread.start();73do {74try {75warmUpThread.join();76} catch (InterruptedException ie) {}77} while(warmUpThread.isAlive());78}7980/**81* Sets BarrierHandler for this thread82*83* @param handler BarrierHandler to synchronize with84*/85public void setHandler(BarrierHandler handler) {86this.handler = handler;87}8889/**90* Returns an instance of MXBeanTestThread with already defined91* allocatedList List and GarbageProducer92*/93public MXBeanTestThread(String garbageProducerId) {94super(Integer.toString(++threadCount));95allocatedList = new ArrayList<Object>();96gp = GarbageUtils.getGarbageProducer(garbageProducerId);97maxThreadMemory = Runtime.getRuntime().maxMemory()/4;98}99100/**101* Returns an instance of MXBeanTestThread with already defined102* allocatedList List and default GarbageProducer103*/104public MXBeanTestThread() {105this("intArr");106}107108/**109* Returns an instance of MXBeanTestThread with already defined110* allocatedList List and Stresser111*/112public MXBeanTestThread(Stresser stresser) {113this("intArr");114this.stresser = stresser;115}116117/**118* Sets maximum amount of memory that could be used at once for each119* TestThread in StressTest120*/121public void setMaxThreadMemory (long memory) {122maxThreadMemory = memory;123}124125/**126* Returns expected memory allocated by TestThread during StressTest127* @return expected memory amount128*/129public long getStressAllocatedBytes() {130return stressAllocatedBytes;131}132133@Override134public void run() {135doWork();136}137138/**139* Implementation of TestThread behavior logic140*/141public void doWork() {142handler.ready();143allocate();144handler.ready();145}146147/**148* Allocates memory for amount of time specified in Stresser instance149*/150protected void allocateStress() {151// Size of long[] array that allocates 2 Mb + 1 byte152int MAX_ARR_SIZE=262146;153// Anount of memory allocated by thread with existing links154// Which means that these objects can't be collected by GC155long actuallyAllocated = 0;156// ensure LocalRandom is loaded and has enough memory157LocalRandom.init();158try {159while (stresser.continueExecution()) {160//int chunkSize = LocalRandom.nextInt(MAX_OBJECT_SIZE);161//Object obj = gp.create(chunkSize);162int chunkSize = LocalRandom.nextInt(MAX_ARR_SIZE);163Object obj = new long[chunkSize];164allocatedList.add(obj);165actuallyAllocated += chunkSize*8;166if (actuallyAllocated > maxThreadMemory) {167// Allocated more then allowed to one thread168// re-define allocatedList to allow GC to delete169// created objects170stressAllocatedBytes += actuallyAllocated;171actuallyAllocated = 0;172allocatedList = new ArrayList<Object>();173}174}175} catch (OutOfMemoryError e) {176} finally {177stressAllocatedBytes += actuallyAllocated;178}179}180181/**182* Allocates memory once according test settings183*/184protected void allocate() {185long actuallyAllocated = 0;186for (int i = 0; i < ThreadMXBeanTestBase.allocArr.length; i++) {187long size = ThreadMXBeanTestBase.allocArr[i];188if (actuallyAllocated + size > maxThreadMemory) {189break;190}191allocatedList.add(gp.create(size));192actuallyAllocated += size;193}194}195}196197198