ppsspp/base/timeutil.cpp

104 lines
2 KiB
C++
Raw Normal View History

2012-03-24 23:39:19 +01:00
#include "base/basictypes.h"
#include "base/logging.h"
2012-03-24 23:39:19 +01:00
#include "base/timeutil.h"
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/time.h>
#include <unistd.h>
#endif
#include <stdio.h>
static double curtime = 0;
static float curtime_f = 0;
#ifdef _WIN32
__int64 _frequency = 0;
__int64 _starttime = 0;
double real_time_now(){
2012-10-30 13:20:55 +01:00
if (_frequency == 0) {
QueryPerformanceFrequency((LARGE_INTEGER*)&_frequency);
QueryPerformanceCounter((LARGE_INTEGER*)&_starttime);
curtime=0;
}
__int64 time;
QueryPerformanceCounter((LARGE_INTEGER*)&time);
return ((double) (time - _starttime) / (double) _frequency);
2012-03-24 23:39:19 +01:00
}
#else
double real_time_now() {
2012-10-30 13:20:55 +01:00
static time_t start;
struct timeval tv;
gettimeofday(&tv, NULL);
if (start == 0) {
start = tv.tv_sec;
}
tv.tv_sec -= start;
return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
2012-03-24 23:39:19 +01:00
}
#endif
void time_update() {
2012-10-30 13:20:55 +01:00
curtime = real_time_now();
curtime_f = (float)curtime;
2012-03-24 23:39:19 +01:00
2012-10-30 13:20:55 +01:00
//printf("curtime: %f %f\n", curtime, curtime_f);
// also smooth time.
//curtime+=float((double) (time-_starttime) / (double) _frequency);
//curtime*=0.5f;
//curtime+=1.0f/60.0f;
//lastTime=curtime;
//curtime_f = (float)curtime;
2012-03-24 23:39:19 +01:00
}
float time_now() {
2012-10-30 13:20:55 +01:00
return curtime_f;
2012-03-24 23:39:19 +01:00
}
double time_now_d() {
2012-10-30 13:20:55 +01:00
return curtime;
2012-03-24 23:39:19 +01:00
}
int time_now_ms() {
2012-10-30 13:20:55 +01:00
return int(curtime*1000.0);
2012-03-24 23:39:19 +01:00
}
void sleep_ms(int ms) {
#ifdef _WIN32
#ifndef METRO
2012-10-30 13:20:55 +01:00
Sleep(ms);
2012-03-24 23:39:19 +01:00
#endif
#else
2012-10-30 13:20:55 +01:00
usleep(ms * 1000);
2012-03-24 23:39:19 +01:00
#endif
}
LoggingDeadline::LoggingDeadline(const char *name, int ms) : name_(name), endCalled_(false) {
totalTime_ = (double)ms * 0.001;
time_update();
endTime_ = time_now_d() + totalTime_;
}
LoggingDeadline::~LoggingDeadline() {
if (!endCalled_)
End();
}
bool LoggingDeadline::End() {
time_update();
if (time_now_d() > endTime_) {
double late = (time_now_d() - endTime_);
double totalTime = late + totalTime_;
2013-01-11 00:29:26 +01:00
ELOG("===== %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;
}