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 /**
  35  * Misc utilities in JSR166 performance tests
  36  */
  37 
  38 import java.util.concurrent.*;
  39 import java.util.concurrent.atomic.*;
  40 
  41 class LoopHelpers {
  42 
  43     // Some mindless computation to do between synchronizations...
  44 
  45     /**
  46      * generates 32 bit pseudo-random numbers.
  47      * Adapted from http://www.snippets.org
  48      */
  49     public static int compute1(int x) {
  50         int lo = 16807 * (x & 0xFFFF);
  51         int hi = 16807 * (x >>> 16);
  52         lo += (hi & 0x7FFF) << 16;
  53         if ((lo & 0x80000000) != 0) {
  54             lo &= 0x7fffffff;
  55             ++lo;
  56         }
  57         lo += hi >>> 15;
  58         if (lo == 0 || (lo & 0x80000000) != 0) {
  59             lo &= 0x7fffffff;
  60             ++lo;
  61         }
  62         return lo;
  63     }
  64 
  65     /**
  66      *  Computes a linear congruential random number a random number
  67      *  of times.
  68      */
  69     public static int compute2(int x) {
  70         int loops = (x >>> 4) & 7;
  71         while (loops-- > 0) {
  72             x = (x * 2147483647) % 16807;
  73         }
  74         return x;
  75     }
  76 
  77     /**
  78      * An actually useful random number generator, but unsynchronized.
  79      * Basically same as java.util.Random.
  80      */
  81     public static class SimpleRandom {
  82         private final static long multiplier = 0x5DEECE66DL;
  83         private final static long addend = 0xBL;
  84         private final static long mask = (1L << 48) - 1;
  85         static final AtomicLong seq = new AtomicLong(1);
  86         private long seed = System.nanoTime() + seq.getAndIncrement();
  87 
  88         public void setSeed(long s) {
  89             seed = s;
  90         }
  91 
  92         public int next() {
  93             long nextseed = (seed * multiplier + addend) & mask;
  94             seed = nextseed;
  95             return ((int)(nextseed >>> 17)) & 0x7FFFFFFF;
  96         }
  97     }
  98 
  99     public static class BarrierTimer implements Runnable {
 100         public volatile long startTime;
 101         public volatile long endTime;
 102         public void run() {
 103             long t = System.nanoTime();
 104             if (startTime == 0)
 105                 startTime = t;
 106             else
 107                 endTime = t;
 108         }
 109         public void clear() {
 110             startTime = 0;
 111             endTime = 0;
 112         }
 113         public long getTime() {
 114             return endTime - startTime;
 115         }
 116     }
 117 
 118     public static String rightJustify(long n) {
 119         // There's probably a better way to do this...
 120         String field = "         ";
 121         String num = Long.toString(n);
 122         if (num.length() >= field.length())
 123             return num;
 124         StringBuffer b = new StringBuffer(field);
 125         b.replace(b.length()-num.length(), b.length(), num);
 126         return b.toString();
 127     }
 128 
 129 }
--- EOF ---