• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • superkaramba
 

superkaramba

  • superkaramba
  • src
cpusensor.cpp
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 <tqglobal.h>
12 
13 #ifdef __FreeBSD__
14 #include <sys/time.h>
15 #include <sys/dkstat.h>
16 #include <sys/param.h>
17 #include <sys/sysctl.h>
18 #include <sys/resource.h>
19 #endif
20 
21 #if defined(Q_OS_NETBSD)
22 #include <sys/param.h>
23 #include <sys/sysctl.h>
24 #include <sys/sched.h>
25 #endif
26 
27 #include "cpusensor.h"
28 
29 CPUSensor::CPUSensor( TQString cpu, int interval ) :
30  Sensor(interval), userTicks(0), sysTicks(0), niceTicks(0), idleTicks(0)
31 {
32  cpuNbr = cpu;
33  TQRegExp rx("^\\d+$");
34  if( rx.search( cpu.lower() ) == -1)
35  cpuNbr = "";
36  cpuNbr = "cpu"+cpuNbr;
37  getCPULoad();
38 }
39 
40 CPUSensor::~CPUSensor()
41 {
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  TQFile file("/proc/stat");
73  TQString line;
74  if ( file.open(IO_ReadOnly | IO_Translate) )
75  {
76  TQTextStream t( &file ); // use a text stream
77  TQRegExp rx( cpuNbr+"\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)");
78  line = t.readLine();
79  rx.search( line );
80  while( (line = t.readLine()) !=0 && rx.cap(0) == "" )
81  {
82  rx.search( line );
83  }
84  //user
85  u = rx.cap(1).toLong();
86  //nice
87  n = rx.cap(2).toLong();
88  //system
89  s = rx.cap(3).toLong();
90  //idle
91  i = rx.cap(4).toLong();
92  file.close();
93  }
94 #endif
95 #endif
96  else
97  {
98  u = 0;
99  s = 0;
100  n = 0;
101  i = 0;
102  }
103 }
104 
105 int CPUSensor::getCPULoad()
106 {
107  long uTicks, sTicks, nTicks, iTicks;
108 
109  getTicks(uTicks, sTicks, nTicks, iTicks);
110 
111  const long totalTicks = ((uTicks - userTicks) +
112  (sTicks - sysTicks) +
113  (nTicks - niceTicks) +
114  (iTicks - idleTicks));
115 
116  int load = (totalTicks == 0) ? 0 : (int) ( 100.0 * ( (uTicks+sTicks+nTicks) - (userTicks+sysTicks+niceTicks))/( totalTicks+0.001) + 0.5 );
117  user = (totalTicks == 0) ? 0 : (int) ( 100.0 * ( uTicks - userTicks)/( totalTicks+0.001) + 0.5 );
118  idle = (totalTicks == 0) ? 0 : (int) ( 100.0 * ( iTicks - idleTicks)/( totalTicks+0.001) + 0.5 );
119  system = (totalTicks == 0) ? 0 : (int) ( 100.0 * ( sTicks - sysTicks)/( totalTicks+0.001) + 0.5 );
120  nice = (totalTicks == 0) ? 0 : (int) ( 100.0 * ( nTicks - niceTicks)/( totalTicks+0.001) + 0.5 );
121 
122  userTicks = uTicks;
123  sysTicks = sTicks;
124  niceTicks = nTicks;
125  idleTicks = iTicks;
126 
127  return load;
128 }
129 
130 void CPUSensor::update()
131 {
132  SensorParams *sp;
133  Meter *meter;
134  TQString format;
135  int load = getCPULoad();
136 
137  TQObjectListIt it( *objList );
138  while (it != 0)
139  {
140  sp = (SensorParams*)(*it);
141  meter = sp->getMeter();
142  format = sp->getParam( "FORMAT" );
143 
144  if( format.length() == 0)
145  {
146  format = "%v";
147  }
148  format.replace( TQRegExp("%load", false), TQString::number( load ) );
149  format.replace( TQRegExp("%user", false), TQString::number( user ) );
150  format.replace( TQRegExp("%nice", false), TQString::number( nice ) );
151  format.replace( TQRegExp("%idle", false), TQString::number( idle ) );
152  format.replace( TQRegExp("%system", false), TQString::number( system ) );
153  format.replace( TQRegExp("%v", false), TQString::number( load ) );
154 
155  meter->setValue( format );
156  ++it;
157  }
158 }
159 
160 void CPUSensor::setMaxValue( SensorParams *sp )
161 {
162  Meter *meter;
163  meter = sp->getMeter();
164  meter->setMax( 100 );
165 }
166 
167 #include "cpusensor.moc"
SensorParams
Hans Karlsson.
Definition: sensorparams.h:31

superkaramba

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

superkaramba

Skip menu "superkaramba"
  • kcalc
  •   knumber
  • superkaramba
Generated for superkaramba by doxygen 1.8.13
This website is maintained by Timothy Pearson.