一、前言
在很多軟件上,會在某個部位顯示一個部件,專門顯示當前的CPU使用率以及內存佔用,方便用戶判斷當前程序或者當前環境中是否還有剩餘的CPU和內存留給程序使用,在不用打開任務管理器或者資源查看器的時候直接得知當前系統的運行情況。尤其是視頻監控系統,如果64路全開,肯定很佔用CPU和內存情況,這樣直接在軟件上直觀的查看到當前佔用率,用戶更方便判斷當前電腦環境是否適合打開多少路通道。
採集本地系統的實時CPU使用率,如果使用的GetSystemTimes函數,會發現和本地任務管理器中的不一致(主要集中在win10系統/win7和XP系統貌似正常),那是因爲計數統計的方式不一樣,採用GetSystemTimes函數獲取到的值相對來說是系統底層的數據。爲了能夠和任務管理器中展示的一致,試驗過各種辦法後決定採用命令行獲取的形式處理,這樣獲取到的值是一致的,在win10以下的系統執行 typeperf \Processor(_Total)% Processor Time,在win10及以上的系統執行 typeperf \Processor Information(_Total)% Processor Utility。執行命令採用QProcess類,執行結果有信號通知,直接讀取解析即可。同理在linux系統上也是採用執行命令行的形式獲取,比如linux上獲取CPU佔用命令是 cat /proc/stat,獲取內存相關命令是 cat /proc/meminfo。在windows上獲取內存相關使用函數 GlobalMemoryStatusEx。
二、主要功能
- 實時顯示當前CPU佔用率。
- 實時顯示內存使用情況。
- 包括共多少內存、已使用多少內存。
- 全平臺通用,包括windows、linux、ARM。
- 發出信號通知佔用率和內存使用情況等,以便自行顯示到其他地方。
三、效果圖
四、開源主頁
- 以上作品完整源碼下載都在開源主頁,會持續不斷更新作品數量和質量,歡迎各位關注。
- 本開源項目已經成功升級到V2.0版本,分門別類,圖文並茂,保你爽到爆。
- Qt開源武林祕籍開發經驗,看完學完,20K起薪,沒有找我!
- 國內站點:https://gitee.com/feiyangqingyun/QWidgetDemo
- 國際站點:https://github.com/feiyangqingyun/QWidgetDemo
- 開源祕籍:https://gitee.com/feiyangqingyun/qtkaifajingyan
- 個人主頁:https://qtchina.blog.csdn.net/
- 知乎主頁:https://www.zhihu.com/people/feiyangqingyun/
五、核心代碼
#pragma execution_character_set("utf-8")
#include "cpumemorylabel.h"
#include "qtimer.h"
#include "qprocess.h"
#include "qdebug.h"
#ifdef Q_OS_WIN
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x502
#endif
#include "windows.h"
#endif
#define MB (1024 * 1024)
#define KB (1024)
CpuMemoryLabel::CpuMemoryLabel(QWidget *parent) : QLabel(parent)
{
totalNew = idleNew = totalOld = idleOld = 0;
cpuPercent = 0;
memoryPercent = 0;
memoryAll = 0;
memoryUse = 0;
//獲取CPU佔用情況定時器
timerCPU = new QTimer(this);
connect(timerCPU, SIGNAL(timeout()), this, SLOT(getCPU()));
//獲取內存佔用情況定時器
timerMemory = new QTimer(this);
connect(timerMemory, SIGNAL(timeout()), this, SLOT(getMemory()));
//執行命令獲取
process = new QProcess(this);
connect(process, SIGNAL(readyRead()), this, SLOT(readData()));
showText = true;
}
CpuMemoryLabel::~CpuMemoryLabel()
{
this->stop();
}
void CpuMemoryLabel::start(int interval)
{
this->getCPU();
this->getMemory();
if (!timerCPU->isActive()) {
timerCPU->start(interval);
}
if (!timerMemory->isActive()) {
timerMemory->start(interval + 1000);
}
}
void CpuMemoryLabel::stop()
{
process->close();
if (timerCPU->isActive()) {
timerCPU->stop();
}
if (timerMemory->isActive()) {
timerMemory->stop();
}
}
void CpuMemoryLabel::getCPU()
{
#ifdef Q_OS_WIN
#if 0
static FILETIME lastIdleTime;
static FILETIME lastKernelTime;
static FILETIME lastUserTime;
FILETIME newIdleTime;
FILETIME newKernelTime;
FILETIME newUserTime;
//採用GetSystemTimes獲取的CPU佔用和任務管理器的不一致
GetSystemTimes(&newIdleTime, &newKernelTime, &newUserTime);
int offset = 31;
quint64 a, b;
quint64 idle, kernel, user;
a = (lastIdleTime.dwHighDateTime << offset) | lastIdleTime.dwLowDateTime;
b = (newIdleTime.dwHighDateTime << offset) | newIdleTime.dwLowDateTime;
idle = b - a;
a = (lastKernelTime.dwHighDateTime << offset) | lastKernelTime.dwLowDateTime;
b = (newKernelTime.dwHighDateTime << offset) | newKernelTime.dwLowDateTime;
kernel = b - a;
a = (lastUserTime.dwHighDateTime << offset) | lastUserTime.dwLowDateTime;
b = (newUserTime.dwHighDateTime << offset) | newUserTime.dwLowDateTime;
user = b - a;
cpuPercent = float(kernel + user - idle) * 100 / float(kernel + user);
lastIdleTime = newIdleTime;
lastKernelTime = newKernelTime;
lastUserTime = newUserTime;
this->setData();
#else
//獲取系統版本區分win10
bool win10 = false;
#if (QT_VERSION >= QT_VERSION_CHECK(5,4,0))
win10 = (QSysInfo::productVersion().mid(0, 2).toInt() >= 10);
#else
win10 = (QSysInfo::WindowsVersion >= 192);
#endif
QString cmd = "\\Processor(_Total)\\% Processor Time";
if (win10) {
cmd = "\\Processor Information(_Total)\\% Processor Utility";
}
if (process->state() == QProcess::NotRunning) {
process->start("typeperf", QStringList() << cmd);
}
#endif
#elif defined(Q_OS_UNIX) && !defined(Q_OS_WASM)
if (process->state() == QProcess::NotRunning) {
totalNew = idleNew = 0;
process->start("cat", QStringList() << "/proc/stat");
}
#endif
}
void CpuMemoryLabel::getMemory()
{
#ifdef Q_OS_WIN
MEMORYSTATUSEX statex;
statex.dwLength = sizeof(statex);
GlobalMemoryStatusEx(&statex);
memoryPercent = statex.dwMemoryLoad;
memoryAll = statex.ullTotalPhys / MB;
memoryFree = statex.ullAvailPhys / MB;
memoryUse = memoryAll - memoryFree;
this->setData();
#elif defined(Q_OS_UNIX) && !defined(Q_OS_WASM)
if (process->state() == QProcess::NotRunning) {
process->start("cat", QStringList() << "/proc/meminfo");
}
#endif
}
void CpuMemoryLabel::readData()
{
#ifdef Q_OS_WIN
while (!process->atEnd()) {
QString s = QLatin1String(process->readLine());
s = s.split(",").last();
s.replace("\r", "");
s.replace("\n", "");
s.replace("\"", "");
if (!s.isEmpty() && s.length() < 10) {
cpuPercent = qRound(s.toFloat());
}
}
#elif defined(Q_OS_UNIX) && !defined(Q_OS_WASM)
while (!process->atEnd()) {
QString s = QLatin1String(process->readLine());
if (s.startsWith("cpu")) {
QStringList list = s.split(" ");
idleNew = list.at(5).toUInt();
foreach (QString value, list) {
totalNew += value.toUInt();
}
quint64 total = totalNew - totalOld;
quint64 idle = idleNew - idleOld;
cpuPercent = 100 * (total - idle) / total;
totalOld = totalNew;
idleOld = idleNew;
break;
} else if (s.startsWith("MemTotal")) {
s.replace(" ", "");
s = s.split(":").at(1);
memoryAll = s.left(s.length() - 3).toUInt() / KB;
} else if (s.startsWith("MemFree")) {
s.replace(" ", "");
s = s.split(":").at(1);
memoryFree = s.left(s.length() - 3).toUInt() / KB;
} else if (s.startsWith("Buffers")) {
s.replace(" ", "");
s = s.split(":").at(1);
memoryFree += s.left(s.length() - 3).toUInt() / KB;
} else if (s.startsWith("Cached")) {
s.replace(" ", "");
s = s.split(":").at(1);
memoryFree += s.left(s.length() - 3).toUInt() / KB;
memoryUse = memoryAll - memoryFree;
memoryPercent = 100 * memoryUse / memoryAll;
break;
}
}
#endif
this->setData();
}
void CpuMemoryLabel::setData()
{
cpuPercent = (cpuPercent < 0 ? 0 : cpuPercent);
cpuPercent = (cpuPercent > 100 ? 0 : cpuPercent);
QString msg = QString("CPU %1% Mem %2% ( 已用 %3 MB / 共 %4 MB )").arg(cpuPercent).arg(memoryPercent).arg(memoryUse).arg(memoryAll);
if (showText) {
this->setText(msg);
}
emit textChanged(msg);
emit valueChanged(cpuPercent, memoryPercent, memoryAll, memoryUse, memoryFree);
}
QSize CpuMemoryLabel::sizeHint() const
{
return QSize(300, 30);
}
QSize CpuMemoryLabel::minimumSizeHint() const
{
return QSize(30, 10);
}
bool CpuMemoryLabel::getShowText() const
{
return this->showText;
}
void CpuMemoryLabel::setShowText(bool showText)
{
this->showText = showText;
}