Path: blob/master/test/micro/org/openjdk/bench/vm/compiler/WriteBarrier.java
41161 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package org.openjdk.bench.vm.compiler;2324import org.openjdk.jmh.annotations.Benchmark;25import org.openjdk.jmh.annotations.BenchmarkMode;26import org.openjdk.jmh.annotations.Mode;27import org.openjdk.jmh.annotations.OutputTimeUnit;28import org.openjdk.jmh.annotations.Scope;29import org.openjdk.jmh.annotations.Setup;30import org.openjdk.jmh.annotations.State;3132import java.util.Random;33import java.util.concurrent.TimeUnit;3435@BenchmarkMode(Mode.AverageTime)36@OutputTimeUnit(TimeUnit.NANOSECONDS)37@State(Scope.Thread)38public class WriteBarrier {3940// For array references41public static final int NUM_REFERENCES_SMALL = 32;42public static final int NUM_REFERENCES_LARGE = 2048;4344// For array update tests45private Object[] theArraySmall;46private Object[] realReferencesSmall;47private Object[] nullReferencesSmall;48private int[] indicesSmall;4950private Object[] theArrayLarge;51private Object[] realReferencesLarge;52private Object[] nullReferencesLarge;53private int[] indicesLarge;5455// For field update tests56public Referencer head = null;57public Referencer tail = null;5859// For random number generation60private int m_w;61private int m_z;6263// For field references64public class Referencer {65Referencer next = null;66Referencer() {67this.next = null;68}69void append(Referencer r) {70this.next = r;71}72void clear() {73this.next = null;74}75}7677@Setup78public void setup() {79theArraySmall = new Object[NUM_REFERENCES_SMALL];80realReferencesSmall = new Object[NUM_REFERENCES_SMALL];81nullReferencesSmall = new Object[NUM_REFERENCES_SMALL];82indicesSmall = new int[NUM_REFERENCES_SMALL];8384theArrayLarge = new Object[NUM_REFERENCES_LARGE];85realReferencesLarge = new Object[NUM_REFERENCES_LARGE];86nullReferencesLarge = new Object[NUM_REFERENCES_LARGE];87indicesLarge = new int[NUM_REFERENCES_LARGE];8889m_w = (int) System.currentTimeMillis();90Random random = new Random();91m_z = random.nextInt(10000) + 1;9293for (int i = 0; i < NUM_REFERENCES_SMALL; i++) {94indicesSmall[i] = get_random() % (NUM_REFERENCES_SMALL - 1);95realReferencesSmall[i] = new Object();96}9798for (int i = 0; i < NUM_REFERENCES_LARGE; i++) {99indicesLarge[i] = get_random() % (NUM_REFERENCES_LARGE - 1);100realReferencesLarge[i] = new Object();101}102103// Build a small linked structure104this.head = new Referencer();105this.tail = new Referencer();106this.head.append(this.tail);107108// This will (hopefully) promote objects to old space109// Run with -XX:+DisableExplicitGC to keep110// objects in young space111System.gc();112}113114private int get_random() {115m_z = 36969 * (m_z & 65535) + (m_z >> 16);116m_w = 18000 * (m_w & 65535) + (m_w >> 16);117return Math.abs((m_z << 16) + m_w); /* 32-bit result */118}119120@Benchmark121public void testArrayWriteBarrierFastPathRealSmall() {122for (int i = 0; i < NUM_REFERENCES_SMALL; i++) {123theArraySmall[indicesSmall[NUM_REFERENCES_SMALL - i - 1]] = realReferencesSmall[indicesSmall[i]];124}125}126127@Benchmark128public void testArrayWriteBarrierFastPathNullSmall() {129for (int i = 0; i < NUM_REFERENCES_SMALL; i++) {130theArraySmall[indicesSmall[NUM_REFERENCES_SMALL - i - 1]] = nullReferencesSmall[indicesSmall[i]];131}132}133134@Benchmark135public void testArrayWriteBarrierFastPathRealLarge() {136for (int i = 0; i < NUM_REFERENCES_LARGE; i++) {137theArrayLarge[indicesLarge[NUM_REFERENCES_LARGE - i - 1]] = realReferencesLarge[indicesLarge[i]];138}139}140141@Benchmark142public void testArrayWriteBarrierFastPathNullLarge() {143for (int i = 0; i < NUM_REFERENCES_LARGE; i++) {144theArrayLarge[indicesLarge[NUM_REFERENCES_LARGE - i - 1]] = nullReferencesLarge[indicesLarge[i]];145}146}147148@Benchmark()149public void testFieldWriteBarrierFastPath() {150// Shuffle everything around151this.tail.append(this.head);152this.head.clear();153this.head.append(this.tail);154this.tail.clear();155}156}157158159