Linux get processor count

How to obtain the number of CPUs/cores in Linux from the command line?

Note that both of these will end up counting twice as many cores as actually exist if you’re on a system with hyperthreading (e.g, P4, or Core i7).

cat /proc/cpuinfo | awk ‘/^processor/‘ | tail -1 also will return the wrong number if the CPU numbers are 0-based.

The first line return 1 Core less then existing. Better cat /proc/cpuinfo | awk ‘/^processor/‘| wc -l

Processing the contents of /proc/cpuinfo is needlessly baroque. Use nproc which is part of coreutils, so it should be available on most Linux installs.

Command nproc prints the number of processing units available to the current process, which may be less than the number of online processors.

To find the number of all installed cores/processors use nproc —all

This doesn’t work with hyperthreading if I need the number of physical cores. Returns 8 on my quad core i7 box.

The most portable solution I have found is the getconf command:

getconf _NPROCESSORS_ONLN 

This works on both Linux and Mac OS X. Another benefit of this over some of the other approaches is that getconf has been around for a long time. Some of the older Linux machines I have to do development on don’t have the nproc or lscpu commands available, but they have getconf .

Editor’s note: While the getconf utility is POSIX-mandated, the specific _NPROCESSORS_ONLN and _NPROCESSORS_CONF values are not. That said, as stated, they work on Linux platforms as well as on macOS; on FreeBSD/PC-BSD, you must omit the leading _ .

This worked for me on Red Hat Entreprise Linux 5.4, Centos 6.5 & 7 and Mac OSX 10.9 (Mavericks). It seems this it the most portable, as lscpu is not installed by default on these systems. Thanks!

@CiroSantilli六四事件法轮功纳米比亚威视 From github.com/gstrauss/plasma/blob/master/plasma_sysconf.c it looks like I was wrong: it’s only optional. «sysconf _SC_NPROCESSORS_ONLN and _SC_NPROCESSORS_CONF are not required by standards, but are provided on numerous unix platforms and documented as optional by Open Group.»

@HaseebJadoon, you missed enclosing the getconf command in quotes /bin/sh -c ‘getconf _NPROCESSORS_ONLN’ . Though you asked a long time ago, just wanted to share my suggestion if it is helpful for you or someone else.

  • The problem with the /proc/cpuinfo -based answers is that they parse information that was meant for human consumption and thus lacks a stable format designed for machine parsing: the output format can differ across platforms and runtime conditions; using lscpu -p on Linux (and sysctl on macOS) bypasses that problem.
  • getconf _NPROCESSORS_ONLN / getconf NPROCESSORS_ONLN doesn’t distinguish between logical and physical CPUs.
Читайте также:  Вывести определенную строку файла linux

Here’s a sh (POSIX-compliant) snippet that works on Linux and macOS for determining the number of — online — logical or physical CPUs; see the comments for details.

Uses lscpu for Linux, and sysctl for macOS.

Terminology note: CPU refers to the smallest processing unit as seen by the OS. Non-hyper-threading cores each correspond to 1 CPU, whereas hyper-threading cores contain more than 1 (typically: 2) — logical — CPU.
Linux uses the following taxonomy [1] , starting with the smallest unit:
CPU < core < socket < book < node
with each level comprising 1 or more instances of the next lower level.

#!/bin/sh # macOS: Use `sysctl -n hw.*cpu_max`, which returns the values of # interest directly. # CAVEAT: Using the "_max" key suffixes means that the *maximum* # available number of CPUs is reported, whereas the # current power-management mode could make *fewer* CPUs # available; dropping the "_max" suffix would report the # number of *currently* available ones; see [1] below. # # Linux: Parse output from `lscpu -p`, where each output line represents # a distinct (logical) CPU. # Note: Newer versions of `lscpu` support more flexible output # formats, but we stick with the parseable legacy format # generated by `-p` to support older distros, too. # `-p` reports *online* CPUs only - i.e., on hot-pluggable # systems, currently disabled (offline) CPUs are NOT # reported. # Number of LOGICAL CPUs (includes those reported by hyper-threading cores) # Linux: Simply count the number of (non-comment) output lines from `lscpu -p`, # which tells us the number of *logical* CPUs. logicalCpuCount=$([ $(uname) = 'Darwin' ] && sysctl -n hw.logicalcpu_max || lscpu -p | egrep -v '^#' | wc -l) # Number of PHYSICAL CPUs (cores). # Linux: The 2nd column contains the core ID, with each core ID having 1 or # - in the case of hyperthreading - more logical CPUs. # Counting the *unique* cores across lines tells us the # number of *physical* CPUs (cores). physicalCpuCount=$([ $(uname) = 'Darwin' ] && sysctl -n hw.physicalcpu_max || lscpu -p | egrep -v '^#' | sort -u -t, -k 2,4 | wc -l) # Print the values. cat  

