Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/management/MemoryMXBean/LowMemoryTest.java
38821 views
/*1* Copyright (c) 2003, 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* @test25* @bug 453053826* @summary Basic unit test of memory management testing:27* 1) setUsageThreshold() and getUsageThreshold()28* 2) test low memory detection on the old generation.29*30* @author Mandy Chung31*32* @library /lib/testlibrary/33* @build LowMemoryTest MemoryUtil RunUtil34* @run main/timeout=600 LowMemoryTest35* @requires vm.opt.ExplicitGCInvokesConcurrent != "true"36* @requires vm.opt.ExplicitGCInvokesConcurrentAndUnloadsClasses != "true"37* @requires vm.opt.DisableExplicitGC != "true"38*/3940import java.lang.management.*;41import java.util.*;42import java.util.concurrent.Phaser;43import javax.management.*;44import javax.management.openmbean.CompositeData;4546public class LowMemoryTest {47private static final MemoryMXBean mm = ManagementFactory.getMemoryMXBean();48private static final List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();49private static final Phaser phaser = new Phaser(2);50private static MemoryPoolMXBean mpool = null;51private static boolean trace = false;52private static boolean testFailed = false;53private static final int NUM_TRIGGERS = 5;54private static final int NUM_CHUNKS = 2;55private static long chunkSize;5657/**58* Run the test multiple times with different GC versions.59* First with default command line specified by the framework.60* Then with GC versions specified by the test.61*/62public static void main(String a[]) throws Throwable {63final String main = "LowMemoryTest$TestMain";64RunUtil.runTestKeepGcOpts(main);65RunUtil.runTestClearGcOpts(main, "-XX:+UseSerialGC");66RunUtil.runTestClearGcOpts(main, "-XX:+UseParallelGC");67RunUtil.runTestClearGcOpts(main, "-XX:+UseG1GC");68RunUtil.runTestClearGcOpts(main, "-XX:+UseConcMarkSweepGC");69}7071private static volatile boolean listenerInvoked = false;72static class SensorListener implements NotificationListener {73@Override74public void handleNotification(Notification notif, Object handback) {75String type = notif.getType();76if (type.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED) ||77type.equals(MemoryNotificationInfo.78MEMORY_COLLECTION_THRESHOLD_EXCEEDED)) {7980MemoryNotificationInfo minfo = MemoryNotificationInfo.81from((CompositeData) notif.getUserData());8283MemoryUtil.printMemoryNotificationInfo(minfo, type);84listenerInvoked = true;85}86}87}8889static class TestListener implements NotificationListener {90private boolean isRelaxed = false;91private int triggers = 0;92private final long[] count = new long[NUM_TRIGGERS * 2];93private final long[] usedMemory = new long[NUM_TRIGGERS * 2];9495public TestListener() {96isRelaxed = ManagementFactory.getRuntimeMXBean().getInputArguments().contains("-XX:+UseConcMarkSweepGC");97}9899@Override100public void handleNotification(Notification notif, Object handback) {101MemoryNotificationInfo minfo = MemoryNotificationInfo.102from((CompositeData) notif.getUserData());103count[triggers] = minfo.getCount();104usedMemory[triggers] = minfo.getUsage().getUsed();105triggers++;106}107public void checkResult() throws Exception {108if ((!isRelaxed && triggers != NUM_TRIGGERS) ||109(isRelaxed && triggers < NUM_TRIGGERS)) {110throw new RuntimeException("Unexpected number of triggers = " +111triggers + " but expected to be " + NUM_TRIGGERS);112}113114for (int i = 0; i < triggers; i++) {115if (count[i] != i+1) {116throw new RuntimeException("Unexpected count of" +117" notification #" + i +118" count = " + count[i] +119" but expected to be " + (i+1));120}121if (usedMemory[i] < newThreshold) {122throw new RuntimeException("Used memory = " +123usedMemory[i] + " is less than the threshold = " +124newThreshold);125}126}127}128}129130private static long newThreshold;131132private static class TestMain {133public static void main(String args[]) throws Exception {134if (args.length > 0 && args[0].equals("trace")) {135trace = true;136}137138// Find the Old generation which supports low memory detection139ListIterator iter = pools.listIterator();140while (iter.hasNext()) {141MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();142if (p.getType() == MemoryType.HEAP &&143p.isUsageThresholdSupported()) {144mpool = p;145if (trace) {146System.out.println("Selected memory pool for low memory " +147"detection.");148MemoryUtil.printMemoryPool(mpool);149}150break;151}152}153154TestListener listener = new TestListener();155SensorListener l2 = new SensorListener();156NotificationEmitter emitter = (NotificationEmitter) mm;157emitter.addNotificationListener(listener, null, null);158emitter.addNotificationListener(l2, null, null);159160Thread allocator = new AllocatorThread();161Thread sweeper = new SweeperThread();162163// Now set threshold164MemoryUsage mu = mpool.getUsage();165chunkSize = (mu.getMax() - mu.getUsed()) / 20;166newThreshold = mu.getUsed() + (chunkSize * NUM_CHUNKS);167168System.out.println("Setting threshold for " + mpool.getName() +169" from " + mpool.getUsageThreshold() + " to " + newThreshold +170". Current used = " + mu.getUsed());171mpool.setUsageThreshold(newThreshold);172173if (mpool.getUsageThreshold() != newThreshold) {174throw new RuntimeException("TEST FAILED: " +175"Threshold for Memory pool " + mpool.getName() +176"is " + mpool.getUsageThreshold() + " but expected to be" +177newThreshold);178}179180181allocator.start();182// Force Allocator start first183phaser.arriveAndAwaitAdvance();184sweeper.start();185186187try {188allocator.join();189// Wait until AllocatorThread's done190phaser.arriveAndAwaitAdvance();191sweeper.join();192} catch (InterruptedException e) {193System.out.println("Unexpected exception:" + e);194testFailed = true;195}196197listener.checkResult();198199if (testFailed)200throw new RuntimeException("TEST FAILED.");201202System.out.println(RunUtil.successMessage);203204}205}206207private static void goSleep(long ms) {208try {209Thread.sleep(ms);210} catch (InterruptedException e) {211System.out.println("Unexpected exception:" + e);212testFailed = true;213}214}215216private static final List<Object> objectPool = new ArrayList<>();217static class AllocatorThread extends Thread {218public void doTask() {219int iterations = 0;220int numElements = (int) (chunkSize / 4); // minimal object size221while (!listenerInvoked || mpool.getUsage().getUsed() < mpool.getUsageThreshold()) {222iterations++;223if (trace) {224System.out.println(" Iteration " + iterations +225": before allocation " +226mpool.getUsage().getUsed());227}228229Object[] o = new Object[numElements];230if (iterations <= NUM_CHUNKS) {231// only hold a reference to the first NUM_CHUNKS232// allocated objects233objectPool.add(o);234}235236if (trace) {237System.out.println(" " +238" after allocation " +239mpool.getUsage().getUsed());240}241goSleep(100);242}243}244@Override245public void run() {246for (int i = 1; i <= NUM_TRIGGERS; i++) {247// Sync with SweeperThread's second phase.248phaser.arriveAndAwaitAdvance();249System.out.println("AllocatorThread is doing task " + i +250" phase " + phaser.getPhase());251doTask();252// Sync with SweeperThread's first phase.253phaser.arriveAndAwaitAdvance();254System.out.println("AllocatorThread done task " + i +255" phase " + phaser.getPhase());256if (testFailed) {257return;258}259}260}261}262263static class SweeperThread extends Thread {264private void doTask() {265for (; mpool.getUsage().getUsed() >=266mpool.getUsageThreshold();) {267// clear all allocated objects and invoke GC268objectPool.clear();269mm.gc();270goSleep(100);271}272}273@Override274public void run() {275for (int i = 1; i <= NUM_TRIGGERS; i++) {276// Sync with AllocatorThread's first phase.277phaser.arriveAndAwaitAdvance();278System.out.println("SweepThread is doing task " + i +279" phase " + phaser.getPhase());280doTask();281282listenerInvoked = false;283284// Sync with AllocatorThread's second phase.285phaser.arriveAndAwaitAdvance();286System.out.println("SweepThread done task " + i +287" phase " + phaser.getPhase());288if (testFailed) return;289}290}291}292}293294295