Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aligning to cache line and knowing the cache line size

To prevent false sharing, I want to align each element of an array to a cache line. So first I need to know the size of a cache line, so I assign each element that amount of bytes. Secondly I want the start of the array to be aligned to a cache line.

I am using Linux and 8-core x86 platform. First how do I find the cache line size. Secondly, how do I align to a cache line in C. I am using the gcc compiler.

So the structure would be following for example, assuming a cache line size of 64.

element[0] occupies bytes 0-63 element[1] occupies bytes 64-127 element[2] occupies bytes 128-191 

and so on, assuming of-course that 0-63 is aligned to a cache line.

like image 538
MetallicPriest Avatar asked Sep 02 '11 09:09

MetallicPriest


People also ask

How are cache lines aligned?

cache lines are 32 bytes in size and are aligned to 32 byte offsets. memory locations which are offset by multiples of 4K bytes compete for 4 L1 cache lines. memory locations which are offset by multiples of 64K bytes compete for 4 L2 cache lines. L1 has separate cache lines for instructions and data - Harvard.

What is line size and cache size?

The chunks of memory handled by the cache are called cache lines. The size of these chunks is called the cache line size. Common cache line sizes are 32, 64 and 128 bytes. A cache can only hold a limited number of lines, determined by the cache size.

What is the optimum line size in a cache?

Cache line size is 64 bytes.


Video Answer


2 Answers

I am using Linux and 8-core x86 platform. First how do I find the cache line size.

$ getconf LEVEL1_DCACHE_LINESIZE 64 

Pass the value as a macro definition to the compiler.

$ gcc -DLEVEL1_DCACHE_LINESIZE=`getconf LEVEL1_DCACHE_LINESIZE` ... 

At run-time sysconf(_SC_LEVEL1_DCACHE_LINESIZE) can be used to get L1 cache size.

like image 164
Maxim Egorushkin Avatar answered Sep 19 '22 13:09

Maxim Egorushkin


To know the sizes, you need to look it up using the documentation for the processor, afaik there is no programatic way to do it. On the plus side however, most cache lines are of a standard size, based on intels standards. On x86 cache lines are 64 bytes, however, to prevent false sharing, you need to follow the guidelines of the processor you are targeting (intel has some special notes on its netburst based processors), generally you need to align to 64 bytes for this (intel states that you should also avoid crossing 16 byte boundries).

To do this in C or C++ requires that you use the standard aligned_alloc function or one of the compiler specific specifiers such as __attribute__((align(64))) or __declspec(align(64)). To pad between members in a struct to split them onto different cache lines, you need on insert a member big enough to align it to the next 64 byte boundery

like image 30
Necrolis Avatar answered Sep 20 '22 13:09

Necrolis