Skip to content

Instantly share code, notes, and snippets.

@azat
azat / test-clocks-monotonicity.cpp
Last active July 12, 2024 12:18
Test CLOCK_MONOTONIC/CLOCK_MONOTONIC_RAW/rdtsc monotonicity
#include <atomic>
#include <bits/types/clockid_t.h>
#include <cstdint>
#include <cstdlib>
#include <ctime>
#include <cerrno>
#include <iostream>
#include <list>
#include <random>
#include <sched.h>
@azat
azat / lockfree-rename-benchmark.sh
Created June 7, 2024 13:40
Measure overhead of fsync_part_directory for SELECTs from MergeTree/ReplicatedMergeTree in ClickHouse (due to rename under lock)
#!/usr/bin/env bash
# Measure overhead of fsync_part_directory for SELECTs from MergeTree/ReplicatedMergeTree (due to rename under lock)
# Refs: https://github.com/ClickHouse/ClickHouse/pull/64955
set -e
bin=$1 && shift
id=$BASHPID
@azat
azat / addr2line
Last active January 29, 2024 16:10
Wrapper for llvm addr2line (faster then binutils) for perf
#!/usr/bin/env bash
# This is wrapper for faster addr2line, that is required for "perf --call-graph dwarf"
# NOTE: since v6.3 perf supports specifying addr2line and supports LLVM so it is not required there
# stdbuf is important here to avoid buffering, otherwise perf will stuck
stdbuf -o0 -i0 sed -e 's/,//' -e 's/^$/ffffffffffffff/' | llvm-addr2line "$@"
@azat
azat / test-locks.cpp
Last active January 28, 2024 15:10
Test various locks (under contention and not)
#include <boost/smart_ptr/detail/spinlock.hpp>
#include <cstdint>
#include <cstring>
#include <cassert>
#include <iostream>
#include <list>
#include <mutex>
#include <optional>
#include <shared_mutex>
#include <thread>
@azat
azat / test-syscalls-musl-static.sh
Last active July 12, 2024 12:37
How many syscalls on Linux can you have?
$ lscpu -J | jq '.lscpu.[] | select(.field == "Model name:").data' -r
AMD Ryzen Threadripper PRO 5975WX 32-Cores
$ musl-clang -static -O3 -o test-syscalls test-syscalls.cpp
$ ldd test-syscalls
not a dynamic executable
$ ./test-syscalls
gettid: 48735.848us, 20518777.061 cps, 48.736 ns per call
clock_gettime (VDSO): 20660.516us, 48401501.686 cps, 20.661 ns per call
nothing_indirect_call: 0.020us, 50000000000000.000 cps, 0.000 ns per call
nothing: 0.020us, 50000000000000.000 cps, 0.000 ns per call
@azat
azat / test-clocks.cpp
Created January 26, 2024 08:26
Test clocks (clock_gettime and RDTSC)
#include <cstdint>
#include <ctime>
#include <cerrno>
#include <iostream>
#include <utility>
uint64_t now_ns(clockid_t clock)
{
struct timespec ts;
clock_gettime(clock, &ts);
@azat
azat / test-threads-v2.cpp
Last active January 25, 2024 21:35
test pthread_create
#include <cstdint>
#include <ctime>
#include <iostream>
#include <sys/types.h>
#include <thread>
#include <list>
uint64_t now_ns()
{
struct timespec ts;
@azat
azat / lockstat.py
Created January 17, 2024 16:14
pthread lock statistics with eBPF
#!/usr/bin/env python
# Based on https://github.com/goldshtn/linux-tracing-workshop
# Based on https://gist.github.com/SaveTheRbtz/807be09f73d13b80e429d45bd1707e00
"""
The MIT License (MIT)
Copyright (c) 2017 Sasha Goldshtein
Copyright (c) 2018 Alexey Ivanov
#include <bits/time.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <time.h>
// https://igoro.com/archive/gallery-of-processor-cache-effects/
u_int64_t now_ns()
@azat
azat / bench-jemalloc-cache-oblivious.c
Last active December 20, 2023 19:33
Answers the question "Does cache oblivious in jemalloc still make sense?" - Yes
#include <bits/time.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <time.h>
// Answers the question "Does cache oblivious in jemalloc still make sense?"
// The short answer is "Yes"!
//