QT取得システムメモリ使用率、CPU使用率、ハードディスク容量(windows、linuxともに利用可能)

7980 ワード

システムリソースを取得するクラスを書き、マクロ定義でlinux下とwindows下でそれぞれ異なるコードを実行することを区別しました.ヘッダファイル:
#ifndef RESOURCE_MINITOR_H
#define RESOURCE_MINITOR_H
#include 
#include 
#include 
#include 
#include 
#if defined(Q_OS_LINUX)
#include "sys/statfs.h"
#else
#pragma comment(lib,"Kernel32.lib")
#pragma comment(lib,"Psapi.lib")
#include 
#include 
#include
#include
#include 
//#include 
#include 
#include 
#endif

class MySysInfo : public QObject
{
    Q_OBJECT
public:
    explicit MySysInfo(QObject *parent = nullptr);
private slots:
    void GetResource();
public:
    bool GetMemUsage(double &nMemTotal,double &nMemUsed);
    bool GetNetUsage();
    bool GetDiskSpeed();
    bool GetCpuUsage(double &nCpuRate);
    bool GetdiskSpace(unsigned long &lFreeAll,unsigned long &lTotalAll);
    bool GetPathSpace(const QString & path);
private:
    const int m_timer_interval__ = 1000;
    QTimer monitor_timer__;
    double m_send_bytes__ = 0;
    double m_recv_bytes__ = 0;
    double m_disk_read__ = 0;
    double m_disk_write__ = 0;
    double m_cpu_total__ = 0;
    double m_cpu_use__ = 0;
};
#endif // RESOURCE_MINITOR_H

ソースファイル:
#include "mysysinfo.h"
MySysInfo::MySysInfo(QObject *parent) : QObject(parent)
{
    connect(&monitor_timer__, &QTimer::timeout, this, &MySysInfo::GetResource);
    monitor_timer__.start(m_timer_interval__);
}
void MySysInfo::GetResource()
{
    double nCpuRate = 0;
    GetCpuUsage(nCpuRate);
    GetDiskSpeed();
    double nMemTotal;
    double nMemUsed;
    GetMemUsage(nMemTotal,nMemUsed);
    GetNetUsage();
    unsigned long lFreeAll;
    unsigned long lTotalAll;
    GetdiskSpace(lFreeAll,lTotalAll);
    GetPathSpace("/");
    qDebug()< 6)
    {
        qDebug("mem total:%.0lfMB free:%.0lfMB",lst[1].toDouble(),lst[6].toDouble());
        nMemTotal = lst[1].toDouble();
        nMemUsed = nMemTotal-lst[6].toDouble();
        return true;
    }
    else
    {
        return false;
    }
#else
    MEMORYSTATUSEX memsStat;
        memsStat.dwLength = sizeof(memsStat);
        if(!GlobalMemoryStatusEx(&memsStat))//             ,     
        {
            return false;
        }
        double nMemFree = memsStat.ullAvailPhys/( 1024.0*1024.0 );
        nMemTotal = memsStat.ullTotalPhys/( 1024.0*1024.0 );
        nMemUsed= nMemTotal- nMemFree;
        qDebug("windows:mem total:%.0lfMB,use:%.0lfMB",nMemTotal,nMemUsed);
        return true;
