Add deterministic QueryPerformanceCounter/Frequency mocks

Implemented Windows high-resolution timing mocks for deterministic simulation:

**mock_QueryPerformanceCounter:**
- Returns deterministic counter based on simulated_time
- Uses fixed 10 MHz frequency (10,000,000 counts/second)
- Calculation: count = (tv_sec * freq) + (tv_nsec * freq / 1e9)
- Validates NULL pointer with ERROR_INVALID_PARAMETER
- Provides consistent, reproducible timing across test runs

**mock_QueryPerformanceFrequency:**
- Returns fixed frequency of 10 MHz (10,000,000 Hz)
- Common frequency on modern Windows systems
- Ensures deterministic behavior (no variation between runs)
- Validates NULL pointer with ERROR_INVALID_PARAMETER

**Integration:**
- Added function declarations to system.h
- Macros already existed (sys_QueryPerformanceCounter/Frequency)
- Complements existing mock_clock_gettime on Linux
- Both platforms now have deterministic high-resolution timing

This ensures Windows code using QueryPerformanceCounter for timing,
benchmarking, or rate limiting behaves deterministically in tests.
The fixed 10 MHz frequency provides nanosecond-level precision
(100ns per tick) suitable for most timing needs.
This commit is contained in:
Claude
2025-11-02 10:44:20 +00:00
parent aa4be1e203
commit d959306719
2 changed files with 33 additions and 0 deletions
+31
View File
@@ -1183,6 +1183,37 @@ int mock__mkdir(char *path)
return _mkdir(path);
}
BOOL mock_QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)
{
if (lpPerformanceCount == NULL) {
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
// Use simulated time to generate deterministic performance counter
// Frequency is 10 MHz (10,000,000 counts per second)
const LONGLONG frequency = 10000000LL;
LONGLONG count = (LONGLONG)simulated_time.tv_sec * frequency;
count += ((LONGLONG)simulated_time.tv_nsec * frequency) / 1000000000LL;
lpPerformanceCount->QuadPart = count;
return TRUE;
}
BOOL mock_QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)
{
if (lpFrequency == NULL) {
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
// Return fixed frequency of 10 MHz for deterministic behavior
// This is a common frequency on modern systems
lpFrequency->QuadPart = 10000000LL; // 10 million counts per second
return TRUE;
}
#else
int mock_clock_gettime(clockid_t clockid, struct timespec *tp)