1. Overview
A Central Processing Unit (CPU) can have multiple levels of cache memory to store frequently accessed data and instructions. The cache memory divides into three levels:
- L1 cache – fastest, but smallest, data and instructions
- L2 cache – slower, but bigger, data-only
- L3 cache – slowest, but biggest, data-only
In this tutorial, we’ll discuss various Linux commands for checking the size of the L1, L2, and L3 cache.
2. Using lscpu
The lscpu command is a useful command-line utility for obtaining in-depth insights into the CPU architecture and its features along with cache size. lscpu provides the detailed sizes of the L1 cache, L2 cache, and L3 cache which are integral to understanding the processor’s performance and caching capabilities:
$ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 4
...
Caches (sum of all):
L1d: 64 KiB (2 instances)
L1i: 64 KiB (2 instances)
L2: 512 KiB (2 instances)
L3: 3 MiB (1 instance)
...
The output provides information about the size of the L1, L2, and L3 cache, CPU architecture, and total CPUs.
2.1. L1 Cache Size
Firstly, let’s check the size of the Level 1 data cache (L1d) specifically using lscpu. We’ll use grep to filter the line of output that contains information about the L1d cache:
$ lscpu | grep "L1d cache"
L1d cache: 64 KiB (2 instances)
The output states the L1d cache size is 64 KiB (kilobytes), and the presence of 2 instances indicates two separate L1d caches, one per core. Each core has its own dedicated L1d cache, which reduces contention and allows each core to operate independently.
By changing the option to L1i (Level 1 instruction), we can get the size and instances of the L1i cache. It’s specifically designed to hold frequently accessed instructions, enabling fast execution without accessing main memory. In summary, the L1d cache stores data, while the L1i cache stores instructions for the cores to execute.
2.2. L2 Cache Size
Similarly, we can check the size of the L2 cache:
$ lscpu | grep "L2 cache"
L2 cache: 512 KiB (2 instances)
The CPU has two independent instances of the L2 cache. Each instance represents a separate copy of the cache, with a size of 512 KiB, usually associated with different cores or threads.
2.3. L3 Cache Size
Likewise, let’s check the L3 cache size information:
$ lscpu | grep "L3 cache"
L3 cache: 3 MiB (1 instance)
The output displays that the CPU has one instance of L3 cache memory with a size of 3 MiB.
3. Using lshw
Another useful command to check the size of caches in a Linux system is the lshw command. This command provides detailed information about the system’s hardware configuration, CPU cache size including the CPU version, and speed:
$ sudo lshw
description: Laptop
product: Inspiron 15-3567 (078B)
vendor: Dell Inc.
serial: 8VY0ZN2
width: 64 bits
...
*-cache:0
description: L1 cache
physical id: 41
slot: L1 Cache
size: 128KiB
capacity: 128KiB
capabilities: synchronous internal write-back unified
configuration: level=1
*-cache:1
description: L2 cache
physical id: 42
slot: L2 Cache
size: 512KiB
capacity: 512KiB
capabilities: synchronous internal write-back unified
configuration: level=2
*-cache:2
description: L3 cache
physical id: 43
slot: L3 Cache
size: 3MiB
capacity: 3MiB
capabilities: synchronous internal write-back unified
configuration: level=3
...
The output shows the cache information of a Dell Inspiron 15-3567 Laptop. The L1 cache has a size of 128KiB, the L2 cache has a size of 512KiB, and the L3 cache has a size of 3MiB. Also, the output specifies the configuration of the caches, physical IDs, slots, and capacities.
3.1. L1 Cache Size
Let’s use the lshw command with the -C memory option to check the size of the L1 cache. Further, we’ll add grep -A 4 ‘L1 cache’ to filter the output and display lines related to the L1 cache, along with the next four (4) lines:
$ sudo lshw -C memory | grep -A 4 'L1 cache'
description: L1 cache
physical id: 41
slot: L1 Cache
size: 128KiB
capacity: 128KiB
The output provides the L1 cache information of the system’s memory. In this case, L1 cache has a physical id of 41 and a slot of L1 Cache. Further, the cache size is 128KiB, and the capacity is 128KiB.
3.2. L2 Cache Size
In the same way, we can **check the size of the L2 cache using lshw:
**
$ sudo lshw -C memory | grep -A 4 'L2 cache'
description: L2 cache
physical id: 42
slot: L2 Cache
size: 512KiB
capacity: 512KiB
The command output shows the L2 cache information of the system’s memory. The L2 cache has a size and capacity of 512KiB.
3.3. L3 Cache Size
Likewise, let’s execute the lshw command to identify L3 cache size information:
$ sudo lshw -C memory | grep -A 4 'L3 cache'
description: L3 cache
physical id: 43
slot: L3 Cache
size: 3MiB
capacity: 3MiB
The output displays the L3 cache size information and its capacity of 3MiB along with the physical id, i.e., 43.
4. Using dmidecode
The dmidecode command is a powerful command-line tool. In particular, it can retrieve a system’s hardware-related information, such as cache size and capacity, processor, RAM, BIOS details, memory size, and others in a readable format.
Let’s check the detailed size information of the L1, L2, and L3 cache with dmidecode:
$ sudo dmidecode
# dmidecode 3.4
Getting SMBIOS data from sysfs.
...
Cache Information
Socket Designation: L1 Cache
Configuration: Enabled, Not Socketed, Level 1
Operational Mode: Write Back
Location: Internal
Installed Size: 128 kB
Maximum Size: 128 kB
...
Handle 0x0042, DMI type 7, 19 bytes
Cache Information
Socket Designation: L2 Cache
Configuration: Enabled, Not Socketed, Level 2
Operational Mode: Write Back
Location: Internal
Installed Size: 512 kB
Maximum Size: 512 kB
...
Handle 0x0043, DMI type 7, 19 bytes
Cache Information
Socket Designation: L3 Cache
Configuration: Enabled, Not Socketed, Level 3
Operational Mode: Write Back
Location: Internal
Installed Size: 3 MB
Maximum Size: 3 MB
...
The output shows details such as the size of the L1, L2, and L3 cache, operational mode, location, and others.
4.1. L1 Cache Size
Similarly, let’s check the L1 cache size information with the -t cache option that specifies the type of information to retrieve. Again, we’ll pipe to grep to filter the output and display lines related to the “Level 1” cache along with the following four lines:
$ sudo dmidecode -t cache | grep -A 4 "Level 1"
Configuration: Enabled, Not Socketed, Level 1
Operational Mode: Write Back
Location: Internal
Installed Size: 128 kB
Maximum Size: 128 kB
The specific command gives us information on the L1 cache size. We see that the maximum capacity of the cache is 128 KB.
4.2. L2 Cache Size
Subsequently, we can check the L2 cache size information with dmidecode:
$ sudo dmidecode -t cache | grep -A 4 "Level 2"
Configuration: Enabled, Not Socketed, Level 2
Operational Mode: Write Back
Location: Internal
Installed Size: 512 kB
Maximum Size: 512 kB
In this case, the installed size of the Level 2 cache is 512 kB, which is the maximum size of the cache.
4.3. L3 Cache Size
Likewise, let’s use the dmidecode command to check L3 cache size information:
$ sudo dmidecode -t cache | grep -A 4 "Level 3"
Configuration: Enabled, Not Socketed, Level 3
Operational Mode: Write Back
Location: Internal
Installed Size: 3 MB
Maximum Size: 3 MB
The installed size of the Level 3 cache is 3 MB, which is also the maximum size of the cache. Moreover, we also see that the L3 cache operates in Write Back operational mode.
5. Conclusion
In this article, we’ve learned several commands to check the size of the L1, L2, and L3 cache in a Linux system. These are lscpu, lshw, and dmidecode.
In short, the appropriate commands and options can show the cache size, IDs, slots, and capacities. Further, by using this information, we can optimize the system’s performance.