{"id":16799248,"url":"https://github.com/steve-chavez/spinlocks","last_synced_at":"2025-03-17T04:44:55.504Z","repository":{"id":209663815,"uuid":"532744869","full_name":"steve-chavez/spinlocks","owner":"steve-chavez","description":"Just some experiments with spinlocks, done in C99","archived":false,"fork":false,"pushed_at":"2023-11-28T14:19:42.000Z","size":9,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-15T15:51:47.594Z","etag":null,"topics":["c99","spinlock"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/steve-chavez.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2022-09-05T04:39:14.000Z","updated_at":"2023-11-28T14:17:33.000Z","dependencies_parsed_at":"2023-11-28T15:44:46.632Z","dependency_job_id":null,"html_url":"https://github.com/steve-chavez/spinlocks","commit_stats":null,"previous_names":["steve-chavez/spinlocks"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steve-chavez%2Fspinlocks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steve-chavez%2Fspinlocks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steve-chavez%2Fspinlocks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steve-chavez%2Fspinlocks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/steve-chavez","download_url":"https://codeload.github.com/steve-chavez/spinlocks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243976490,"owners_count":20377692,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c99","spinlock"],"created_at":"2024-10-13T09:28:20.215Z","updated_at":"2025-03-17T04:44:55.479Z","avatar_url":"https://github.com/steve-chavez.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Spinlocks\n\nJust some experiments with spinlocks, done in C99. Using this [excellent playlist by CoffeeBeforeArch on youtube](https://www.youtube.com/playlist?list=PLxNPSjHT5qvsZyes3ATYvhPhwkoY33L2v).\n\n## Setup\n\nYou'll need to install [Nix](https://nixos.org/download) to try these. I tried to install the `perf` command with `apt` and failed miserably. Nix makes that easy plus with `nix-shell` it's only available for this project.\n\nThen do:\n\n```\n$ nix-shell\n```\n\n## Naive\n\n\n```\nmake spinlock\n\nspinlock-bench ./spinlock.o\nThe count is: 100000\n\nreal    0m0.009s\nuser    0m0.005s\nsys     0m0.004s\nThe count is: 200000\n\nreal    0m0.023s\nuser    0m0.044s\nsys     0m0.000s\nThe count is: 400000\n\nreal    0m0.103s\nuser    0m0.392s\nsys     0m0.000s\nThe count is: 800000\n\nreal    0m0.478s\nuser    0m3.658s\nsys     0m0.000s\n\nspinlock-stat ./spinlock.o\nThe count is: 800000\n\n Performance counter stats for './spinlock.o 8':\n\n          3,971.95 msec task-clock:u              #    7.469 CPUs utilized\n                 0      context-switches:u        #    0.000 K/sec\n                 0      cpu-migrations:u          #    0.000 K/sec\n                71      page-faults:u             #    0.018 K/sec\n     8,174,888,035      cycles:u                  #    2.058 GHz                      (61.85%)\n        68,128,831      instructions:u            #    0.01  insn per cycle           (74.56%)\n        18,262,842      branches:u                #    4.598 M/sec                    (74.78%)\n           832,278      branch-misses:u           #    4.56% of all branches          (75.25%)\n        19,736,218      L1-dcache-loads:u         #    4.969 M/sec                    (75.59%)\n         8,759,096      L1-dcache-load-misses:u   #   44.38% of all L1-dcache hits    (75.64%)\n         1,235,026      LLC-loads:u               #    0.311 M/sec                    (49.92%)\n               252      LLC-load-misses:u         #    0.02% of all LL-cache hits     (49.59%)\n\n       0.531788030 seconds time elapsed\n\n       3.969189000 seconds user\n       0.004009000 seconds sys\n```\n\n## Locally spinning\n\n```\nmake spinlockally\n\nspinlock-bench ./spinlockally.o\nThe count is: 100000\n\nreal    0m0.004s\nuser    0m0.004s\nsys     0m0.000s\nThe count is: 200000\n\nreal    0m0.023s\nuser    0m0.044s\nsys     0m0.000s\nThe count is: 400000\n\nreal    0m0.083s\nuser    0m0.323s\nsys     0m0.000s\nThe count is: 800000\n\nreal    0m0.219s\nuser    0m1.668s\nsys     0m0.004s\n\nspinlock-stat ./spinlockally.o\nThe count is: 800000\n\n Performance counter stats for './spinlockally.o 8':\n\n          2,151.71 msec task-clock:u              #    7.687 CPUs utilized\n                 0      context-switches:u        #    0.000 K/sec\n                 0      cpu-migrations:u          #    0.000 K/sec\n                74      page-faults:u             #    0.034 K/sec\n     4,460,741,205      cycles:u                  #    2.073 GHz                      (61.42%)\n       807,699,645      instructions:u            #    0.18  insn per cycle           (74.35%)\n       269,623,840      branches:u                #  125.307 M/sec                    (74.48%)\n         3,527,201      branch-misses:u           #    1.31% of all branches          (75.47%)\n       267,746,311      L1-dcache-loads:u         #  124.434 M/sec                    (76.45%)\n        11,623,242      L1-dcache-load-misses:u   #    4.34% of all L1-dcache hits    (76.58%)\n         7,881,325      LLC-loads:u               #    3.663 M/sec                    (49.73%)\n                 4      LLC-load-misses:u         #    0.00% of all LL-cache hits     (48.78%)\n\n       0.279924663 seconds time elapsed\n\n       2.149269000 seconds user\n       0.004017000 seconds sys\n```\n\n## Active backoff\n\n```\nmake spinlock_active_backoff\n\nspinlock-bench ./spinlock_active_backoff.o\nThe count is: 100000\n\nreal    0m0.005s\nuser    0m0.005s\nsys     0m0.000s\nThe count is: 200000\n\nreal    0m0.012s\nuser    0m0.020s\nsys     0m0.000s\nThe count is: 400000\n\nreal    0m0.033s\nuser    0m0.126s\nsys     0m0.000s\nThe count is: 800000\n\nreal    0m0.123s\nuser    0m0.894s\nsys     0m0.000s\n```\n\n## Passive backoff\n\n```\nmake spinlock_passive_backoff\n\nspinlock-bench ./spinlock_passive_backoff.o\nThe count is: 100000\n\nreal    0m0.005s\nuser    0m0.005s\nsys     0m0.000s\nThe count is: 200000\n\nreal    0m0.018s\nuser    0m0.034s\nsys     0m0.000s\nThe count is: 400000\n\nreal    0m0.040s\nuser    0m0.156s\nsys     0m0.000s\nThe count is: 800000\n\nreal    0m0.117s\nuser    0m0.875s\nsys     0m0.000s\n```\n\n## pthread spinlock\n\n```\nmake spinlock_pthread\n\nspinlock-bench ./spinlock_pthread.o\nThe count is: 100000\n\nreal    0m0.002s\nuser    0m0.002s\nsys     0m0.000s\nThe count is: 200000\n\nreal    0m0.005s\nuser    0m0.008s\nsys     0m0.000s\nThe count is: 400000\n\nreal    0m0.017s\nuser    0m0.051s\nsys     0m0.004s\nThe count is: 800000\n\nreal    0m0.040s\nuser    0m0.252s\nsys     0m0.000s\n```\n\n## Exponential backoff\n\n```\nmake spinlock_exp_backoff\n\nspinlock-bench ./spinlock_exp_backoff.o\nThe count is: 100000\n\nreal    0m0.006s\nuser    0m0.006s\nsys     0m0.000s\nThe count is: 200000\n\nreal    0m0.012s\nuser    0m0.014s\nsys     0m0.005s\nThe count is: 400000\n\nreal    0m0.012s\nuser    0m0.039s\nsys     0m0.000s\nThe count is: 800000\n\nreal    0m0.018s\nuser    0m0.102s\nsys     0m0.000s\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsteve-chavez%2Fspinlocks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsteve-chavez%2Fspinlocks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsteve-chavez%2Fspinlocks/lists"}