Count cpu cores linux

3 Ways to check CPU Cores in Linux

In this blog post, we will discuss 3 different ways to check the number of cpu cores in Linux. This is useful information to have if you are trying to optimize your system for performance.

Each method has its own advantages and disadvantages, so be sure to read through all of them before choosing the one that is best for you. Let’s get started!

Methods to get number of CPU cores in Linux

The following commands can be used to check the number of CPU cores in Linux.

  • cat /proc/cpuinfo
  • lscpu
  • sudo dmidecode -t 4
  • nproc
  • hwinfo
  • getconf _NPROCESSORS_ONLN

Understanding CPU cores in Linux

When you are trying to optimize your system for performance, it is important to understand the concept of CPU cores. In Linux, a CPU core is essentially a processor.

This means that, if you have multiple CPU cores, your system can process multiple tasks at the same time. This can result in a significant increase in performance.

But the number of CPU cores is not the same as the number of processors.

Understanding CPU cores and hyper threading in Linux

Hyper threading is a feature that was first introduced in Intel processors. It allows a single CPU core to process multiple tasks at the same time by creating virtual cores.

This can be useful if you are trying to optimize your system for performance, as it can increase the number of tasks that can be processed simultaneously.

However, it is important to note that not all processors support hyper threading.

Understanding threads per core in Linux

In Linux, threads per core is a way of measuring the number of threads that can be processed simultaneously on a single CPU core. This is different than the number of CPU cores, as it measures the number of tasks that can be processed at the same time. There are 2 threads per core in most of the current CPU architecture.

  • The number of CPU cores = sockets * Core per socket
  • The number of vCPU = CPU cores * threads per core

Check CPU cores with lscpu command in Linux

The best way to check the number of CPU cores in Linux is using the lscpu command. Open the terminal and run this command lscpu. It gives you a lot of information about cpu, including the number of cores, the vendor_id, model name etc.
echo «Cores = $(( $(lscpu | awk ‘/^Socket\(s\)/< print $2 >‘) * $(lscpu | awk ‘/^Core\(s\) per socket/< print $4 >‘) ))»

It will list the number of CPU cores on your system.

The number of CPU cores = sockets * Core per socket

sockets=$(lscpu | awk '/^Socket\(s\)/< print $2 >') Core per socket=$(lscpu | awk '/^Core\(s\) per socket/< print $4 >') 

We can use this command to get the number of CPU cores.

echo "Cores = $(( $(lscpu | awk '/^Socket\(s\)/< print $2 >') * $(lscpu | awk '/^Core\(s\) per socket/< print $4 >') ))" 

The lscpu command displays either one single CPU family or all families detected by querying sysfs (on Linux kernels with CONFIG_SYSFS). It supports Intel x86, AMD Family 16h/18h, and ARM processors.

Читайте также:  Подключение к remoteapp linux

In the following examples, the number of CPU cores is 1 * 2=2

$ lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 4 On-line CPU(s) list: 0-3 Thread(s) per core: 2 Core(s) per socket: 2 Socket(s): 1 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 85 

From this example, we can get that the number of CPU cores are 2 * 8 =16

 $ lscpu | grep -E '^Thread|^Core|^Socket|^CPU\(' CPU(s): 32 Thread(s) per core: 2 Core(s) per socket: 8 Socket(s): 2 

Check CPU cores from /proc/cpuinfo File in Linux

Another way to check the number of cpu cores in Linux is by looking at the /proc/cpuinfo file. Open the terminal and run this command: cat /proc/cpuinfo. It will list a lot of information about the cpu, including the number of CPU cores on your system.

This file also contains CPU vendor_id, model name etc. To view it, just type the following command into your terminal:

cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 85 model name : Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz stepping : 7 microcode : 0x500320a cpu MHz : 3109.590 cache size : 36608 KB physical id : 0 

check the physical number of CPU:

cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l
cat /proc/cpuinfo |grep "processor"|wc -l 

Check CPU cores with dmidecode command in Linux

