test/runtime/contended/DefaultValue.java

Mon, 20 May 2013 15:43:50 +0400

author
shade
date
Mon, 20 May 2013 15:43:50 +0400
changeset 5145
5e3573e08a83
parent 0
f90c822e73f8
permissions
-rw-r--r--

8014871: Move @Contended regression tests to the same place
Summary: Move the missing test to appropriate location.
Reviewed-by: dholmes, sla

     1 /*
     2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 import java.io.BufferedReader;
    25 import java.io.InputStreamReader;
    26 import java.lang.Class;
    27 import java.lang.String;
    28 import java.lang.System;
    29 import java.lang.management.ManagementFactory;
    30 import java.lang.management.RuntimeMXBean;
    31 import java.util.ArrayList;
    32 import java.util.List;
    33 import java.util.concurrent.CyclicBarrier;
    34 import java.util.regex.Matcher;
    35 import java.util.regex.Pattern;
    36 import java.lang.reflect.Field;
    37 import java.lang.reflect.Modifier;
    38 import sun.misc.Unsafe;
    39 import sun.misc.Contended;
    41 /*
    42  * @test
    43  * @bug     8014509
    44  * @summary \@Contended: explicit default value behaves differently from the implicit value
    45  *
    46  * @run main/othervm -XX:-RestrictContended DefaultValue
    47  */
    48 public class DefaultValue {
    50     private static final Unsafe U;
    51     private static int ADDRESS_SIZE;
    52     private static int HEADER_SIZE;
    54     static {
    55         // steal Unsafe
    56         try {
    57             Field unsafe = Unsafe.class.getDeclaredField("theUnsafe");
    58             unsafe.setAccessible(true);
    59             U = (Unsafe) unsafe.get(null);
    60         } catch (NoSuchFieldException | IllegalAccessException e) {
    61             throw new IllegalStateException(e);
    62         }
    64         // When running with CompressedOops on 64-bit platform, the address size
    65         // reported by Unsafe is still 8, while the real reference fields are 4 bytes long.
    66         // Try to guess the reference field size with this naive trick.
    67         try {
    68             long off1 = U.objectFieldOffset(CompressedOopsClass.class.getField("obj1"));
    69             long off2 = U.objectFieldOffset(CompressedOopsClass.class.getField("obj2"));
    70             ADDRESS_SIZE = (int) Math.abs(off2 - off1);
    71             HEADER_SIZE = (int) Math.min(off1, off2);
    72         } catch (NoSuchFieldException e) {
    73             ADDRESS_SIZE = -1;
    74         }
    75     }
    77     static class CompressedOopsClass {
    78         public Object obj1;
    79         public Object obj2;
    80     }
    82     public static boolean arePaddedPairwise(Class klass, String field1, String field2) throws Exception {
    83         Field f1 = klass.getField(field1);
    84         Field f2 = klass.getField(field2);
    86         int diff = offset(f1) - offset(f2);
    87         if (diff < 0) {
    88             // f1 is first
    89             return (offset(f2) - (offset(f1) + getSize(f1))) > 64;
    90         } else {
    91             // f2 is first
    92             return (offset(f1) - (offset(f2) + getSize(f2))) > 64;
    93         }
    94     }
    96     public static boolean sameLayout(Class klass1, Class klass2) throws Exception {
    97         for (Field f1 : klass1.getDeclaredFields()) {
    98             Field f2 = klass2.getDeclaredField(f1.getName());
    99             if (offset(f1) != offset(f2)) {
   100                 return false;
   101             }
   102         }
   104         for (Field f2 : klass1.getDeclaredFields()) {
   105             Field f1 = klass2.getDeclaredField(f2.getName());
   106             if (offset(f1) != offset(f2)) {
   107                 return false;
   108             }
   109         }
   111         return true;
   112     }
   114     public static boolean isStatic(Field field) {
   115         return Modifier.isStatic(field.getModifiers());
   116     }
   118     public static int offset(Field field) {
   119         if (isStatic(field)) {
   120             return (int) U.staticFieldOffset(field);
   121         } else {
   122             return (int) U.objectFieldOffset(field);
   123         }
   124     }
   126     public static int getSize(Field field) {
   127         Class type = field.getType();
   128         if (type == byte.class)    { return 1; }
   129         if (type == boolean.class) { return 1; }
   130         if (type == short.class)   { return 2; }
   131         if (type == char.class)    { return 2; }
   132         if (type == int.class)     { return 4; }
   133         if (type == float.class)   { return 4; }
   134         if (type == long.class)    { return 8; }
   135         if (type == double.class)  { return 8; }
   136         return ADDRESS_SIZE;
   137     }
   139     public static void main(String[] args) throws Exception {
   140         boolean endResult = true;
   142         if (!arePaddedPairwise(R1.class, "int1", "int2")) {
   143             System.err.println("R1 failed");
   144             endResult &= false;
   145         }
   147         if (!arePaddedPairwise(R2.class, "int1", "int2")) {
   148             System.err.println("R2 failed");
   149             endResult &= false;
   150         }
   152         if (!arePaddedPairwise(R3.class, "int1", "int2")) {
   153             System.err.println("R3 failed");
   154             endResult &= false;
   155         }
   157         System.out.println(endResult ? "Test PASSES" : "Test FAILS");
   158         if (!endResult) {
   159            throw new Error("Test failed");
   160         }
   161     }
   163     public static class R1 {
   164         @Contended
   165         public int int1;
   166         @Contended
   167         public int int2;
   168     }
   170     public static class R2 {
   171         @Contended("")
   172         public int int1;
   173         @Contended("")
   174         public int int2;
   175     }
   177     public static class R3 {
   178         @Contended()
   179         public int int1;
   180         @Contended()
   181         public int int2;
   182     }
   184 }

mercurial