Linux check whether hyperthreading is enabled or not

There parameters need to be obained: no. of physical CPU; no. of cores on each CPU; no. of all threads.

In my case, the CPU name: Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHz

(1) Check the number of physical CPU

# cat /proc/cpuinfo | grep "physical id" | sort | uniq

physical id : 0

physical id : 1

//// Here we can see the number of physical CPU is 2. Each physical CPU has its own fan.

(2) Check the number of logical cores on each CPU

# cat /proc/cpuinfo | grep "cores" | uniq

cpu cores : 10

//// Here each physical CPU has 10 cores.

(3) Check the number of all threads

# cat /proc/cpuinfo | grep "processor" | wc -l

40

//// After obtaining above three parameters, now we can judge whether the hyperthreading is enabled or not.

In the case withou hyperthreading, number of all threads= no. of physical CPU * no. of cores on each CPU * 1, then there is only one thread on each core.

When hyperthreading is enabled, for example, N threads are available on each core. Then the following relationship can be achived:

number of all threads= no. of physical CPU * no. of cores on each CPU * N.

NOTE: OpenMP

The OpenMP parallel language can also be used to check the number of threads per core.

#include "stdio.h"
#include "omp.h"

int main()
{
    int num_thread, myid;
    
    #pragma omp parallel
    {
        num_threads= omp_get_num_threads();    // get the total number of threads on one core
        myid= omp_get_thread_num();            // get the ID of the current thread
        printf("
Hello world from the thread %d out of %d threads!");
    }    
    return 0;
}

Use the following command to compile the C file "hello.c":

$ gcc -fopenmp -o run.o hello.c

$ ./run.o

原文地址:https://www.cnblogs.com/alliance/p/8193591.html