test/hotspot/jtreg/gc/survivorAlignment/SurvivorAlignmentTestMain.java
author lkorinth
Wed, 13 Nov 2019 11:37:29 +0100
changeset 59053 ba6c248cae19
parent 53523 4c5184c56dc2
permissions -rw-r--r--
8232365: Implementation for JEP 363: Remove the Concurrent Mark Sweep (CMS) Garbage Collector Reviewed-by: kbarrett, tschatzl, erikj, coleenp, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27890
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
     1
/*
53523
4c5184c56dc2 8214799: Add package declaration to each JTREG test case in the gc folder
lkorinth
parents: 47216
diff changeset
     2
 * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
27890
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
     4
 *
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
     8
 *
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    13
 * accompanied this code).
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    14
 *
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    18
 *
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    21
 * questions.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    22
 */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    23
53523
4c5184c56dc2 8214799: Add package declaration to each JTREG test case in the gc folder
lkorinth
parents: 47216
diff changeset
    24
package gc.survivorAlignment;
4c5184c56dc2 8214799: Add package declaration to each JTREG test case in the gc folder
lkorinth
parents: 47216
diff changeset
    25
27890
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    26
import java.lang.management.ManagementFactory;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    27
import java.lang.management.MemoryPoolMXBean;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    28
import java.util.Objects;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    29
import java.util.Optional;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    30
import java.util.regex.Matcher;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    31
import java.util.regex.Pattern;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    32
30604
b8d532cb6420 8067013: Rename the com.oracle.java.testlibary package
ykantser
parents: 27890
diff changeset
    33
import jdk.test.lib.Asserts;
27890
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    34
import com.sun.management.ThreadMXBean;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    35
import sun.hotspot.WhiteBox;
38152
80e5da81fb2c 8154258: [TESTBUG] Various serviceability tests fail compilation
dsamersoff
parents: 30604
diff changeset
    36
import jdk.internal.misc.Unsafe;
27890
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    37
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    38
/**
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    39
 * Main class for tests on {@code SurvivorAlignmentInBytes} option.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    40
 *
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    41
 * Typical usage is to obtain instance using fromArgs method, allocate objects
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    42
 * and verify that actual memory usage in tested heap space is close to
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    43
 * expected.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    44
 */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    45
