Skip to content

Instantly share code, notes, and snippets.

// Written with ChatGPT
#include <iostream>
#include <chrono>
#include <format>
#include <print>
#include <fmt/core.h>
#include <fmt/format.h>
#include <fmt/chrono.h>
#include <fmt/ranges.h>
@azat
azat / clickhouse.untracked_memory.stacktrace
Created December 10, 2024 20:05
Example of stacktrace with untracked_memory for ThreadStatus
2024.12.10 14:13:33.100758 [ 3466 ] {b0c2bae7-e930-484d-9a98-26d972c8a985} <Trace> PushingToViews: Pushing (sequentially) from default.t_null (97ff26c2-3608-4b4b-b9a9-41696dd3f1a1) to default.t_mv (6ce3ce80-e1b0-4986-a79f-6c318399a8a6) took 21719 ms.
2024.12.10 14:13:33.100823 [ 3466 ] {} <Error> ThreadStatus: Untracked memory left: -96.00 B
2024.12.10 14:13:33.100969 [ 3135 ] {} <Trace> BaseDaemon: Received signal 6
2024.12.10 14:13:33.101277 [ 13793 ] {} <Fatal> BaseDaemon: ########## Short fault info ############
2024.12.10 14:13:33.101325 [ 13793 ] {} <Fatal> BaseDaemon: (version 24.12.1.1, build id: FBBF1E9C30254289BBFDF63093B967C2508ACB4C, git hash: 423da86c3064580c77679684d61b2b9d73da756f, architecture: x86_64) (from thread 3466) Received signal 6
2024.12.10 14:13:33.101344 [ 13793 ] {} <Fatal> BaseDaemon: Signal description: Aborted
2024.12.10 14:13:33.101351 [ 13793 ] {} <Fatal> BaseDaemon:
2024.12.10 14:13:33.101370 [ 13793 ] {} <Fatal> BaseDaemon: Stack trace: 0x000000000c16f4ed 0x000000000c42cafc
import os
import pyinotify
def get_current_memory_cgroup_path():
try:
with open("/proc/self/cgroup", "r") as f:
for line in f:
parts = line.strip().split(":")
if len(parts) == 3 and parts[1] == "":
#!/usr/bin/env python3
import matplotlib.pyplot as plt
def hashsocket_1(fd):
return fd
def hashsocket_2(fd):
@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);