瀏覽代碼

change IVSysMan for get System cpu load and memory info.

yuchuli 3 年之前
父節點
當前提交
58ca77bb8e

+ 4 - 2
src/tool/IVSysMan/IVSysMan.pro

@@ -55,7 +55,8 @@ SOURCES += \
     switchbutton.cpp \
     cpumem.cpp \
     ../../include/msgtype/fault.pb.cc \
-    progunit.cpp
+    progunit.cpp \
+    procmemstat.cpp
 
 HEADERS += \
     ../../include/msgtype/switch.pb.h \
@@ -68,7 +69,8 @@ HEADERS += \
     switchbutton.h \
     cpumem.h \
     ../../include/msgtype/fault.pb.h \
-    progunit.h
+    progunit.h \
+    procmemstat.h
 
 FORMS += \
         mainwindow.ui

+ 35 - 0
src/tool/IVSysMan/centerview.cpp

@@ -20,6 +20,12 @@ CenterView::CenterView(QTabWidget * pTab,ProgMon * pPM)
         connect(gu,SIGNAL(ProgLogClick(ProgUnit*,ProgramViewUnit *,bool)),this,SLOT(onProgLogClick(ProgUnit*,ProgramViewUnit *,bool)));
         mvectorGropup.push_back(gu);
     }
+
+    QGroupBox * p = CreateInfoGroup(mTabMain,"Info");
+
+    QTimer * timer = new QTimer();
+    connect(timer,SIGNAL(timeout()),this,SLOT(onTimer()));
+    timer->start(1000);
 }
 
 CenterView::~CenterView()
@@ -59,6 +65,11 @@ void CenterView::ResetView()
         mvectorGropup.push_back(gu);
     }
 
+
+    QGroupBox * p = CreateInfoGroup(mTabMain,"Info");
+
+
+
     for(i=0;i<5;i++)
     {
         mvectorGropup[i]->CreateView();
@@ -90,6 +101,22 @@ void CenterView::ResetView()
 
 }
 
+QGroupBox * CenterView::CreateInfoGroup(QTabWidget * p,std::string strtab)
+{
+    QGroupBox * pGroup = new QGroupBox();
+    pGroup->setGeometry(0,0,1800,1000);
+
+    QScrollArea * pScroll = new QScrollArea();
+    pScroll->setWidget(pGroup);
+
+    p->addTab(pScroll,strtab.data());
+
+    mpPTE = new QPlainTextEdit(pGroup);
+    mpPTE->setGeometry(30,30,800,600);
+
+    return pGroup;
+}
+
 QGroupBox * CenterView::CreateGroup(QTabWidget * p,std::string strtab)
 {
     QGroupBox * pGroup = new QGroupBox();
@@ -142,3 +169,11 @@ void CenterView::UpdateState()
         mvectorGropup.at(i)->UpdateState();
     }
 }
+
+void CenterView::onTimer()
+{
+
+    QString strsysinfo = mpPM->GetSysInfo();
+//    qDebug(strsysinfo.toLatin1().data());
+    mpPTE->setPlainText(strsysinfo);
+}

+ 9 - 0
src/tool/IVSysMan/centerview.h

@@ -4,6 +4,7 @@
 #include <QTabWidget>
 #include <QGroupBox>
 #include <QScrollArea>
+#include <QPlainTextEdit>
 
 #include <vector>
 
@@ -25,11 +26,14 @@ public:
     std::vector<GroupUnit * > mvectorGropup;
 private:
     QGroupBox *  CreateGroup(QTabWidget * p,std::string strtab);//设置模块按钮_tjc
+    QGroupBox *  CreateInfoGroup(QTabWidget * p,std::string strtab);
 
 private slots:
     void onProgClick(ProgUnit * pu,ProgramViewUnit * pvu,bool bClick);
     void onProgLogClick(ProgUnit * pu,ProgramViewUnit * pvu,bool bClick);
 
+    void onTimer();
+
 signals:
     void ProgClick(ProgUnit *, ProgramViewUnit * ,bool);
     void ProgLogClick(ProgUnit *, ProgramViewUnit * ,bool);
