1 /*
   2  * Copyright (c) 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.event.runtime;
  27 
  28 import java.io.IOException;
  29 import java.time.Duration;
  30 import java.util.List;
  31 
  32 import jdk.jfr.Recording;
  33 import jdk.jfr.consumer.RecordedClass;
  34 import jdk.jfr.consumer.RecordedClassLoader;
  35 import jdk.jfr.consumer.RecordedEvent;
  36 import jdk.test.lib.Asserts;
  37 import jdk.test.lib.jfr.EventNames;
  38 import jdk.test.lib.jfr.Events;
  39 import jdk.test.lib.jfr.TestClassLoader;
  40 
  41 /**
  42  * @test
  43  * @key jfr
  44  * @requires vm.hasJFR
  45  * @library /test/lib /test/jdk
  46  * @build jdk.jfr.event.runtime.TestClasses
  47  * @run main/othervm jdk.jfr.event.runtime.TestTableStatisticsEvent
  48  */
  49 public final class TestTableStatisticsEvent {
  50 
  51     static private void exerciseStringTable() {
  52       final int SIZE = 10000;
  53       String strings[] = new String[SIZE];
  54       for (int i=0; i<SIZE; i++) {
  55         strings[i] = new String("New String "+i);
  56       }
  57     }
  58 
  59     static private void validateCommonTableEvent(RecordedEvent event, String name) throws IOException {
  60       Long numberOfBuckets = event.getValue("numberOfBuckets");
  61       Long numberOfEntries = event.getValue("numberOfEntries");
  62       Long totalFootprint = event.getValue("totalFootprint");
  63       Long maximumBucketCount = event.getValue("maximumBucketCount");
  64       float averageBucketCount = event.getValue("averageBucketCount");
  65       float varianceOfBucketCount = event.getValue("varianceOfBucketCount");
  66       float stdDevOfBucketCount = event.getValue("stdDevOfBucketCount");
  67       float addRate = event.getValue("insertionRate");
  68       float removeRate = event.getValue("removalRate");
  69       
  70       Asserts.assertTrue((numberOfBuckets>0), "No buckets for " + name);
  71       Asserts.assertTrue((totalFootprint>0), "Zero sized table for " + name);
  72       Asserts.assertTrue((maximumBucketCount>=averageBucketCount), "Maximum bucket count not equal to or greater than the average for " + name);
  73       if ((addRate > 0.0) && (addRate > removeRate)) {
  74         Asserts.assertTrue((numberOfEntries>0), "Entries marked as added, but no entries found for " + name);
  75       }
  76     }
  77 
  78     static private void validateStringTableEvent(RecordedEvent event, String name) throws IOException {
  79       validateCommonTableEvent(event, name);
  80 
  81       Long numberOfEntries = event.getValue("numberOfEntries");
  82       Asserts.assertTrue((numberOfEntries>0), "No entries for " + name);
  83     }
  84 
  85     static private void testStringTableStatisticsEvent(Recording recording, String name) throws IOException {
  86       boolean gotEvent = false;
  87       List<RecordedEvent> events = Events.fromRecording(recording);
  88       for (RecordedEvent event : events) {
  89         if (event.toString().contains(name)) {
  90           gotEvent = true;
  91           validateStringTableEvent(event, name);
  92         }
  93       }
  94       Asserts.assertTrue(gotEvent, "No table statistics event found for " + name);
  95     }
  96 
  97     static private void testCommonTableStatisticsEvent(Recording recording, String name) throws IOException {
  98       boolean gotEvent = false;
  99       List<RecordedEvent> events = Events.fromRecording(recording);
 100       for (RecordedEvent event : events) {
 101         if (event.toString().contains(name)) {
 102           gotEvent = true;
 103           validateCommonTableEvent(event, name);
 104         }
 105       }
 106       Asserts.assertTrue(gotEvent, "No table statistics event found for " + name);
 107     }
 108 
 109     public static void main(String[] args) throws Throwable {
 110       final long DURATION_MS = 2000;
 111       Recording recording = new Recording();
 112       recording.enable(EventNames.SymbolTableStatistics).withThreshold(Duration.ofMillis(DURATION_MS));
 113       recording.enable(EventNames.StringTableStatistics).withThreshold(Duration.ofMillis(DURATION_MS));
 114       recording.enable(EventNames.PlaceholderTableStatistics).withThreshold(Duration.ofMillis(DURATION_MS));
 115       recording.enable(EventNames.LoaderConstraintsTableStatistics).withThreshold(Duration.ofMillis(DURATION_MS));
 116       recording.enable(EventNames.ProtectionDomainCacheTableStatistics).withThreshold(Duration.ofMillis(DURATION_MS));
 117       recording.start();
 118       exerciseStringTable();
 119       Thread.currentThread().sleep(DURATION_MS/1000);
 120       System.gc();
 121       recording.stop();
 122 
 123       testCommonTableStatisticsEvent(recording, "SymbolTable");
 124       testStringTableStatisticsEvent(recording, "StringTable");
 125       testCommonTableStatisticsEvent(recording, "PlaceholderTable");
 126       testCommonTableStatisticsEvent(recording, "StringTable");
 127       testCommonTableStatisticsEvent(recording, "LoaderConstraintsTable");
 128       testCommonTableStatisticsEvent(recording, "ProtectionDomainCacheTable");
 129     }
 130 }