问题在于:我试图根据IOCTL sample skeleton在内核模式WDM驱动程序中计算CPU频率,但如果我尝试使用QueryPerformanceFrequency或者它不想编译QueryPerformanceCounter的。我觉得可能是因为它的内核模式驱动程序不允许我包含Windows.h,其中包含这些方法 - it forces me to use Ntddk.h instead。
通常我会在Win32控制台应用程序中使用此功能来计算此信息,但由于它不会影响Intel CPU上的Turbo Boost,因此不准确:
float GetCoreFrequency()
{
// __rdtsc: Returns the processor time stamp which records the number of clock cycles since the last reset.
// QueryPerformanceCounter: Returns a high resolution time stamp that can be used for time-interval measurements.
// Get the frequency which defines the step size of the QueryPerformanceCounter method.
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
// Get the number of cycles before we start.
ULONG cyclesBefore = __rdtsc();
// Get the start time.
LARGE_INTEGER startTime;
QueryPerformanceCounter(&startTime);
ULONG cyclesAfter;
LARGE_INTEGER endTime;
// Get the number of cycles over some arbitrary but short enough period of time.
do
{
cyclesAfter = __rdtsc();
QueryPerformanceCounter(&endTime);
} while ((endTime.QuadPart - startTime.QuadPart) / frequency.QuadPart < 1);
// Return the number of MHz.
return (cyclesAfter - cyclesBefore)*pow(10, -6) / ((endTime.QuadPart - startTime.QuadPart) / frequency.QuadPart);
}
actual formula for CPU frequency on Intel chips是最大频率乘以可从芯片本身的两个CPU寄存器(APERF和MPERF)获得的比率:
frequency = maximumFrequency * APERF / MPERF;
转换为此代码:
frequency = maximumFrequency * __readmsr(232) / __readmsr(231);
我希望能够从QueryPerformanceFrequency获得最大频率,但由于我无法将其包括在内,我如何才能获得最大频率?