#endif
}
bool MySysInfo::GetNetUsage()
{
#if defined(Q_OS_LINUX)
    QProcess process;
    process.start("cat /proc/net/dev"); //    /proc/net/dev         ,            
    process.waitForFinished();
    process.readLine();
    process.readLine();
    while(!process.atEnd())
    {
        QString str = process.readLine();
        str.replace("
",""); str.replace(QRegExp("( ){1,}")," "); auto lst = str.split(" "); if(lst.size() > 9 && lst[0] == "enp2s0:") { double recv = 0; double send = 0; if(lst.size() > 1) recv = lst[1].toDouble(); if(lst.size() > 9) send = lst[9].toDouble(); qDebug("%s :%.0lfbyte/s :%.0lfbyte/s",lst[0].toStdString().c_str(),(recv - m_recv_bytes__) / (m_timer_interval__ / 1000.0),(send - m_send_bytes__) / (m_timer_interval__ / 1000.0)); m_recv_bytes__ = recv; m_send_bytes__ = send; } } #endif return true; } #if defined(Q_OS_WIN32) __int64 Filetime2Int64(const FILETIME* ftime) { LARGE_INTEGER li; li.LowPart = ftime->dwLowDateTime; li.HighPart = ftime->dwHighDateTime; return li.QuadPart; } __int64 CompareFileTime(FILETIME preTime,FILETIME nowTime) { return Filetime2Int64(&nowTime) - Filetime2Int64(&preTime); } #endif bool MySysInfo::GetCpuUsage(double &nCpuRate) { nCpuRate = -1; #if defined(Q_OS_LINUX) QProcess process; process.start("cat /proc/stat"); process.waitForFinished(); QString str = process.readLine(); str.replace("
",""); str.replace(QRegExp("( ){1,}")," "); auto lst = str.split(" "); if(lst.size() > 3) { double use = lst[1].toDouble() + lst[2].toDouble() + lst[3].toDouble(); double total = 0; for(int i = 1;i < lst.size();++i) total += lst[i].toDouble(); if(total - m_cpu_total__ > 0) { qDebug("cpu rate:%.2lf%%",(use - m_cpu_use__) / (total - m_cpu_total__) * 100.0); m_cpu_total__ = total; m_cpu_use__ = use; nCpuRate = (use - m_cpu_use__) / (total - m_cpu_total__) * 100.0; return true; } } #else HANDLE hEvent; bool res; static FILETIME preIdleTime; static FILETIME preKernelTime; static FILETIME preUserTime; FILETIME idleTime; FILETIME kernelTime; FILETIME userTime; res = GetSystemTimes(&idleTime,&kernelTime,&userTime); preIdleTime = idleTime; preKernelTime = kernelTime; preUserTime = userTime; hEvent = CreateEvent(nullptr,FALSE,FALSE,nullptr);// nonsignaled WaitForSingleObject(hEvent,500);// 500 res = GetSystemTimes(&idleTime,&kernelTime,&userTime); long long idle = CompareFileTime(preIdleTime,idleTime); long long kernel = CompareFileTime(preKernelTime,kernelTime); long long user = CompareFileTime(preUserTime,userTime); nCpuRate =ceil( 100.0*( kernel + user - idle ) / ( kernel + user ) ); qDebug()< 5) { qDebug("disk read:%.0lfkb/s disk write:%.0lfkb/s",(lst[4].toDouble() - m_disk_read__ ) / (m_timer_interval__ / 1000.0),(lst[5].toDouble() - m_disk_write__) / (m_timer_interval__ / 1000.0)); m_disk_read__ = lst[4].toDouble(); m_disk_write__ = lst[5].toDouble(); return true; } #endif return false; } bool MySysInfo::GetdiskSpace(unsigned long &lFreeAll,unsigned long &lTotalAll) { #if defined(Q_OS_LINUX) QProcess process; process.start("df -k"); process.waitForFinished(); process.readLine(); while(!process.atEnd()) { QString str = process.readLine(); if(str.startsWith("/dev/sda")) { str.replace("
",""); str.replace(QRegExp("( ){1,}")," "); auto lst = str.split(" "); if(lst.size() > 5) qDebug(" :%s :%.0lfMB :%.0lfMB",lst[5].toStdString().c_str(),lst[2].toDouble()/1024.0,lst[3].toDouble()/1024.0); lFreeAll += lst[2].toDouble()/1024.0; lTotalAll += lst[3].toDouble()/1024.0+lFreeAll; } } #else static char path[_MAX_PATH];// int curdrive = _getdrive(); lFreeAll = 0UL; lTotalAll = 0UL; for(int drive = 1; drive <= curdrive; drive++ )// { if( !_chdrive( drive ) ) { sprintf(path, "%c:\\", drive + 'A' - 1 ); ULARGE_INTEGER caller, total, free; WCHAR wszClassName[_MAX_PATH]; memset(wszClassName,0,sizeof(wszClassName)); MultiByteToWideChar(CP_ACP,0,path,strlen(path)+1,wszClassName, sizeof(wszClassName)/sizeof(wszClassName[0])); if (GetDiskFreeSpaceEx(wszClassName, &caller, &total, &free) == 0) { qDebug()<