Skip to content

Commit

Permalink
8313722: JFR: Avoid unnecessary calls to Events.from(Recording)
Browse files Browse the repository at this point in the history
Reviewed-by: mgronlun
  • Loading branch information
egahlin committed Dec 1, 2023
1 parent 42af8ce commit 3a09a05
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 34 deletions.
18 changes: 10 additions & 8 deletions test/jdk/jdk/jfr/api/event/TestExtends.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,6 +23,7 @@

package jdk.jfr.api.event;

import java.util.List;
import jdk.jfr.Event;
import jdk.jfr.EventType;
import jdk.jfr.Recording;
Expand Down Expand Up @@ -88,7 +89,8 @@ public static void main(String[] args) throws Exception {
m.commit();

r.stop();
for (RecordedEvent re : Events.fromRecording(r)) {
List<RecordedEvent> events = Events.fromRecording(r);
for (RecordedEvent re : events) {
System.out.println(re);
}
// Grandpa
Expand Down Expand Up @@ -127,18 +129,18 @@ public static void main(String[] args) throws Exception {
verifyField(meType, "hiddenField");
verifyFieldCount(meType, 11);

for (RecordedEvent re : Events.fromRecording(r)) {
for (RecordedEvent re : events) {
System.out.println(re);
}

RecordedEvent grandpa = findEvent(r, GrandpaEvent.class.getName());
RecordedEvent grandpa = findEvent(events, GrandpaEvent.class.getName());
Asserts.assertEquals(grandpa.getValue("gPublicField"), 4);
Asserts.assertEquals(grandpa.getValue("gProtectedField"), 3);
Asserts.assertEquals(grandpa.getValue("gPrivateField"), 2);
Asserts.assertEquals(grandpa.getValue("gDefaultField"), 1);
Asserts.assertEquals(grandpa.getValue("hiddenField"), 4711);

RecordedEvent parent = findEvent(r, ParentEvent.class.getName());
RecordedEvent parent = findEvent(events, ParentEvent.class.getName());
Asserts.assertEquals(parent.getValue("gPublicField"), 4);
Asserts.assertEquals(parent.getValue("gProtectedField"), 3);
Asserts.assertEquals(parent.getValue("gDefaultField"), 1);
Expand All @@ -148,7 +150,7 @@ public static void main(String[] args) throws Exception {
Asserts.assertEquals(parent.getValue("pDefaultField"), 10);
Asserts.assertEquals(parent.getValue("hiddenField"), true);

RecordedEvent me = findEvent(r, MeEvent.class.getName());
RecordedEvent me = findEvent(events, MeEvent.class.getName());
Asserts.assertEquals(me.getValue("gPublicField"), 4);
Asserts.assertEquals(me.getValue("gProtectedField"), 3);
Asserts.assertEquals(me.getValue("gDefaultField"), 1);
Expand All @@ -162,8 +164,8 @@ public static void main(String[] args) throws Exception {
Asserts.assertEquals(me.getValue("hiddenField"), "Hidden");
}

private static RecordedEvent findEvent(Recording r, String name) throws Exception {
for (RecordedEvent re : Events.fromRecording(r)) {
private static RecordedEvent findEvent(List<RecordedEvent> events, String name) throws Exception {
for (RecordedEvent re : events) {
if (re.getEventType().getName().equals(name)) {
return re;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -65,7 +65,7 @@ public static void main(String[] args) throws Exception {
List<RecordedEvent> events = Events.fromRecording(recording);
System.out.println("Events: " + events.size());
Events.hasEvents(events);
for (RecordedEvent event : Events.fromRecording(recording)) {
for (RecordedEvent event : events) {
Events.assertField(event, "size").atLeast(2L * 1024 * 1024);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -65,7 +65,7 @@ public static void main(String[] args) throws Exception {
List<RecordedEvent> events = Events.fromRecording(recording);
System.out.println("Events: " + events.size());
Events.hasEvents(events);
for (RecordedEvent event : Events.fromRecording(recording)) {
for (RecordedEvent event : events) {
Events.assertField(event, "size").atLeast(2L * 1024 * 1024);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -48,10 +48,11 @@ public static void test(String expectedYoungCollector, String expectedOldCollect
GCHelper.callSystemGc(5, true);
recording.stop();

if (!checkCollectors(recording, expectedYoungCollector, expectedOldCollector)) {
List<RecordedEvent> allEvents = Events.fromRecording(recording);
if (!checkCollectors(allEvents, expectedYoungCollector, expectedOldCollector)) {
return;
}
List<RecordedEvent> events = GCHelper.removeFirstAndLastGC(Events.fromRecording(recording));
List<RecordedEvent> events = GCHelper.removeFirstAndLastGC(allEvents);
for (RecordedEvent event : events) {
System.out.println("Event:" + event);
}
Expand Down Expand Up @@ -190,8 +191,8 @@ private static void checkSpace(RecordedEvent event, String structName) {
Asserts.assertEquals(size, end - start, "Size mismatch");
}

private static boolean checkCollectors(Recording recording, String expectedYoung, String expectedOld) throws Exception {
for (RecordedEvent event : Events.fromRecording(recording)) {
private static boolean checkCollectors(List<RecordedEvent> events, String expectedYoung, String expectedOld) throws Exception {
for (RecordedEvent event : events) {
if (Events.isEventType(event, EventNames.GCConfiguration)) {
final String young = Events.assertField(event, "youngCollector").notEmpty().getValue();
final String old = Events.assertField(event, "oldCollector").notEmpty().getValue();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -53,11 +53,11 @@ public static void test(String gcName) throws Exception {
recording.stop();

System.out.println("gcName=" + gcName);
for (RecordedEvent event : Events.fromRecording(recording)) {
List<RecordedEvent> events = Events.fromRecording(recording);
for (RecordedEvent event : events) {
System.out.println("Event: " + event);
}

List<RecordedEvent> events= Events.fromRecording(recording);
Optional<RecordedEvent> gcEvent = events.stream()
.filter(e -> isMySystemGc(e, gcName))
.findFirst();
Expand Down
6 changes: 3 additions & 3 deletions test/jdk/jdk/jfr/event/oldobject/TestMetadataRetention.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -100,10 +100,10 @@ public static void main(String[] args) throws Throwable {
RecordedEvent chunkRotation = findChunkRotationEvent(events);
try {
// Sanity check that class was unloaded
Events.hasEvent(recording, EventNames.ClassUnload);
Events.hasEvent(events, EventNames.ClassUnload);
validateClassUnloadEvent(events);
// Validate that metadata for old object event has survived chunk rotation
Events.hasEvent(recording, EventNames.OldObjectSample);
Events.hasEvent(events, EventNames.OldObjectSample);
validateOldObjectEvent(events, chunkRotation.getStartTime());
} catch (Throwable t) {
t.printStackTrace();
Expand Down
7 changes: 4 additions & 3 deletions test/jdk/jdk/jfr/event/runtime/TestSafepointEvents.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -68,9 +68,10 @@ public static void main(String[] args) throws Exception {

try {
// Verify that each event type was seen at least once
List<RecordedEvent> events = Events.fromRecording(recording);
for (String name : EVENT_NAMES) {
boolean found = false;
for (RecordedEvent event : Events.fromRecording(recording)) {
for (RecordedEvent event : events) {
found = event.getEventType().getName().equals(name);
if (found) {
break;
Expand All @@ -81,7 +82,7 @@ public static void main(String[] args) throws Exception {

// Collect all events grouped by safepoint id
SortedMap<Long, Set<String>> safepointIds = new TreeMap<>();
for (RecordedEvent event : Events.fromRecording(recording)) {
for (RecordedEvent event : events) {
Long safepointId = event.getValue("safepointId");
if (!safepointIds.containsKey(safepointId)) {
safepointIds.put(safepointId, new HashSet<>());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -77,7 +77,7 @@ public static void main(String[] args) throws Throwable {
List<String> classesFound = new ArrayList<String>();
List<RecordedEvent> events = Events.fromRecording(recording);
Events.hasEvents(events);
for (RecordedEvent event : Events.fromRecording(recording)) {
for (RecordedEvent event : events) {
String className = Events.assertField(event, "valueBasedClass.name").notEmpty().getValue();
RecordedThread jt = event.getThread();
if (Thread.currentThread().getName().equals(jt.getJavaName())) {
Expand Down
4 changes: 2 additions & 2 deletions test/jdk/jdk/jfr/event/runtime/TestVMOperation.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -54,7 +54,7 @@ public static void main(String[] args) throws Throwable {

List<RecordedEvent> events = Events.fromRecording(recording);
Events.hasEvents(events);
for (RecordedEvent event : Events.fromRecording(recording)) {
for (RecordedEvent event : events) {
String operation = Events.assertField(event, "operation").notEmpty().getValue();
if (operation.equals(VM_OPERATION)) {
Events.assertField(event, "safepoint").equal(true);
Expand Down
9 changes: 6 additions & 3 deletions test/jdk/jdk/jfr/startupargs/TestRetransform.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,9 +23,11 @@

package jdk.jfr.startupargs;

import java.util.List;
import jdk.jfr.Event;
import jdk.jfr.EventType;
import jdk.jfr.Recording;
import jdk.jfr.consumer.RecordedEvent;
import jdk.test.lib.Asserts;
import jdk.test.lib.jfr.Events;
import jdk.test.lib.jfr.SimpleEvent;
Expand Down Expand Up @@ -58,8 +60,9 @@ public static void main(String[] args) throws Exception {
if (type.isEnabled()) {
Asserts.fail("Expected event to be disabled after recording stopped");
}
Events.hasEvent(r, SimpleEvent.class.getName());
Events.hasEvent(r, TestEvent.class.getName());
List<RecordedEvent> events = Events.fromRecording(r);
Events.hasEvent(events, SimpleEvent.class.getName());
Events.hasEvent(events, TestEvent.class.getName());
}

// Classes that are loaded during a recording
Expand Down
7 changes: 6 additions & 1 deletion test/lib/jdk/test/lib/jfr/Events.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -271,7 +271,12 @@ public static boolean isEventType(RecordedEvent event, String typeName) {
* @throws IOException if an event set could not be created due to I/O
* errors.
*/
private static long lastId = -1;
public static List<RecordedEvent> fromRecording(Recording recording) throws IOException {
if (recording.getId() == lastId) {
throw new IOException("Recording with id " + lastId + " has already been dumped. Store the results in a List<RecordedEvent> instead of dumping the recording again");
}
lastId = recording.getId();
return RecordingFile.readAllEvents(makeCopy(recording));
}

Expand Down

1 comment on commit 3a09a05

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.