Skip to content

Instantly share code, notes, and snippets.

@azat
azat / 4k-vs-64k.txt
Created August 26, 2026 20:10
jemalloc: measure different LG_PAGE (4k/64k)
# $ join -t' ' <(tail -n+2 4k.txt | head -n-1 | awk '{printf "%s-%s %s\n", $1, $2, $3}' | sort) <(tail -n+2 64k.txt | head -n-1 | awk '{printf "%s-%s %s\n", $1, $2, $3}' | sort) | awk '{print $1, $2, $3, diff_q=($2-$3)/$3}' | sort -t= -k1,1 -k2,2n
alloc_free_pair-sz=16 6.6 6.7 -0.0149254
alloc_free_pair-sz=128 6.8 6.9 -0.0144928
alloc_free_pair-sz=1024 8.4 8.9 -0.0561798
alloc_free_pair-sz=4096 12.8 13.3 -0.037594
alloc_free_pair-sz=8192 17.4 16.8 0.0357143
alloc_free_pair-sz=16384 25.5 22.1 0.153846
alloc_free_pair-sz=32768 35.4 32.7 0.0825688
alloc_free_pair-sz=262144 274.4 330.8 -0.170496
alloc_free_pair-sz=1048576 330.8 360.2 -0.0816213
// Does jemalloc cache-oblivious large-allocation offsetting still make sense?
//
// Allocates many large-class buffers and touches the same offset(s) in every
// buffer. With cache-oblivious disabled all buffers are page-aligned, so the
// touched lines collide in the L1 set index bits (and since page-offset bits
// survive virtual->physical translation, they also lose most of the set
// diversity of the physically-indexed L2/L3; THP makes it worse but is not
// required); with it enabled jemalloc randomizes the intra-page offset and
// the lines spread across sets.
//
#!/usr/bin/env python3
import clickhouse_driver
c = clickhouse_driver.Client(host='127.1')
c.execute('select * from numbers(100000)', settings={'send_profile_events': 0})
c.execute('select * from numbers(100000)', settings={'send_profile_events': 1})
c.execute('insert into function null() values', ((x, ) for x in range(5)), settings={'send_profile_events': 1})
# this is broken
$ python /tmp/clickhouse-exception.py
(0, '!#Vf5ABBlu!?8He&[nGr')
(0, '\\4(?%F5=>i,2l$?h<@!$')
(0, ':225JvK(tIr>!:*#t\\cD')
(0, 'E93AuwfS%QL~<NYr(&!M')
(0, 'r}e\\ K;8_w$x&fu [ht"')
(0, 'I\\x Kicp;N@okI?*J23 ')
(0, "ml{pd'|O>RX14@][H )]")
(0, 'S"Rj"_c\'".L,MB#9f)\'M')
(0, "'$ltY~+FpQ<=OA_^Edt+")
// 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