Factor out some more useful functions
[experiments/fps-meter.git] / fps-meter.c
1 /*
2  * fps-meter - Example program about how to measure frames per seconds
3  *
4  * Copyright (C) 2013  Antonio Ospite <ospite@studenti.unina.it>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <time.h>
23
24 #ifdef DEBUG
25 #define dbg(...)                     \
26         do {                         \
27                 printf(__VA_ARGS__); \
28                 printf("\n");        \
29                 fflush(stdout);      \
30         } while(0)
31 #else
32 #define dbg(...) do {} while(0)
33 #endif
34
35 #define NSEC_PER_SEC 1000000000
36
37 #define timespecsub(a, b, result)                                \
38         do {                                                     \
39                 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;    \
40                 (result)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec; \
41                 if ((result)->tv_nsec < 0) {                     \
42                         --(result)->tv_sec;                      \
43                         (result)->tv_nsec += 1000000000;         \
44                 }                                                \
45         } while(0)
46
47 struct fps_meter_stats {
48         struct timespec time_start;
49         struct timespec time_end;
50         struct timespec elapsed;
51
52         unsigned int frames;
53         double nsecs;
54 };
55
56 static void fps_meter_init(struct fps_meter_stats *stats)
57 {
58         memset(stats, 0, sizeof(*stats));
59
60         clock_gettime(CLOCK_MONOTONIC, &stats->time_start);
61         dbg("Init time: s: %ld, ns: %ld", stats->time_start.tv_sec, stats->time_start.tv_nsec);
62 }
63
64 static void fps_meter_update(struct fps_meter_stats *stats)
65 {
66         dbg("Start time: s: %ld, ns: %ld", stats->time_start.tv_sec, stats->time_start.tv_nsec);
67
68         clock_gettime(CLOCK_MONOTONIC, &stats->time_end);
69         dbg("End time: s: %ld, ns: %ld", stats->time_end.tv_sec, stats->time_end.tv_nsec);
70
71         timespecsub(&stats->time_end, &stats->time_start, &stats->elapsed);
72         dbg("Elapsed s: %ld ns: %ld", stats->elapsed.tv_sec, stats->elapsed.tv_nsec);
73
74         stats->frames++;
75         stats->nsecs += (stats->elapsed.tv_sec * NSEC_PER_SEC + stats->elapsed.tv_nsec);
76         if (stats->nsecs >= NSEC_PER_SEC) {
77                 /* 
78                  * if we were garanteed that each frame took less than
79                  * a second, then just printing 'frames' would be enough here,
80                  * but if we want to cover the case when a frame may take more
81                  * than a second, some calculations have to be done.
82                  */
83                 float fps = stats->frames / (stats->nsecs / NSEC_PER_SEC);
84                 printf("(frames: %d, nsecs: %f) FPS: %.2f\n", stats->frames, stats->nsecs, fps);
85                 stats->nsecs = 0;
86                 stats->frames = 0;
87         }
88
89         /* update the stats for the next iteration */
90         clock_gettime(CLOCK_MONOTONIC, &stats->time_start);
91 }
92
93 int main(void)
94 {
95         struct fps_meter_stats stats;
96         struct timespec tmp;
97
98         fps_meter_init(&stats);
99         while (1) {
100                 /* simulate some workload of about 60fps */
101                 dbg("Doing some work, which takes about %d ns", NSEC_PER_SEC / 60);
102                 tmp.tv_sec = 0;
103                 tmp.tv_nsec = NSEC_PER_SEC / 60;
104                 clock_nanosleep(CLOCK_MONOTONIC, 0, &tmp, NULL);
105
106                 fps_meter_update(&stats);
107         }
108
109         return 0;
110 }