Linux cpu thread count

How to check how many CPUs are there in Linux system

I am a new Linux user. How do you check how many CPUs are there in Linux system using the command line option?

Introduction: One can obtain the number of CPUs or cores in Linux from the command line. The /proc/cpuinfo file stores CPU and system architecture dependent items, for each supported architecture. You can view /proc/cpuinfo with the help of cat command or grep command/egrep command. This page shows how to use /proc/cpuinfo file and lscpu command to display number of processors on Linux.

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements None
Est. reading time 3 minutes

How do you check how many CPUs are there in Linux system?

You can use one of the following command to find the number of physical CPU cores including all cores on Linux:

  1. lscpu command
  2. cat /proc/cpuinfo
  3. top or htop command
  4. nproc command
  5. hwinfo command
  6. dmidecode -t processor command
  7. getconf _NPROCESSORS_ONLN command

Let us see all commands and examples in details.

How to display information about the CPU on Linux

Just run the lscpu command:
$ lscpu
$ lscpu | egrep ‘Model name|Socket|Thread|NUMA|CPU\(s\)’
$ lscpu -p

The output clearly indicate that I have:

  1. CPU model/make: AMD Ryzen 7 1700 Eight-Core Processor
  2. Socket: Single (1)
  3. CPU Core: 8
  4. Thread per core: 2
  5. Total threads: 16 ( CPU core[8] * Thread per core [2])

Use /proc/cpuinfo to find out how many CPUs are there in Linux

Run top or htop command to obtain the number of CPUs/cores in Linux

Execute nproc print the number of CPUs available on Linux

Let us print the number of installed processors on your system i.e core count:
$ nproc —all
$ echo «Threads/core: $(nproc —all)»
Sample outputs:

How to probe for CPU/core on Linux using hwinfo command

$ hwinfo —cpu —short ## short info ##
$ hwinfo —cpu ## detailed info on CPUs ##

Linux display CPU core with getconf _NPROCESSORS_ONLN command

One can query Linux system configuration variables with getconf command:
$ getconf _NPROCESSORS_ONLN
$ echo «Number of CPU/cores online at $HOSTNAME: $(getconf _NPROCESSORS_ONLN)»
Sample outputs:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

dmidecode -t processor command

You can use get BIOS and hardware information with dmidecode command (DMI table decoder) on Linux. To find out how many CPUs are there in Linux system, run:
$ sudo dmidecode -t 4
$ sudo dmidecode -t 4 | egrep -i ‘core (count|enabled)|thread count|Version’

Here is a quick video demo of lscpu and other commands:

Conclusion

You learned how to display information about the CPU architecture, core, threads, CPU version/model, vendor and other information using various Linux command line options.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How to Check Processor and CPU Details on Linux – Command Examples

Processor/Cpu details

The details about the processor that we shall be talking about include, number of cores, availability of hyper threading, architecture, cache size etc.

To find these details about the cpu on your system can be a bit difficult because the way different commands check them.

The commands that we are going to use include lscpu, /proc/cpuinfo and lstopo (hwloc).
These commands show detailed information about the cpu cores/processing units.

The examples following next would explain how to interpret the output of these commands.

1. Vendor and model of the processor

To find the vendor and model name of the processor, search the /proc/cpuinfo file with the grep command.

Its an Intel processor. Next find the model name that can be used to lookup the exact specifications online on Intel’s website.

Its a «Core 2 Quad Q8400» processor.

2. Architecture

The lscpu commands reports the architecture.

The architecture is x86_64 which is 64 bit.

3. Frequency

The frequency/speed of the processor is reported by both lscpu and /proc/cpuinfo.

The change of frequency can be seen by monitoring the output of /proc/cpuinfo using watch.

Run the above command in a terminal and while it is running, launch some cpu intensive task in parallel and the frequency would increase.

4. Number of cores

Each core on the processor is an actual independant cpu or processing unit. Multiple cores enable the processor to execute multiple program instructions in parallel, thereby increasing the processing speed.

The lscpu command indicates the «cores per socket».

So in this case the number of cores on the processor is 4.
The /proc/cpuinfo file also indicates the number of cores, but it can be bit tricky and confusing.

Simply counting the number of processors may give wrong numbers.