public class SurvivorAlignmentTestMain {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    46
    enum HeapSpace {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    47
        EDEN,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    48
        SURVIVOR,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    49
        TENURED
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    50
    }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    51
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    52
    public static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    53
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    54
    public static final long MAX_TENURING_THRESHOLD = Optional.ofNullable(
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    55
            SurvivorAlignmentTestMain.WHITE_BOX.getIntxVMFlag(
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    56
                    "MaxTenuringThreshold")).orElse(15L);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    57
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    58
    /**
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    59
     * Regexp used to parse memory size params, like 2G, 34m or 15k.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    60
     */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    61
    private static final Pattern SIZE_REGEX
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    62
            = Pattern.compile("(?<size>[0-9]+)(?<multiplier>[GMKgmk])?");
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    63
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    64
    // Names of different heap spaces.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    65
    private static final String DEF_NEW_EDEN = "Eden Space";
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    66
    private static final String DEF_NEW_SURVIVOR = "Survivor Space";
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    67
    private static final String PAR_NEW_EDEN = "Par Eden Space";
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    68
    private static final String PAR_NEW_SURVIVOR = "Par Survivor Space";
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    69
    private static final String PS_EDEN = "PS Eden Space";
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    70
    private static final String PS_SURVIVOR = "PS Survivor Space";
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    71
    private static final String G1_EDEN = "G1 Eden Space";
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    72
    private static final String G1_SURVIVOR = "G1 Survivor Space";
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    73
    private static final String SERIAL_TENURED = "Tenured Gen";
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    74
    private static final String PS_TENURED = "PS Old Gen";
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    75
    private static final String G1_TENURED = "G1 Old Gen";
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    76
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    77
    private static final long G1_HEAP_REGION_SIZE = Optional.ofNullable(
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    78
            SurvivorAlignmentTestMain.WHITE_BOX.getUintxVMFlag(
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    79
                    "G1HeapRegionSize")).orElse(-1L);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    80
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    81
    private static final AlignmentHelper EDEN_SPACE_HELPER;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    82
    private static final AlignmentHelper SURVIVOR_SPACE_HELPER;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    83
    private static final AlignmentHelper TENURED_SPACE_HELPER;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    84
    /**
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    85
     * Amount of memory that should be filled during a test run.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    86
     */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    87
    private final long memoryToFill;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    88
    /**
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    89
     * The size of an objects that will be allocated during a test run.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    90
     */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    91
    private final long objectSize;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    92
    /**
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    93
     * Amount of memory that will be actually occupied by an object in eden
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    94
     * space.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    95
     */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    96
    private final long actualObjectSize;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    97
    /**
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    98
     * Storage for allocated objects.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    99
     */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   100
    private final Object[] garbage;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   101
    /**
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   102
     * Heap space whose memory usage is a subject of assertions during the test
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   103
     * run.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   104
     */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   105
    private final HeapSpace testedSpace;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   106
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   107
    private long[] baselinedThreadMemoryUsage = null;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   108
    private long[] threadIds = null;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   109
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   110
    /**
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   111
     * Initialize {@code EDEN_SPACE_HELPER}, {@code SURVIVOR_SPACE_HELPER} and
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   112
     * {@code TENURED_SPACE_HELPER} to represent heap spaces in use.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   113
     *
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   114
     * Note that regardless to GC object's alignment in survivor space is
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   115
     * expected to be equal to {@code SurvivorAlignmentInBytes} value and
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   116
     * alignment in other spaces is expected to be equal to
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   117
     * {@code ObjectAlignmentInBytes} value.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   118
     *
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   119
     * For G1 GC MXBeans could report memory usage only with region size
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   120
     * precision (if an object allocated in some G1 heap region, then all region
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   121
     * will claimed as used), so for G1's spaces precision is equal to
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   122
     * {@code G1HeapRegionSize} value.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   123
     */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   124
    static {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   125
        AlignmentHelper edenHelper = null;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   126
        AlignmentHelper survivorHelper = null;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   127
        AlignmentHelper tenuredHelper = null;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   128
        for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   129
            switch (pool.getName()) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   130
                case SurvivorAlignmentTestMain.DEF_NEW_EDEN:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   131
                case SurvivorAlignmentTestMain.PAR_NEW_EDEN:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   132
                case SurvivorAlignmentTestMain.PS_EDEN:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   133
                    Asserts.assertNull(edenHelper,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   134
                            "Only one bean for eden space is expected.");
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   135
                    edenHelper = new AlignmentHelper(
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   136
                            AlignmentHelper.OBJECT_ALIGNMENT_IN_BYTES,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   137
                            AlignmentHelper.OBJECT_ALIGNMENT_IN_BYTES,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   138
                            AlignmentHelper.MIN_OBJECT_SIZE, pool);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   139
                    break;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   140
                case SurvivorAlignmentTestMain.G1_EDEN:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   141
                    Asserts.assertNull(edenHelper,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   142
                            "Only one bean for eden space is expected.");
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   143
                    edenHelper = new AlignmentHelper(
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   144
                            SurvivorAlignmentTestMain.G1_HEAP_REGION_SIZE,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   145
                            AlignmentHelper.OBJECT_ALIGNMENT_IN_BYTES,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   146
                            AlignmentHelper.MIN_OBJECT_SIZE, pool);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   147
                    break;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   148
                case SurvivorAlignmentTestMain.DEF_NEW_SURVIVOR:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   149
                case SurvivorAlignmentTestMain.PAR_NEW_SURVIVOR:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   150
                case SurvivorAlignmentTestMain.PS_SURVIVOR:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   151
                    Asserts.assertNull(survivorHelper,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   152
                            "Only one bean for survivor space is expected.");
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   153
                    survivorHelper = new AlignmentHelper(
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   154
                            AlignmentHelper.OBJECT_ALIGNMENT_IN_BYTES,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   155
                            AlignmentHelper.SURVIVOR_ALIGNMENT_IN_BYTES,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   156
                            AlignmentHelper.MIN_OBJECT_SIZE, pool);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   157
                    break;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   158
                case SurvivorAlignmentTestMain.G1_SURVIVOR:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   159
                    Asserts.assertNull(survivorHelper,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   160
                            "Only one bean for survivor space is expected.");
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   161
                    survivorHelper = new AlignmentHelper(
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   162
                            SurvivorAlignmentTestMain.G1_HEAP_REGION_SIZE,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   163
                            AlignmentHelper.SURVIVOR_ALIGNMENT_IN_BYTES,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   164
                            AlignmentHelper.MIN_OBJECT_SIZE, pool);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   165
                    break;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   166
                case SurvivorAlignmentTestMain.SERIAL_TENURED:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   167
                case SurvivorAlignmentTestMain.PS_TENURED:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   168
                case SurvivorAlignmentTestMain.G1_TENURED:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   169
                    Asserts.assertNull(tenuredHelper,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   170
                            "Only one bean for tenured space is expected.");
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   171
                    tenuredHelper = new AlignmentHelper(
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   172
                            AlignmentHelper.OBJECT_ALIGNMENT_IN_BYTES,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   173
                            AlignmentHelper.OBJECT_ALIGNMENT_IN_BYTES,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   174
                            AlignmentHelper.MIN_OBJECT_SIZE, pool);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   175
                    break;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   176
            }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   177
        }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   178
        EDEN_SPACE_HELPER = Objects.requireNonNull(edenHelper,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   179
                "AlignmentHelper for eden space should be initialized.");
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   180
        SURVIVOR_SPACE_HELPER = Objects.requireNonNull(survivorHelper,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   181
                "AlignmentHelper for survivor space should be initialized.");
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   182
        TENURED_SPACE_HELPER = Objects.requireNonNull(tenuredHelper,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   183
                "AlignmentHelper for tenured space should be initialized.");
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   184
    }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   185
    /**
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   186
     * Returns an SurvivorAlignmentTestMain instance constructed using CLI
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   187
     * options.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   188
     *
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   189
     * Following options are expected:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   190
     * <ul>
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   191
     *     <li>memoryToFill</li>
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   192
     *     <li>objectSize</li>
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   193
     * </ul>
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   194
     *
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   195
     * Both argument may contain multiplier suffix k, m or g.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   196
     */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   197
    public static SurvivorAlignmentTestMain fromArgs(String[] args) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   198
        Asserts.assertEQ(args.length, 3, "Expected three arguments: "
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   199
                + "memory size, object size and tested heap space name.");
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   200
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   201
        long memoryToFill = parseSize(args[0]);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   202
        long objectSize = Math.max(parseSize(args[1]),
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   203
                AlignmentHelper.MIN_ARRAY_SIZE);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   204
        HeapSpace testedSpace = HeapSpace.valueOf(args[2]);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   205
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   206
        return new SurvivorAlignmentTestMain(memoryToFill, objectSize,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   207
                testedSpace);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   208
    }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   209
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   210
    /**
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   211
     * Returns a value parsed from a string with format
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   212
     * &lt;integer&gt;&lt;multiplier&gt;.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   213
     */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   214
    private static long parseSize(String sizeString) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   215
        Matcher matcher = SIZE_REGEX.matcher(sizeString);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   216
        Asserts.assertTrue(matcher.matches(),
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   217
                "sizeString should have following format \"[0-9]+([MBK])?\"");
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   218
        long size = Long.valueOf(matcher.group("size"));
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   219
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   220
        if (matcher.group("multiplier") != null) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   221
            long K = 1024L;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   222
            // fall through multipliers
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   223
            switch (matcher.group("multiplier").toLowerCase()) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   224
                case "g":
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   225
                    size *= K;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   226
                case "m":
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   227
                    size *= K;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   228
                case "k":
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   229
                    size *= K;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   230
            }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   231
        }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   232
        return size;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   233
    }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   234
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   235
    private SurvivorAlignmentTestMain(long memoryToFill, long objectSize,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   236
            HeapSpace testedSpace) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   237
        this.objectSize = objectSize;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   238
        this.memoryToFill = memoryToFill;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   239
        this.testedSpace = testedSpace;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   240
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   241
        AlignmentHelper helper = SurvivorAlignmentTestMain.EDEN_SPACE_HELPER;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   242
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   243
        this.actualObjectSize = helper.getObjectSizeInThisSpace(
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   244
                this.objectSize);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   245
        int arrayLength = helper.getObjectsCount(memoryToFill, this.objectSize);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   246
        garbage = new Object[arrayLength];
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   247
    }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   248
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   249
    /**
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   250
     * Allocate byte arrays to fill {@code memoryToFill} memory.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   251
     */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   252
    public void allocate() {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   253
        int byteArrayLength = Math.max((int) (objectSize
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   254
                - Unsafe.ARRAY_BYTE_BASE_OFFSET), 0);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   255
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   256
        for (int i = 0; i < garbage.length; i++) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   257
            garbage[i] = new byte[byteArrayLength];
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   258
        }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   259
    }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   260
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   261
    /**
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   262
     * Release memory occupied after {@code allocate} call.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   263
     */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   264
    public void release() {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   265
        for (int i = 0; i < garbage.length; i++) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   266
            garbage[i] = null;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   267
        }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   268
    }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   269
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   270
    /**
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   271
     * Returns expected amount of memory occupied in a {@code heapSpace} by
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   272
     * objects referenced from {@code garbage} array.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   273
     */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   274
    public long getExpectedMemoryUsage() {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   275
        AlignmentHelper alignmentHelper = getAlignmentHelper(testedSpace);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   276
        return alignmentHelper.getExpectedMemoryUsage(objectSize,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   277
                garbage.length);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   278
    }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   279
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   280
    /**
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   281
     * Verifies that memory usage in a {@code heapSpace} deviates from
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   282
     * {@code expectedUsage} for no more than {@code MAX_RELATIVE_DEVIATION}.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   283
     */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   284
    public void verifyMemoryUsage(long expectedUsage) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   285
        AlignmentHelper alignmentHelper = getAlignmentHelper(testedSpace);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   286
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   287
        long actualMemoryUsage = alignmentHelper.getActualMemoryUsage();
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   288
        boolean otherThreadsAllocatedMemory = areOtherThreadsAllocatedMemory();
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   289
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   290
        long memoryUsageDiff = Math.abs(actualMemoryUsage - expectedUsage);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   291
        long maxAllowedUsageDiff
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   292
                = alignmentHelper.getAllowedMemoryUsageDeviation(expectedUsage);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   293
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   294
        System.out.println("Verifying memory usage in space: " + testedSpace);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   295
        System.out.println("Allocated objects count: " + garbage.length);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   296
        System.out.println("Desired object size: " + objectSize);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   297
        System.out.println("Actual object size: " + actualObjectSize);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   298
        System.out.println("Expected object size in space: "
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   299
                + alignmentHelper.getObjectSizeInThisSpace(objectSize));
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   300
        System.out.println("Expected memory usage: " + expectedUsage);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   301
        System.out.println("Actual memory usage: " + actualMemoryUsage);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   302
        System.out.println("Memory usage diff: " + memoryUsageDiff);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   303
        System.out.println("Max allowed usage diff: " + maxAllowedUsageDiff);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   304
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   305
        if (memoryUsageDiff > maxAllowedUsageDiff
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   306
                && otherThreadsAllocatedMemory) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   307
            System.out.println("Memory usage diff is incorrect, but it seems "
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   308
                    + "like someone else allocated objects");
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   309
            return;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   310
        }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   311
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   312
        Asserts.assertLTE(memoryUsageDiff, maxAllowedUsageDiff,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   313
                "Actual memory usage should not deviate from expected for " +
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   314
                        "more then " + maxAllowedUsageDiff);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   315
    }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   316
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   317
    /**
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   318
     * Baselines amount of memory allocated by each thread.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   319
     */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   320
    public void baselineMemoryAllocation() {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   321
        ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   322
        threadIds = bean.getAllThreadIds();
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   323
        baselinedThreadMemoryUsage = bean.getThreadAllocatedBytes(threadIds);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   324
    }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   325
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   326
    /**
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   327
     * Checks if threads other then the current thread were allocating objects
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   328
     * after baselinedThreadMemoryUsage call.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   329
     *
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   330
     * If baselinedThreadMemoryUsage was not called, then this method will return
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   331
     * {@code false}.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   332
     */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   333
    public boolean areOtherThreadsAllocatedMemory() {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   334
        if (baselinedThreadMemoryUsage == null) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   335
            return false;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   336
        }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   337
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   338
        ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   339
        long currentMemoryAllocation[]
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   340
                = bean.getThreadAllocatedBytes(threadIds);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   341
        boolean otherThreadsAllocatedMemory = false;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   342
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   343
        System.out.println("Verifying amount of memory allocated by threads:");
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   344
        for (int i = 0; i < threadIds.length; i++) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   345
            System.out.format("Thread %d%nbaseline allocation: %d"
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   346
                            + "%ncurrent allocation:%d%n", threadIds[i],
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   347
                    baselinedThreadMemoryUsage[i], currentMemoryAllocation[i]);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   348
            System.out.println(bean.getThreadInfo(threadIds[i]));
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   349
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   350
            long bytesAllocated = Math.abs(currentMemoryAllocation[i]
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   351
                    - baselinedThreadMemoryUsage[i]);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   352
            if (bytesAllocated > 0
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   353
                    && threadIds[i] != Thread.currentThread().getId()) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   354
                otherThreadsAllocatedMemory = true;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   355
            }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   356
        }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   357
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   358
        return otherThreadsAllocatedMemory;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   359
    }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   360
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   361
    @Override
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   362
    public String toString() {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   363
        StringBuilder builder = new StringBuilder();
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   364
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   365
        builder.append(String.format("SurvivorAlignmentTestMain info:%n"));
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   366
        builder.append(String.format("Desired object size: %d%n", objectSize));
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   367
        builder.append(String.format("Memory to fill: %d%n", memoryToFill));
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   368
        builder.append(String.format("Objects to be allocated: %d%n",
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   369
                garbage.length));
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   370
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   371
        builder.append(String.format("Alignment helpers to be used: %n"));
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   372
        for (HeapSpace heapSpace: HeapSpace.values()) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   373
            builder.append(String.format("For space %s:%n%s%n", heapSpace,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   374
                    getAlignmentHelper(heapSpace)));
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   375
        }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   376
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   377
        return builder.toString();
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   378
    }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   379
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   380
    /**
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   381
     * Returns {@code AlignmentHelper} for a space {@code heapSpace}.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   382
     */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   383
    public static AlignmentHelper getAlignmentHelper(HeapSpace heapSpace) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   384
        switch (heapSpace) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   385
            case EDEN:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   386
                return SurvivorAlignmentTestMain.EDEN_SPACE_HELPER;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   387
            case SURVIVOR:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   388
                return SurvivorAlignmentTestMain.SURVIVOR_SPACE_HELPER;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   389
            case TENURED:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   390
                return SurvivorAlignmentTestMain.TENURED_SPACE_HELPER;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   391
            default:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   392
                throw new Error("Unexpected heap space: " + heapSpace);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   393
        }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   394
    }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   395
}