Usdt txgraph tracing #35004

pull HowHsu wants to merge 5 commits into bitcoin:master from HowHsu:usdt-txgraph-tracing-v2 changing 9 files +1534 −0
  1. HowHsu commented at 3:46 PM on April 4, 2026: contributor

    contrib: USDT-based TxGraph tracing pipeline

    This comes from benmarking #34821

    Summary

                                                                                 

    Add a complete tracing pipeline for TxGraph based on USDT tracepoints:

    • 27 USDT tracepoints covering every public TxGraph API (mutations, staging, queries, maintenance)
    • BCC recording script that attaches to all tracepoints via eBPF and writes a binary TXGTRACE trace file
    • Analysis script that parses trace files and reports cluster size distribution, chain-shaped topology classification, and dependency statistics
    • Replay tool that reads trace files, reconstructs TxGraph API calls, and reports per-entry-point timing — used to compare TxGraph performance across different implementations

    Motivation

                                                                                 

    When optimizing TxGraph internals, we need tools to:

    1. Capture real workloads: record the exact sequence of TxGraph operations from a live mainnet node
    2. Understand mempool structure: analyze cluster topology (size distribution, chain vs non-chain, dependency density)
    3. Compare implementations: replay the same trace on different branches for fair, reproducible performance comparison

    The USDT-based approach requires no wrapper class and has negligible overhead when no tracer is attached (one branch check per tracepoint, predicted not-taken).

    Commits

                                                                                 

    1. txgraph: add USDT tracepoints for all TxGraph API operations

                                                                                 

    Adds 27 USDT tracepoints to src/txgraph.cpp and one txgraph:init tracepoint in src/txmempool.cpp (at the MakeTxGraph call site). Variable-length operations (get_ancestors_union, get_descendants_union, count_distinct_clusters) use TRACEPOINT_ACTIVE to conditionally build a fixed-size stack buffer of indices, passed as a pointer for eBPF to read via bpf_probe_read_user. All arguments are explicitly cast to 64-bit types to work around a BCC 0.31.0 bug where bpf_usdt_readarg fails to read sub-8-byte stack arguments (4@offset(%rsp) descriptors always return zero; only 8@ descriptors work correctly).

    2. contrib: add BCC script to record TxGraph traces via USDT

                                                                                 

    contrib/tracing/txgraph/txgraph_trace_recorder.py — a BCC Python script that attaches to all 27 tracepoints and writes events to a TXGTRACE binary file. Each eBPF handler is generated as a standalone C function from Python data definitions (BCC does not allow its builtins inside C macro expansions). For complete traces, bitcoind should be started with TXGRAPH_WAIT_FOR_TRACER=1 environment variable.

    3. contrib: add trace analysis script for cluster topology

                                                                                 

    contrib/tracing/txgraph/analyze_trace.py — parses TXGTRACE files and reports peak/final mempool state, cluster size distribution, chain-shaped cluster classification, and edge sanity checks. Correctly handles staged mutations.

    4. contrib: add txgraph-replay trace replay tool

                                                                                 

    contrib/tracing/txgraph/txgraph_replay.cpp — standalone C++ tool. Replays all operations from a trace file, timing query/trigger operations while leaving mutations untimed. Reports per-entry-point statistics. Built separately from Bitcoin Core via contrib/tracing/txgraph/build_replay.sh, which links against pre-built Bitcoin Core libraries. No changes to Bitcoin Core's CMake build system are required. When started with TXGRAPH_WAIT_FOR_TRACER=1, bitcoind waits for a tracer to attach before mempool initialization (with a 2-second grace period for the BCC perf buffer to become ready), ensuring the trace captures every event from the start.

    Usage

                                                                                 
    # Build Bitcoin Core (standard build, no special flags needed)                   
    cmake -B build                                                                   
    cmake --build build -j$(nproc)                                                   
                                                                                     
    # Build txgraph-replay tool separately                                           
    contrib/tracing/txgraph/build_replay.sh                                          
                                                                                     
    # Start bitcoind (with env var to wait for tracer)                               
    TXGRAPH_WAIT_FOR_TRACER=1 bitcoind -datadir=...                                  
                                                                                     
    # Record trace (in another terminal)                                             
    sudo python3 contrib/tracing/txgraph/txgraph_trace_recorder.py \                 
        -p $(pidof bitcoind) -o trace.bin                                            
                                                                                     
    # Analyze cluster topology                                                       
    python3 contrib/tracing/txgraph/analyze_trace.py trace.bin                       
                                                                                     
    # Replay for performance comparison                                              
    ./build/bin/txgraph-replay trace.bin                                             
    
                                                                                 

    Sample output

                                                                                 

    analyze_trace.py

                                                                                 
    Parameters: max_cluster_count=64 max_cluster_size=404000 acceptable_cost=75000   
    Processed 78223 operations, 9736 CommitStagings                                  
    Peak mempool size: 9688 transactions (at op [#74231](/bitcoin-bitcoin/74231/))                              
                                                                                     
      Peak state (9688 transactions)                                                 
      9688 transactions, 3306 clusters                                               
                                                                                     
        Size    Clusters      Chains   Non-chain    Chain%                           
           1        2723        2723           0    100.0%                           
           2         212         212           0    100.0%                           
          25         207         207           0    100.0%                           
       TOTAL        3306        3266          40     98.8%                           
    
                                                                                 

    txgraph-replay

                                                                                 
    === TxGraph Replay Summary ===                                                   
    Total ops replayed: 78222                                                        
                                                                                     
    Timed entry points:                                                              
      Operation                           Calls     Total (us)       Avg (us)        
      DoWork                               9737         115647          11.88        
      GetMainMemoryUsage                  19482              9           0.00        
      CommitStaging                        9736             14           0.00        
      TOTAL                               58441         115798                       
    

    ~
    ~

  2. DrahtBot commented at 3:46 PM on April 4, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/35004.

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    Concept NACK 0xB10C

    If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

    LLM Linter (✨ experimental)

    Possible places where named args for integral literals may be used (e.g. func(x, /*named_arg=*/0) in C++, and func(x, named_arg=0) in Python):

    • ReadBytes(f, magic, 8) in contrib/tracing/txgraph/txgraph_replay.cpp
    • ReadBytes(f, &init_op, 1) in contrib/tracing/txgraph/txgraph_replay.cpp
    • ReadBytes(f, &out.level, 1) in contrib/tracing/txgraph/txgraph_replay.cpp
    • ReadBytes(f, &level_byte, 1) in contrib/tracing/txgraph/txgraph_replay.cpp
    • ReadBytes(f, &level_byte, 1) in contrib/tracing/txgraph/txgraph_replay.cpp
    • ReadBytes(f, &level_byte, 1) in contrib/tracing/txgraph/txgraph_replay.cpp
    • ReadBytes(f, &level_byte, 1) in contrib/tracing/txgraph/txgraph_replay.cpp
    • ReadBytes(f, &level_byte, 1) in contrib/tracing/txgraph/txgraph_replay.cpp
    • ReadBytes(f, &level_byte, 1) in contrib/tracing/txgraph/txgraph_replay.cpp

    <sup>2026-04-05 06:56:05</sup>

  3. HowHsu force-pushed on Apr 4, 2026
  4. DrahtBot added the label CI failed on Apr 4, 2026
  5. HowHsu force-pushed on Apr 5, 2026
  6. txgraph: add USDT tracepoints for all TxGraph API operations
    Add 27 USDT tracepoints covering every public TxGraph method:
    mutations (add_transaction, remove_transaction, add_dependency,
    set_transaction_fee, unlink_ref), staging (start/abort/commit),
    queries (get_ancestors, get_descendants, get_cluster, exists, etc.),
    and maintenance (do_work, trim, get_block_builder).
    
    Variable-length operations (get_ancestors_union, get_descendants_union,
    count_distinct_clusters) use TRACEPOINT_ACTIVE to conditionally build
    a fixed-size stack buffer of per-element indices, passed as a pointer
    for eBPF to read via bpf_probe_read_user.
    
    The init tracepoint is placed in CTxMemPool's constructor (txmempool.cpp)
    where MakeTxGraph() is called with the configuration parameters. When
    the TXGRAPH_WAIT_FOR_TRACER environment variable is set, bitcoind waits
    for a tracer to attach before firing the init tracepoint, with a
    2-second grace period for the BCC perf buffer to become ready.
    
    All tracepoint arguments are explicitly cast to 64-bit types (uint64_t
    or int64_t) to work around a BCC bug where bpf_usdt_readarg fails to
    read 4-byte USDT stack arguments (descriptors like 4@offset(%rsp)
    always return zero; only 8-byte descriptors work correctly).
    9a41d69eb8
  7. contrib: add BCC script to record TxGraph traces via USDT
    Add a Python BCC script that attaches to all TxGraph USDT tracepoints
    and records operations into a binary TXGTRACE file for offline replay.
    
    The eBPF handler functions are generated at runtime from Python data
    definitions rather than C preprocessor macros, because BCC does not
    allow its builtins (bpf_usdt_readarg, perf_submit) inside macro
    expansions.
    
    Variable-length operations (get_ancestors_union, get_descendants_union,
    count_distinct_clusters) read a fixed-size 64-element uint32_t buffer
    from userspace via bpf_probe_read_user.
    dae803c814
  8. contrib: add trace analysis script for cluster topology
    Add analyze_trace.py that parses TXGTRACE binary files and reports:
    - Peak and final mempool size (transaction count)
    - Cluster size distribution
    - Chain-shaped vs non-chain cluster classification
    - Dependency statistics and edge sanity checks
    
    Correctly handles staged mutations (only applied on CommitStaging,
    discarded on AbortStaging) and tracks oversized cluster warnings.
    4531c38fef
  9. contrib: add txgraph-replay trace replay tool
    Add a standalone tool that replays TXGTRACE binary trace files and
    reports per-entry-point timing statistics. Used to compare TxGraph
    performance across different implementations.
    
    Reads the binary format produced by txgraph_trace_recorder.py,
    reconstructs TxGraph API calls, and measures execution time for
    each query/trigger operation. Mutations are replayed but not timed.
    
    Built separately from Bitcoin Core via build_replay.sh, which links
    against pre-built Bitcoin Core libraries. A standalone CMakeLists.txt
    accepts BITCOIN_SOURCE_DIR and BITCOIN_BUILD_DIR parameters. No
    modifications to Bitcoin Core's root or src/ CMake files are required.
    ccbdaa8594
  10. contrib: extract tracing_defs.py as single source of truth for probe definitions
    Move all opcode constants, probe argument types, and binary format
    serialization/deserialization into tracing_defs.py. Both the BCC
    recorder and the analysis script now import from this shared module.
    
    Adding a new TxGraph tracepoint only requires adding one entry to the
    PROBES list in tracing_defs.py (plus the TRACEPOINT call in txgraph.cpp
    and the dispatch case in txgraph_replay.cpp). The recorder and analyzer
    adapt automatically.
    83f28a6010
  11. HowHsu force-pushed on Apr 5, 2026
  12. DrahtBot removed the label CI failed on Apr 5, 2026
  13. HowHsu marked this as ready for review on Apr 15, 2026
  14. fanquake commented at 11:50 AM on April 30, 2026: member

    Just FYI, there is some discussion happening about swapping out USDT for an IPC based interface. You might have some thoughts to contribute there, if you are actively using it: #35142.

  15. HowHsu commented at 3:43 PM on April 30, 2026: contributor

    Just FYI, there is some discussion happening about swapping out USDT for an IPC based interface. You might have some thoughts to contribute there, if you are actively using it: #35142.

    Thanks @fanquake , I'll take a look tomorrow

  16. 0xB10C commented at 10:06 PM on April 30, 2026: contributor

    Hi @HowHsu, you write:

    When optimizing TxGraph internals, we need tools to...

    Have you or someone else been using this for optimizing TxGraph internals? Is there anything to read up on where this has proven to be valuable and necessary? A PR or blog post maybe. I'm not familiar with "TxGraph internals" and what optimizations are still needed, so some more background would help to motivate this PR for me.

    Furthermore, is this something you think needs to be done continuesly and/or repeatedly?

    I'm asking because this a rather large PR to review, adding more tracepoint than we currently maintain, and a bunch of untested tooling that I'm not sure is being used right now or in the future.

    If this is tooling for a one-off research, it might not be something others want to review, merge, and maintain here.

  17. HowHsu commented at 3:44 AM on May 1, 2026: contributor

    Hi @HowHsu, you write:

    When optimizing TxGraph internals, we need tools to...

    Have you or someone else been using this for optimizing TxGraph internals? Is there anything to read up on where this has proven to be valuable and necessary? A PR or blog post maybe. I'm not familiar with "TxGraph internals" and what optimizations are still needed, so some more background would help to motivate this PR for me.

    Hi @0xB10C , this comes from benmarking #34821, I should have added it at the beginning of this PR description.

    Furthermore, is this something you think needs to be done continuesly and/or repeatedly?

    I did this because, while developing PR 34821, there wasn’t a tool to trace txgraph behavior on a running node or replay it for performance measurement. I believe this will be useful in the future for evaluating whether a given txgraph code change actually has a measurable impact on real-world data.

  18. 0xB10C commented at 9:49 AM on May 6, 2026: contributor

    Furthermore, is this something you think needs to be done continuesly and/or repeatedly?

    I did this because, while developing PR 34821, there wasn’t a tool to trace txgraph behavior on a running node or replay it for performance measurement. I believe this will be useful in the future for evaluating whether a given txgraph code change actually has a measurable impact on real-world data.

    Speaking with people a bit more familiar with txgraph offline, we wondered if this needs to be added and maintained here. I agree that this might be useful to measure changes in the future, but it's not clear to me how frequent these will be done going forward. Can this be a patchset that sits in one of your branches and rebased once you need it again (especially since this is only adding code, not chaning/removing anything)? It seems to me like it can and I lean to a light NACK on merging this here.

  19. edilmedeiros commented at 5:58 PM on May 8, 2026: contributor

    I tend to agree with 0xb10c (https://github.com/bitcoin/bitcoin/pull/35004#issuecomment-4386865213): changes are pretty much orthogonal to anything else and can probably be maintained in a separate fork for a while. I can be wrong, but I feel that the overall sentiment about ebpf tracepoints is that the current balance between maintenance hurdle and usefulness is not great (see #35142). I would suggest waiting for the discussion in #35142 to mature a bit more as it can result in deprecating the current tracepoint interface.

  20. HowHsu closed this on May 9, 2026

  21. HowHsu reopened this on May 9, 2026

  22. HowHsu commented at 1:01 AM on May 9, 2026: contributor

    Close this one according to the comments. Let's wait for https://github.com/bitcoin/bitcoin/issues/35142

  23. HowHsu closed this on May 9, 2026


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-05-12 15:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me