1 /*
  2  * Copyright (c) 2014, 2021, 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.Fork;
 28 import org.openjdk.jmh.annotations.Measurement;
 29 import org.openjdk.jmh.annotations.Mode;
 30 import org.openjdk.jmh.annotations.OutputTimeUnit;
 31 import org.openjdk.jmh.annotations.Param;
 32 import org.openjdk.jmh.annotations.Scope;
 33 import org.openjdk.jmh.annotations.Setup;
 34 import org.openjdk.jmh.annotations.State;
 35 import org.openjdk.jmh.annotations.Threads;
 36 import org.openjdk.jmh.annotations.Warmup;
 37 
 38 import java.util.concurrent.TimeUnit;
 39 
 40 
 41 /**
 42  * Benchmark class for simple lock unlock tests. Nothing big should ever go into this class.
 43  */
 44 @BenchmarkMode(Mode.AverageTime)
 45 @OutputTimeUnit(TimeUnit.NANOSECONDS)
 46 @State(Scope.Benchmark)
 47 @Warmup(iterations = 4, time = 2)
 48 @Measurement(iterations = 4, time = 2)
 49 @Fork(value = 3)
 50 public class LockUnlock {
 51 
 52     @Param("100")
 53     private int innerCount;
 54 
 55     public Object lockObject1;
 56     public Object lockObject2;
 57     public int factorial;
 58     public int dummyInt1;
 59     public int dummyInt2;
 60 
 61     @Setup
 62     public void setup() {
 63         lockObject1 = new Object();
 64         lockObject2 = new Object();
 65         dummyInt1 = 47;
 66         dummyInt2 = 11; // anything
 67     }
 68 
 69     /** Perform a synchronized on a local object within a loop. */
 70     @Benchmark
 71     public void testSimpleLockUnlock() {
 72         Object localObject = lockObject1;
 73         for (int i = 0; i < innerCount; i++) {
 74             synchronized (localObject) {
 75                 dummyInt1++;
 76                 dummyInt2++;
 77             }
 78         }
 79     }
 80 
 81     /** Perform a recursive synchronized on a local object within a loop. */
 82     @Benchmark
 83     public void testRecursiveLockUnlock() {
 84         Object localObject = lockObject1;
 85         for (int i = 0; i < innerCount; i++) {
 86             synchronized (localObject) {
 87                 synchronized (localObject) {
 88                     dummyInt1++;
 89                     dummyInt2++;
 90                 }
 91             }
 92         }
 93     }
 94 
 95     /** Perform two synchronized after each other on the same local object. */
 96     @Benchmark
 97     public void testSerialLockUnlock() {
 98         Object localObject = lockObject1;
 99         for (int i = 0; i < innerCount; i++) {
100             synchronized (localObject) {
101                 dummyInt1++;
102             }
103             synchronized (localObject) {
104                 dummyInt2++;
105             }
106         }
107     }
108 
109     /**
110      * Performs recursive synchronizations on the same local object.
111      * <p/>
112      * Result is 3628800
113      */
114     @Benchmark
115     public void testRecursiveSynchronization() {
116         factorial = fact(10);
117     }
118 
119     private synchronized int fact(int n) {
120         if (n == 0) {
121             return 1;
122         } else {
123             return fact(n - 1) * n;
124         }
125     }
126 
127     /**
128      * With two threads lockObject1 will be contended so should be
129      * inflated.
130      */
131     @Threads(2)
132     @Benchmark
133     public void testContendedLock() {
134         synchronized (lockObject1) {
135             dummyInt1++;
136         }
137     }
138 }