Note that BSD-derived systems other than macOS - e.g., FreeBSD - only support the hw.ncpu key for sysctl , which are deprecated on macOS; I'm unclear on which of the new keys hw.npu corresponds to: hw.(logical|physical)cpu_[max] .

Tip of the hat to @teambob for helping to correct the physical-CPU-count lscpu command.

Caveat: lscpu -p output does NOT include a "book" column (the man page mentions "books" as an entity between socket and node in the taxonomic hierarchy). If "books" are in play on a given Linux system (does anybody know when and how?), the physical-CPU-count command may under-report (this is based on the assumption that lscpu reports IDs that are non-unique across higher-level entities; e.g.: 2 different cores from 2 different sockets could have the same ID).

If you save the code above as, say, shell script cpus , make it executable with chmod +x cpus and place it in folder in your $PATH , you'll see output such as the following:

$ cpus logical 4 physical 4 

Источник

How to Get the Number of Processors/Cores in Linux

It is a no-brainer that the various Linux operating system distributions that provide irreplaceable performance prowess in a development or production environment will lose their performance identity in the absence of processors/cores.

These processors/cores are hardware entities of a computer system responsible for computing how fast the Linux operating system and its hosted programs complete user/system assigned tasks.

The evolution/innovation of various computer hardware infrastructures has led to the production of modern CPUs whose processors/cores embrace features like hyper-threading and multiple cores.

Importance of Knowing the Number of Processors/Cores in Linux

When working under an open-source operating system distribution like Linux, performance is everything. You need both the hardware and software components of your computing system to be at their best.

Therefore, knowing the number of processors/cores on the machine that powers your Linux operating system helps gauge how best your OS can perform under specific/customizable computing conditions.

For instance, you might be able to understand why multiple cores/hyper-threading CPUs have a speed and performance advantage over single-Core CPUs without hyper-threading.

1. Find Linux CPU Processors/Cores Using /proc/cpuinfo File

Whether on a remote Linux server or desktop Linux system, this method will query the /proc/cpuinfo file for lines matching the keyword processor via grep command which passes the processor keyword to a wc (word count) function that sums it up for display.

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

A more detailed output of the above file will look like the following:

Check Linux CPU Info

2. Get Linux CPU Processors/Cores Using lscpu Command

To understand what lscpu command does, we first need to run it:

Check Linux CPU Architecture Information

The lscpu command also highlights the CPU’s Architecture, op-mode(s), address sizes, thread(s) per core, core(s) per socket, family, model name, etc.

3. Check Linux CPU Processors/Cores Using top Command

Key in the command (top) on your Linux system and hit [Enter] on your keyboard. You should get a detailed output on what is going on with your processors/cores.

Monitor Linux CPU Usage

4. List Linux CPU Processors/Cores Using nproc Command

This approach is straightforward and will only output the available CPUs on your Linux system.

$ nproc --all 4 

An alternative approach to execute this command is as follows:

$ echo "Threads/core: $(nproc --all)"

Find Linux CPU Cores

5. List Linux CPU Processors/Cores Using getconf Command

This command is straightforward and can be executed in the following manner:

$ getconf _NPROCESSORS_ONLN 4 

An alternative approach to executing this command is as follows:

$ echo "Number of CPU/cores online at $HOSTNAME: $(getconf _NPROCESSORS_ONLN)"

List Linux CPU Cores

6. Find Linux CPU Processors/Cores Using dmidecode Command

The dmidecodeis command is used to get the Linux system’s hardware related information such as Processor, RAM, BIOS detail, Memory, Serial numbers etc.

Check Linux Hardware Information

We can alternatively modify the execution of the above command to provide us with CPU details like version, core count, core enabled, and thread count.

$ sudo dmidecode -t 4 | egrep -i 'core (count|enabled)|thread count|Version'

Get Linux Hardware Information

The above discussed inbuilt Linux commands cover everything there is to know about the number of processors/cores in your Linux system and much more.

Источник

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