# HG changeset patch # User rwestberg # Date 1529581160 -7200 # Node ID 17d6de3b25fc2a486e2e6767d117f026987bcc9f # Parent db0a17475826bde1b574cb9ee019d9756d521300 8205103: [TESTBUG] Instability in JFR test TestThreadCpuTimeEvent Reviewed-by: mgronlun diff -r db0a17475826 -r 17d6de3b25fc test/jdk/jdk/jfr/event/runtime/TestThreadCpuTimeEvent.java --- 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 void testSimple() throws Throwable { - Recording recording = new Recording(); + static List 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 events = Events.fromRecording(recording); - // Default period is once per chunk - recording.enable(EventNames.ThreadCPULoad).withPeriod(Duration.ofMillis(eventPeriodMillis)); - recording.start(); + 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 { 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 events = Events.fromRecording(recording); - - Events.hasEvents(events); + List 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 beforeEvents = Events.fromRecording(recording); - + List beforeEvents = generateEvents(2, barrier); verifyPerThreadInvariant(beforeEvents, cpuConsumerThreadName); // Run a second single pass