Skip to content

Commit

Permalink
Removed excess time utils (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gazizonoki authored Apr 8, 2024
1 parent 6618cd8 commit efa48d5
Show file tree
Hide file tree
Showing 26 changed files with 20 additions and 1,833 deletions.
7 changes: 4 additions & 3 deletions client/impl/ydb_internal/retry/retry.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ class TRetryContextBase : TNonCopyable {
protected:
TRetryOperationSettings Settings_;
ui32 RetryNumber_;
TSimpleTimer RetryTimer_;
TInstant RetryStartTime_;

protected:
TRetryContextBase(const TRetryOperationSettings& settings)
: Settings_(settings)
, RetryNumber_(0)
, RetryStartTime_(TInstant::Now())
{}

virtual void Reset() {}
Expand All @@ -60,7 +61,7 @@ class TRetryContextBase : TNonCopyable {
if (RetryNumber_ >= Settings_.MaxRetries_) {
return NextStep::Finish;
}
if (RetryTimer_.Get() >= Settings_.MaxTimeout_) {
if (TInstant::Now() - RetryStartTime_ >= Settings_.MaxTimeout_) {
return NextStep::Finish;
}
switch (status.GetStatus()) {
Expand Down Expand Up @@ -107,7 +108,7 @@ class TRetryContextBase : TNonCopyable {
}

TDuration GetRemainingTimeout() {
return Settings_.MaxTimeout_ - RetryTimer_.Get();
return Settings_.MaxTimeout_ - (TInstant::Now() - RetryStartTime_);
}
};

Expand Down
2 changes: 1 addition & 1 deletion client/impl/ydb_internal/retry/retry_async.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TRetryContext : public TThrRefBase, public TRetryContextBase {

public:
TAsyncStatusType Execute() {
this->RetryTimer_.Reset();
this->RetryStartTime_ = TInstant::Now();
this->Retry();
return this->Promise_.GetFuture();
}
Expand Down
2 changes: 1 addition & 1 deletion client/impl/ydb_internal/retry/retry_sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TRetryContext : public TRetryContextBase {

public:
TStatusType Execute() {
this->RetryTimer_.Reset();
this->RetryStartTime_ = TInstant::Now();
TStatusType status = Retry(); // first attempt
for (this->RetryNumber_ = 0; this->RetryNumber_ <= this->Settings_.MaxRetries_;) {
auto nextStep = this->GetNextStep(status);
Expand Down
1 change: 0 additions & 1 deletion util/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
add_subdirectory(charset)
add_subdirectory(draft)

add_library(yutil)
target_compile_options(yutil PRIVATE
Expand Down
117 changes: 5 additions & 112 deletions util/datetime/cputimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,133 +5,26 @@
#include <util/stream/output.h>
#include <util/generic/singleton.h>

#include <format>
#include <iostream>

#if defined(_unix_)
#include <unistd.h>
#include <sched.h>
#elif defined(_win_)
#include <util/system/winint.h>
#endif

TTimer::TTimer(const std::string_view message) {
static const int SMALL_DURATION_CHAR_LENGTH = 9; // strlen("0.123456s")
Message_.Reserve(message.length() + SMALL_DURATION_CHAR_LENGTH + 1); // +"\n"
Message_ << message;
// Do not measure the allocations above.
Start_ = TInstant::Now();
Start_ = Clock_.now();
}

TTimer::~TTimer() {
const TDuration duration = TInstant::Now() - Start_;
const TClock::duration duration = Clock_.now() - Start_;
Message_ << duration << "\n";
std::cerr << Message_.Str();
}

static ui64 ManuallySetCyclesPerSecond = 0;

static ui64 GetCyclesPerSecond() {
if (ManuallySetCyclesPerSecond != 0) {
return ManuallySetCyclesPerSecond;
} else {
return NHPTimer::GetCyclesPerSecond();
}
}

void SetCyclesPerSecond(ui64 cycles) {
ManuallySetCyclesPerSecond = cycles;
}

ui64 GetCyclesPerMillisecond() {
return GetCyclesPerSecond() / 1000;
}

TDuration CyclesToDuration(ui64 cycles) {
return TDuration::MicroSeconds(cycles * 1000000 / GetCyclesPerSecond());
}

TDuration CyclesToDurationSafe(ui64 cycles)
{
constexpr ui64 cyclesLimit = std::numeric_limits<ui64>::max() / 1000000;
if (cycles <= cyclesLimit) {
return CyclesToDuration(cycles);
}
return TDuration::MicroSeconds(cycles / GetCyclesPerSecond() * 1000000);
}

ui64 DurationToCycles(TDuration duration) {
return duration.MicroSeconds() * GetCyclesPerSecond() / 1000000;
}

ui64 DurationToCyclesSafe(TDuration duration)
{
if (duration.MicroSeconds() <= std::numeric_limits<ui64>::max() / GetCyclesPerSecond()) {
return DurationToCycles(duration);
}
return duration.MicroSeconds() / 1000000 * GetCyclesPerSecond();
}

TPrecisionTimer::TPrecisionTimer()
: Start(::GetCycleCount())
{
}

ui64 TPrecisionTimer::GetCycleCount() const {
return ::GetCycleCount() - Start;
}

std::string FormatCycles(ui64 cycles) {
ui64 milliseconds = cycles / GetCyclesPerMillisecond();
ui32 ms = ui32(milliseconds % 1000);
milliseconds /= 1000;
ui32 secs = ui32(milliseconds % 60);
milliseconds /= 60;
ui32 mins = ui32(milliseconds);
std::string result = std::format("%" PRIu32 " m %.2" PRIu32 " s %.3" PRIu32 " ms", mins, secs, ms);
return result;
std::cerr << Message_.str();
}

TFuncTimer::TFuncTimer(const char* func)
: Start_(TInstant::Now())
: Start_(Clock_.now())
, Func_(func)
{
std::cerr << "enter " << Func_ << std::endl;
}

TFuncTimer::~TFuncTimer() {
std::cerr << "leave " << Func_ << " -> " << (TInstant::Now() - Start_).ToString() << std::endl;
}

TTimeLogger::TTimeLogger(const std::string& message, bool verbose)
: Message(message)
, Verbose(verbose)
, OK(false)
, Begin(time(nullptr))
, BeginCycles(GetCycleCount())
{
if (Verbose) {
fprintf(stderr, "=========================================================\n");
fprintf(stderr, "%s started: %.24s (%lu) (%d)\n", Message.data(), ctime(&Begin), (unsigned long)Begin, (int)getpid());
}
}

double TTimeLogger::ElapsedTime() const {
return time(nullptr) - Begin;
}

void TTimeLogger::SetOK() {
OK = true;
}

TTimeLogger::~TTimeLogger() {
time_t tim = time(nullptr);
ui64 endCycles = GetCycleCount();
if (Verbose) {
const char* prefix = (OK) ? "" : "!";
fprintf(stderr, "%s%s ended: %.24s (%lu) (%d) (took %lus = %s)\n",
prefix, Message.data(), ctime(&tim), (unsigned long)tim, (int)getpid(),
(unsigned long)tim - (unsigned long)Begin, FormatCycles(endCycles - BeginCycles).data());
fprintf(stderr, "%s=========================================================\n", prefix);
}
std::cerr << "leave " << Func_ << " -> " << Clock_.now() - Start_ << std::endl;
}
107 changes: 9 additions & 98 deletions util/datetime/cputimer.h
Original file line number Diff line number Diff line change
@@ -1,116 +1,27 @@
#pragma once

#include "base.h"

#include <util/system/rusage.h>
#include <util/stream/str.h>
#include <chrono>

class TTimer {
private:
TInstant Start_;
TStringStream Message_;
using TClock = std::chrono::high_resolution_clock;

TClock Clock_;
std::chrono::time_point<TClock, TClock::duration> Start_;
std::stringstream Message_;

public:
TTimer(const std::string_view message = " took: ");
~TTimer();
};

class TSimpleTimer {
TInstant T;

public:
TSimpleTimer() {
Reset();
}
TDuration Get() const {
return TInstant::Now() - T;
}
void Reset() {
T = TInstant::Now();
}
};

class TProfileTimer {
TDuration T;

public:
TProfileTimer() {
Reset();
}
TDuration Get() const {
return TRusage::Get().Utime - T;
}
TDuration Step() {
TRusage r;
r.Fill();
TDuration d = r.Utime - T;
T = r.Utime;
return d;
}
void Reset() {
T = TRusage::Get().Utime;
}
};

/// Return cached processor cycle count per second. Method takes 1 second at first invocation.
/// Note, on older systems cycle rate may change during program lifetime,
/// so returned value may be incorrect. Modern Intel and AMD processors keep constant TSC rate.
ui64 GetCyclesPerMillisecond();
void SetCyclesPerSecond(ui64 cycles);

TDuration CyclesToDuration(ui64 cycles);
ui64 DurationToCycles(TDuration duration);

// NBS-3400 - CyclesToDuration and DurationToCycles may overflow for long running events
TDuration CyclesToDurationSafe(ui64 cycles);
ui64 DurationToCyclesSafe(TDuration duration);

class TPrecisionTimer {
private:
ui64 Start = 0;

public:
TPrecisionTimer();

ui64 GetCycleCount() const;
};

std::string FormatCycles(ui64 cycles);

class TFuncTimer {
using TClock = std::chrono::high_resolution_clock;
public:
TFuncTimer(const char* func);
~TFuncTimer();

private:
const TInstant Start_;
TClock Clock_;
const std::chrono::time_point<TClock, TClock::duration> Start_;
const char* Func_;
};

class TFakeTimer {
public:
inline TFakeTimer(const char* = nullptr) noexcept {
}
};

#if defined(WITH_DEBUG)
#define TDebugTimer TFuncTimer
#else
#define TDebugTimer TFakeTimer
#endif

class TTimeLogger {
private:
std::string Message;
bool Verbose;
bool OK;
time_t Begin;
ui64 BeginCycles;

public:
TTimeLogger(const std::string& message, bool verbose = true);
~TTimeLogger();

void SetOK();
double ElapsedTime() const;
};
11 changes: 0 additions & 11 deletions util/draft/CMakeLists.txt

This file was deleted.

Loading

0 comments on commit efa48d5

Please sign in to comment.