hotspot/test/gc/g1/humongousObjects/TestHeapCounters.java
author kzhaldyb
Tue, 02 Feb 2016 18:06:14 +0300
changeset 35933 e7a76427c582
child 38152 80e5da81fb2c
permissions -rw-r--r--
8132721: Add tests which check that heap counters work as expected during Humongous allocations Reviewed-by: jmasa, tschatzl, dfazunen
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35933
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
     1
/*
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
     4
 *
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
     8
 *
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    13
 * accompanied this code).
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    14
 *
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    18
 *
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    21
 * questions.
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    22
 *
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    23
 */
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    24
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    25
package gc.g1.humongousObjects;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    26
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    27
import gc.testlibrary.Helpers;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    28
import jdk.test.lib.Asserts;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    29
import sun.hotspot.WhiteBox;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    30
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    31
import java.lang.management.GarbageCollectorMXBean;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    32
import java.lang.management.ManagementFactory;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    33
import java.util.ArrayList;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    34
import java.util.Arrays;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    35
import java.util.List;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    36
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    37
/**
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    38
 * @test TestHeapCounters
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    39
 * @summary Checks that heap counters work as expected after humongous allocations/deallocations
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    40
 * @requires vm.gc=="G1" | vm.gc=="null"
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    41
 * @library /testlibrary /test/lib /
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    42
 * @modules java.management
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    43
 * @build sun.hotspot.WhiteBox
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    44
 *        gc.testlibrary.Helpers
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    45
 *        gc.g1.humongousObjects.TestHeapCounters
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    46
 * @run driver ClassFileInstaller sun.hotspot.WhiteBox
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    47
 *             sun.hotspot.WhiteBox$WhiteBoxPermission
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    48
 *
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    49
 * @run main/othervm -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    50
 *                   -Xmx128m -Xms128m
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    51
 *                   -XX:G1HeapRegionSize=1M -XX:InitiatingHeapOccupancyPercent=100 -XX:-G1UseAdaptiveIHOP
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    52
 *                   -Xlog:gc -Xlog:gc:file=TestHeapCountersRuntime.gc.log
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    53
 *                    gc.g1.humongousObjects.TestHeapCounters RUNTIME_COUNTER
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    54
 *
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    55
 * @run main/othervm -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    56
 *                   -Xmx128m -Xms128m
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    57
 *                   -XX:G1HeapRegionSize=1M -XX:InitiatingHeapOccupancyPercent=100 -XX:-G1UseAdaptiveIHOP
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    58
 *                   -Xlog:gc -Xlog:gc:file=TestHeapCountersMXBean.gc.log
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    59
 *                    gc.g1.humongousObjects.TestHeapCounters MX_BEAN_COUNTER
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    60
 */
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    61
public class TestHeapCounters {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    62
    private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    63
    private static final int G1_REGION_SIZE = WHITE_BOX.g1RegionSize();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    64
    private static final int HALF_G1_REGION_SIZE = G1_REGION_SIZE / 2;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    65
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    66
    // Since during deallocation GC could free (very unlikely) some non-humongous data this value relaxes amount of
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    67
    // memory we expect to be freed.
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    68
    private static final double ALLOCATION_SIZE_TOLERANCE_FACTOR = 0.85D;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    69
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    70
    private enum MemoryCounter {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    71
        MX_BEAN_COUNTER {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    72
            @Override
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    73
            public long getUsedMemory() {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    74
                return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    75
            }
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    76
        },
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    77
        RUNTIME_COUNTER {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    78
            @Override
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    79
            public long getUsedMemory() {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    80
                return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    81
            }
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    82
        };
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    83
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    84
        public abstract long getUsedMemory();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    85
    }
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    86
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    87
    private static class Allocation {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    88
        private byte[] allocation;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    89
        public final long expectedSize;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    90
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    91
        public Allocation(int allocationSize, long allocationExpectedSize) {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    92
            allocation = new byte[allocationSize];
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    93
            expectedSize = allocationExpectedSize;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    94
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    95
            System.out.println(String.format("Object size is %d; Object is %shumongous",
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    96
                    WHITE_BOX.getObjectSize(allocation),
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    97
                    (WHITE_BOX.g1IsHumongous(allocation) ? "" : "non-")));
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    98
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    99
            selfTest();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   100
        }
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   101
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   102
        private void selfTest() {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   103
            boolean isHumongous = WHITE_BOX.getObjectSize(allocation) > HALF_G1_REGION_SIZE;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   104
            boolean shouldBeHumongous = WHITE_BOX.g1IsHumongous(allocation);
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   105
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   106
            // Sanity check
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   107
            Asserts.assertEquals(isHumongous, shouldBeHumongous,
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   108
                    String.format("Test Bug: Object of size %d is expected to be %shumongous but it is not",
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   109
                            WHITE_BOX.getObjectSize(allocation), (shouldBeHumongous ? "" : "non-")));
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   110
        }
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   111
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   112
        public void forgetAllocation() {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   113
            allocation = null;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   114
        }
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   115
    }
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   116
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   117
    public static void main(String[] args) {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   118
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   119
        if (args.length != 1) {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   120
            throw new Error("Expected memory counter name wasn't provided as command line argument");
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   121
        }
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   122
        MemoryCounter memoryCounter = MemoryCounter.valueOf(args[0].toUpperCase());
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   123
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   124
        int byteArrayMemoryOverhead = Helpers.detectByteArrayAllocationOverhead();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   125
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   126
        // Largest non-humongous byte[]
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   127
        int maxByteArrayNonHumongousSize = HALF_G1_REGION_SIZE - byteArrayMemoryOverhead;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   128
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   129
        // Maximum byte[] that takes one region
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   130
        int maxByteArrayOneRegionSize = G1_REGION_SIZE - byteArrayMemoryOverhead;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   131
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   132
        List<Integer> allocationSizes = Arrays.asList(
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   133
                (int) maxByteArrayNonHumongousSize + 1,
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   134
                (int) (0.8f * maxByteArrayOneRegionSize),
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   135
                (int) (maxByteArrayOneRegionSize),
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   136
                (int) (1.2f * maxByteArrayOneRegionSize),
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   137
                (int) (1.5f * maxByteArrayOneRegionSize),
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   138
                (int) (1.7f * maxByteArrayOneRegionSize),
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   139
                (int) (2.0f * maxByteArrayOneRegionSize),
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   140
                (int) (2.5f * maxByteArrayOneRegionSize)
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   141
        );
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   142
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   143
        List<Allocation> allocations = new ArrayList<>();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   144
        List<GarbageCollectorMXBean> gcBeans =
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   145
                ManagementFactory.getGarbageCollectorMXBeans();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   146
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   147
        long gcCountBefore = gcBeans.stream().mapToLong(GarbageCollectorMXBean::getCollectionCount).sum();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   148
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   149
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   150
        System.out.println("Starting allocations - no GC should happen until we finish them");
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   151
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   152
        for (int allocationSize : allocationSizes) {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   153
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   154
            long usedMemoryBefore = memoryCounter.getUsedMemory();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   155
            long expectedAllocationSize = (long) Math.ceil((double) allocationSize / G1_REGION_SIZE) * G1_REGION_SIZE;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   156
            allocations.add(new Allocation(allocationSize, expectedAllocationSize));
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   157
            long usedMemoryAfter = memoryCounter.getUsedMemory();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   158
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   159
            System.out.format("Expected allocation size: %d\nUsed memory before allocation: %d\n"
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   160
                            + "Used memory after allocation: %d\n",
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   161
                    expectedAllocationSize, usedMemoryBefore, usedMemoryAfter);
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   162
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   163
            long gcCountNow = gcBeans.stream().mapToLong(GarbageCollectorMXBean::getCollectionCount).sum();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   164
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   165
            if (gcCountNow == gcCountBefore) {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   166
                // We should allocate at least allocation.expectedSize
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   167
                Asserts.assertGreaterThanOrEqual(usedMemoryAfter - usedMemoryBefore, expectedAllocationSize,
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   168
                        "Counter of type " + memoryCounter.getClass().getSimpleName() +
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   169
                                " returned wrong allocation size");
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   170
            } else {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   171
                System.out.println("GC happened during allocation so the check is skipped");
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   172
                gcCountBefore = gcCountNow;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   173
            }
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   174
        }
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   175
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   176
        System.out.println("Finished allocations - no GC should have happened before this line");
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   177
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   178
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   179
        allocations.stream().forEach(allocation -> {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   180
            long usedMemoryBefore = memoryCounter.getUsedMemory();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   181
            allocation.forgetAllocation();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   182
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   183
            WHITE_BOX.fullGC();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   184
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   185
            long usedMemoryAfter = memoryCounter.getUsedMemory();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   186
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   187
            // We should free at least allocation.expectedSize * ALLOCATION_SIZE_TOLERANCE_FACTOR
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   188
            Asserts.assertGreaterThanOrEqual(usedMemoryBefore - usedMemoryAfter,
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   189
                    (long) (allocation.expectedSize * ALLOCATION_SIZE_TOLERANCE_FACTOR),
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   190
                    "Counter of type " + memoryCounter.getClass().getSimpleName() + " returned wrong allocation size");
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   191
        });
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   192
    }
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   193
}