In case of hyper threaded processors, the number of processors that the operating system sees is twice the number of cores.

However /proc/cpuinfo has a field named ‘core id’ which is a unique id for each core in a single processor. Counting the core id would give a clear indication of the number of actual cores on the processor

Rare, but in case you are on a system that has multiple physical processors (yes, it means 2 or more processors fitted on the motherboard), then the results of /proc/cpuinfo would be different. In case of multiple processors, the ‘physical id’ would indicate multiple values.

If there are more than 1 physical ids, then there are multiple physical processors on the system. And you have to count the cores on each processor separately.

5. Hyper threading

Hyper threading is an Intel technology that allows individual cores to perform like 2 logical processing units. This, in a way increases the processing power of each core in a limited manner.

To check whether the processor has hyper-threading, 2 different values have to be compared. First is the number of actual cores, and second is the number of logical processing units.

If the number of cores is equal to the number of processing units as seen by the OS, then NO hyper threading. Otherwise if the number of processing units is greater/twice the number of cores, then YES hyper threading.

Take this example of a Core 2 Quad Q8400 processor

Number of processors as shown by /proc/cpuinfo is 4

Number of ‘cpu cores’ = 4 as well as ‘siblings’ = 4 and unique ‘core id’ = 4

Therefore total number of processing units = number of actual cores. So there is no hyper threading on this processor, and the same can be confirmed from the specs of the processor on Intel’s website.

Incase of hyper threading being present the output of /proc/cpuinfo or lscpu would be different.

Note the «Thread(s) per core: 2» which indicate that there are 2 threads per core, with a total of 4 cores. So the number of processing units seen by the OS is 8.

Now lets take a look at the output of /proc/cpuinfo.

The ‘cpu cores’ = 4 and siblings = 8 which means there are 4 cores and 2 hyperthreads per core. Number of processors as shown by /proc/cpuinfo would also be 8.

For the Core2Quad Q8400 processor, both dmidecode and /proc/cpuinfo show the hyperthreading flag enabled, inspite of hyper threading not being available on the processor.

Hwloc / lstopo

Hwloc (Portable hardware locality) is a small utility that reports the structure of the processor in a neat visual diagram. The diagram shows the number of cores, hyperthreads and cache size. A single diagram tells it all.

The above diagram clearly shows —

Total L2 Cache — 4096 KB — 4MB
Total Cores — 4
Processing unit per core — 1

Hyper-threaded processor
For a hyperthreaded processor, the hwloc output diagram could look like this

The diagram indicates

Total L3 Cache — 8MB
Total Cores — 4
Processing units per Core — 2 [hyper threading]

Conclusion

To learn more about commands for checking CPU information on Linux check this post:
9 Commands to Check CPU Information on Linux

If you have any feedback or questions let us know in the comments below.

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected] .

10 thoughts on “ How to Check Processor and CPU Details on Linux – Command Examples ”

If you are looking for physical CPU count /proc/cpuinfo is confusing . It gives the number of threads .

To get physical CPU count .

]# dmidecode -t 4 | egrep -i “Designation|Intel|core|thread”
Socket Designation: CPU1
Manufacturer: Intel
HTT (Multi-threading)
Version: Intel(R) Xeon(R) CPU L5640 @ 2.27GHz
Core Count: 6
Core Enabled: 6
Thread Count: 12
Socket Designation: CPU2
Manufacturer: Intel
HTT (Multi-threading)
Version: Intel(R) Xeon(R) CPU L5640 @ 2.27GHz
Core Count: 6
Core Enabled: 6
Thread Count: 12

The same server if i give

]# cat /proc/cpuinfo | egrep processor | wc -l
24

So its means i have a 24 threads system , so dont rely on cpuinfo .

Useless use of cat.

cat /proc/cpuinfo | grep processor

grep processor /proc/cpuinfo

In the last example you have hwloc as the command to run. You meant to have $ lstopo.

Источник

4 commands to check thread count per process (threads vs processes) in Linux

Table of Contents

In Linux, some processes are divided into pieces called threads. In one liner, threads are essentially just processes with a shared address space on Linux. In this article we will get some brief overview on threads and processes, also some examples to show threads per process, check thread count per process, check number of threads allowed, count threads and some more related topics.

