GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Optimize method tracing when the function is not profiled by fandreuz · Pull Request #1471 · async-profiler/async-profiler

Conversation

@fandreuz

@fandreuz fandreuz commented Sep 10, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Description

In the current implementation, we always make a JNI call to recordExit regardless of whether the current function call breaks the latency threshold or not. We could move the check from C++ to Java, so we could save a JNI call when the method is not going to be profiled.

Related issues

#1421

Motivation and context

This simple class shows a significant improvement in terms of throughput:

public class Test {
    private static volatile long x = 0;

    public static void doStuff() throws Exception {
        ++x;
    }

    public static void main(String[] args) throws Exception {
        long start = System.nanoTime();
        while (System.nanoTime() - start < 10_000_000_000l) {
            doStuff();
        }
        System.out.println(x);
    }
}
Branch Value
master 83084633
latency-jni-optimize 119116380

So we get approximately 40% more method calls when the method is never profiled (in this case the workload is negligible).

How has this been tested?

The current set of tests is good enough to validate this change.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
fandreuz added 7 commits September 9, 2025 19:00
@fandreuz
rE0
0527639
@fandreuz
take duration
9057458
@fandreuz
no bool
8c6bdc9
@fandreuz
do stuff
211cb3c
@fandreuz
logs
f36b5b3
@fandreuz
ns
048fb57
@fandreuz
comment and logs
2b50ebf
fandreuz
fandreuz commented Sep 10, 2025
View reviewed changes
Comment thread src/helper/one/profiler/Instrument.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

fandreuz added 2 commits September 10, 2025 08:40
@fandreuz
fix cmpl
46e8eb5
@fandreuz
ops
afedffe
@fandreuz
fandreuz force-pushed the latency-jni-optimize-v2 branch from 6bf8adb to afedffe Compare September 10, 2025 09:07
Baraa-Hasheesh
Baraa-Hasheesh reviewed Sep 10, 2025
View reviewed changes
Comment thread src/instrument.cpp Resolved

Uh oh!

There was an error while loading. Please reload this page.

@fandreuz
fandreuz force-pushed the latency-jni-optimize-v2 branch from 28079b3 to afedffe Compare September 10, 2025 14:01
Baraa-Hasheesh
Baraa-Hasheesh reviewed Sep 11, 2025
View reviewed changes
Comment thread src/profiler.cpp Resolved

Uh oh!

There was an error while loading. Please reload this page.

