Skip to content

Commit

Permalink
Record CPU time (time between start and stop method calls).
Browse files Browse the repository at this point in the history
  • Loading branch information
acaudwell committed Sep 10, 2012
1 parent d431f52 commit 9db0ae4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
32 changes: 22 additions & 10 deletions timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
GLTimer::GLTimer() {
query_id = 0;
query_value = 0;
running = false;
query_start = 0;
query_stop = 0;
cpu_time = 0;
}

GLTimer::GLTimer(const std::string& name) : name(name) {
query_id = 0;
query_value = 0;
running = false;
query_start = 0;
query_stop = 0;
cpu_time = 0;
}

GLTimer::~GLTimer() {
Expand All @@ -24,21 +28,25 @@ void GLTimer::unload() {
glDeleteQueries(1, &query_id);
query_id = 0;
}
running = false;
query_start = query_stop = 0;
}

void GLTimer::start() {
if(running) return;
if(query_start > 0) return;

query_start = SDL_GetTicks();

if(!query_id) glGenQueries( 1, &query_id );

glBeginQuery(GL_TIME_ELAPSED, query_id);

running = true;
query_stop = 0;
}

void GLTimer::stop() {
if(running) glEndQuery(GL_TIME_ELAPSED);
if(!query_start || query_stop > 0) return;
glEndQuery(GL_TIME_ELAPSED);
query_stop = SDL_GetTicks();
}

const std::string& GLTimer::getName() const {
Expand All @@ -49,12 +57,16 @@ GLuint64 GLTimer::getValue() const {
return query_value;
}

int GLTimer::getMillis() const {
Uint32 GLTimer::getGLMillis() const {
return query_value / 1000000;
}

Uint32 GLTimer::getCPUMillis() const {
return cpu_time;
}

bool GLTimer::check() {
if(!running) return false;
if(!query_start) return false;

GLuint64 elapsed;
GLint available = 0;
Expand All @@ -66,8 +78,8 @@ bool GLTimer::check() {
glGetQueryObjectui64v(query_id, GL_QUERY_RESULT, &elapsed);

query_value = elapsed;

running = false;
cpu_time = query_stop-query_start;
query_start = query_stop = 0;

return true;
}
10 changes: 6 additions & 4 deletions timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
#include "gl.h"

class GLTimer {

Uint32 query_start;
Uint32 query_stop;
Uint32 cpu_time;
GLuint64 query_value;
GLuint query_id;
std::string name;
bool running;
public:
GLTimer();
GLTimer(const std::string& name);
Expand All @@ -22,8 +23,9 @@ class GLTimer {

const std::string& getName() const;
GLuint64 getValue() const;
int getMillis() const;

Uint32 getGLMillis() const;
Uint32 getCPUMillis() const;

void unload();
};

Expand Down

0 comments on commit 9db0ae4

Please sign in to comment.