周期获取Linux系统内存
rvolume.h中代码:
1 #ifndef VOLUME_H 2 #define VOLUME_H 3 #include <QtCore/QCoreApplication> 4 #include <QtSql> 5 #include <QTimer> 6 class RVolume : public QObject 7 { 8 Q_OBJECT 9 public: 10 RVolume(int); 11 ~RVolume(); 12 private: 13 char m_cBuff[256]; 14 char m_cName1[20]; 15 char m_cName2[20]; 16 char m_cName3[20]; 17 char m_cUnit[20]; 18 unsigned int m_nMemFree; 19 unsigned int m_nBuffers; 20 unsigned int m_nCached; 21 unsigned int m_nTotalFree; 22 QTimer * counter; 23 QSqlDatabase dbconn; 24 private slots: 25 void getVolume(); 26 }; 27 28 #endif
rvolume.cpp中代码:
1 #include <QtCore/QCoreApplication> 2 #include <QObject> 3 #include <QtSql> 4 #include <QTime> 5 #include <QSqlQuery> 6 #include <QTextCodec> 7 #include "rvolume.h" 8 9 RVolume::RVolume(int m_nTimer) 10 { 11 qDebug() << "The time interval is" <<m_nTimer<<"s."; 12 counter = new QTimer( this ); 13 connect( counter, SIGNAL(timeout()),this, SLOT( getVolume()) ); 14 counter->start((m_nTimer)*1000); 15 16 if(QSqlDatabase::contains("GBond")) 17 { 18 dbconn = QSqlDatabase::database("GBond"); 19 } 20 else 21 { 22 dbconn = QSqlDatabase::addDatabase("QSQLITE", "GBond"); 23 } 24 dbconn.setDatabaseName("/home/Data/Volume.db"); 25 } 26 27 RVolume::~RVolume() 28 { 29 delete counter; 30 } 31 32 void RVolume::getVolume() 33 { 34 FILE *fd; 35 fd = fopen ("/proc/meminfo", "r"); 36 fgets (m_cBuff, sizeof(m_cBuff), fd); 37 fgets (m_cBuff, sizeof(m_cBuff), fd); 38 sscanf (m_cBuff, "%s %u %s", m_cName1, &m_nMemFree,m_cUnit); 39 fgets (m_cBuff, sizeof(m_cBuff), fd); 40 sscanf (m_cBuff, "%s %u %s", m_cName2, &m_nBuffers,m_cUnit); 41 fgets (m_cBuff, sizeof(m_cBuff), fd); 42 sscanf (m_cBuff, "%s %u %s", m_cName3, &m_nCached,m_cUnit); 43 fclose(fd); 44 45 m_nTotalFree=m_nMemFree+m_nBuffers+m_nCached; 46 QDate date; 47 QVariantList itts; 48 itts << date.currentDate () ; 49 50 QTime time; 51 QVariantList imts; 52 imts << time.currentTime () ; 53 54 QVariantList ints; 55 ints << m_nTotalFree; 56 57 if (dbconn.open()) 58 { 59 QSqlQuery query("create table volume(QDate dater,QTime timer,totalFree unsigned int)", dbconn); 60 query.prepare("insert into volume values (?, ?, ?)"); 61 query.addBindValue(itts); 62 query.addBindValue(imts); 63 query.addBindValue(ints); 64 qDebug()<<"\n**** A new information was written into the table.**** "; 65 if (!query.execBatch()) 66 { 67 qDebug() << query.lastError(); 68 } 69 } 70 }
main.cpp中代码:
1 #include <QtCore/QCoreApplication> 2 #include <QTextStream> 3 #include "rvolume.h" 4 5 int main(int argc, char *argv[]) 6 { 7 QCoreApplication a(argc, argv); 8 QTextStream out(stdout); 9 if (argc < 3) 10 { 11 out << "Usage : volume -t time_interval(Unit seconds)" << endl; 12 exit(0); 13 } 14 15 const char *gs_cMark = "-t",*gs_cTime; 16 char *g_cMark_in = argv[1], *g_cTime_in = argv[2]; 17 gs_cTime=g_cTime_in; 18 QString str =gs_cTime; 19 bool ok; 20 int g_nTimer = str. toInt( &ok, 10 ); 21 if ( QString::compare(g_cMark_in,gs_cMark )!=0|| g_nTimer<=0) 22 { 23 out << "The mark or the time interval is invalid! Exit."<< endl ; 24 exit(0); 25 } 26 RVolume memory(g_nTimer); 27 return a.exec(); 28 }
volume.pro中代码:
1 ###################################################################### 2 # Automatically generated by qmake (2.01a) ?? 4? 17 15:00:26 2012 3 ###################################################################### 4 5 TEMPLATE = app 6 QT -= gui 7 QT += sql 8 TARGET = 9 DEPENDPATH += . 10 INCLUDEPATH += . 11 12 # Input 13 HEADERS += rvolume.h 14 SOURCES += main.cpp rvolume.cpp
配置环境变量:vi /etc/profile
在arm中运行结果如下: