rev 10304 : [mq]: jake_pre_integ

   1 /*
   2  * Copyright (c) 2013, 2015, 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  */
  23 
  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 jdk.internal.vm.annotation.Contended;
  40 
  41 /*
  42  * @test
  43  * @bug     8003985
  44  * @summary Support Contended Annotation - JEP 142
  45  * @modules java.base/sun.misc
  46  * @run main/othervm -XX:-RestrictContended Basic
  47  */
  48 public class Basic {
  49 
  50     private static final Unsafe U;
  51     private static int ADDRESS_SIZE;
  52     private static int HEADER_SIZE;
  53 
  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         }
  63 
  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     }
  76 
  77     static class CompressedOopsClass {
  78         public Object obj1;
  79         public Object obj2;
  80     }
  81 
  82     public static boolean arePaddedPairwise(Class klass, String field1, String field2) throws Exception {
  83         Field f1 = klass.getDeclaredField(field1);
  84         Field f2 = klass.getDeclaredField(field2);
  85 
  86         if (isStatic(f1) != isStatic(f2)) {
  87             return true; // these guys are in naturally disjoint locations
  88         }
  89 
  90         int diff = offset(f1) - offset(f2);
  91         if (diff < 0) {
  92             // f1 is first
  93             return (offset(f2) - (offset(f1) + getSize(f1))) > 64;
  94         } else {
  95             // f2 is first
  96             return (offset(f1) - (offset(f2) + getSize(f2))) > 64;
  97         }
  98     }
  99 
 100     public static boolean isPadded(Class klass, String field1) throws Exception {
 101         Field f1 = klass.getDeclaredField(field1);
 102 
 103         if (isStatic(f1)) {
 104             return offset(f1) > 128 + 64;
 105         }
 106 
 107         return offset(f1) > 64;
 108     }
 109 
 110     public static boolean sameLayout(Class klass1, Class klass2) throws Exception {
 111         for (Field f1 : klass1.getDeclaredFields()) {
 112             Field f2 = klass2.getDeclaredField(f1.getName());
 113             if (offset(f1) != offset(f2)) {
 114                 return false;
 115             }
 116         }
 117 
 118         for (Field f2 : klass1.getDeclaredFields()) {
 119             Field f1 = klass2.getDeclaredField(f2.getName());
 120             if (offset(f1) != offset(f2)) {
 121                 return false;
 122             }
 123         }
 124 
 125         return true;
 126     }
 127 
 128     public static boolean isStatic(Field field) {
 129         return Modifier.isStatic(field.getModifiers());
 130     }
 131 
 132     public static int offset(Field field) {
 133         if (isStatic(field)) {
 134             return (int) U.staticFieldOffset(field);
 135         } else {
 136             return (int) U.objectFieldOffset(field);
 137         }
 138     }
 139 
 140     public static int getSize(Field field) {
 141         Class type = field.getType();
 142         if (type == byte.class)    { return 1; }
 143         if (type == boolean.class) { return 1; }
 144         if (type == short.class)   { return 2; }
 145         if (type == char.class)    { return 2; }
 146         if (type == int.class)     { return 4; }
 147         if (type == float.class)   { return 4; }
 148         if (type == long.class)    { return 8; }
 149         if (type == double.class)  { return 8; }
 150         return ADDRESS_SIZE;
 151     }
 152 
 153     public static void main(String[] args) throws Exception {
 154         boolean endResult = true;
 155 
 156         // --------------- INSTANCE FIELDS ---------------------
 157 
 158         if (arePaddedPairwise(Test1.class, "int1", "int2") ||
 159                 isPadded(Test1.class, "int1") ||
 160                 isPadded(Test1.class, "int2")) {
 161             System.err.println("Test1 failed");
 162             endResult &= false;
 163         }
 164 
 165         if (!arePaddedPairwise(Test2.class, "int1", "int2") ||
 166                 !isPadded(Test2.class, "int1") ||
 167                 isPadded(Test2.class, "int2")) {
 168             System.err.println("Test2 failed");
 169             endResult &= false;
 170         }
 171 
 172         if (!arePaddedPairwise(Test3.class, "int1", "int2") ||
 173                 !isPadded(Test3.class, "int1") ||
 174                 !isPadded(Test3.class, "int2")) {
 175             System.err.println("Test3 failed");
 176             endResult &= false;
 177         }
 178 
 179         if (arePaddedPairwise(Test4.class, "int1", "int2") ||
 180                 !isPadded(Test4.class, "int1") ||
 181                 !isPadded(Test4.class, "int2")) {
 182             System.err.println("Test4 failed");
 183             endResult &= false;
 184         }
 185 
 186         if (!arePaddedPairwise(Test5.class, "int1", "int2") ||
 187                 !isPadded(Test5.class, "int1") ||
 188                 !isPadded(Test5.class, "int2")) {
 189             System.err.println("Test5 failed");
 190             endResult &= false;
 191         }
 192 
 193         if (!arePaddedPairwise(Test6.class, "int1", "int2") ||
 194                 !isPadded(Test6.class, "int1") ||
 195                 !isPadded(Test6.class, "int2")) {
 196             System.err.println("Test6 failed");
 197             endResult &= false;
 198         }
 199 
 200         if (arePaddedPairwise(Test7.class, "int1", "int2") ||
 201                 !isPadded(Test7.class, "int1") ||
 202                 !isPadded(Test7.class, "int2")) {
 203             System.err.println("Test7 failed");
 204             endResult &= false;
 205         }
 206 
 207         if (!arePaddedPairwise(Test8.class, "int1", "int2") ||
 208                 !isPadded(Test8.class, "int1") ||
 209                 !isPadded(Test8.class, "int2")) {
 210             System.err.println("Test8 failed");
 211             endResult &= false;
 212         }
 213 
 214         if (!arePaddedPairwise(Test9.class, "int1", "int2") ||
 215                 !isPadded(Test9.class, "int1") ||
 216                 !isPadded(Test9.class, "int2")) {
 217             System.err.println("Test9 failed");
 218             endResult &= false;
 219         }
 220 
 221         if (!sameLayout(Test4.class, Test7.class)) {
 222             System.err.println("Test4 and Test7 have different layouts");
 223             endResult &= false;
 224         }
 225 
 226         if (!sameLayout(Test5.class, Test6.class)) {
 227             System.err.println("Test5 and Test6 have different layouts");
 228             endResult &= false;
 229         }
 230 
 231         if (!sameLayout(Test8.class, Test9.class)) {
 232             System.err.println("Test8 and Test9 have different layouts");
 233             endResult &= false;
 234         }
 235 
 236         System.out.println(endResult ? "Test PASSES" : "Test FAILS");
 237         if (!endResult) {
 238            throw new Error("Test failed");
 239         }
 240     }
 241 
 242     // ----------------------------------- INSTANCE FIELDS -----------------------------------------
 243 
 244     // naturally packed
 245     public static class Test1 {
 246                                  private int int1;
 247                                  private int int2;
 248     }
 249 
 250     // int1 is padded
 251     public static class Test2 {
 252         @Contended               private int int1;
 253                                  private int int2;
 254     }
 255 
 256     // both fields are padded
 257     public static class Test3 {
 258         @Contended               private int int1;
 259         @Contended               private int int2;
 260     }
 261 
 262     // fields are padded in the singular group
 263     public static class Test4 {
 264         @Contended("sameGroup")  private int int1;
 265         @Contended("sameGroup")  private int int2;
 266     }
 267 
 268     // fields are padded in disjoint groups
 269     public static class Test5 {
 270         @Contended("diffGroup1") private int int1;
 271         @Contended("diffGroup2") private int int2;
 272     }
 273 
 274     // fields are padded in disjoint groups
 275     public static class Test6 {
 276         @Contended               private int int1;
 277         @Contended("diffGroup2") private int int2;
 278     }
 279 
 280     // fields are padded in the singular group
 281     @Contended
 282     public static class Test7 {
 283                                  private int int1;
 284                                  private int int2;
 285     }
 286 
 287     // all fields are padded as the group, and one field is padded specifically
 288     @Contended
 289     public static class Test8 {
 290         @Contended               private int int1;
 291                                  private int int2;
 292     }
 293 
 294     // all fields are padded as the group, and one field is padded specifically
 295     @Contended
 296     public static class Test9 {
 297         @Contended("group")      private int int1;
 298                                  private int int2;
 299     }
 300 
 301 }
 302 
--- EOF ---