Changeset be53b87 for benchmark/mutex


Ignore:
Timestamp:
Jul 29, 2019, 1:46:24 PM (5 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
5453237
Parents:
033ff37
git-author:
Peter A. Buhr <pabuhr@…> (07/29/19 12:40:16)
git-committer:
Peter A. Buhr <pabuhr@…> (07/29/19 13:46:24)
Message:

update Java benchmarks with multiple mechanisms to trick the JIT

File:
1 edited

Legend:

Unmodified
Added
Removed
  • benchmark/mutex/JavaThread.java

    r033ff37 rbe53b87  
    11public class JavaThread {
    2         public synchronized void noop() {}
     2        // Simplistic low-quality Marsaglia Shift-XOR pseudo-random number generator.
     3        // Bijective   
     4        // Cycle length for non-zero values is 4G-1.
     5        // 0 is absorbing and should be avoided -- fixed point.
     6        // The returned value is typically masked to produce a positive value.
     7        static volatile int Ticket = 0 ;
    38
    4         public static void main(String[] args) {
    5                 int NoOfTimes = 5000000;
     9        private static int nextRandom (int x) {
     10                if (x == 0) {
     11                        // reseed the PRNG
     12                        // Ticket is accessed infrequently and does not constitute a coherence hot-spot.
     13                        // Note that we use a non-atomic racy increment -- the race is rare and benign.
     14                        // If the race is a concern switch to an AtomicInteger. 
     15                        // In addition accesses to the RW volatile global "Ticket"  variable are not
     16                        // (readily) predictable at compile-time so the JIT will not be able to elide
     17                        // nextRandom() invocations. 
     18                        x = ++Ticket ;
     19                        if (x == 0) x = 1 ;
     20                }
     21                x ^= x << 6;
     22                x ^= x >>> 21;
     23                x ^= x << 7;
     24                return x ;   
     25        }
     26        static int x = 2;
     27
     28        static private final int NoOfTimes = Integer.parseInt("100000000") ;
     29
     30        public synchronized void noop() {
     31                x = nextRandom( x );
     32        }
     33        public static void helper() throws InterruptedException {
    634                JavaThread j = new JavaThread();
    7                 long start = System.nanoTime();
     35                // Inhibit biased locking ...
     36                x = (j.hashCode() ^ System.identityHashCode(j)) | 1 ;     
    837                for(int i = 1; i <= NoOfTimes; i += 1) {
     38                        x = nextRandom(x);
    939                        j.noop();
    1040                }
     41        }
     42        public static void InnerMain() throws InterruptedException {
     43                long start = System.nanoTime();
     44                helper();
    1145                long end = System.nanoTime();
    12                 System.out.println( (end - start) / NoOfTimes);
     46                System.out.println( (end - start) / NoOfTimes );
     47        }
     48        public static void main(String[] args) throws InterruptedException {
     49                for (int n = Integer.parseInt("5"); --n >= 0 ; ) {
     50                        InnerMain();
     51                        Thread.sleep(2000);     // 2 seconds
     52                        x = nextRandom(x);
     53                }
     54                if ( x == 0 ) System.out.println(x);
    1355        }
    1456}
Note: See TracChangeset for help on using the changeset viewer.