KEMBAR78
Sample JVM method exit via bytecode rewriting by fandreuz · Pull Request #1421 · async-profiler/async-profiler · GitHub
Skip to content

Conversation

@fandreuz
Copy link
Contributor

@fandreuz fandreuz commented Jul 29, 2025

Description

In this PR I enable recording JVM method exit via bytecode instrumentation. The bulk of this PR consists in:

  • tracking all exit points for the method
  • inserting invocation towards async-profiler functions
  • fixing jump offsets

I'm also adding a new parameter called latency.

  • When latency is absent, the previous behavior is kept
  • When latency is specified (can be 0) any execution of the method which exceeds latency is going to record a sample. total will contain the duration of the method execution.

Related issues

n/a

Motivation and context

There are several use cases enabled by this PR:

  • Recording the total duration of a method
  • Taking a sample only when the latency of a method exceeds a threshold

How has this been tested?

New tests in InstrumentTests.


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

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

⚠️ Clang-Tidy found issue(s) with the introduced code (1/1)

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

⚠️ Clang-Tidy found issue(s) with the introduced code (1/1)

@fandreuz fandreuz changed the title Enable recording JVM method exit via bytecode instrumentation Sample JVM method exit via bytecode instrumentation Jul 29, 2025
@fandreuz
Copy link
Contributor Author

fandreuz commented Aug 1, 2025

We're also missing tableswitch

@fandreuz fandreuz force-pushed the instrument-method-end branch from bb14c68 to 579e2d6 Compare September 5, 2025 11:09
@fandreuz fandreuz force-pushed the instrument-method-end branch from 9a51a88 to 2cbfe2a Compare September 5, 2025 12:28
const char* class_name;
const char* method_name;
int out = rewriteClass(&class_name, &method_name);
if (out == 0) {
Copy link
Member

Choose a reason for hiding this comment

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

Can be a case in switch below

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, I reverted this after 0f9931d, because this would cause some repetition where I handle errors


constexpr u16 MAX_CODE_LENGTH = 65534;

enum {
Copy link
Member

Choose a reason for hiding this comment

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

Make it named enum and let functions return this type instead of int.
Constants no longer need to be negative.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@apangin apangin merged commit 2e0e3ab into async-profiler:master Sep 6, 2025
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants