SDL_GetPerformanceFrequency() - Does it ever return a different result?


#1

Hi,

Is it possible that SDL_GetPerformanceFrequency() can return different values at different times during throughout the execution of my runtime? I.e. do any of the platforms that SDL runs on have a variable performance frequency?

I’m asking to know if its a good idea to cache it once at startup.

Cheers,
Ant


#2

On which platforms do you run ?
According to windows documentation

The frequency of the performance counter is fixed at system boot and is consistent across all processors.


#3

Yeah I read that but was wondering about the other platforms. My assumption is it’s the same right now but just wanted to confirm.


#4

Windows was already covered so here’s the SDL code for the other 99.9% of platforms.

Uint64
SDL_GetPerformanceFrequency(void)
{
    if (!ticks_started) {
        SDL_TicksInit();
    }

    if (has_monotonic_time) {
#if HAVE_CLOCK_GETTIME
        return 1000000000;
#elif defined(__APPLE__)
        Uint64 freq = mach_base_info.denom;
        freq *= 1000000000;
        freq /= mach_base_info.numer;
        return freq;
#endif
    } 
        
    return 1000000;
}

mach_base_info gets initialized only once on initialization and can’t change unless the SDL library is somehow reloaded. The Apple documentation doesn’t seem to state it outright, but it’s hinting at the fact that its behavior is the same as Windows. They probably do it on boot too.

The value returned by SDL_GetPerformanceFrequency is not going to change during runtime.

It is possible to configure some systems to use different timer hardware, but I think these usually require a restart.


#5

Thanks for the answers guys! Appreciate the insight.


#6

Having a definite answer to this would still be cool - sure, on Windows
it never changes while the program is running and on the Unix-like
systems it seems to be a compiletime constant anyway (because the
internal timers return real times, usually nanoseconds), but that
doesn’t mean it’s constant on all platforms - unless SDL guarantees it.

Any insight on this, Sam or Ryan (or someone else)?

Cheers,
Daniel


#7

The other platforms are haiku and psp. They both have compile-time constants too.

haiku:

Uint64
SDL_GetPerformanceFrequency(void)
{
    return 1000000;
}

psp:

Uint64
SDL_GetPerformanceFrequency(void)
{
    return 1000;
}

Even the dummy implementation just returns 1000. Obviously, it won’t be of any use there if the counter functions throw SDL_Unsupported.