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