TimeUtil.cpp cleanup

This commit is contained in:
Henrik Rydgård 2020-09-25 09:33:42 +02:00
parent 6fb0e92de3
commit 5411a40f2a
2 changed files with 5 additions and 45 deletions

View file

@ -20,9 +20,9 @@ static double curtime = 0;
#ifdef _WIN32 #ifdef _WIN32
LARGE_INTEGER frequency; static LARGE_INTEGER frequency;
double frequencyMult; static double frequencyMult;
LARGE_INTEGER startTime; static LARGE_INTEGER startTime;
double time_now_d() { double time_now_d() {
if (frequency.QuadPart == 0) { if (frequency.QuadPart == 0) {
@ -39,8 +39,8 @@ double time_now_d() {
#else #else
uint64_t _frequency = 0; static uint64_t _frequency = 0;
uint64_t _starttime = 0; static uint64_t _starttime = 0;
double time_now_d() { double time_now_d() {
static time_t start; static time_t start;
@ -64,26 +64,3 @@ void sleep_ms(int ms) {
usleep(ms * 1000); usleep(ms * 1000);
#endif #endif
} }
LoggingDeadline::LoggingDeadline(const char *name, int ms) : name_(name), endCalled_(false) {
totalTime_ = (double)ms * 0.001;
endTime_ = time_now_d() + totalTime_;
}
LoggingDeadline::~LoggingDeadline() {
if (!endCalled_)
End();
}
bool LoggingDeadline::End() {
endCalled_ = true;
double now = time_now_d();
if (now > endTime_) {
double late = (now - endTime_);
double totalTime = late + totalTime_;
ERROR_LOG(SYSTEM, "===== %0.2fms DEADLINE PASSED FOR %s at %0.2fms - %0.2fms late =====", totalTime_ * 1000.0, name_, 1000.0 * totalTime, 1000.0 * late);
return false;
}
return true;
}

View file

@ -1,24 +1,7 @@
#pragma once #pragma once
// http://linux.die.net/man/3/clock_gettime
// Seconds. // Seconds.
double time_now_d(); double time_now_d();
// Sleep. Does not necessarily have millisecond granularity, especially on Windows. // Sleep. Does not necessarily have millisecond granularity, especially on Windows.
void sleep_ms(int ms); void sleep_ms(int ms);
// Can be sprinkled around the code to make sure that thing don't take
// unexpectedly long. What that means is up to you.
class LoggingDeadline {
public:
LoggingDeadline(const char *name, int deadline_in_ms);
~LoggingDeadline();
bool End();
private:
const char *name_;
bool endCalled_;
double totalTime_;
double endTime_;
};