简体   繁体   中英

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

I know how to get the number of logical cores in C.

sysconf(_SC_NPROCESSORS_CONF);

This will return 4 on my i3 processor. But actually there are only 2 cores in an i3.

How can I get physical core count?

This is a C solution using libcpuid .

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.

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:

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

While on my laptop (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).

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....)

The ht presence in flags: line means that your CPU has hyper-threading . The number of CPU threads is given by the number of processor: lines. The actual number of physical cores is given by cpu cores: (all this using a 4.1 kernel on my machine).

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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