bitcoin/src/test/CMakeLists.txt
merge-script 19f277711e
Merge bitcoin/bitcoin#26593: tracing: Only prepare tracepoint arguments when actually tracing
0de3e96e33 tracing: use bitcoind pid in bcc tracing examples (0xb10c)
411c6cfc6c tracing: only prepare tracepoint args if attached (0xb10c)
d524c1ec06 tracing: dedup TRACE macros & rename to TRACEPOINT (0xb10c)

Pull request description:

  Currently, if the tracepoints are compiled (e.g. in depends and release builds), we always prepare the tracepoint arguments regardless of the tracepoints being used or not. We made sure that the argument preparation is as cheap as possible, but we can almost completely eliminate any overhead for users not interested in the tracepoints (the vast majority), by gating the tracepoint argument preparation with an `if(something is attached to this tracepoint)`. To achieve this, we use the optional semaphore feature provided by SystemTap.

  The first commit simplifies and deduplicates our tracepoint macros from 13 TRACEx macros to a single TRACEPOINT macro. This makes them easier to use and also avoids more duplicate macro definitions in the second commit.

  The Linux tracing tools I'm aware of (bcc, bpftrace, libbpf, and systemtap) all support the semaphore gating feature. Thus, all existing tracepoints and their argument preparation is gated in the second commit. For details, please refer to the commit messages and the updated documentation in `doc/tracing.md`.

  Also adding unit tests that include all tracepoint macros to make sure there are no compiler problems with them (e.g. some varadiac extension not supported).

  Reviewers might want to check:
  - Do the tracepoints still work for you? Do the examples in `contrib/tracing/` run on your system (as bpftrace frequently breaks on every new version, please test master too if it should't work for you)? Do the CI interface tests still pass?
  - Is the new documentation clear?
  - The `TRACEPOINT_SEMAPHORE(event, context)` macros places global variables in our global namespace. Is this something we strictly want to avoid or maybe move to all `TRACEPOINT_SEMAPHORE`s to a separate .cpp file or even namespace? I like having the `TRACEPOINT_SEMAPHORE()` in same file as the `TRACEPOINT()`, but open for suggestion on alternative approaches.
  - Are newly added tracepoints in the unit tests visible when using `readelf -n build/src/test/test_bitcoin`? You can run the new unit tests with `./build/src/test/test_bitcoin --run_test=util_trace_tests* --log_level=all`.
  <details><summary>Two of the added unit tests demonstrate that we are only processing the tracepoint arguments when attached by having a test-failure condition in the tracepoint argument preparation. The following bpftrace script can be used to demonstrate that the tests do indeed fail when attached to the tracepoints.</summary>

  `fail_tests.bt`:

  ```c
  #!/usr/bin/env bpftrace

  usdt:./build/src/test/test_bitcoin:test:check_if_attached {
    printf("the 'check_if_attached' test should have failed\n");
  }

  usdt:./build/src/test/test_bitcoin:test:expensive_section {
    printf("the 'expensive_section' test should have failed\n");
  }
  ```

  Run the unit tests with `./build/src/test/test_bitcoin` and start `bpftrace fail_tests.bt -p $(pidof test_bitcoin)` in a separate terminal. The unit tests should fail with:

  ```
  Running 594 test cases...
  test/util_trace_tests.cpp(31): error: in "util_trace_tests/test_tracepoint_check_if_attached": check false has failed
  test/util_trace_tests.cpp(51): error: in "util_trace_tests/test_tracepoint_manual_tracepoint_active_check": check false has failed

  *** 2 failures are detected in the test module "Bitcoin Core Test Suite"
  ```

  </details>

  These links might provide more contextual information for reviewers:
  - [How SystemTap Userspace Probes Work by eklitzke](https://eklitzke.org/how-sytemtap-userspace-probes-work) (actually an example on Bitcoin Core; mentions that with semaphores "the overhead for an untraced process is effectively zero.")
  - [libbpf comment on USDT semaphore handling](1596a09b5d/src/usdt.c (L83-L92)) (can recommend the whole comment for background on how the tracepoints and tracing tools work together)
  - https://sourceware.org/systemtap/wiki/UserSpaceProbeImplementation#Semaphore_Handling

ACKs for top commit:
  willcl-ark:
    utACK 0de3e96e33
  laanwj:
    re-ACK 0de3e96e33
  jb55:
    utACK 0de3e96e33
  vasild:
    ACK 0de3e96e33

Tree-SHA512: 0e5e0dc5e0353beaf5c446e4be03d447e64228b1be71ee9972fde1d6fac3fac71a9d73c6ce4fa68975f87db2b2bf6eee2009921a2a145e24d83a475d007a559b
2024-11-11 10:33:28 +00:00

218 lines
5.7 KiB
CMake

# Copyright (c) 2023-present The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or https://opensource.org/license/mit/.
include(GenerateHeaders)
generate_header_from_json(data/base58_encode_decode.json)
generate_header_from_json(data/bip341_wallet_vectors.json)
generate_header_from_json(data/blockfilters.json)
generate_header_from_json(data/key_io_invalid.json)
generate_header_from_json(data/key_io_valid.json)
generate_header_from_json(data/script_tests.json)
generate_header_from_json(data/sighash.json)
generate_header_from_json(data/tx_invalid.json)
generate_header_from_json(data/tx_valid.json)
generate_header_from_raw(data/asmap.raw test::data)
# Do not use generator expressions in test sources because the
# SOURCES property is processed to gather test suite macros.
add_executable(test_bitcoin
main.cpp
$<TARGET_OBJECTS:bitcoin_consensus>
${CMAKE_CURRENT_BINARY_DIR}/data/asmap.raw.h
${CMAKE_CURRENT_BINARY_DIR}/data/base58_encode_decode.json.h
${CMAKE_CURRENT_BINARY_DIR}/data/bip341_wallet_vectors.json.h
${CMAKE_CURRENT_BINARY_DIR}/data/blockfilters.json.h
${CMAKE_CURRENT_BINARY_DIR}/data/key_io_invalid.json.h
${CMAKE_CURRENT_BINARY_DIR}/data/key_io_valid.json.h
${CMAKE_CURRENT_BINARY_DIR}/data/script_tests.json.h
${CMAKE_CURRENT_BINARY_DIR}/data/sighash.json.h
${CMAKE_CURRENT_BINARY_DIR}/data/tx_invalid.json.h
${CMAKE_CURRENT_BINARY_DIR}/data/tx_valid.json.h
addrman_tests.cpp
allocator_tests.cpp
amount_tests.cpp
argsman_tests.cpp
arith_uint256_tests.cpp
banman_tests.cpp
base32_tests.cpp
base58_tests.cpp
base64_tests.cpp
bech32_tests.cpp
bip32_tests.cpp
bip324_tests.cpp
blockchain_tests.cpp
blockencodings_tests.cpp
blockfilter_index_tests.cpp
blockfilter_tests.cpp
blockmanager_tests.cpp
bloom_tests.cpp
bswap_tests.cpp
checkqueue_tests.cpp
cluster_linearize_tests.cpp
coins_tests.cpp
coinscachepair_tests.cpp
coinstatsindex_tests.cpp
common_url_tests.cpp
compilerbug_tests.cpp
compress_tests.cpp
crypto_tests.cpp
cuckoocache_tests.cpp
dbwrapper_tests.cpp
denialofservice_tests.cpp
descriptor_tests.cpp
disconnected_transactions.cpp
feefrac_tests.cpp
flatfile_tests.cpp
fs_tests.cpp
getarg_tests.cpp
hash_tests.cpp
headers_sync_chainwork_tests.cpp
httpserver_tests.cpp
i2p_tests.cpp
interfaces_tests.cpp
key_io_tests.cpp
key_tests.cpp
logging_tests.cpp
mempool_tests.cpp
merkle_tests.cpp
merkleblock_tests.cpp
miner_tests.cpp
miniminer_tests.cpp
miniscript_tests.cpp
minisketch_tests.cpp
multisig_tests.cpp
net_peer_connection_tests.cpp
net_peer_eviction_tests.cpp
net_tests.cpp
netbase_tests.cpp
node_warnings_tests.cpp
orphanage_tests.cpp
peerman_tests.cpp
pmt_tests.cpp
policy_fee_tests.cpp
policyestimator_tests.cpp
pool_tests.cpp
pow_tests.cpp
prevector_tests.cpp
raii_event_tests.cpp
random_tests.cpp
rbf_tests.cpp
rest_tests.cpp
result_tests.cpp
reverselock_tests.cpp
rpc_tests.cpp
sanity_tests.cpp
scheduler_tests.cpp
script_p2sh_tests.cpp
script_parse_tests.cpp
script_segwit_tests.cpp
script_standard_tests.cpp
script_tests.cpp
scriptnum_tests.cpp
serfloat_tests.cpp
serialize_tests.cpp
settings_tests.cpp
sighash_tests.cpp
sigopcount_tests.cpp
skiplist_tests.cpp
sock_tests.cpp
span_tests.cpp
streams_tests.cpp
sync_tests.cpp
system_tests.cpp
timeoffsets_tests.cpp
torcontrol_tests.cpp
transaction_tests.cpp
translation_tests.cpp
txdownload_tests.cpp
txindex_tests.cpp
txpackage_tests.cpp
txreconciliation_tests.cpp
txrequest_tests.cpp
txvalidation_tests.cpp
txvalidationcache_tests.cpp
uint256_tests.cpp
util_string_tests.cpp
util_tests.cpp
util_threadnames_tests.cpp
util_trace_tests.cpp
validation_block_tests.cpp
validation_chainstate_tests.cpp
validation_chainstatemanager_tests.cpp
validation_flush_tests.cpp
validation_tests.cpp
validationinterface_tests.cpp
versionbits_tests.cpp
)
target_link_libraries(test_bitcoin
core_interface
test_util
bitcoin_cli
bitcoin_node
minisketch
secp256k1
Boost::headers
$<TARGET_NAME_IF_EXISTS:libevent::libevent>
)
if(ENABLE_WALLET)
add_subdirectory(${PROJECT_SOURCE_DIR}/src/wallet/test wallet)
endif()
if(WITH_MULTIPROCESS)
target_link_libraries(bitcoin_ipc_test
PRIVATE
core_interface
univalue
)
target_sources(test_bitcoin
PRIVATE
ipc_tests.cpp
)
target_link_libraries(test_bitcoin bitcoin_ipc_test bitcoin_ipc)
endif()
function(add_boost_test source_file)
if(NOT EXISTS ${source_file})
return()
endif()
file(READ "${source_file}" source_file_content)
string(REGEX
MATCH "(BOOST_FIXTURE_TEST_SUITE|BOOST_AUTO_TEST_SUITE)\\(([A-Za-z0-9_]+)"
test_suite_macro "${source_file_content}"
)
string(REGEX
REPLACE "(BOOST_FIXTURE_TEST_SUITE|BOOST_AUTO_TEST_SUITE)\\(" ""
test_suite_name "${test_suite_macro}"
)
if(test_suite_name)
add_test(NAME ${test_suite_name}
COMMAND test_bitcoin --run_test=${test_suite_name} --catch_system_error=no --log_level=test_suite -- DEBUG_LOG_OUT
)
set_property(TEST ${test_suite_name} PROPERTY
SKIP_REGULAR_EXPRESSION "no test cases matching filter"
)
endif()
endfunction()
function(add_all_test_targets)
get_target_property(test_source_dir test_bitcoin SOURCE_DIR)
get_target_property(test_sources test_bitcoin SOURCES)
foreach(test_source ${test_sources})
cmake_path(IS_RELATIVE test_source result)
if(result)
cmake_path(APPEND test_source_dir ${test_source} OUTPUT_VARIABLE test_source)
endif()
add_boost_test(${test_source})
endforeach()
endfunction()
add_all_test_targets()
install(TARGETS test_bitcoin
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)