1 /*
   2  * Copyright (c) 2018, 2018, 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 
  24 
  25 package org.graalvm.compiler.hotspot.lir.test;
  26 
  27 import static org.graalvm.compiler.test.SubprocessUtil.getVMCommandLine;
  28 import static org.graalvm.compiler.test.SubprocessUtil.withoutDebuggerArguments;
  29 
  30 import java.util.ArrayList;
  31 import java.util.Iterator;
  32 import java.util.List;
  33 
  34 import org.graalvm.compiler.api.directives.GraalDirectives;
  35 import org.graalvm.compiler.core.common.LIRKind;
  36 import org.graalvm.compiler.hotspot.HotSpotBackend;
  37 import org.graalvm.compiler.hotspot.debug.BenchmarkCounters;
  38 import org.graalvm.compiler.lir.ConstantValue;
  39 import org.graalvm.compiler.lir.gen.LIRGeneratorTool;
  40 import org.graalvm.compiler.lir.jtt.LIRTest;
  41 import org.graalvm.compiler.lir.jtt.LIRTestSpecification;
  42 import org.graalvm.compiler.test.SubprocessUtil;
  43 import org.junit.Assert;
  44 import org.junit.Assume;
  45 import org.junit.Before;
  46 import org.junit.Test;
  47 
  48 import jdk.vm.ci.amd64.AMD64;
  49 import jdk.vm.ci.meta.JavaConstant;
  50 import jdk.vm.ci.meta.JavaKind;
  51 import jdk.vm.ci.meta.ResolvedJavaMethod;
  52 
  53 public class BenchmarkCounterOverflowTest extends LIRTest {
  54     private static final String SUBPROCESS_PROPERTY = BenchmarkCounterOverflowTest.class.getSimpleName() + ".subprocess.call";
  55     private static final boolean VERBOSE = Boolean.getBoolean(BenchmarkCounterOverflowTest.class.getSimpleName() + ".verbose");
  56 
  57     private static LIRKind intKind;
  58 
  59     @Before
  60     public void checkAMD64() {
  61         Assume.assumeTrue("skipping AMD64 specific test", getTarget().arch instanceof AMD64);
  62         Assume.assumeTrue("skipping HotSpot specific test", getBackend() instanceof HotSpotBackend);
  63     }
  64 
  65     @Before
  66     public void setUp() {
  67         intKind = LIRKind.fromJavaKind(getBackend().getTarget().arch, JavaKind.Long);
  68     }
  69 
  70     private static final LIRTestSpecification constCounterIncrement = new LIRTestSpecification() {
  71         @Override
  72         public void generate(LIRGeneratorTool gen) {
  73             gen.append(gen.createBenchmarkCounter("counter", "test", new ConstantValue(intKind, JavaConstant.forLong(Integer.MAX_VALUE))));
  74         }
  75     };
  76 
  77     @SuppressWarnings("unused")
  78     @LIRIntrinsic
  79     public static void counterInc(LIRTestSpecification spec) {
  80     }
  81 
  82     public static void test(long count) {
  83         for (long i = 0; i < count; i++) {
  84             counterInc(constCounterIncrement);
  85             GraalDirectives.blackhole(i);
  86         }
  87     }
  88 
  89     @Test
  90     public void incrementCounter() {
  91         Assume.assumeTrue("not a subprocess -> skip", Boolean.getBoolean(SUBPROCESS_PROPERTY));
  92         BenchmarkCounters.enabled = true;
  93 
  94         Object[] args = new Object[]{Integer.MAX_VALUE * 4L};
  95         ResolvedJavaMethod method = getResolvedJavaMethod("test");
  96         executeActualCheckDeopt(getInitialOptions(), method, EMPTY, null, args);
  97     }
  98 
  99     @Test
 100     public void spawnSubprocess() throws Throwable {
 101         Assume.assumeFalse("subprocess already spawned -> skip", Boolean.getBoolean(SUBPROCESS_PROPERTY));
 102         List<String> vmArgs = withoutDebuggerArguments(getVMCommandLine());
 103         vmArgs.add("-XX:JVMCICounterSize=1");
 104         vmArgs.add("-Dgraal." + BenchmarkCounters.Options.AbortOnBenchmarkCounterOverflow.getName() + "=true");
 105         vmArgs.add("-D" + SUBPROCESS_PROPERTY + "=true");
 106 
 107         // Disable increment range checks (e.g. HotSpotCounterOp.checkIncrements())
 108         vmArgs.add("-dsa");
 109         vmArgs.add("-da");
 110 
 111         List<String> mainClassAndArgs = new ArrayList<>();
 112         mainClassAndArgs.add("com.oracle.mxtool.junit.MxJUnitWrapper");
 113         mainClassAndArgs.add(BenchmarkCounterOverflowTest.class.getName());
 114 
 115         SubprocessUtil.Subprocess proc = SubprocessUtil.java(vmArgs, mainClassAndArgs);
 116 
 117         if (VERBOSE) {
 118             System.out.println(proc);
 119         }
 120 
 121         Assert.assertNotEquals("Expected non-zero exit status", 0, proc.exitCode);
 122 
 123         Iterator<String> it = proc.output.iterator();
 124         while (it.hasNext()) {
 125             String line = it.next();
 126             if (line.contains("Problematic frame:")) {
 127                 if (!it.hasNext()) {
 128                     // no more line
 129                     break;
 130                 }
 131                 line = it.next();
 132                 if (line.contains(BenchmarkCounterOverflowTest.class.getName() + ".test")) {
 133                     return;
 134                 }
 135                 Assert.fail("Unexpected stack trace: " + line);
 136             }
 137         }
 138         Assert.fail(String.format("Could not find method in output:%n%s", proc));
 139     }
 140 }