1 /*
   2  * Copyright (c) 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.  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.test.lib.Asserts;
  37 import jdk.test.lib.jfr.EventNames;
  38 import jdk.test.lib.jfr.Events;
  39 
  40 /**
  41  * @test
  42  * @summary Test event period.
  43  * @key jfr
  44  *
  45  * @library /lib /
  46  * @run main/othervm jdk.jfr.api.recording.event.TestPeriod
  47  */
  48 public class TestPeriod {
  49     private static final String EVENT_PATH = EventNames.ThreadAllocationStatistics;
  50     private static final long ERROR_MARGIN = 20; // 186 ms has been measured, when period was set to 200 ms
  51 
  52     public static void main(String[] args) throws Throwable {
  53         long[] periods = { 100, 200 };
  54         int eventCount = 4;
  55         int deltaCount;
  56         for (long period : periods) {
  57             List<Long> deltaBetweenEvents;
  58             do {
  59                 deltaBetweenEvents = createPeriodicEvents(period, eventCount);
  60                 deltaCount = deltaBetweenEvents.size();
  61                 if (deltaCount < eventCount - 1) {
  62                     System.out.println("Didn't get sufficent number of events. Retrying...");
  63                     System.out.println();
  64                 }
  65             } while (deltaCount < eventCount - 1);
  66             for (int i = 0; i < eventCount - 1; i++) {
  67                 verifyDelta(deltaBetweenEvents.get(i), period);
  68             }
  69             System.out.println();
  70         }
  71     }
  72 
  73     private static List<Long> createPeriodicEvents(long period, int eventCount) throws Exception, IOException {
  74         System.out.println("Provoking events with period " + period + " ms");
  75         Recording r = new Recording();
  76         r.start();
  77         runWithPeriod(r, period, eventCount + 1);
  78         r.stop();
  79 
  80         long prevTime = -1;
  81         List<Long> deltas = new ArrayList<>();
  82         for (RecordedEvent event : Events.fromRecording(r)) {
  83             if (Events.isEventType(event, EVENT_PATH) && isMyThread(event)) {
  84                 long timeMillis = event.getEndTime().toEpochMilli();
  85                 if (prevTime != -1) {
  86                     long delta = timeMillis - prevTime;
  87                     deltas.add(delta);
  88                     System.out.printf("event: time=%d, delta=%d%n", timeMillis, delta);
  89                 }
  90                 prevTime = timeMillis;
  91             }
  92         }
  93         r.close();
  94         return deltas;
  95     }
  96 
  97     // We only check that time is at least as expected.
  98     // We ignore if time is much longer than expected, since anything can happen
  99     // during heavy load,
 100     private static void verifyDelta(long actual, long expected) {
 101         System.out.printf("verifyDelta: actaul=%d, expected=%d (errorMargin=%d)%n", actual, expected, ERROR_MARGIN);
 102         Asserts.assertGreaterThan(actual, expected - ERROR_MARGIN, "period delta too short");
 103     }
 104 
 105     private static boolean isMyThread(RecordedEvent event) {
 106         Object o = event.getValue("thread");
 107         if (o instanceof RecordedThread) {
 108             RecordedThread rt = (RecordedThread) o;
 109             return Thread.currentThread().getId() == rt.getJavaThreadId();
 110         }
 111         return false;
 112     }
 113 
 114     @SuppressWarnings("unused")
 115     private static byte[] dummy = null;
 116 
 117     // Generate at least minEvents event with given period
 118     private static void runWithPeriod(Recording r, long period, int minEventCount) throws Exception {
 119         r.enable(EVENT_PATH).withPeriod(Duration.ofMillis(period));
 120         long endTime = System.currentTimeMillis() + period * minEventCount;
 121         while (System.currentTimeMillis() < endTime) {
 122             dummy = new byte[100];
 123             Thread.sleep(1);
 124         }
 125     }
 126 
 127 }