1 /*
   2  * Copyright (c) 2013, 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.event.runtime;
  27 
  28 import static jdk.test.lib.Asserts.assertTrue;
  29 
  30 import java.time.Duration;
  31 import java.util.ArrayList;
  32 import java.util.List;
  33 import java.util.Random;
  34 
  35 import jdk.jfr.Recording;
  36 import jdk.jfr.consumer.RecordedEvent;
  37 import jdk.test.lib.jfr.EventNames;
  38 import jdk.test.lib.jfr.Events;
  39 
  40 /**
  41  * @test
  42  * @key jfr
  43  *
  44  * @library /lib /
  45  * @run main/othervm jdk.jfr.event.runtime.TestJavaThreadStatisticsEvent
  46  */
  47 public class TestJavaThreadStatisticsEvent {
  48     private static final String EVENT_NAME = EventNames.JavaThreadStatistics;
  49 
  50     private static final int THREAD_COUNT = 10;
  51     private static final Random RAND = new Random(4711);
  52 
  53     // verify thread count: 0 <= daemon <= active <= peak <= accumulated.
  54     public static void main(String[] args) throws Throwable {
  55         Recording recording = new Recording();
  56         recording.enable(EVENT_NAME).withThreshold(Duration.ofMillis(10));
  57         recording.start();
  58 
  59         List<Thread> threads = new ArrayList<>();
  60         for (int i = 0; i < THREAD_COUNT; i++) {
  61             Thread thread = new Thread(TestJavaThreadStatisticsEvent::sleepRandom);
  62             thread.start();
  63             threads.add(thread);
  64         }
  65         for (Thread thread : threads) {
  66             thread.join();
  67         }
  68 
  69         recording.stop();
  70         List<RecordedEvent> events = Events.fromRecording(recording);
  71         boolean isAnyFound = false;
  72         for (RecordedEvent event : events) {
  73             System.out.println("Event:" + event);
  74             isAnyFound = true;
  75             // verify thread count: 0 <= daemon <= active <= peak <= accumulated.
  76             long daemonCount = Events.assertField(event, "daemonCount").atLeast(0L).getValue();
  77             long activeCount = Events.assertField(event, "activeCount").atLeast(daemonCount).getValue();
  78             long peakCount = Events.assertField(event, "peakCount").atLeast(activeCount).atLeast(1L).getValue();
  79             Events.assertField(event, "accumulatedCount").atLeast(peakCount).getValue();
  80         }
  81         assertTrue(isAnyFound, "Correct event not found");
  82     }
  83 
  84     static void sleepRandom() {
  85         try {
  86             Thread.sleep(RAND.nextInt(10));
  87         } catch (InterruptedException e) {
  88             e.printStackTrace();
  89         }
  90     }
  91 }