1 /*
   2  * Copyright (c) 2018, 2019, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.jfr.api.recording.event;
  27 
  28 import java.io.IOException;
  29 import java.time.Duration;
  30 import java.util.ArrayList;
  31 import java.util.List;
  32 
  33 import jdk.jfr.Recording;
  34 import jdk.jfr.consumer.RecordedEvent;
  35 import jdk.jfr.consumer.RecordedThread;
  36 import jdk.testlibrary.Asserts;
  37 import jdk.testlibrary.jfr.EventNames;
  38 import jdk.testlibrary.jfr.Events;
  39 
  40 /*
  41  * @test
  42  * @summary Test event period.
  43  * @key jfr
  44  * @library /lib/testlibrary
  45  * @run main/othervm jdk.jfr.api.recording.event.TestPeriod
  46  */
  47 public class TestPeriod {
  48     private static final String EVENT_PATH = EventNames.ThreadAllocationStatistics;
  49     private static final long ERROR_MARGIN = 20; // 186 ms has been measured, when period was set to 200 ms
  50 
  51     public static void main(String[] args) throws Throwable {
  52         long[] periods = { 100, 200 };
  53         int eventCount = 4;
  54         int deltaCount;
  55         for (long period : periods) {
  56             List<Long> deltaBetweenEvents;
  57             do {
  58                 deltaBetweenEvents = createPeriodicEvents(period, eventCount);
  59                 deltaCount = deltaBetweenEvents.size();
  60                 if (deltaCount < eventCount - 1) {
  61                     System.out.println("Didn't get sufficent number of events. Retrying...");
  62                     System.out.println();
  63                 }
  64             } while (deltaCount < eventCount - 1);
  65             for (int i = 0; i < eventCount - 1; i++) {
  66                 verifyDelta(deltaBetweenEvents.get(i), period);
  67             }
  68             System.out.println();
  69         }
  70     }
  71 
  72     private static List<Long> createPeriodicEvents(long period, int eventCount) throws Exception, IOException {
  73         System.out.println("Provoking events with period " + period + " ms");
  74         Recording r = new Recording();
  75         r.start();
  76         runWithPeriod(r, period, eventCount + 1);
  77         r.stop();
  78 
  79         long prevTime = -1;
  80         List<Long> deltas = new ArrayList<>();
  81         for (RecordedEvent event : Events.fromRecording(r)) {
  82             if (Events.isEventType(event, EVENT_PATH) && isMyThread(event)) {
  83                 long timeMillis = event.getEndTime().toEpochMilli();
  84                 if (prevTime != -1) {
  85                     long delta = timeMillis - prevTime;
  86                     deltas.add(delta);
  87                     System.out.printf("event: time=%d, delta=%d%n", timeMillis, delta);
  88                 }
  89                 prevTime = timeMillis;
  90             }
  91         }
  92         r.close();
  93         return deltas;
  94     }
  95 
  96     // We only check that time is at least as expected.
  97     // We ignore if time is much longer than expected, since anything can happen
  98     // during heavy load,
  99     private static void verifyDelta(long actual, long expected) {
 100         System.out.printf("verifyDelta: actaul=%d, expected=%d (errorMargin=%d)%n", actual, expected, ERROR_MARGIN);
 101         Asserts.assertGreaterThan(actual, expected - ERROR_MARGIN, "period delta too short");
 102     }
 103 
 104     private static boolean isMyThread(RecordedEvent event) {
 105         Object o = event.getValue("thread");
 106         if (o instanceof RecordedThread) {
 107             RecordedThread rt = (RecordedThread) o;
 108             return Thread.currentThread().getId() == rt.getJavaThreadId();
 109         }
 110         return false;
 111     }
 112 
 113     @SuppressWarnings("unused")
 114     private static byte[] dummy = null;
 115 
 116     // Generate at least minEvents event with given period
 117     private static void runWithPeriod(Recording r, long period, int minEventCount) throws Exception {
 118         r.enable(EVENT_PATH).withPeriod(Duration.ofMillis(period));
 119         long endTime = System.currentTimeMillis() + period * minEventCount;
 120         while (System.currentTimeMillis() < endTime) {
 121             dummy = new byte[100];
 122             Thread.sleep(1);
 123         }
 124     }
 125 
 126 }