Skip to content

Commit ae41208

Browse files
committed
Add more metrics for account/storage touched/changed
1 parent 85af75f commit ae41208

File tree

9 files changed

+125
-11
lines changed

9 files changed

+125
-11
lines changed

category/execution/ethereum/execute_block.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ std::vector<std::vector<std::optional<Address>>> recover_authorities(
178178

179179
template <Traits traits>
180180
void execute_block_header(
181-
Chain const &chain, BlockState &block_state, BlockHeader const &header)
181+
Chain const &chain, BlockState &block_state, BlockHeader const &header,
182+
BlockMetrics &block_metrics)
182183
{
183184
State state{block_state, Incarnation{header.number, 0}};
184185

@@ -204,7 +205,7 @@ void execute_block_header(
204205
}
205206

206207
MONAD_ASSERT(block_state.can_merge(state));
207-
block_state.merge(state);
208+
block_state.merge(state, block_metrics);
208209
record_account_access_events(MONAD_ACCT_ACCESS_BLOCK_PROLOGUE, state);
209210
}
210211

@@ -341,7 +342,8 @@ Result<std::vector<Receipt>> execute_block(
341342
MONAD_ASSERT(senders.size() == call_tracers.size());
342343
MONAD_ASSERT(senders.size() == state_tracers.size());
343344

344-
execute_block_header<traits>(chain, block_state, block.header);
345+
execute_block_header<traits>(
346+
chain, block_state, block.header, block_metrics);
345347

346348
BOOST_OUTCOME_TRY(
347349
auto const retvals,
@@ -373,7 +375,7 @@ Result<std::vector<Receipt>> execute_block(
373375
}
374376

375377
MONAD_ASSERT(block_state.can_merge(state));
376-
block_state.merge(state);
378+
block_state.merge(state, block_metrics);
377379
record_account_access_events(MONAD_ACCT_ACCESS_BLOCK_EPILOGUE, state);
378380

379381
return retvals;

category/execution/ethereum/execute_block.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ struct Block;
4141
struct Chain;
4242

4343
template <Traits traits>
44-
void execute_block_header(Chain const &, BlockState &, BlockHeader const &);
44+
void execute_block_header(
45+
Chain const &, BlockState &, BlockHeader const &, BlockMetrics &);
4546

4647
template <Traits traits>
4748
Result<std::vector<Receipt>> execute_block_transactions(

category/execution/ethereum/execute_transaction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ Result<Receipt> ExecuteTransaction<traits>::operator()()
417417
auto const receipt = execute_final(state, result.value());
418418
call_tracer_.on_finish(receipt.gas_used);
419419
trace::run_tracer<traits>(state_tracer_, state);
420-
block_state_.merge(state);
420+
block_state_.merge(state, block_metrics_);
421421
record_txn_output_events(
422422
static_cast<uint32_t>(this->i_),
423423
receipt,
@@ -443,7 +443,7 @@ Result<Receipt> ExecuteTransaction<traits>::operator()()
443443
auto const receipt = execute_final(state, result.value());
444444
call_tracer_.on_finish(receipt.gas_used);
445445
trace::run_tracer<traits>(state_tracer_, state);
446-
block_state_.merge(state);
446+
block_state_.merge(state, block_metrics_);
447447
record_txn_output_events(
448448
static_cast<uint32_t>(this->i_),
449449
receipt,

category/execution/ethereum/metrics/block_metrics.hpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ MONAD_NAMESPACE_BEGIN
2424
class BlockMetrics
2525
{
2626
uint32_t n_retries_{0};
27+
uint32_t account_touched_{0};
28+
uint32_t account_changed_{0};
29+
uint32_t storage_touched_{0};
30+
uint32_t storage_changed_{0};
2731
std::chrono::microseconds tx_exec_time_{1};
2832

2933
public:
@@ -37,6 +41,26 @@ class BlockMetrics
3741
return n_retries_;
3842
}
3943

44+
uint32_t num_account_touched() const
45+
{
46+
return account_touched_;
47+
}
48+
49+
uint32_t num_account_changed() const
50+
{
51+
return account_changed_;
52+
}
53+
54+
uint32_t num_storage_touched() const
55+
{
56+
return storage_touched_;
57+
}
58+
59+
uint32_t num_storage_changed() const
60+
{
61+
return storage_changed_;
62+
}
63+
4064
void set_tx_exec_time(std::chrono::microseconds const exec_time)
4165
{
4266
tx_exec_time_ = exec_time;
@@ -46,6 +70,26 @@ class BlockMetrics
4670
{
4771
return tx_exec_time_;
4872
}
73+
74+
void add_account_touched(uint32_t const value)
75+
{
76+
account_touched_ += value;
77+
}
78+
79+
void add_account_changed(uint32_t const value)
80+
{
81+
account_changed_ += value;
82+
}
83+
84+
void add_storage_touched(uint32_t const value)
85+
{
86+
storage_touched_ += value;
87+
}
88+
89+
void add_storage_changed(uint32_t const value)
90+
{
91+
storage_changed_ += value;
92+
}
4993
};
5094

5195
MONAD_NAMESPACE_END

category/execution/ethereum/state2/block_state.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,65 @@ void BlockState::merge(State const &state)
236236
}
237237
}
238238

239+
void BlockState::merge(State const &state, BlockMetrics &block_metrics)
240+
{
241+
ankerl::unordered_dense::segmented_set<bytes32_t> code_hashes;
242+
243+
auto const &current = state.current();
244+
for (auto const &[address, stack] : current) {
245+
MONAD_ASSERT(stack.size() == 1);
246+
MONAD_ASSERT(stack.version() == 0);
247+
auto const &account_state = stack.recent();
248+
auto const &account = account_state.account_;
249+
if (account.has_value()) {
250+
code_hashes.insert(account.value().code_hash);
251+
}
252+
}
253+
254+
auto const &code = state.code();
255+
for (auto const &code_hash : code_hashes) {
256+
auto const it = code.find(code_hash);
257+
if (it == code.end()) {
258+
continue;
259+
}
260+
code_.emplace(code_hash, it->second->intercode()); // TODO try_emplace
261+
}
262+
263+
MONAD_ASSERT(state_);
264+
for (auto const &[address, stack] : current) {
265+
auto const &account_state = stack.recent();
266+
auto const &account = account_state.account_;
267+
auto const &storage = account_state.storage_;
268+
StateDeltas::accessor it{};
269+
MONAD_ASSERT(state_->find(it, address));
270+
it->second.account.second = account;
271+
if (account.has_value()) {
272+
block_metrics.add_account_touched(1);
273+
if (it->second.account.first != account) {
274+
block_metrics.add_account_changed(1);
275+
}
276+
for (auto const &[key, value] : storage) {
277+
StorageDeltas::accessor it2{};
278+
block_metrics.add_storage_touched(1);
279+
if (it->second.storage.find(it2, key)) {
280+
it2->second.second = value;
281+
if (it2->second.first != value) {
282+
block_metrics.add_storage_changed(1);
283+
}
284+
}
285+
else {
286+
it->second.storage.emplace(
287+
key, std::make_pair(bytes32_t{}, value));
288+
block_metrics.add_storage_changed(1);
289+
}
290+
}
291+
}
292+
else {
293+
it->second.storage.clear();
294+
}
295+
}
296+
}
297+
239298
void BlockState::commit(
240299
bytes32_t const &block_id, BlockHeader const &header,
241300
std::vector<Receipt> const &receipts,

category/execution/ethereum/state2/block_state.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <category/execution/ethereum/core/receipt.hpp>
2222
#include <category/execution/ethereum/core/transaction.hpp>
2323
#include <category/execution/ethereum/db/db.hpp>
24+
#include <category/execution/ethereum/metrics/block_metrics.hpp>
2425
#include <category/execution/ethereum/state2/state_deltas.hpp>
2526
#include <category/execution/ethereum/trace/call_tracer.hpp>
2627
#include <category/execution/ethereum/types/incarnation.hpp>
@@ -58,6 +59,8 @@ class BlockState final
5859

5960
void merge(State const &);
6061

62+
void merge(State const &, BlockMetrics &);
63+
6164
void commit(
6265
bytes32_t const &block_id, BlockHeader const &,
6366
std::vector<Receipt> const & = {},

category/rpc/monad_executor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,8 @@ namespace
427427
chain_context.authorities.data(), transactions_size};
428428

429429
// Execute block header
430-
execute_block_header<traits>(chain, block_state, header);
431430
BlockMetrics metrics{};
431+
execute_block_header<traits>(chain, block_state, header, metrics);
432432

433433
// Prepare state tracers and auxiliary noop call tracers.
434434
std::vector<std::unique_ptr<trace::StateTracer>> state_tracers{};

cmd/monad/runloop_ethereum.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Result<void> process_ethereum_block(
8585
fiber::PriorityPool &priority_pool, Block &block, bytes32_t const &block_id,
8686
bytes32_t const &parent_block_id, bool const enable_tracing)
8787
{
88-
[[maybe_unused]] auto const block_start = std::chrono::system_clock::now();
88+
[[maybe_unused]] auto const block_start = std::chrono::steady_clock::now();
8989
auto const block_begin = std::chrono::steady_clock::now();
9090

9191
// Block input validation
@@ -189,7 +189,8 @@ Result<void> process_ethereum_block(
189189
"__exec_block,bl={:8},ts={}"
190190
",tx={:5},rt={:4},rtp={:5.2f}%"
191191
",sr={:>7},txe={:>8},cmt={:>8},tot={:>8},tpse={:5},tps={:5}"
192-
",gas={:9},gpse={:4},gps={:3}{}{}{}",
192+
",gas={:9},gpse={:4},gps={:3},at={},am={},st={},sm={}"
193+
"{}{}{}",
193194
block.header.number,
194195
std::chrono::duration_cast<std::chrono::milliseconds>(
195196
block_start.time_since_epoch())
@@ -210,6 +211,10 @@ Result<void> process_ethereum_block(
210211
output_header.gas_used /
211212
(uint64_t)std::max(1L, block_metrics.tx_exec_time().count()),
212213
output_header.gas_used / (uint64_t)std::max(1L, block_time.count()),
214+
block_metrics.num_account_touched(),
215+
block_metrics.num_account_changed(),
216+
block_metrics.num_storage_touched(),
217+
block_metrics.num_storage_changed(),
213218
db.print_stats(),
214219
vm.print_and_reset_block_counts(),
215220
vm.print_compiler_stats());

cmd/monad/runloop_monad.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ Result<BlockExecOutput> propose_block(
155155
vm::VM &vm, fiber::PriorityPool &priority_pool, bool const is_first_block,
156156
bool const enable_tracing, BlockCache &block_cache)
157157
{
158-
[[maybe_unused]] auto const block_start = std::chrono::system_clock::now();
158+
[[maybe_unused]] auto const block_start = std::chrono::steady_clock::now();
159159
auto const block_begin = std::chrono::steady_clock::now();
160160
auto const &block_hash_buffer =
161161
block_hash_chain.find_chain(consensus_header.parent_id());

0 commit comments

Comments
 (0)