Linux number of processors

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?

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
Читайте также:  Узнать системное время linux

Источник

Get the Number of Processors/Cores in Linux

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

1. Overview

In this tutorial, we’ll look at ways to check the number of processors or cores available in a Linux machine. We’ll take a closer look at some of the Bash utilities and system files that can be used for this purpose.

2. The lscpu Utility

First, let’s check the default output of the lscpu command:

$ lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian Address sizes: 39 bits physical, 48 bits virtual CPU(s): 8 On-line CPU(s) list: 0-7 Thread(s) per core: 2 Core(s) per socket: 4 Socket(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 142 Model name: Intel(R) Core(TM) i5-10310U CPU @ 1.70GHz . 

Here, the CPU(s) value indicates the number of logical cores, which is equal to 8 in our output.

The number of logical cores is equal to “Thread(s) per core” × “Core(s) per socket” × “Socket(s)” and the number of physical cores on a machine equals “Core(s) per socket” × “Socket(s)”.

Based on the above output, we can infer that our machine has 4 physical cores, and since hyperthreading is enabled (“Thread(s) per core” equals 2), we get 8 logical cores.

Читайте также:  Проброс принтера через rdp linux

Let’s also check the command to directly get the number of physical cores on a machine:

$ lscpu -b -p=Core,Socket | grep -v '^#' | sort -u | wc -l 4

Here, we’re using the -p option to parse only the “Core” and “Socket” columns. Alongside that, we’re also using the -b option to limit the output to online CPUs only.

3. The /proc/cpuinfo File

The lscpu utility derives its information from the /proc/cpuinfo file, which contains processor-wise information for all the logical cores or processors:

$ cat /proc/cpuinfo | egrep "processor|core id" 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

Therefore, let’s get the number of logical cores on a machine based on the contents of the /proc/cpuinfo file:

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

Additionally, we can also count the number of unique core ids to get the number of physical cores on a machine:

$ grep ^"core id" /proc/cpuinfo | sort -u | wc -l 4

4. Other Alternatives

There are a few other commands we can use to get the processor information. Let’s take a look.

4.1. The nproc Command

Let’s check the number of processors available on the system using the nproc utility:

Here, we used here the –all option to print the number of installed processors.

4.2. Querying Using getconf

We can query the configured processor values using the getconf command:

$ getconf -a | grep PROCESSORS _NPROCESSORS_CONF 8 _NPROCESSORS_ONLN 8 

In this case, the -a option lists all the configuration variables of the system and their values. Alternatively, we can also query for just the processor-related parameters. So, let’s check the number of configured processors:

$ getconf _NPROCESSORS_CONF 8

And, the number of processors online:

$ getconf _NPROCESSORS_ONLN 8

5. Conclusion

In this article, we looked at different ways to get the number of cores on a Linux machine.

Читайте также:  Linux replace with regex

First, we introduced the lscpu utility of Bash. Additionally, we learned to interpret its output to calculate the number of physical and logical cores. We also checked the commands to directly get the physical and logical processor count on a machine.

Then, we looked at ways to derive the same information from the /proc/cpuinfo file. Finally, we also examined ways to use nproc and getconf commands to get the CPU core information.

Источник

How to find the number of CPU cores including virtual?

How do I find out the number of cores my CPU has, including virtual cores (hyper threading cores) using the command line?

6 Answers 6

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

To check the number of cores !

cat /proc/cpuinfo | grep 'core id' core id : 0 core id : 1 

Or lscpu will show you all output:

lscpu Architecture: i686 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 2 On-line CPU(s) list: 0,1 Thread(s) per core: 1 Core(s) per socket: 2 Socket(s): 1 Vendor ID: GenuineIntel CPU family: 15 Model: 4 Stepping: 7 CPU MHz: 2792.992 BogoMIPS: 5585.98 L1d cache: 16K L2 cache: 1024K 

nproc is also useful in scripts depending on the number of cores available to it. E.g. make -j$(nproc) .

To add to the existing answers, you can determine information about Intel’s HyperThreading by looking at the «siblings» line in /proc/cpuinfo. The example below is from a 2 socket machine. It shows the CPU has 6 cores but 12 «siblings». On Intel CPUs this means HyperThreading is enabled and there are 6 physical cores.

processor : 23 vendor_id : GenuineIntel cpu family : 6 model : 62 model name : Intel(R) Xeon(R) CPU E5-2430 v2 @ 2.50GHz stepping : 4 microcode : 0x428 cpu MHz : 1599.707 cache size : 15360 KB physical id : 1 siblings : 12 core id : 5 cpu cores : 6 apicid : 43 initial apicid : 43 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat xsaveopt pln pts dtherm tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms bogomips : 5005.20 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management: 

dmidecode is also useful for determining what hardware a Linux system is running on.

Источник

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