Skip to content

Commit ce58857

Browse files
committed
Add more metrics for account/storage touched/changed
1 parent 1eb7356 commit ce58857

File tree

5 files changed

+59
-5
lines changed

5 files changed

+59
-5
lines changed

category/execution/ethereum/db/db_cache.hpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ class DbCache final : public Db
6969
StorageCache storage_{10'000'000};
7070
Proposals proposals_;
7171

72+
uint64_t num_account_touched_{0};
73+
uint64_t num_account_changed_{0};
74+
uint64_t num_storage_touched_{0};
75+
uint64_t num_storage_changed_{0};
76+
7277
public:
7378
DbCache(Db &db)
7479
: db_{db}
@@ -222,34 +227,59 @@ class DbCache final : public Db
222227
virtual std::string print_stats() override
223228
{
224229
return db_.print_stats() + ",ac=" + accounts_.print_stats() +
225-
",sc=" + storage_.print_stats();
230+
",sc=" + storage_.print_stats() + print_read_write_stats();
226231
}
227232

228233
virtual uint64_t get_block_number() const override
229234
{
230235
return db_.get_block_number();
231236
}
232237

238+
std::string print_read_write_stats() const
239+
{
240+
std::string str = std::format(
241+
",at={},am={},st={},sm={}",
242+
num_account_touched_,
243+
num_account_changed_,
244+
num_storage_touched_,
245+
num_storage_changed_);
246+
return str;
247+
}
248+
233249
private:
234250
void insert_in_lru_caches(StateDeltas const &state_deltas)
235251
{
252+
num_account_touched_ = 0;
253+
num_account_changed_ = 0;
254+
num_storage_touched_ = 0;
255+
num_storage_changed_ = 0;
236256
for (auto it = state_deltas.cbegin(); it != state_deltas.cend(); ++it) {
237257
auto const &address = it->first;
238258
auto const &account_delta = it->second.account;
239259
accounts_.insert(address, account_delta.second);
240260
auto const &storage = it->second.storage;
241261
auto const &account = account_delta.second;
262+
num_account_touched_++;
263+
bool account_changed = account_delta.first != account;
242264
if (account.has_value()) {
243265
for (auto it2 = storage.cbegin(); it2 != storage.cend();
244266
++it2) {
245267
auto const &key = it2->first;
246268
auto const &storage_delta = it2->second;
247269
auto const incarnation = account->incarnation;
270+
num_storage_touched_++;
271+
if (storage_delta.first != storage_delta.second) {
272+
num_storage_changed_++;
273+
account_changed = true;
274+
}
248275
storage_.insert(
249276
StorageKey(address, incarnation, key),
250277
storage_delta.second);
251278
}
252279
}
280+
if (account_changed) {
281+
num_account_changed_++;
282+
}
253283
}
254284
}
255285
};

category/execution/ethereum/state2/block_state.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ BlockState::BlockState(Db &db, vm::VM &monad_vm)
5151
: db_{db}
5252
, vm_{monad_vm}
5353
, state_(std::make_unique<StateDeltas>())
54+
, final_state_(std::make_unique<StateDeltas>())
5455
{
5556
}
5657

@@ -211,13 +212,26 @@ void BlockState::merge(State const &state)
211212
}
212213

213214
MONAD_ASSERT(state_);
215+
MONAD_ASSERT(final_state_);
214216
for (auto const &[address, stack] : current) {
215217
auto const &account_state = stack.recent();
216218
auto const &account = account_state.account_;
217219
auto const &storage = account_state.storage_;
218220
StateDeltas::accessor it{};
219221
MONAD_ASSERT(state_->find(it, address));
220222
it->second.account.second = account;
223+
StateDeltas::accessor final_it{};
224+
if (final_state_->find(final_it, address)) {
225+
MONAD_ASSERT(
226+
final_it->second.account.first == it->second.account.first);
227+
final_it->second.account.second = account;
228+
}
229+
else {
230+
final_state_->emplace(
231+
final_it,
232+
address,
233+
StateDelta{.account = it->second.account, .storage = {}});
234+
}
221235
if (account.has_value()) {
222236
for (auto const &[key, value] : storage) {
223237
StorageDeltas::accessor it2{};
@@ -226,12 +240,20 @@ void BlockState::merge(State const &state)
226240
}
227241
else {
228242
it->second.storage.emplace(
229-
key, std::make_pair(bytes32_t{}, value));
243+
it2, key, std::make_pair(bytes32_t{}, value));
244+
}
245+
StorageDeltas::accessor final_it2{};
246+
if (final_it->second.storage.find(final_it2, key)) {
247+
final_it2->second.second = value;
248+
}
249+
else {
250+
final_it->second.storage.emplace(key, it2->second);
230251
}
231252
}
232253
}
233254
else {
234255
it->second.storage.clear();
256+
final_it->second.storage.clear();
235257
}
236258
}
237259
}
@@ -246,7 +268,7 @@ void BlockState::commit(
246268
std::optional<std::vector<Withdrawal>> const &withdrawals)
247269
{
248270
db_.commit(
249-
std::move(state_),
271+
std::move(final_state_),
250272
code_,
251273
block_id,
252274
header,
@@ -262,6 +284,7 @@ void BlockState::log_debug()
262284
{
263285
MONAD_ASSERT(state_);
264286
LOG_DEBUG("State Deltas: {}", *state_);
287+
LOG_DEBUG("Final States: {}", *final_state_);
265288
LOG_DEBUG("Code Deltas: {}", code_);
266289
}
267290

category/execution/ethereum/state2/block_state.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class BlockState final
3838
Db &db_;
3939
vm::VM &vm_;
4040
std::unique_ptr<StateDeltas> state_;
41+
std::unique_ptr<StateDeltas> final_state_;
4142
Code code_;
4243

4344
public:

cmd/monad/runloop_ethereum.cpp

Lines changed: 1 addition & 1 deletion
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

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)