Is there any way to know the size of L1, L2, L3 cache and RAM in Linux?

If you have lshw installed:

$ sudo lshw -C memory

Example

$ sudo lshw -C memory
...
  *-cache:0
       description: L1 cache
       physical id: a
       slot: Internal L1 Cache
       size: 32KiB
       capacity: 32KiB
       capabilities: asynchronous internal write-through data
  *-cache:1
       description: L2 cache
       physical id: b
       slot: Internal L2 Cache
       size: 256KiB
       capacity: 256KiB
       capabilities: burst internal write-through unified
  *-cache:2
       description: L3 cache
       physical id: c
       slot: Internal L3 Cache
       size: 3MiB
       capacity: 8MiB
       capabilities: burst internal write-back
  *-memory
       description: System Memory
       physical id: 2a
       slot: System board or motherboard
       size: 8GiB
     *-bank:0
          description: SODIMM DDR3 Synchronous 1334 MHz (0.7 ns)
          product: M471B5273CH0-CH9
          vendor: Samsung
          physical id: 0
          serial: 67010644
          slot: DIMM 1
          size: 4GiB
          width: 64 bits
          clock: 1334MHz (0.7ns)
     *-bank:1
          description: SODIMM DDR3 Synchronous 1334 MHz (0.7 ns)
          product: 16JTF51264HZ-1G4H1
          vendor: Micron Technology
          physical id: 1
          serial: 3749C127
          slot: DIMM 2
          size: 4GiB
          width: 64 bits
          clock: 1334MHz (0.7ns)

lscpu

If you care only about the sizes, try lscpu from util-linux.

Example

$ 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:                 37
Model name:            Intel(R) Core(TM) i5 CPU       M 560  @ 2.67GHz
Stepping:              5
CPU MHz:               1199.000
BogoMIPS:              5319.88
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              3072K
NUMA node0 CPU(s):     0-3

x86info

There should be also package/command called x86info. Assuming you have i386/x86_64, x86info -c should provide more detailed information about caches.

Example

$ x86info -c
x86info v1.30.  Dave Jones 2001-2011
Feedback to <[email protected]>.

Found 4 identical CPUs
Extended Family: 0 Extended Model: 2 Family: 6 Model: 37 Stepping: 5
Type: 0 (Original OEM)
CPU Model (x86info's best guess): Core i7 (Nehalem) [Clarkdale/Arrandale]
Processor name string (BIOS programmed): Intel(R) Core(TM) i5 CPU       M 560  @ 2.67GHz

Cache info
 L1 Instruction cache: 32KB, 4-way associative. 64 byte line size.
 L1 Data cache: 32KB, 8-way associative. 64 byte line size.
 L2 (MLC): 256KB, 8-way associative. 64 byte line size.
TLB info
 Instruction TLB: 2MB or 4MB pages, fully associative, 7 entries
 Instruction TLB: 4K pages, 4-way associative, 64 entries.
 Data TLB: 4KB or 4MB pages, fully associative, 32 entries.
 Data TLB: 4KB pages, 4-way associative, 64 entries
 Data TLB: 4K pages, 4-way associative, 512 entries.
 Data TLB: 4KB or 4MB pages, fully associative, 32 entries.
 Data TLB: 4KB pages, 4-way associative, 64 entries
 64 byte prefetching.
 Data TLB: 4K pages, 4-way associative, 512 entries.
Found unknown cache descriptors: dd 
Total processor threads: 4
This system has 1 dual-core processor with hyper-threading (2 threads per core) running at an estimated 2.65GHz

getconf

getconf -a | grep CACHE

gives:

LEVEL1_ICACHE_SIZE                 32768
LEVEL1_ICACHE_ASSOC                8
LEVEL1_ICACHE_LINESIZE             64
LEVEL1_DCACHE_SIZE                 32768
LEVEL1_DCACHE_ASSOC                8
LEVEL1_DCACHE_LINESIZE             64
LEVEL2_CACHE_SIZE                  262144
LEVEL2_CACHE_ASSOC                 8
LEVEL2_CACHE_LINESIZE              64
LEVEL3_CACHE_SIZE                  20971520
LEVEL3_CACHE_ASSOC                 20
LEVEL3_CACHE_LINESIZE              64
LEVEL4_CACHE_SIZE                  0
LEVEL4_CACHE_ASSOC                 0
LEVEL4_CACHE_LINESIZE              0

Or for a single level:

getconf LEVEL2_CACHE_SIZE

The cool thing about this interface is that it is just a wrapper around the POSIX sysconf C function (cache arguments are non-POSIX extensions), and so it can be used from C code as well.

Tested in Ubuntu 16.04.

x86 CPUID instruction

The CPUID x86 instruction also offers cache information, and can be directly accessed by userland: https://en.wikipedia.org/wiki/CPUID

glibc seems to use that method for x86. I haven't confirmed by step debugging / instruction tracing, but the source for 2.28 sysdeps/x86/cacheinfo.c does that:

__cpuid (2, eax, ebx, ecx, edx);

TODO create a minimal C example, lazy now, asked at: https://stackoverflow.com/questions/14283171/how-to-receive-l1-l2-l3-cache-size-using-cpuid-instruction-in-x86

ARM also has an architecture-defined mechanism to find cache sizes through registers such as the Cache Size ID Register (CCSIDR), see the ARMv8 Programmers' Manual 11.6 "Cache discovery" for an overview.

Memory size

A few methods:

free
cat /proc/meminfo
sysinfo()

bibliography:

  • https://stackoverflow.com/questions/20348007/how-can-i-find-out-the-total-physical-memory-ram-of-my-linux-box-suitable-to-b
  • https://askubuntu.com/questions/898941/how-to-check-ram-size/898942
  • https://stackoverflow.com/questions/349889/how-do-you-determine-the-amount-of-linux-system-ram-in-c

Tags:

Hardware

Ram