LoopHelpers.java revision 3203
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn/*
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn *
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn * This code is free software; you can redistribute it and/or modify it
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn * under the terms of the GNU General Public License version 2 only, as
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn * published by the Free Software Foundation.
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn *
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn * This code is distributed in the hope that it will be useful, but WITHOUT
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn * version 2 for more details (a copy is included in the LICENSE file that
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn * accompanied this code).
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn *
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn * You should have received a copy of the GNU General Public License version
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn * 2 along with this work; if not, write to the Free Software Foundation,
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn *
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn * or visit www.oracle.com if you need additional information or have any
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn * questions.
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn */
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn/*
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn * This file is available under and governed by the GNU General Public
f841f6ad96ea6675d6c6b35c749eaac601799fdfraf * License version 2 only, as published by the Free Software Foundation.
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn * However, the following notice accompanied the original version of this
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn * file:
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn *
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn * Written by Doug Lea with assistance from members of JCP JSR-166
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn * Expert Group and released to the public domain, as explained at
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn * http://creativecommons.org/licenses/publicdomain
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn */
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn/**
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn * Misc utilities in JSR166 performance tests
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn */
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn
31e37bb439502e3f7c4c0a9a77d655ea5d56887avnimport java.util.concurrent.*;
31e37bb439502e3f7c4c0a9a77d655ea5d56887avnimport java.util.concurrent.atomic.*;
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn
31e37bb439502e3f7c4c0a9a77d655ea5d56887avnclass LoopHelpers {
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn // Some mindless computation to do between synchronizations...
31e37bb439502e3f7c4c0a9a77d655ea5d56887avn
62e6146930fb254c3db3332a4597f6fffe081b2cdb /**
f841f6ad96ea6675d6c6b35c749eaac601799fdfraf * generates 32 bit pseudo-random numbers.
* Adapted from http://www.snippets.org
*/
public static int compute1(int x) {
int lo = 16807 * (x & 0xFFFF);
int hi = 16807 * (x >>> 16);
lo += (hi & 0x7FFF) << 16;
if ((lo & 0x80000000) != 0) {
lo &= 0x7fffffff;
++lo;
}
lo += hi >>> 15;
if (lo == 0 || (lo & 0x80000000) != 0) {
lo &= 0x7fffffff;
++lo;
}
return lo;
}
/**
* Computes a linear congruential random number a random number
* of times.
*/
public static int compute2(int x) {
int loops = (x >>> 4) & 7;
while (loops-- > 0) {
x = (x * 2147483647) % 16807;
}
return x;
}
/**
* An actually useful random number generator, but unsynchronized.
* Basically same as java.util.Random.
*/
public static class SimpleRandom {
private static final long multiplier = 0x5DEECE66DL;
private static final long addend = 0xBL;
private static final long mask = (1L << 48) - 1;
static final AtomicLong seq = new AtomicLong(1);
private long seed = System.nanoTime() + seq.getAndIncrement();
public void setSeed(long s) {
seed = s;
}
public int next() {
long nextseed = (seed * multiplier + addend) & mask;
seed = nextseed;
return ((int)(nextseed >>> 17)) & 0x7FFFFFFF;
}
}
public static class BarrierTimer implements Runnable {
public volatile long startTime;
public volatile long endTime;
public void run() {
long t = System.nanoTime();
if (startTime == 0)
startTime = t;
else
endTime = t;
}
public void clear() {
startTime = 0;
endTime = 0;
}
public long getTime() {
return endTime - startTime;
}
}
public static String rightJustify(long n) {
// There's probably a better way to do this...
String field = " ";
String num = Long.toString(n);
if (num.length() >= field.length())
return num;
StringBuffer b = new StringBuffer(field);
b.replace(b.length()-num.length(), b.length(), num);
return b.toString();
}
}