Skip to content
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

Incorrect detection of logical CPU number #3725

Open
Lastique opened this issue Jan 29, 2024 · 3 comments
Open

Incorrect detection of logical CPU number #3725

Lastique opened this issue Jan 29, 2024 · 3 comments

Comments

@Lastique
Copy link

On a Core i7 12700K libopenh264 detects 64 logical cores as it prints "NUMBER OF LOGIC PROCESSORS ON CHIP: 64" during encoder context initialization. On this CPU, with E-cores disabled, there are 8 physical cores with HT (i.e. a total of 16 logical cores).

The problem is in this portion of code:

if (uiCPU & WELS_CPU_HTT) {
*pNumberOfLogicProcessors = (uiFeatureB & 0x00ff0000) >> 16; // feature bits: 23-16 on returned EBX
} else {
*pNumberOfLogicProcessors = 0;
}
if (!strcmp ((const char*)chVendorName, CPU_Vendor_INTEL)) {
if (uiMaxCpuidLevel >= 4) {
uiFeatureC = 0;
WelsCPUId (0x4, &uiFeatureA, &uiFeatureB, &uiFeatureC, &uiFeatureD);
if (uiFeatureA != 0) {
*pNumberOfLogicProcessors = ((uiFeatureA & 0xfc000000) >> 26) + 1;
}
}
}

First, the line 159 uses a stale value of uiFeatureB that was initialized at line 141 by calling WelsCPUId(7, ...). Leaf 7 does not contain information about logical core count, and the value obtained here is basically garbage. It looks like the intention was to use uiFeatureB initialized at line 75 by WelsCPUId(1, ...).

Next, the line 168 uses bits 26-31 of EAX of CPUID leaf 4 to deduce the number of logical cores. This is incorrect as this field indicates the maximum number of core IDs per package, not the actual number of logical processors. And indeed, on my CPU this field reads the value of 63.

To obtain the number of logical/physical processors you should parse CPUID leaf 0x1F or, if not supported, 0x0B. See Intel Software Developer's Manual, CPUID description for details.

@yump
Copy link

yump commented Mar 29, 2024

IMO CPUID is not a good source of information on the number of threads a parallel program should use. CPUs may be offlined, or your process may be affined to a subset of all CPUs on the system. Better to ask the OS (sched_getaffinity on Linux).

@Lastique
Copy link
Author

I don't disagree, but still the current code is incorrect.

@Talaqalotaibipmp
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants