Path: blob/master/test/hotspot/jtreg/runtime/Metaspace/elastic/MetaspaceTestManyArenasManyThreads.java
41155 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2020 SAP SE. All rights reserved.3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*23*/24import java.util.concurrent.CyclicBarrier;2526import static java.lang.System.currentTimeMillis;2728public class MetaspaceTestManyArenasManyThreads extends MetaspaceTestWithThreads {2930// Several threads allocate from a single arena.31// This mimicks several threads loading classes via the same class loader.3233public MetaspaceTestManyArenasManyThreads(MetaspaceTestContext context, long testAllocationCeiling, int numThreads, int seconds) {34super(context, testAllocationCeiling, numThreads, seconds);35}3637public void runTest() throws Exception {3839long t_start = currentTimeMillis();40long t_stop = t_start + (seconds * 1000);4142CyclicBarrier gate = new CyclicBarrier(numThreads + 1);4344final long ceilingPerThread = testAllocationCeiling / numThreads;4546for (int i = 0; i < numThreads; i ++) {47// Create n test threads, each one with its own allocator/arena pair48MetaspaceTestArena arena = context.createArena(RandomHelper.fiftyfifty(), ceilingPerThread);49RandomAllocator allocator = new RandomAllocator(arena);50RandomAllocatorThread thread = new RandomAllocatorThread(gate, allocator, i);51threads[i] = thread;52thread.start();53}5455gate.await();5657// while test is running, skim the arenas and kill any arena which is saturated (has started getting an58// untoward number of allocation failures)59while (System.currentTimeMillis() < t_stop) {6061// Wait a bit62Thread.sleep(200);6364for (RandomAllocatorThread t: threads) {65if (t.allocator.arena.numAllocationFailures > 0) {66t.interrupt();67t.join();68context.destroyArena(t.allocator.arena);6970// Create a new arena, allocator, then a new thread (note: do not pass in a start gate this time71// since we do not need to wait) and fire it up.72MetaspaceTestArena arena = context.createArena(RandomHelper.fiftyfifty(), ceilingPerThread);73RandomAllocator allocator = new RandomAllocator(arena);74RandomAllocatorThread t2 = new RandomAllocatorThread(null, allocator, t.id);75threads[t.id] = t2;76t2.start();77}78}7980}8182// Stop all threads.83stopAllThreads();8485context.updateTotals();86System.out.println(" ## Finished: " + context);8788context.checkStatistics();8990// Destroy all arenas; then purge the space.91destroyArenasAndPurgeSpace();9293context.destroy();9495context.updateTotals();9697System.out.println("This took " + (System.currentTimeMillis() - t_start) + "ms");9899}100101}102103104105