Line 0
Link Here
|
|
|
1 |
--- cpustat.cpp.orig 2017-09-23 12:46:10 UTC |
2 |
+++ cpustat.cpp |
3 |
@@ -23,15 +23,68 @@ |
4 |
** |
5 |
** END_COMMON_COPYRIGHT_HEADER */ |
6 |
|
7 |
- |
8 |
+#ifdef HAVE_CONFIG_H |
9 |
+#include "config.h" |
10 |
+#endif |
11 |
#include <unistd.h> |
12 |
+#ifdef HAVE_SYSCTL_H |
13 |
+extern "C" |
14 |
+{ |
15 |
+ #include <stdlib.h> |
16 |
+ #include <limits.h> |
17 |
+ #include <string.h> |
18 |
+ #include <sys/resource.h> /* CPUSTATES */ |
19 |
|
20 |
+ #include <sys/types.h> |
21 |
+ #include <sys/sysctl.h> |
22 |
+} |
23 |
+#endif |
24 |
#include "cpustat.h" |
25 |
#include "cpustat_p.h" |
26 |
|
27 |
|
28 |
namespace SysStat { |
29 |
|
30 |
+#ifdef HAVE_SYSCTL_H |
31 |
+char *GetFirstFragment(char *string, const char *delim) |
32 |
+{ |
33 |
+ char *token = NULL; |
34 |
+ |
35 |
+ token = strsep(&string, delim); |
36 |
+ if (token != NULL) |
37 |
+ { |
38 |
+ /* We need only the first fragment, so no loop! */ |
39 |
+ return token; |
40 |
+ } |
41 |
+ else |
42 |
+ return NULL; |
43 |
+} |
44 |
+ |
45 |
+int GetCpu(void) |
46 |
+{ |
47 |
+ static int mib[] = { CTL_HW, HW_NCPU }; |
48 |
+ int buf; |
49 |
+ size_t len = sizeof(int); |
50 |
+ |
51 |
+ if (sysctl(mib, 2, &buf, &len, NULL, 0) < 0) |
52 |
+ return 0; |
53 |
+ else |
54 |
+ return buf; |
55 |
+} |
56 |
+ |
57 |
+/* Frequence is in MHz */ |
58 |
+ulong CurrentFreq(void) |
59 |
+{ |
60 |
+ ulong freq; |
61 |
+ size_t len = sizeof(freq); |
62 |
+ |
63 |
+ if (sysctlbyname("dev.cpu.0.freq", &freq, &len, NULL, 0) < 0) |
64 |
+ return 0; |
65 |
+ else |
66 |
+ return freq; |
67 |
+} |
68 |
+#endif |
69 |
+ |
70 |
CpuStatPrivate::CpuStatPrivate(CpuStat *parent) |
71 |
: BaseStatPrivate(parent) |
72 |
, mMonitoring(CpuStat::LoadAndFrequency) |
73 |
@@ -39,7 +92,10 @@ CpuStatPrivate::CpuStatPrivate(CpuStat * |
74 |
mSource = defaultSource(); |
75 |
|
76 |
connect(mTimer, SIGNAL(timeout()), SLOT(timeout())); |
77 |
- |
78 |
+#ifdef HAVE_SYSCTL_H |
79 |
+ size_t flen=2; |
80 |
+ sysctlnametomib("kern.cp_times",mib,&flen); |
81 |
+#endif |
82 |
mUserHz = sysconf(_SC_CLK_TCK); |
83 |
|
84 |
updateSources(); |
85 |
@@ -47,6 +103,49 @@ CpuStatPrivate::CpuStatPrivate(CpuStat * |
86 |
|
87 |
void CpuStatPrivate::addSource(const QString &source) |
88 |
{ |
89 |
+#ifdef HAVE_SYSCTL_H |
90 |
+ char buf[1024]; |
91 |
+ char *tokens, *t; |
92 |
+ ulong min = 0, max = 0; |
93 |
+ size_t len = sizeof(buf); |
94 |
+ |
95 |
+ /* The string returned by the dev.cpu.0.freq_levels sysctl |
96 |
+ * is a space separated list of MHz/milliwatts. |
97 |
+ */ |
98 |
+ if (sysctlbyname("dev.cpu.0.freq_levels", buf, &len, NULL, 0) < 0) |
99 |
+ return; |
100 |
+ |
101 |
+ t = strndup(buf, len); |
102 |
+ if (t == NULL) |
103 |
+ { |
104 |
+ free(t); |
105 |
+ return; |
106 |
+ } |
107 |
+ |
108 |
+ while ((tokens = strsep(&t, " ")) != NULL) |
109 |
+ { |
110 |
+ char *freq; |
111 |
+ ulong res; |
112 |
+ |
113 |
+ freq = GetFirstFragment(tokens, "/"); |
114 |
+ if (freq != NULL) |
115 |
+ { |
116 |
+ res = strtoul(freq, &freq, 10); |
117 |
+ if (res > max) |
118 |
+ { |
119 |
+ max = res; |
120 |
+ } |
121 |
+ else |
122 |
+ { |
123 |
+ if ((min == 0) || (res < min)) |
124 |
+ min = res; |
125 |
+ } |
126 |
+ } |
127 |
+ } |
128 |
+ |
129 |
+ free(t); |
130 |
+ mBounds[source] = qMakePair(min, max); |
131 |
+#else |
132 |
bool ok; |
133 |
|
134 |
uint min = readAllFile(qPrintable(QString("/sys/devices/system/cpu/%1/cpufreq/scaling_min_freq").arg(source))).toUInt(&ok); |
135 |
@@ -56,11 +155,26 @@ void CpuStatPrivate::addSource(const QSt |
136 |
if (ok) |
137 |
mBounds[source] = qMakePair(min, max); |
138 |
} |
139 |
+#endif |
140 |
} |
141 |
|
142 |
void CpuStatPrivate::updateSources() |
143 |
{ |
144 |
mSources.clear(); |
145 |
+#ifdef HAVE_SYSCTL_H |
146 |
+ int cpu; |
147 |
+ |
148 |
+ cpu = GetCpu(); |
149 |
+ for (int i =0;i<cpu;i++) |
150 |
+ { |
151 |
+ mSources.append(QString("cpu%1").arg(i)); |
152 |
+ |
153 |
+ |
154 |
+ |
155 |
+ addSource(QString("cpu%1").arg(i)); |
156 |
+ } |
157 |
+ mBounds.clear(); |
158 |
+#else |
159 |
|
160 |
foreach (const QString &row, readAllFile("/proc/stat").split(QChar('\n'), QString::SkipEmptyParts)) |
161 |
{ |
162 |
@@ -97,6 +211,7 @@ void CpuStatPrivate::updateSources() |
163 |
addSource(QString("cpu%1").arg(number)); |
164 |
} |
165 |
} |
166 |
+#endif |
167 |
} |
168 |
|
169 |
CpuStatPrivate::~CpuStatPrivate() |
170 |
@@ -117,14 +232,98 @@ void CpuStatPrivate::recalculateMinMax() |
171 |
{ |
172 |
int cores = 1; |
173 |
if (mSource == "cpu") |
174 |
+#ifdef HAVE_SYSCTL_H |
175 |
+ cores = mSources.size(); |
176 |
+#else |
177 |
cores = mSources.size() - 1; |
178 |
- |
179 |
+#endif |
180 |
mIntervalMin = static_cast<float>(mTimer->interval()) / 1000 * static_cast<float>(mUserHz) * static_cast<float>(cores) / 1.25; // -25% |
181 |
mIntervalMax = static_cast<float>(mTimer->interval()) / 1000 * static_cast<float>(mUserHz) * static_cast<float>(cores) * 1.25; // +25% |
182 |
} |
183 |
|
184 |
void CpuStatPrivate::timeout() |
185 |
{ |
186 |
+#ifdef HAVE_SYSCTL_H |
187 |
+ if ( (mMonitoring == CpuStat::LoadOnly) |
188 |
+ || (mMonitoring == CpuStat::LoadAndFrequency) ) |
189 |
+ { |
190 |
+ size_t cp_size = sizeof(long) * CPUSTATES * GetCpu(); |
191 |
+ long *cp_times = (long *)malloc(cp_size); |
192 |
+ Values current; |
193 |
+ int cpuNumber = mSource.midRef(3).toInt(); |
194 |
+ if (sysctl(mib,2, cp_times, &cp_size, NULL, 0) < 0) |
195 |
+ free(cp_times); |
196 |
+ |
197 |
+ current.user = static_cast<ulong>(cp_times[CP_USER+cpuNumber*CPUSTATES]); |
198 |
+ current.nice = static_cast<ulong>(cp_times[CP_NICE+cpuNumber*CPUSTATES]); |
199 |
+ current.system = static_cast<ulong>(cp_times[CP_SYS+cpuNumber*CPUSTATES]); |
200 |
+ current.idle = static_cast<ulong>(cp_times[CP_IDLE+cpuNumber*CPUSTATES]); |
201 |
+ current.other = static_cast<ulong>(cp_times[CP_INTR+cpuNumber*CPUSTATES]); |
202 |
+ current.total = current.user + current.nice + current.system+current.idle+current.other; |
203 |
+ |
204 |
+ float sumDelta = static_cast<float>(current.total - mPrevious.total); |
205 |
+ |
206 |
+ if ((mPrevious.total != 0) && ((sumDelta < mIntervalMin) || (sumDelta > mIntervalMax))) |
207 |
+ { |
208 |
+ if (mMonitoring == CpuStat::LoadAndFrequency) |
209 |
+ emit update(0.0, 0.0, 0.0, 0.0, 0.0, 0); |
210 |
+ else |
211 |
+ emit update(0.0, 0.0, 0.0, 0.0, 0.0); |
212 |
+ |
213 |
+ mPrevious.clear(); |
214 |
+ } |
215 |
+ else |
216 |
+ { |
217 |
+ if (mMonitoring == CpuStat::LoadAndFrequency) |
218 |
+ { |
219 |
+ ulong freq = 0; |
220 |
+ |
221 |
+ freq = CurrentFreq(); |
222 |
+ if (freq > 0) |
223 |
+ { |
224 |
+ emit update( |
225 |
+ static_cast<float>(current.user - mPrevious.user ) / sumDelta, |
226 |
+ static_cast<float>(current.nice - mPrevious.nice ) / sumDelta, |
227 |
+ static_cast<float>(current.system - mPrevious.system ) / sumDelta, |
228 |
+ static_cast<float>(current.idle - mPrevious.idle ) / sumDelta, |
229 |
+ static_cast<float>(current.other - mPrevious.other ) / sumDelta, |
230 |
+ freq); |
231 |
+ } |
232 |
+ else |
233 |
+ { |
234 |
+ emit update( |
235 |
+ static_cast<float>(current.user - mPrevious.user ) / sumDelta, |
236 |
+ static_cast<float>(current.nice - mPrevious.nice ) / sumDelta, |
237 |
+ static_cast<float>(current.system - mPrevious.system ) / sumDelta, |
238 |
+ static_cast<float>(current.idle - mPrevious.idle ) / sumDelta, |
239 |
+ static_cast<float>(current.other - mPrevious.other ) / sumDelta); |
240 |
+ } |
241 |
+ } |
242 |
+ else |
243 |
+ { |
244 |
+ |
245 |
+ emit update( |
246 |
+ static_cast<float>(current.user - mPrevious.user ) / sumDelta, |
247 |
+ static_cast<float>(current.nice - mPrevious.nice ) / sumDelta, |
248 |
+ static_cast<float>(current.system - mPrevious.system ) / sumDelta, |
249 |
+ static_cast<float>(current.idle - mPrevious.idle ) / sumDelta, |
250 |
+ static_cast<float>(current.other - mPrevious.other ) / sumDelta); |
251 |
+ } |
252 |
+ |
253 |
+ mPrevious = current; |
254 |
+ } |
255 |
+ |
256 |
+ free(cp_times); |
257 |
+ } |
258 |
+ else |
259 |
+ { |
260 |
+ ulong freq = 0; |
261 |
+ |
262 |
+ freq = CurrentFreq(); |
263 |
+ if (freq > 0) |
264 |
+ emit update(freq); |
265 |
+ } |
266 |
+#else |
267 |
if ( (mMonitoring == CpuStat::LoadOnly) |
268 |
|| (mMonitoring == CpuStat::LoadAndFrequency) ) |
269 |
{ |
270 |
@@ -258,6 +457,7 @@ void CpuStatPrivate::timeout() |
271 |
} |
272 |
emit update(freq); |
273 |
} |
274 |
+#endif |
275 |
} |
276 |
|
277 |
QString CpuStatPrivate::defaultSource() |
278 |
@@ -301,10 +501,15 @@ CpuStat::CpuStat(QObject *parent) |
279 |
{ |
280 |
impl = new CpuStatPrivate(this); |
281 |
baseimpl = impl; |
282 |
- |
283 |
+#ifdef HAVE_SYSCTL_H |
284 |
+ connect(impl, SIGNAL(update(float,float,float,float,float,ulong)), this, SIGNAL(update(float,float,float,float,float,ulong))); |
285 |
+ connect(impl, SIGNAL(update(float,float,float,float,float)), this, SIGNAL(update(float,float,float,float,float))); |
286 |
+ connect(impl, SIGNAL(update(ulong)), this, SIGNAL(update(ulong))); |
287 |
+#else |
288 |
connect(impl, SIGNAL(update(float,float,float,float,float,uint)), this, SIGNAL(update(float,float,float,float,float,uint))); |
289 |
connect(impl, SIGNAL(update(float,float,float,float)), this, SIGNAL(update(float,float,float,float))); |
290 |
connect(impl, SIGNAL(update(uint)), this, SIGNAL(update(uint))); |
291 |
+#endif |
292 |
} |
293 |
|
294 |
CpuStat::~CpuStat() |