• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • kdeutils
  • Sitemap
  • Contact Us
 

superkaramba

cpu.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 "cpu.h"
00012 
00013 #include <QTextStream>
00014 #include <QFile>
00015 
00016 #ifdef __FreeBSD__
00017 #include <sys/time.h>
00018 #include <sys/dkstat.h>
00019 #include <sys/param.h>
00020 #include <sys/sysctl.h>
00021 #include <sys/resource.h>
00022 #endif
00023 
00024 #if defined(Q_OS_NETBSD)
00025 #include <sys/param.h>
00026 #include <sys/sysctl.h>
00027 #include <sys/sched.h>
00028 #endif
00029 
00030 CPUSensor::CPUSensor(const QString &cpu, int interval) :
00031         Sensor(interval), userTicks(0), sysTicks(0), niceTicks(0), idleTicks(0)
00032 {
00033     cpuNbr = cpu;
00034     QRegExp rx("^\\d+$");
00035     if (rx.indexIn(cpu.toLower()) == -1)
00036         cpuNbr = "";
00037     cpuNbr = "cpu" + cpuNbr;
00038     getCPULoad();
00039 }
00040 
00041 CPUSensor::~CPUSensor()
00042 {}
00043 
00044 void CPUSensor::getTicks(long &u, long &s, long &n, long &i)
00045 {
00046 #ifdef __FreeBSD__
00047     static long cp_time[CPUSTATES];
00048 
00049     size_t size = sizeof(cp_time);
00050 
00051     /* get the cp_time array */
00052     if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) != -1) {
00053         u = cp_time[CP_USER];
00054         s = cp_time[CP_SYS] + cp_time[CP_INTR];
00055         n = cp_time[CP_NICE];
00056         i = cp_time[CP_IDLE];
00057     }
00058 #else
00059 #if defined(Q_OS_NETBSD)
00060     static uint64_t cp_time[CPUSTATES];
00061 
00062     size_t size = sizeof(cp_time);
00063 
00064     /* get the cp_time array */
00065     if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) != -1) {
00066         u = cp_time[CP_USER];
00067         s = cp_time[CP_SYS] + cp_time[CP_INTR];
00068         n = cp_time[CP_NICE];
00069         i = cp_time[CP_IDLE];
00070     }
00071 #else
00072     QFile file("/proc/stat");
00073     QString line;
00074     if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
00075         QTextStream t(&file);          // use a text stream
00076         QRegExp rx(cpuNbr + "\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)");
00077         line = t.readLine();
00078         rx.indexIn(line);
00079 
00080         while ((line = t.readLine()) != 0 && rx.cap(0).isEmpty()) {
00081             rx.indexIn(line);
00082         }
00083         //user
00084         u = rx.cap(1).toLong();
00085         //nice
00086         n = rx.cap(2).toLong();
00087         //system
00088         s = rx.cap(3).toLong();
00089         //idle
00090         i = rx.cap(4).toLong();
00091         file.close();
00092     }
00093 #endif
00094 #endif
00095     else {
00096         u = 0;
00097         s = 0;
00098         n = 0;
00099         i = 0;
00100     }
00101 }
00102 
00103 int CPUSensor::getCPULoad()
00104 {
00105     long uTicks, sTicks, nTicks, iTicks;
00106 
00107     getTicks(uTicks, sTicks, nTicks, iTicks);
00108 
00109     const long totalTicks = ((uTicks - userTicks) +
00110                              (sTicks - sysTicks) +
00111                              (nTicks - niceTicks) +
00112                              (iTicks - idleTicks));
00113 
00114     int load  = (totalTicks == 0) ? 0 : (int)(100.0 * ((uTicks + sTicks + nTicks) - (userTicks + sysTicks + niceTicks)) / (totalTicks + 0.001) + 0.5);
00115     user = (totalTicks == 0) ? 0 : (int)(100.0 * (uTicks - userTicks) / (totalTicks + 0.001) + 0.5);
00116     idle = (totalTicks == 0) ? 0 : (int)(100.0 * (iTicks - idleTicks) / (totalTicks + 0.001) + 0.5);
00117     system = (totalTicks == 0) ? 0 : (int)(100.0 * (sTicks - sysTicks) / (totalTicks + 0.001) + 0.5);
00118     nice = (totalTicks == 0) ? 0 : (int)(100.0 * (nTicks - niceTicks) / (totalTicks + 0.001) + 0.5);
00119     suload = (totalTicks == 0) ? 0 : (int)(100.0 * ((uTicks + sTicks) - (userTicks + sysTicks)) / (totalTicks+0.001) + 0.5);
00120 
00121     userTicks = uTicks;
00122     sysTicks = sTicks;
00123     niceTicks = nTicks;
00124     idleTicks = iTicks;
00125 
00126     return load;
00127 }
00128 
00129 void CPUSensor::update()
00130 {
00131     SensorParams *sp;
00132     Meter *meter;
00133     QString format;
00134     int load = getCPULoad();
00135 
00136     QObject *it;
00137     foreach(it, *objList) {
00138         sp = qobject_cast<SensorParams*>(it);
00139         meter = sp->getMeter();
00140         format = sp->getParam("FORMAT");
00141 
00142         if (format.length() == 0) {
00143             format = QString::number(load);
00144             meter->setValue(format);
00145             continue;
00146         }
00147 
00148         int index = 0;
00149 
00150         index = format.indexOf("%load", 0, Qt::CaseInsensitive);
00151         if (index != -1)
00152             format.replace(index, 5, QString::number(load));
00153 
00154         index = format.indexOf("%v", 0, Qt::CaseInsensitive);
00155         if (index != -1)
00156             format.replace(index, 2, QString::number(load));
00157 
00158         index = format.indexOf("%user", 0, Qt::CaseInsensitive);
00159         if (index != -1)
00160             format.replace(index, 5, QString::number(user));
00161 
00162         index = format.indexOf("%nice", 0, Qt::CaseInsensitive);
00163         if (index != -1)
00164             format.replace(index, 5, QString::number(nice));
00165 
00166         index = format.indexOf("%system", 0, Qt::CaseInsensitive);
00167         if (index != -1)
00168             format.replace(index, 7, QString::number(system));
00169 
00170         index = format.indexOf("%idle", 0, Qt::CaseInsensitive);
00171         if (index != -1)
00172           format.replace(index, 5, QString::number(idle));
00173 
00174         index = format.indexOf("%suload", 0, Qt::CaseInsensitive);
00175         if (index != -1)
00176           format.replace(index, 7, QString::number(suload));
00177 
00178         meter->setValue(format);
00179     }
00180 }
00181 
00182 void CPUSensor::setMaxValue(SensorParams *sp)
00183 {
00184     Meter *meter;
00185     meter = sp->getMeter();
00186     meter->setMax(100);
00187 }
00188 
00189 #include "cpu.moc"

superkaramba

Skip menu "superkaramba"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kdeutils

Skip menu "kdeutils"
  • ark
  • kcalc
  • kcharselect
  • kdelirc
  • kdessh
  • kdf
  • kfloppy
  • kgpg
  • kjots
  • klaptopdaemon
  • kmilo
  • ksim
  • ktimer
  • kwallet
  • superkaramba
Generated for kdeutils by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal