1. Introduction
The lscpu command in Linux is a powerful tool for gathering detailed information about the CPU architecture of a system. Whether we are system administrators, developers, or simply curious users, understanding the capabilities of our hardware is essential for optimizing performance and troubleshooting issues.
In this tutorial, we’ll explore the lscpu command and its various options and provide numerous examples to help understand its usage.
2. Getting Started with lscpu
First of all, a processor can contain multiple cores, and each core can support multiple hardware threads. These threads are virtual CPUs that the operating system sees and can schedule tasks on. So, the lscpu command provides information, including the number of processors, cores per processor, threads per core, CPU family, model, and more.
To invoke the lscpu command, we open a terminal window and type lscpu. This command will display a comprehensive overview of our system’s CPU architecture:
$ 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: 61
Model name: Intel(R) Core(TM) i5-5200U CPU @ 2.20GHz
Stepping: 4
CPU MHz: 971.253
CPU max MHz: 2700,0000
CPU min MHz: 500,0000
BogoMIPS: 4389.76
Virtualization: VT-x
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 3072K
NUMA node0 CPU(s): 0-3
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 cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap intel_pt xsaveopt dtherm ida arat pln pts md_clear flush_l1d
Here, we can see the architecture information, which is x86_64 in this case. Also, we get information about the total number of CPUs and the number of threads supported per core in our system.
Moreover, we observe the number of cores per CPU socket, the description of the CPU model, and the current CPU frequency in MHz, along with the sizes of the CPU L1/L2/L3 caches of our hardware.
3. Exploring Different Options
The lscpu command supports various options to customize its output according to specific requirements. Let’s explore some of these options with examples.
3.1. lscpu -p
We can use the -p or –parse option to display selected fields from the CPU information. This command shows each CPU’s CPU, core, and socket numbers:
$ lscpu -p=cpu,core,socket
# The following is the parsable format, which can be fed to other
# programs. Each different item in every column has an unique ID
# starting from zero.
# CPU,Core,Socket
0,0,0
1,1,0
2,0,0
3,1,0
3.2. lscpu -e
The -e or –extended option provides a human-readable format for CPU information.
Let’s see an output:
$ lscpu -e
CPU NODE SOCKET CORE L1d:L1i:L2:L3 ONLINE MAXMHZ MINMHZ
0 0 0 0 0:0:0:0 yes 2700,0000 500,0000
1 0 0 1 1:1:1:0 yes 2700,0000 500,0000
2 0 0 0 0:0:0:0 yes 2700,0000 500,0000
3 0 0 1 1:1:1:0 yes 2700,0000 500,0000
This output provides additional details such as CPU node, socket, and cache sizes in a readable format.
4. NUMA Architecture
In addition to providing insights into CPU architecture, lscpu can be invaluable for optimizing system performance and resource allocation. For instance, understanding the NUMA (Non-Uniform Memory Access) architecture can help in configuring applications and workload distribution for better efficiency.
Viewing NUMA Information: The –extended displays information about NUMA nodes.
$ lscpu --extended
By leveraging lscpu to gather NUMA-related information, system administrators and developers can fine-tune their systems to maximize performance in NUMA-aware environments. This underscores the versatility of lscpu beyond its primary function, making it an indispensable tool in the Linux ecosystem.
NUMA architecture is prevalent in modern multi-socket systems, where each processor socket has its own memory bank. This design allows for faster memory access within a socket but introduces latency when accessing memory across sockets. Understanding NUMA is crucial for optimizing system performance, especially in environments where applications are sensitive to memory access latency.
For example, in a NUMA-aware environment, it’s beneficial to allocate tasks to CPUs within the same NUMA node to minimize memory access latency. By utilizing lscpu to identify the CPU-to-NUMA node mapping, administrators can ensure that applications are configured to make efficient use of system resources.
5. Binding a Process to a NUMA Node
Suppose we have a memory-intensive application that we want to run on CPUs within the same NUMA node. For that we can use the information from lscpu -e to bind the process to the appropriate CPUs using tools like numactl.
This command binds the process to NUMA node 0, ensuring it uses the CPUs and memory within that node, thus optimizing performance:
$ numactl --cpunodebind=0 --membind=0 <application_command>
This can be very useful for applications that benefit from specific CPU allocations, such as databases or virtual machines, to determine whether CPU and NUMA node assignments can enhance performance.
For instance, if an application is spread across multiple NUMA nodes, it might suffer from increased latency due to cross-node memory access. By examining the output of lscpu -e, we can check if an application’s threads are distributed across multiple NUMA nodes. If so, we might consider reconfiguring the application or system settings to localize the threads within a single NUMA node.
For example, we can test the binding with the sleep command:
$ numactl --cpunodebind=0 --membind=0 sleep 60
In this example, numactl is used to bind the sleep 60 process to NUMA node 0. While sleep 60 runs, we can check the CPU and memory binding to ensure the command works as expected. This is particularly useful for verifying NUMA configurations and understanding how processes are distributed across CPUs and memory nodes.
5.1. Parsing lscpu Output
Here is a simple bash script that parses lscpu -e output to list all CPUs in each NUMA node:
#!/bin/bash
# Get the extended CPU information
lscpu -e | awk 'NR>1 {print "CPU "$1" is in NUMA node "$2}'
This script uses awk to process the output of lscpu -e, skipping the header line and printing the CPU and NUMA node information:
CPU 0 is in NUMA node 0
CPU 1 is in NUMA node 0
CPU 2 is in NUMA node 0
CPU 3 is in NUMA node 0
6. Conclusion
In this article, we discussed the lscpu command as a valuable tool for obtaining detailed information about the CPU architecture of a Linux system.
By mastering the lscpu command and its various options, we can gain valuable insights into our system’s CPU configuration and make informed decisions. We should experiment with different options, explore the provided examples, and deepen our understanding of CPU architecture in Linux.