hotspot/test/compiler/unsafe/UnsafeOffHeapBooleanTest.java
author zmajo
Tue, 30 Aug 2016 09:30:16 +0200
changeset 41052 3362c4368286
permissions -rw-r--r--
8161720: Better byte behavior for off-heap data Summary: Normalize boolean values read with Unsafe. Reviewed-by: aph, simonis, jrose, psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41052
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
     1
/*
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
     4
 *
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
     8
 *
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    13
 * accompanied this code).
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    14
 *
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    18
 *
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    21
 * questions.
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    22
 */
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    23
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    24
/*
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    25
 * @test
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    26
 * @bug 8161720
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    27
 * @modules java.base/jdk.internal.misc
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    28
 * @run main/othervm -Xint UnsafeOffHeapBooleanTest 1
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    29
 * @run main/othervm -XX:+TieredCompilation -XX:TieredStopAtLevel=3 -Xbatch  UnsafeOffHeapBooleanTest 20000
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    30
 * @run main/othervm -XX:-TieredCompilation -Xbatch UnsafeOffHeapBooleanTest 20000
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    31
 */
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    32
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    33
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    34
import java.lang.reflect.Field;
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    35
import jdk.internal.misc.Unsafe;
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    36
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    37
public class UnsafeOffHeapBooleanTest {
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    38
    static boolean bool0 = false, bool1 = false, result = false;
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    39
    static Unsafe UNSAFE = Unsafe.getUnsafe();
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    40
    static long offHeapMemory;
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    41
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    42
    public static void test() {
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    43
        // Write two bytes to the off-heap memory location, both
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    44
        // bytes correspond to the boolean value 'true'.
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    45
        UNSAFE.putShort(null, offHeapMemory, (short)0x0204);
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    46
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    47
        // Read two bytes from the storage allocated above (as booleans).
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    48
        bool0 = UNSAFE.getBoolean(null, offHeapMemory + 0);
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    49
        bool1 = UNSAFE.getBoolean(null, offHeapMemory + 1);
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    50
        result = bool0 & bool1;
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    51
    }
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    52
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    53
    public static void main(String args[]) {
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    54
        System.out.println("### Test started");
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    55
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    56
        if (args.length != 1) {
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    57
            throw new RuntimeException("### Test failure: test called with incorrect number of arguments");
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    58
        }
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    59
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    60
        // Allocate two bytes of storage.
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    61
        offHeapMemory = UNSAFE.allocateMemory(2);
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    62
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    63
        try {
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    64
            for (int i = 0; i < Integer.parseInt(args[0]); i++) {
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    65
                test();
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    66
            }
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    67
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    68
            // Check if the two 'true' boolean values were normalized
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    69
            // (i.e., reduced from the range 1...255 to 1).
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    70
            if (!bool0 || !bool1 || !result) {
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    71
                System.out.println("Some of the results below are wrong");
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    72
                System.out.println("bool0 is: " + bool0);
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    73
                System.out.println("bool1 is: " + bool1);
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    74
                System.out.println("bool0 & bool1 is: " + result);
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    75
                System.out.println("===================================");
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    76
                throw new RuntimeException("### Test failed");
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    77
            } else {
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    78
                System.out.println("Test generated correct results");
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    79
                System.out.println("bool0 is: " + bool0);
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    80
                System.out.println("bool1 is: " + bool1);
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    81
                System.out.println("bool0 & bool1 is: " + result);
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    82
                System.out.println("===================================");
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    83
            }
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    84
        } catch (NumberFormatException e) {
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    85
            throw new RuntimeException("### Test failure: test called with incorrectly formatted parameter");
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    86
        }
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    87
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    88
        UNSAFE.freeMemory(offHeapMemory);
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    89
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    90
        System.out.println("### Test passed");
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    91
    }
3362c4368286 8161720: Better byte behavior for off-heap data
zmajo
parents:
diff changeset
    92
}