procmemstat.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include "procmemstat.h"
  2. #include <QFile>
  3. #include <iostream>
  4. ProcMemStat::ProcMemStat()
  5. {
  6. }
  7. void ProcMemStat::UpdateCPUMemStat(QString & strInfo)
  8. {
  9. #define MAX_CPU 256 //Support 256 cpu core
  10. static bool bFirst = true;
  11. static int nNowCPUCore = 0;
  12. static qint64 nLastCPUTotal,nLastIdleTotal,nLastIrqCount;
  13. static qint64 nVectorLastCPU[MAX_CPU];
  14. static qint64 nVectorLastIdle[MAX_CPU];
  15. static qint64 nVectorLastIrq[MAX_CPU];
  16. qint64 nNowCPUTotal,nNowIdleTotal,nIrqCount;
  17. float fcputotal;
  18. float firqtotal;
  19. float fVectorCPU[MAX_CPU];
  20. float fVectorIrq[MAX_CPU];
  21. qint64 nTotalDiff = 0;
  22. int ncpunum;
  23. // qint64 tuser,tnice,tsys,tidle,tiowait,tirq,tsoftirq;
  24. qint64 titem[7];
  25. QFile xFileStat;
  26. xFileStat.setFileName("/proc/stat");
  27. if(xFileStat.open(QIODevice::ReadOnly))
  28. {
  29. QByteArray ba = xFileStat.readAll();
  30. QList<QByteArray> xlist = ba.split('\n');
  31. int nsize = xlist.size();
  32. if(nsize > 1)
  33. {
  34. QString strline(xlist.at(0));
  35. QStringList baitem = strline.split(QRegExp("[\t ,;]+"));
  36. if(baitem.size()>=8)
  37. {
  38. unsigned int i;
  39. nNowCPUTotal = 0;
  40. for(i=0;i<7;i++)
  41. {
  42. titem[i] = QString(baitem[i+1]).toLongLong();
  43. nNowCPUTotal = nNowCPUTotal + titem[i];
  44. }
  45. nNowIdleTotal = titem[3];
  46. nIrqCount = titem[5] + titem[6];
  47. nTotalDiff = nNowCPUTotal - nLastCPUTotal;
  48. if((bFirst == false)&&(nTotalDiff > 0))
  49. {
  50. fcputotal = 100.0 - 100.0*(nNowIdleTotal - nLastIdleTotal)/nTotalDiff;
  51. firqtotal = 100.0*(nIrqCount - nLastIrqCount)/nTotalDiff;
  52. }
  53. else
  54. {
  55. fcputotal = 0;
  56. firqtotal = 0;
  57. }
  58. int ncpuindex = 0;
  59. for(i=1;i<xlist.size();i++)
  60. {
  61. QString strcpu(xlist.at(i));
  62. if(strcpu.left(3) == "cpu")
  63. {
  64. baitem = strcpu.split(QRegExp("[\t ,;]+"));
  65. if(baitem.size() >= 8)
  66. {
  67. unsigned int j;
  68. qint64 evCPUTotal = 0;
  69. for(j=0;j<7;j++)
  70. {
  71. titem[j] = QString(baitem[j+1]).toLongLong();
  72. evCPUTotal = evCPUTotal + titem[j];
  73. }
  74. qint64 evIdleTotal = titem[3];
  75. qint64 evIrqCount = titem[5] + titem[6];
  76. if(ncpuindex < MAX_CPU)
  77. {
  78. qint64 nevDiff = evCPUTotal - nVectorLastCPU[ncpuindex];
  79. if((nevDiff >0)&&(bFirst == false))
  80. {
  81. fVectorCPU[ncpuindex] = 100.0 - 100.0*(evIdleTotal - nVectorLastIdle[ncpuindex])/nevDiff;
  82. fVectorIrq[ncpuindex] = 100.0*(evIrqCount - nVectorLastIrq[ncpuindex])/nevDiff;
  83. }
  84. else
  85. {
  86. fVectorCPU[ncpuindex] = 0;
  87. fVectorIrq[ncpuindex] = 0;
  88. }
  89. nVectorLastCPU[ncpuindex] = evCPUTotal;
  90. nVectorLastIdle[ncpuindex] = evIdleTotal;
  91. nVectorLastIrq[ncpuindex] = evIrqCount;
  92. }
  93. }
  94. ncpuindex++;
  95. }
  96. }
  97. ncpunum = ncpuindex;
  98. nLastCPUTotal = nNowCPUTotal;
  99. nLastIdleTotal = nNowIdleTotal;
  100. nLastIrqCount = nIrqCount;
  101. }
  102. if(bFirst)bFirst = false;
  103. }
  104. }
  105. xFileStat.close();
  106. strInfo.clear();
  107. strInfo = strInfo + QString("CPU: Used ") + QString::number(fcputotal,'f',3)
  108. +QString(" Irq Used : ")+ QString::number(firqtotal,'f',3)
  109. +QString(" Core Count: ")+QString::number(ncpunum)+QString("\n");
  110. std::cout<<" CPU: Used "<<fcputotal<<" Irq used: "<<firqtotal<<" Core Count: "<<ncpunum<<std::endl;
  111. unsigned int i;
  112. for(i=0;i<ncpunum;i++)
  113. {
  114. strInfo = strInfo + QString(" core ")+QString::number(i)
  115. +QString(" Used: ")+ QString::number(fVectorCPU[i],'f',3)
  116. +QString(" Irq used: ") + QString::number(fVectorIrq[i],'f',3)+QString("\n");
  117. std::cout<<" core "<<i<<" Used: "<<fVectorCPU[i]<<" Irq used: "<<fVectorIrq[i]<<std::endl;
  118. }
  119. QFile xFileMem;
  120. xFileMem.setFileName("/proc/meminfo");
  121. if(xFileMem.open(QIODevice::ReadOnly))
  122. {
  123. QByteArray ba = xFileMem.readAll();
  124. QList<QByteArray> xlist = ba.split('\n');
  125. int nsize = xlist.size();
  126. if(nsize > 1)
  127. {
  128. QString strline(xlist.at(0));
  129. QStringList baitem = strline.split(QRegExp("[\t ,;]+"));
  130. if(baitem.size()>= 2)
  131. {
  132. qint64 memtotal = QString(baitem[1]).toLongLong()/1024;
  133. strInfo = strInfo + QString("Mem Total: ") + QString::number(memtotal)+ QString(" MB\n");
  134. std::cout<<" Mem Total "<< memtotal<<" MB"<<std::endl;
  135. }
  136. strline = QString(xlist.at(1));
  137. baitem = strline.split(QRegExp("[\t ,;]+"));
  138. if(baitem.size()>= 2)
  139. {
  140. qint64 memfree = QString(baitem[1]).toLongLong()/1024;
  141. strInfo = strInfo + QString("Mem Free: ") + QString::number(memfree)+ QString(" MB\n");
  142. std::cout<<" Mem Free "<< memfree<<" MB"<<std::endl;
  143. }
  144. }
  145. }
  146. xFileMem.close();
  147. }