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

superkaramba

  • sources
  • kde-4.12
  • kdeutils
  • superkaramba
  • src
  • sensors
cpu.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2003 by Hans Karlsson *
3  * karlsson.h@home.se *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  ***************************************************************************/
10 
11 #include "cpu.h"
12 
13 #include <QTextStream>
14 #include <QFile>
15 
16 #ifdef __FreeBSD__
17 #include <sys/time.h>
18 #include <sys/dkstat.h>
19 #include <sys/param.h>
20 #include <sys/sysctl.h>
21 #include <sys/resource.h>
22 #endif
23 
24 #if defined(Q_OS_NETBSD)
25 #include <sys/param.h>
26 #include <sys/sysctl.h>
27 #include <sys/sched.h>
28 #endif
29 
30 CPUSensor::CPUSensor(const QString &cpu, int interval) :
31  Sensor(interval), userTicks(0), sysTicks(0), niceTicks(0), idleTicks(0)
32 {
33  cpuNbr = cpu;
34  QRegExp rx("^\\d+$");
35  if (rx.indexIn(cpu.toLower()) == -1)
36  cpuNbr = "";
37  cpuNbr = "cpu" + cpuNbr;
38  getCPULoad();
39 }
40 
41 CPUSensor::~CPUSensor()
42 {}
43 
44 void CPUSensor::getTicks(long &u, long &s, long &n, long &i)
45 {
46 #ifdef __FreeBSD__
47  static long cp_time[CPUSTATES];
48 
49  size_t size = sizeof(cp_time);
50 
51  /* get the cp_time array */
52  if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) != -1) {
53  u = cp_time[CP_USER];
54  s = cp_time[CP_SYS] + cp_time[CP_INTR];
55  n = cp_time[CP_NICE];
56  i = cp_time[CP_IDLE];
57  }
58 #else
59 #if defined(Q_OS_NETBSD)
60  static uint64_t cp_time[CPUSTATES];
61 
62  size_t size = sizeof(cp_time);
63 
64  /* get the cp_time array */
65  if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) != -1) {
66  u = cp_time[CP_USER];
67  s = cp_time[CP_SYS] + cp_time[CP_INTR];
68  n = cp_time[CP_NICE];
69  i = cp_time[CP_IDLE];
70  }
71 #else
72  QFile file("/proc/stat");
73  QString line;
74  if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
75  QTextStream t(&file); // use a text stream
76  QRegExp rx(cpuNbr + "\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)");
77  line = t.readLine();
78  rx.indexIn(line);
79 
80  while ((line = t.readLine()) != 0 && rx.cap(0).isEmpty()) {
81  rx.indexIn(line);
82  }
83  //user
84  u = rx.cap(1).toLong();
85  //nice
86  n = rx.cap(2).toLong();
87  //system
88  s = rx.cap(3).toLong();
89  //idle
90  i = rx.cap(4).toLong();
91  file.close();
92  }
93 #endif
94 #endif
95  else {
96  u = 0;
97  s = 0;
98  n = 0;
99  i = 0;
100  }
101 }
102 
103 int CPUSensor::getCPULoad()
104 {
105  long uTicks, sTicks, nTicks, iTicks;
106 
107  getTicks(uTicks, sTicks, nTicks, iTicks);
108 
109  const long totalTicks = ((uTicks - userTicks) +
110  (sTicks - sysTicks) +
111  (nTicks - niceTicks) +
112  (iTicks - idleTicks));
113 
114  int load = (totalTicks == 0) ? 0 : (int)(100.0 * ((uTicks + sTicks + nTicks) - (userTicks + sysTicks + niceTicks)) / (totalTicks + 0.001) + 0.5);
115  user = (totalTicks == 0) ? 0 : (int)(100.0 * (uTicks - userTicks) / (totalTicks + 0.001) + 0.5);
116  idle = (totalTicks == 0) ? 0 : (int)(100.0 * (iTicks - idleTicks) / (totalTicks + 0.001) + 0.5);
117  system = (totalTicks == 0) ? 0 : (int)(100.0 * (sTicks - sysTicks) / (totalTicks + 0.001) + 0.5);
118  nice = (totalTicks == 0) ? 0 : (int)(100.0 * (nTicks - niceTicks) / (totalTicks + 0.001) + 0.5);
119  suload = (totalTicks == 0) ? 0 : (int)(100.0 * ((uTicks + sTicks) - (userTicks + sysTicks)) / (totalTicks+0.001) + 0.5);
120 
121  userTicks = uTicks;
122  sysTicks = sTicks;
123  niceTicks = nTicks;
124  idleTicks = iTicks;
125 
126  return load;
127 }
128 
129 void CPUSensor::update()
130 {
131  SensorParams *sp;
132  Meter *meter;
133  QString format;
134  int load = getCPULoad();
135 
136  QObject *it;
137  foreach(it, *objList) {
138  sp = qobject_cast<SensorParams*>(it);
139  meter = sp->getMeter();
140  format = sp->getParam("FORMAT");
141 
142  if (format.length() == 0) {
143  format = QString::number(load);
144  meter->setValue(format);
145  continue;
146  }
147 
148  int index = 0;
149 
150  index = format.indexOf("%load", 0, Qt::CaseInsensitive);
151  if (index != -1)
152  format.replace(index, 5, QString::number(load));
153 
154  index = format.indexOf("%v", 0, Qt::CaseInsensitive);
155  if (index != -1)
156  format.replace(index, 2, QString::number(load));
157 
158  index = format.indexOf("%user", 0, Qt::CaseInsensitive);
159  if (index != -1)
160  format.replace(index, 5, QString::number(user));
161 
162  index = format.indexOf("%nice", 0, Qt::CaseInsensitive);
163  if (index != -1)
164  format.replace(index, 5, QString::number(nice));
165 
166  index = format.indexOf("%system", 0, Qt::CaseInsensitive);
167  if (index != -1)
168  format.replace(index, 7, QString::number(system));
169 
170  index = format.indexOf("%idle", 0, Qt::CaseInsensitive);
171  if (index != -1)
172  format.replace(index, 5, QString::number(idle));
173 
174  index = format.indexOf("%suload", 0, Qt::CaseInsensitive);
175  if (index != -1)
176  format.replace(index, 7, QString::number(suload));
177 
178  meter->setValue(format);
179  }
180 }
181 
182 void CPUSensor::setMaxValue(SensorParams *sp)
183 {
184  Meter *meter;
185  meter = sp->getMeter();
186  meter->setMax(100);
187 }
188 
189 #include "cpu.moc"
CPUSensor::update
void update()
Definition: cpu.cpp:129
CPUSensor::getCPULoad
int getCPULoad()
Definition: cpu.cpp:103
CPUSensor::~CPUSensor
~CPUSensor()
Definition: cpu.cpp:41
SensorParams::getMeter
Meter * getMeter() const
Definition: sensorparams.cpp:31
QObject
Sensor
Definition: sensor.h:17
SensorParams
Hans Karlsson.
Definition: sensorparams.h:30
CPUSensor::CPUSensor
CPUSensor(const QString &cpu, int interval)
Definition: cpu.cpp:30
SensorParams::getParam
QString getParam(const QString &) const
Definition: sensorparams.cpp:26
Meter::setMax
virtual void setMax(int max)
Definition: meters/meter.cpp:126
cpu.h
CPUSensor::setMaxValue
void setMaxValue(SensorParams *sp)
Definition: cpu.cpp:182
Meter
Definition: meters/meter.h:23
Sensor::objList
QList< QObject * > * objList
Definition: sensor.h:39
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:07:19 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

superkaramba

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

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • kremotecontrol
  • ktimer
  • kwallet
  • superkaramba
  • sweeper

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal