Windows CPU、内存、磁盘、使用率计算

Windows CPU、内存、磁盘、使用率计算

原创作品,转载请标明出处。

编译环境:

  • Visual Studio 2017
  • gcc version 4.8.3 (GCC) (使用c99标准:-std=c99)

调用Win32 API:

cpu使用率:

C代码:

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
#include 
#include 
#include 

typedef unsigned long long uint64;

void perrno(const char *info)
{
    printf("%s errno: %d\n", info, GetLastError());
    exit(EXIT_FAILURE);
}

uint64 CompareFileTime2(FILETIME * lpFileTime1, FILETIME * lpFileTime2)
{
    return ((uint64)lpFileTime2->dwHighDateTime << 32 | lpFileTime2->dwLowDateTime) - ((uint64)lpFileTime1->dwHighDateTime << 32 | lpFileTime1->dwLowDateTime);
}

int GetCpuUsage(uint64 *used, uint64 *total, uint64 *percent)
{
    if (used == NULL || total == NULL || percent == NULL)
        return -1;

    FILETIME idle_1, kernel_1, user_1, idle_2, kernel_2, user_2;
    uint64 idle, kernel, user;

    if (!GetSystemTimes(&idle_1, &kernel_1, &user_1))
        perrno("GetSystemTimes");

    Sleep(1000);

    if (!GetSystemTimes(&idle_2, &kernel_2, &user_2))
        perrno("GetSystemTimes");

    idle = CompareFileTime2(&idle_1, &idle_2);
    user = CompareFileTime2(&user_1, &user_2);
    kernel = CompareFileTime2(&kernel_1, &kernel_2);

    *total = kernel + user;
    if (*total == 0)
        *total = 1;

    *used = *total - idle;

    *percent = *used * 100 / *total;

    return 0;
}

int main()
{
    uint64 used, total, percent;

    for (size_t i = 0; i < 10; i++)
    {
        if (GetCpuUsage(&used, &total, &percent))
            printf("GetCpuUsage");

        fprintf(stdout, "percent:%lld\n", percent);
    }

    system("pause");
    return EXIT_SUCCESS;
}

memory使用率

C代码:

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
#include 
#include 
#include 

typedef unsigned long long uint64;

void perrno(const char *info)
{
    printf("%s errno: %d\n", info, GetLastError());
    exit(EXIT_FAILURE);
}

int GetMemoryUsage(uint64 *used, uint64 *total, uint64 *percent)
{
    if (used == NULL || total == NULL || percent == NULL)
        return -1;

    MEMORYSTATUSEX state;
    state.dwLength = sizeof(state);

    if (!GlobalMemoryStatusEx(&state))
        perrno("GlobalMemoryStatusEx");

    *used = state.ullTotalPhys - state.ullAvailPhys;
    *total = state.ullTotalPhys;
    *percent = state.dwMemoryLoad;

    return 0;
}

int main()
{
    uint64 used, total, percent;

    for (size_t i = 0; i < 10; i++)
    {
        if (GetMemoryUsage(&used, &total, &percent))
            printf("GetMemoryUsage");

        fprintf(stdout, "percent:%lld\n", percent);
        Sleep(1000);
    }

    system("pause");
    return EXIT_SUCCESS;
}

disk使用率(非IO使用率)

C代码

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
#include 
#include 
#include 

typedef unsigned long long uint64;

void perrno(const char *info)
{
    printf("%s errno: %d\n", info, GetLastError());
    exit(EXIT_FAILURE);
}

int GetDiskUsage(uint64 *used, uint64 *total, uint64 *percent)
{
    if (used == NULL || total == NULL || percent == NULL)
        return -1;

    char buf[MAX_PATH] = { 0 };
    DWORD ret = GetLogicalDriveStringsA(MAX_PATH - 1, buf);
    if (ret < 0)
        return -1;

    ULARGE_INTEGER qwFreeBytesToCaller, qwTotalBytes, qwFreeBytes;
    for (size_t i = 0; i < ret; i+=strlen(buf) + 1)
    {
        if (!GetDiskFreeSpaceExA(buf + i, &qwFreeBytesToCaller, &qwTotalBytes, &qwFreeBytes))
            return -1;

        *total += qwTotalBytes.QuadPart;
        *used += qwTotalBytes.QuadPart - qwFreeBytes.QuadPart;
    }

    if (*total == 0)
        *total = 1;

    *percent = *used * 100 / *total;

    return 0;
}

int main()
{
    uint64 used = 0, total = 0, percent = 0;

    for (size_t i = 0; i < 10; i++)
    {
        if (GetDiskUsage(&used, &total, &percent))
            perrno("GetDiskUsage");

        fprintf(stdout, "percent:%llu\n", percent);
        Sleep(1000);
    }

    system("pause");
    return EXIT_SUCCESS;
}

你可能感兴趣的:(Windows CPU、内存、磁盘、使用率计算)