Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GR-61151] Trim internal JFR stacktraces to match OpenJDK #10385

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,9 @@ public Boolean getValue(OptionValues values) {
@Option(help = "file:doc-files/StartFlightRecordingHelp.txt")//
public static final RuntimeOptionKey<String> StartFlightRecording = new RuntimeOptionKey<>("", Immutable);

@Option(help = "Determine whether to trim internal frames from JFR stacktraces (defaults to true).")//
public static final RuntimeOptionKey<Boolean> JfrTrimInternalStackTraces = new RuntimeOptionKey<>(true);

@Option(help = "file:doc-files/FlightRecorderLoggingHelp.txt")//
public static final RuntimeOptionKey<String> FlightRecorderLogging = new RuntimeOptionKey<>("all=warning", Immutable);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public Class<?> getSourceClass() {
return sourceClass;
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public String getSourceClassName() {
Class<?> clazz = getSourceClass();
return (clazz != null) ? clazz.getName() : "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static RuntimeSupport.Hook initializationHook() {
public static RuntimeSupport.Hook startupHook() {
return isFirstIsolate -> {
periodicEventSetup();

SubstrateJVM.getStackTraceRepo().setTrimInternalStackTraces(SubstrateOptions.JfrTrimInternalStackTraces.getValue());
boolean startRecording = SubstrateOptions.FlightRecorder.getValue() || !SubstrateOptions.StartFlightRecording.getValue().isEmpty();
if (startRecording) {
initRecording();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public class JfrStackTraceRepository implements JfrRepository {
private final JfrStackTraceEpochData epochData1;

private int stackTraceDepth;
private boolean trimInternalStackTraces;
Copy link
Member

Choose a reason for hiding this comment

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

I guess this is a constant at runtime. Too bad that we can't declare it as final here. Not sure how important that is in practice though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes it's a bit unfortunate. It gets set during startup once the runtime options are parsed. We also need to change this during the JFR tests.

Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to turn the option from a RuntimeOptionKey into a HostedOptionKey and make sure trimming is installed at build time? Is it really necessary for the option to be mutable at run time?

Copy link
Collaborator Author

@roberttoyonaga roberttoyonaga Jan 6, 2025

Choose a reason for hiding this comment

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

Yes, I think it could make sense to set this option at build time with a HostedOptionKey. The 2 downsides would be:

  1. We'd need to remove the test that checks trimming stacktraces, since we won't be able to toggle the option dynamically at runtime. Is there a way to provide HostedOptionKey to the mx native-unittest build?
  2. Users will need to rebuild their executables if they discover that they need full stacktraces. This is probably not much of a problem since I think most users won't need that.

Do you think it's worth it?

Copy link
Member

@fniephaus fniephaus Jan 6, 2025

Choose a reason for hiding this comment

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

RE 1: I guess you could unconditionally disable trimming in native_unittests_task in mx_substratevm.py. That should do the try on the gate. When you want to run mx native-unittest on the cmd line, you need to pass it as an additional build flag.
RE 2: Agreed, normal users are probably not too interested in this. Disabling trimming only really seems useful to measure JFR overhead, something only we really care about, right?

Do you think it's worth it?

Leaving this up to you. :)


@Platforms(Platform.HOSTED_ONLY.class)
JfrStackTraceRepository() {
Expand All @@ -89,6 +90,15 @@ public int getStackTraceDepth() {
return stackTraceDepth;
}

public void setTrimInternalStackTraces(boolean value) {
trimInternalStackTraces = value;
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public boolean getTrimInternalStackTraces() {
return trimInternalStackTraces;
}

public void teardown() {
epochData0.teardown();
epochData1.teardown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.oracle.svm.core.code.CodeInfoTable;
import com.oracle.svm.core.code.FrameInfoQueryResult;
import com.oracle.svm.core.code.UntetheredCodeInfo;
import com.oracle.svm.core.jdk.UninterruptibleUtils;
import com.oracle.svm.core.jfr.JfrBuffer;
import com.oracle.svm.core.jfr.JfrFrameType;
import com.oracle.svm.core.jfr.JfrNativeEventWriter;
Expand All @@ -56,6 +57,7 @@
public final class SamplerJfrStackTraceSerializer implements SamplerStackTraceSerializer {
/** This value is used by multiple threads but only by a single thread at a time. */
private static final CodeInfoDecoder.FrameInfoCursor FRAME_INFO_CURSOR = new CodeInfoDecoder.FrameInfoCursor();
private static final String SUBSTRATEVM_PREFIX = "com.oracle.svm";

@Override
@Uninterruptible(reason = "Prevent JFR recording and epoch change.")
Expand Down Expand Up @@ -167,8 +169,12 @@ private static int visitFrame(JfrNativeEventWriterData data, CodeInfo codeInfo,
int numStackTraceElements = 0;
FRAME_INFO_CURSOR.initialize(codeInfo, ip, false);
while (FRAME_INFO_CURSOR.advance()) {
FrameInfoQueryResult frame = FRAME_INFO_CURSOR.get();
if (SubstrateJVM.getStackTraceRepo().getTrimInternalStackTraces() &&
Copy link
Member

Choose a reason for hiding this comment

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

Since it's constant, I prefer moving the condition before the loop, even if the compiler can do that for us ;)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hmm I'm not sure we can move the conditional out of the loop since we must recheck it with each new advancement of FRAME_INFO_CURSOR. SubstrateJVM.getStackTraceRepo().getTrimInternalStackTraces() is constant but we can't even move that part outside since we still need to execute the loop to potentially serialize frames.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I was only talking about moving SubstrateJVM.getStackTraceRepo().getTrimInternalStackTraces() outside the loop.

UninterruptibleUtils.String.startsWith(frame.getSourceClassName(), SUBSTRATEVM_PREFIX)) {
continue;
}
if (data.isNonNull()) {
FrameInfoQueryResult frame = FRAME_INFO_CURSOR.get();
serializeStackTraceElement(data, frame);
Copy link
Member

Choose a reason for hiding this comment

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

Should this additional check be within the if (data.isNonNull()) { branch or not?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, maybe moving it inside if (data.isNonNull()) would be a small optimization

Copy link
Collaborator Author

@roberttoyonaga roberttoyonaga Jan 6, 2025

Choose a reason for hiding this comment

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

Actually, I don't think it should be moved inside the if (data.isNonNull()) check. We must increment numStackTraceElements regardless of the if (data.isNonNull()) check, but must not increment if we are trimming the current stacktrace.

}
numStackTraceElements++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.graalvm.nativeimage.hosted.RuntimeProxyCreation;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;

import com.oracle.svm.core.jfr.HasJfrSupport;
Expand All @@ -65,6 +66,11 @@ public static void checkForJFR() {
assumeTrue("skipping JFR tests", !ImageInfo.inImageCode() || HasJfrSupport.get());
}

@Before
public void disableTrimStacktraces() {
SubstrateJVM.getStackTraceRepo().setTrimInternalStackTraces(false);
}

@After
public void deleteTemporaryFiles() throws Throwable {
if (!isDebuggingEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2025, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2025, 2025, Red Hat Inc. 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package com.oracle.svm.test.jfr;

import com.oracle.svm.test.jfr.events.StackTraceEvent;
import com.oracle.svm.core.jfr.SubstrateJVM;

import jdk.jfr.Recording;
import jdk.jfr.consumer.RecordedEvent;
import jdk.jfr.consumer.RecordedFrame;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class TestTrimStackTraces extends JfrRecordingTest {
@Rule public TestName testName = new TestName();

@Test
public void test() throws Throwable {
String[] events = new String[]{StackTraceEvent.class.getName()};
SubstrateJVM.getStackTraceRepo().setTrimInternalStackTraces(true);
Recording recording = startRecording(events);

StackTraceEvent event = new StackTraceEvent();
event.commit();

stopRecording(recording, this::validateEvents);
}

private void validateEvents(List<RecordedEvent> events) {
assertEquals(1, events.size());
RecordedEvent event = events.getFirst();
List<RecordedFrame> frames = event.getStackTrace().getFrames();
assertTrue(frames.size() > 0);
assertFalse(frames.stream().anyMatch(e -> testName.getMethodName().equals(e.getMethod().getName())));
}
}