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.time.Instant;
  31 import java.util.List;
  32 
  33 import jdk.jfr.FlightRecorder;
  34 import jdk.jfr.Recording;
  35 import jdk.jfr.consumer.RecordedEvent;
  36 import jdk.test.lib.Asserts;
  37 import jdk.test.lib.jfr.Events;
  38 import jdk.test.lib.jfr.SimpleEvent;
  39 
  40 /**
  41  * @test
  42  * @summary Test periodic setting that involves chunks.
  43  * @key jfr
  44  * @requires vm.hasJFR
  45  * @library /test/lib
  46  * @run main/othervm jdk.jfr.api.recording.event.TestChunkPeriod
  47  */
  48 public class TestChunkPeriod {
  49 
  50     // Margin of error is to avoid issues where JFR and
  51     // System.currentMillis take the clock differently
  52     private static final Duration MARGIN_OF_ERROR = Duration.ofNanos(1_000_000_000); // 1 s
  53 
  54     public static void main(String[] args) throws Throwable {
  55         FlightRecorder.addPeriodicEvent(SimpleEvent.class, () -> {
  56             SimpleEvent  pe = new SimpleEvent();
  57             pe.commit();
  58         });
  59         testBeginChunk();
  60         testEndChunk();
  61         testEveryChunk();
  62     }
  63 
  64     private static void testBeginChunk() throws IOException {
  65         Recording r = new Recording();
  66         r.enable(SimpleEvent.class).with("period", "beginChunk");
  67         Instant beforeStart = Instant.now().minus(MARGIN_OF_ERROR);
  68         r.start();
  69         Instant afterStart = Instant.now().plus(MARGIN_OF_ERROR);
  70         r.stop();
  71         List<RecordedEvent> events = Events.fromRecording(r);
  72         Asserts.assertEquals(events.size(), 1, "Expected one event with beginChunk");
  73         RecordedEvent event = events.get(0);
  74         Asserts.assertGreaterThanOrEqual(event.getStartTime(), beforeStart);
  75         Asserts.assertGreaterThanOrEqual(afterStart, event.getStartTime());
  76         r.close();
  77     }
  78 
  79     private static void testEndChunk() throws IOException {
  80         Recording r = new Recording();
  81         r.enable(SimpleEvent.class).with("period", "endChunk");
  82         r.start();
  83         Instant beforeStop = Instant.now().minus(MARGIN_OF_ERROR);
  84         r.stop();
  85         Instant afterStop =  Instant.now().plus(MARGIN_OF_ERROR);
  86         List<RecordedEvent> events = Events.fromRecording(r);
  87         Asserts.assertEquals(events.size(), 1, "Expected one event with endChunk");
  88         RecordedEvent event = events.get(0);
  89         Asserts.assertGreaterThanOrEqual(event.getStartTime(), beforeStop);
  90         Asserts.assertGreaterThanOrEqual(afterStop, event.getStartTime());
  91         r.close();
  92     }
  93 
  94     private static void testEveryChunk() throws IOException {
  95         Recording r = new Recording();
  96         r.enable(SimpleEvent.class).with("period", "everyChunk");
  97         r.start();
  98         r.stop();
  99         List<RecordedEvent> events = Events.fromRecording(r);
 100         Asserts.assertEquals(events.size(), 2, "Expected two events with everyChunk");
 101         r.close();
 102     }
 103 }