Reputation: 21
I am currently working on a multithreaded setup with 3 threads running in parallel, doing some extremely time critical calculations. The entire setup will run for a few milliseconds altogether before the worker threads finish their work and stop.
Overall this setup is working fine, as long as I know that I have at least 3 physical CPU cores at my disposal - it doesn't work well with only two CPU cores and hyperthreading. On such a system it is slower than just letting one thread handle both workloads the main thread spawns off.
Now my problem is that under Windows I can easily detect the number of physical cores by querying the info from some system calls and adjust the setup accordingly, but all the searches about this problem on Linux have yielded no usable results - the best recommendation is to read the text output of an external tool. The fact that these tools exist tell me that there must be a way to detect the number of physical cores. I tried CPUID but it also only returns the doubled number on a hyperthreading-capable CPU, as do the C++ thread functions.
So, is there any reliable way to get this info in a machine readable form?
Upvotes: 2
Views: 224
Reputation: 718658
Try running lscpu -J
as an external command. This will give you a bunch of hardware related parameters in a JSON file. For example:
$ lscpu -J
{
"lscpu": [
{"field": "Architecture:", "data": "x86_64"},
{"field": "CPU op-mode(s):", "data": "32-bit, 64-bit"},
{"field": "Byte Order:", "data": "Little Endian"},
{"field": "CPU(s):", "data": "4"},
{"field": "On-line CPU(s) list:", "data": "0-3"},
{"field": "Thread(s) per core:", "data": "2"},
{"field": "Core(s) per socket:", "data": "2"},
{"field": "Socket(s):", "data": "1"},
{"field": "NUMA node(s):", "data": "1"},
{"field": "Vendor ID:", "data": "GenuineIntel"},
{"field": "CPU family:", "data": "6"},
{"field": "Model:", "data": "58"},
{"field": "Model name:", "data": "Intel(R) Core(TM) i5-3340M CPU @ 2.70GHz"},
{"field": "Stepping:", "data": "9"},
{"field": "CPU MHz:", "data": "1351.427"},
{"field": "CPU max MHz:", "data": "3400.0000"},
{"field": "CPU min MHz:", "data": "1200.0000"},
{"field": "BogoMIPS:", "data": "5382.52"},
{"field": "Virtualization:", "data": "VT-x"},
{"field": "L1d cache:", "data": "32K"},
{"field": "L1i cache:", "data": "32K"},
{"field": "L2 cache:", "data": "256K"},
{"field": "L3 cache:", "data": "3072K"},
{"field": "NUMA node0 CPU(s):", "data": "0-3"},
{"field": "Flags:", "data": "fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm cpuid_fault epb pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms xsaveopt dtherm ida arat pln pts md_clear flush_l1d"}
]
}
$
For more information, refer to the lscpu
manual entry.
Upvotes: 1