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.gc.refstat;
  27 
  28 import java.time.Duration;
  29 import java.util.ArrayList;
  30 import java.util.Arrays;
  31 import java.util.List;
  32 import java.util.stream.Collectors;
  33 
  34 import jdk.jfr.Recording;
  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.GCHelper;
  40 
  41 
  42 public class RefStatEvent {
  43 
  44     private static final String gcEventPath = EventNames.GarbageCollection;
  45     private static final String refStatsEventPath = EventNames.GCReferenceStatistics;
  46     public static byte[] garbage;
  47 
  48     public static void test(String gcName) throws Exception {
  49         Recording recording = new Recording();
  50         recording.enable(refStatsEventPath).withThreshold(Duration.ofMillis(0));
  51         recording.enable(gcEventPath).withThreshold(Duration.ofMillis(0));
  52 
  53         recording.start();
  54         GCHelper.callSystemGc(6, true);
  55         recording.stop();
  56 
  57         System.out.println("gcName=" + gcName);
  58         List<RecordedEvent> events = GCHelper.removeFirstAndLastGC(Events.fromRecording(recording));
  59         Events.hasEvents(events);
  60         for (RecordedEvent event : events) {
  61             System.out.println("Event: " + event);
  62         }
  63 
  64         for (RecordedEvent event : events) {
  65             if (!Events.isEventType(event, gcEventPath)) {
  66                 continue;
  67             }
  68             System.out.println("Event: " + event);
  69             final int gcId = Events.assertField(event, "gcId").getValue();
  70             final String name = Events.assertField(event, "name").notEmpty().getValue();
  71             if (gcName.equals(name)) {
  72                 verifyRefStatEvents(events, gcId);
  73             }
  74         }
  75     }
  76 
  77     // Check that we have a refStat event for each type for this GC.
  78     private static void verifyRefStatEvents(List<RecordedEvent> events, int gcId) {
  79         List<String> expectedTypes = Arrays.asList("Soft reference", "Weak reference", "Final reference", "Phantom reference");
  80         List<String> actualTypes = new ArrayList<>();
  81         try {
  82             for (RecordedEvent event : events) {
  83                 if (!Events.isEventType(event, refStatsEventPath)) {
  84                     continue;
  85                 }
  86                 Events.assertField(event, "count").atLeast(0L);
  87                 if (Events.assertField(event, "gcId").isEqual(gcId)) {
  88                     actualTypes.add(Events.assertField(event, "type").notEmpty().getValue());
  89                 }
  90             }
  91 
  92             Asserts.assertEquals(actualTypes.size(), expectedTypes.size(), "Wrong number of refStat events");
  93             Asserts.assertTrue(expectedTypes.containsAll(actualTypes), "Found unknown refStat types");
  94             Asserts.assertTrue(actualTypes.containsAll(expectedTypes), "Missning refStat types");
  95         } catch (Exception e) {
  96             System.out.println("Expected refStatTypes: " + expectedTypes.stream().collect(Collectors.joining(", ")));
  97             System.out.println("Got refStatTypes: " + actualTypes.stream().collect(Collectors.joining(", ")));
  98             throw e;
  99         }
 100     }
 101 
 102 }