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

cpusensor.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 
00011 #include "cpusensor.h"
00012 
00013 CPUSensor::CPUSensor( QString cpu, int interval ) : Sensor( interval )
00014 {
00015     cpuNbr = cpu;
00016     QRegExp rx("^\\d+$");
00017     if( rx.search( cpu.lower() ) == -1)
00018         cpuNbr = "";
00019     cpuNbr = "cpu"+cpuNbr;
00020     getCPULoad();
00021 }
00022 CPUSensor::~CPUSensor()
00023 {}
00024 
00025 void CPUSensor::getTicks (long &u,long &s,long &n,long &i)
00026 {
00027     QFile file("/proc/stat");
00028     QString line;
00029     if ( file.open(IO_ReadOnly | IO_Translate) )
00030     {
00031         QTextStream t( &file );        // use a text stream
00032         QRegExp rx( cpuNbr+"\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)");
00033         line = t.readLine();
00034         rx.search( line );
00035         while( (line = t.readLine()) !=0 && rx.cap(0) == "" )
00036         {
00037             rx.search( line );
00038         }
00039         //user
00040         u = rx.cap(1).toLong();
00041         //system
00042         s = rx.cap(2).toLong();
00043         //nice
00044         n = rx.cap(3).toLong();
00045         //idle
00046         i = rx.cap(4).toLong();
00047         file.close();
00048     }
00049     else
00050     {
00051         u = 0;
00052         s = 0;
00053         n = 0;
00054         i = 0;
00055     }
00056 
00057 }
00058 
00059 int CPUSensor::getCPULoad()
00060 {
00061     long uTicks, sTicks, nTicks, iTicks;
00062 
00063     getTicks(uTicks, sTicks, nTicks, iTicks);
00064 
00065     const long totalTicks = ((uTicks - userTicks) +
00066                              (sTicks - sysTicks) +
00067                              (nTicks - niceTicks) +
00068                              (iTicks - idleTicks));
00069 
00070     int load  = (totalTicks == 0) ? 0 : (int) ( 100.0 * ( (uTicks+sTicks+nTicks) - (userTicks+sysTicks+niceTicks))/( totalTicks+0.001) + 0.5 );
00071     user = (totalTicks == 0) ? 0 : (int) ( 100.0 * ( uTicks - userTicks)/( totalTicks+0.001) + 0.5 );
00072     idle = (totalTicks == 0) ? 0 : (int) ( 100.0 * ( iTicks - idleTicks)/( totalTicks+0.001) + 0.5 );
00073     system = (totalTicks == 0) ? 0 : (int) ( 100.0 * ( sTicks - sysTicks)/( totalTicks+0.001) + 0.5 );
00074     nice = (totalTicks == 0) ? 0 : (int) ( 100.0 * ( nTicks - niceTicks)/( totalTicks+0.001) + 0.5 );
00075 
00076     userTicks = uTicks;
00077     sysTicks = sTicks;
00078     niceTicks = nTicks;
00079     idleTicks = iTicks;
00080 
00081     return load;
00082 }
00083 
00084 void CPUSensor::update()
00085 {
00086     SensorParams *sp;
00087     Meter *meter;
00088     QString format;
00089     int load = getCPULoad();
00090 
00091     QObjectListIt it( *objList );
00092     while (it != 0)
00093     {
00094         sp = (SensorParams*)(*it);
00095         meter = sp->getMeter();
00096         format = sp->getParam( "FORMAT" );
00097 
00098         if( format.length() == 0)
00099         {
00100             format = "%v";
00101         }
00102         format.replace( QRegExp("%load", false), QString::number( load ) );
00103         format.replace( QRegExp("%user", false), QString::number( user ) );
00104         format.replace( QRegExp("%nice", false), QString::number( nice ) );
00105         format.replace( QRegExp("%idle", false), QString::number( idle ) );
00106         format.replace( QRegExp("%system", false), QString::number( system ) );
00107         format.replace( QRegExp("%v", false), QString::number( load ) );
00108 
00109         meter->setValue( format );
00110         ++it;
00111     }
00112 }
00113 
00114 void CPUSensor::setMaxValue( SensorParams *sp )
00115 {
00116     Meter *meter;
00117     meter = sp->getMeter();
00118     meter->setMax( 100 );
00119 }

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