Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Examples

LCDCpuSensor.cpp

00001 #include "LCDCpuSensor.h"
00002 #include "LCDUtils.h"
00003 
00004 #include <fstream>
00005 #include <sys/types.h>
00006 #include <time.h>
00007 #include <unistd.h>
00008 #include <string>
00009 
00010 using namespace std;
00011 
00012 LCDCpuSensor::LCDCpuSensor(string cpuName)
00013 {
00014   _cpuName = "cpu";
00015   _userTicks = 0;
00016   _sysTicks = 0;
00017   _niceTicks = 0;
00018   _idleTicks = 0;
00019 }
00020 
00021 void LCDCpuSensor::waitForChange()
00022 {
00023   sleep(1);
00024 }
00025 
00026 string LCDCpuSensor::getCurrentValue()
00027 {
00028   long uTicks, sTicks, nTicks, iTicks;
00029 
00030   getTicks(uTicks, sTicks, nTicks, iTicks);
00031 
00032   const long totalTicks = ((uTicks - _userTicks) +
00033                            (sTicks - _sysTicks) +
00034                            (nTicks - _niceTicks) +
00035                            (iTicks - _idleTicks));
00036 
00037   int load  = (totalTicks == 0) ? 0 : (int) ( 100.0 * ( (uTicks+sTicks+nTicks) - (_userTicks+_sysTicks+_niceTicks))/( totalTicks+0.001) + 0.5 );
00038 
00039   _userTicks = uTicks;
00040   _sysTicks = sTicks;
00041   _niceTicks = nTicks;
00042   _idleTicks = iTicks;
00043 
00044   return intToString(load);
00045 }
00046 
00047 void LCDCpuSensor::getTicks (long &u,long &s,long &n,long &i)
00048 {
00049     fstream file;
00050     string item = "";
00051 
00052     file.open("/proc/stat", ios::in);
00053 
00054     if (file.is_open())
00055     {
00056       while (item != _cpuName)
00057       {
00058         file >> item;
00059       }
00060       file >> u >> s >> n >> i;
00061       
00062       file.close();
00063     }
00064     else
00065     {
00066         u = 0;
00067         s = 0;
00068         n = 0;
00069         i = 0;
00070     }
00071 
00072 }

Generated on Wed Aug 4 19:39:09 2004 for LCDApi by doxygen 1.3.6