Skip to content

Conversation

@ashu-mehra
Copy link
Contributor

@ashu-mehra ashu-mehra commented Jul 28, 2025

This PR implements the code for cpu features names string using FormatBuffer stringStream. It also improves and extends FormatBuffer with an additional method that appends comma separated strings to the buffer. It also adds a method to stringStream that that appends separated strings separated by a delimiter to the buffer. This is useful in creating the cpu features names string.
This code will also be useful in Leyden to implement cpu feature check for AOTCodeCache [0].
Platforms affected: x86-64 and aarch64
Other platforms can be done if and when Leyden changes are ported to them.

[0] openjdk/leyden#84


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-8364128: Improve gathering of cpu feature names using stringStream (Enhancement - P4)

Reviewers

Contributors

  • Johan Sjölen <jsjolen@openjdk.org>

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 26515

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

Using diff file

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

Using Webrev

Link to Webrev Comment

Signed-off-by: Ashutosh Mehra <asmehra@redhat.com>
@bridgekeeper
Copy link

bridgekeeper bot commented Jul 28, 2025

👋 Welcome back asmehra! 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 Jul 28, 2025

@ashu-mehra This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8364128: Improve gathering of cpu feature names using stringStream

Co-authored-by: Johan Sjölen <jsjolen@openjdk.org>
Reviewed-by: kvn, jsjolen

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 467 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the rfr Pull request is ready for review label Jul 28, 2025
@openjdk
Copy link

openjdk bot commented Jul 28, 2025

@ashu-mehra The following label will be automatically applied to this pull request:

  • hotspot

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 hotspot-dev@openjdk.org label Jul 28, 2025
@mlbridge
Copy link

mlbridge bot commented Jul 28, 2025

Webrevs

Copy link
Contributor

@jdksjolen jdksjolen left a comment

Choose a reason for hiding this comment

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

It seems like we can just use a stringStream instead and not have to deal with any overflow of some buffer, since we strdup at the end.

#define CPU_INFO_BUF_SIZE 1024
#endif // CPU_INFO_BUF_SIZE

template<size_t bufsz> class FormatBuffer;
Copy link
Contributor

Choose a reason for hiding this comment

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

Style: template <size_t bufsz>.


template<size_t bufsz> class FormatBuffer;

using CpuInfoBuffer = FormatBuffer<CPU_INFO_BUF_SIZE>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it important that we avoid dynamic allocation at this stage of VM initialization? Is that why we can't use stringStream instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I think stringStream can be used as well. Dynamic allocation shouldn't be a concern. I can set the initial size to be same as the CPU_INFO_BUF_SIZE to reduce churn a bit. I will try this out. Thanks for the suggestion.

_features_string = extract_features_string(_cpu_info_string,
strnlen(_cpu_info_string, sizeof(buf)),
cpu_info_size);
_cpu_info_string = os::strdup(info_buffer);
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I'm going to need some help here :-). How can passing a FormatBuffer to os::strdup not fail to compile?

Copy link
Contributor

Choose a reason for hiding this comment

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

And here we do os::strdup, so now we do have dynamic allocation, so maybe just use a stringStream?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I'm going to need some help here :-). How can passing a FormatBuffer to os::strdup not fail to compile?

It works because of this trick in its base class FormatBufferBase:

operator const char *() const { return _buf; }

Comment on lines 1107 to 1111
assert(!info_buffer.overflow(), "not enough buffer size");
info_buffer.append(", ");
assert(!info_buffer.overflow(), "not enough buffer size");
int features_offset = info_buffer.length();
insert_features_names(_features, info_buffer);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't you just check the overflow at the end?

