Skip to content

Commit

Permalink
Use std::thread::hardware_concurrency when libcpuid failed (pingcap…
Browse files Browse the repository at this point in the history
  • Loading branch information
JinheLin authored Jun 1, 2021
1 parent 43b1d72 commit dce81ce
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions dbms/src/Common/getNumberOfPhysicalCPUCores.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,35 @@

unsigned getNumberOfPhysicalCPUCores()
{
unsigned res = 0;
#if defined(__x86_64__)

cpu_raw_data_t raw_data;
if (0 != cpuid_get_raw_data(&raw_data))
throw DB::Exception("Cannot cpuid_get_raw_data: " + std::string(cpuid_error()), DB::ErrorCodes::CPUID_ERROR);

cpu_id_t data;
if (0 != cpu_identify(&raw_data, &data))
throw DB::Exception("Cannot cpu_identify: " + std::string(cpuid_error()), DB::ErrorCodes::CPUID_ERROR);

/// On Xen VMs, libcpuid returns wrong info (zero number of cores). Fallback to alternative method.
if (data.num_logical_cpus == 0)
return std::thread::hardware_concurrency();

unsigned res = data.num_cores * data.total_logical_cpus / data.num_logical_cpus;
if (0 == cpuid_get_raw_data(&raw_data) && 0 == cpu_identify(&raw_data, &data) && data.num_logical_cpus != 0)
{
res = data.num_cores * data.total_logical_cpus / data.num_logical_cpus;
}

/// Also, libcpuid gives strange result on Google Compute Engine VMs.
/// Example:
/// num_cores = 12, /// number of physical cores on current CPU socket
/// total_logical_cpus = 1, /// total number of logical cores on all sockets
/// num_logical_cpus = 24. /// number of logical cores on current CPU socket
/// It means two-way hyper-threading (24 / 12), but contradictory, 'total_logical_cpus' == 1.

if (res != 0)
{
return res;
}
// else fallback
#endif

/// As a fallback (also for non-x86 architectures) assume there are no hyper-threading on the system.
/// (Actually, only Aarch64 is supported).
return std::thread::hardware_concurrency();
res = std::thread::hardware_concurrency();
if (res == 0)
{
throw DB::Exception("Cannot Identify CPU: Unsupported processor", DB::ErrorCodes::CPUID_ERROR);
}
return res;
}

0 comments on commit dce81ce

Please sign in to comment.