False-sharing-demo - Demo for performance effects of CPU cache false-sharing

Example of CPU cache false-sharing in Go.

A simple example where 2 integer variables are incremented concurrently.
Baseline version suffers from false-sharing due to values share same cache line:

type IntVars struct {
	i1 int64
	i2 int64
}

Optimized version eliminates false sharing by introducing padding between memory locations:

type IntVars struct {
	i1 int64
	_  cpu.CacheLinePad // padding
	i2 int64
}

Reproducing results

  1. Install benchstat:
go install golang.org/x/perf/cmd/benchstat@latest
  1. Run benchmarks for simple increments: a++:
▶ make bench

name                          old time/op  new time/op  delta
Increment1Value-2             1.66ns ± 8%  1.71ns ± 7%     ~     (p=0.421 n=5+5)
Increment2ValuesInParallel-2  2.34ns ± 5%  1.59ns ± 3%  -32.23%  (p=0.008 n=5+5)
  1. Run benchmarks for atomic increments: atomic.AddInt64(addr, 1):
▶ make bench-atomic

name                          old time/op  new time/op  delta
Increment1Value-2             5.65ns ± 5%  5.85ns ± 6%     ~     (p=0.310 n=5+5)
Increment2ValuesInParallel-2  41.6ns ±10%   5.4ns ± 8%  -87.12%  (p=0.008 n=5+5)

CPU cache miss stats

On Linux, one can measure L1 cache misses to demonstrate false-sharing.

  1. Build executables for both original and optimized versions:
▶ make build

GOOS=linux GOARCH=amd64 go build -o test
GOOS=linux GOARCH=amd64 go build -tags padded -o test-padded
  1. Run perf for both executables and compare numbers:
▶ perf stat -B -e L1-dcache-load-misses ./test

 Performance counter stats for './test':

         8,954,010      L1-dcache-load-misses
▶ perf stat -B -e L1-dcache-load-misses ./test-padded

 Performance counter stats for './test-padded':

           204,287      L1-dcache-load-misses
Similar Resources

Demo of schema change failures with SQLite INTEGERs.

SQLite Schema Migration Bug This is a repository to reproduce a bug with Ent and the modernc.org/sqlite (non-CGO) SQLite 3 driver. Reproduce Simply ru

Dec 9, 2021

Small demo of using physac 2d physics engine in golang

Physac-go 2D physics engine in golang For now this is just a small demo. Maybe later I'll convert it into a proper module. References Original Physac

Jul 26, 2022

A simple application, demo at this point, on how to pull a backup from Collibra on prem (say for Cohesity backup)

A simple application, demo at this point, on how to pull a backup from Collibra on prem (say for Cohesity backup)

go-get-collibra-backup Introduction This repository is a very simple go application that's intended, at this point, more of a demonstration about how

Dec 10, 2021

A kubernetes operator demo generated by code-generator, it only watches cr's events

intro a small operator demo which only watches car cr's events. environment: ubuntu-20.04-amd64 k3s-1.21 docker-20.10.7 go-1.17 tools code-generator d

Dec 13, 2021

Automated-gke-cilium-networkpolicy-demo - Quickly provision and tear down a GKE cluster with Cilium enabled for working with Network Policy.

Automated GKE Network Policy Demo Before running the automation, make sure you have the correct variables in env-automation/group_vars/all.yaml. There

Jan 1, 2022

Demo of skaffold's port-forwarding with ko builder (does not work)

skaffold port-forwarding : Ko builder vs docker builder When using ko builder (see folder ko/), port forwarding does not work (skaffold debug or skaff

Jan 6, 2022

Demo of creating a 5 year subscription using Stripe Subscriptions.

Scheduled subscription demo This simple demo shows how to create a subscription in Stripe with a 5 year duration. This is accomplished by charging the

Jan 19, 2022

GoKubernetes - Demo application for Kubernetes Up and Running

GoKubernetes - Demo application for Kubernetes Up and Running

Demo application for "Kubernetes Up and Running" Running kubectl run --restart=N

Jan 22, 2022

Kubernetes Admission Controller Demo: Validating Webhook for Namespace lifecycle events

Kubernetes Admission Controller Based on How to build a Kubernetes Webhook | Admission controllers Local Kuberbetes cluster # create kubernetes cluste

Feb 27, 2022
kubernetes Display Resource (CPU/Memory/Gpu/PodCount) Usage and Request and Limit.
kubernetes Display Resource (CPU/Memory/Gpu/PodCount) Usage and Request and Limit.

kubectl resource-view A plugin to access Kubernetes resource requests, limits, and usage. Display Resource (CPU/Memory/Gpu/PodCount) Usage and Request

Apr 22, 2022
Litestream-read-replica-demo - A demo application for running live read replication on fly.io with Litestream

Litestream Read Replica Demo A demo application for running live read replicatio

Oct 18, 2022
The dataset metadata sharing platform beckend
The dataset metadata sharing platform beckend

portal-backend The dataset metadata sharing platform backend. How to contribute If you’re interested in contributing code, the best starting point is

Nov 17, 2022
concurrent, cache-efficient, and Dockerfile-agnostic builder toolkit
concurrent, cache-efficient, and Dockerfile-agnostic builder toolkit

BuildKit BuildKit is a toolkit for converting source code to build artifacts in an efficient, expressive and repeatable manner. Key features: Automati

Dec 31, 2022
Aws-secretsmanager-caching-extension - Cache server for AWS Secrets Manager
Aws-secretsmanager-caching-extension - Cache server for AWS Secrets Manager

AWS Lambda Extension / Sidecar Container Cache Server The cache server is writte

Aug 12, 2022
Generic sharded thread safe LRU cache in Go.

cache Cache is a thread safe, generic, and sharded in memory LRU cache object. This is achieved by partitioning values across many smaller LRU (least

Sep 12, 2022
A demo repository that shows CI/CD integration using DroneCI + ArgoCD + Kubernetes.
A demo repository that shows CI/CD integration using DroneCI + ArgoCD + Kubernetes.

CI/CD Demo This is the demo repo for my blog post. This tutorial shows how to build CI/CD pipeline with DroneCI and ArgoCD. In this demo, we use Drone

Oct 18, 2022
Automating Kubernetes Rollouts with Argo and Prometheus. Checkout the demo URL below
Automating Kubernetes Rollouts with Argo and Prometheus. Checkout the demo URL below

observe-argo-rollout Demo for Automating and Monitoring Kubernetes Rollouts with Argo and Prometheus Performing Demo The demo can be found on Katacoda

Nov 16, 2022
A super simple demo to document my journey to reasonably sized docker containers.

hello-docker A super simple demo to document my journey to reasonably sized docker containers. Task at Hand Build a docker container as small as possi

Nov 30, 2021
IP Counter Demo

IP Counter Demo This is a demo of maintaining counts of IP hits and a top 100 list of the IPs with the most hits. How to Run $ go build # macOS or Lin

Nov 25, 2021