2

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?

gengisdave
  • 1,969
  • 5
  • 21
  • 22
Bharat jain
  • 419
  • 1
  • 9
  • 16

4 Answers4

6

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

gengisdave
  • 1,969
  • 5
  • 21
  • 22
  • thanks for the solution .... I am just a beginner so can you tell me how do i integrate this libcpuid into c. I am using Xcode IDE on Ubuntu. – Bharat jain Jul 14 '15 at 13:12
  • It seems there are no packages for ubuntu/debian; you can search it with `apt-cache search libcpuid` if there are packages (you will need the -dev package too); otherwise you can compile it from sources (see http://libcpuid.sourceforge.net/download.html for instructions) – gengisdave Jul 14 '15 at 13:22
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
Zibri
  • 9,096
  • 3
  • 52
  • 44
1

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 (e.g. 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).

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1
#include <stdio.h>

int main(int argc, char **argv)
{
        unsigned int lcores = 0, tsibs = 0;

        char buff[32];
        char path[64];

        for (lcores = 0;;lcores++) {
                FILE *cpu;

                snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%u/topology/thread_siblings_list", lcores);

                cpu = fopen(path, "r");
                if (!cpu) break;

                while (fscanf(cpu, "%[0-9]", buff)) {
                        tsibs++;
                        if (fgetc(cpu) != ',') break;
                }

                fclose(cpu);
        }

        printf("physical cores %u\n", lcores / (tsibs / lcores));
}

thread_siblings_list has a comma delimited list of cores which are "thread siblings" with the current core.

Divide the number of logical cores by the number of siblings to get the siblings per core. Divide the number of logical cores by the siblings per core to get the number of physical cores.

Arran Cudbard-Bell
  • 5,912
  • 2
  • 26
  • 48