False sharing
Overview
False sharing is a performance-degrading usage pattern that can occur in systems with a distributed, coherent cache where the smallest resource block size is managed by caching mechanisms.
- It refers to a condition where two processors attempt to write to different memory addresses that happen to map to the same cache line.
- When processors repeatedly write to the same cache line, it triggers a substantial amount of communication between their caches due to the cache coherence protocol.
Example
#include <cstdio>
#include <chrono>
#include <pthread.h>
constexpr size_t
#if defined(__cpp_lib_hardware_interference_size)
CACHE_LINE_SIZE = hardware_destructive_interference_size,
#else
CACHE_LINE_SIZE = 64,
#endif
MAX_THREADS = 8, MANY_ITERATIONS = 1000000000;
void* worker(void* arg) {
volatile int* counter = (int*)arg;
for (int i = 0; i < MANY_ITERATIONS; i++) (*counter)++;
return NULL;
}
void test1(int num_threads) {
auto begin = std::chrono::high_resolution_clock::now();
pthread_t threads[MAX_THREADS];
int counter[MAX_THREADS];
for (int i = 0; i < num_threads; i++)
pthread_create(&threads[i], NULL, &worker, &counter[i]);
for (int i = 0; i < num_threads; i++)
pthread_join(threads[i], NULL);
auto end = std::chrono::high_resolution_clock::now();
auto elapsed =
std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
printf("Time measured: %.3f seconds.\n", elapsed.count() * 1e-9);
}
struct padded_t
{
int counter;
char padding[CACHE_LINE_SIZE - sizeof(int)];
};
void test2(int num_threads) {
auto begin = std::chrono::high_resolution_clock::now();
pthread_t threads[MAX_THREADS];
padded_t counter[MAX_THREADS];
for (int i = 0; i < num_threads; i++)
pthread_create(&threads[i], NULL, &worker, &(counter[i].counter));
for (int i = 0; i < num_threads; i++)
pthread_join(threads[i], NULL);
auto end = std::chrono::high_resolution_clock::now();
auto elapsed =
std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
printf("Time measured: %.3f seconds.\n", elapsed.count() * 1e-9);
}
int main()
{
test1(8);
test2(8);
}
Running the above code yields results similar to the following:
Time measured: 2.946 seconds.
Time measured: 2.533 seconds.
Leave a comment