changeset 51328:17d6de3b25fc

8205103: [TESTBUG] Instability in JFR test TestThreadCpuTimeEvent Reviewed-by: mgronlun
author rwestberg
date Thu, 21 Jun 2018 13:39:20 +0200
parents db0a17475826
children 36ca515343e0
files test/jdk/jdk/jfr/event/runtime/TestThreadCpuTimeEvent.java
diffstat 1 files changed, 39 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/test/jdk/jdk/jfr/event/runtime/TestThreadCpuTimeEvent.java	Tue Jun 19 13:03:12 2018 -0400
+++ b/test/jdk/jdk/jfr/event/runtime/TestThreadCpuTimeEvent.java	Thu Jun 21 13:39:20 2018 +0200
@@ -160,26 +160,48 @@
         return totalTime;
     }
 
+    static List<RecordedEvent> generateEvents(int minimumEventCount, CyclicBarrier barrier) throws Throwable {
+        int retryCount = 0;
+
+        while (true) {
+            Recording recording = new Recording();
+
+            // Default period is once per chunk
+            recording.enable(EventNames.ThreadCPULoad).withPeriod(Duration.ofMillis(eventPeriodMillis));
+            recording.start();
+
+            // Run a single pass
+            barrier.await();
+            barrier.await();
+
+            recording.stop();
+            List<RecordedEvent> events = Events.fromRecording(recording);
+
+            long numEvents = events.stream()
+                    .filter(e -> e.getThread().getJavaName().equals(cpuConsumerThreadName))
+                    .count();
+
+            // If the JFR periodicals thread is really starved, we may not get enough events.
+            // In that case, we simply retry the operation.
+            if (numEvents < minimumEventCount) {
+                System.out.println("Not enough events recorded, trying again...");
+                if (retryCount++ > 10) {
+                    Asserts.fail("Retry count exceeded");
+                    throw new RuntimeException();
+                }
+            } else {
+                return events;
+            }
+        }
+    }
+
     static void testSimple() throws Throwable {
-        Recording recording = new Recording();
-
-        // Default period is once per chunk
-        recording.enable(EventNames.ThreadCPULoad).withPeriod(Duration.ofMillis(eventPeriodMillis));
-        recording.start();
-
         Duration testRunTime = Duration.ofMillis(eventPeriodMillis * cpuConsumerRunFactor);
         CyclicBarrier barrier = new CyclicBarrier(2);
         CpuConsumingThread thread = new CpuConsumingThread(testRunTime, barrier);
+        thread.start();
 
-        // Run a single pass
-        thread.start();
-        barrier.await();
-        barrier.await();
-
-        recording.stop();
-        List<RecordedEvent> events = Events.fromRecording(recording);
-
-        Events.hasEvents(events);
+        List<RecordedEvent> events = generateEvents(1, barrier);
         verifyPerThreadInvariant(events, cpuConsumerThreadName);
 
         thread.interrupt();
@@ -187,23 +209,12 @@
     }
 
     static void testCompareWithMXBean() throws Throwable {
-        Recording recording = new Recording();
-
-        recording.enable(EventNames.ThreadCPULoad).withPeriod(Duration.ofMillis(eventPeriodMillis));
-        recording.start();
-
         Duration testRunTime = Duration.ofMillis(eventPeriodMillis * cpuConsumerRunFactor);
         CyclicBarrier barrier = new CyclicBarrier(2);
         CpuConsumingThread thread = new CpuConsumingThread(testRunTime, barrier);
+        thread.start();
 
-        // Run a single pass
-        thread.start();
-        barrier.await();
-        barrier.await();
-
-        recording.stop();
-        List<RecordedEvent> beforeEvents = Events.fromRecording(recording);
-
+        List<RecordedEvent> beforeEvents = generateEvents(2, barrier);
         verifyPerThreadInvariant(beforeEvents, cpuConsumerThreadName);
 
         // Run a second single pass