Skip to content

Conversation

Arraying
Copy link
Member

@Arraying Arraying commented Aug 22, 2025

Hi all,

This PR refactors javaClasses' print_stack_element_to_stream to use a stringStream instead of manually maintaining a buffer with os::snprintf chains.

The JBS issue outlines to do this for all occurrences of snprintf-like chains. The majority of the calls to os::snprintf, os::snprintf_checked, os::vsnprintf, etc. occur just once. LogTagSet::vwrite is the only place to my knowledge where there remains a vsnprintf chain using manual buffer arithmetic. After consulting @jdksjolen, we decided that due to the
a) low-level nature of the code, and
b) widespread usage of logging in the VM,
this should probably warrant a larger discussion and/or separate issue. Personally, I feel like the performance & correctness risks of introducing a high-level abstraction here outweighs the benefits.

I've run tests with JDK tiers 1-3 on macOS (AArch64, x64), Linux (AArch64, x64), and Windows (x64); all green.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8364103: Convert existing sprintf-chains to stringStream (Enhancement - P4)

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/26894/head:pull/26894
$ git checkout pull/26894

Update a local copy of the PR:
$ git checkout pull/26894
$ git pull https://git.openjdk.org/jdk.git pull/26894/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 26894

View PR using the GUI difftool:
$ git pr show -t 26894

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/26894.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Aug 22, 2025

👋 Welcome back Arraying! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Aug 22, 2025

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk openjdk bot changed the title 8364103 8364103: Convert existing sprintf-chains to stringStream Aug 22, 2025
@openjdk
Copy link

openjdk bot commented Aug 22, 2025

@Arraying The following label will be automatically applied to this pull request:

  • hotspot-runtime

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the hotspot-runtime hotspot-runtime-dev@openjdk.org label Aug 22, 2025
@Arraying Arraying marked this pull request as ready for review August 22, 2025 08:53
@openjdk openjdk bot added the rfr Pull request is ready for review label Aug 22, 2025
@mlbridge
Copy link

mlbridge bot commented Aug 22, 2025

Webrevs

Copy link
Member

@iklam iklam left a comment

Choose a reason for hiding this comment

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

Why are we printing to a buffer first?

Originally this function was java_lang_Throwable::print_stack_element_to_buffer() and returns a buffer. Hence the anachronistic comment "// Print stack trace element to resource allocated buffer" which is no longer correct.

I can't find any information from the history of this file:

https://github.com/openjdk/jdk/blame/68f3dd76c92ce3b0219ac2cc346718dd865645d9/hotspot/src/share/vm/classfile/javaClasses.cpp#L1161

I am guessing the reason is to avoid interleaving with other threads. I think the above comment should be updated to reflect this.

const size_t buf_size = buf_len + 64;
char* buf = NEW_RESOURCE_ARRAY(char, buf_size);
// The string stream that will handle all of the formatting and outputting.
stringStream ss;
Copy link
Member

Choose a reason for hiding this comment

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

This comment is not necessary.

@@ -2588,75 +2588,70 @@ static void print_stack_element_to_stream(outputStream* st, Handle mirror, int m
int version, int bci, Symbol* name) {
ResourceMark rm;

// Get strings and string lengths
// Get strings.
Copy link
Member

Choose a reason for hiding this comment

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

This comment is useless and should be removed.


// Print stack trace line in buffer
size_t buf_off = os::snprintf_checked(buf, buf_size, "\tat %s.%s(", klass_name, method_name);
ss.print("\tat %s.%s(", klass_name, method_name);
Copy link
Member

Choose a reason for hiding this comment

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

The comment about this line is useless and should be removed.


// Print module information
if (module_name != nullptr) {
if (module_version != nullptr) {
buf_off += os::snprintf_checked(buf + buf_off, buf_size - buf_off, "%s@%s/", module_name, module_version);
ss.print("%s@%s/", module_name, module_version);

Choose a reason for hiding this comment

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

Here and other places, this change is going to merge-conflict with #26849.
I think that PR should be allowed to go first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot-runtime hotspot-runtime-dev@openjdk.org rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

3 participants