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 startAt = 16 * M;
  42         private static final long spikeAt = 4 * M;
  43         private static volatile LinkedList<byte[]> keepAlive;
  44         private static volatile Object dummy;
  45 
  46         public static void main(String[] args) throws Exception {
  47             System.out.println("Allocating live-set");
  48 
  49             // Allocate live-set
  50             keepAlive = new LinkedList<>();
  51             while (Runtime.getRuntime().freeMemory() > startAt) {
  52                 while (Runtime.getRuntime().freeMemory() > startAt) {
  53                     keepAlive.add(new byte[128 * K]);
  54                 }
  55 
  56                 // Compact live-set and let allocation rate settle down
  57                 System.gc();
  58                 Thread.sleep(2000);
  59             }
  60 
  61             System.out.println("Allocating garbage slowly");
  62 
  63             // Allocate garbage slowly, such that the sampled allocation rate on
  64             // average becomes zero MB/s for the last 1 second windows. If free
  65             // memory goes below the spike limit we induce an allocation spike.
  66             // The expected behavior is that the "High Usage" rule kicks in before
  67             // the spike happens, avoiding an "Allocation Stall".
  68             for (int i = 0; i < 300; i++) {
  69                 final long free = Runtime.getRuntime().freeMemory();
  70                 System.out.println("Free: " + (free / M) + "M");
  71 
  72                 if (free > spikeAt) {
  73                     // Low allocation rate
  74                     dummy = new byte[128 * K];
  75                 } else {
  76                     // High allocation rate
  77                     dummy = new byte[8 * M];
  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:+UnlockDiagnosticVMOptions",
  91                                                   "-XX:-ZProactive",
  92                                                   "-Xms128M",
  93                                                   "-Xmx128M",
  94                                                   "-XX:ParallelGCThreads=1",
  95                                                   "-XX:ConcGCThreads=1",
  96                                                   "-Xlog:gc",
  97                                                   Test.class.getName() })
  98                     .shouldNotContain("Allocation Stall")
  99                     .shouldContain("High Usage")
 100                     .shouldHaveExitValue(0);
 101     }
 102 }