Path: blob/master/test/jdk/jdk/jfr/event/gc/objectcount/ObjectCountAfterGCEvent.java
41710 views
/*1* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package jdk.jfr.event.gc.objectcount;2425import java.util.List;26import java.util.Optional;27import java.util.stream.Collectors;2829import jdk.jfr.Recording;30import jdk.jfr.consumer.RecordedEvent;31import jdk.test.lib.Asserts;32import jdk.test.lib.jfr.EventNames;33import jdk.test.lib.jfr.Events;343536public class ObjectCountAfterGCEvent {3738private static final String objectCountEventPath = EventNames.ObjectCountAfterGC;39private static final String gcEventPath = EventNames.GarbageCollection;40private static final String heapSummaryEventPath = EventNames.GCHeapSummary;4142public static void test(String gcName) throws Exception {43Recording recording = new Recording();44recording.enable(objectCountEventPath);45recording.enable(gcEventPath);46recording.enable(heapSummaryEventPath);4748ObjectCountEventVerifier.createTestData();49recording.start();50System.gc();51System.gc();52recording.stop();5354System.out.println("gcName=" + gcName);55for (RecordedEvent event : Events.fromRecording(recording)) {56System.out.println("Event: " + event);57}5859List<RecordedEvent> events= Events.fromRecording(recording);60Optional<RecordedEvent> gcEvent = events.stream()61.filter(e -> isMySystemGc(e, gcName))62.findFirst();63Asserts.assertTrue(gcEvent.isPresent(), "No event System.gc event of type " + gcEventPath);64System.out.println("Found System.gc event: " + gcEvent.get());65int gcId = Events.assertField(gcEvent.get(), "gcId").getValue();6667List<RecordedEvent> objCountEvents = events.stream()68.filter(e -> Events.isEventType(e, objectCountEventPath))69.filter(e -> isGcId(e, gcId))70.collect(Collectors.toList());71Asserts.assertFalse(objCountEvents.isEmpty(), "No objCountEvents for gcId=" + gcId);7273Optional<RecordedEvent> heapSummaryEvent = events.stream()74.filter(e -> Events.isEventType(e, heapSummaryEventPath))75.filter(e -> isGcId(e, gcId))76.filter(e -> "After GC".equals(Events.assertField(e, "when").getValue()))77.findFirst();78Asserts.assertTrue(heapSummaryEvent.isPresent(), "No heapSummary for gcId=" + gcId);79System.out.println("Found heapSummaryEvent: " + heapSummaryEvent.get());8081Events.assertField(heapSummaryEvent.get(), "heapUsed").atLeast(0L).getValue();82ObjectCountEventVerifier.verify(objCountEvents);83}8485private static boolean isGcId(RecordedEvent event, int gcId) {86return gcId == (int)Events.assertField(event, "gcId").getValue();87}8889private static boolean isMySystemGc(RecordedEvent event, String gcName) {90return Events.isEventType(event, gcEventPath) &&91gcName.equals(Events.assertField(event, "name").getValue()) &&92"System.gc()".equals(Events.assertField(event, "cause").getValue());93}9495}969798