1 /*
   2  * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.openjdk.bench.vm.lang;
  24 
  25 import org.openjdk.jmh.annotations.Benchmark;
  26 import org.openjdk.jmh.annotations.BenchmarkMode;
  27 import org.openjdk.jmh.annotations.Mode;
  28 import org.openjdk.jmh.annotations.OutputTimeUnit;
  29 import org.openjdk.jmh.annotations.Param;
  30 import org.openjdk.jmh.annotations.Scope;
  31 import org.openjdk.jmh.annotations.Setup;
  32 import org.openjdk.jmh.annotations.State;
  33 import org.openjdk.jmh.annotations.Threads;
  34 
  35 import java.util.concurrent.TimeUnit;
  36 
  37 
  38 /**
  39  * Benchmark class for simple lock unlock tests. Nothing big should ever go into this class.
  40  */
  41 @BenchmarkMode(Mode.AverageTime)
  42 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  43 @State(Scope.Benchmark)
  44 public class LockUnlock {
  45 
  46     @Param("100")
  47     private int innerCount;
  48 
  49     public Object lockObject1;
  50     public Object lockObject2;
  51     public int factorial;
  52     public int dummyInt1;
  53     public int dummyInt2;
  54 
  55     @Setup
  56     public void setup() {
  57         lockObject1 = new Object();
  58         lockObject2 = new Object();
  59         dummyInt1 = 47;
  60         dummyInt2 = 11; // anything
  61     }
  62 
  63     /** Perform a synchronized on a local object within a loop. */
  64     @Benchmark
  65     public void testSimpleLockUnlock() {
  66         Object localObject = lockObject1;
  67         for (int i = 0; i < innerCount; i++) {
  68             synchronized (localObject) {
  69                 dummyInt1++;
  70                 dummyInt2++;
  71             }
  72         }
  73     }
  74 
  75     /** Perform a recursive synchronized on a local object within a loop. */
  76     @Benchmark
  77     public void testRecursiveLockUnlock() {
  78         Object localObject = lockObject1;
  79         for (int i = 0; i < innerCount; i++) {
  80             synchronized (localObject) {
  81                 synchronized (localObject) {
  82                     dummyInt1++;
  83                     dummyInt2++;
  84                 }
  85             }
  86         }
  87     }
  88 
  89     /** Perform two synchronized after each other on the same local object. */
  90     @Benchmark
  91     public void testSerialLockUnlock() {
  92         Object localObject = lockObject1;
  93         for (int i = 0; i < innerCount; i++) {
  94             synchronized (localObject) {
  95                 dummyInt1++;
  96             }
  97             synchronized (localObject) {
  98                 dummyInt2++;
  99             }
 100         }
 101     }
 102 
 103     /**
 104      * Performs recursive synchronizations on the same local object.
 105      * <p/>
 106      * Result is 3628800
 107      */
 108     @Benchmark
 109     public void testRecursiveSynchronization() {
 110         factorial = fact(10);
 111     }
 112 
 113     private synchronized int fact(int n) {
 114         if (n == 0) {
 115             return 1;
 116         } else {
 117             return fact(n - 1) * n;
 118         }
 119     }
 120 
 121     /**
 122      * With two threads lockObject1 will be contended so should be
 123      * inflated.
 124      */
 125     @Threads(2)
 126     @Benchmark
 127     public void testContendedLock() {
 128         synchronized (lockObject1) {
 129             dummyInt1++;
 130         }
 131     }
 132 }