Threads vs Processes

  • A thread is very similar to a process, it has an identifier (TID, or thread ID), and the kernel schedules and runs threads just like processes.
  • However, unlike separate processes, which usually do not share system resources such as memory and I/O connections with other processes, all threads inside a single process share their system resources and some memory.
  • A process with one thread is single-threaded , and a process with more than one thread is multithreaded .
  • All processes start out single-threaded . This starting thread is usually called the main thread. The main thread may then start new threads in order for the process to become multithreaded, similar to the way a process can call fork() to start a new process.
  • The primary advantage of a multithreaded process is that when the process has a lot to do, threads can run simultaneously on multiple processors, potentially speeding up computation.
  • Although you can also achieve simultaneous computation with multiple processes, threads start faster than processes, and it is often easier and/or more efficient for threads to intercommunicate using their shared memory than it is for processes to communicate over a channel such as a network connection or a pipe.

Show threads per process

1. Using PID task

You can count threads with the list of available sub directories inside /proc/

/task/ . The count of total available sub-directories inside this part is directly proportional to the thread count per process for the provided PID.

For example to check java thread count, I have a Java process for which you can see I have multiple sub-directories so it means this is a multi threaded process. using ls command under this path you can show threads per process for java

But then again I have another process for which as you can see I have single sub-directory hence we know this is a single thread process

2. Using ps command

You can also use » ps » command to show threads per process. With » ps » we can list LWP (Light Weight process) which depicts Thread ID of the respective process and NWLP (Number of Threads).

To show threads per process using ps command you can use below argument

3. Using pstree command

You can also use pstree to show threads per process. Here as you see java thread count and check number of threads for java process

4. Using top command

In top , by default we will not be able to see thread count per process. But when running top , it is possible to change which fields to display and add this column to print thread count per process can be added manually.

  • Press f
  • This will show a list of fields that top can display. The fields that are displayed in bold are the ones that top will display.
  • Use the down arrow to navigate to «nTH» (Number of Threads).
  • Press to select «nTH«
  • Press ‘s‘ to sort on number of threads.
  • Press ‘q‘ to display the data of threads count.

Next you should see a new column at the end of top command with number of thread (nTH) column to show threads per process

Check thread count per process

Next you can use the above explained commands to also check thread count per process by customising them a little bit.

1. Using PID status

To check thread count per process you can use below command. For example here java thread count is 59 threads in my Linux environment

While amsHelper process has single thread

2. Using ps command

We used ps command to show threads per process and count threads, we can also use » ps » command to get LWP and NLWP details, which when combined with » wc » we can count threads per process.

To check thread count per process for a particular PID for example to check java thread count:

Check number of threads allowed in Linux system?

Linux doesn’t have a separate threads per process limit, j ust a limit on the total number of processes on the system . This value controls the maximum number of threads that can be created using fork() . During initialization the kernel sets this value such that even if the maximum number of threads is created

To check number of threads which Linux system can allow

The minimum number of threads that can be written to threads-max is 20 .
The maximum value that can be written to threads-max is given by the constant FUTEX_TID_MASK (0x3fffffff) .
If a value outside of this range is written to threads-max an error EINVAL occurs.

The default value depends on memory size. You can use threads-max to check number of threads allowed in Linux. You can increase thread count per process limit like this:

There is also a limit on the number of processes (an hence threads) that a single user may create, see ulimit for details regarding these limits:

Here, the system is able to create 35,000 threads/processes in total and a single user can create 10000 number of processes.

The logic is very simple here every CPU can execute 1 process at a time, if there are 8 cores that means 8 to 10 processes at a time can be executed easily without any stress but if number of running or runnable threads per CPU increases drastically then there will be performance issue.

What is the maximum processes count allowed in Linux?

Verify the value for kernel.pid_max

Here I can execute 35,000 processes simultaneously in my system that can run in separate memory spaces.

To change the value of kernel.pid_max to 65534:

Lastly I hope the steps from the article to show threads per process, check thread count per process, check number of threads allowed on Linux was helpful. So, let me know your suggestions and feedback using the comment section.

Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Источник

Читайте также:  Microsoft windows powershell ise
Оцените статью