1 /* 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 it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 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 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 */ 22 23 /* 24 * This file is available under and governed by the GNU General Public 25 * License version 2 only, as published by the Free Software Foundation. 26 * However, the following notice accompanied the original version of this 27 * file: 28 * 29 * Written by Doug Lea with assistance from members of JCP JSR-166 30 * Expert Group and released to the public domain, as explained at 31 * http://creativecommons.org/licenses/publicdomain 32 */ 33 /** 34 * Misc utilities in JSR166 performance tests 35 */ 36 37 import java.util.concurrent.*; 38 import java.util.concurrent.atomic.*; 39 40 class LoopHelpers { 41 42 // Some mindless computation to do between synchronizations... 43 44 /** 45 * generates 32 bit pseudo-random numbers. 46 * Adapted from http://www.snippets.org 47 */ 48 public static int compute1(int x) { 49 int lo = 16807 * (x & 0xFFFF); 50 int hi = 16807 * (x >>> 16); 51 lo += (hi & 0x7FFF) << 16; 52 if ((lo & 0x80000000) != 0) { 53 lo &= 0x7fffffff; 54 ++lo; 55 } 56 lo += hi >>> 15; 57 if (lo == 0 || (lo & 0x80000000) != 0) { 58 lo &= 0x7fffffff; 59 ++lo; 60 } 61 return lo; 62 } 63 64 /** 65 * Computes a linear congruential random number a random number 66 * of times. 67 */ 68 public static int compute2(int x) { 69 int loops = (x >>> 4) & 7; 70 while (loops-- > 0) { 71 x = (x * 2147483647) % 16807; 72 } 73 return x; 74 } 75 76 /** 77 * An actually useful random number generator, but unsynchronized. 78 * Basically same as java.util.Random. 79 */ 80 public static class SimpleRandom { 81 private final static long multiplier = 0x5DEECE66DL; 82 private final static long addend = 0xBL; 83 private final static long mask = (1L << 48) - 1; 84 static final AtomicLong seq = new AtomicLong(1); 85 private long seed = System.nanoTime() + seq.getAndIncrement(); 86 87 public void setSeed(long s) { 88 seed = s; 89 } 90 91 public int next() { 92 long nextseed = (seed * multiplier + addend) & mask; 93 seed = nextseed; 94 return ((int)(nextseed >>> 17)) & 0x7FFFFFFF; 95 } 96 } 97 98 public static class BarrierTimer implements Runnable { 99 public volatile long startTime; 100 public volatile long endTime; 101 public void run() { 102 long t = System.nanoTime(); 103 if (startTime == 0) 104 startTime = t; 105 else 106 endTime = t; 107 } 108 public void clear() { 109 startTime = 0; 110 endTime = 0; 111 } 112 public long getTime() { 113 return endTime - startTime; 114 } 115 } 116 117 public static String rightJustify(long n) { 118 // There's probably a better way to do this... 119 String field = " "; 120 String num = Long.toString(n); 121 if (num.length() >= field.length()) 122 return num; 123 StringBuffer b = new StringBuffer(field); 124 b.replace(b.length()-num.length(), b.length(), num); 125 return b.toString(); 126 } 127 128 }