1 /* 2 * Copyright (c) 2014, 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 * To change this template, choose Tools | Templates 25 * and open the template in the editor. 26 */ 27 28 package org.openjdk.bench.vm.compiler; 29 30 import org.openjdk.jmh.annotations.Benchmark; 31 import org.openjdk.jmh.annotations.BenchmarkMode; 32 import org.openjdk.jmh.annotations.Mode; 33 import org.openjdk.jmh.annotations.OutputTimeUnit; 34 import org.openjdk.jmh.annotations.Scope; 35 import org.openjdk.jmh.annotations.Setup; 36 import org.openjdk.jmh.annotations.State; 37 38 import java.util.Random; 39 import java.util.concurrent.TimeUnit; 40 41 @BenchmarkMode(Mode.AverageTime) 42 @OutputTimeUnit(TimeUnit.NANOSECONDS) 43 @State(Scope.Thread) 44 public class WriteBarrier { 45 46 // For array references 47 public static final int NUM_REFERENCES_SMALL = 32; 48 public static final int NUM_REFERENCES_LARGE = 2048; 49 50 // For array update tests 51 private Object[] theArraySmall; 52 private Object[] realReferencesSmall; 53 private Object[] nullReferencesSmall; 54 private int[] indicesSmall; 55 56 private Object[] theArrayLarge; 57 private Object[] realReferencesLarge; 58 private Object[] nullReferencesLarge; 59 private int[] indicesLarge; 60 61 // For field update tests 62 public Referencer head = null; 63 public Referencer tail = null; 64 65 // For random number generation 66 private int m_w; 67 private int m_z; 68 69 // For field references 70 public class Referencer { 71 Referencer next = null; 72 Referencer() { 73 this.next = null; 74 } 75 void append(Referencer r) { 76 this.next = r; 77 } 78 void clear() { 79 this.next = null; 80 } 81 } 82 83 @Setup 84 public void setup() { 85 theArraySmall = new Object[NUM_REFERENCES_SMALL]; 86 realReferencesSmall = new Object[NUM_REFERENCES_SMALL]; 87 nullReferencesSmall = new Object[NUM_REFERENCES_SMALL]; 88 indicesSmall = new int[NUM_REFERENCES_SMALL]; 89 90 theArrayLarge = new Object[NUM_REFERENCES_LARGE]; 91 realReferencesLarge = new Object[NUM_REFERENCES_LARGE]; 92 nullReferencesLarge = new Object[NUM_REFERENCES_LARGE]; 93 indicesLarge = new int[NUM_REFERENCES_LARGE]; 94 95 m_w = (int) System.currentTimeMillis(); 96 Random random = new Random(); 97 m_z = random.nextInt(10000) + 1; 98 99 for (int i = 0; i < NUM_REFERENCES_SMALL; i++) { 100 indicesSmall[i] = get_random() % (NUM_REFERENCES_SMALL - 1); 101 realReferencesSmall[i] = new Object(); 102 } 103 104 for (int i = 0; i < NUM_REFERENCES_LARGE; i++) { 105 indicesLarge[i] = get_random() % (NUM_REFERENCES_LARGE - 1); 106 realReferencesLarge[i] = new Object(); 107 } 108 109 // Build a small linked structure 110 this.head = new Referencer(); 111 this.tail = new Referencer(); 112 this.head.append(this.tail); 113 114 // This will (hopefully) promote objects to old space 115 // Run with -XX:+DisableExplicitGC to keep 116 // objects in young space 117 System.gc(); 118 } 119 120 private int get_random() { 121 m_z = 36969 * (m_z & 65535) + (m_z >> 16); 122 m_w = 18000 * (m_w & 65535) + (m_w >> 16); 123 return Math.abs((m_z << 16) + m_w); /* 32-bit result */ 124 } 125 126 @Benchmark 127 public void testArrayWriteBarrierFastPathRealSmall() { 128 for (int i = 0; i < NUM_REFERENCES_SMALL; i++) { 129 theArraySmall[indicesSmall[NUM_REFERENCES_SMALL - i - 1]] = realReferencesSmall[indicesSmall[i]]; 130 } 131 } 132 133 @Benchmark 134 public void testArrayWriteBarrierFastPathNullSmall() { 135 for (int i = 0; i < NUM_REFERENCES_SMALL; i++) { 136 theArraySmall[indicesSmall[NUM_REFERENCES_SMALL - i - 1]] = nullReferencesSmall[indicesSmall[i]]; 137 } 138 } 139 140 @Benchmark 141 public void testArrayWriteBarrierFastPathRealLarge() { 142 for (int i = 0; i < NUM_REFERENCES_LARGE; i++) { 143 theArrayLarge[indicesLarge[NUM_REFERENCES_LARGE - i - 1]] = realReferencesLarge[indicesLarge[i]]; 144 } 145 } 146 147 @Benchmark 148 public void testArrayWriteBarrierFastPathNullLarge() { 149 for (int i = 0; i < NUM_REFERENCES_LARGE; i++) { 150 theArrayLarge[indicesLarge[NUM_REFERENCES_LARGE - i - 1]] = nullReferencesLarge[indicesLarge[i]]; 151 } 152 } 153 154 @Benchmark() 155 public void testFieldWriteBarrierFastPath() { 156 // Shuffle everything around 157 this.tail.append(this.head); 158 this.head.clear(); 159 this.head.append(this.tail); 160 this.tail.clear(); 161 } 162 }