Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/InterruptGC/InterruptGC.java
41159 views
/*1* Copyright (c) 2002, 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*/222324/*25* @test26* @key stress randomness27*28* @summary converted from VM Testbase gc/gctests/InterruptGC.29* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent]30* VM Testbase readme:31* In this test, threads perform garbage collection while constantly32* interrupting each other. Another thread generates the garbage.33* The test runs for approximately one minute (see nsk.share.runner.ThreadsRunner34* and nsk.share.runner.RunParams). The test passes if no exceptions are generated.35*36* @library /vmTestbase37* /test/lib38* @run main/othervm gc.gctests.InterruptGC.InterruptGC -gp random(arrays) -ms low39*/4041package gc.gctests.InterruptGC;4243import nsk.share.gc.*;44import nsk.share.test.*;45import nsk.share.gc.gp.*;4647import java.util.*;4849/**50* The test starts one thread which generates garbage and several other51* thread which continuously do System.gc() and interrupt each other.52*/53public class InterruptGC extends ThreadedGCTest implements GarbageProducerAware, MemoryStrategyAware {54private GarbageProducer garbageProducer;55private MemoryStrategy memoryStrategy;56private List<Interrupter> interrupters = new ArrayList<Interrupter>();57private int count;58private long size;5960private class GarbageCreator implements Runnable {61public void run() {62Object[] arr = new Object[count];63for (int i = 0; i < count && getExecutionController().continueExecution(); ++i)64arr[i] = garbageProducer.create(size);65}66}6768private class Interrupter implements Runnable {69private Thread thread;7071public void run() {72if (thread == null)73thread = Thread.currentThread();74Interrupter interrupter = interrupters.get(LocalRandom.nextInt(interrupters.size()));75Thread thread = interrupter.getThread();76if (thread != null)77thread.interrupt();78System.gc();79}8081public Thread getThread() {82return thread;83}84}8586protected Runnable createRunnable(int i) {87switch (i) {88case 0:89return new GarbageCreator();90default:91Interrupter interrupter = new Interrupter();92interrupters.add(interrupter);93return interrupter;94}95}9697public void run() {98size = GarbageUtils.getArraySize(runParams.getTestMemory(), memoryStrategy);99count = GarbageUtils.getArrayCount(runParams.getTestMemory(), memoryStrategy);100super.run();101}102103public void setGarbageProducer(GarbageProducer garbageProducer) {104this.garbageProducer = garbageProducer;105}106107public void setMemoryStrategy(MemoryStrategy memoryStrategy) {108this.memoryStrategy = memoryStrategy;109}110111public static void main(String[] args) {112GC.runTest(new InterruptGC(), args);113}114}115116117