Programmatically get the cache line size?

Programmatically get the cache line size?

梦中楼上月下 发布于 2021-11-28 字数 212 浏览 710 回复 9 原文

All platforms welcome, please specify the platform for your answer.

A similar question: How to programmatically get the CPU cache page size in C++?

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。



需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。


撞了怀 2022-06-07 9 楼

You can also try to do it programmatically by measuring some timing. Obviously, it won't always be as precise as cpuid and the likes, but it is more portable. ATLAS does it at its configuration stage, you may want to look at it:

烂人 2022-06-07 8 楼

You can use std::hardware_destructive_interference_size since C++17.
Its defined as:

Minimum offset between two objects to avoid false sharing. Guaranteed
to be at least alignof(std::max_align_t)

调妓 2022-06-07 7 楼

ARMv6 and above has C0 or the Cache Type Register. However, its only available in privileged mode.

For example, from Cortex™-A8 Technical Reference Manual:

The purpose of the Cache Type Register is to determine the instruction
and data cache minimum line length in bytes to enable a range of
addresses to be invalidated.

The Cache Type Register is:

  • a read-only register
  • accessible in privileged modes only.

The contents of the Cache Type Register depend on the specific
implementation. Figure 3-2 shows the bit arrangement of the Cache
Type Register...

Don't assume the ARM processor has a cache (apparently, some can be configured without one). The standard way to determine it is via C0. From the ARM ARM, page B6-6:

From ARMv6, the System Control Coprocessor Cache Type register is the
mandated method to define the L1 caches, see Cache Type register on
page B6-14. It is also the recommended method for earlier variants of
the architecture. In addition, Considerations for additional levels of
cache on page B6-12 describes architecture guidelines for level 2
cache support.

请止步禁区 2022-06-07 6 楼

On the Windows platform:


The GetLogicalProcessorInformation
function will give you characteristics
of the logical processors in use by
the system. You can walk the
returned by the function looking for
entries of type RelationCache. Each
such entry contains a ProcessorMask
which tells you which processor(s) the
entry applies to, and in the
CACHE_DESCRIPTOR, it tells you what
type of cache is being described and
how big the cache line is for that

著墨染雨君画夕 2022-06-07 5 楼

If you're using SDL2 you can use this function:

int SDL_GetCPUCacheLineSize(void);

Which returns the size of the L1 cache line size, in bytes.

In my x86_64 machine, running this code snippet:

printf("CacheLineSize = %d",SDL_GetCPUCacheLineSize());

Produces CacheLineSize = 64

I know I'm a little late, but just adding information for future visitors.
The SDL documentation currently says the number returned is in KB, but it is actually in bytes.

夜声。 2022-06-07 4 楼

On x86, you can use the CPUID instruction with function 2 to determine various properties of the cache and the TLB. Parsing the output of function 2 is somewhat complicated, so I'll refer you to section 3.1.3 of the Intel Processor Identification and the CPUID Instruction (PDF).

To get this data from C/C++ code, you'll need to use inline assembly, compiler intrinsics, or call an external assembly function to perform the CPUID instruction.

过期情话 2022-06-07 3 楼

I have been working on some cache line stuff and needed to write a cross-platform function. I committed it to a github repo at, or you can just use the source below. Feel free to do whatever you want with it.


// Author: Nick Strupat
// Date: October 29, 2010
// Returns the cache line size (in bytes) of the processor, or 0 on failure

#include <stddef.h>
size_t cache_line_size();

#if defined(__APPLE__)

#include <sys/sysctl.h>
size_t cache_line_size() {
    size_t line_size = 0;
    size_t sizeof_line_size = sizeof(line_size);
    sysctlbyname("hw.cachelinesize", &line_size, &sizeof_line_size, 0, 0);
    return line_size;

#elif defined(_WIN32)

#include <stdlib.h>
#include <windows.h>
size_t cache_line_size() {
    size_t line_size = 0;
    DWORD buffer_size = 0;
    DWORD i = 0;

    GetLogicalProcessorInformation(0, &buffer_size);
    buffer = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION *)malloc(buffer_size);
    GetLogicalProcessorInformation(&buffer[0], &buffer_size);

    for (i = 0; i != buffer_size / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); ++i) {
        if (buffer[i].Relationship == RelationCache && buffer[i].Cache.Level == 1) {
            line_size = buffer[i].Cache.LineSize;

    return line_size;

#elif defined(linux)

#include <stdio.h>
size_t cache_line_size() {
    FILE * p = 0;
    p = fopen("/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size", "r");
    unsigned int i = 0;
    if (p) {
        fscanf(p, "%d", &i);
    return i;

#error Unrecognized platform

涫野音 2022-06-07 2 楼

On Linux look at sysconf(3).


You can also get it from the command line using getconf:

探春 2022-06-07 1 楼

On Linux (with a reasonably recent kernel), you can get this information out of /sys:


This directory has a subdirectory for each level of cache. Each of those directories contains the following files:


This gives you more information about the cache then you'd ever hope to know, including the cacheline size (coherency_line_size) as well as what CPUs share this cache. This is very useful if you are doing multithreaded programming with shared data (you'll get better results if the threads sharing data are also sharing a cache).