Skip to content

Commit d524c1e

Browse files
0xB10Cmartinus
andcommitted
tracing: dedup TRACE macros & rename to TRACEPOINT
This deduplicates the TRACEx macros by using systemtaps STAP_PROBEV[0] variadic macro instead of the DTrace compability DTRACE_PROBE[1] macros. Bitcoin Core never had DTrace tracepoints, so we don't need to use the drop-in replacement for it. As noted in pr25541[2], these macros aren't compatibile with DTrace on macOS anyway. This also renames the TRACEx macro to TRACEPOINT to clarify what the macro does: inserting a tracepoint vs tracing (logging) something. [0]: https://sourceware.org/git/?p=systemtap.git;a=blob;f=includes/sys/sdt.h;h=24d5e01c37805e55c36f7202e5d4e821b85167a1;hb=ecab2afea46099b4e7dfd551462689224afdbe3a#l407 [1]: https://sourceware.org/git/?p=systemtap.git;a=blob;f=includes/sys/sdt.h;h=24d5e01c37805e55c36f7202e5d4e821b85167a1;hb=ecab2afea46099b4e7dfd551462689224afdbe3a#l490 [2]: https://github.com/bitcoin/bitcoin/pull/25541/files#diff-553886c5f808e01e3452c7b21e879cc355da388ef7680bf310f6acb926d43266R30-R31 Co-authored-by: Martin Leitner-Ankerl <[email protected]>
1 parent 1c7ca6e commit d524c1e

File tree

11 files changed

+62
-70
lines changed

11 files changed

+62
-70
lines changed

cmake/module/FindUSDT.cmake

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,16 @@ if(USDT_INCLUDE_DIR)
3636
include(CheckCXXSourceCompiles)
3737
set(CMAKE_REQUIRED_INCLUDES ${USDT_INCLUDE_DIR})
3838
check_cxx_source_compiles("
39+
// Setting SDT_USE_VARIADIC lets systemtap (sys/sdt.h) know that we want to use
40+
// the optional variadic macros to define tracepoints.
41+
#define SDT_USE_VARIADIC 1
3942
#include <sys/sdt.h>
4043
4144
int main()
4245
{
43-
DTRACE_PROBE(context, event);
46+
STAP_PROBEV(context, event);
4447
int a, b, c, d, e, f, g;
45-
DTRACE_PROBE7(context, event, a, b, c, d, e, f, g);
48+
STAP_PROBEV(context, event, a, b, c, d, e, f, g);
4649
}
4750
" HAVE_USDT_H
4851
)

doc/tracing.md

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -265,35 +265,19 @@ Arguments passed:
265265

266266
## Adding tracepoints to Bitcoin Core
267267

268-
To add a new tracepoint, `#include <util/trace.h>` in the compilation unit where
269-
the tracepoint is inserted. Use one of the `TRACEx` macros listed below
270-
depending on the number of arguments passed to the tracepoint. Up to 12
271-
arguments can be provided. The `context` and `event` specify the names by which
272-
the tracepoint is referred to. Please use `snake_case` and try to make sure that
273-
the tracepoint names make sense even without detailed knowledge of the
274-
implementation details. Do not forget to update the tracepoint list in this
268+
Use the `TRACEPOINT` macro to add a new tracepoint. If not yet included, include
269+
`util/trace.h` (defines the tracepoint macros) with `#include <util/trace.h>`.
270+
Each tracepoint needs a `context` and an `event`. Please use `snake_case` and
271+
try to make sure that the tracepoint names make sense even without detailed
272+
knowledge of the implementation details. You can pass zero to twelve arguments
273+
to the tracepoint. Do not forget to update the tracepoint list in this
275274
document.
276275

