Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/gc/containers/ContainersTest.java
41155 views
/*1* Copyright (c) 2010, 2018, 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*/22package vm.gc.containers;2324import java.lang.management.ManagementFactory;25import java.lang.management.MemoryUsage;26import java.util.*;27import nsk.share.TestBug;28import nsk.share.gc.*;29import nsk.share.gc.gp.GarbageProducer;30import nsk.share.gc.gp.GarbageProducerAware;31import nsk.share.gc.gp.MemoryStrategy;32import nsk.share.gc.gp.MemoryStrategyAware;33import nsk.share.test.ExecutionController;3435/*36* The main class and launcher for all containers tests.37* Allow to set containers via set -ct parameters. See parseFromString decription.38*/39public class ContainersTest extends ThreadedGCTest implements MemoryStrategyAware, GarbageProducerAware {4041private GarbageProducer garbageProducer;42private MemoryStrategy memoryStrategy;43MemoryUsage mbean = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();4445public ContainersTest(String[] args) {46for (int i = 0; i < args.length; i++) {47if (args[i].equals("-ct")) {48descrs.add(ContainerDescription.parseFromString(args[i + 1]));49}50}51if (descrs.size() == 0) {52throw new TestBug("No containers were given");53}54}5556@Override57public void setMemoryStrategy(MemoryStrategy memoryStrategy) {58this.memoryStrategy = memoryStrategy;59}6061@Override62public void setGarbageProducer(GarbageProducer gp) {63this.garbageProducer = gp;64}6566private class Mutator implements Runnable {6768Container container;6970public Mutator(Container container) {71this.container = container;72}7374@Override75public void run() {76ExecutionController stresser = getExecutionController();77container.setExecutionController(stresser);78try {79container.initialize();80while (stresser.continueExecution()) {81container.update();82}83} catch (OutOfMemoryError oome) {84// The OOME is teoretically possible in the case85// if data structure has a large overhead86}87}88}89List<ContainerDescription> descrs = new ArrayList<ContainerDescription>();90List<Container> containers = new ArrayList<Container>();9192@Override93protected Runnable createRunnable(int i) {94return new Mutator(containers.get(i));95}9697public Container createContainter(long maxMemory, ContainerDescription descr) {98String[] prefixes = {"java.util", "java.util.concurrent", "vm.gc.containers"};99String name = descr.getName();100Class clz = null;101for (String prefix : prefixes) {102try {103clz = Class.forName(prefix + "." + name);104} catch (ClassNotFoundException ex) {105// no such class, contiue106}107}108Object container;109try {110container = clz.newInstance();111} catch (Exception ex) {112throw new TestBug("Not able to create a container", ex);113}114// Deques are also collections...115if (container instanceof Map) {116return new MapContainer((Map) container, maxMemory,117descr.getGarbageProducer() != null ? descr.getGarbageProducer() : garbageProducer,118descr.getMemoryStrategy() != null ? descr.getMemoryStrategy() : memoryStrategy,119descr.getSpeed());120} else if (container instanceof Deque) {121return new DequeueContainer((Deque) container, maxMemory,122descr.getGarbageProducer() != null ? descr.getGarbageProducer() : garbageProducer,123descr.getMemoryStrategy() != null ? descr.getMemoryStrategy() : memoryStrategy,124descr.getSpeed(), runParams.getNumberOfThreads());125} else if (container instanceof Collection) {126return new CollectionContainer((Collection) container, maxMemory,127descr.getGarbageProducer() != null ? descr.getGarbageProducer() : garbageProducer,128descr.getMemoryStrategy() != null ? descr.getMemoryStrategy() : memoryStrategy,129descr.getSpeed());130}131throw new TestBug("Not able to create a container");132}133134void createEmptyContainers(long maxMemory) {135containers = new ArrayList<Container>();136for (int i = 0; containers.size() < runParams.getNumberOfThreads(); i++) {137Container container = createContainter(maxMemory / runParams.getNumberOfThreads(),138descrs.get(i % descrs.size()));139// we share container between workers if threadsCount > 1140for (int cnt = descrs.get(i % descrs.size()).getThreadsCount();141cnt != 0 && containers.size() < runParams.getNumberOfThreads();142cnt--) {143containers.add(container);144}145}146}147148@Override149public void run() {150long maxMemory = runParams.getTestMemory();151createEmptyContainers(maxMemory);152super.run();153}154155public static void main(String args[]) {156GC.runTest(new ContainersTest(args), args);157}158}159160161