123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- #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();
- }
|