Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#!/usr/bin/env python32# Copyright (c) 2021-present The Bitcoin Core developers3# Distributed under the MIT software license, see the accompanying4# file COPYING or http://www.opensource.org/licenses/mit-license.php.6import sys7import ctypes8from bcc import BPF, USDT10"""Example logging Bitcoin Core utxo set cache flushes utilizing11the utxocache:flush tracepoint."""13# USAGE: ./contrib/tracing/log_utxocache_flush.py <pid of bitcoind>15# BCC: The C program to be compiled to an eBPF program (by BCC) and loaded into16# a sandboxed Linux kernel VM.17program = """18# include <uapi/linux/ptrace.h>20struct data_t21{22u64 duration;23u32 mode;24u64 coins_count;25u64 coins_mem_usage;26bool is_flush_for_prune;27};29// BPF perf buffer to push the data to user space.30BPF_PERF_OUTPUT(flush);32int trace_flush(struct pt_regs *ctx) {33struct data_t data = {};34bpf_usdt_readarg(1, ctx, &data.duration);35bpf_usdt_readarg(2, ctx, &data.mode);36bpf_usdt_readarg(3, ctx, &data.coins_count);37bpf_usdt_readarg(4, ctx, &data.coins_mem_usage);38bpf_usdt_readarg(5, ctx, &data.is_flush_for_prune);39flush.perf_submit(ctx, &data, sizeof(data));40return 0;41}42"""44FLUSH_MODES = [45'NONE',46'IF_NEEDED',47'PERIODIC',48'FORCE_FLUSH',49'FORCE_SYNC',50]53class Data(ctypes.Structure):54# define output data structure corresponding to struct data_t55_fields_ = [56("duration", ctypes.c_uint64),57("mode", ctypes.c_uint32),58("coins_count", ctypes.c_uint64),59("coins_mem_usage", ctypes.c_uint64),60("is_flush_for_prune", ctypes.c_bool)61]64def print_event(event):65print("%-15d %-12s %-15d %-15s %-8s" % (66event.duration,67FLUSH_MODES[event.mode],68event.coins_count,69"%.2f kB" % (event.coins_mem_usage/1000),70event.is_flush_for_prune71))74def main(pid):75print(f"Hooking into bitcoind with pid {pid}")76bitcoind_with_usdts = USDT(pid=int(pid))78# attaching the trace functions defined in the BPF program79# to the tracepoints80bitcoind_with_usdts.enable_probe(81probe="flush", fn_name="trace_flush")82b = BPF(text=program, usdt_contexts=[bitcoind_with_usdts])84def handle_flush(_, data, size):85""" Coins Flush handler.86Called each time coin caches and indexes are flushed."""87event = ctypes.cast(data, ctypes.POINTER(Data)).contents88print_event(event)90b["flush"].open_perf_buffer(handle_flush)91print("Logging utxocache flushes. Ctrl-C to end...")92print("%-15s %-12s %-15s %-15s %-8s" % ("Duration (µs)", "Mode",93"Coins Count", "Memory Usage",94"Flush for Prune"))96while True:97try:98b.perf_buffer_poll()99except KeyboardInterrupt:100exit(0)103if __name__ == "__main__":104if len(sys.argv) != 2:105print("USAGE: ", sys.argv[0], "<pid of bitcoind>")106exit(1)108pid = sys.argv[1]109main(pid)
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.