File: cpuload.cpp

package info (click to toggle)
libqwt 4.2.0-6
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 9,832 kB
  • ctags: 5,512
  • sloc: cpp: 22,973; ansic: 244; makefile: 64
file content (291 lines) | stat: -rw-r--r-- 5,995 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <iostream>
#include <fstream>
#ifdef QWT_QTOPIA
#include <qpe/qpeapplication.h>
#endif
#include <qapplication.h>
#include <qfont.h>
#include <qlabel.h>
#include <qpopupmenu.h>
#include <qmenubar.h>
#include <qpushbutton.h>

#include <qwt_thermo.h>
#include <qwt_math.h>
#include "cpuload.h"

Lowpass::Lowpass()
{
    tsampl = 1.0;
    tconst = 0.0;
    recalc();
    reset(0.0);
}

double Lowpass::input(double v)
{
    val = c2 * v + c1 * val;
    return val;
}

void Lowpass::recalc()
{
    if (tconst > 0.00001)
       c1 = exp(-tsampl / tconst);
    else
       c1 = 0.0;

    c2 = 1.0 - c1;
}


void MainWin::setTSampl(double sec)
{
    int i; 
    int ms = int(sec * 1000.0);

    if(tmrID != 0) 
       killTimer(tmrID);

    for(i=0;i<ThermoCnt;i++)
       lp[i].setTSampl(sec);

    tmrID = startTimer(ms);
}

void MainWin::setTConst(double sec)
{
    for(int i=0;i<ThermoCnt;i++)
       lp[i].setTConst(sec);
}

void MainWin::read()
{
    static char buffer[20];

    std::ifstream inp("/proc/stat");
    inp >> buffer;
    for(int i = 0; i < ThermoCnt; i++)
    {
        old[i] = val[i];
        inp >> val[i];
    }
}


//---------------------------------------
// MainWin::update()
//
//  read values from /proc/loadavg
//  and display them. Adjust thermometer scales
//  if necessary.
//---------------------------------------
void MainWin::update()
{
    unsigned long delta[ThermoCnt];
    unsigned long sum = 0;
    double factor;
    double newval,maxval;
    int i;

    read();

    for(i=0;i<ThermoCnt;i++)
    {
        delta[i] = val[i] - old[i];
        sum += delta[i];
    }

    if (sum > 0)
       factor = 100.0 / double(sum);
    else 
       factor = 0.0;

    if (dynscale)
    {
        for(i=0;i<ThermoCnt;i++)
        {
            newval = lp[i].input(double(delta[i]) * factor);
            maxval = qwtMax(qwtCeil125(newval), 3.0);
            if ( (maxval > th[i]->maxValue()) 
                || (maxval < 0.35 * th[i]->maxValue()) )
            {
               th[i]->setRange(0.0,qwtCeil125(maxval));
            }
            th[i] -> setValue(newval);
        }
    }
    else
    {           
        for(i=0;i<ThermoCnt;i++)
           th[i]->setValue(lp[i].input(double(delta[i]) * factor));     
    }
}

//---------------------------------------
// MainWin::timerEvent
//
//  update thermometers
//---------------------------------------
void MainWin::timerEvent(QTimerEvent *)
{
    update();
}

