summaryrefslogtreecommitdiffstats
path: root/src/processmanager/qioidledelegate.cpp
blob: 4be5fbc8c015918e0ac058e1e6f5a28b9a2286a2 (plain)
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
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtAddOn.JsonStream module of the Qt.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QFile>
#include <QDebug>
#include <QStringList>

#ifdef Q_OS_MAC
#include <mach/mach.h>
#include <mach/mach_host.h>
#include <mach/processor_info.h>
#endif

#include "qioidledelegate.h"

QT_BEGIN_NAMESPACE_PROCESSMANAGER

const int    kIdleTimerInterval = 1000;
const double kDefaultLoadThreshold = 0.4;

/*!
  \class QIoIdleDelegate
  \brief The QIoIdleDelegate class generates \l{idleCpuAvailable()} signals.
  \inmodule QtProcessManager

  The QIoIdleDelegate determintes available CPU resources by watching
  the IO activity level on your system.  This delegate is appropriate
  when your system is primarily IO bound and not CPU bound.  When idle
  CPU resources are requested it checks the IO load level
  approximately once per second.  The calculated IO load level is
  measured by looking at the percentage of time spent in IO requests
  over the last time interval.  When the load level is below the
  threshold set by \l{loadThreshold}, the idleCpuAvailable() signal
  will be emitted.

  Under Linux, the \c{/proc/diskstats} file system object provides
  basic IO statistics use across all storage devices.  The
  QIoIdleDelegate reads statistics for exactly one of these devices,
  from the \c{/sys/block/DEVICE/stat} file system entry.  To use the
  QIoIdleDelegate under Linux, you must set the device property to be
  the name of the disk object.

  For example, on a standard Linux laptop, the hard disk usually shows
  up as \c{/dev/sda}, with appropriate file system partitions [on my
  laptop \c{/dev/sda1} is the main partition and \c{/dev/sda5} is the
  swap partition].  If I want to monitor io use on the main device, I
  could write:

  \code
    QIoIdleDelegate *delegate = new QIoIdleDelegate;
    delegate->setDevice("sda1");
  \endcode

  If I want to monitor disk use in general:

  \code
    delegate->setDevice("sda");
  \endcode

*/

/*!
  \property QIoIdleDelegate::idleInterval
  \brief Time in milliseconds before a new idle CPU request will be fulfilled
 */

/*!
  \property QIoIdleDelegate::device
  \brief Unix device name for the disk that you want to measure
 */

/*!
  \property QIoIdleDelegate::loadThreshold
  \brief Load level we need to be under to generate idle CPU requests.

  This value is a double that ranges from 0.0 to 1.0.  A value greater
  than or equal to 1.0 guarantees that \l{idleCpuAvailable()} signals
  will always be emitted.  A value less than 0.0 blocks all \l{idleCpuAvailable()}
  signals.
 */


/*!
    Construct a QIoIdleDelegate with an optional \a parent.
*/

QIoIdleDelegate::QIoIdleDelegate(QObject *parent)
    : QIdleDelegate(parent)
    , m_load(1.0)
    , m_loadThreshold(kDefaultLoadThreshold)
    , m_last_msecs(0)
{
    connect(&m_timer, SIGNAL(timeout()), SLOT(timeout()));
    m_timer.setInterval(kIdleTimerInterval);
}

/*!
    Turn on or off idle requests based on \a state.
*/

void QIoIdleDelegate::handleStateChange(bool state)
{
    if (state) {
        updateStats(true);
        m_elapsed.start();
        m_timer.start();
    }
    else {
        m_elapsed.invalidate();
        m_timer.stop();
    }
}

/*!
  \internal
 */

double QIoIdleDelegate::updateStats(bool reset)
{
    int msecs = 0;
    m_load = 1.0;  // Always reset to max in case of error

#if defined(Q_OS_LINUX)
    QStringList stats;
    QFile file(QString::fromLatin1("/sys/block/%1/stat").arg(m_device));
    if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QString line = QString::fromLocal8Bit(file.readLine());
        stats = line.split(' ', QString::SkipEmptyParts);
    }
    if (stats.size() < 11)
        qFatal("Unable to read /sys/block stat file");

    msecs = stats.at(9).toInt();
#elif defined(Q_OS_MAC)
    // ### TODO
#endif
    if (!reset && m_last_msecs > 0 && m_elapsed.isValid()) {
        qint64 elapsed = m_elapsed.restart();
        if (elapsed > 0)
            m_load = (msecs - m_last_msecs) / (double) elapsed;
    }
    m_last_msecs = msecs;
    return m_load;
}

/*!
  \internal
 */

void QIoIdleDelegate::timeout()
{
    if (updateStats(false) <= m_loadThreshold)
        emit idleCpuAvailable();
    emit loadUpdate(m_load);
}

/*!
  Return the current launch interval in milliseconds
 */

int QIoIdleDelegate::idleInterval() const
{
    return m_timer.interval();
}

/*!
  Set the current idle interval to \a interval milliseconds
*/

void QIoIdleDelegate::setIdleInterval(int interval)
{
    if (m_timer.interval() != interval) {
        m_timer.stop();
        m_timer.setInterval(interval);
        if (enabled() && requested())
            m_timer.start();
        emit idleIntervalChanged();
    }
}

/*!
  Return the current load threshold as a number from 0.0 to 1.0
 */

double QIoIdleDelegate::loadThreshold() const
{
    return m_loadThreshold;
}

/*!
  Set the current load threshold to \a threshold milliseconds
*/

void QIoIdleDelegate::setLoadThreshold(double threshold)
{
    if (m_loadThreshold != threshold) {
        m_loadThreshold = threshold;
        emit loadThresholdChanged();
    }
}

/*!
  Return the current IO device
 */

QString QIoIdleDelegate::device() const
{
    return m_device;
}

/*!
  Set the current IO device
*/

void QIoIdleDelegate::setDevice(const QString& device)
{
    if (m_device != device) {
        m_device = device;
        m_last_msecs = 0;
        emit deviceChanged();
    }
}

/*!
  \fn void QIoIdleDelegate::idleIntervalChanged()
  This signal is emitted when the idleInterval is changed.
 */

/*!
  \fn void QIoIdleDelegate::loadThresholdChanged()
  This signal is emitted when the loadThreshold is changed.
 */

/*!
  \fn void QIoIdleDelegate::deviceChanged()
  This signal is emitted when the device is changed.
 */

/*!
  \fn void QIoIdleDelegate::loadUpdate(double load)
  This signal is emitted when the load is read.  It mainly
  serves as a debugging signal.  The \a load value will
  range between 0.0 and 1.0
 */


#include "moc_qioidledelegate.cpp"

QT_END_NAMESPACE_PROCESSMANAGER