Comment on lines 73 to 90
// Appends comma separated strings obtained by mapping given range of numbers to strings
template<typename FN>
void insert_string_list(int start, int limit, FN fn) {
bool first = true;
for (int i = start; i < limit; i++) {
const char* str = fn(i);
if (str == nullptr) {
continue;
}
const char* comma = first ? "" : ", ";
int result = append("%s%s", comma, str);
if (result < 0) {
return;
}
first = false;
}
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This method seems overly specific to the use-case.

Something like this is more widely applicable.

// Append strings returned by gen, separating each with separator.
// Stops when gen returns null or when buffer is out of space.
template <typename Generator>
void join(Generator gen, const char* separator) {
    bool first = true;
    const char* str = gen();
    while (str != nullptr) {
      const char* sep = first ? "" : separator;
      int result = append("%s%s", sep, str);
      if (result < 0) {
        return;
      }
      first = false;
    }
  return;
}

Example usage:

void VM_Version::insert_features_names(uint64_t features, CpuInfoBuffer& info_buffer) {
  int i = 0;
  info_buffer.join([&]() {
    while (!supports_feature((VM_Version::Feature_Flag)i) && i < MAX_CPU_FEATURES) {
      i++;
    }
    if (i >= MAX_CPU_FEATURES) {
      return nullptr;
    }
    return _features_names[i];
  }, ", ");
  assert(!info_buffer.overflow(), "not enough buffer size");
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

+1
I can add this to the stringStream class.

Signed-off-by: Ashutosh Mehra <asmehra@redhat.com>
Signed-off-by: Ashutosh Mehra <asmehra@redhat.com>
Signed-off-by: Ashutosh Mehra <asmehra@redhat.com>
Signed-off-by: Ashutosh Mehra <asmehra@redhat.com>
@ashu-mehra
Copy link
Contributor Author

/contributor add @jdksjolen

@openjdk
Copy link

openjdk bot commented Jul 30, 2025

@ashu-mehra
Contributor Johan Sjölen <jsjolen@openjdk.org> successfully added.

Signed-off-by: Ashutosh Mehra <asmehra@redhat.com>
@ashu-mehra
Copy link
Contributor Author

@jdksjolen I updated the code as per your suggestion. Please review.

@ashu-mehra ashu-mehra changed the title 8364128: Improve gathering of cpu feature names using FormatBuffer 8364128: Improve gathering of cpu feature names using stringStream Aug 5, 2025
@ashu-mehra
Copy link
Contributor Author

@jdksjolen ping

Copy link
Contributor

@jdksjolen jdksjolen left a comment

Choose a reason for hiding this comment

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

This looks good to me! Added a few comments on the join method.

first = false;
str = gen();
}
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's delete this return

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

void inc_cr() { inc(); cr(); }

// Append strings returned by gen, separating each with separator.
// Stops when gen returns null or when buffer is out of space.
Copy link
Contributor

Choose a reason for hiding this comment

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

We never really stop when the buffer runs out of space.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the comment.

// Append strings returned by gen, separating each with separator.
// Stops when gen returns null or when buffer is out of space.
template <typename Generator>
void join(Generator gen, const char* separator) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We can move this to the outputStream class instead!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is in the outputStream class

Copy link
Contributor

Choose a reason for hiding this comment

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

Oops, I thought you moved it to the stringStream class, my bad :).

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Aug 5, 2025
Signed-off-by: Ashutosh Mehra <asmehra@redhat.com>
@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Aug 5, 2025
@ashu-mehra
Copy link
Contributor Author

@jdksjolen new commit pushed to address your comments.

@ashu-mehra
Copy link
Contributor Author

Can I get one more review of this please.

@vnkozlov
Copy link
Contributor

vnkozlov commented Aug 6, 2025

I submitted our testing.

Copy link
Contributor

@vnkozlov vnkozlov left a comment

Choose a reason for hiding this comment

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

My testing passed.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Aug 7, 2025
@ashu-mehra
Copy link
Contributor Author

/integrate

@openjdk
Copy link

openjdk bot commented Aug 7, 2025

Going to push as commit bc3d865.
Since your change was applied there have been 470 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Aug 7, 2025
@openjdk openjdk bot closed this Aug 7, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Aug 7, 2025
@openjdk
Copy link

openjdk bot commented Aug 7, 2025

@ashu-mehra Pushed as commit bc3d865.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.


enum Feature_Flag {
#define DECLARE_CPU_FEATURE_FLAG(id, name, bit) CPU_##id = (1 << bit),
#define DECLARE_CPU_FEATURE_FLAG(id, name, bit) CPU_##id = bit,
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be greatly appreciated if you could give the Graal devs a heads-up when changing CPU feature flags. JVMCI sees different CPU features with this change, and eventually leads to SIGILL by generating instruction not supported by the underlying CPU.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mur47x111 sorry for breaking JVMCI with this. I was not aware of such a dependency at all and I touched this code for the first time. For future work, how do I check if my change in hotspot has the potential to break JVMCI? And what is the best forum to inform about such changes?

Copy link
Contributor

Choose a reason for hiding this comment

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

I will add a JVMCI test to assert this. You can inform us via /label add graal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hotspot hotspot-dev@openjdk.org integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.

4 participants