Skip to content

Commit

Permalink
winapi: Squash me
Browse files Browse the repository at this point in the history
  • Loading branch information
GXTX committed Oct 17, 2023
1 parent 235dde3 commit 487f296
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions lib/winapi/profiling.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT

// SPDX-FileCopyrightText: 2019 Stefan Schmidt

#define USE_RDTSC_FOR_FREQ
#include <profileapi.h>
#ifdef USE_RDTSC_FOR_FREQ
#include <synchapi.h>
Expand All @@ -11,33 +11,32 @@
#include <xboxkrnl/xboxkrnl.h>

#ifdef USE_RDTSC_FOR_FREQ
static LARGE_INTEGER frequency = {0, 0};
static LARGE_INTEGER frequency = {{0, 0}};
static void __attribute__((constructor)) PrimeQueryPerformanceFrequency ()
{
#define AVG_SET 2
ULARGE_INTEGER f_rdtsc, avg = {0, 0}, s_rdtsc;
ULARGE_INTEGER f_rdtsc = {{0, 0}}, s_rdtsc = {{0, 0}};
ULONG f_ticks = 0, s_ticks = 0;

Sleep(500);
KeEnterCriticalRegion();

// The values generated after launching aren't accurate, give it time to increment...
Sleep(700);

for (int i = 0; i < AVG_SET; i++) {
// If we call rdtsc too fast we'll end up with div by 0
Sleep(200);
f_rdtsc.QuadPart = __rdtsc();
f_ticks = KeTickCount;

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

s_rdtsc.QuadPart -= f_rdtsc.QuadPart;
s_rdtsc.QuadPart /= s_ticks - f_ticks;
s_rdtsc.QuadPart = __rdtsc();
s_ticks = KeTickCount;

f_rdtsc.QuadPart = __rdtsc();
f_ticks = KeTickCount;
s_rdtsc.QuadPart -= f_rdtsc.QuadPart;
s_rdtsc.QuadPart /= s_ticks - f_ticks;

// Skip the first result as invalid
if (i)
avg.QuadPart += s_rdtsc.QuadPart;
}
frequency.QuadPart = avg.QuadPart / (AVG_SET - 1) * 1000LL;
frequency.QuadPart = s_rdtsc.QuadPart;
frequency.QuadPart *= 1000LL;

KeLeaveCriticalRegion();
}
#endif

Expand All @@ -59,4 +58,4 @@ BOOL QueryPerformanceFrequency (LARGE_INTEGER *lpFrequency)
lpFrequency->QuadPart = 733333333;
#endif
return TRUE;
}
}

0 comments on commit 487f296

Please sign in to comment.