博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[C++]-Windows下获取CPU、内存利用率
阅读量:2055 次
发布时间:2019-04-28

本文共 1998 字,大约阅读时间需要 6 分钟。

CPU利用率

内存利用率

 

在《》中介绍了通过性能计数器获取计算机性能信息,若只是获取CPU利用率及内存利用率,可以直接通过API获取。Windows提供了相关函数来获取:

  • GetSystemTimes:获取CPU的空闲、内核、用户使用时间;

  • GetProcessTimes:获取进程的内核、用户使用时间,以及创建、退出时间等;

  • GlobalMemoryStatusEx:获取内存相关的详细信息;

CPU利用率

GetProcessTimes返回的使用时间是FILETIME格式的,获取CPU利用率只需获得一段时间内空闲时间与使用时间即可方便计算得到。为了得到当前CPU利用率,需要调用两次(间隔一段时间,如1s),然后计算其差值。

其中kernelTime中包含idleTime,所以需要减去后才能得到实际的内核使用时间。

__int64 Filetime2Int64(const FILETIME &ftime){    LARGE_INTEGER li;    li.LowPart = ftime.dwLowDateTime;    li.HighPart = ftime.dwHighDateTime;    return li.QuadPart;}__int64 CompareFileTime2(const FILETIME &preTime, const FILETIME &nowTime){    return Filetime2Int64(nowTime) - Filetime2Int64(preTime);}double getCpuUsage(){    FILETIME preIdleTime;    FILETIME preKernelTime;    FILETIME preUserTime;    GetSystemTimes(&preIdleTime, &preKernelTime, &preUserTime);    Sleep(1000);    FILETIME idleTime;    FILETIME kernelTime;    FILETIME userTime;    GetSystemTimes(&idleTime, &kernelTime, &userTime);    auto idle = CompareFileTime2(preIdleTime, idleTime);    auto kernel = CompareFileTime2(preKernelTime, kernelTime);    auto user = CompareFileTime2(preUserTime, userTime);    if (kernel + user == 0)        return 0;    return 1.0*(kernel + user - idle) / (kernel + user);}

内存利用率

通过GlobalMemoryStatusEx获取内心相关详细信息,信息的详细结构:

typedef struct _MEMORYSTATUSEX {  DWORD     dwLength;  DWORD     dwMemoryLoad;  DWORDLONG ullTotalPhys;  DWORDLONG ullAvailPhys;  DWORDLONG ullTotalPageFile;  DWORDLONG ullAvailPageFile;  DWORDLONG ullTotalVirtual;  DWORDLONG ullAvailVirtual;  DWORDLONG ullAvailExtendedVirtual;} MEMORYSTATUSEX, *LPMEMORYSTATUSEX;

其中dwMemoryLoad即为内存利用率。

const int Byte2MB = 1024 * 1024;void getMemoryRate() {    MEMORYSTATUSEX memStatus;    memStatus.dwLength = sizeof(memStatus);    GlobalMemoryStatusEx(&memStatus);    int nAvail = (int)(memStatus.ullAvailPhys / Byte2MB);    int nTotal = (int)(memStatus.ullTotalPhys / Byte2MB);    cout << "Memory: " << memStatus.dwMemoryLoad << "%, " << nAvail << "/" << nTotal << endl;}

 

转载地址:http://sgnlf.baihongyu.com/

你可能感兴趣的文章
leetcode 热题 Hot 100-4. 对称二叉树
查看>>
Leetcode C++《热题 Hot 100-12》226.翻转二叉树
查看>>
Leetcode C++《热题 Hot 100-13》234.回文链表
查看>>
Leetcode C++《热题 Hot 100-14》283.移动零
查看>>
Leetcode C++《热题 Hot 100-15》437.路径总和III
查看>>
Leetcode C++《热题 Hot 100-16》448.找到所有数组中消失的数字
查看>>
Leetcode C++《热题 Hot 100-17》461.汉明距离
查看>>
Leetcode C++《热题 Hot 100-18》538.把二叉搜索树转换为累加树
查看>>
Leetcode C++《热题 Hot 100-19》543.二叉树的直径
查看>>
Leetcode C++《热题 Hot 100-21》581.最短无序连续子数组
查看>>
Leetcode C++《热题 Hot 100-22》2.两数相加
查看>>
Leetcode C++《热题 Hot 100-23》3.无重复字符的最长子串
查看>>
Leetcode C++《热题 Hot 100-24》5.最长回文子串
查看>>
Leetcode C++《热题 Hot 100-26》15.三数之和
查看>>
Leetcode C++《热题 Hot 100-27》17.电话号码的字母组合
查看>>
Leetcode C++《热题 Hot 100-28》19.删除链表的倒数第N个节点
查看>>
Leetcode C++《热题 Hot 100-29》22.括号生成
查看>>
Leetcode C++《热题 Hot 100-30》31.下一个排列
查看>>
Leetcode C++《热题 Hot 100-40》64.最小路径和
查看>>
Leetcode C++《热题 Hot 100-41》75.颜色分类
查看>>