Convert ticks to 64-bit, added nanosecond precision to the API

Fixes https://github.com/libsdl-org/SDL/issues/5512
Fixes https://github.com/libsdl-org/SDL/issues/6731
This commit is contained in:
Sam Lantinga
2022-12-02 01:17:17 -08:00
parent 764b899a13
commit 8121bbd083
96 changed files with 938 additions and 1243 deletions

View File

@@ -164,7 +164,7 @@ static int s_nNumAxes;
static AxisState *s_arrAxisState;
static int s_iCurrentBinding;
static Uint32 s_unPendingAdvanceTime;
static Uint64 s_unPendingAdvanceTime;
static SDL_bool s_bBindingComplete;
static SDL_Window *window;
@@ -354,7 +354,7 @@ WatchJoystick(SDL_Joystick *joystick)
SDL_Event event;
SDL_Rect dst;
Uint8 alpha = 200, alpha_step = -1;
Uint32 alpha_ticks = 0;
Uint64 alpha_ticks = 0;
SDL_JoystickID nJoystickID;
background_front = LoadTexture(screen, "controllermap.bmp", SDL_FALSE, NULL, NULL);
@@ -409,7 +409,7 @@ WatchJoystick(SDL_Joystick *joystick)
dst.y = s_arrBindingDisplay[iElement].y;
SDL_QueryTexture(marker, NULL, NULL, &dst.w, &dst.h);
if (SDL_GetTicks() - alpha_ticks > 5) {
if (SDL_GetTicks() >= (alpha_ticks + 5)) {
alpha_ticks = SDL_GetTicks();
alpha += alpha_step;
if (alpha == 255) {

View File

@@ -127,10 +127,10 @@ static int SDLCALL adder(void *junk)
static void runAdder(void)
{
Uint32 start, end;
Uint64 start, end;
int T = NThreads;
start = SDL_GetTicks();
start = SDL_GetTicksNS();
threadDone = SDL_CreateSemaphore(0);
@@ -146,9 +146,9 @@ static void runAdder(void)
SDL_DestroySemaphore(threadDone);
end = SDL_GetTicks();
end = SDL_GetTicksNS();
SDL_Log("Finished in %f sec\n", (end - start) / 1000.f);
SDL_Log("Finished in %f sec\n", (end - start) / 1000000000.0);
}
static void RunEpicTest()
@@ -585,7 +585,7 @@ static void RunFIFOTest(SDL_bool lock_free)
SDL_Thread *fifo_thread = NULL;
WriterData writerData[NUM_WRITERS];
ReaderData readerData[NUM_READERS];
Uint32 start, end;
Uint64 start, end;
int i, j;
int grand_total;
char textBuffer[1024];
@@ -601,7 +601,7 @@ static void RunFIFOTest(SDL_bool lock_free)
queue.mutex = SDL_CreateMutex();
}
start = SDL_GetTicks();
start = SDL_GetTicksNS();
#ifdef TEST_SPINLOCK_FIFO
/* Start a monitoring thread */
@@ -646,7 +646,7 @@ static void RunFIFOTest(SDL_bool lock_free)
SDL_WaitThread(readerData[i].thread, NULL);
}
end = SDL_GetTicks();
end = SDL_GetTicksNS();
/* Wait for the FIFO thread */
if (fifo_thread) {
@@ -657,7 +657,7 @@ static void RunFIFOTest(SDL_bool lock_free)
SDL_DestroyMutex(queue.mutex);
}
SDL_Log("Finished in %f sec\n", (end - start) / 1000.f);
SDL_Log("Finished in %f sec\n", (end - start) / 1000000000.0);
SDL_Log("\n");
for (i = 0; i < NUM_WRITERS; ++i) {

View File

@@ -61,11 +61,11 @@ int timer_getPerformanceFrequency(void *arg)
*/
int timer_delayAndGetTicks(void *arg)
{
const Uint32 testDelay = 100;
const Uint32 marginOfError = 25;
Uint32 result;
Uint32 result2;
Uint32 difference;
const int testDelay = 100;
const int marginOfError = 25;
Uint64 result;
Uint64 result2;
Sint64 difference;
/* Zero delay */
SDL_Delay(0);
@@ -81,17 +81,17 @@ int timer_delayAndGetTicks(void *arg)
/* Get ticks count - should be non-zero by now */
result = SDL_GetTicks();
SDLTest_AssertPass("Call to SDL_GetTicks()");
SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %" SDL_PRIu32, result);
SDLTest_AssertCheck(result > 0, "Check result value, expected: >0, got: %" SDL_PRIu64, result);
/* Delay a bit longer and measure ticks and verify difference */
SDL_Delay(testDelay);
SDLTest_AssertPass("Call to SDL_Delay(%" SDL_PRIu32 ")", testDelay);
SDLTest_AssertPass("Call to SDL_Delay(%d)", testDelay);
result2 = SDL_GetTicks();
SDLTest_AssertPass("Call to SDL_GetTicks()");
SDLTest_AssertCheck(result2 > 0, "Check result value, expected: >0, got: %" SDL_PRIu32, result2);
SDLTest_AssertCheck(result2 > 0, "Check result value, expected: >0, got: %" SDL_PRIu64, result2);
difference = result2 - result;
SDLTest_AssertCheck(difference > (testDelay - marginOfError), "Check difference, expected: >%" SDL_PRIu32 ", got: %" SDL_PRIu32, testDelay - marginOfError, difference);
SDLTest_AssertCheck(difference < (testDelay + marginOfError), "Check difference, expected: <%" SDL_PRIu32 ", got: %" SDL_PRIu32, testDelay + marginOfError, difference);
SDLTest_AssertCheck(difference > (testDelay - marginOfError), "Check difference, expected: >%d, got: %" SDL_PRIu64, testDelay - marginOfError, difference);
SDLTest_AssertCheck(difference < (testDelay + marginOfError), "Check difference, expected: <%d, got: %" SDL_PRIu64, testDelay + marginOfError, difference);
return TEST_COMPLETED;
}

View File

@@ -31,8 +31,9 @@ static int cycle_direction = 1;
static int current_alpha = 255;
static int current_color = 255;
static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
static Uint32 next_fps_check, frames;
static const Uint32 fps_check_delay = 5000;
static Uint64 next_fps_check;
static Uint32 frames;
static const int fps_check_delay = 5000;
int done;
@@ -175,7 +176,7 @@ void DrawRects(SDL_Renderer *renderer)
void loop()
{
Uint32 now;
Uint64 now;
int i;
SDL_Event event;
@@ -204,9 +205,9 @@ void loop()
#endif
frames++;
now = SDL_GetTicks();
if (SDL_TICKS_PASSED(now, next_fps_check)) {
if (now >= next_fps_check) {
/* Print out some timing information */
const Uint32 then = next_fps_check - fps_check_delay;
const Uint64 then = next_fps_check - fps_check_delay;
const double fps = ((double)frames * 1000) / (now - then);
SDL_Log("%2.2f frames per second\n", fps);
next_fps_check = now + fps_check_delay;

View File

@@ -164,7 +164,8 @@ int main(int argc, char *argv[])
{
int i;
const char *icon = "icon.bmp";
Uint32 then, now, frames;
Uint64 then, now;
Uint32 frames;
/* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);

View File

@@ -203,7 +203,8 @@ int main(int argc, char *argv[])
int i, done;
SDL_DisplayMode mode;
SDL_Event event;
Uint32 then, now, frames;
Uint64 then, now;
Uint32 frames;
int status;
int dw, dh;
int swap_interval = 0;

View File

@@ -615,7 +615,7 @@ int main(int argc, char *argv[])
int value;
int i;
SDL_DisplayMode mode;
Uint32 then, now;
Uint64 then, now;
int status;
shader_data *data;

View File

@@ -434,7 +434,7 @@ int main(int argc, char *argv[])
int value;
int i;
SDL_DisplayMode mode;
Uint32 then, now;
Uint64 then, now;
int status;
shader_data *data;
char *path = NULL;

View File

@@ -278,7 +278,8 @@ void loop()
int main(int argc, char *argv[])
{
int i;
Uint32 then, now, frames;
Uint64 then, now;
Uint32 frames;
/* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);

View File

@@ -93,7 +93,8 @@ void loop()
int main(int argc, char *argv[])
{
#ifndef __EMSCRIPTEN__
Uint32 then, now, frames;
Uint64 then, now;
Uint32 frames;
#endif
/* Enable standard application logging */

View File

@@ -112,7 +112,7 @@ int main(int argc, char *argv[])
{
int i;
int frames;
Uint32 then, now;
Uint64 then, now;
/* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);

View File

@@ -205,7 +205,7 @@ int main(int argc, char *argv[])
{
int i;
int frames;
Uint32 then, now;
Uint64 then, now;
/* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);

View File

@@ -102,7 +102,7 @@ int main(int argc, char *argv[])
{
int i;
int frames;
Uint32 then, now;
Uint64 then, now;
/* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);

View File

@@ -98,9 +98,9 @@ TestRealWorld(int init_sem)
static void
TestWaitTimeout(void)
{
Uint32 start_ticks;
Uint32 end_ticks;
Uint32 duration;
Uint64 start_ticks;
Uint64 end_ticks;
Uint64 duration;
int retval;
sem = SDL_CreateSemaphore(0);
@@ -114,7 +114,7 @@ TestWaitTimeout(void)
/* Accept a little offset in the effective wait */
SDL_assert(duration > 1900 && duration < 2050);
SDL_Log("Wait took %" SDL_PRIu32 " milliseconds\n\n", duration);
SDL_Log("Wait took %" SDL_PRIu64 " milliseconds\n\n", duration);
/* Check to make sure the return value indicates timed out */
if (retval != SDL_MUTEX_TIMEDOUT) {
@@ -127,9 +127,9 @@ TestWaitTimeout(void)
static void
TestOverheadUncontended(void)
{
Uint32 start_ticks;
Uint32 end_ticks;
Uint32 duration;
Uint64 start_ticks;
Uint64 end_ticks;
Uint64 duration;
int i, j;
sem = SDL_CreateSemaphore(0);
@@ -147,7 +147,7 @@ TestOverheadUncontended(void)
end_ticks = SDL_GetTicks();
duration = end_ticks - start_ticks;
SDL_Log("Took %" SDL_PRIu32 " milliseconds\n\n", duration);
SDL_Log("Took %" SDL_PRIu64 " milliseconds\n\n", duration);
SDL_DestroySemaphore(sem);
}
@@ -179,9 +179,9 @@ ThreadFuncOverheadContended(void *data)
static void
TestOverheadContended(SDL_bool try_wait)
{
Uint32 start_ticks;
Uint32 end_ticks;
Uint32 duration;
Uint64 start_ticks;
Uint64 end_ticks;
Uint64 duration;
Thread_State thread_states[NUM_THREADS] = { { 0 } };
char textBuffer[1024];
int loop_count;
@@ -223,7 +223,7 @@ TestOverheadContended(SDL_bool try_wait)
SDL_assert_release((loop_count - content_count) == NUM_OVERHEAD_OPS * NUM_OVERHEAD_OPS_MULT);
duration = end_ticks - start_ticks;
SDL_Log("Took %" SDL_PRIu32 " milliseconds, threads %s %d out of %d times in total (%.2f%%)\n",
SDL_Log("Took %" SDL_PRIu64 " milliseconds, threads %s %d out of %d times in total (%.2f%%)\n",
duration, try_wait ? "where contended" : "timed out", content_count,
loop_count, ((float)content_count * 100) / loop_count);
/* Print how many semaphores where consumed per thread */

View File

@@ -37,8 +37,9 @@ static SDL_Rect *positions;
static SDL_Rect *velocities;
static int sprite_w, sprite_h;
static SDL_BlendMode blendMode = SDL_BLENDMODE_BLEND;
static Uint32 next_fps_check, frames;
static const Uint32 fps_check_delay = 5000;
static Uint64 next_fps_check;
static Uint32 frames;
static const int fps_check_delay = 5000;
static int use_rendergeometry = 0;
/* Number of iterations to move sprites - used for visual tests. */
@@ -393,7 +394,7 @@ void MoveSprites(SDL_Renderer *renderer, SDL_Texture *sprite)
void loop()
{
Uint32 now;
Uint64 now;
int i;
SDL_Event event;
@@ -415,9 +416,9 @@ void loop()
frames++;
now = SDL_GetTicks();
if (SDL_TICKS_PASSED(now, next_fps_check)) {
if (now >= next_fps_check) {
/* Print out some timing information */
const Uint32 then = next_fps_check - fps_check_delay;
const Uint64 then = next_fps_check - fps_check_delay;
const double fps = ((double)frames * 1000) / (now - then);
SDL_Log("%2.2f frames per second\n", fps);
next_fps_check = now + fps_check_delay;

View File

@@ -37,9 +37,8 @@ int main(int argc, char *argv[])
{
int i, desired;
SDL_TimerID t1, t2, t3;
Uint64 start64, now64;
Uint32 start32, now32;
Uint64 start, now;
Uint64 start_perf, now_perf;
/* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
@@ -58,13 +57,11 @@ int main(int argc, char *argv[])
/* Verify SDL_GetTicks* acts monotonically increasing, and not erratic. */
SDL_Log("Sanity-checking GetTicks\n");
for (i = 0; i < 1000; ++i) {
start64 = SDL_GetTicks64();
start32 = SDL_GetTicks();
start = SDL_GetTicks();
SDL_Delay(1);
now64 = SDL_GetTicks64() - start64;
now32 = SDL_GetTicks() - start32;
if (now32 > 100 || now64 > 100) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "testtimer.c: Delta time erratic at iter %d. Delay 1ms = %d ms in ticks, %d ms in ticks64\n", i, (int)now32, (int)now64);
now = SDL_GetTicks() - start;
if (now > 100) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "testtimer.c: Delta time erratic at iter %d. Delay 1ms = %d ms in ticks\n", i, (int)now);
SDL_Quit();
return 1;
}
@@ -120,22 +117,20 @@ int main(int argc, char *argv[])
SDL_RemoveTimer(t2);
SDL_RemoveTimer(t3);
start = SDL_GetPerformanceCounter();
start_perf = SDL_GetPerformanceCounter();
for (i = 0; i < 1000000; ++i) {
ticktock(0, NULL);
}
now = SDL_GetPerformanceCounter();
SDL_Log("1 million iterations of ticktock took %f ms\n", (double)((now - start) * 1000) / SDL_GetPerformanceFrequency());
now_perf = SDL_GetPerformanceCounter();
SDL_Log("1 million iterations of ticktock took %f ms\n", (double)((now_perf - start_perf) * 1000) / SDL_GetPerformanceFrequency());
SDL_Log("Performance counter frequency: %" SDL_PRIu64 "\n", SDL_GetPerformanceFrequency());
start64 = SDL_GetTicks64();
start32 = SDL_GetTicks();
start = SDL_GetPerformanceCounter();
start = SDL_GetTicks();
start_perf = SDL_GetPerformanceCounter();
SDL_Delay(1000);
now = SDL_GetPerformanceCounter();
now64 = SDL_GetTicks64();
now32 = SDL_GetTicks();
SDL_Log("Delay 1 second = %d ms in ticks, %d ms in ticks64, %f ms according to performance counter\n", (int)(now32 - start32), (int)(now64 - start64), (double)((now - start) * 1000) / SDL_GetPerformanceFrequency());
now_perf = SDL_GetPerformanceCounter();
now = SDL_GetTicks();
SDL_Log("Delay 1 second = %d ms in ticks, %f ms according to performance counter\n", (int)(now - start), (double)((now_perf - start_perf) * 1000) / SDL_GetPerformanceFrequency());
SDL_Quit();
return 0;

View File

@@ -146,7 +146,8 @@ void loop()
int main(int argc, char *argv[])
{
int i;
Uint32 then, now, frames;
Uint64 then, now;
Uint32 frames;
/* Initialize test framework */
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);

View File

@@ -1081,7 +1081,8 @@ int main(int argc, char **argv)
int done;
SDL_DisplayMode mode;
SDL_Event event;
Uint32 then, now, frames;
Uint64 then, now;
Uint32 frames;
int dw, dh;
/* Enable standard application logging */

View File

@@ -254,7 +254,8 @@ int main(int argc, char **argv)
int current = 0;
int pitch;
Uint8 *raw_yuv;
Uint32 then, now, i, iterations = 100;
Uint64 then, now;
Uint32 i, iterations = 100;
SDL_bool should_run_automated_tests = SDL_FALSE;
while (argv[arg] && *argv[arg] == '-') {
@@ -345,7 +346,7 @@ int main(int argc, char **argv)
SDL_ConvertPixels(original->w, original->h, yuv_format, raw_yuv, pitch, rgb_format, converted->pixels, converted->pitch);
}
now = SDL_GetTicks();
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "%" SDL_PRIu32 " iterations in %" SDL_PRIu32 " ms, %.2fms each\n", iterations, (now - then), (float)(now - then) / iterations);
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "%" SDL_PRIu32 " iterations in %" SDL_PRIu64 " ms, %.2fms each\n", iterations, (now - then), (float)(now - then) / iterations);
window = SDL_CreateWindow("YUV test",
SDL_WINDOWPOS_UNDEFINED,