2003-05-21 21:18:29 +00:00
|
|
|
/*
|
2004-08-04 15:48:43 +00:00
|
|
|
* Precise timer routines using Mach timing
|
2003-05-21 21:18:29 +00:00
|
|
|
*
|
2004-08-04 15:48:43 +00:00
|
|
|
* Copyright (c) 2003-2004, Dan Villiom Podlaski Christiansen
|
2003-05-21 21:18:29 +00:00
|
|
|
*
|
2004-08-04 15:48:43 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person
|
|
|
|
* obtaining a copy of this software and associated documentation
|
|
|
|
* files (the "Software"), to deal in the Software without
|
|
|
|
* restriction, including without limitation the rights to use, copy,
|
|
|
|
* modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
|
|
* of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2003-05-21 21:18:29 +00:00
|
|
|
*
|
2004-08-04 15:48:43 +00:00
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
2003-05-21 21:18:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
2011-01-06 12:25:33 +00:00
|
|
|
#include <stdint.h>
|
2003-05-21 21:18:29 +00:00
|
|
|
#include <stdlib.h>
|
2004-08-04 15:48:43 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <sys/time.h>
|
2003-05-21 21:18:29 +00:00
|
|
|
#include <mach/mach_time.h>
|
|
|
|
|
2005-11-14 00:30:37 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "mp_msg.h"
|
2003-05-21 21:18:29 +00:00
|
|
|
#include "timer.h"
|
|
|
|
|
|
|
|
/* global variables */
|
|
|
|
static double timebase_ratio;
|
|
|
|
|
|
|
|
|
2005-04-27 14:55:30 +00:00
|
|
|
/* the core sleep function, uses floats and is used in MPlayer G2 */
|
2010-02-22 21:53:06 +00:00
|
|
|
static float sleep_accurate(float time_frame)
|
2005-04-27 14:55:30 +00:00
|
|
|
{
|
|
|
|
uint64_t deadline = time_frame / timebase_ratio + mach_absolute_time();
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2005-04-27 14:55:30 +00:00
|
|
|
mach_wait_until(deadline);
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2005-04-27 14:55:30 +00:00
|
|
|
return (mach_absolute_time() - deadline) * timebase_ratio;
|
|
|
|
}
|
|
|
|
|
2004-08-04 15:48:43 +00:00
|
|
|
/* wrapper for MPlayer G1 */
|
|
|
|
int usec_sleep(int usec_delay)
|
|
|
|
{
|
2005-04-27 14:55:30 +00:00
|
|
|
return sleep_accurate(usec_delay / 1e6) * 1e6;
|
2003-05-21 21:18:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-04 15:48:43 +00:00
|
|
|
/* current time in microseconds */
|
2009-05-04 17:35:26 +00:00
|
|
|
unsigned int GetTimer(void)
|
2003-05-21 21:18:29 +00:00
|
|
|
{
|
2008-04-29 01:34:58 +00:00
|
|
|
return (unsigned int)(uint64_t)(mach_absolute_time() * timebase_ratio * 1e6);
|
2004-08-04 15:48:43 +00:00
|
|
|
}
|
2003-05-21 21:18:29 +00:00
|
|
|
|
2004-08-04 15:48:43 +00:00
|
|
|
/* current time in milliseconds */
|
2009-05-04 17:35:26 +00:00
|
|
|
unsigned int GetTimerMS(void)
|
2003-05-21 21:18:29 +00:00
|
|
|
{
|
2008-04-29 01:34:58 +00:00
|
|
|
return (unsigned int)(uint64_t)(mach_absolute_time() * timebase_ratio * 1e3);
|
2003-05-21 21:18:29 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 15:48:43 +00:00
|
|
|
/* initialize timer, must be called at least once at start */
|
2009-05-04 17:35:26 +00:00
|
|
|
void InitTimer(void)
|
2003-05-21 21:18:29 +00:00
|
|
|
{
|
|
|
|
struct mach_timebase_info timebase;
|
|
|
|
|
|
|
|
mach_timebase_info(&timebase);
|
2009-07-06 23:26:13 +00:00
|
|
|
timebase_ratio = (double)timebase.numer / (double)timebase.denom
|
2003-05-21 21:18:29 +00:00
|
|
|
* (double)1e-9;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2004-08-04 15:48:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2007-11-21 09:22:33 +00:00
|
|
|
int main(void) {
|
2004-08-04 15:48:43 +00:00
|
|
|
int i,j, r, c = 200;
|
|
|
|
long long t = 0;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2003-05-21 21:18:29 +00:00
|
|
|
InitTimer();
|
|
|
|
|
2004-08-04 15:48:43 +00:00
|
|
|
for (i = 0; i < c; i++) {
|
|
|
|
const int delay = rand() / (RAND_MAX / 1e5);
|
|
|
|
j = GetTimer();
|
|
|
|
#if 1
|
|
|
|
r = usec_sleep(delay);
|
|
|
|
#else
|
|
|
|
r = sleep_accurate(delay / 1e6) * 1e6;
|
|
|
|
#endif
|
|
|
|
j = (GetTimer() - j) - delay;
|
|
|
|
printf("sleep time:%8i %5i (%i)\n", delay, j, j - r);
|
|
|
|
t += j - r;
|
2003-05-21 21:18:29 +00:00
|
|
|
}
|
2004-08-04 15:48:43 +00:00
|
|
|
fprintf(stderr, "average error:\t%lli\n", t / c);
|
2003-05-21 21:18:29 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|