test/runtime/contended/Basic.java

changeset 5145
5e3573e08a83
parent 0
f90c822e73f8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/runtime/contended/Basic.java	Mon May 20 15:43:50 2013 +0400
     1.3 @@ -0,0 +1,302 @@
     1.4 +/*
     1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +import java.io.BufferedReader;
    1.28 +import java.io.InputStreamReader;
    1.29 +import java.lang.Class;
    1.30 +import java.lang.String;
    1.31 +import java.lang.System;
    1.32 +import java.lang.management.ManagementFactory;
    1.33 +import java.lang.management.RuntimeMXBean;
    1.34 +import java.util.ArrayList;
    1.35 +import java.util.List;
    1.36 +import java.util.concurrent.CyclicBarrier;
    1.37 +import java.util.regex.Matcher;
    1.38 +import java.util.regex.Pattern;
    1.39 +import java.lang.reflect.Field;
    1.40 +import java.lang.reflect.Modifier;
    1.41 +import sun.misc.Unsafe;
    1.42 +import sun.misc.Contended;
    1.43 +
    1.44 +/*
    1.45 + * @test
    1.46 + * @bug     8003985
    1.47 + * @summary Support Contended Annotation - JEP 142
    1.48 + *
    1.49 + * @run main/othervm -XX:-RestrictContended Basic
    1.50 + */
    1.51 +public class Basic {
    1.52 +
    1.53 +    private static final Unsafe U;
    1.54 +    private static int ADDRESS_SIZE;
    1.55 +    private static int HEADER_SIZE;
    1.56 +
    1.57 +    static {
    1.58 +        // steal Unsafe
    1.59 +        try {
    1.60 +            Field unsafe = Unsafe.class.getDeclaredField("theUnsafe");
    1.61 +            unsafe.setAccessible(true);
    1.62 +            U = (Unsafe) unsafe.get(null);
    1.63 +        } catch (NoSuchFieldException | IllegalAccessException e) {
    1.64 +            throw new IllegalStateException(e);
    1.65 +        }
    1.66 +
    1.67 +        // When running with CompressedOops on 64-bit platform, the address size
    1.68 +        // reported by Unsafe is still 8, while the real reference fields are 4 bytes long.
    1.69 +        // Try to guess the reference field size with this naive trick.
    1.70 +        try {
    1.71 +            long off1 = U.objectFieldOffset(CompressedOopsClass.class.getField("obj1"));
    1.72 +            long off2 = U.objectFieldOffset(CompressedOopsClass.class.getField("obj2"));
    1.73 +            ADDRESS_SIZE = (int) Math.abs(off2 - off1);
    1.74 +            HEADER_SIZE = (int) Math.min(off1, off2);
    1.75 +        } catch (NoSuchFieldException e) {
    1.76 +            ADDRESS_SIZE = -1;
    1.77 +        }
    1.78 +    }
    1.79 +
    1.80 +    static class CompressedOopsClass {
    1.81 +        public Object obj1;
    1.82 +        public Object obj2;
    1.83 +    }
    1.84 +
    1.85 +    public static boolean arePaddedPairwise(Class klass, String field1, String field2) throws Exception {
    1.86 +        Field f1 = klass.getDeclaredField(field1);
    1.87 +        Field f2 = klass.getDeclaredField(field2);
    1.88 +
    1.89 +        if (isStatic(f1) != isStatic(f2)) {
    1.90 +            return true; // these guys are in naturally disjoint locations
    1.91 +        }
    1.92 +
    1.93 +        int diff = offset(f1) - offset(f2);
    1.94 +        if (diff < 0) {
    1.95 +            // f1 is first
    1.96 +            return (offset(f2) - (offset(f1) + getSize(f1))) > 64;
    1.97 +        } else {
    1.98 +            // f2 is first
    1.99 +            return (offset(f1) - (offset(f2) + getSize(f2))) > 64;
   1.100 +        }
   1.101 +    }
   1.102 +
   1.103 +    public static boolean isPadded(Class klass, String field1) throws Exception {
   1.104 +        Field f1 = klass.getDeclaredField(field1);
   1.105 +
   1.106 +        if (isStatic(f1)) {
   1.107 +            return offset(f1) > 128 + 64;
   1.108 +        }
   1.109 +
   1.110 +        return offset(f1) > 64;
   1.111 +    }
   1.112 +
   1.113 +    public static boolean sameLayout(Class klass1, Class klass2) throws Exception {
   1.114 +        for (Field f1 : klass1.getDeclaredFields()) {
   1.115 +            Field f2 = klass2.getDeclaredField(f1.getName());
   1.116 +            if (offset(f1) != offset(f2)) {
   1.117 +                return false;
   1.118 +            }
   1.119 +        }
   1.120 +
   1.121 +        for (Field f2 : klass1.getDeclaredFields()) {
   1.122 +            Field f1 = klass2.getDeclaredField(f2.getName());
   1.123 +            if (offset(f1) != offset(f2)) {
   1.124 +                return false;
   1.125 +            }
   1.126 +        }
   1.127 +
   1.128 +        return true;
   1.129 +    }
   1.130 +
   1.131 +    public static boolean isStatic(Field field) {
   1.132 +        return Modifier.isStatic(field.getModifiers());
   1.133 +    }
   1.134 +
   1.135 +    public static int offset(Field field) {
   1.136 +        if (isStatic(field)) {
   1.137 +            return (int) U.staticFieldOffset(field);
   1.138 +        } else {
   1.139 +            return (int) U.objectFieldOffset(field);
   1.140 +        }
   1.141 +    }
   1.142 +
   1.143 +    public static int getSize(Field field) {
   1.144 +        Class type = field.getType();
   1.145 +        if (type == byte.class)    { return 1; }
   1.146 +        if (type == boolean.class) { return 1; }
   1.147 +        if (type == short.class)   { return 2; }
   1.148 +        if (type == char.class)    { return 2; }
   1.149 +        if (type == int.class)     { return 4; }
   1.150 +        if (type == float.class)   { return 4; }
   1.151 +        if (type == long.class)    { return 8; }
   1.152 +        if (type == double.class)  { return 8; }
   1.153 +        return ADDRESS_SIZE;
   1.154 +    }
   1.155 +
   1.156 +    public static void main(String[] args) throws Exception {
   1.157 +        boolean endResult = true;
   1.158 +
   1.159 +        // --------------- INSTANCE FIELDS ---------------------
   1.160 +
   1.161 +        if (arePaddedPairwise(Test1.class, "int1", "int2") ||
   1.162 +                isPadded(Test1.class, "int1") ||
   1.163 +                isPadded(Test1.class, "int2")) {
   1.164 +            System.err.println("Test1 failed");
   1.165 +            endResult &= false;
   1.166 +        }
   1.167 +
   1.168 +        if (!arePaddedPairwise(Test2.class, "int1", "int2") ||
   1.169 +                !isPadded(Test2.class, "int1") ||
   1.170 +                isPadded(Test2.class, "int2")) {
   1.171 +            System.err.println("Test2 failed");
   1.172 +            endResult &= false;
   1.173 +        }
   1.174 +
   1.175 +        if (!arePaddedPairwise(Test3.class, "int1", "int2") ||
   1.176 +                !isPadded(Test3.class, "int1") ||
   1.177 +                !isPadded(Test3.class, "int2")) {
   1.178 +            System.err.println("Test3 failed");
   1.179 +            endResult &= false;
   1.180 +        }
   1.181 +
   1.182 +        if (arePaddedPairwise(Test4.class, "int1", "int2") ||
   1.183 +                !isPadded(Test4.class, "int1") ||
   1.184 +                !isPadded(Test4.class, "int2")) {
   1.185 +            System.err.println("Test4 failed");
   1.186 +            endResult &= false;
   1.187 +        }
   1.188 +
   1.189 +        if (!arePaddedPairwise(Test5.class, "int1", "int2") ||
   1.190 +                !isPadded(Test5.class, "int1") ||
   1.191 +                !isPadded(Test5.class, "int2")) {
   1.192 +            System.err.println("Test5 failed");
   1.193 +            endResult &= false;
   1.194 +        }
   1.195 +
   1.196 +        if (!arePaddedPairwise(Test6.class, "int1", "int2") ||
   1.197 +                !isPadded(Test6.class, "int1") ||
   1.198 +                !isPadded(Test6.class, "int2")) {
   1.199 +            System.err.println("Test6 failed");
   1.200 +            endResult &= false;
   1.201 +        }
   1.202 +
   1.203 +        if (arePaddedPairwise(Test7.class, "int1", "int2") ||
   1.204 +                !isPadded(Test7.class, "int1") ||
   1.205 +                !isPadded(Test7.class, "int2")) {
   1.206 +            System.err.println("Test7 failed");
   1.207 +            endResult &= false;
   1.208 +        }
   1.209 +
   1.210 +        if (!arePaddedPairwise(Test8.class, "int1", "int2") ||
   1.211 +                !isPadded(Test8.class, "int1") ||
   1.212 +                !isPadded(Test8.class, "int2")) {
   1.213 +            System.err.println("Test8 failed");
   1.214 +            endResult &= false;
   1.215 +        }
   1.216 +
   1.217 +        if (!arePaddedPairwise(Test9.class, "int1", "int2") ||
   1.218 +                !isPadded(Test9.class, "int1") ||
   1.219 +                !isPadded(Test9.class, "int2")) {
   1.220 +            System.err.println("Test9 failed");
   1.221 +            endResult &= false;
   1.222 +        }
   1.223 +
   1.224 +        if (!sameLayout(Test4.class, Test7.class)) {
   1.225 +            System.err.println("Test4 and Test7 have different layouts");
   1.226 +            endResult &= false;
   1.227 +        }
   1.228 +
   1.229 +        if (!sameLayout(Test5.class, Test6.class)) {
   1.230 +            System.err.println("Test5 and Test6 have different layouts");
   1.231 +            endResult &= false;
   1.232 +        }
   1.233 +
   1.234 +        if (!sameLayout(Test8.class, Test9.class)) {
   1.235 +            System.err.println("Test8 and Test9 have different layouts");
   1.236 +            endResult &= false;
   1.237 +        }
   1.238 +
   1.239 +        System.out.println(endResult ? "Test PASSES" : "Test FAILS");
   1.240 +        if (!endResult) {
   1.241 +           throw new Error("Test failed");
   1.242 +        }
   1.243 +    }
   1.244 +
   1.245 +    // ----------------------------------- INSTANCE FIELDS -----------------------------------------
   1.246 +
   1.247 +    // naturally packed
   1.248 +    public static class Test1 {
   1.249 +                                 private int int1;
   1.250 +                                 private int int2;
   1.251 +    }
   1.252 +
   1.253 +    // int1 is padded
   1.254 +    public static class Test2 {
   1.255 +        @Contended               private int int1;
   1.256 +                                 private int int2;
   1.257 +    }
   1.258 +
   1.259 +    // both fields are padded
   1.260 +    public static class Test3 {
   1.261 +        @Contended               private int int1;
   1.262 +        @Contended               private int int2;
   1.263 +    }
   1.264 +
   1.265 +    // fields are padded in the singular group
   1.266 +    public static class Test4 {
   1.267 +        @Contended("sameGroup")  private int int1;
   1.268 +        @Contended("sameGroup")  private int int2;
   1.269 +    }
   1.270 +
   1.271 +    // fields are padded in disjoint groups
   1.272 +    public static class Test5 {
   1.273 +        @Contended("diffGroup1") private int int1;
   1.274 +        @Contended("diffGroup2") private int int2;
   1.275 +    }
   1.276 +
   1.277 +    // fields are padded in disjoint groups
   1.278 +    public static class Test6 {
   1.279 +        @Contended               private int int1;
   1.280 +        @Contended("diffGroup2") private int int2;
   1.281 +    }
   1.282 +
   1.283 +    // fields are padded in the singular group
   1.284 +    @Contended
   1.285 +    public static class Test7 {
   1.286 +                                 private int int1;
   1.287 +                                 private int int2;
   1.288 +    }
   1.289 +
   1.290 +    // all fields are padded as the group, and one field is padded specifically
   1.291 +    @Contended
   1.292 +    public static class Test8 {
   1.293 +        @Contended               private int int1;
   1.294 +                                 private int int2;
   1.295 +    }
   1.296 +
   1.297 +    // all fields are padded as the group, and one field is padded specifically
   1.298 +    @Contended
   1.299 +    public static class Test9 {
   1.300 +        @Contended("group")      private int int1;
   1.301 +                                 private int int2;
   1.302 +    }
   1.303 +
   1.304 +}
   1.305 +

mercurial