dmidecode command can be used to check the number of cpu cores in Linux. It is a command-line tool to retrieve device data that the Linux kernel exposes through MCU. To run it, just type the following command into your terminal:

$ dmidecode -t processor | grep "Core Count"

This command is very useful if you want to get more information about your hardware info, such as CPU or memory.

dmidecode --type processor dmidecode --type memory 

As you can see, there are three different ways to check the number of cpu cores in Linux. Each method has its own advantages and disadvantages, so be sure to choose the one that is best for you.

If you want a simple way to check the number of cpu cores, then I recommend using the /proc/cpuinfo file. If you want more information about your system’s cpu, then I recommend using the lscpu command. Whichever method you choose, be sure to use it to optimize your system for performance. Thanks for reading

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

howtouselinux.com is dedicated to providing comprehensive information on using Linux.

We hope you find our site helpful and informative.

Источник

Number of processors/cores in command line

Nice — but not as ubiquitous as /proc/cpuinfo. nproc is there on my ubuntu VM, but not on my RedHat 5.5-based machine.

Читайте также:  Настройка kali linux usb

Make sure to nproc —all for all installed Processing Units. Without —all , nproc only shows Processing Units available to the current process. Read the man page for more details. MMV.

You should link to: gnu.org/software/coreutils/manual/html_node/… The cyberciti page does not any more information than what is already mentioned here except showing more ads.

The most simplest tool comes with glibc and is called getconf :

$ getconf _NPROCESSORS_ONLN 4 

Unfortunately, while this utility is available on Solaris, _NPROCESSORS_ONLN (or anything with cpu, proc, etc) is listed in its output. I was hoping since it’s a posix tool it’d work on Linux/Solaris so I didn’t have to use branching.

I think the method you give is the most portable on Linux. Instead of spawning unnecessary cat and wc processes, you can shorten it a bit:

$ grep --count ^processor /proc/cpuinfo 2 

If you want to do this so it works on linux and OS X, you can do:

CORES=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || sysctl -n hw.ncpu) 

On newer kernels you could also possibly use the the /sys/devices/system/cpu/ interface to get a bit more information:

$ ls /sys/devices/system/cpu/ cpu0 cpufreq kernel_max offline possible present release cpu1 cpuidle modalias online power probe uevent $ cat /sys/devices/system/cpu/kernel_max 255 $ cat /sys/devices/system/cpu/offline 2-63 $ cat /sys/devices/system/cpu/possible 0-63 $ cat /sys/devices/system/cpu/present 0-1 $ cat /sys/devices/system/cpu/online 0-1 

See the official docs for more information on what all these mean.

nproc uses this, mostly ($OMP_NUM_THREADS then readdir sysfs). I think this one is the better interface.

When someone asks for «the number of processors/cores» there are 2 answers being requested. The number of «processors» would be the physical number installed in sockets on the machine.

The number of «cores» would be physical cores. Hyperthreaded (virtual) cores would not be included (at least to my mind). As someone who writes a lot of programs with thread pools, you really need to know the count of physical cores vs cores/hyperthreads. That said, you can modify the following script to get the answers that you need.

#!/bin/bash MODEL=`cat /cpu/procinfo | grep "model name" | sort | uniq` ALL=`cat /proc/cpuinfo | grep "bogo" | wc -l` PHYSICAL=`cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l` CORES=`cat /proc/cpuinfo | grep "cpu cores" | sort | uniq | cut -d':' -f2` PHY_CORES=$(($PHYSICAL * $CORES)) echo "Type $MODEL" echo "Processors $PHYSICAL" echo "Physical cores $PHY_CORES" echo "Including hyperthreading cores $ALL" 

The result on a machine with 2 model Xeon X5650 physical processors each with 6 physical cores that also support hyperthreading:

Type model name : Intel(R) Xeon(R) CPU X5650 @ 2.67GHz Processors 2 Physical cores 12 Including hyperthreading cores 24 

On a machine with 2 mdeol Xeon E5472 processors each with 4 physical cores that doesn’t support hyperthreading

Type model name : Intel(R) Xeon(R) CPU E5472 @ 3.00GHz Processors 2 Physical cores 8 Including hyperthreading cores 8 

