test/hotspot/jtreg/gc/z/TestHighUsage.java
author pliden
Thu, 23 May 2019 08:09:29 +0200
changeset 55002 da9840e2f7da
child 55224 1de459182903
permissions -rw-r--r--
8224185: ZGC: Introduce "High Usage" rule Reviewed-by: eosterlund
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55002
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
     1
/*
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
     4
 *
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
     7
 * published by the Free Software Foundation.
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
     8
 *
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    13
 * accompanied this code).
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    14
 *
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    18
 *
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    21
 * questions.
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    22
 */
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    23
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    24
package gc.z;
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    25
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    26
/*
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    27
 * @test TestHighUsage
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    28
 * @requires vm.gc.Z & !vm.graal.enabled
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    29
 * @summary Test ZGC "High Usage" rule
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    30
 * @library /test/lib
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    31
 * @run main/othervm gc.z.TestHighUsage
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    32
 */
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    33
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    34
import java.util.LinkedList;
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    35
import jdk.test.lib.process.ProcessTools;
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    36
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    37
public class TestHighUsage {
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    38
    static class Test {
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    39
        private static final int K = 1024;
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    40
        private static final int M = K * K;
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    41
        private static final long startAt = 16 * M;
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    42
        private static final long spikeAt = 4 * M;
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    43
        private static volatile LinkedList<byte[]> keepAlive;
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    44
        private static volatile Object dummy;
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    45
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    46
        public static void main(String[] args) throws Exception {
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    47
            System.out.println("Allocating live-set");
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    48
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    49
            // Allocate live-set
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    50
            keepAlive = new LinkedList<>();
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    51
            while (Runtime.getRuntime().freeMemory() > startAt) {
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    52
                while (Runtime.getRuntime().freeMemory() > startAt) {
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    53
                    keepAlive.add(new byte[128 * K]);
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    54
                }
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    55
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    56
                // Compact live-set and let allocation rate settle down
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    57
                System.gc();
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    58
                Thread.sleep(2000);
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    59
            }
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    60
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    61
            System.out.println("Allocating garbage slowly");
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    62
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    63
            // Allocate garbage slowly, such that the sampled allocation rate on
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    64
            // average becomes zero MB/s for the last 1 second windows. If free
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    65
            // memory goes below the spike limit we induce an allocation spike.
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    66
            // The expected behavior is that the "High Usage" rule kicks in before
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    67
            // the spike happens, avoiding an "Allocation Stall".
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    68
            for (int i = 0; i < 300; i++) {
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    69
                final long free = Runtime.getRuntime().freeMemory();
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    70
                System.out.println("Free: " + (free / M) + "M");
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    71
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    72
                if (free > spikeAt) {
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    73
                    // Low allocation rate
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    74
                    dummy = new byte[128 * K];
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    75
                } else {
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    76
                    // High allocation rate
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    77
                    dummy = new byte[8 * M];
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    78
                }
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    79
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    80
                Thread.sleep(250);
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    81
            }
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    82
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    83
            System.out.println("Done");
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    84
        }
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    85
    }
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    86
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    87
    public static void main(String[] args) throws Exception {
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    88
        ProcessTools.executeTestJvm(new String[]{ "-XX:+UnlockExperimentalVMOptions",
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    89
                                                  "-XX:+UseZGC",
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    90
                                                  "-XX:+UnlockDiagnosticVMOptions",
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    91
                                                  "-XX:-ZProactive",
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    92
                                                  "-Xms128M",
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    93
                                                  "-Xmx128M",
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    94
                                                  "-XX:ParallelGCThreads=1",
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    95
                                                  "-XX:ConcGCThreads=1",
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    96
                                                  "-Xlog:gc",
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    97
                                                  Test.class.getName() })
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    98
                    .shouldNotContain("Allocation Stall")
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
    99
                    .shouldContain("High Usage")
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
   100
                    .shouldHaveExitValue(0);
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
   101
    }
da9840e2f7da 8224185: ZGC: Introduce "High Usage" rule
pliden
parents:
diff changeset
   102
}