changeset 1078:5164ebcc4936

7901234: Stray thread in StackProfiler prevents VM from exiting
author shade
date Mon, 12 Jan 2015 12:42:25 +0300
parents 1ba1bb91b83c
children f1ab355e5f76
files jmh-core-it/src/test/java/org/openjdk/jmh/it/fails/FailingForkedBenchStackProfilerTest.java jmh-core/src/main/java/org/openjdk/jmh/runner/LoopBenchmarkHandler.java
diffstat 2 files changed, 106 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core-it/src/test/java/org/openjdk/jmh/it/fails/FailingForkedBenchStackProfilerTest.java	Mon Jan 12 12:42:25 2015 +0300
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package org.openjdk.jmh.it.fails;
+
+import junit.framework.Assert;
+import org.junit.Test;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.Threads;
+import org.openjdk.jmh.annotations.Warmup;
+import org.openjdk.jmh.it.Fixtures;
+import org.openjdk.jmh.profile.StackProfiler;
+import org.openjdk.jmh.runner.Runner;
+import org.openjdk.jmh.runner.RunnerException;
+import org.openjdk.jmh.runner.options.Options;
+import org.openjdk.jmh.runner.options.OptionsBuilder;
+
+/**
+ * Baseline test:
+ * Checks if assertions are propagated back to integration tests.
+ */
+@State(Scope.Thread)
+public class FailingForkedBenchStackProfilerTest {
+
+    @Benchmark
+    @BenchmarkMode(Mode.All)
+    @Warmup(iterations = 0)
+    @Measurement(iterations = 1, time = 1)
+    @Fork(1)
+    @Threads(4)
+    public void doTest() {
+        Assert.fail();
+    }
+
+    @Test(timeout = 60*1000)
+    public void invokeAPI() throws RunnerException {
+        try {
+            Options opt = new OptionsBuilder()
+                    .include(Fixtures.getTestMask(this.getClass()))
+                    .shouldFailOnError(true)
+                    .forks(1)
+                    .addProfiler(StackProfiler.class)
+                    .build();
+            new Runner(opt).run();
+
+            org.junit.Assert.fail("Should have failed");
+        } catch (Throwable t) {
+            // expected
+        }
+    }
+
+}
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/LoopBenchmarkHandler.java	Thu Dec 25 19:42:56 2014 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/LoopBenchmarkHandler.java	Mon Jan 12 12:42:25 2015 +0300
@@ -117,30 +117,36 @@
 
         // Wait for the result, continuously polling the worker threads.
         // The abrupt exception in any worker will float up here.
-        int expected = numThreads;
-        while (expected > 0) {
-            for (Map.Entry<BenchmarkTask, Future<Collection<? extends Result>>> re : results.entrySet()) {
-                BenchmarkTask task = re.getKey();
-                Future<Collection<? extends Result>> fr = re.getValue();
-                try {
-                    long waitFor = Math.max(TimeUnit.MILLISECONDS.toNanos(100), waitDeadline - System.nanoTime());
-                    fr.get(waitFor, TimeUnit.NANOSECONDS);
-                    expected--;
-                } catch (InterruptedException ex) {
-                    throw new BenchmarkException(ex);
-                } catch (ExecutionException ex) {
-                    // unwrap: ExecutionException -> Throwable-wrapper -> InvocationTargetException
-                    Throwable cause = ex.getCause().getCause().getCause();
-                    throw new BenchmarkException(cause);
-                } catch (TimeoutException e) {
-                    // try to kick the thread, if it was already started
-                    Thread runner = task.runner;
-                    if (runner != null) {
-                        out.print("(*interrupt*) ");
-                        runner.interrupt();
+        try {
+            int expected = numThreads;
+            while (expected > 0) {
+                for (Map.Entry<BenchmarkTask, Future<Collection<? extends Result>>> re : results.entrySet()) {
+                    BenchmarkTask task = re.getKey();
+                    Future<Collection<? extends Result>> fr = re.getValue();
+                    try {
+                        long waitFor = Math.max(TimeUnit.MILLISECONDS.toNanos(100), waitDeadline - System.nanoTime());
+                        fr.get(waitFor, TimeUnit.NANOSECONDS);
+                        expected--;
+                    } catch (InterruptedException ex) {
+                        throw new BenchmarkException(ex);
+                    } catch (ExecutionException ex) {
+                        // unwrap: ExecutionException -> Throwable-wrapper -> InvocationTargetException
+                        Throwable cause = ex.getCause().getCause().getCause();
+                        throw new BenchmarkException(cause);
+                    } catch (TimeoutException e) {
+                        // try to kick the thread, if it was already started
+                        Thread runner = task.runner;
+                        if (runner != null) {
+                            out.print("(*interrupt*) ");
+                            runner.interrupt();
+                        }
                     }
                 }
             }
+        } finally {
+            // profilers stop when after all threads are confirmed to be
+            // finished to capture the edge behaviors; or, on a failure path
+            stopProfilers(benchmarkParams, params, iterationResults);
         }
 
         // Get the results.
@@ -156,10 +162,6 @@
             }
         }
 
-        // profilers stop when after all threads are confirmed to be
-        // finished to capture the edge behaviors
-        stopProfilers(benchmarkParams, params, iterationResults);
-
         return iterationResults;
     }