1 /* 2 * Copyright (c) 2019, Oracle and/or its affiliates. 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 it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 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 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package gc.z; 25 26 /* 27 * @test TestHighUsage 28 * @requires vm.gc.Z & !vm.graal.enabled 29 * @summary Test ZGC "High Usage" rule 30 * @library /test/lib 31 * @run main/othervm gc.z.TestHighUsage 32 */ 33 34 import java.util.LinkedList; 35 import jdk.test.lib.process.ProcessTools; 36 37 public class TestHighUsage { 38 static class Test { 39 private static final int K = 1024; 40 private static final int M = K * K; 41 private static final long maxCapacity = Runtime.getRuntime().maxMemory(); 42 private static final long slowAllocationThreshold = 16 * M; 43 private static final long highUsageThreshold = maxCapacity / 20; // 5% 44 private static volatile LinkedList<byte[]> keepAlive; 45 private static volatile Object dummy; 46 47 public static void main(String[] args) throws Exception { 48 System.out.println("Max capacity: " + (maxCapacity / M) + "M"); 49 System.out.println("High usage threshold: " + (highUsageThreshold / M) + "M"); 50 System.out.println("Allocating live-set"); 51 52 // Allocate live-set 53 keepAlive = new LinkedList<>(); 54 while (Runtime.getRuntime().freeMemory() > slowAllocationThreshold) { 55 while (Runtime.getRuntime().freeMemory() > slowAllocationThreshold) { 56 keepAlive.add(new byte[128 * K]); 57 } 58 59 // Compact live-set and let allocation rate settle down 60 System.gc(); 61 Thread.sleep(2000); 62 } 63 64 System.out.println("Allocating garbage slowly"); 65 66 // Allocate garbage slowly, so that the sampled allocation rate on average 67 // becomes zero MB/s for the last 1 second windows. Once we reach the high 68 // usage threshold we idle to allow for a "High Usage" GC cycle to happen. 69 // We need to allocate slowly to avoid an "Allocation Rate" GC cycle. 70 for (int i = 0; i < 300; i++) { 71 if (Runtime.getRuntime().freeMemory() > highUsageThreshold) { 72 // Allocate 73 dummy = new byte[128 * K]; 74 System.out.println("Free: " + (Runtime.getRuntime().freeMemory() / M) + "M (Allocating)"); 75 } else { 76 // Idle 77 System.out.println("Free: " + (Runtime.getRuntime().freeMemory() / M) + "M (Idling)"); 78 } 79 80 Thread.sleep(250); 81 } 82 83 System.out.println("Done"); 84 } 85 } 86 87 public static void main(String[] args) throws Exception { 88 ProcessTools.executeTestJvm(new String[]{ "-XX:+UnlockExperimentalVMOptions", 89 "-XX:+UseZGC", 90 "-XX:-ZProactive", 91 "-Xms128M", 92 "-Xmx128M", 93 "-XX:ParallelGCThreads=1", 94 "-XX:ConcGCThreads=1", 95 "-Xlog:gc,gc+start", 96 Test.class.getName() }) 97 .shouldNotContain("Allocation Stall") 98 .shouldContain("High Usage") 99 .shouldHaveExitValue(0); 100 } 101 }