Calculate the limit delay once and for all
[experiments/fps-limit.git] / fps-limit.c
index a5d00a7..2c24807 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <stdio.h>
-#include <string.h>
-#include <time.h>
-
-#ifdef DEBUG
-#define dbg(...)                     \
-       do {                         \
-               printf(__VA_ARGS__); \
-               printf("\n");        \
-               fflush(stdout);      \
-       } while(0)
-#else
-#define dbg(...) do {} while(0)
-#endif
+#include "fps-limit.h"
 
 #define FPS 60
 
 
 #define FPS 60
 
-#define NSEC_PER_SEC 1e9
-
-#define timespecsub(a, b, result)                                \
-       do {                                                     \
-               (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;    \
-               (result)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec; \
-               if ((result)->tv_nsec < 0) {                     \
-                       --(result)->tv_sec;                      \
-                       (result)->tv_nsec += 1e9;                \
-               }                                                \
-       } while(0)
-
-struct fps_limit_stats {
-       struct timespec time_start;
-       struct timespec time_end;
-       unsigned int fps;
-};
-
-static void fps_limit_init(struct fps_limit_stats *stats, unsigned int fps)
-{
-       memset(stats, 0, sizeof(*stats));
-
-       clock_gettime(CLOCK_MONOTONIC, &stats->time_start);
-       dbg("Init time: s: %ld, ns: %ld", stats->time_start.tv_sec, stats->time_start.tv_nsec);
-
-       stats->fps = fps;
-}
-
-static void fps_limit_sleep(struct fps_limit_stats *stats)
-{
-       struct timespec elapsed;
-
-       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, &elapsed);
-       dbg("Elapsed s: %ld ns: %ld", elapsed.tv_sec, elapsed.tv_nsec);
-
-       if (elapsed.tv_sec == 0 && elapsed.tv_nsec < NSEC_PER_SEC / stats->fps) {
-               struct timespec remaining = {0, 0};
-
-               /* remaining delay, _relative_ to time_end */
-               remaining.tv_nsec = (NSEC_PER_SEC / stats->fps) - elapsed.tv_nsec;
-               dbg("Sleeping for: ns: %ld", remaining.tv_nsec);
-
-               clock_nanosleep(CLOCK_MONOTONIC, 0, &remaining, NULL);
-       }
-
-       /* update the stats for the next iteration */
-       clock_gettime(CLOCK_MONOTONIC, &stats->time_start);
-}
-
 int main(void)
 {
        struct fps_limit_stats stats;
 int main(void)
 {
        struct fps_limit_stats stats;
@@ -95,11 +28,14 @@ int main(void)
 
        fps_limit_init(&stats, FPS);
        while (1) {
 
        fps_limit_init(&stats, FPS);
        while (1) {
-               dbg("Doing some work, max: %d", NSEC_PER_SEC / FPS);
+               fps_limit_dbg("Doing some work, minimum: %dns", NSEC_PER_SEC / FPS);
+
+               /* simulate a shorter workload */
                tmp.tv_sec = 0;
                tmp.tv_nsec = (NSEC_PER_SEC / FPS) * 2 / 3;
                nanosleep(&tmp, NULL);
 
                tmp.tv_sec = 0;
                tmp.tv_nsec = (NSEC_PER_SEC / FPS) * 2 / 3;
                nanosleep(&tmp, NULL);
 
+               /* and wait for the delay imposed by the limit */
                fps_limit_sleep(&stats);
        }
 
                fps_limit_sleep(&stats);
        }