Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tracing: add utxocache tracepoints #1

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 0 additions & 33 deletions contrib/tracing/log_utxo.bt

This file was deleted.

42 changes: 24 additions & 18 deletions contrib/tracing/log_utxo_coins.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
struct coin_data
{
u32 height;
u32 index;
char hash[MAX_HASH_SIZE];
long value;
u32 isCoinBase;
0xB10C marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be bool too

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to isCoinBase?

Cause, index refers to the COutPoint::n here

class COutPoint
{
public:
uint256 hash;
uint32_t n;

For the isCoinBase which refers to Coin::fCoinBase it is of unsigned int type.

bitcoin/src/coins.h

Lines 30 to 37 in 7be143a

class Coin
{
public:
//! unspent transaction output
CTxOut out;
//! whether containing transaction was a coinbase
unsigned int fCoinBase : 1;

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, sorry if that was unclear. For me the comment is on the isCoinbase line.

image

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use Coin::IsCoinBase() which returns a bool

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expand All @@ -28,51 +29,55 @@
int trace_add_coin(struct pt_regs *ctx) {
struct coin_data data = {};
bpf_usdt_readarg_p(1, ctx, &data.hash, MAX_HASH_SIZE);
bpf_usdt_readarg(2, ctx, &data.height);
bpf_usdt_readarg(3, ctx, &data.value);
bpf_usdt_readarg(4, ctx, &data.isCoinBase);
bpf_usdt_readarg(5, ctx, &data.usage);
bpf_usdt_readarg(2, ctx, &data.index);
bpf_usdt_readarg(3, ctx, &data.height);
bpf_usdt_readarg(4, ctx, &data.value);
bpf_usdt_readarg(5, ctx, &data.isCoinBase);
bpf_usdt_readarg(6, ctx, &data.usage);
coins_added.perf_submit(ctx, &data, sizeof(data));
return 0;
}

int trace_spend_coin(struct pt_regs *ctx) {
struct coin_data data = {};
bpf_usdt_readarg_p(1, ctx, &data.hash, MAX_HASH_SIZE);
bpf_usdt_readarg(2, ctx, &data.height);
bpf_usdt_readarg(3, ctx, &data.value);
bpf_usdt_readarg(4, ctx, &data.isCoinBase);
bpf_usdt_readarg(5, ctx, &data.usage);
bpf_usdt_readarg(2, ctx, &data.index);
bpf_usdt_readarg(3, ctx, &data.height);
bpf_usdt_readarg(4, ctx, &data.value);
bpf_usdt_readarg(5, ctx, &data.isCoinBase);
bpf_usdt_readarg(6, ctx, &data.usage);
coins_spent.perf_submit(ctx, &data, sizeof(data));
return 0;
}

int trace_uncached_coin(struct pt_regs *ctx) {
struct coin_data data = {};
bpf_usdt_readarg_p(1, ctx, &data.hash, MAX_HASH_SIZE);
bpf_usdt_readarg(2, ctx, &data.height);
bpf_usdt_readarg(3, ctx, &data.value);
bpf_usdt_readarg(4, ctx, &data.isCoinBase);
bpf_usdt_readarg(5, ctx, &data.usage);
bpf_usdt_readarg(2, ctx, &data.index);
bpf_usdt_readarg(3, ctx, &data.height);
bpf_usdt_readarg(4, ctx, &data.value);
bpf_usdt_readarg(5, ctx, &data.isCoinBase);
bpf_usdt_readarg(6, ctx, &data.usage);
coins_uncached.perf_submit(ctx, &data, sizeof(data));
return 0;
}
"""


def print_event(event, action):
print("%-68s %-10s %-10d %-15d %-10s %-10d" %
(event.hash.decode('ASCII'), action, event.height, event.value, "YES" if event.isCoinBase else "NO", event.usage))
# print(event.hash.decode('ASCII'), action, event.height, event.value, "YES" if event.isCoinBase else "NO", event.usage)
arnabsen1729 marked this conversation as resolved.
Show resolved Hide resolved
print("%-70s %-10s %-10d %-15d %-10s %-10d" %
(f"{event.hash.decode('ASCII')}:{str(event.index)}", action, event.height, event.value, "YES" if event.isCoinBase else "NO", event.usage))


def main(bitcoind_path):
bitcoind_with_usdts = USDT(path=str(bitcoind_path))
bitcoind_with_usdts.enable_probe(
probe="coins_added", fn_name="trace_add_coin")
probe="add", fn_name="trace_add_coin")
bitcoind_with_usdts.enable_probe(
probe="coins_spent", fn_name="trace_spend_coin")
probe="spent", fn_name="trace_spend_coin")
bitcoind_with_usdts.enable_probe(
probe="coins_uncached", fn_name="trace_uncached_coin")
probe="uncache", fn_name="trace_uncached_coin")

b = BPF(text=program, usdt_contexts=[bitcoind_with_usdts])

Expand All @@ -93,7 +98,8 @@ def handle_coins_uncached(_, data, size):
b["coins_uncached"].open_perf_buffer(handle_coins_uncached)

print("Logging Add, Spend and Uncache in the UTXO set. Ctrl-C to end...")
print("%-68s %-10s %-10s %-15s %-10s %s"%("txHash","Action", "Height", "Satoshis", "Coin Base", "Usage" ))
print("%-70s %-10s %-10s %-15s %-10s %s" %
("txHash", "Action", "Height", "Satoshis", "Coin Base", "Usage"))
arnabsen1729 marked this conversation as resolved.
Show resolved Hide resolved
while True:
try:
b.perf_buffer_poll()
Expand Down
15 changes: 9 additions & 6 deletions src/coins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,13 @@ void CCoinsViewCache::AddCoin(const COutPoint &outpoint, Coin&& coin, bool possi
it->second.coin = std::move(coin);
it->second.flags |= CCoinsCacheEntry::DIRTY | (fresh ? CCoinsCacheEntry::FRESH : 0);
cachedCoinsUsage += it->second.coin.DynamicMemoryUsage();
TRACE5(utxo, coins_added,
TRACE6(utxocache, add,
outpoint.hash.ToString().c_str(),
(uint32_t)outpoint.n,
(uint32_t)coin.nHeight,
(int64_t)coin.out.nValue,
(uint)coin.fCoinBase,
arnabsen1729 marked this conversation as resolved.
Show resolved Hide resolved
cachedCoinsUsage
(uint64_t)cachedCoinsUsage
);
}

Expand Down Expand Up @@ -129,12 +130,13 @@ bool CCoinsViewCache::SpendCoin(const COutPoint &outpoint, Coin* moveout) {
if (it == cacheCoins.end()) return false;
cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();

TRACE5(utxo, coins_spent,
TRACE6(utxocache, spent,
outpoint.hash.ToString().c_str(),
(uint32_t)outpoint.n,
(uint32_t)it->second.coin.nHeight,
(int64_t)it->second.coin.out.nValue,
(uint)it->second.coin.fCoinBase,
arnabsen1729 marked this conversation as resolved.
Show resolved Hide resolved
cachedCoinsUsage
(uint64_t)cachedCoinsUsage
);

if (moveout) {
Expand Down Expand Up @@ -250,12 +252,13 @@ void CCoinsViewCache::Uncache(const COutPoint& hash)
if (it != cacheCoins.end() && it->second.flags == 0) {
cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();

TRACE5(utxo, coins_uncached,
TRACE6(utxocache, uncache,
hash.hash.ToString().c_str(),
(uint32_t)hash.n,
(uint32_t)it->second.coin.nHeight,
(int64_t)it->second.coin.out.nValue,
(uint)it->second.coin.fCoinBase,
arnabsen1729 marked this conversation as resolved.
Show resolved Hide resolved
cachedCoinsUsage
(uint64_t)cachedCoinsUsage
);

cacheCoins.erase(it);
Expand Down