MainWin::MainWin(QApplication &a): 
    QWidget()
{
    int i;
    QColor cFill("MidnightBlue");

    for(i=0;i<ThermoCnt;i++)
       lp[i].reset(0);
    
    for(i=0;i<ThermoCnt;i++)
    {
        th[i] = new QwtThermo(this,"");
        lb[i] = new QLabel(this);
    }

    puProg = new QPopupMenu;
    puProg->insertItem("Quit", &a, SLOT(quit()));

    puScale = new QPopupMenu;
    puScale->insertItem("Dynamic", this, SLOT(setDynScale()));
    puScale->insertItem("Fixed (0-100)", this, SLOT(setFixedScale()));


    puConf = new QPopupMenu;
    puConf->insertItem("Update Mode...", this, SLOT(showDialog()));
    puConf->insertItem("Scale", puScale);

    menu = new QMenuBar(this);
    menu->insertItem("Program", puProg);
    menu->insertItem("Options", puConf);
    
    cfg = new ConfigDiag;

    lb[CpuUser]->setText("% User");
    lb[CpuNice]->setText("% Nice");
    lb[CpuSystem]->setText("% System");
    lb[CpuIdle]->setText(" % Idle");

    for(i=0;i<ThermoCnt;i++)
    {
        th[i]->setGeometry(10 + i*60 ,30,50,100);
        lb[i]->setGeometry(10 + i*60, 130,50,20);
    }

    for(i=0;i<ThermoCnt;i++)
    {
        th[i]->setOrientation(Qt::Vertical, QwtThermo::Left);
        th[i]->setRange(0.0,100.0);
        th[i]->setValue(0.0);
        th[i]->setBorderWidth(1);
        th[i]->setPipeWidth(4);
        th[i]->setFont(QFont("Helvetica",10));
        th[i]->setScaleMaxMajor(6);
        th[i]->setScaleMaxMinor(5);
        th[i]->setFillColor(cFill);

        lb[i]->setAlignment(Qt::AlignRight|Qt::AlignTop);
    }
    
    dynscale = 0;
    tmrID = 0;

    connect(cfg, SIGNAL(tConstChg(double)), SLOT(setTConst(double)));
    connect(cfg, SIGNAL(tSamplChg(double)), SLOT(setTSampl(double)));

    read();
    setTConst(1.0);
    setTSampl(0.2);
    cfg->setTConst(1.0);
    cfg->setTSampl(0.2);

    setFixedSize(10 + ThermoCnt*60 ,150);
}

void MainWin::setFixedScale()
{
    dynscale = 0;
    for(int i=0;i<ThermoCnt;i++)
       th[i]->setRange(0.0,100.0);
}

MainWin::~MainWin()
{
    for(int i=0;i<ThermoCnt;i++)
    {
        delete th[i];
        delete lb[i];
    }
    delete cfg;
    delete menu;
    delete puProg;

    if (tmrID != 0) 
        killTimer(tmrID);
}

void MainWin::showDialog()
{
    cfg->show();
}

ConfigDiag::ConfigDiag(QWidget *p, const char *name): 
    QDialog(p, name)
{
    ctConst = new QwtCounter(this);
    ctSampl = new QwtCounter(this);
    lbSampl = new QLabel("Update Rate [s]", this);
    lbConst = new QLabel("Average Time [s]", this);
    btDismiss = new QPushButton("Dismiss",this);

    lbSampl->setGeometry(10,10,120,20);
    lbConst->setGeometry(10,40,120,20);
    ctSampl->setGeometry(130,10,120,20);
    ctConst->setGeometry(130,40,120,20);
    btDismiss->setGeometry(95,75,70,25);

    ctConst->setRange(0.0,60,0.5);
    ctSampl->setRange(0.1,5.0,0.1);

    setCaption("CPU Load - Configuration");
    setFixedSize(260,110);

    connect(btDismiss, SIGNAL(clicked()), SLOT(accept()));
    connect(ctConst, SIGNAL(valueChanged(double)), SLOT(chgTConst(double)));
    connect(ctSampl, SIGNAL(valueChanged(double)), SLOT(chgTSampl(double)));
}

ConfigDiag::~ConfigDiag()
{
}

void ConfigDiag::setTSampl(double t)
{
    ctSampl->setValue(t);
}

void ConfigDiag::setTConst(double t)
{
    ctConst->setValue(t);
}


int main (int argc, char **argv)
{
#ifdef QWT_QTOPIA
    QPEApplication a(argc, argv);
#else
    QApplication a(argc, argv);
#endif

    MainWin w(a);
    a.setMainWidget(&w);
    w.setCaption("CPU Load");
    w.show();
    
    return a.exec();
}