-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Fix cpuinfo init on Linux without CPU sysfs lists #28230
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
tianleiwu
wants to merge
1
commit into
microsoft:main
Choose a base branch
from
tianleiwu:tlwu/fix-cpuinfo-sysfs-fallback
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+554
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| diff --git a/src/linux/processors.c b/src/linux/processors.c | ||
| index 47bee76..d0c5569 100644 | ||
| --- a/src/linux/processors.c | ||
| +++ b/src/linux/processors.c | ||
| @@ -2,0 +3 @@ | ||
| +#include <unistd.h> | ||
| @@ -291,0 +293,22 @@ | ||
| +static uint32_t cpuinfo_linux_get_max_processor_from_sysconf( | ||
| + uint32_t max_processors_count, | ||
| + const char* processor_list_name) { | ||
| + const long nproc = sysconf(_SC_NPROCESSORS_ONLN); | ||
| + if (nproc <= 0) { | ||
| + cpuinfo_log_warning( | ||
| + "failed to query online processors from sysconf(_SC_NPROCESSORS_ONLN) for %s", | ||
| + processor_list_name); | ||
| + return UINT32_MAX; | ||
| + } | ||
| + | ||
| + uint32_t max_processor = (uint32_t)(nproc - 1); | ||
| + if ((uint64_t)nproc > (uint64_t)max_processors_count) { | ||
| + cpuinfo_log_warning( | ||
| + "online processors count %ld exceeds system limit %" PRIu32 ": truncating to the latter", | ||
| + nproc, | ||
| + max_processors_count); | ||
| + max_processor = max_processors_count - 1; | ||
| + } | ||
| + return max_processor; | ||
| +} | ||
| + | ||
| @@ -301 +324 @@ | ||
| - return UINT32_MAX; | ||
| + return cpuinfo_linux_get_max_processor_from_sysconf(max_processors_count, POSSIBLE_CPULIST_FILENAME); | ||
| @@ -323 +346 @@ | ||
| - return UINT32_MAX; | ||
| + return cpuinfo_linux_get_max_processor_from_sysconf(max_processors_count, PRESENT_CPULIST_FILENAME); | ||
| @@ -357,0 +381,31 @@ | ||
| +static bool cpuinfo_linux_detect_processors_from_sysconf( | ||
| + uint32_t max_processors_count, | ||
| + uint32_t* processor0_flags, | ||
| + uint32_t processor_struct_size, | ||
| + uint32_t detected_flag, | ||
| + const char* processor_list_name) { | ||
| + const long nproc = sysconf(_SC_NPROCESSORS_ONLN); | ||
| + if (nproc <= 0) { | ||
| + cpuinfo_log_warning( | ||
| + "failed to query online processors from sysconf(_SC_NPROCESSORS_ONLN) for %s", | ||
| + processor_list_name); | ||
| + return false; | ||
| + } | ||
| + | ||
| + uint32_t processors_count = (uint32_t)nproc; | ||
| + if ((uint64_t)nproc > (uint64_t)max_processors_count) { | ||
| + cpuinfo_log_warning( | ||
| + "online processors count %ld exceeds system limit %" PRIu32 ": truncating to the latter", | ||
| + nproc, | ||
| + max_processors_count); | ||
| + processors_count = max_processors_count; | ||
| + } | ||
| + | ||
| + for (uint32_t processor = 0; processor < processors_count; processor++) { | ||
| + *((uint32_t*)((uintptr_t)processor0_flags + processor_struct_size * processor)) |= detected_flag; | ||
| + } | ||
| + cpuinfo_log_warning( | ||
| + "falling back to sysconf(_SC_NPROCESSORS_ONLN) = %ld for %s", nproc, processor_list_name); | ||
| + return true; | ||
| +} | ||
| + | ||
| @@ -373 +427,6 @@ | ||
| - return false; | ||
| + return cpuinfo_linux_detect_processors_from_sysconf( | ||
| + max_processors_count, | ||
| + processor0_flags, | ||
| + processor_struct_size, | ||
| + possible_flag, | ||
| + POSSIBLE_CPULIST_FILENAME); | ||
| @@ -392 +451,6 @@ | ||
| - return false; | ||
| + return cpuinfo_linux_detect_processors_from_sysconf( | ||
| + max_processors_count, | ||
| + processor0_flags, | ||
| + processor_struct_size, | ||
| + present_flag, | ||
| + PRESENT_CPULIST_FILENAME); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
fix_missing_sysfs_fallback.patchmodifies Linux-only sources (src/linux/processors.c), but it’s being applied unconditionally in the “Windows ARM64/ARM64EC” cpuinfo patch chain. That increases the chance of Windows builds breaking in the future if the Linux patch stops applying cleanly (even though the fix is Linux-specific). Consider applying this patch only under the Linux branch (or gating it byCMAKE_SYSTEM_NAME STREQUAL "Linux").