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
|
/****************************************************************************
**
** 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$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, 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>
#ifdef Q_OS_MAC
#include <mach/mach.h>
#include <mach/mach_host.h>
#include <mach/processor_info.h>
#endif
#if defined(Q_OS_LINUX_ANDROID)
#include <ctype.h>
#endif
#include "qcpuidledelegate.h"
QT_BEGIN_NAMESPACE_PROCESSMANAGER
const int kIdleTimerInterval = 1000;
const double kDefaultLoadThreshold = 0.4;
/*!
\class QCpuIdleDelegate
\brief The QCpuIdleDelegate class generates a periodic cpu.
\inmodule QtProcessManager
The QCpuIdleDelegate class generates a periodic cpu for
creation of idle resources. When idle CPU resources are requested
it checks the system load level approximately once per second. When
the load level is below the threshold set by \l{loadThreshold}, the
idleCpuAvailable() signal will be omitted.
*/
/*!
\property QCpuIdleDelegate::idleInterval
\brief Time in milliseconds before a new idle CPU request will be fulfilled
*/
/*!
\property QCpuIdleDelegate::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 QCpuIdleDelegate with an optional \a parent.
*/
QCpuIdleDelegate::QCpuIdleDelegate(QObject *parent)
: QIdleDelegate(parent)
, m_load(1.0)
, m_loadThreshold(kDefaultLoadThreshold)
, m_total(0)
, m_idle(0)
{
connect(&m_timer, SIGNAL(timeout()), SLOT(timeout()));
m_timer.setInterval(kIdleTimerInterval);
}
/*!
Turn on or off idle requests based on \a state.
*/
void QCpuIdleDelegate::handleStateChange(bool state)
{
if (state) {
updateStats(true);
m_timer.start();
}
else
m_timer.stop();
}
/*!
\internal
*/
double QCpuIdleDelegate::updateStats(bool reset)
{
int idle = 0;
int total = 0;
m_load = 1.0; // Always reset to max in case of error
#if defined(Q_OS_LINUX)
QList<QByteArray> stats;
QFile file(QStringLiteral("/proc/stat"));
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QByteArray line = file.readLine();
int offset = 0;
while (offset < line.size() && !isdigit(line.at(offset)))
offset++;
if (offset < line.size())
stats = line.mid(offset).split(' ');
}
if (stats.size() < 5)
qFatal("Unable to read /proc/stat file");
idle = stats.at(3).toInt();
for (int i = 1 ; i < stats.size() ; i++)
total += stats.at(i).toInt();
#elif defined(Q_OS_MAC)
natural_t n_cpus;
processor_info_t pinfo;
mach_msg_type_number_t msg_count;
kern_return_t status = host_processor_info(mach_host_self(),
PROCESSOR_CPU_LOAD_INFO,
&n_cpus,
(processor_info_array_t *)&pinfo,
&msg_count);
if (status != KERN_SUCCESS)
qFatal("Unable to read host processor info");
for (unsigned int cpu = 0 ; cpu < n_cpus ; cpu++) {
processor_info_t p = pinfo + (CPU_STATE_MAX * cpu);
total += p[CPU_STATE_USER] + p[CPU_STATE_SYSTEM]
+ p[CPU_STATE_NICE] + p[CPU_STATE_IDLE];
idle += p[CPU_STATE_IDLE];
}
vm_deallocate(mach_task_self(),
(vm_address_t)pinfo,
(vm_size_t)sizeof(*pinfo) * msg_count);
#endif
if (!reset && total > m_total)
m_load = 1.0 - ((float) (idle - m_idle)) / (total - m_total);
m_total = total;
m_idle = idle;
return m_load;
}
/*!
\internal
*/
void QCpuIdleDelegate::timeout()
{
if (updateStats(false) <= m_loadThreshold)
emit idleCpuAvailable();
emit loadUpdate(m_load);
}
/*!
Return the current launch interval in milliseconds
*/
int QCpuIdleDelegate::idleInterval() const
{
return m_timer.interval();
}
/*!
Set the current idle interval to \a interval milliseconds
*/
void QCpuIdleDelegate::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 QCpuIdleDelegate::loadThreshold() const
{
return m_loadThreshold;
}
/*!
Set the current load threshold to \a threshold milliseconds
*/
void QCpuIdleDelegate::setLoadThreshold(double threshold)
{
if (m_loadThreshold != threshold) {
m_loadThreshold = threshold;
emit loadThresholdChanged();
}
}
/*!
\fn void QCpuIdleDelegate::idleIntervalChanged()
This signal is emitted when the idleInterval is changed.
*/
/*!
\fn void QCpuIdleDelegate::loadThresholdChanged()
This signal is emitted when the loadThreshold is changed.
*/
/*!
\fn void QCpuIdleDelegate::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_qcpuidledelegate.cpp"
QT_END_NAMESPACE_PROCESSMANAGER
|