Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

memsensor.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2003 by Hans Karlsson                                   *
00003  *   karlsson.h@home.se                                                      *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  ***************************************************************************/
00010 #include "memsensor.h"
00011 #include <qfile.h>
00012 #include <qtextstream.h>
00013 #include <qstring.h>
00014 #include <qregexp.h>
00015 MemSensor::MemSensor(int msec) : Sensor(msec)
00016 {
00017     readValues();
00018 }
00019 MemSensor::~MemSensor()
00020 {}
00021 
00022 
00023 int MemSensor::getMemTotal()
00024 {
00025     QRegExp rx( "MemTotal:\\s*(\\d+)" );
00026     rx.search( meminfo );
00027     return ( rx.cap(1).toInt() );
00028 }
00029 
00030 int MemSensor::getMemFree()
00031 {
00032     QRegExp rx( "MemFree:\\s*(\\d+)" );
00033     rx.search( meminfo );
00034     return ( rx.cap(1).toInt() );
00035 }
00036 
00037 
00038 int MemSensor::getBuffers()
00039 {
00040     QRegExp rx( "Buffers:\\s*(\\d+)" );
00041     rx.search( meminfo );
00042     return ( rx.cap(1).toInt() );
00043 }
00044 
00045 int MemSensor::getCached()
00046 {
00047     QRegExp rx1( "Cached:\\s*(\\d+)" );
00048     QRegExp rx2( "SwapCached:\\s*(\\d+)" );
00049     rx1.search( meminfo );
00050     rx2.search( meminfo );
00051     return ( rx1.cap(1).toInt() + rx2.cap(1).toInt() );
00052 }
00053 
00054 
00055 int MemSensor::getSwapTotal()
00056 {
00057     QRegExp rx( "SwapTotal:\\s*(\\d+)" );
00058     rx.search( meminfo );
00059     return ( rx.cap(1).toInt() );
00060 }
00061 
00062 int MemSensor::getSwapFree()
00063 {
00064     QRegExp rx( "SwapFree:\\s*(\\d+)" );
00065     rx.search( meminfo );
00066     return ( rx.cap(1).toInt() );
00067 }
00068 
00069 
00070 void MemSensor::readValues()
00071 {
00072     QFile file("/proc/meminfo");
00073     QString line;
00074     if ( file.open(IO_ReadOnly | IO_Translate) )
00075     {
00076         QTextStream t( &file );        // use a text stream
00077         meminfo = t.read();
00078         file.close();
00079     }
00080 }
00081 
00082 void MemSensor::update()
00083 {
00084     readValues();
00085     QString format;
00086     SensorParams *sp;
00087     Meter *meter;
00088     QObjectListIt it( *objList );
00089 
00090     int totalMem = getMemTotal();
00091     int usedMem = totalMem - getMemFree();
00092     int usedMemNoBuffers =  usedMem - getBuffers() - getCached();
00093     int totalSwap = getSwapTotal();
00094     int usedSwap = totalSwap - getSwapFree();
00095 
00096     while (it != 0)
00097     {
00098         sp = (SensorParams*)(*it);
00099         meter = sp->getMeter();
00100         format = sp->getParam("FORMAT");
00101         if (format.length() == 0 )
00102         {
00103             format = "%um";
00104         }
00105 
00106         format.replace( QRegExp("%fmb", false), QString::number( (int)(( totalMem - usedMemNoBuffers)/1024.0+0.5)));
00107         format.replace( QRegExp("%fm", false), QString::number( (int)( ( totalMem - usedMem  )/1024.0+0.5) ));
00108 
00109         format.replace( QRegExp("%umb", false), QString::number( (int)((usedMemNoBuffers)/1024.0+0.5)));
00110         format.replace( QRegExp("%um", false), QString::number( (int)((usedMem)/1024.0+0.5 )));
00111 
00112         format.replace( QRegExp("%tm", false), QString::number( (int)( (totalMem)/1024.0+0.5)));
00113 
00114         format.replace( QRegExp("%fs", false), QString::number( (int)((totalSwap - usedSwap)/1024.0+0.5)));
00115         format.replace( QRegExp("%us", false), QString::number( (int)(usedSwap/1024.0+0.5)));
00116         format.replace( QRegExp("%ts", false), QString::number( (int)(totalSwap/1024.0+0.5)));
00117 
00118         meter->setValue(format);
00119         ++it;
00120     }
00121 }
00122 
00123 void MemSensor::setMaxValue( SensorParams *sp )
00124 {
00125     Meter *meter;
00126     meter = sp->getMeter();
00127     QString f;
00128     f = sp->getParam("FORMAT");
00129 
00130     if (f.length() == 0 )
00131     {
00132         f = "%um";
00133     }
00134     if( f=="%fm" || f== "%um" || f=="%fmb" || f=="%umb" )
00135         meter->setMax( getMemTotal() / 1024 );
00136 
00137     if( f=="%fs" || f== "%us" )
00138         meter->setMax( getSwapTotal() / 1024 );
00139 }

Generated on Mon May 16 13:59:20 2005 for karamba by  doxygen 1.3.9.1