@fandreuz
fix switch fmt
b7fec95
apangin
apangin reviewed Sep 11, 2025
View reviewed changes
Comment thread src/instrument.cpp Outdated
void JNICALL Instrument::recordExit0(JNIEnv* jni, jobject unused, jlong durationNs) {
if (!_enabled) return;

u64 now_ticks = TSC::ticks();

@apangin apangin Sep 11, 2025

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

What worries me most in this change is that it introduces inaccuracy in reported timings.
There can be a potentially large delay between the last call to System.nanoTime and this TSC::ticks()`, especially if a thread is de-scheduled from CPU in this time window. This delay can cause shift in the reported event start time, which may in turn break correlation with other events.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@fandreuz fandreuz Sep 12, 2025

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I see, how about 027ba68 ?

We'll record a slightly higher durationNs than what we use to test the latency. But if anything, the measurement is going to be more realistic, since it'll also include the JNI penalty.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@apangin apangin Sep 12, 2025

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

That's better. Now timings should be more accurate, for the price of an extra nanotime call.
I know how to improve this further by leveraging jdk.jfr.internal.JVM.counterTime() intrinsic in Java code, but this will require lots of changes. We can discuss it and probably leave for a separate PR.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

fandreuz reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread src/profiler.cpp Resolved

Uh oh!

There was an error while loading. Please reload this page.

fandreuz added 3 commits September 12, 2025 09:16
@fandreuz
pass startTimeNs
027ba68
@fandreuz
no switch
454e908
@fandreuz
inline
377c867
apangin
apangin reviewed Sep 12, 2025
View reviewed changes
Comment thread src/instrument.cpp Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/instrument.cpp Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/instrument.cpp
Comment on lines +353 to +358
case Result::CLASS_DOES_NOT_MATCH:
Log::debug("Skipping instrumentation of %s: class does not match", class_name.c_str());
break;
case Result::PROFILER_CLASS:
Log::trace("Skipping instrumentation of %s: internal profiler class", class_name.c_str());
break;

@apangin apangin Sep 12, 2025

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Why debug in one case and trace in another?
But in my opinion, neither of these logs is useful.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@apangin apangin Sep 12, 2025

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Also, add

default:
    break;

to suppress compiler warning.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@fandreuz fandreuz Sep 15, 2025

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

But in my opinion, neither of these logs is useful.

I agree, that's why I made them invisible by default. The idea is not to skip quietly any decision I take during the flow, and let this information be available upon closer inspection.

to suppress compiler warning.

e87c715

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@apangin apangin Sep 15, 2025

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

But these messages get recorded in .jfr anyway.
I suggest to remove them completely or change them both to trace and adjust logging not write trace level logs to jfr.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@fandreuz fandreuz Sep 15, 2025

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I see, I didn't know they go there. 74053fc

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/instrument.cpp Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/instrument.cpp Outdated
void JNICALL Instrument::recordExit0(JNIEnv* jni, jobject unused, jlong durationNs) {
if (!_enabled) return;

u64 now_ticks = TSC::ticks();

@apangin apangin Sep 12, 2025

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

That's better. Now timings should be more accurate, for the price of an extra nanotime call.
I know how to improve this further by leveraging jdk.jfr.internal.JVM.counterTime() intrinsic in Java code, but this will require lots of changes. We can discuss it and probably leave for a separate PR.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

fandreuz reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
fandreuz added 3 commits September 15, 2025 08:35
@fandreuz
review comments
e87c715
@fandreuz
dont write trace
74053fc
@fandreuz
ops
bd4b91b
apangin
apangin reviewed Sep 15, 2025
View reviewed changes
Comment thread src/flightRecorder.cpp Outdated
Comment on lines +1023 to +1024
buf->putVar32(LOG_ERROR - FlightRecorder::MIN_LOG_LEVEL + 1);
for (int i = FlightRecorder::MIN_LOG_LEVEL; i <= LOG_ERROR; i++) {

@apangin apangin Sep 15, 2025

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Leave the code as it was. There's nothing bad in listing all available levels, especially since there is a (theoretical) possibility to call writeLog directly bypassing checks in Log::log.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@fandreuz fandreuz Sep 15, 2025

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I see, reverted in 148e6b3

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/log.cpp Outdated
}

void Log::log(LogLevel level, const char* msg, va_list args) {
if (_level > level && FlightRecorder::MIN_LOG_LEVEL > level) {

@apangin apangin Sep 15, 2025

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

nit: I'd reverse the check: when an input argument participates in some condition, it better reads when an argument is on the left side. CONSTANT > arg looks unnatural to me.

if (level < _level && level < FlightRecorder::MIN_LOG_LEVEL)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

fandreuz reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@fandreuz fandreuz Sep 15, 2025

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

148e6b3

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/log.cpp Outdated

// Write all messages to JFR, if active
if (level < LOG_ERROR) {
if (level >= FlightRecorder::MIN_LOG_LEVEL) {

@apangin apangin Sep 15, 2025

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Have you removed < LOG_ERROR intentionally?
Error message typically indicates a situation when profiler is unable to execute a user command. Errors don't happen out of the blue at runtime and don't need to be recorded.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

fandreuz reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@fandreuz fandreuz Sep 15, 2025

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Got it, 148e6b3

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
apangin
apangin reviewed Sep 15, 2025
View reviewed changes
Comment thread src/helper/one/profiler/Instrument.java

public static void recordExit(long startTimeNs, long minLatency) {
if (System.nanoTime() - startTimeNs >= minLatency) {
recordExit0(startTimeNs);

@apangin apangin Sep 15, 2025

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

If latency == 0, you can call recordExit0 directly from the generated code. Even the name of the method suits well :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@apangin apangin Sep 15, 2025

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Ah, this will complicate decision how many frames to skip in the stacktrace.
But I think the optimization to skip redundant nanoTime call is useful. Maybe we can add another variant of recordExit call? wdyt?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@fandreuz fandreuz Sep 15, 2025

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Maybe we can add another variant of recordExit call?

Yeah I think this is the easiest way: a9a8604

Or we add something like EventType::METHOD_TRACE_ZERO.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
fandreuz added 2 commits September 15, 2025 18:52
@fandreuz
review
148e6b3
@fandreuz
zero
a9a8604
@apangin
apangin merged commit 6f2a9b8 into async-profiler:master Sep 15, 2025
krk pushed a commit to krk/async-profiler that referenced this pull request Sep 22, 2025
@fandreuz @krk
Optimize method tracing when the function is not profiled (async-prof…
28340ae
…iler#1471)
jbachorik pushed a commit to DataDog/async-profiler that referenced this pull request Jan 2, 2026
@fandreuz @jbachorik
Optimize method tracing when the function is not profiled (async-prof…
3f9adc8
…iler#1471)
jbachorik pushed a commit to DataDog/async-profiler that referenced this pull request Jan 6, 2026
@fandreuz @jbachorik
Optimize method tracing when the function is not profiled (async-prof…
62660e6
…iler#1471)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@apangin apangin apangin left review comments
+1 more reviewer
@Baraa-Hasheesh Baraa-Hasheesh Baraa-Hasheesh left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@fandreuz @apangin @Baraa-Hasheesh
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.