|
| 1 | +/* |
| 2 | +* Copyright (c) 2020, 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 java.util.concurrent.ThreadLocalRandom; |
| 26 | +import java.util.concurrent.TimeUnit; |
| 27 | + |
| 28 | +import org.openjdk.jmh.annotations.Benchmark; |
| 29 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 30 | +import org.openjdk.jmh.annotations.Mode; |
| 31 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 32 | +import org.openjdk.jmh.annotations.Param; |
| 33 | +import org.openjdk.jmh.annotations.Scope; |
| 34 | +import org.openjdk.jmh.annotations.Setup; |
| 35 | +import org.openjdk.jmh.annotations.State; |
| 36 | +import org.openjdk.jmh.annotations.Threads; |
| 37 | +import org.openjdk.jmh.infra.Blackhole; |
| 38 | + |
| 39 | +@State(Scope.Benchmark) |
| 40 | +@Threads(Threads.MAX) |
| 41 | +public class MonitorBench { |
| 42 | + |
| 43 | + @Param({"100", "250"}) |
| 44 | + int consumeUnlocked; |
| 45 | + |
| 46 | + @Param({"100", "250"}) |
| 47 | + int consumeLocked; |
| 48 | + |
| 49 | + @Param({"0", "1"}) |
| 50 | + int throwThreshold; |
| 51 | + |
| 52 | + @Param({"10000"}) |
| 53 | + int range; |
| 54 | + |
| 55 | + @Param({"1"}) |
| 56 | + static int locksSize; |
| 57 | + |
| 58 | + Object[] sharedLocks; |
| 59 | + |
| 60 | + @Setup |
| 61 | + public void setup() { |
| 62 | + sharedLocks = new Object[locksSize]; |
| 63 | + for (int i = 0; i < locksSize; i++) { |
| 64 | + sharedLocks[i] = new Object(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + int update2(int sharedIndex) throws Exception { |
| 69 | + synchronized (sharedLocks[sharedIndex]) { |
| 70 | + Blackhole.consumeCPU(consumeLocked); |
| 71 | + if (ThreadLocalRandom.current().nextInt(range) < throwThreshold) { |
| 72 | + throw new Exception("Update failed"); |
| 73 | + } else { |
| 74 | + Blackhole.consumeCPU(consumeLocked); |
| 75 | + return 0; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + int update1(int sharedIndex) throws Exception { |
| 81 | + synchronized (sharedLocks[sharedIndex]) { |
| 82 | + Blackhole.consumeCPU(consumeLocked); |
| 83 | + return update2(sharedIndex); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Benchmark |
| 88 | + @BenchmarkMode(Mode.Throughput) |
| 89 | + @OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 90 | + public int action() throws InterruptedException { |
| 91 | + Blackhole.consumeCPU(consumeUnlocked); |
| 92 | + int sharedLockIndex = ThreadLocalRandom.current().nextInt(locksSize); |
| 93 | + Object sharedLock = sharedLocks[sharedLockIndex]; |
| 94 | + synchronized (sharedLock) { |
| 95 | + while (true) { |
| 96 | + try { |
| 97 | + return update1(sharedLockIndex); |
| 98 | + } catch (Exception e) { |
| 99 | + sharedLock.wait(100); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments