cisco/openh264

Incorrect detection of logical CPU number

Lastique opened this issue · 2 comments

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.