1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-03-09 15:40:10 +00:00

optimistic out-msg-queue broadcast

This commit is contained in:
birydrad 2024-11-26 14:01:20 +04:00
parent 6df6f182bf
commit bf572f9599
24 changed files with 623 additions and 126 deletions

View file

@ -149,4 +149,19 @@ std::enable_if_t<std::is_arithmetic<T>::value, string> to_string(const T &x) {
return sb.as_cslice().str();
}
template <class SB>
struct LambdaPrintHelper {
SB& sb;
};
template <class SB, class F>
SB& operator<<(const LambdaPrintHelper<SB>& helper, F&& f) {
f(helper.sb);
return helper.sb;
}
struct LambdaPrint {};
inline LambdaPrintHelper<td::StringBuilder> operator<<(td::StringBuilder& sb, const LambdaPrint&) {
return LambdaPrintHelper<td::StringBuilder>{sb};
}
} // namespace td

View file

@ -22,6 +22,8 @@
#include "td/utils/logging.h"
#include "td/utils/Time.h"
#include <numeric>
namespace td {
Timer::Timer(bool is_paused) : is_paused_(is_paused) {
@ -60,12 +62,15 @@ StringBuilder &operator<<(StringBuilder &string_builder, const Timer &timer) {
return string_builder << format::as_time(timer.elapsed());
}
PerfWarningTimer::PerfWarningTimer(string name, double max_duration, std::function<void(double)>&& callback)
PerfWarningTimer::PerfWarningTimer(string name, double max_duration, std::function<void(double)> &&callback)
: name_(std::move(name)), start_at_(Time::now()), max_duration_(max_duration), callback_(std::move(callback)) {
}
PerfWarningTimer::PerfWarningTimer(PerfWarningTimer &&other)
: name_(std::move(other.name_)), start_at_(other.start_at_), max_duration_(other.max_duration_), callback_(std::move(other.callback_)) {
: name_(std::move(other.name_))
, start_at_(other.start_at_)
, max_duration_(other.max_duration_)
, callback_(std::move(other.callback_)) {
other.start_at_ = 0;
}
@ -134,4 +139,34 @@ double ThreadCpuTimer::elapsed() const {
return res;
}
PerfLogAction PerfLog::start_action(std::string name) {
auto i = entries_.size();
entries_.push_back({.name = std::move(name), .begin = td::Timestamp::now().at()});
return PerfLogAction{i, std::unique_ptr<PerfLog, EmptyDeleter>(this)};
}
td::StringBuilder &operator<<(StringBuilder &sb, const PerfLog &log) {
sb << "{";
std::vector<size_t> ids(log.entries_.size());
std::iota(ids.begin(), ids.end(), 0);
std::sort(ids.begin(), ids.end(), [&](auto a, auto b) {
return log.entries_[a].end - log.entries_[a].begin > log.entries_[b].end - log.entries_[b].begin;
});
sb << "{";
for (size_t i = 0; i < log.entries_.size(); i++) {
sb << "\n\t";
auto &entry = log.entries_[ids[i]];
sb << "{" << entry.name << ":" << entry.begin << "->" << entry.end << "(" << entry.end - entry.begin << ")"
<< td::format::cond(entry.status.is_error(), entry.status, "") << "}";
}
sb << "\n}";
return sb;
}
double PerfLog::finish_action(size_t i, td::Status status) {
auto &entry = entries_[i];
CHECK(entry.end == 0);
entry.end = td::Timestamp::now().at();
entry.status = std::move(status);
return entry.end - entry.begin;
}
} // namespace td

View file

@ -19,6 +19,7 @@
#pragma once
#include "td/utils/StringBuilder.h"
#include "td/utils/Status.h"
#include <functional>
@ -46,7 +47,7 @@ class Timer {
class PerfWarningTimer {
public:
explicit PerfWarningTimer(string name, double max_duration = 0.1, std::function<void(double)>&& callback = {});
explicit PerfWarningTimer(string name, double max_duration = 0.1, std::function<void(double)> &&callback = {});
PerfWarningTimer(const PerfWarningTimer &) = delete;
PerfWarningTimer &operator=(const PerfWarningTimer &) = delete;
PerfWarningTimer(PerfWarningTimer &&other);
@ -80,4 +81,44 @@ class ThreadCpuTimer {
bool is_paused_{false};
};
class PerfLog;
struct EmptyDeleter {
template <class T>
void operator()(T *) {
}
};
class PerfLogAction {
public:
template <class T>
double finish(const T &result);
size_t i_{0};
std::unique_ptr<PerfLog, EmptyDeleter> perf_log_;
};
class PerfLog {
public:
PerfLogAction start_action(std::string name);
friend td::StringBuilder &operator<<(td::StringBuilder &sb, const PerfLog &log);
private:
struct Entry {
std::string name;
double begin{};
double end{};
td::Status status;
};
std::vector<Entry> entries_;
friend class PerfLogAction;
double finish_action(size_t i, td::Status status);
};
template <class T>
double PerfLogAction::finish(const T &result) {
if (result.is_ok()) {
return perf_log_->finish_action(i_, td::Status::OK());
} else {
return perf_log_->finish_action(i_, result.error().clone());
}
}
} // namespace td

View file

@ -74,6 +74,7 @@
#define LOG(level) LOG_IMPL(level, level, true, ::td::Slice())
#define LOG_IF(level, condition) LOG_IMPL(level, level, condition, #condition)
#define FLOG(level) LOG_IMPL(level, level, true, ::td::Slice()) << td::LambdaPrint{} << [&](auto &sb)
#define VLOG(level) LOG_IMPL(DEBUG, level, true, TD_DEFINE_STR(level))
#define VLOG_IF(level, condition) LOG_IMPL(DEBUG, level, condition, TD_DEFINE_STR(level) " " #condition)
@ -95,13 +96,13 @@ inline bool no_return_func() {
#define DUMMY_LOG_CHECK(condition) LOG_IF(NEVER, !(condition))
#ifdef TD_DEBUG
#if TD_MSVC
#if TD_MSVC
#define LOG_CHECK(condition) \
__analysis_assume(!!(condition)); \
LOG_IMPL(FATAL, FATAL, !(condition), #condition)
#else
#else
#define LOG_CHECK(condition) LOG_IMPL(FATAL, FATAL, !(condition) && no_return_func(), #condition)
#endif
#endif
#else
#define LOG_CHECK DUMMY_LOG_CHECK
#endif
@ -263,6 +264,9 @@ class Logger {
sb_ << other;
return *this;
}
LambdaPrintHelper<td::Logger> operator<<(const LambdaPrint &) {
return LambdaPrintHelper<td::Logger>{*this};
}
MutableCSlice as_cslice() {
return sb_.as_cslice();