Updated timer test and fixed performance counter on Mac OS X
authorSam Lantinga <slouken@libsdl.org>
Sun, 23 Jun 2013 22:19:38 -0700
changeset 7316c3052ed2c310
parent 7315 417fa81cf17e
child 7317 37b77749523b
Updated timer test and fixed performance counter on Mac OS X
src/timer/unix/SDL_systimer.c
test/testtimer.c
     1.1 --- a/src/timer/unix/SDL_systimer.c	Sun Jun 23 15:00:23 2013 -0700
     1.2 +++ b/src/timer/unix/SDL_systimer.c	Sun Jun 23 22:19:38 2013 -0700
     1.3 @@ -66,10 +66,10 @@
     1.4          has_monotonic_time = SDL_TRUE;
     1.5      } else
     1.6  #elif defined(__APPLE__)
     1.7 -    start_mach = mach_absolute_time();
     1.8      kern_return_t ret = mach_timebase_info(&mach_base_info);
     1.9      if (ret == 0) {
    1.10          has_monotonic_time = SDL_TRUE;
    1.11 +        start_mach = mach_absolute_time();
    1.12      } else
    1.13  #endif
    1.14      {
    1.15 @@ -85,12 +85,11 @@
    1.16  #if HAVE_CLOCK_GETTIME
    1.17          struct timespec now;
    1.18          clock_gettime(CLOCK_MONOTONIC, &now);
    1.19 -        ticks =
    1.20 -            (now.tv_sec - start_ts.tv_sec) * 1000 + (now.tv_nsec -
    1.21 +        ticks = (now.tv_sec - start_ts.tv_sec) * 1000 + (now.tv_nsec -
    1.22                                                   start_ts.tv_nsec) / 1000000;
    1.23  #elif defined(__APPLE__)
    1.24          uint64_t now = mach_absolute_time();
    1.25 -        ticks = (now - start_mach) * mach_base_info.numer / mach_base_info.denom / 1000000;
    1.26 +        ticks = (((now - start_mach) * mach_base_info.numer) / mach_base_info.denom) / 1000000;
    1.27  #endif
    1.28      } else {
    1.29          struct timeval now;
    1.30 @@ -136,7 +135,10 @@
    1.31  #if HAVE_CLOCK_GETTIME
    1.32          return 1000000000;
    1.33  #elif defined(__APPLE__)
    1.34 -        return mach_base_info.denom / mach_base_info.numer * 1000000;
    1.35 +        Uint64 freq = mach_base_info.numer;
    1.36 +        freq *= 1000000000;
    1.37 +        freq /= mach_base_info.denom;
    1.38 +        return freq;
    1.39  #endif
    1.40      } else {
    1.41          return 1000000;
     2.1 --- a/test/testtimer.c	Sun Jun 23 15:00:23 2013 -0700
     2.2 +++ b/test/testtimer.c	Sun Jun 23 22:19:38 2013 -0700
     2.3 @@ -14,16 +14,6 @@
     2.4     platform
     2.5  */
     2.6  
     2.7 -#if 1 /* FIXME: Rework this using the 2.0 API */
     2.8 -#include <stdio.h>
     2.9 -#include "SDL.h"
    2.10 -
    2.11 -int main(int argc, char *argv[])
    2.12 -{
    2.13 -    printf("FIXME\n");
    2.14 -    return 0;
    2.15 -}
    2.16 -#else
    2.17  #include <stdlib.h>
    2.18  #include <stdio.h>
    2.19  
    2.20 @@ -34,7 +24,7 @@
    2.21  static int ticks = 0;
    2.22  
    2.23  static Uint32 SDLCALL
    2.24 -ticktock(Uint32 interval)
    2.25 +ticktock(Uint32 interval, void *param)
    2.26  {
    2.27      ++ticks;
    2.28      return (interval);
    2.29 @@ -52,6 +42,7 @@
    2.30  {
    2.31      int i, desired;
    2.32      SDL_TimerID t1, t2, t3;
    2.33 +    Uint32 start32, now32;
    2.34      Uint64 start, now;
    2.35  
    2.36      if (SDL_Init(SDL_INIT_TIMER) < 0) {
    2.37 @@ -67,14 +58,14 @@
    2.38      if (desired == 0) {
    2.39          desired = DEFAULT_RESOLUTION;
    2.40      }
    2.41 -    SDL_SetTimer(desired, ticktock);
    2.42 +    t1 = SDL_AddTimer(desired, ticktock, NULL);
    2.43  
    2.44      /* Wait 10 seconds */
    2.45      printf("Waiting 10 seconds\n");
    2.46      SDL_Delay(10 * 1000);
    2.47  
    2.48      /* Stop the timer */
    2.49 -    SDL_SetTimer(0, NULL);
    2.50 +    SDL_RemoveTimer(t1);
    2.51  
    2.52      /* Print the results */
    2.53      if (ticks) {
    2.54 @@ -109,14 +100,21 @@
    2.55  
    2.56      start = SDL_GetPerformanceCounter();
    2.57      for (i = 0; i < 1000000; ++i) {
    2.58 -        ticktock(0);
    2.59 +        ticktock(0, NULL);
    2.60      }
    2.61      now = SDL_GetPerformanceCounter();
    2.62      printf("1 million iterations of ticktock took %f ms\n", (double)((now - start)*1000) / SDL_GetPerformanceFrequency());
    2.63  
    2.64 +    printf("Performance counter frequency: %lld\n", SDL_GetPerformanceFrequency());
    2.65 +    start32 = SDL_GetTicks();
    2.66 +    start = SDL_GetPerformanceCounter();
    2.67 +    SDL_Delay(1000);
    2.68 +    now = SDL_GetPerformanceCounter();
    2.69 +    now32 = SDL_GetTicks();
    2.70 +    printf("Delay 1 second = %d ms in ticks, %f ms according to performance counter\n", (now32-start32), (double)((now - start)*1000) / SDL_GetPerformanceFrequency());
    2.71 +
    2.72      SDL_Quit();
    2.73      return (0);
    2.74  }
    2.75 -#endif
    2.76  
    2.77  /* vi: set ts=4 sw=4 expandtab: */