hotspot/test/gc/g1/humongousObjects/TestHeapCounters.java
author stsmirno
Mon, 17 Oct 2016 18:54:12 -0400
changeset 41705 332239c052cc
parent 40631 ed82623d7831
permissions -rw-r--r--
8165687: Fix license and copyright headers in jd9 under hotspot/test Summary: Legal notices and Oracle copyrights were updated (white and blank space, commas) in tests files for uniformity to meet Oracle requirements. Reviewed-by: dholmes, iris Contributed-by: Stanislav Smirnov <stanislav.smirnov@oracle.com>, Vassili Igouchkine <vassili.igouchkine@oracle.com>
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
package gc.g1.humongousObjects;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    25
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    26
import gc.testlibrary.Helpers;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    27
import jdk.test.lib.Asserts;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    28
import sun.hotspot.WhiteBox;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    29
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    30
import java.lang.management.GarbageCollectorMXBean;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    31
import java.lang.management.ManagementFactory;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    32
import java.util.ArrayList;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    33
import java.util.Arrays;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    34
import java.util.List;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    35
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
 * @test TestHeapCounters
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    38
 * @summary Checks that heap counters work as expected after humongous allocations/deallocations
39414
4adf52148100 8160088: update hotspot tests depending on GC to use @requires vm.gc.X
dfazunen
parents: 38152
diff changeset
    39
 * @requires vm.gc.G1
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 39414
diff changeset
    40
 * @library /test/lib /
38152
80e5da81fb2c 8154258: [TESTBUG] Various serviceability tests fail compilation
dsamersoff
parents: 35933
diff changeset
    41
 * @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
    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
 * @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
    45
 *             sun.hotspot.WhiteBox$WhiteBoxPermission
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    46
 *
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    47
 * @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
    48
 *                   -Xmx128m -Xms128m
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    49
 *                   -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
    50
 *                   -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
    51
 *                    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
    52
 *
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    53
 * @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
    54
 *                   -Xmx128m -Xms128m
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    55
 *                   -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
    56
 *                   -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
    57
 *                    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
    58
 */
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    59
public class TestHeapCounters {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    60
    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
    61
    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
    62
    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
    63
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    64
    // 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
    65
    // 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
    66
    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
    67
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    68
    private enum MemoryCounter {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    69
        MX_BEAN_COUNTER {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    70
            @Override
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    71
            public long getUsedMemory() {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    72
                return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    73
            }
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    74
        },
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    75
        RUNTIME_COUNTER {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    76
            @Override
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    77
            public long getUsedMemory() {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    78
                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
    79
            }
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    80
        };
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
        public abstract long getUsedMemory();
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
    private static class Allocation {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    86
        private byte[] allocation;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    87
        public final long expectedSize;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    88
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    89
        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
    90
            allocation = new byte[allocationSize];
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    91
            expectedSize = allocationExpectedSize;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    92
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    93
            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
    94
                    WHITE_BOX.getObjectSize(allocation),
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    95
                    (WHITE_BOX.g1IsHumongous(allocation) ? "" : "non-")));
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    96
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
    97
            selfTest();
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
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   100
        private void selfTest() {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   101
            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
   102
            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
   103
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   104
            // Sanity check
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   105
            Asserts.assertEquals(isHumongous, shouldBeHumongous,
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   106
                    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
   107
                            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
   108
        }
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   109
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   110
        public void forgetAllocation() {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   111
            allocation = null;
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
    }
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
    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
   116
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   117
        if (args.length != 1) {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   118
            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
   119
        }
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   120
        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
   121
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   122
        int byteArrayMemoryOverhead = Helpers.detectByteArrayAllocationOverhead();
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
        // Largest non-humongous byte[]
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   125
        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
   126
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   127
        // 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
   128
        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
   129
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   130
        List<Integer> allocationSizes = Arrays.asList(
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   131
                (int) maxByteArrayNonHumongousSize + 1,
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   132
                (int) (0.8f * maxByteArrayOneRegionSize),
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   133
                (int) (maxByteArrayOneRegionSize),
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   134
                (int) (1.2f * maxByteArrayOneRegionSize),
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   135
                (int) (1.5f * maxByteArrayOneRegionSize),
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   136
                (int) (1.7f * maxByteArrayOneRegionSize),
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   137
                (int) (2.0f * maxByteArrayOneRegionSize),
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   138
                (int) (2.5f * maxByteArrayOneRegionSize)
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   139
        );
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   140
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   141
        List<Allocation> allocations = new ArrayList<>();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   142
        List<GarbageCollectorMXBean> gcBeans =
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   143
                ManagementFactory.getGarbageCollectorMXBeans();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   144
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   145
        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
   146
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
        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
   149
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   150
        for (int allocationSize : allocationSizes) {
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
            long usedMemoryBefore = memoryCounter.getUsedMemory();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   153
            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
   154
            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
   155
            long usedMemoryAfter = memoryCounter.getUsedMemory();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   156
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   157
            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
   158
                            + "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
   159
                    expectedAllocationSize, usedMemoryBefore, usedMemoryAfter);
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   160
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   161
            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
   162
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   163
            if (gcCountNow == gcCountBefore) {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   164
                // 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
   165
                Asserts.assertGreaterThanOrEqual(usedMemoryAfter - usedMemoryBefore, expectedAllocationSize,
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   166
                        "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
   167
                                " returned wrong allocation size");
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   168
            } else {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   169
                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
   170
                gcCountBefore = gcCountNow;
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   171
            }
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   172
        }
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
        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
   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
        allocations.stream().forEach(allocation -> {
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   178
            long usedMemoryBefore = memoryCounter.getUsedMemory();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   179
            allocation.forgetAllocation();
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   180
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   181
            WHITE_BOX.fullGC();
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
            long usedMemoryAfter = memoryCounter.getUsedMemory();
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
            // 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
   186
            Asserts.assertGreaterThanOrEqual(usedMemoryBefore - usedMemoryAfter,
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   187
                    (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
   188
                    "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
   189
        });
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   190
    }
e7a76427c582 8132721: Add tests which check that heap counters work as expected during Humongous allocations
kzhaldyb
parents:
diff changeset
   191
}