Skip to content

Commit 76144d4

Browse files
authored
Session stats improvements (#1746)
* Detailed session stats for work time and storage stat cache * Bring back the old session state file writing
1 parent 4ec6711 commit 76144d4

File tree

12 files changed

+277
-152
lines changed

12 files changed

+277
-152
lines changed

crypto/block/transaction.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,12 +1710,12 @@ bool Transaction::run_precompiled_contract(const ComputePhaseConfig& cfg, precom
17101710
ComputePhase& cp = *compute_phase;
17111711
CHECK(cp.precompiled_gas_usage);
17121712
td::uint64 gas_usage = cp.precompiled_gas_usage.value();
1713-
td::Timer timer;
1713+
td::RealCpuTimer timer;
17141714
auto result =
17151715
impl.run(my_addr, now, start_lt, balance, new_data, *in_msg_body, in_msg, msg_balance_remaining, in_msg_extern,
17161716
compute_vm_libraries(cfg), cfg.global_version, cfg.max_vm_data_depth, new_code,
17171717
cfg.unpacked_config_tuple, due_payment.not_null() ? due_payment : td::zero_refint(), gas_usage);
1718-
double elapsed = timer.elapsed();
1718+
time_tvm = timer.elapsed_both();
17191719
cp.vm_init_state_hash = td::Bits256::zero();
17201720
cp.exit_code = result.exit_code;
17211721
cp.out_of_gas = false;
@@ -1726,15 +1726,15 @@ bool Transaction::run_precompiled_contract(const ComputePhaseConfig& cfg, precom
17261726
cp.success = (cp.accepted && result.committed);
17271727
LOG(INFO) << "Running precompiled smart contract " << impl.get_name() << ": exit_code=" << result.exit_code
17281728
<< " accepted=" << result.accepted << " success=" << cp.success << " gas_used=" << gas_usage
1729-
<< " time=" << elapsed << "s";
1729+
<< " time=" << time_tvm.real << "s cpu_time=" << time_tvm.cpu;
17301730
if (cp.accepted & use_msg_state) {
17311731
was_activated = true;
17321732
acc_status = Account::acc_active;
17331733
}
17341734
if (cfg.with_vm_log) {
17351735
cp.vm_log = PSTRING() << "Running precompiled smart contract " << impl.get_name()
17361736
<< ": exit_code=" << result.exit_code << " accepted=" << result.accepted
1737-
<< " success=" << cp.success << " gas_used=" << gas_usage << " time=" << elapsed << "s";
1737+
<< " success=" << cp.success << " gas_used=" << gas_usage << " time=" << time_tvm.real << "s";
17381738
}
17391739
if (cp.success) {
17401740
cp.new_data = impl.get_c4();
@@ -1943,9 +1943,9 @@ bool Transaction::prepare_compute_phase(const ComputePhaseConfig& cfg) {
19431943

19441944
LOG(DEBUG) << "starting VM";
19451945
cp.vm_init_state_hash = vm.get_state_hash();
1946-
td::Timer timer;
1946+
td::RealCpuTimer timer;
19471947
cp.exit_code = ~vm.run();
1948-
double elapsed = timer.elapsed();
1948+
time_tvm = timer.elapsed_both();
19491949
LOG(DEBUG) << "VM terminated with exit code " << cp.exit_code;
19501950
cp.out_of_gas = (cp.exit_code == ~(int)vm::Excno::out_of_gas);
19511951
cp.vm_final_state_hash = vm.get_final_state_hash(cp.exit_code);
@@ -1971,7 +1971,7 @@ bool Transaction::prepare_compute_phase(const ComputePhaseConfig& cfg) {
19711971
LOG(INFO) << "steps: " << vm.get_steps_count() << " gas: used=" << gas.gas_consumed() << ", max=" << gas.gas_max
19721972
<< ", limit=" << gas.gas_limit << ", credit=" << gas.gas_credit;
19731973
LOG(INFO) << "out_of_gas=" << cp.out_of_gas << ", accepted=" << cp.accepted << ", success=" << cp.success
1974-
<< ", time=" << elapsed << "s";
1974+
<< ", time=" << time_tvm.real << "s, cpu_time=" << time_tvm.cpu;
19751975
if (logger != nullptr) {
19761976
cp.vm_log = logger->get_log();
19771977
}
@@ -3196,7 +3196,13 @@ td::Status Transaction::check_state_limits(const SizeLimitsConfig& size_limits,
31963196
}
31973197
{
31983198
TD_PERF_COUNTER(transaction_storage_stat_a);
3199-
td::Timer timer;
3199+
td::RealCpuTimer timer;
3200+
SCOPE_EXIT {
3201+
LOG_IF(INFO, timer.elapsed_real() > 0.1) << "Compute used storage (1) took " << timer.elapsed_real() << "s";
3202+
if (is_account_stat) {
3203+
time_storage_stat += timer.elapsed_both();
3204+
}
3205+
};
32003206
if (is_account_stat && compute_phase) {
32013207
storage_stat.add_hint(compute_phase->vm_loaded_cells);
32023208
}
@@ -3208,9 +3214,6 @@ td::Status Transaction::check_state_limits(const SizeLimitsConfig& size_limits,
32083214
storage_stat_updates.push_back(new_library);
32093215
}
32103216
TRY_STATUS(storage_stat.replace_roots({new_code, new_data, new_library}, /* check_merkle_depth = */ true));
3211-
if (timer.elapsed() > 0.1) {
3212-
LOG(INFO) << "Compute used storage (1) took " << timer.elapsed() << "s";
3213-
}
32143217
}
32153218

32163219
if (storage_stat.get_total_cells() > size_limits.max_acc_state_cells ||
@@ -3511,9 +3514,11 @@ bool Transaction::compute_state(const SerializeConfig& cfg) {
35113514
auto roots = new_storage_for_stat->prefetch_all_refs();
35123515
storage_stat_updates.insert(storage_stat_updates.end(), roots.begin(), roots.end());
35133516
{
3517+
td::RealCpuTimer timer;
35143518
StorageStatCalculationContext context{true};
35153519
StorageStatCalculationContext::Guard guard{&context};
35163520
td::Status S = stats.replace_roots(roots);
3521+
time_storage_stat += timer.elapsed_both();
35173522
if (S.is_error()) {
35183523
LOG(ERROR) << "Cannot recompute storage stats for account " << account.addr.to_hex() << ": " << S.move_as_error();
35193524
return false;

crypto/block/transaction.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ struct Transaction {
399399
td::optional<td::Bits256> new_storage_dict_hash;
400400
bool gas_limit_overridden{false};
401401
std::vector<Ref<vm::Cell>> storage_stat_updates;
402+
td::RealCpuTimer::Time time_tvm, time_storage_stat;
402403
Transaction(const Account& _account, int ttype, ton::LogicalTime req_start_lt, ton::UnixTime _now,
403404
Ref<vm::Cell> _inmsg = {});
404405
bool unpack_input_msg(bool ihr_delivered, const ActionPhaseConfig* cfg);

tdutils/td/utils/Timer.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,52 @@ class ThreadCpuTimer {
8181
bool is_paused_{false};
8282
};
8383

84+
class RealCpuTimer {
85+
public:
86+
RealCpuTimer() : RealCpuTimer(false) {
87+
}
88+
explicit RealCpuTimer(bool is_paused) : real_(is_paused), cpu_(is_paused) {
89+
}
90+
RealCpuTimer(const RealCpuTimer &other) = default;
91+
RealCpuTimer &operator=(const RealCpuTimer &other) = default;
92+
93+
double elapsed_real() const {
94+
return real_.elapsed();
95+
}
96+
double elapsed_cpu() const {
97+
return cpu_.elapsed();
98+
}
99+
struct Time {
100+
double real = 0.0, cpu = 0.0;
101+
double get(bool is_cpu) const {
102+
return is_cpu ? cpu : real;
103+
}
104+
Time &operator+=(const Time &other) {
105+
real += other.real;
106+
cpu += other.cpu;
107+
return *this;
108+
}
109+
Time operator-(const Time &other) const {
110+
return {.real = real - other.real, .cpu = cpu - other.cpu};
111+
}
112+
};
113+
Time elapsed_both() const {
114+
return {.real = real_.elapsed(), .cpu = cpu_.elapsed()};
115+
}
116+
void pause() {
117+
real_.pause();
118+
cpu_.pause();
119+
}
120+
void resume() {
121+
real_.resume();
122+
cpu_.resume();
123+
}
124+
125+
private:
126+
Timer real_;
127+
ThreadCpuTimer cpu_;
128+
};
129+
84130
class PerfLog;
85131
struct EmptyDeleter {
86132
template <class T>

tl/generate/scheme/ton_api.tl

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -955,21 +955,35 @@ validatorStats.blockStats
955955
ext_msgs:validatorStats.blockStats.extMsgsStats transactions:int shard_configuration:(vector tonNode.blockIdExt)
956956
old_out_msg_queue_size:long new_out_msg_queue_size:long msg_queue_cleaned:int
957957
neighbors:(vector validatorStats.blockStats.neighborStats) = validatorStats.BlockStats;
958+
validatorStats.collateWorkTimeStats
959+
total:double queue_cleanup:double prelim_storage_stat:double trx_tvm:double trx_storage_stat:double
960+
trx_other:double final_storage_stat:double create_block:double create_collated_data:double create_block_candidate:double
961+
= validatorStats.CollateWorkTimeStats;
962+
validatorStats.storageStatCacheStats
963+
small_cnt:long small_cells:long hit_cnt:long hit_cells:long miss_cnt:long miss_cells:long = validatorStats.StorageStatCacheStats;
958964

959965
validatorStats.collatedBlock
960966
block_id:tonNode.blockIdExt collated_data_hash:int256 cc_seqno:int collated_at:double
961967
bytes:int collated_data_bytes:int attempt:int
962968
self:int256 is_validator:Bool
963969
total_time:double work_time:double cpu_work_time:double time_stats:string
970+
work_time_real_stats:validatorStats.collateWorkTimeStats
971+
work_time_cpu_stats:validatorStats.collateWorkTimeStats
964972
block_limits:validatorStats.blockLimitsStatus
965-
block_stats:validatorStats.blockStats = validatorSession.stats.CollatedBlock;
973+
block_stats:validatorStats.blockStats
974+
storage_stat_cache:validatorStats.storageStatCacheStats = validatorSession.stats.CollatedBlock;
966975

976+
validatorStats.validateWorkTimeStats
977+
total:double trx_tvm:double trx_storage_stat:double trx_other:double = validatorStats.ValidateWorkTimeStats;
967978
validatorStats.validatedBlock
968979
block_id:tonNode.blockIdExt collated_data_hash:int256 validated_at:double
969980
self:int256
970981
valid:Bool comment:string
971982
bytes:int collated_data_bytes:int
972-
total_time:double work_time:double cpu_work_time:double = validatorStats.ValidatedBlock;
983+
total_time:double work_time:double cpu_work_time:double
984+
work_time_real_stats:validatorStats.validateWorkTimeStats
985+
work_time_cpu_stats:validatorStats.validateWorkTimeStats
986+
storage_stat_cache:validatorStats.storageStatCacheStats = validatorStats.ValidatedBlock;
973987

974988
validatorStats.newValidatorGroup.node id:int256 pubkey:PublicKey adnl_id:int256 weight:long = validatorStats.newValidatorGroup.Node;
975989
validatorStats.newValidatorGroup session_id:int256 shard:tonNode.shardId cc_seqno:int

tl/generate/scheme/ton_api.tlo

1.44 KB
Binary file not shown.

validator/impl/collator-impl.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class Collator final : public td::actor::Actor {
118118
Ref<vm::Cell> msg_root, block::Account* acc, UnixTime utime, LogicalTime lt,
119119
block::StoragePhaseConfig* storage_phase_cfg, block::ComputePhaseConfig* compute_phase_cfg,
120120
block::ActionPhaseConfig* action_phase_cfg, block::SerializeConfig* serialize_cfg, bool external,
121-
LogicalTime after_lt);
121+
LogicalTime after_lt, CollationStats* stats = nullptr);
122122

123123
private:
124124
void start_up() override;
@@ -404,8 +404,7 @@ class Collator final : public td::actor::Actor {
404404
static td::uint32 get_skip_externals_queue_size();
405405

406406
private:
407-
td::Timer work_timer_{true};
408-
td::ThreadCpuTimer cpu_work_timer_{true};
407+
td::RealCpuTimer work_timer_{true};
409408
CollationStats stats_;
410409

411410
void finalize_stats();

0 commit comments

Comments
 (0)