277-
```c
278-
#define TRACE(context, event)
279-
#define TRACE1(context, event, a)
280-
#define TRACE2(context, event, a, b)
281-
#define TRACE3(context, event, a, b, c)
282-
#define TRACE4(context, event, a, b, c, d)
283-
#define TRACE5(context, event, a, b, c, d, e)
284-
#define TRACE6(context, event, a, b, c, d, e, f)
285-
#define TRACE7(context, event, a, b, c, d, e, f, g)
286-
#define TRACE8(context, event, a, b, c, d, e, f, g, h)
287-
#define TRACE9(context, event, a, b, c, d, e, f, g, h, i)
288-
#define TRACE10(context, event, a, b, c, d, e, f, g, h, i, j)
289-
#define TRACE11(context, event, a, b, c, d, e, f, g, h, i, j, k)
290-
#define TRACE12(context, event, a, b, c, d, e, f, g, h, i, j, k, l)
291-
```
292-
293-
For example:
276+
For example, a tracepoint in the `net` context for the event `inbound_message`
277+
and six arguments:
294278

295279
```C++
296-
TRACE6(net, inbound_message,
280+
TRACEPOINT(net, inbound_message,
297281
pnode->GetId(),
298282
pnode->m_addr_name.c_str(),
299283
pnode->ConnectionTypeAsString().c_str(),
@@ -347,7 +331,7 @@ first six argument fields. Binary data can be placed in later arguments. The BCC
347331
supports reading from all 12 arguments.
348332

349333
#### Strings as C-style String
350-
Generally, strings should be passed into the `TRACEx` macros as pointers to
334+
Generally, strings should be passed into the `TRACEPOINT` macros as pointers to
351335
C-style strings (a null-terminated sequence of characters). For C++
352336
`std::strings`, [`c_str()`] can be used. It's recommended to document the
353337
maximum expected string size if known.

src/coins.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ void CCoinsViewCache::AddCoin(const COutPoint &outpoint, Coin&& coin, bool possi
9797
it->second.coin = std::move(coin);
9898
it->second.AddFlags(CCoinsCacheEntry::DIRTY | (fresh ? CCoinsCacheEntry::FRESH : 0), *it, m_sentinel);
9999
cachedCoinsUsage += it->second.coin.DynamicMemoryUsage();
100-
TRACE5(utxocache, add,
100+
TRACEPOINT(utxocache, add,
101101
outpoint.hash.data(),
102102
(uint32_t)outpoint.n,
103103
(uint32_t)it->second.coin.nHeight,
@@ -131,7 +131,7 @@ bool CCoinsViewCache::SpendCoin(const COutPoint &outpoint, Coin* moveout) {
131131
CCoinsMap::iterator it = FetchCoin(outpoint);
132132
if (it == cacheCoins.end()) return false;
133133
cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
134-
TRACE5(utxocache, spent,
134+
TRACEPOINT(utxocache, spent,
135135
outpoint.hash.data(),
136136
(uint32_t)outpoint.n,
137137
(uint32_t)it->second.coin.nHeight,
@@ -278,7 +278,7 @@ void CCoinsViewCache::Uncache(const COutPoint& hash)
278278
CCoinsMap::iterator it = cacheCoins.find(hash);
279279
if (it != cacheCoins.end() && !it->second.IsDirty() && !it->second.IsFresh()) {
280280
cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
281-
TRACE5(utxocache, uncache,
281+
TRACEPOINT(utxocache, uncache,
282282
hash.hash.data(),
283283
(uint32_t)hash.n,
284284
(uint32_t)it->second.coin.nHeight,

src/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3809,7 +3809,7 @@ void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
38093809
CaptureMessage(pnode->addr, msg.m_type, msg.data, /*is_incoming=*/false);
38103810
}
38113811

3812-
TRACE6(net, outbound_message,
3812+
TRACEPOINT(net, outbound_message,
38133813
pnode->GetId(),
38143814
pnode->m_addr_name.c_str(),
38153815
pnode->ConnectionTypeAsString().c_str(),

src/net_processing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5421,7 +5421,7 @@ bool PeerManagerImpl::ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt
54215421
CNetMessage& msg{poll_result->first};
54225422
bool fMoreWork = poll_result->second;
54235423

5424-
TRACE6(net, inbound_message,
5424+
TRACEPOINT(net, inbound_message,
54255425
pfrom->GetId(),
54265426
pfrom->m_addr_name.c_str(),
54275427
pfrom->ConnectionTypeAsString().c_str(),

src/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ add_executable(test_bitcoin
135135
util_string_tests.cpp
136136
util_tests.cpp
137137
util_threadnames_tests.cpp
138+
util_trace_tests.cpp
138139
validation_block_tests.cpp
139140
validation_chainstate_tests.cpp
140141
validation_chainstatemanager_tests.cpp

src/test/util_trace_tests.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) 2023 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <test/util/setup_common.h>
6+
7+
#include <boost/test/unit_test.hpp>
8+
9+
#include <util/trace.h>
10+
11+
BOOST_FIXTURE_TEST_SUITE(util_trace_tests, BasicTestingSetup)
12+
13+
// Tests the TRACEPOINT macro and that we can compile tracepoints with 0 to 12 args.
14+
BOOST_AUTO_TEST_CASE(test_tracepoints)
15+
{
16+
TRACEPOINT(test, zero_args);
17+
TRACEPOINT(test, one_arg, 1);
18+
TRACEPOINT(test, six_args, 1, 2, 3, 4, 5, 6);
19+
TRACEPOINT(test, twelve_args, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
20+
BOOST_CHECK(true);
21+
}
22+
23+
BOOST_AUTO_TEST_SUITE_END()

src/txmempool.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ void CTxMemPool::addUnchecked(const CTxMemPoolEntry &entry, setEntries &setAnces
477477
txns_randomized.emplace_back(newit->GetSharedTx());
478478
newit->idx_randomized = txns_randomized.size() - 1;
479479

480-
TRACE3(mempool, added,
480+
TRACEPOINT(mempool, added,
481481
entry.GetTx().GetHash().data(),
482482
entry.GetTxSize(),
483483
entry.GetFee()
@@ -497,7 +497,7 @@ void CTxMemPool::removeUnchecked(txiter it, MemPoolRemovalReason reason)
497497
// notification.
498498
m_opts.signals->TransactionRemovedFromMempool(it->GetSharedTx(), reason, mempool_sequence);
499499
}
500-
TRACE5(mempool, removed,
500+
TRACEPOINT(mempool, removed,
501501
it->GetTx().GetHash().data(),
502502
RemovalReasonToString(reason).c_str(),
503503
it->GetTxSize(),

src/util/trace.h

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,18 @@
99

1010
#ifdef ENABLE_TRACING
1111

12-
#include <sys/sdt.h>
12+
// Setting SDT_USE_VARIADIC lets systemtap (sys/sdt.h) know that we want to use
13+
// the optional variadic macros to define tracepoints.
14+
#define SDT_USE_VARIADIC 1
1315

14-
#define TRACE(context, event) DTRACE_PROBE(context, event)
15-
#define TRACE1(context, event, a) DTRACE_PROBE1(context, event, a)
16-
#define TRACE2(context, event, a, b) DTRACE_PROBE2(context, event, a, b)
17-
#define TRACE3(context, event, a, b, c) DTRACE_PROBE3(context, event, a, b, c)
18-
#define TRACE4(context, event, a, b, c, d) DTRACE_PROBE4(context, event, a, b, c, d)
19-
#define TRACE5(context, event, a, b, c, d, e) DTRACE_PROBE5(context, event, a, b, c, d, e)
20-
#define TRACE6(context, event, a, b, c, d, e, f) DTRACE_PROBE6(context, event, a, b, c, d, e, f)
21-
#define TRACE7(context, event, a, b, c, d, e, f, g) DTRACE_PROBE7(context, event, a, b, c, d, e, f, g)
22-
#define TRACE8(context, event, a, b, c, d, e, f, g, h) DTRACE_PROBE8(context, event, a, b, c, d, e, f, g, h)
23-
#define TRACE9(context, event, a, b, c, d, e, f, g, h, i) DTRACE_PROBE9(context, event, a, b, c, d, e, f, g, h, i)
24-
#define TRACE10(context, event, a, b, c, d, e, f, g, h, i, j) DTRACE_PROBE10(context, event, a, b, c, d, e, f, g, h, i, j)
25-
#define TRACE11(context, event, a, b, c, d, e, f, g, h, i, j, k) DTRACE_PROBE11(context, event, a, b, c, d, e, f, g, h, i, j, k)
26-
#define TRACE12(context, event, a, b, c, d, e, f, g, h, i, j, k, l) DTRACE_PROBE12(context, event, a, b, c, d, e, f, g, h, i, j, k, l)
16+
#include <sys/sdt.h>
2717

18+
// A USDT tracepoint with zero to twelve arguments.
19+
#define TRACEPOINT(context, ...) \
20+
STAP_PROBEV(context, __VA_ARGS__);
2821
#else
2922

30-
#define TRACE(context, event)
31-
#define TRACE1(context, event, a)
32-
#define TRACE2(context, event, a, b)
33-
#define TRACE3(context, event, a, b, c)
34-
#define TRACE4(context, event, a, b, c, d)
35-
#define TRACE5(context, event, a, b, c, d, e)
36-
#define TRACE6(context, event, a, b, c, d, e, f)
37-
#define TRACE7(context, event, a, b, c, d, e, f, g)
38-
#define TRACE8(context, event, a, b, c, d, e, f, g, h)
39-
#define TRACE9(context, event, a, b, c, d, e, f, g, h, i)
40-
#define TRACE10(context, event, a, b, c, d, e, f, g, h, i, j)
41-
#define TRACE11(context, event, a, b, c, d, e, f, g, h, i, j, k)
42-
#define TRACE12(context, event, a, b, c, d, e, f, g, h, i, j, k, l)
23+
#define TRACEPOINT(context, ...)
4324

4425
#endif
4526

src/validation.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,7 @@ bool MemPoolAccept::Finalize(const ATMPArgs& args, Workspace& ws)
13041304
tx.GetWitnessHash().ToString(),
13051305
entry->GetFee(),
13061306
entry->GetTxSize());
1307-
TRACE7(mempool, replaced,
1307+
TRACEPOINT(mempool, replaced,
13081308
it->GetTx().GetHash().data(),
13091309
it->GetTxSize(),
13101310
it->GetFee(),
@@ -1866,7 +1866,7 @@ MempoolAcceptResult AcceptToMemoryPool(Chainstate& active_chainstate, const CTra
18661866

18671867
for (const COutPoint& hashTx : coins_to_uncache)
18681868
active_chainstate.CoinsTip().Uncache(hashTx);
1869-
TRACE2(mempool, rejected,
1869+
TRACEPOINT(mempool, rejected,
18701870
tx->GetHash().data(),
18711871
result.m_state.GetRejectReason().c_str()
18721872
);
@@ -2736,7 +2736,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
27362736
Ticks<SecondsDouble>(m_chainman.time_index),
27372737
Ticks<MillisecondsDouble>(m_chainman.time_index) / m_chainman.num_blocks_total);
27382738

2739-
TRACE6(validation, block_connected,
2739+
TRACEPOINT(validation, block_connected,
27402740
block_hash.data(),
27412741
pindex->nHeight,
27422742
block.vtx.size(),
@@ -2912,7 +2912,7 @@ bool Chainstate::FlushStateToDisk(
29122912
}
29132913
m_last_flush = nNow;
29142914
full_flush_completed = true;
2915-
TRACE5(utxocache, flush,
2915+
TRACEPOINT(utxocache, flush,
29162916
int64_t{Ticks<std::chrono::microseconds>(SteadyClock::now() - nNow)},
29172917
(uint32_t)mode,
29182918
(uint64_t)coins_count,

0 commit comments

Comments
 (0)