Updated timer test and fixed performance counter on Mac OS X

This commit is contained in:
Sam Lantinga 2013-06-23 22:19:38 -07:00
parent 7827720b8b
commit 75894c0d8b
2 changed files with 20 additions and 20 deletions

View file

@ -66,10 +66,10 @@ SDL_StartTicks(void)
has_monotonic_time = SDL_TRUE; has_monotonic_time = SDL_TRUE;
} else } else
#elif defined(__APPLE__) #elif defined(__APPLE__)
start_mach = mach_absolute_time();
kern_return_t ret = mach_timebase_info(&mach_base_info); kern_return_t ret = mach_timebase_info(&mach_base_info);
if (ret == 0) { if (ret == 0) {
has_monotonic_time = SDL_TRUE; has_monotonic_time = SDL_TRUE;
start_mach = mach_absolute_time();
} else } else
#endif #endif
{ {
@ -85,12 +85,11 @@ SDL_GetTicks(void)
#if HAVE_CLOCK_GETTIME #if HAVE_CLOCK_GETTIME
struct timespec now; struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now); clock_gettime(CLOCK_MONOTONIC, &now);
ticks = ticks = (now.tv_sec - start_ts.tv_sec) * 1000 + (now.tv_nsec -
(now.tv_sec - start_ts.tv_sec) * 1000 + (now.tv_nsec -
start_ts.tv_nsec) / 1000000; start_ts.tv_nsec) / 1000000;
#elif defined(__APPLE__) #elif defined(__APPLE__)
uint64_t now = mach_absolute_time(); uint64_t now = mach_absolute_time();
ticks = (now - start_mach) * mach_base_info.numer / mach_base_info.denom / 1000000; ticks = (((now - start_mach) * mach_base_info.numer) / mach_base_info.denom) / 1000000;
#endif #endif
} else { } else {
struct timeval now; struct timeval now;
@ -136,7 +135,10 @@ SDL_GetPerformanceFrequency(void)
#if HAVE_CLOCK_GETTIME #if HAVE_CLOCK_GETTIME
return 1000000000; return 1000000000;
#elif defined(__APPLE__) #elif defined(__APPLE__)
return mach_base_info.denom / mach_base_info.numer * 1000000; Uint64 freq = mach_base_info.numer;
freq *= 1000000000;
freq /= mach_base_info.denom;
return freq;
#endif #endif
} else { } else {
return 1000000; return 1000000;

View file

@ -14,16 +14,6 @@
platform platform
*/ */
#if 1 /* FIXME: Rework this using the 2.0 API */
#include <stdio.h>
#include "SDL.h"
int main(int argc, char *argv[])
{
printf("FIXME\n");
return 0;
}
#else
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
@ -34,7 +24,7 @@ int main(int argc, char *argv[])
static int ticks = 0; static int ticks = 0;
static Uint32 SDLCALL static Uint32 SDLCALL
ticktock(Uint32 interval) ticktock(Uint32 interval, void *param)
{ {
++ticks; ++ticks;
return (interval); return (interval);
@ -52,6 +42,7 @@ main(int argc, char *argv[])
{ {
int i, desired; int i, desired;
SDL_TimerID t1, t2, t3; SDL_TimerID t1, t2, t3;
Uint32 start32, now32;
Uint64 start, now; Uint64 start, now;
if (SDL_Init(SDL_INIT_TIMER) < 0) { if (SDL_Init(SDL_INIT_TIMER) < 0) {
@ -67,14 +58,14 @@ main(int argc, char *argv[])
if (desired == 0) { if (desired == 0) {
desired = DEFAULT_RESOLUTION; desired = DEFAULT_RESOLUTION;
} }
SDL_SetTimer(desired, ticktock); t1 = SDL_AddTimer(desired, ticktock, NULL);
/* Wait 10 seconds */ /* Wait 10 seconds */
printf("Waiting 10 seconds\n"); printf("Waiting 10 seconds\n");
SDL_Delay(10 * 1000); SDL_Delay(10 * 1000);
/* Stop the timer */ /* Stop the timer */
SDL_SetTimer(0, NULL); SDL_RemoveTimer(t1);
/* Print the results */ /* Print the results */
if (ticks) { if (ticks) {
@ -109,14 +100,21 @@ main(int argc, char *argv[])
start = SDL_GetPerformanceCounter(); start = SDL_GetPerformanceCounter();
for (i = 0; i < 1000000; ++i) { for (i = 0; i < 1000000; ++i) {
ticktock(0); ticktock(0, NULL);
} }
now = SDL_GetPerformanceCounter(); now = SDL_GetPerformanceCounter();
printf("1 million iterations of ticktock took %f ms\n", (double)((now - start)*1000) / SDL_GetPerformanceFrequency()); printf("1 million iterations of ticktock took %f ms\n", (double)((now - start)*1000) / SDL_GetPerformanceFrequency());
printf("Performance counter frequency: %lld\n", SDL_GetPerformanceFrequency());
start32 = SDL_GetTicks();
start = SDL_GetPerformanceCounter();
SDL_Delay(1000);
now = SDL_GetPerformanceCounter();
now32 = SDL_GetTicks();
printf("Delay 1 second = %d ms in ticks, %f ms according to performance counter\n", (now32-start32), (double)((now - start)*1000) / SDL_GetPerformanceFrequency());
SDL_Quit(); SDL_Quit();
return (0); return (0);
} }
#endif
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */