1 /* 2 * Copyright (c) 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 /* 25 * @test 26 * @bug 8143628 27 * @summary Test unsafe access for float 28 * @modules java.base/jdk.internal.misc 29 * @run testng/othervm -Diters=100 -Xint JdkInternalMiscUnsafeAccessTestFloat 30 * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 JdkInternalMiscUnsafeAccessTestFloat 31 * @run testng/othervm -Diters=20000 -XX:-TieredCompilation JdkInternalMiscUnsafeAccessTestFloat 32 * @run testng/othervm -Diters=20000 JdkInternalMiscUnsafeAccessTestFloat 33 */ 34 35 import org.testng.annotations.Test; 36 37 import java.lang.reflect.Field; 38 39 import static org.testng.Assert.*; 40 41 public class JdkInternalMiscUnsafeAccessTestFloat { 42 static final int ITERS = Integer.getInteger("iters", 1); 43 static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10); 44 45 static final jdk.internal.misc.Unsafe UNSAFE; 46 47 static final long V_OFFSET; 48 49 static final Object STATIC_V_BASE; 50 51 static final long STATIC_V_OFFSET; 52 53 static int ARRAY_OFFSET; 54 55 static int ARRAY_SHIFT; 56 57 static { 58 try { 59 Field f = jdk.internal.misc.Unsafe.class.getDeclaredField("theUnsafe"); 60 f.setAccessible(true); 61 UNSAFE = (jdk.internal.misc.Unsafe) f.get(null); 62 } catch (Exception e) { 63 throw new RuntimeException("Unable to get Unsafe instance.", e); 64 } 65 66 try { 67 Field staticVField = JdkInternalMiscUnsafeAccessTestFloat.class.getDeclaredField("static_v"); 68 STATIC_V_BASE = UNSAFE.staticFieldBase(staticVField); 69 STATIC_V_OFFSET = UNSAFE.staticFieldOffset(staticVField); 70 } catch (Exception e) { 71 throw new RuntimeException(e); 72 } 73 74 try { 75 Field vField = JdkInternalMiscUnsafeAccessTestFloat.class.getDeclaredField("v"); 76 V_OFFSET = UNSAFE.objectFieldOffset(vField); 77 } catch (Exception e) { 78 throw new RuntimeException(e); 79 } 80 81 ARRAY_OFFSET = UNSAFE.arrayBaseOffset(float[].class); 82 int ascale = UNSAFE.arrayIndexScale(float[].class); 83 ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale); 84 } 85 86 static float static_v; 87 88 float v; 89 90 @Test 91 public void testFieldInstance() { 92 JdkInternalMiscUnsafeAccessTestFloat t = new JdkInternalMiscUnsafeAccessTestFloat(); 93 for (int c = 0; c < ITERS; c++) { 94 testAccess(t, V_OFFSET); 95 } 96 } 97 98 @Test 99 public void testFieldStatic() { 100 for (int c = 0; c < ITERS; c++) { 101 testAccess(STATIC_V_BASE, STATIC_V_OFFSET); 102 } 103 } 104 105 @Test 106 public void testArray() { 107 float[] array = new float[10]; 108 for (int c = 0; c < ITERS; c++) { 109 for (int i = 0; i < array.length; i++) { 110 testAccess(array, (((long) i) << ARRAY_SHIFT) + ARRAY_OFFSET); 111 } 112 } 113 } 114 115 @Test 116 public void testArrayOffHeap() { 117 int size = 10; 118 long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT); 119 try { 120 for (int c = 0; c < ITERS; c++) { 121 for (int i = 0; i < size; i++) { 122 testAccess(null, (((long) i) << ARRAY_SHIFT) + address); 123 } 124 } 125 } finally { 126 UNSAFE.freeMemory(address); 127 } 128 } 129 130 @Test 131 public void testArrayOffHeapDirect() { 132 int size = 10; 133 long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT); 134 try { 135 for (int c = 0; c < ITERS; c++) { 136 for (int i = 0; i < size; i++) { 137 testAccess((((long) i) << ARRAY_SHIFT) + address); 138 } 139 } 140 } finally { 141 UNSAFE.freeMemory(address); 142 } 143 } 144 145 static void testAccess(Object base, long offset) { 146 // Plain 147 { 148 UNSAFE.putFloat(base, offset, 1.0f); 149 float x = UNSAFE.getFloat(base, offset); 150 assertEquals(x, 1.0f, "set float value"); 151 } 152 153 // Volatile 154 { 155 UNSAFE.putFloatVolatile(base, offset, 2.0f); 156 float x = UNSAFE.getFloatVolatile(base, offset); 157 assertEquals(x, 2.0f, "putVolatile float value"); 158 } 159 160 161 // Lazy 162 { 163 UNSAFE.putFloatRelease(base, offset, 1.0f); 164 float x = UNSAFE.getFloatAcquire(base, offset); 165 assertEquals(x, 1.0f, "putRelease float value"); 166 } 167 168 // Opaque 169 { 170 UNSAFE.putFloatOpaque(base, offset, 2.0f); 171 float x = UNSAFE.getFloatOpaque(base, offset); 172 assertEquals(x, 2.0f, "putOpaque float value"); 173 } 174 175 176 UNSAFE.putFloat(base, offset, 1.0f); 177 178 // Compare 179 { 180 boolean r = UNSAFE.compareAndSwapFloat(base, offset, 1.0f, 2.0f); 181 assertEquals(r, true, "success compareAndSwap float"); 182 float x = UNSAFE.getFloat(base, offset); 183 assertEquals(x, 2.0f, "success compareAndSwap float value"); 184 } 185 186 { 187 boolean r = UNSAFE.compareAndSwapFloat(base, offset, 1.0f, 3.0f); 188 assertEquals(r, false, "failing compareAndSwap float"); 189 float x = UNSAFE.getFloat(base, offset); 190 assertEquals(x, 2.0f, "failing compareAndSwap float value"); 191 } 192 193 // Advanced compare 194 { 195 float r = UNSAFE.compareAndExchangeFloatVolatile(base, offset, 2.0f, 1.0f); 196 assertEquals(r, 2.0f, "success compareAndExchangeVolatile float"); 197 float x = UNSAFE.getFloat(base, offset); 198 assertEquals(x, 1.0f, "success compareAndExchangeVolatile float value"); 199 } 200 201 { 202 float r = UNSAFE.compareAndExchangeFloatVolatile(base, offset, 2.0f, 3.0f); 203 assertEquals(r, 1.0f, "failing compareAndExchangeVolatile float"); 204 float x = UNSAFE.getFloat(base, offset); 205 assertEquals(x, 1.0f, "failing compareAndExchangeVolatile float value"); 206 } 207 208 { 209 float r = UNSAFE.compareAndExchangeFloatAcquire(base, offset, 1.0f, 2.0f); 210 assertEquals(r, 1.0f, "success compareAndExchangeAcquire float"); 211 float x = UNSAFE.getFloat(base, offset); 212 assertEquals(x, 2.0f, "success compareAndExchangeAcquire float value"); 213 } 214 215 { 216 float r = UNSAFE.compareAndExchangeFloatAcquire(base, offset, 1.0f, 3.0f); 217 assertEquals(r, 2.0f, "failing compareAndExchangeAcquire float"); 218 float x = UNSAFE.getFloat(base, offset); 219 assertEquals(x, 2.0f, "failing compareAndExchangeAcquire float value"); 220 } 221 222 { 223 float r = UNSAFE.compareAndExchangeFloatRelease(base, offset, 2.0f, 1.0f); 224 assertEquals(r, 2.0f, "success compareAndExchangeRelease float"); 225 float x = UNSAFE.getFloat(base, offset); 226 assertEquals(x, 1.0f, "success compareAndExchangeRelease float value"); 227 } 228 229 { 230 float r = UNSAFE.compareAndExchangeFloatRelease(base, offset, 2.0f, 3.0f); 231 assertEquals(r, 1.0f, "failing compareAndExchangeRelease float"); 232 float x = UNSAFE.getFloat(base, offset); 233 assertEquals(x, 1.0f, "failing compareAndExchangeRelease float value"); 234 } 235 236 { 237 boolean success = false; 238 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 239 success = UNSAFE.weakCompareAndSwapFloat(base, offset, 1.0f, 2.0f); 240 } 241 assertEquals(success, true, "weakCompareAndSwap float"); 242 float x = UNSAFE.getFloat(base, offset); 243 assertEquals(x, 2.0f, "weakCompareAndSwap float value"); 244 } 245 246 { 247 boolean success = false; 248 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 249 success = UNSAFE.weakCompareAndSwapFloatAcquire(base, offset, 2.0f, 1.0f); 250 } 251 assertEquals(success, true, "weakCompareAndSwapAcquire float"); 252 float x = UNSAFE.getFloat(base, offset); 253 assertEquals(x, 1.0f, "weakCompareAndSwapAcquire float"); 254 } 255 256 { 257 boolean success = false; 258 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 259 success = UNSAFE.weakCompareAndSwapFloatRelease(base, offset, 1.0f, 2.0f); 260 } 261 assertEquals(success, true, "weakCompareAndSwapRelease float"); 262 float x = UNSAFE.getFloat(base, offset); 263 assertEquals(x, 2.0f, "weakCompareAndSwapRelease float"); 264 } 265 266 { 267 boolean success = false; 268 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 269 success = UNSAFE.weakCompareAndSwapFloatVolatile(base, offset, 2.0f, 1.0f); 270 } 271 assertEquals(success, true, "weakCompareAndSwapVolatile float"); 272 float x = UNSAFE.getFloat(base, offset); 273 assertEquals(x, 1.0f, "weakCompareAndSwapVolatile float"); 274 } 275 276 UNSAFE.putFloat(base, offset, 2.0f); 277 278 // Compare set and get 279 { 280 float o = UNSAFE.getAndSetFloat(base, offset, 1.0f); 281 assertEquals(o, 2.0f, "getAndSet float"); 282 float x = UNSAFE.getFloat(base, offset); 283 assertEquals(x, 1.0f, "getAndSet float value"); 284 } 285 286 UNSAFE.putFloat(base, offset, 1.0f); 287 288 // get and add, add and get 289 { 290 float o = UNSAFE.getAndAddFloat(base, offset, 2.0f); 291 assertEquals(o, 1.0f, "getAndAdd float"); 292 float x = UNSAFE.getFloat(base, offset); 293 assertEquals(x, (float)(1.0f + 2.0f), "getAndAdd float"); 294 } 295 } 296 297 static void testAccess(long address) { 298 // Plain 299 { 300 UNSAFE.putFloat(address, 1.0f); 301 float x = UNSAFE.getFloat(address); 302 assertEquals(x, 1.0f, "set float value"); 303 } 304 } 305 } 306