From: Antonio Ospite Date: Sun, 14 Jul 2013 10:53:36 +0000 (+0200) Subject: Factor out some more useful functions X-Git-Url: https://git.ao2.it/experiments/fps-meter.git/commitdiff_plain/d73b9869570111a3249139508e43f3f722adad2e Factor out some more useful functions --- diff --git a/fps-meter.c b/fps-meter.c index c66cb69..0de937e 100644 --- a/fps-meter.c +++ b/fps-meter.c @@ -18,6 +18,7 @@ */ #include +#include #include #ifdef DEBUG @@ -43,47 +44,66 @@ } \ } while(0) -int main(void) -{ +struct fps_meter_stats { struct timespec time_start; struct timespec time_end; struct timespec elapsed; - struct timespec tmp; + unsigned int frames; + double nsecs; +}; - unsigned int frames = 0; - double nsecs = 0; - float fps; +static void fps_meter_init(struct fps_meter_stats *stats) +{ + memset(stats, 0, sizeof(*stats)); - while (1) { - clock_gettime(CLOCK_MONOTONIC, &time_start); - dbg("Start time: s: %ld, ns: %ld", time_start.tv_sec, time_start.tv_nsec); + clock_gettime(CLOCK_MONOTONIC, &stats->time_start); + dbg("Init time: s: %ld, ns: %ld", stats->time_start.tv_sec, stats->time_start.tv_nsec); +} + +static void fps_meter_update(struct fps_meter_stats *stats) +{ + dbg("Start time: s: %ld, ns: %ld", stats->time_start.tv_sec, stats->time_start.tv_nsec); + + clock_gettime(CLOCK_MONOTONIC, &stats->time_end); + dbg("End time: s: %ld, ns: %ld", stats->time_end.tv_sec, stats->time_end.tv_nsec); + + timespecsub(&stats->time_end, &stats->time_start, &stats->elapsed); + dbg("Elapsed s: %ld ns: %ld", stats->elapsed.tv_sec, stats->elapsed.tv_nsec); + stats->frames++; + stats->nsecs += (stats->elapsed.tv_sec * NSEC_PER_SEC + stats->elapsed.tv_nsec); + if (stats->nsecs >= NSEC_PER_SEC) { + /* + * if we were garanteed that each frame took less than + * a second, then just printing 'frames' would be enough here, + * but if we want to cover the case when a frame may take more + * than a second, some calculations have to be done. + */ + float fps = stats->frames / (stats->nsecs / NSEC_PER_SEC); + printf("(frames: %d, nsecs: %f) FPS: %.2f\n", stats->frames, stats->nsecs, fps); + stats->nsecs = 0; + stats->frames = 0; + } + + /* update the stats for the next iteration */ + clock_gettime(CLOCK_MONOTONIC, &stats->time_start); +} + +int main(void) +{ + struct fps_meter_stats stats; + struct timespec tmp; + + fps_meter_init(&stats); + while (1) { /* simulate some workload of about 60fps */ dbg("Doing some work, which takes about %d ns", NSEC_PER_SEC / 60); tmp.tv_sec = 0; tmp.tv_nsec = NSEC_PER_SEC / 60; clock_nanosleep(CLOCK_MONOTONIC, 0, &tmp, NULL); - clock_gettime(CLOCK_MONOTONIC, &time_end); - dbg("End time: s: %ld, ns: %ld", time_end.tv_sec, time_end.tv_nsec); - timespecsub(&time_end, &time_start, &elapsed); - dbg("Elapsed s: %ld ns: %ld", elapsed.tv_sec, elapsed.tv_nsec); - - frames++; - nsecs += (elapsed.tv_sec * NSEC_PER_SEC + elapsed.tv_nsec); - if (nsecs >= NSEC_PER_SEC) { - /* - * if each frame takes less than a second, then just - * printing 'frames' is enough, but if we want to - * cover the case when a frame may take more than - * a second, some calculations have to be done. - */ - fps = frames / (nsecs / NSEC_PER_SEC); - printf("(frames: %d, nsecs: %f) FPS: %.2f\n", frames, nsecs, fps); - nsecs = 0; - frames = 0; - } + fps_meter_update(&stats); } return 0;