false sharing
   HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to practical disciplines (includi ...
, 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. 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 Multiprocessing is the use of two or more central processing units (CPUs) within a single computer system. The term also refers to the ability of a system to support more than one processor or the ability to allocate tasks between them. There ar ...
CPU cache A CPU cache is a hardware cache used by the central processing unit (CPU) of a computer to reduce the average cost (time or energy) to access data from the main memory. A cache is a smaller, faster memory, located closer to a processor core, whic ...
s, where
memory Memory is the faculty of the mind by which data or information is encoded, stored, and retrieved when needed. It is the retention of information over time for the purpose of influencing future action. If past events could not be remembered ...
is cached in
lines Line most often refers to: * Line (geometry), object with zero thickness and curvature that stretches to infinity * Telephone line, a single-user circuit on a telephone communication system Line, lines, The Line, or LINE may also refer to: Arts ...
of some small
power of two A power of two is a number of the form where is an integer, that is, the result of exponentiation with number two as the base and integer  as the exponent. In a context where only integers are considered, is restricted to non-negativ ...
word A word is a basic element of language that carries an objective or practical meaning, can be used on its own, and is uninterruptible. Despite the fact that language speakers often have an intuitive grasp of what a word is, there is no conse ...
size (e.g., 64 aligned, contiguous
byte The byte is a unit of digital information that most commonly consists of eight bits. Historically, the byte was the number of bits used to encode a single character of text in a computer and for this reason it is the smallest addressable uni ...
s). If two processors operate on independent data in the same
memory address In computing, a memory address is a reference to a specific memory location used at various levels by software and hardware. Memory addresses are fixed-length sequences of digits conventionally displayed and manipulated as unsigned integers. ...
region storable in a single line, the cache coherency mechanisms in the system may force the whole line across the
bus A bus (contracted from omnibus, with variants multibus, motorbus, autobus, etc.) is a road vehicle that carries significantly more passengers than an average car or van. It is most commonly used in public transport, but is also in use for cha ...
or interconnect with every data write, forcing memory stalls in addition to wasting system
bandwidth Bandwidth commonly refers to: * Bandwidth (signal processing) or ''analog bandwidth'', ''frequency bandwidth'', or ''radio bandwidth'', a measure of the width of a frequency range * Bandwidth (computing), the rate of data transfer, bit rate or thr ...
. In some cases, the elimination of false sharing can result in order-of-magnitude performance improvements. 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

#include #include #include using namespace std; constexpr bool FALSE_SHARING = true; constexpr size_t #if defined(__cpp_lib_hardware_interference_size) CACHE_LINE_SIZE = hardware_destructive_interference_size, #else CACHE_LINE_SIZE = 64, #endif SECOND_ALIGN = FALSE_SHARING ? sizeof(int) : CACHE_LINE_SIZE; using atomic_type = atomic; struct shared_or_not ; int main() This C++20-code shows the impact of false sharing. The first member a of shared_or_not is always aligned on a cacheline beginning to prevent the data structure to straddle a cacheline so that a and b are accidentally split and there's no way to show the effect of false sharing. The second member b is directly placed after a in the same cacheline or aligned on the next cacheline depending on if FALSE_SHARING is set to true or false. If FALSE_SHARING is set to false the code usually runs multiple times faster.


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 Padding is thin cushioned material sometimes added to clothes. Padding may also be referred to as batting when used as a layer in lining quilts or as a packaging or stuffing material. When padding is used in clothes, it is often done in an attempt ...
(unused bytes) between variables. However, some of these program changes may increase the size of the objects, leading to higher memory use. Compile-time data transformations can also mitigate false-sharing. 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. There are tools for detecting false sharing. There are also systems that both detect and repair false sharing in executing programs. However, these systems incur some execution overhead.


References

{{reflist


External links


Easy Understanding on False Sharing



Dr Dobbs article: Eliminate False Sharing


Cache coherency Computer memory