@@ -46,6 +50,11 @@ public:
 
 
     void ResetView();
+
+private:
+    QPlainTextEdit * mpPTE;
+
+
 };
 
 #endif // CENTERVIEW_H

+ 8 - 2
src/tool/IVSysMan/cpumem.cpp

@@ -19,6 +19,9 @@ typedef struct {
     unsigned long nice;
     unsigned long system;
     unsigned long idle;
+    unsigned long iowait;
+    unsigned long irq;
+    unsigned long softirg;
 }Total_Cpu_Occupy_t;
 
 
@@ -68,10 +71,11 @@ unsigned long get_cpu_total_occupy(){
 
     fgets(buff,sizeof(buff),fd);
     char name[64]={0};
-    sscanf(buff,"%s %ld %ld %ld %ld",name,&t.user,&t.nice,&t.system,&t.idle);
+    sscanf(buff,"%s %ld %ld %ld %ld %ld %ld %ld",name,&t.user,&t.nice,&t.system,&t.idle,
+          &t.iowait,&t.irq,&t.softirg);
     fclose(fd);
 
-    return (t.user + t.nice + t.system + t.idle);
+    return (t.user + t.nice + t.system + t.idle+t.iowait+t.irq+t.softirg);
 #endif
 
 #ifdef IV_OS_WIN
@@ -139,6 +143,8 @@ float get_proc_cpu(unsigned int pid,unsigned long & nLastProcTotal,unsigned long
     float pcpu = 0.0;
     if(nLastAllTotal == 0)
     {
+        nLastProcTotal = procputime1;
+        nLastAllTotal = totalcputime1;
         return 0.0; //if First,Not Count;
     }
     else

+ 164 - 0
src/tool/IVSysMan/procmemstat.cpp

@@ -0,0 +1,164 @@
+#include "procmemstat.h"
+
+#include  <QFile>
+
+#include <iostream>
+
+ProcMemStat::ProcMemStat()
+{
+
+}
+
+void ProcMemStat::UpdateCPUMemStat(QString & strInfo)
+{
+#define MAX_CPU 256   //Support 256 cpu core
+    static bool bFirst = true;
+    static int nNowCPUCore = 0;
+    static qint64 nLastCPUTotal,nLastIdleTotal,nLastIrqCount;
+    static qint64 nVectorLastCPU[MAX_CPU];
+    static qint64 nVectorLastIdle[MAX_CPU];
+    static qint64 nVectorLastIrq[MAX_CPU];
+    qint64 nNowCPUTotal,nNowIdleTotal,nIrqCount;
+    float fcputotal;
+    float firqtotal;
+    float fVectorCPU[MAX_CPU];
+    float fVectorIrq[MAX_CPU];
+    qint64 nTotalDiff = 0;
+    int ncpunum;
+
+//    qint64 tuser,tnice,tsys,tidle,tiowait,tirq,tsoftirq;
+    qint64 titem[7];
+
+    QFile xFileStat;
+    xFileStat.setFileName("/proc/stat");
+    if(xFileStat.open(QIODevice::ReadOnly))
+    {
+        QByteArray ba = xFileStat.readAll();
+        QList<QByteArray> xlist = ba.split('\n');
+        int nsize = xlist.size();
+        if(nsize > 1)
+        {
+            QString strline(xlist.at(0));
+            QStringList baitem = strline.split(QRegExp("[\t ,;]+"));
+            if(baitem.size()>=8)
+            {
+                unsigned int i;
+                nNowCPUTotal = 0;
+                for(i=0;i<7;i++)
+                {
+                    titem[i] = QString(baitem[i+1]).toLongLong();
+                    nNowCPUTotal = nNowCPUTotal + titem[i];
+                }
+                nNowIdleTotal = titem[3];
+                nIrqCount = titem[5] + titem[6];
+                nTotalDiff = nNowCPUTotal - nLastCPUTotal;
+                if((bFirst == false)&&(nTotalDiff > 0))
+                {
+                    fcputotal = 100.0 - 100.0*(nNowIdleTotal - nLastIdleTotal)/nTotalDiff;
+                    firqtotal = 100.0*(nIrqCount - nLastIrqCount)/nTotalDiff;
+                }
+                else
+                {
+                    fcputotal = 0;
+                    firqtotal = 0;
+                }
+
+                int ncpuindex = 0;
+                for(i=1;i<xlist.size();i++)
+                {
+
+                    QString strcpu(xlist.at(i));
+                    if(strcpu.left(3) == "cpu")
+                    {
+                        baitem = strcpu.split(QRegExp("[\t ,;]+"));
+                        if(baitem.size() >= 8)
+                        {
+                            unsigned int j;
+                            qint64 evCPUTotal = 0;
+                            for(j=0;j<7;j++)
+                            {
+                                titem[j] = QString(baitem[j+1]).toLongLong();
+                                evCPUTotal = evCPUTotal + titem[j];
+                            }
+                            qint64 evIdleTotal = titem[3];
+                            qint64 evIrqCount = titem[5] + titem[6];
+                            if(ncpuindex < MAX_CPU)
+                            {
+                                qint64 nevDiff = evCPUTotal - nVectorLastCPU[ncpuindex];
+
+                                if((nevDiff >0)&&(bFirst == false))
+                                {
+                                    fVectorCPU[ncpuindex] = 100.0 - 100.0*(evIdleTotal - nVectorLastIdle[ncpuindex])/nevDiff;
+                                    fVectorIrq[ncpuindex] = 100.0*(evIrqCount - nVectorLastIrq[ncpuindex])/nevDiff;
+                                }
+                                else
+                                {
+                                    fVectorCPU[ncpuindex] = 0;
+                                    fVectorIrq[ncpuindex] = 0;
+                                }
+
+                                nVectorLastCPU[ncpuindex] = evCPUTotal;
+                                nVectorLastIdle[ncpuindex] = evIdleTotal;
+                                nVectorLastIrq[ncpuindex] = evIrqCount;
+                            }
+                        }
+
+                        ncpuindex++;
+                    }
+                }
+                ncpunum = ncpuindex;
+                nLastCPUTotal = nNowCPUTotal;
+                nLastIdleTotal = nNowIdleTotal;
+                nLastIrqCount = nIrqCount;
+            }
+
+            if(bFirst)bFirst = false;
+        }
+    }
+    xFileStat.close();
+
+    strInfo.clear();
+    strInfo = strInfo + QString("CPU: Used ") + QString::number(fcputotal,'f',3)
+            +QString(" Irq Used : ")+ QString::number(firqtotal,'f',3)
+            +QString(" Core Count: ")+QString::number(ncpunum)+QString("\n");
+    std::cout<<"  CPU: Used "<<fcputotal<<" Irq used: "<<firqtotal<<"  Core Count: "<<ncpunum<<std::endl;
+
+    unsigned int i;
+    for(i=0;i<ncpunum;i++)
+    {
+        strInfo = strInfo + QString("      core ")+QString::number(i)
+                +QString("  Used: ")+ QString::number(fVectorCPU[i],'f',3)
+                +QString(" Irq used: ") + QString::number(fVectorIrq[i],'f',3)+QString("\n");
+        std::cout<<"        core "<<i<<" Used: "<<fVectorCPU[i]<<" Irq used: "<<fVectorIrq[i]<<std::endl;
+    }
+
+    QFile xFileMem;
+    xFileMem.setFileName("/proc/meminfo");
+    if(xFileMem.open(QIODevice::ReadOnly))
+    {
+        QByteArray ba = xFileMem.readAll();
+        QList<QByteArray> xlist = ba.split('\n');
+        int nsize = xlist.size();
+        if(nsize > 1)
+        {
+            QString strline(xlist.at(0));
+            QStringList baitem = strline.split(QRegExp("[\t ,;]+"));
+            if(baitem.size()>= 2)
+            {
+                qint64 memtotal = QString(baitem[1]).toLongLong()/1024;
+                strInfo = strInfo + QString("Mem Total: ") + QString::number(memtotal)+ QString(" MB\n");
+                std::cout<<"    Mem Total "<< memtotal<<" MB"<<std::endl;
+            }
+            strline = QString(xlist.at(1));
+            baitem = strline.split(QRegExp("[\t ,;]+"));
+            if(baitem.size()>= 2)
+            {
+                qint64 memfree = QString(baitem[1]).toLongLong()/1024;
+                strInfo = strInfo + QString("Mem Free: ") + QString::number(memfree)+ QString(" MB\n");
+                std::cout<<"    Mem Free "<< memfree<<" MB"<<std::endl;
+            }
+        }
+
+    }
+    xFileMem.close();
+}

+ 20 - 0
src/tool/IVSysMan/procmemstat.h

@@ -0,0 +1,20 @@
+#ifndef PROCMEMSTAT_H
+#define PROCMEMSTAT_H
+
+
+#include <QThread>
+#include <vector>
+
+class ProcMemStat
+{
+public:
+    ProcMemStat();
+    qint64 mPhyMemTotal;
+    qint64 mPhyMemAvail;
+    qint64 mMemTotal;
+    float mfCPULoad;
+    std::vector<float> mvectorcpuload;
+    void UpdateCPUMemStat(QString & strInfo);
+};
+
+#endif // PROCMEMSTAT_H

+ 35 - 0
src/tool/IVSysMan/progmon.cpp

@@ -18,6 +18,8 @@ extern iv::Ivfault * ivfault;
 
 ProgMon::ProgMon(std::string path)
 {
+   UpdateCPUMemStat();
+
     mvectorprog = loadprogunit(path);
 
     InitLog();
@@ -652,6 +654,11 @@ void ProgMon::run()
             msleep(100);
             continue;
         }
+        QString strsysinfo;
+        mPMS.UpdateCPUMemStat(strsysinfo);
+        mMutexSysinfo.lock();
+        mstrsysInfo = strsysinfo;
+        mMutexSysinfo.unlock();
         nLastUpdate = nNow;
         int i;
 
@@ -1026,3 +1033,31 @@ int ProgMon::get_path_availspace(const QString & path)
 #endif
 }
 
+void ProgMon::UpdateCPUMemStat()
+{
+    return;
+    static qint64 nLastCPUTotal,nLastIdleTotal;
+    static std::vector<qint64> nVectorLastCPU;
+    static std::vector<qint64> nVectorLastIdle;
+
+    QFile xFileStat;
+    xFileStat.setFileName("/proc/stat");
+    if(xFileStat.open(QIODevice::ReadOnly))
+    {
+        QByteArray ba = xFileStat.readAll();
+        QList<QByteArray> xlist = ba.split('\n');
+        int nsize = xlist.size();
+    }
+    xFileStat.close();
+
+}
+
+QString ProgMon::GetSysInfo()
+{
+    QString strsysinfo;
+    mMutexSysinfo.lock();
+    strsysinfo = mstrsysInfo;
+    mMutexSysinfo.unlock();
+    return strsysinfo;
+}
+

+ 17 - 0
src/tool/IVSysMan/progmon.h

@@ -19,6 +19,8 @@
 #include "QtDBus/QDBusConnection"
 #include "progunit.h"
 
+#include "procmemstat.h"
+
 #ifdef IV_OS_WIN
 #include <windows.h>
 #endif
@@ -137,6 +139,21 @@ private:
 
     int get_path_availspace(const QString & path);
 
+private:
+    qint64 mPhyMemTotal;
+    qint64 mPhyMemAvail;
+    qint64 mMemTotal;
+    float mfCPULoad;
+    std::vector<float> mvectorcpuload;
+    void UpdateCPUMemStat();
+
+    ProcMemStat mPMS;
+
+    QString mstrsysInfo = "";
+    QMutex mMutexSysinfo;
+
+public:
+    QString GetSysInfo();
 };
 
 #endif // PROGMON_H