test/hotspot/jtreg/gc/survivorAlignment/SurvivorAlignmentTestMain.java
author lkorinth
Mon, 28 Jan 2019 15:53:56 +0100
changeset 53523 4c5184c56dc2
parent 47216 71c04702a3d5
child 59053 ba6c248cae19
permissions -rw-r--r--
8214799: Add package declaration to each JTREG test case in the gc folder Reviewed-by: lmesnik, tschatzl
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 CMS_TENURED = "CMS Old Gen";
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    75
    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
    76
    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
    77
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    78
    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
    79
            SurvivorAlignmentTestMain.WHITE_BOX.getUintxVMFlag(
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    80
                    "G1HeapRegionSize")).orElse(-1L);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    81
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    82
    /**
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    83
     * Min size of free chunk in CMS generation.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    84
     * An object allocated in CMS generation will at least occupy this amount
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    85
     * of bytes.
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 static final long CMS_MIN_FREE_CHUNK_SIZE
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    88
            = 3L * Unsafe.ADDRESS_SIZE;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    89
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    90
    private static final AlignmentHelper EDEN_SPACE_HELPER;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    91
    private static final AlignmentHelper SURVIVOR_SPACE_HELPER;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    92
    private static final AlignmentHelper TENURED_SPACE_HELPER;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    93
    /**
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    94
     * 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
    95
     */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
    96
    private final long memoryToFill;
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
     * 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
    99
     */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   100
    private final long objectSize;
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
     * 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
   103
     * space.
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 long actualObjectSize;
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
     * Storage for allocated objects.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   108
     */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   109
    private final Object[] garbage;
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
     * 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
   112
     * run.
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
    private final HeapSpace testedSpace;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   115
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   116
    private long[] baselinedThreadMemoryUsage = null;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   117
    private long[] threadIds = null;
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
    /**
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   120
     * 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
   121
     * {@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
   122
     *
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   123
     * 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
   124
     * 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
   125
     * 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
   126
     * {@code ObjectAlignmentInBytes} value.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   127
     *
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   128
     * In CMS generation we can't allocate less then {@code MinFreeChunk} value,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   129
     * for other CGs we expect that object of size {@code MIN_OBJECT_SIZE}
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   130
     * could be allocated as it is (of course, its size could be aligned
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   131
     * according to alignment value used in a particular space).
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   132
     *
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   133
     * 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
   134
     * 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
   135
     * 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
   136
     * {@code G1HeapRegionSize} value.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   137
     */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   138
    static {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   139
        AlignmentHelper edenHelper = null;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   140
        AlignmentHelper survivorHelper = null;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   141
        AlignmentHelper tenuredHelper = null;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   142
        for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   143
            switch (pool.getName()) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   144
                case SurvivorAlignmentTestMain.DEF_NEW_EDEN:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   145
                case SurvivorAlignmentTestMain.PAR_NEW_EDEN:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   146
                case SurvivorAlignmentTestMain.PS_EDEN:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   147
                    Asserts.assertNull(edenHelper,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   148
                            "Only one bean for eden space is expected.");
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   149
                    edenHelper = new AlignmentHelper(
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   150
                            AlignmentHelper.OBJECT_ALIGNMENT_IN_BYTES,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   151
                            AlignmentHelper.OBJECT_ALIGNMENT_IN_BYTES,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   152
                            AlignmentHelper.MIN_OBJECT_SIZE, pool);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   153
                    break;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   154
                case SurvivorAlignmentTestMain.G1_EDEN:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   155
                    Asserts.assertNull(edenHelper,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   156
                            "Only one bean for eden space is expected.");
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   157
                    edenHelper = new AlignmentHelper(
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   158
                            SurvivorAlignmentTestMain.G1_HEAP_REGION_SIZE,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   159
                            AlignmentHelper.OBJECT_ALIGNMENT_IN_BYTES,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   160
                            AlignmentHelper.MIN_OBJECT_SIZE, pool);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   161
                    break;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   162
                case SurvivorAlignmentTestMain.DEF_NEW_SURVIVOR:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   163
                case SurvivorAlignmentTestMain.PAR_NEW_SURVIVOR:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   164
                case SurvivorAlignmentTestMain.PS_SURVIVOR:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   165
                    Asserts.assertNull(survivorHelper,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   166
                            "Only one bean for survivor space is expected.");
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   167
                    survivorHelper = new AlignmentHelper(
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   168
                            AlignmentHelper.OBJECT_ALIGNMENT_IN_BYTES,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   169
                            AlignmentHelper.SURVIVOR_ALIGNMENT_IN_BYTES,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   170
                            AlignmentHelper.MIN_OBJECT_SIZE, pool);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   171
                    break;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   172
                case SurvivorAlignmentTestMain.G1_SURVIVOR:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   173
                    Asserts.assertNull(survivorHelper,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   174
                            "Only one bean for survivor space is expected.");
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   175
                    survivorHelper = new AlignmentHelper(
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   176
                            SurvivorAlignmentTestMain.G1_HEAP_REGION_SIZE,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   177
                            AlignmentHelper.SURVIVOR_ALIGNMENT_IN_BYTES,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   178
                            AlignmentHelper.MIN_OBJECT_SIZE, pool);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   179
                    break;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   180
                case SurvivorAlignmentTestMain.SERIAL_TENURED:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   181
                case SurvivorAlignmentTestMain.PS_TENURED:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   182
                case SurvivorAlignmentTestMain.G1_TENURED:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   183
                    Asserts.assertNull(tenuredHelper,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   184
                            "Only one bean for tenured space is expected.");
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   185
                    tenuredHelper = new AlignmentHelper(
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   186
                            AlignmentHelper.OBJECT_ALIGNMENT_IN_BYTES,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   187
                            AlignmentHelper.OBJECT_ALIGNMENT_IN_BYTES,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   188
                            AlignmentHelper.MIN_OBJECT_SIZE, pool);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   189
                    break;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   190
                case SurvivorAlignmentTestMain.CMS_TENURED:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   191
                    Asserts.assertNull(tenuredHelper,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   192
                            "Only one bean for tenured space is expected.");
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   193
                    tenuredHelper = new AlignmentHelper(
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   194
                            AlignmentHelper.OBJECT_ALIGNMENT_IN_BYTES,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   195
                            AlignmentHelper.OBJECT_ALIGNMENT_IN_BYTES,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   196
                            SurvivorAlignmentTestMain.CMS_MIN_FREE_CHUNK_SIZE,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   197
                            pool);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   198
                    break;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   199
            }
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
        EDEN_SPACE_HELPER = Objects.requireNonNull(edenHelper,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   202
                "AlignmentHelper for eden space should be initialized.");
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   203
        SURVIVOR_SPACE_HELPER = Objects.requireNonNull(survivorHelper,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   204
                "AlignmentHelper for survivor space should be initialized.");
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   205
        TENURED_SPACE_HELPER = Objects.requireNonNull(tenuredHelper,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   206
                "AlignmentHelper for tenured space should be initialized.");
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   207
    }
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
     * Returns an SurvivorAlignmentTestMain instance constructed using CLI
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   210
     * options.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   211
     *
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   212
     * Following options are expected:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   213
     * <ul>
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   214
     *     <li>memoryToFill</li>
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   215
     *     <li>objectSize</li>
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   216
     * </ul>
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   217
     *
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   218
     * 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
   219
     */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   220
    public static SurvivorAlignmentTestMain fromArgs(String[] args) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   221
        Asserts.assertEQ(args.length, 3, "Expected three arguments: "
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   222
                + "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
   223
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   224
        long memoryToFill = parseSize(args[0]);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   225
        long objectSize = Math.max(parseSize(args[1]),
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   226
                AlignmentHelper.MIN_ARRAY_SIZE);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   227
        HeapSpace testedSpace = HeapSpace.valueOf(args[2]);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   228
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   229
        return new SurvivorAlignmentTestMain(memoryToFill, objectSize,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   230
                testedSpace);
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
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
     * 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
   235
     * &lt;integer&gt;&lt;multiplier&gt;.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   236
     */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   237
    private static long parseSize(String sizeString) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   238
        Matcher matcher = SIZE_REGEX.matcher(sizeString);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   239
        Asserts.assertTrue(matcher.matches(),
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   240
                "sizeString should have following format \"[0-9]+([MBK])?\"");
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   241
        long size = Long.valueOf(matcher.group("size"));
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
        if (matcher.group("multiplier") != null) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   244
            long K = 1024L;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   245
            // fall through multipliers
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   246
            switch (matcher.group("multiplier").toLowerCase()) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   247
                case "g":
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   248
                    size *= K;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   249
                case "m":
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   250
                    size *= K;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   251
                case "k":
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   252
                    size *= K;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   253
            }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   254
        }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   255
        return size;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   256
    }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   257
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   258
    private SurvivorAlignmentTestMain(long memoryToFill, long objectSize,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   259
            HeapSpace testedSpace) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   260
        this.objectSize = objectSize;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   261
        this.memoryToFill = memoryToFill;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   262
        this.testedSpace = testedSpace;
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
        AlignmentHelper helper = SurvivorAlignmentTestMain.EDEN_SPACE_HELPER;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   265
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   266
        this.actualObjectSize = helper.getObjectSizeInThisSpace(
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   267
                this.objectSize);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   268
        int arrayLength = helper.getObjectsCount(memoryToFill, this.objectSize);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   269
        garbage = new Object[arrayLength];
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
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   272
    /**
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   273
     * Allocate byte arrays to fill {@code memoryToFill} memory.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   274
     */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   275
    public void allocate() {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   276
        int byteArrayLength = Math.max((int) (objectSize
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   277
                - Unsafe.ARRAY_BYTE_BASE_OFFSET), 0);
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
        for (int i = 0; i < garbage.length; i++) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   280
            garbage[i] = new byte[byteArrayLength];
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   281
        }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   282
    }
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
    /**
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   285
     * Release memory occupied after {@code allocate} call.
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
    public void release() {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   288
        for (int i = 0; i < garbage.length; i++) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   289
            garbage[i] = null;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   290
        }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   291
    }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   292
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
     * 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
   295
     * objects referenced from {@code garbage} array.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   296
     */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   297
    public long getExpectedMemoryUsage() {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   298
        AlignmentHelper alignmentHelper = getAlignmentHelper(testedSpace);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   299
        return alignmentHelper.getExpectedMemoryUsage(objectSize,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   300
                garbage.length);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   301
    }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   302
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   303
    /**
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   304
     * 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
   305
     * {@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
   306
     */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   307
    public void verifyMemoryUsage(long expectedUsage) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   308
        AlignmentHelper alignmentHelper = getAlignmentHelper(testedSpace);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   309
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   310
        long actualMemoryUsage = alignmentHelper.getActualMemoryUsage();
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   311
        boolean otherThreadsAllocatedMemory = areOtherThreadsAllocatedMemory();
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   312
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   313
        long memoryUsageDiff = Math.abs(actualMemoryUsage - expectedUsage);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   314
        long maxAllowedUsageDiff
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   315
                = alignmentHelper.getAllowedMemoryUsageDeviation(expectedUsage);
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
        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
   318
        System.out.println("Allocated objects count: " + garbage.length);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   319
        System.out.println("Desired object size: " + objectSize);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   320
        System.out.println("Actual object size: " + actualObjectSize);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   321
        System.out.println("Expected object size in space: "
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   322
                + alignmentHelper.getObjectSizeInThisSpace(objectSize));
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   323
        System.out.println("Expected memory usage: " + expectedUsage);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   324
        System.out.println("Actual memory usage: " + actualMemoryUsage);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   325
        System.out.println("Memory usage diff: " + memoryUsageDiff);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   326
        System.out.println("Max allowed usage diff: " + maxAllowedUsageDiff);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   327
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   328
        if (memoryUsageDiff > maxAllowedUsageDiff
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   329
                && otherThreadsAllocatedMemory) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   330
            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
   331
                    + "like someone else allocated objects");
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   332
            return;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   333
        }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   334
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   335
        Asserts.assertLTE(memoryUsageDiff, maxAllowedUsageDiff,
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   336
                "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
   337
                        "more then " + maxAllowedUsageDiff);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   338
    }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   339
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   340
    /**
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   341
     * Baselines amount of memory allocated by each thread.
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
    public void baselineMemoryAllocation() {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   344
        ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   345
        threadIds = bean.getAllThreadIds();
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   346
        baselinedThreadMemoryUsage = bean.getThreadAllocatedBytes(threadIds);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   347
    }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   348
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
     * 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
   351
     * after baselinedThreadMemoryUsage call.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   352
     *
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   353
     * 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
   354
     * {@code false}.
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
    public boolean areOtherThreadsAllocatedMemory() {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   357
        if (baselinedThreadMemoryUsage == null) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   358
            return false;
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
        ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   362
        long currentMemoryAllocation[]
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   363
                = bean.getThreadAllocatedBytes(threadIds);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   364
        boolean otherThreadsAllocatedMemory = false;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   365
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   366
        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
   367
        for (int i = 0; i < threadIds.length; i++) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   368
            System.out.format("Thread %d%nbaseline allocation: %d"
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   369
                            + "%ncurrent allocation:%d%n", threadIds[i],
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   370
                    baselinedThreadMemoryUsage[i], currentMemoryAllocation[i]);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   371
            System.out.println(bean.getThreadInfo(threadIds[i]));
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   372
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   373
            long bytesAllocated = Math.abs(currentMemoryAllocation[i]
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   374
                    - baselinedThreadMemoryUsage[i]);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   375
            if (bytesAllocated > 0
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   376
                    && threadIds[i] != Thread.currentThread().getId()) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   377
                otherThreadsAllocatedMemory = true;
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
        return otherThreadsAllocatedMemory;
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
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   384
    @Override
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   385
    public String toString() {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   386
        StringBuilder builder = new StringBuilder();
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   387
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   388
        builder.append(String.format("SurvivorAlignmentTestMain info:%n"));
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   389
        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
   390
        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
   391
        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
   392
                garbage.length));
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
        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
   395
        for (HeapSpace heapSpace: HeapSpace.values()) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   396
            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
   397
                    getAlignmentHelper(heapSpace)));
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   398
        }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   399
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   400
        return builder.toString();
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   401
    }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   402
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   403
    /**
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   404
     * Returns {@code AlignmentHelper} for a space {@code heapSpace}.
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   405
     */
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   406
    public static AlignmentHelper getAlignmentHelper(HeapSpace heapSpace) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   407
        switch (heapSpace) {
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   408
            case EDEN:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   409
                return SurvivorAlignmentTestMain.EDEN_SPACE_HELPER;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   410
            case SURVIVOR:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   411
                return SurvivorAlignmentTestMain.SURVIVOR_SPACE_HELPER;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   412
            case TENURED:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   413
                return SurvivorAlignmentTestMain.TENURED_SPACE_HELPER;
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   414
            default:
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   415
                throw new Error("Unexpected heap space: " + heapSpace);
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   416
        }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   417
    }
9c0e6a049e1f 8037968: Add tests on alignment of objects copied to survivor space
fzhinkin
parents:
diff changeset
   418
}