1 /*
 2  * Copyright Amazon.com Inc. 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.compiler.pea;
24 
25 import org.openjdk.jmh.annotations.Benchmark;
26 import org.openjdk.jmh.annotations.BenchmarkMode;
27 import org.openjdk.jmh.annotations.CompilerControl;
28 import org.openjdk.jmh.annotations.Fork;
29 import org.openjdk.jmh.annotations.Measurement;
30 import org.openjdk.jmh.annotations.Mode;
31 import org.openjdk.jmh.annotations.Param;
32 import org.openjdk.jmh.annotations.Scope;
33 import org.openjdk.jmh.annotations.State;
34 import org.openjdk.jmh.annotations.OutputTimeUnit;
35 import org.openjdk.jmh.annotations.Warmup;
36 
37 import java.util.concurrent.TimeUnit;
38 
39 @BenchmarkMode(Mode.AverageTime)
40 @OutputTimeUnit(TimeUnit.NANOSECONDS)
41 @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
42 @Measurement(iterations = 20, time = 1, timeUnit = TimeUnit.SECONDS)
43 @State(Scope.Benchmark)
44 @Fork(value = 3)
45 public class Simple {
46     @Param({"1", "2", "4", "8"})
47     int odd;
48     private Object _cache;
49 
50     void example1(boolean cond) {
51         Object x = new Object();
52         if (cond) {
53             _cache = x;
54         }
55     }
56 
57     @Benchmark
58     public void example1() {
59         for (int i = 0; i< 1024; ++i) {
60             boolean cond = (i % odd) == 0;
61             example1(cond);
62         }
63     }
64     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
65     static void escaped_as_argument(Object x) {}
66 
67     // Ivanov suggests making this happen first.
68     // we don't need to create JVMState for the cloning Allocate.
69     @Benchmark
70     public void example1_ArgEscape() {
71         for (int i = 0; i< 1024; ++i) {
72             boolean cond = (i % odd) == 0;
73             Object x = new Object();
74 
75             if (cond) {
76                 escaped_as_argument(x);
77             }
78         }
79     }
80 }