Fix measuring the CPU load on Windows.

This commit is contained in:
Sadie Powell 2024-08-27 14:48:28 +01:00
parent 56b9a4e48e
commit 491d82677d
3 changed files with 12 additions and 15 deletions

View File

@ -127,17 +127,14 @@ public:
size_t Recv = 0;
#ifdef _WIN32
/** Cpu usage at last sample
*/
FILETIME LastCPU;
/** CPU performance frequency on boot. */
LARGE_INTEGER BootCPU;
/** Time QP sample was read
*/
LARGE_INTEGER LastSampled;
/** CPU performance frequency at the last sample. */
LARGE_INTEGER LastCPU;
/** QP frequency
*/
LARGE_INTEGER QPFrequency;
/** Time the last sample was read. */
FILETIME LastSampled;
#else
/** CPU usage at the last sample. */
timeval LastCPU;

View File

@ -309,8 +309,8 @@ void CommandStats::DoStats(Stats::Context& stats)
{
KernelTime.dwHighDateTime += UserTime.dwHighDateTime;
KernelTime.dwLowDateTime += UserTime.dwLowDateTime;
double n_eaten = (double)( ( (uint64_t)(KernelTime.dwHighDateTime - ServerInstance->Stats.LastCPU.dwHighDateTime) << 32 ) + (uint64_t)(KernelTime.dwLowDateTime - ServerInstance->Stats.LastCPU.dwLowDateTime) )/100000;
double n_elapsed = (double)(ThisSample.QuadPart - ServerInstance->Stats.LastSampled.QuadPart) / ServerInstance->Stats.QPFrequency.QuadPart;
double n_eaten = (double)( ( (uint64_t)(KernelTime.dwHighDateTime - ServerInstance->Stats.LastSampled.dwHighDateTime) << 32 ) + (uint64_t)(KernelTime.dwLowDateTime - ServerInstance->Stats.LastSampled.dwLowDateTime) )/100000;
double n_elapsed = (double)(ThisSample.QuadPart - ServerInstance->Stats.LastCPU.QuadPart) / ServerInstance->Stats.BootCPU.QuadPart;
double per = (n_eaten/n_elapsed);
stats.AddRow(249, INSP_FORMAT("CPU Use (now): {:03.5}%", per));

View File

@ -105,7 +105,7 @@ namespace
ServerInstance->Stats.LastSampled.tv_nsec = ServerInstance->Time_ns();
ServerInstance->Stats.LastCPU = ru.ru_utime;
#else
if (!QueryPerformanceCounter(&ServerInstance->Stats.LastSampled))
if (!QueryPerformanceCounter(&ServerInstance->Stats.LastCPU))
return; // Should never happen.
FILETIME CreationTime;
@ -114,8 +114,8 @@ namespace
FILETIME UserTime;
GetProcessTimes(GetCurrentProcess(), &CreationTime, &ExitTime, &KernelTime, &UserTime);
ServerInstance->Stats.LastCPU.dwHighDateTime = KernelTime.dwHighDateTime + UserTime.dwHighDateTime;
ServerInstance->Stats.LastCPU.dwLowDateTime = KernelTime.dwLowDateTime + UserTime.dwLowDateTime;
ServerInstance->Stats.LastSampled.dwHighDateTime = KernelTime.dwHighDateTime + UserTime.dwHighDateTime;
ServerInstance->Stats.LastSampled.dwLowDateTime = KernelTime.dwLowDateTime + UserTime.dwLowDateTime;
#endif
}
@ -588,7 +588,7 @@ InspIRCd::InspIRCd(int argc, char** argv)
FreeConsole();
}
QueryPerformanceFrequency(&this->Stats.QPFrequency);
QueryPerformanceFrequency(&this->Stats.BootCPU);
#endif
WritePID();