False sharing explained

In computer science, false sharing is a performance-degrading usage pattern that can arise in systems with distributed, coherent caches at the size of the smallest resource block managed by the caching mechanism. When a system participant attempts to periodically access data that is not being altered by another party, but that data shares a cache block with data that is being altered, the caching protocol may force the first participant to reload the whole cache block despite a lack of logical necessity.[1] The caching system is unaware of activity within this block and forces the first participant to bear the caching system overhead required by true shared access of a resource.

Multiprocessor CPU caches

By far the most common usage of this term is in modern multiprocessor CPU caches, where memory is cached in lines of some small power of two word size (e.g., 64 aligned, contiguous bytes). If two processors operate on independent data in the same memory address region storable in a single line, the cache coherency mechanisms in the system may force the whole line across the bus or interconnect with every data write, forcing memory stalls in addition to wasting system bandwidth. In some cases, the elimination of false sharing can result in order-of-magnitude performance improvements.[2] False sharing is an inherent artifact of automatically synchronized cache protocols and can also exist in environments such as distributed file systems or databases, but current prevalence is limited to RAM caches.

Example

  1. include
  2. include
  3. include
  4. include
  5. include
  6. include
  7. include

using namespace std;using namespace chrono;

  1. if defined(__cpp_lib_hardware_interference_size)

// default cacheline size from runtimeconstexpr size_t CL_SIZE = hardware_constructive_interference_size;

  1. else

// most common cacheline size otherwiseconstexpr size_t CL_SIZE = 64;

  1. endif

int mainThis code shows the effect of false sharing. It creates an increasing number of threads from one thread to the number of physical threads in the system. Each thread sequentially increments one byte of a cache line, which as a whole is shared among all threads. The higher the level of contention between threads, the longer each increment takes. This are the results on a Zen4 system with 16 cores and 32 threads:

1: 1
2: 4
3: 6
4: 9
5: 11
6: 13
7: 15
8: 17
9: 16
10: 18
11: 21
12: 25
13: 29
14: 35
15: 39
16: 41
17: 43
18: 44
19: 48
20: 49
21: 51
22: 53
23: 58
24: 61
25: 68
26: 75
27: 79
28: 82
29: 85
30: 88
31: 91
32: 94
As you can see, on the system in question it can take up to a 100 nanoseconds to complete an increment operation on the shared cache line, which corresponds to approx. 420 clock cycles on this CPU.

Mitigation

There are ways of mitigating the effects of false sharing. For instance, false sharing in CPU caches can be prevented by reordering variables or adding padding (unused bytes) between variables. However, some of these program changes may increase the size of the objects, leading to higher memory use.[2] Compile-time data transformations can also mitigate false-sharing.[3] However, some of these transformations may not always be allowed. For instance, the C++ programming language standard draft of C++23 mandates that data members must be laid out so that later members have higher addresses.[4]

There are tools for detecting false sharing.[5] [6] There are also systems that both detect and repair false sharing in executing programs. However, these systems incur some execution overhead.[7] [8]

External links

Notes and References

  1. Book: Patterson, David . Computer organization and design: the hardware/software interface . Morgan Kaufmann . Waltham, MA . 2012 . 978-0-12-374750-1 . 746618653 . 537.
  2. Bolosky . William J. . Scott . Michael L. . False sharing and its effect on shared memory performance . Sedms'93: USENIX Systems on USENIX Experiences with Distributed and Multiprocessor Systems . 1993-09-22 . 4 . 11 July 2021.
  3. Jeremiassen . Tor E. . Eggers . Susan J. . Reducing false sharing on shared memory multiprocessors through compile time data transformations . ACM SIGPLAN Notices . Association for Computing Machinery (ACM) . 30 . 8 . 1995 . 0362-1340 . 10.1145/209937.209955 . 179–188. free .
  4. Web site: Working Draft, Standard for Programming Language C++ [class] . eel.is . 2021-07-11.
  5. Web site: perf-c2c(1) . Linux manual page . 2016-09-01 . 2021-08-08.
  6. Chabbi . Milind . Wen . Shasha . Liu . Xu . Proceedings of the 23rd ACM SIGPLAN Symposium on Principles and Practice of Parallel Programming . Featherlight on-the-fly false-sharing detection . ACM . New York, NY, USA . 2018-02-10 . 152–167 . 10.1145/3178487.3178499 . 9781450349826 .
  7. Nanavati . Mihir . Spear . Mark . Taylor . Nathan . Rajagopalan . Shriram . Meyer . Dutch T. . Aiello . William . Warfield . Andrew . Proceedings of the 8th ACM European Conference on Computer Systems . Whose cache line is it anyway? . ACM Press . New York, New York, USA . 2013 . 141–154 . 10.1145/2465351.2465366. 9781450319942 .
  8. Liu . Tongping . Berger . Emery D. . SHERIFF: precise detection and automatic mitigation of false sharing . ACM SIGPLAN Notices . Association for Computing Machinery (ACM) . 46 . 10 . 2011-10-18 . 0362-1340 . 10.1145/2076021.2048070 . 3–18.