Skip to content

Commit

Permalink
winapi: Allow dynamic calculation of CPU frequency for QueryPerforman…
Browse files Browse the repository at this point in the history
…ceFrequency
  • Loading branch information
GXTX committed Oct 16, 2023
1 parent 64f6505 commit 3faa575
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions lib/winapi/profiling.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// SPDX-FileCopyrightText: 2019 Stefan Schmidt

#include <profileapi.h>
#ifdef USE_RDTSC_FOR_FREQ
#include <synchapi.h>
#endif
#include <assert.h>
#include <stddef.h>
#include <xboxkrnl/xboxkrnl.h>
Expand All @@ -19,6 +22,36 @@ BOOL QueryPerformanceFrequency (LARGE_INTEGER *lpFrequency)
{
assert(lpFrequency != NULL);

#ifdef USE_RDTSC_FOR_FREQ
#define AVG_SET 10
ULARGE_INTEGER f_rdtsc, avg;
ULONG f_ticks = 0;

avg.QuadPart = 0;

for (int i = 0; i < AVG_SET; i++) {
ULARGE_INTEGER s_rdtsc;
ULONG s_ticks;

s_rdtsc.QuadPart = __rdtsc();
s_ticks = KeTickCount;

s_rdtsc.QuadPart -= f_rdtsc.QuadPart;
s_rdtsc.QuadPart /= s_ticks - f_ticks;

f_rdtsc.QuadPart = __rdtsc();
f_ticks = KeTickCount;

// Skip the first result as invalid
if (i)
avg.QuadPart += s_rdtsc.QuadPart;

// If we call rdtsc too fast we'll end up with div by 0
Sleep(10);
}
lpFrequency->QuadPart = (avg.QuadPart / (AVG_SET - 1)) * 1000;
#else
lpFrequency->QuadPart = 733333333;
#endif
return TRUE;
}

0 comments on commit 3faa575

Please sign in to comment.