hotspot/test/gc/g1/humongousObjects/TestHeapCounters.java
author dsamersoff
Sun, 01 May 2016 12:47:00 +0300
changeset 38152 80e5da81fb2c
parent 35933 e7a76427c582
child 39414 4adf52148100
permissions -rw-r--r--
8154258: [TESTBUG] Various serviceability tests fail compilation Summary: Replace sun.misc.Unsafe with jdk.internal.misc.Unsafe Reviewed-by: chegar, kvn
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 /
38152
80e5da81fb2c 8154258: [TESTBUG] Various serviceability tests fail compilation
dsamersoff
parents: 35933
diff changeset
    42
 * @modules java.base/jdk.internal.misc
35933
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    43
 * @modules java.management
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    44
 * @build sun.hotspot.WhiteBox
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    45
 *        gc.testlibrary.Helpers
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    46
 *        gc.g1.humongousObjects.TestHeapCounters
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    47
 * @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
    48
 *             sun.hotspot.WhiteBox$WhiteBoxPermission
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    49
 *
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    50
 * @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
    51
 *                   -Xmx128m -Xms128m
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    52
 *                   -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
    53
 *                   -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
    54
 *                    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
    55
 *
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    56
 * @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
    57
 *                   -Xmx128m -Xms128m
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    58
 *                   -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
    59
 *                   -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
    60
 *                    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
    61
 */
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    62
public class TestHeapCounters {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    63
    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
    64
    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
    65
    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
    66
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    67
    // 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
    68
    // 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
    69
    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
    70
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    71
    private enum MemoryCounter {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    72
        MX_BEAN_COUNTER {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    73
            @Override
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    74
            public long getUsedMemory() {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    75
                return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
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
        },
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    78
        RUNTIME_COUNTER {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    79
            @Override
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    80
            public long getUsedMemory() {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    81
                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
    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
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    85
        public abstract long getUsedMemory();
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
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    88
    private static class Allocation {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    89
        private byte[] allocation;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    90
        public final long expectedSize;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    91
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    92
        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
    93
            allocation = new byte[allocationSize];
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    94
            expectedSize = allocationExpectedSize;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    95
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    96
            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
    97
                    WHITE_BOX.getObjectSize(allocation),
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    98
                    (WHITE_BOX.g1IsHumongous(allocation) ? "" : "non-")));
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    99
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   100
            selfTest();
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
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   103
        private void selfTest() {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   104
            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
   105
            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
   106
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   107
            // Sanity check
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   108
            Asserts.assertEquals(isHumongous, shouldBeHumongous,
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   109
                    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
   110
                            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
   111
        }
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   112
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   113
        public void forgetAllocation() {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   114
            allocation = null;
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
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   118
    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
   119
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   120
        if (args.length != 1) {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   121
            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
   122
        }
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   123
        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
   124
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   125
        int byteArrayMemoryOverhead = Helpers.detectByteArrayAllocationOverhead();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   126
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   127
        // Largest non-humongous byte[]
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   128
        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
   129
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   130
        // 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
   131
        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
   132
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   133
        List<Integer> allocationSizes = Arrays.asList(
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   134
                (int) maxByteArrayNonHumongousSize + 1,
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   135
                (int) (0.8f * maxByteArrayOneRegionSize),
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   136
                (int) (maxByteArrayOneRegionSize),
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   137
                (int) (1.2f * maxByteArrayOneRegionSize),
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   138
                (int) (1.5f * maxByteArrayOneRegionSize),
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   139
                (int) (1.7f * maxByteArrayOneRegionSize),
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   140
                (int) (2.0f * maxByteArrayOneRegionSize),
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   141
                (int) (2.5f * maxByteArrayOneRegionSize)
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
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   144
        List<Allocation> allocations = new ArrayList<>();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   145
        List<GarbageCollectorMXBean> gcBeans =
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   146
                ManagementFactory.getGarbageCollectorMXBeans();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   147
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   148
        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
   149
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   150
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   151
        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
   152
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   153
        for (int allocationSize : allocationSizes) {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   154
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   155
            long usedMemoryBefore = memoryCounter.getUsedMemory();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   156
            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
   157
            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
   158
            long usedMemoryAfter = memoryCounter.getUsedMemory();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   159
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   160
            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
   161
                            + "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
   162
                    expectedAllocationSize, usedMemoryBefore, usedMemoryAfter);
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   163
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   164
            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
   165
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   166
            if (gcCountNow == gcCountBefore) {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   167
                // 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
   168
                Asserts.assertGreaterThanOrEqual(usedMemoryAfter - usedMemoryBefore, expectedAllocationSize,
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   169
                        "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
   170
                                " returned wrong allocation size");
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   171
            } else {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   172
                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
   173
                gcCountBefore = gcCountNow;
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
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   177
        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
   178
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   179
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   180
        allocations.stream().forEach(allocation -> {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   181
            long usedMemoryBefore = memoryCounter.getUsedMemory();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   182
            allocation.forgetAllocation();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   183
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   184
            WHITE_BOX.fullGC();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   185
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   186
            long usedMemoryAfter = memoryCounter.getUsedMemory();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   187
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   188
            // 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
   189
            Asserts.assertGreaterThanOrEqual(usedMemoryBefore - usedMemoryAfter,
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   190
                    (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
   191
                    "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
   192
        });
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   193
    }
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   194
}