Check Number of CPU Cores and Threads on Linux

Check Number of CPU Cores and Threads on Linux

Understanding the hardware configuration of the Linux system is fundamental for optimizing its performance and ensuring efficient resource allocation. One crucial aspect of this understanding is determining the number of CPU cores and threads available, as they directly influence the system's ability to handle tasks concurrently. This tutorial shows how to check number of CPU cores and threads on Linux.

Execute the following command to obtain the number of CPU cores:

awk -F': ' '/cpu cores/{print $2;exit}' /proc/cpuinfo

To fetch the count of threads (logical processors), utilize the provided command:

grep -c ^processor /proc/cpuinfo

Output example of both commands:

24
32

Here's a breakdown of the first command:

  • awk - is a command line tool for text processing.
  • -F': ' - specifies the field separator as a colon followed by a space.
  • /cpu cores/ - it instructs awk to search for lines containing cpu cores.
  • {print $2;exit} - prints the second field (after the colon and space), representing the number of CPU cores. The exit statement ensures that awk exits after processing the first occurrence of cpu cores.

Here's a breakdown of the second command:

  • grep - is a command line tool for pattern matching.
  • -c - this option instructs grep to count the number of lines that match the pattern.
  • ^processor - this is the pattern to match. In this context, ^ is a special character in regex that anchors the pattern to the start of a line, and processor is the literal string we're matching.

Leave a Comment

Cancel reply

Your email address will not be published.