-
Notifications
You must be signed in to change notification settings - Fork 6.1k
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
base: master
Are you sure you want to change the base?
8364128: Improve gathering of cpu feature names using FormatBuffer #26515
Conversation
Signed-off-by: Ashutosh Mehra <[email protected]>
👋 Welcome back asmehra! A progress list of the required criteria for merging this PR into |
❗ This change is not yet ready to be integrated. |
@ashu-mehra The following label will be automatically applied to this pull request:
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. |
Webrevs
|
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.
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; |
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.
Style: template <size_t bufsz>
.
|
||
template<size_t bufsz> class FormatBuffer; | ||
|
||
using CpuInfoBuffer = FormatBuffer<CPU_INFO_BUF_SIZE>; |
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.
Is it important that we avoid dynamic allocation at this stage of VM initialization? Is that why we can't use stringStream
instead?
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.
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); |
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.
Sorry, I'm going to need some help here :-). How can passing a FormatBuffer
to os::strdup
not fail to compile?
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.
And here we do os::strdup
, so now we do have dynamic allocation, so maybe just use a stringStream
?
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.
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; }
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); |
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.
Can't you just check the overflow at the end?
// 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; | ||
} |
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.
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");
}
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.
+1
I can add this to the stringStream class.
Signed-off-by: Ashutosh Mehra <[email protected]>
Signed-off-by: Ashutosh Mehra <[email protected]>
Signed-off-by: Ashutosh Mehra <[email protected]>
Signed-off-by: Ashutosh Mehra <[email protected]>
/contributor add @jdksjolen |
@ashu-mehra |
Signed-off-by: Ashutosh Mehra <[email protected]>
@jdksjolen I updated the code as per your suggestion. Please review. |
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
Issue
Contributors
<[email protected]>
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