Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/gc/g1/TestGreyReclaimedHumongousObjects.java
32284 views
/*1* Copyright (c) 2015, 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*/2223/*24* @test TestGreyReclaimedHumongousObjects.java25* @bug 806936726* @requires vm.gc == "G1" | vm.gc == "null"27* @summary Test handling of marked but unscanned reclaimed humongous objects.28* @key gc29* @run main/othervm -XX:+UseG1GC -Xss32m -Xmx128m -XX:G1HeapRegionSize=1m30* -XX:+UnlockExperimentalVMOptions31* -XX:+G1EagerReclaimHumongousObjects32* -XX:+G1EagerReclaimHumongousObjectsWithStaleRefs33* TestGreyReclaimedHumongousObjects 1048576 9034*/3536// This test spawns a bunch of threads, each of them rapidly37// allocating large objects and storing them into a circular buffer38// associated with the thread. The circular buffer results in these39// objects becoming dead in fairly short order.40//41// The situation we're trying to provoke is42//43// (1) A humongous object H is marked and added to the mark stack.44//45// (2) An evacuation pause determines H is no longer live, and46// reclaims it. This occurs before concurrent marking has gotten47// around to processing the mark stack entry for H.48//49// (3) Concurrent marking processes the mark stack entry for H. The50// bug is that it would attempt to scan the now dead object.51//52// Unfortunately, this test is *very* sensitive to configuration.53// Among the parameters that affect whether / how often we'll get into54// the desired situation within a reasonable amount of time are:55//56// - THREAD_COUNT: The number of allocating threads.57//58// - OLD_COUNT: The number of objects each thread keeps.59//60// - MAX_MEMORY: The maximum heap size.61//62// - G1HeapRegionSize63//64// - The size of the objects being allocated.65//66// The parameter values specified here:67//68// - THREAD_COUNT = 1269// - OLD_COUNT == 470// - MAX_MEMORY == 128m71// - G1HeapRegionSize = 1m72// - Object size = 1048576 (2 regions after header overhead and roundup)73//74// seems to work well at provoking the desired state fairly quickly.75// Even relatively small perturbations may change that. The key76// factors seem to be keeping the heap mostly full of live objects but77// having them become dead fairly quickly.7879import java.util.Date;80import java.util.concurrent.ExecutorService;81import java.util.concurrent.Executors;82import java.util.concurrent.ThreadFactory;83import java.util.concurrent.TimeUnit;84import sun.management.ManagementFactoryHelper;85import com.sun.management.HotSpotDiagnosticMXBean;86import com.sun.management.VMOption;8788public class TestGreyReclaimedHumongousObjects {8990static class NamedThreadFactory implements ThreadFactory {91private int threadNum = 0;9293@Override94public Thread newThread(Runnable r) {95return new Thread(r, THREAD_NAME + (threadNum++));96}97}9899static class Runner extends Thread {100private final Date startDate = new Date();101private final int obj_size;102private final Object[] old_garbage;103private int old_index = 0;104105public Runner(int obj_size) {106this.obj_size = obj_size;107old_garbage = new Object[OLD_COUNT];108}109110private void allocate_garbage() {111byte[] garbage = new byte[obj_size];112old_garbage[Math.abs(++old_index % OLD_COUNT)] = garbage;113}114115@Override116public void run() {117try {118while (!isInterrupted()) {119allocate_garbage();120Thread.sleep(0); // Yield, to ensure interruptable.121}122} catch (InterruptedException e) {123System.out.println("Aborted after "124+ (new Date().getTime() - startDate.getTime())125+ " ms");126interrupt();127}128}129}130131public static void main(String[] args) throws Exception {132HotSpotDiagnosticMXBean diagnostic = ManagementFactoryHelper.getDiagnosticMXBean();133134System.out.println("Max memory= " + MAX_MEMORY + " bytes");135136int obj_size = 0;137long seconds_to_run = 0;138if (args.length != 2) {139throw new RuntimeException("Object size argument must be supplied");140} else {141obj_size = Integer.parseInt(args[0]);142seconds_to_run = Integer.parseInt(args[1]);143}144System.out.println("Objects size= " + obj_size + " bytes");145System.out.println("Seconds to run=" + seconds_to_run);146147int region_size =148Integer.parseInt(diagnostic.getVMOption("G1HeapRegionSize").getValue());149if (obj_size < (region_size / 2)) {150throw new RuntimeException("Object size " + obj_size +151" is not humongous with region size " + region_size);152}153154ExecutorService executor =155Executors.newFixedThreadPool(THREAD_COUNT, new NamedThreadFactory());156System.out.println("Starting " + THREAD_COUNT + " threads");157158for (int i = 0; i < THREAD_COUNT; i++) {159executor.execute(new Runner(obj_size));160}161162Thread.sleep(seconds_to_run * 1000);163executor.shutdownNow();164165if (!executor.awaitTermination(10, TimeUnit.SECONDS)) {166System.err.println("Thread pool did not terminate after 10 seconds after shutdown");167}168}169170private static final long MAX_MEMORY = Runtime.getRuntime().maxMemory();171private static final int OLD_COUNT = 4;172private static final int THREAD_COUNT = 12;173private static final String THREAD_NAME = "TestGreyRH-";174}175176177178