Windows C++ 로 시스템 CPU 사용률 출력하기

프로그래밍/C, C++|2021. 4. 22. 08:53
반응형

#include <Windows.h>

#include <iostream>

using namespace std;

static float CalculateCPULoad();

static unsigned long long FileTimeToInt64();

float GetCPULoad();

int main()

{

    printf("%.1f", GetCPULoad()); // 소수점 1자리까지 출력

    return 0;

}

static float CalculateCPULoad(unsigned long long idleTicks, unsigned long long totalTicks)

{

    static unsigned long long _previousTotalTicks = 0;

    static unsigned long long _previousIdleTicks = 0;

    unsigned long long totalTicksSinceLastTime = totalTicks - _previousTotalTicks;

    unsigned long long idleTicksSinceLastTime = idleTicks - _previousIdleTicks;

    float ret = 1.0f - ((totalTicksSinceLastTime > 0) ? ((float)idleTicksSinceLastTime) / totalTicksSinceLastTime : 0);

    _previousTotalTicks = totalTicks;

    _previousIdleTicks = idleTicks;

    return ret;

}

static unsigned long long FileTimeToInt64(const FILETIME& ft) { return (((unsigned long long)(ft.dwHighDateTime)) << 32) | ((unsigned long long)ft.dwLowDateTime); }

// Returns 1.0f for "CPU fully pinned", 0.0f for "CPU idle", or somewhere in between

// You'll need to call this at regular intervals, since it measures the load between

// the previous call and the current one.  Returns -1.0 on error.

float GetCPULoad()

{

    FILETIME idleTime, kernelTime, userTime;

    return GetSystemTimes(&idleTime, &kernelTime, &userTime) ? CalculateCPULoad(FileTimeToInt64(idleTime), FileTimeToInt64(kernelTime) + FileTimeToInt64(userTime)) : -1.0f;

}

 

[출처] https://stackoverflow.com/questions/23143693/retrieving-cpu-load-percent-total-in-windows-with-c

 

반응형

댓글()