Источник

How to know number of cores of a system in Linux?

I wanted to find out how many cores my system has, so I searched the same question in Google. I got some commands such as the lscpu command. When I tried this command, it gave me the following result:

$ lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 4 On-line CPU(s) list: 0-3 Thread(s) per core: 1 Core(s) per socket: 4 Socket(s): 1 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 23 Stepping: 10 CPU MHz: 1998.000 BogoMIPS: 5302.48 Virtualization: VT-x L1d cache: 32K L1i cache: 32K L2 cache: 2048K NUMA node0 CPU(s): 0-3 

Which of those indicates cores of a Linux system?

Читайте также:  Linux swap ssd or hdd

Is there any other command to tell the number of cores, or am I assuming it is completely wrong?

Your image of text isn’t very helpful. It can’t be copied into an editor, and it doesn’t index very well, meaning that other users with the same problem are less likely to find the answer here. Please edit your post to incorporate the relevant text directly (preferably using copy+paste to avoid transcription errors).

@สมหวังแนวหน้า kind of a nitpick, but the the grep arg should be processor , not precessor , correct? . Thanks for the help!

12 Answers 12

To get a complete picture you need to look at the number of threads per core, cores per socket and sockets. If you multiply these numbers you will get the number of CPUs on your system.

CPUs = Threads per core X cores per socket X sockets

CPUs are what you see when you run htop (these do not equate to physical CPUs).

Here is an example from a desktop machine:

$ lscpu | grep -E '^Thread|^Core|^Socket|^CPU\(' CPU(s): 8 Thread(s) per core: 2 Core(s) per socket: 4 Socket(s): 1 
$ lscpu | grep -E '^Thread|^Core|^Socket|^CPU\(' CPU(s): 32 Thread(s) per core: 2 Core(s) per socket: 8 Socket(s): 2 

The output of nproc corresponds to the CPU count from lscpu . For the desktop machine above this should match the 8 CPU(s) reported by lscpu :

The output of /proc/cpuinfo should match this information, for example on the desktop system above we can see there are 8 processors (CPUs) and 4 cores (core id 0-3):

$ grep -E 'processor|core id' /proc/cpuinfo processor : 0 core id : 0 processor : 1 core id : 0 processor : 2 core id : 1 processor : 3 core id : 1 processor : 4 core id : 2 processor : 5 core id : 2 processor : 6 core id : 3 processor : 7 core id : 3 

The cpu cores reported by /proc/cpuinfo corresponds to the Core(s) per socket reported by lscpu . For the desktop machine above this should match the 4 Core(s) per socket reported by lscpu:

$ grep -m 1 'cpu cores' /proc/cpuinfo cpu cores : 4 

To specifically answer your question you tell how many cores you have by multiplying the number of cores you have per socket by the number of sockets you have.

Cores = Cores per socket X Sockets

For the example systems above the desktop has 4 cores:

$ echo "Cores = $(( $(lscpu | awk '/^Socket\(s\)/< print $2 >') * $(lscpu | awk '/^Core\(s\) per socket/< print $4 >') ))" Cores = 4 
$ echo "Cores = $(( $(lscpu | awk '/^Socket\(s\)/< print $2 >') * $(lscpu | awk '/^Core\(s\) per socket/< print $4 >') ))" Cores = 16 

Another useful utility is dmidecode which outputs per socket information. In the case of the server system listed above we expect to see 8 cores per socket and 16 threads per socket:

$ sudo dmidecode -t 4 | grep -E 'Socket Designation|Count' Socket Designation: CPU1 Core Count: 8 Thread Count: 16 Socket Designation: CPU2 Core Count: 8 Thread Count: 16 

The lscpu command has a number of useful options that you may like to check out, for example:

$ lscpu --all --extended $ lscpu --all --parse=CPU,SOCKET,CORE | grep -v '^#' 

See man lscpu for details.

  • You need to be aware of sockets, cores and threads
  • You need to be careful of the term CPU as it means different things in different contexts

Источник

Оцените статью
Adblock
detector