Skip to content

8364128: Improve gathering of cpu feature names using FormatBuffer #26515

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

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. It also improves and extends FormatBuffer with an additional method that appends comma separated strings 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 FormatBuffer (Enhancement - P4)

Contributors

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

@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

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

@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.

@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.

@ashu-mehra
Copy link
Contributor Author

/contributor add @jdksjolen

@openjdk
Copy link

openjdk bot commented Jul 30, 2025

@ashu-mehra
Contributor Johan Sjölen <[email protected]> successfully added.

Signed-off-by: Ashutosh Mehra <[email protected]>
@ashu-mehra
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot [email protected] rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

2 participants