简体   繁体   English

如何使用C编程获取Linux上的内核数?

[英]How to get number of cores on linux using c programming?

I know how to get the number of logical cores in C. 我知道如何获取C中逻辑核心的数量。

sysconf(_SC_NPROCESSORS_CONF);

This will return 4 on my i3 processor. 这将在我的i3处理器上返回4。 But actually there are only 2 cores in an i3. 但是实际上i3中只有2个内核。

How can I get physical core count? 如何获得物理核数?

This is a C solution using libcpuid . 这是使用libcpuid的C解决方案。

cores.c: cores.c:

#include <stdio.h>
#include <libcpuid.h>

int main(void)
{
    struct cpu_raw_data_t raw;
    struct cpu_id_t data;

    cpuid_get_raw_data(&raw);
    cpu_identify(&raw, &data);
    printf("No. of Physical Core(s) : %d\n", data.num_cores);
    return 0;
}

This is a C++ solution using Boost. 这是使用Boost的C ++解决方案。

cores.cpp: cores.cpp:

// use boost to get number of cores on the processor
// compile with : g++ -o cores cores.cpp -lboost_system -lboost_thread

#include <iostream>
#include <boost/thread.hpp>

int main ()
{
    std::cout << "No. of Physical Core(s) : " << boost::thread::physical_concurrency() << std::endl;
    std::cout << "No. of Logical Core(s) : " << boost::thread::hardware_concurrency() << std::endl;
    return 0;
}

On my desktop (i5 2310) it returns: 在我的桌面(i5 2310)上,它返回:

No. of Physical Core(s) : 4
No. of Logical Core(s) : 4

While on my laptop (i5 480M): 在笔记本电脑(i5 480M)上:

No. of Physical Core(s) : 2
No. of Logical Core(s) : 4

Meaning that my laptop processor have Hyper-Threading tecnology 这意味着我的笔记本电脑处理器具有超线程技术

You might simply read and parse /proc/cpuinfo pseudo-file (see proc(5) for details; open that pseudo-file as a text file and read it sequentially line by line; try cat /proc/cpuinfo in a terminal). 您可能只需要读取和解析/proc/cpuinfo伪文件 (有关详细信息,请参见proc(5) ;将该伪文件作为文本文件打开并逐行依次读取;在终端中尝试cat /proc/cpuinfo )。

The advantage is that you just are parsing a (Linux-specific) text [pseudo-]file (without needing any external libraries, like in Gengisdave's answer ), the disadvantage is that you need to parse it (not a big deal, read 80 bytes lines with fgets in a loop then use sscanf and test the scanned item count....) 好处是您只在解析一个(特定于Linux的)文本[pseudo-]文件(不需要任何外部库,例如Gengisdave的答案 ),缺点是您需要对其进行解析(没什么大不了,读80循环中带有fgets字节行,然后使用sscanf并测试扫描的项目计数。...)

The ht presence in flags: line means that your CPU has hyper-threading . flags:行中的ht存在意味着您的CPU具有超线程 The number of CPU threads is given by the number of processor: lines. CPU线程数由processor:行数指定。 The actual number of physical cores is given by cpu cores: (all this using a 4.1 kernel on my machine). 物理核心的实际数量由cpu cores:给出cpu cores:所有这些都使用我的机器上的4.1内核)。

I am not sure you are right in wanting to understand how many physical cores you have. 我不确定您是否想了解您拥有多少个物理核心是正确的。 Hyper-threading may actually be useful. 超线程实际上可能是有用的。 You need to benchmark. 您需要进行基准测试。

And you probably should make the number of working threads (eg the size of your thread pool) in your application be user -configurable . 您可能应该使应用程序中工作线程数 (例如,线程池的大小) 用户配置的 Even on a 4 core hyper-threaded processor, I might want to have no more than 3 running threads (because I want to use the other threads for something else). 即使在4核超线程处理器上,我可能也希望不超过3个正在运行的线程(因为我想将其他线程用于其他用途)。

Without any lib: 没有任何库:

int main()
{
unsigned int eax=11,ebx=0,ecx=1,edx=0;

asm volatile("cpuid"
        : "=a" (eax),
          "=b" (ebx),
          "=c" (ecx),
          "=d" (edx)
        : "0" (eax), "2" (ecx)
        : );

printf("Cores: %d\nThreads: %d\nActual thread: %d\n",eax,ebx,edx);
}

Output: 输出:

Cores: 4
Threads: 8
Actual thread: 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM