diff options
Diffstat (limited to 'src/testbed/gnunet-service-testbed_cpustatus.c')
-rw-r--r-- | src/testbed/gnunet-service-testbed_cpustatus.c | 631 |
1 files changed, 316 insertions, 315 deletions
diff --git a/src/testbed/gnunet-service-testbed_cpustatus.c b/src/testbed/gnunet-service-testbed_cpustatus.c index f3df9a96e..ae6450176 100644 --- a/src/testbed/gnunet-service-testbed_cpustatus.c +++ b/src/testbed/gnunet-service-testbed_cpustatus.c | |||
@@ -82,12 +82,12 @@ static double agedIOLoad = -1; | |||
82 | */ | 82 | */ |
83 | struct GNUNET_BIO_WriteHandle *bw; | 83 | struct GNUNET_BIO_WriteHandle *bw; |
84 | 84 | ||
85 | struct GNUNET_SCHEDULER_Task * sample_load_task_id; | 85 | struct GNUNET_SCHEDULER_Task *sample_load_task_id; |
86 | 86 | ||
87 | 87 | ||
88 | #ifdef OSX | 88 | #ifdef OSX |
89 | static int | 89 | static int |
90 | initMachCpuStats() | 90 | initMachCpuStats () |
91 | { | 91 | { |
92 | unsigned int cpu_count; | 92 | unsigned int cpu_count; |
93 | processor_cpu_load_info_t cpu_load; | 93 | processor_cpu_load_info_t cpu_load; |
@@ -95,27 +95,27 @@ initMachCpuStats() | |||
95 | kern_return_t kret; | 95 | kern_return_t kret; |
96 | int i, j; | 96 | int i, j; |
97 | 97 | ||
98 | kret = host_processor_info(mach_host_self(), | 98 | kret = host_processor_info (mach_host_self (), |
99 | PROCESSOR_CPU_LOAD_INFO, | 99 | PROCESSOR_CPU_LOAD_INFO, |
100 | &cpu_count, | 100 | &cpu_count, |
101 | (processor_info_array_t *)&cpu_load, | 101 | (processor_info_array_t *) &cpu_load, |
102 | &cpu_msg_count); | 102 | &cpu_msg_count); |
103 | if (kret != KERN_SUCCESS) | 103 | if (kret != KERN_SUCCESS) |
104 | { | 104 | { |
105 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "host_processor_info failed."); | 105 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "host_processor_info failed."); |
106 | return GNUNET_SYSERR; | 106 | return GNUNET_SYSERR; |
107 | } | 107 | } |
108 | prev_cpu_load = GNUNET_malloc(cpu_count * sizeof(*prev_cpu_load)); | 108 | prev_cpu_load = GNUNET_malloc (cpu_count * sizeof(*prev_cpu_load)); |
109 | for (i = 0; i < cpu_count; i++) | 109 | for (i = 0; i < cpu_count; i++) |
110 | { | ||
111 | for (j = 0; j < CPU_STATE_MAX; j++) | ||
110 | { | 112 | { |
111 | for (j = 0; j < CPU_STATE_MAX; j++) | 113 | prev_cpu_load[i].cpu_ticks[j] = cpu_load[i].cpu_ticks[j]; |
112 | { | ||
113 | prev_cpu_load[i].cpu_ticks[j] = cpu_load[i].cpu_ticks[j]; | ||
114 | } | ||
115 | } | 114 | } |
116 | vm_deallocate(mach_task_self(), | 115 | } |
117 | (vm_address_t)cpu_load, | 116 | vm_deallocate (mach_task_self (), |
118 | (vm_size_t)(cpu_msg_count * sizeof(*cpu_load))); | 117 | (vm_address_t) cpu_load, |
118 | (vm_size_t) (cpu_msg_count * sizeof(*cpu_load))); | ||
119 | return GNUNET_OK; | 119 | return GNUNET_OK; |
120 | } | 120 | } |
121 | #endif | 121 | #endif |
@@ -127,7 +127,7 @@ initMachCpuStats() | |||
127 | * If there is an error the method returns -1. | 127 | * If there is an error the method returns -1. |
128 | */ | 128 | */ |
129 | static int | 129 | static int |
130 | updateUsage() | 130 | updateUsage () |
131 | { | 131 | { |
132 | currentIOLoad = -1; | 132 | currentIOLoad = -1; |
133 | currentCPULoad = -1; | 133 | currentCPULoad = -1; |
@@ -136,69 +136,69 @@ updateUsage() | |||
136 | if that does not work, disable /proc/stat for the future | 136 | if that does not work, disable /proc/stat for the future |
137 | by closing the file and use the next-best method. */ | 137 | by closing the file and use the next-best method. */ |
138 | if (proc_stat != NULL) | 138 | if (proc_stat != NULL) |
139 | { | ||
140 | static unsigned long long last_cpu_results[5] = { 0, 0, 0, 0, 0 }; | ||
141 | static int have_last_cpu = GNUNET_NO; | ||
142 | int ret; | ||
143 | char line[256]; | ||
144 | unsigned long long user_read, system_read, nice_read, idle_read, | ||
145 | iowait_read; | ||
146 | unsigned long long user, system, nice, idle, iowait; | ||
147 | unsigned long long usage_time = 0, total_time = 1; | ||
148 | |||
149 | /* Get the first line with the data */ | ||
150 | rewind (proc_stat); | ||
151 | fflush (proc_stat); | ||
152 | if (NULL == fgets (line, 256, proc_stat)) | ||
139 | { | 153 | { |
140 | static unsigned long long last_cpu_results[5] = { 0, 0, 0, 0, 0 }; | 154 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, |
141 | static int have_last_cpu = GNUNET_NO; | 155 | "fgets", "/proc/stat"); |
142 | int ret; | 156 | proc_stat = NULL; /* don't try again */ |
143 | char line[256]; | 157 | } |
144 | unsigned long long user_read, system_read, nice_read, idle_read, | 158 | else |
145 | iowait_read; | 159 | { |
146 | unsigned long long user, system, nice, idle, iowait; | 160 | iowait_read = 0; |
147 | unsigned long long usage_time = 0, total_time = 1; | 161 | ret = sscanf (line, "%*s %llu %llu %llu %llu %llu", |
148 | 162 | &user_read, | |
149 | /* Get the first line with the data */ | 163 | &system_read, &nice_read, &idle_read, &iowait_read); |
150 | rewind(proc_stat); | 164 | if (ret < 4) |
151 | fflush(proc_stat); | 165 | { |
152 | if (NULL == fgets(line, 256, proc_stat)) | 166 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, |
153 | { | 167 | "fgets-sscanf", "/proc/stat"); |
154 | GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, | 168 | fclose (proc_stat); |
155 | "fgets", "/proc/stat"); | 169 | proc_stat = NULL; /* don't try again */ |
156 | proc_stat = NULL; /* don't try again */ | 170 | have_last_cpu = GNUNET_NO; |
157 | } | 171 | } |
158 | else | 172 | else |
173 | { | ||
174 | /* Store the current usage */ | ||
175 | user = user_read - last_cpu_results[0]; | ||
176 | system = system_read - last_cpu_results[1]; | ||
177 | nice = nice_read - last_cpu_results[2]; | ||
178 | idle = idle_read - last_cpu_results[3]; | ||
179 | iowait = iowait_read - last_cpu_results[4]; | ||
180 | /* Calculate the % usage */ | ||
181 | usage_time = user + system + nice; | ||
182 | total_time = usage_time + idle + iowait; | ||
183 | if ((total_time > 0) && (have_last_cpu == GNUNET_YES)) | ||
159 | { | 184 | { |
160 | iowait_read = 0; | 185 | currentCPULoad = (int) (100L * usage_time / total_time); |
161 | ret = sscanf(line, "%*s %llu %llu %llu %llu %llu", | 186 | if (ret > 4) |
162 | &user_read, | 187 | currentIOLoad = (int) (100L * iowait / total_time); |
163 | &system_read, &nice_read, &idle_read, &iowait_read); | ||
164 | if (ret < 4) | ||
165 | { | ||
166 | GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, | ||
167 | "fgets-sscanf", "/proc/stat"); | ||
168 | fclose(proc_stat); | ||
169 | proc_stat = NULL; /* don't try again */ | ||
170 | have_last_cpu = GNUNET_NO; | ||
171 | } | ||
172 | else | 188 | else |
173 | { | 189 | currentIOLoad = -1; /* 2.4 kernel */ |
174 | /* Store the current usage */ | ||
175 | user = user_read - last_cpu_results[0]; | ||
176 | system = system_read - last_cpu_results[1]; | ||
177 | nice = nice_read - last_cpu_results[2]; | ||
178 | idle = idle_read - last_cpu_results[3]; | ||
179 | iowait = iowait_read - last_cpu_results[4]; | ||
180 | /* Calculate the % usage */ | ||
181 | usage_time = user + system + nice; | ||
182 | total_time = usage_time + idle + iowait; | ||
183 | if ((total_time > 0) && (have_last_cpu == GNUNET_YES)) | ||
184 | { | ||
185 | currentCPULoad = (int)(100L * usage_time / total_time); | ||
186 | if (ret > 4) | ||
187 | currentIOLoad = (int)(100L * iowait / total_time); | ||
188 | else | ||
189 | currentIOLoad = -1; /* 2.4 kernel */ | ||
190 | } | ||
191 | /* Store the values for the next calculation */ | ||
192 | last_cpu_results[0] = user_read; | ||
193 | last_cpu_results[1] = system_read; | ||
194 | last_cpu_results[2] = nice_read; | ||
195 | last_cpu_results[3] = idle_read; | ||
196 | last_cpu_results[4] = iowait_read; | ||
197 | have_last_cpu = GNUNET_YES; | ||
198 | return GNUNET_OK; | ||
199 | } | ||
200 | } | 190 | } |
191 | /* Store the values for the next calculation */ | ||
192 | last_cpu_results[0] = user_read; | ||
193 | last_cpu_results[1] = system_read; | ||
194 | last_cpu_results[2] = nice_read; | ||
195 | last_cpu_results[3] = idle_read; | ||
196 | last_cpu_results[4] = iowait_read; | ||
197 | have_last_cpu = GNUNET_YES; | ||
198 | return GNUNET_OK; | ||
199 | } | ||
201 | } | 200 | } |
201 | } | ||
202 | #endif | 202 | #endif |
203 | 203 | ||
204 | #ifdef OSX | 204 | #ifdef OSX |
@@ -212,91 +212,91 @@ updateUsage() | |||
212 | int i, j; | 212 | int i, j; |
213 | 213 | ||
214 | t_idle_all = t_total_all = 0; | 214 | t_idle_all = t_total_all = 0; |
215 | kret = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, | 215 | kret = host_processor_info (mach_host_self (), PROCESSOR_CPU_LOAD_INFO, |
216 | &cpu_count, | 216 | &cpu_count, |
217 | (processor_info_array_t *)&cpu_load, | 217 | (processor_info_array_t *) &cpu_load, |
218 | &cpu_msg_count); | 218 | &cpu_msg_count); |
219 | if (kret == KERN_SUCCESS) | 219 | if (kret == KERN_SUCCESS) |
220 | { | ||
221 | for (i = 0; i < cpu_count; i++) | ||
220 | { | 222 | { |
221 | for (i = 0; i < cpu_count; i++) | 223 | if (cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM] >= |
222 | { | 224 | prev_cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM]) |
223 | if (cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM] >= | 225 | { |
224 | prev_cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM]) | 226 | t_sys = cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM] |
225 | { | 227 | - prev_cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM]; |
226 | t_sys = cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM] - | 228 | } |
227 | prev_cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM]; | ||
228 | } | ||
229 | else | ||
230 | { | ||
231 | t_sys = cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM] + | ||
232 | (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM] + | ||
233 | 1); | ||
234 | } | ||
235 | |||
236 | if (cpu_load[i].cpu_ticks[CPU_STATE_USER] >= | ||
237 | prev_cpu_load[i].cpu_ticks[CPU_STATE_USER]) | ||
238 | { | ||
239 | t_user = cpu_load[i].cpu_ticks[CPU_STATE_USER] - | ||
240 | prev_cpu_load[i].cpu_ticks[CPU_STATE_USER]; | ||
241 | } | ||
242 | else | ||
243 | { | ||
244 | t_user = cpu_load[i].cpu_ticks[CPU_STATE_USER] + | ||
245 | (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_USER] + | ||
246 | 1); | ||
247 | } | ||
248 | |||
249 | if (cpu_load[i].cpu_ticks[CPU_STATE_NICE] >= | ||
250 | prev_cpu_load[i].cpu_ticks[CPU_STATE_NICE]) | ||
251 | { | ||
252 | t_nice = cpu_load[i].cpu_ticks[CPU_STATE_NICE] - | ||
253 | prev_cpu_load[i].cpu_ticks[CPU_STATE_NICE]; | ||
254 | } | ||
255 | else | ||
256 | { | ||
257 | t_nice = cpu_load[i].cpu_ticks[CPU_STATE_NICE] + | ||
258 | (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_NICE] + | ||
259 | 1); | ||
260 | } | ||
261 | |||
262 | if (cpu_load[i].cpu_ticks[CPU_STATE_IDLE] >= | ||
263 | prev_cpu_load[i].cpu_ticks[CPU_STATE_IDLE]) | ||
264 | { | ||
265 | t_idle = cpu_load[i].cpu_ticks[CPU_STATE_IDLE] - | ||
266 | prev_cpu_load[i].cpu_ticks[CPU_STATE_IDLE]; | ||
267 | } | ||
268 | else | ||
269 | { | ||
270 | t_idle = cpu_load[i].cpu_ticks[CPU_STATE_IDLE] + | ||
271 | (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_IDLE] + | ||
272 | 1); | ||
273 | } | ||
274 | t_total = t_sys + t_user + t_nice + t_idle; | ||
275 | t_idle_all += t_idle; | ||
276 | t_total_all += t_total; | ||
277 | } | ||
278 | for (i = 0; i < cpu_count; i++) | ||
279 | { | ||
280 | for (j = 0; j < CPU_STATE_MAX; j++) | ||
281 | { | ||
282 | prev_cpu_load[i].cpu_ticks[j] = cpu_load[i].cpu_ticks[j]; | ||
283 | } | ||
284 | } | ||
285 | if (t_total_all > 0) | ||
286 | currentCPULoad = 100 - (100 * t_idle_all) / t_total_all; | ||
287 | else | 229 | else |
288 | currentCPULoad = -1; | 230 | { |
289 | vm_deallocate(mach_task_self(), | 231 | t_sys = cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM] |
290 | (vm_address_t)cpu_load, | 232 | + (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM] |
291 | (vm_size_t)(cpu_msg_count * sizeof(*cpu_load))); | 233 | + 1); |
292 | currentIOLoad = -1; /* FIXME-OSX! */ | 234 | } |
293 | return GNUNET_OK; | 235 | |
236 | if (cpu_load[i].cpu_ticks[CPU_STATE_USER] >= | ||
237 | prev_cpu_load[i].cpu_ticks[CPU_STATE_USER]) | ||
238 | { | ||
239 | t_user = cpu_load[i].cpu_ticks[CPU_STATE_USER] | ||
240 | - prev_cpu_load[i].cpu_ticks[CPU_STATE_USER]; | ||
241 | } | ||
242 | else | ||
243 | { | ||
244 | t_user = cpu_load[i].cpu_ticks[CPU_STATE_USER] | ||
245 | + (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_USER] | ||
246 | + 1); | ||
247 | } | ||
248 | |||
249 | if (cpu_load[i].cpu_ticks[CPU_STATE_NICE] >= | ||
250 | prev_cpu_load[i].cpu_ticks[CPU_STATE_NICE]) | ||
251 | { | ||
252 | t_nice = cpu_load[i].cpu_ticks[CPU_STATE_NICE] | ||
253 | - prev_cpu_load[i].cpu_ticks[CPU_STATE_NICE]; | ||
254 | } | ||
255 | else | ||
256 | { | ||
257 | t_nice = cpu_load[i].cpu_ticks[CPU_STATE_NICE] | ||
258 | + (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_NICE] | ||
259 | + 1); | ||
260 | } | ||
261 | |||
262 | if (cpu_load[i].cpu_ticks[CPU_STATE_IDLE] >= | ||
263 | prev_cpu_load[i].cpu_ticks[CPU_STATE_IDLE]) | ||
264 | { | ||
265 | t_idle = cpu_load[i].cpu_ticks[CPU_STATE_IDLE] | ||
266 | - prev_cpu_load[i].cpu_ticks[CPU_STATE_IDLE]; | ||
267 | } | ||
268 | else | ||
269 | { | ||
270 | t_idle = cpu_load[i].cpu_ticks[CPU_STATE_IDLE] | ||
271 | + (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_IDLE] | ||
272 | + 1); | ||
273 | } | ||
274 | t_total = t_sys + t_user + t_nice + t_idle; | ||
275 | t_idle_all += t_idle; | ||
276 | t_total_all += t_total; | ||
294 | } | 277 | } |
295 | else | 278 | for (i = 0; i < cpu_count; i++) |
296 | { | 279 | { |
297 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "host_processor_info failed."); | 280 | for (j = 0; j < CPU_STATE_MAX; j++) |
298 | return GNUNET_SYSERR; | 281 | { |
282 | prev_cpu_load[i].cpu_ticks[j] = cpu_load[i].cpu_ticks[j]; | ||
283 | } | ||
299 | } | 284 | } |
285 | if (t_total_all > 0) | ||
286 | currentCPULoad = 100 - (100 * t_idle_all) / t_total_all; | ||
287 | else | ||
288 | currentCPULoad = -1; | ||
289 | vm_deallocate (mach_task_self (), | ||
290 | (vm_address_t) cpu_load, | ||
291 | (vm_size_t) (cpu_msg_count * sizeof(*cpu_load))); | ||
292 | currentIOLoad = -1; /* FIXME-OSX! */ | ||
293 | return GNUNET_OK; | ||
294 | } | ||
295 | else | ||
296 | { | ||
297 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "host_processor_info failed."); | ||
298 | return GNUNET_SYSERR; | ||
299 | } | ||
300 | } | 300 | } |
301 | #endif | 301 | #endif |
302 | /* try kstat (Solaris only) */ | 302 | /* try kstat (Solaris only) */ |
@@ -315,48 +315,48 @@ updateUsage() | |||
315 | 315 | ||
316 | if (kstat_once == 1) | 316 | if (kstat_once == 1) |
317 | goto ABORT_KSTAT; | 317 | goto ABORT_KSTAT; |
318 | kc = kstat_open(); | 318 | kc = kstat_open (); |
319 | if (kc == NULL) | 319 | if (kc == NULL) |
320 | { | 320 | { |
321 | GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kstat_close"); | 321 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kstat_close"); |
322 | goto ABORT_KSTAT; | 322 | goto ABORT_KSTAT; |
323 | } | 323 | } |
324 | 324 | ||
325 | idlecount = 0; | 325 | idlecount = 0; |
326 | totalcount = 0; | 326 | totalcount = 0; |
327 | for (khelper = kc->kc_chain; khelper != NULL; khelper = khelper->ks_next) | 327 | for (khelper = kc->kc_chain; khelper != NULL; khelper = khelper->ks_next) |
328 | { | ||
329 | cpu_stat_t stats; | ||
330 | |||
331 | if (0 != strncmp (khelper->ks_name, "cpu_stat", strlen ("cpu_stat"))) | ||
332 | continue; | ||
333 | if (khelper->ks_data_size > sizeof(cpu_stat_t)) | ||
334 | continue; /* better save then sorry! */ | ||
335 | if (-1 != kstat_read (kc, khelper, &stats)) | ||
328 | { | 336 | { |
329 | cpu_stat_t stats; | 337 | idlecount += stats.cpu_sysinfo.cpu[CPU_IDLE]; |
330 | 338 | totalcount | |
331 | if (0 != strncmp(khelper->ks_name, "cpu_stat", strlen("cpu_stat"))) | 339 | += stats.cpu_sysinfo.cpu[CPU_IDLE] |
332 | continue; | 340 | + stats.cpu_sysinfo.cpu[CPU_USER] |
333 | if (khelper->ks_data_size > sizeof(cpu_stat_t)) | 341 | + stats.cpu_sysinfo.cpu[CPU_KERNEL] |
334 | continue; /* better save then sorry! */ | 342 | + stats.cpu_sysinfo.cpu[CPU_WAIT]; |
335 | if (-1 != kstat_read(kc, khelper, &stats)) | ||
336 | { | ||
337 | idlecount += stats.cpu_sysinfo.cpu[CPU_IDLE]; | ||
338 | totalcount | ||
339 | += stats.cpu_sysinfo.cpu[CPU_IDLE] + | ||
340 | stats.cpu_sysinfo.cpu[CPU_USER] + | ||
341 | stats.cpu_sysinfo.cpu[CPU_KERNEL] + | ||
342 | stats.cpu_sysinfo.cpu[CPU_WAIT]; | ||
343 | } | ||
344 | } | 343 | } |
345 | if (0 != kstat_close(kc)) | 344 | } |
346 | GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kstat_close"); | 345 | if (0 != kstat_close (kc)) |
346 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kstat_close"); | ||
347 | if ((idlecount == 0) && (totalcount == 0)) | 347 | if ((idlecount == 0) && (totalcount == 0)) |
348 | goto ABORT_KSTAT; /* no stats found => abort */ | 348 | goto ABORT_KSTAT; /* no stats found => abort */ |
349 | deltaidle = idlecount - last_idlecount; | 349 | deltaidle = idlecount - last_idlecount; |
350 | deltatotal = totalcount - last_totalcount; | 350 | deltatotal = totalcount - last_totalcount; |
351 | if ((deltatotal > 0) && (last_totalcount > 0)) | 351 | if ((deltatotal > 0) && (last_totalcount > 0)) |
352 | { | 352 | { |
353 | currentCPULoad = (unsigned int)(100.0 * deltaidle / deltatotal); | 353 | currentCPULoad = (unsigned int) (100.0 * deltaidle / deltatotal); |
354 | if (currentCPULoad > 100) | 354 | if (currentCPULoad > 100) |
355 | currentCPULoad = 100; /* odd */ | 355 | currentCPULoad = 100; /* odd */ |
356 | if (currentCPULoad < 0) | 356 | if (currentCPULoad < 0) |
357 | currentCPULoad = 0; /* odd */ | 357 | currentCPULoad = 0; /* odd */ |
358 | currentCPULoad = 100 - currentCPULoad; /* computed idle-load before! */ | 358 | currentCPULoad = 100 - currentCPULoad; /* computed idle-load before! */ |
359 | } | 359 | } |
360 | else | 360 | else |
361 | currentCPULoad = -1; | 361 | currentCPULoad = -1; |
362 | currentIOLoad = -1; /* FIXME-SOLARIS! */ | 362 | currentIOLoad = -1; /* FIXME-SOLARIS! */ |
@@ -377,24 +377,24 @@ ABORT_KSTAT: | |||
377 | { | 377 | { |
378 | static int warnOnce = 0; | 378 | static int warnOnce = 0; |
379 | double loadavg; | 379 | double loadavg; |
380 | if (1 != getloadavg(&loadavg, 1)) | 380 | if (1 != getloadavg (&loadavg, 1)) |
381 | { | ||
382 | /* only warn once, if there is a problem with | ||
383 | getloadavg, we're going to hit it frequently... */ | ||
384 | if (warnOnce == 0) | ||
381 | { | 385 | { |
382 | /* only warn once, if there is a problem with | 386 | warnOnce = 1; |
383 | getloadavg, we're going to hit it frequently... */ | 387 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "getloadavg"); |
384 | if (warnOnce == 0) | ||
385 | { | ||
386 | warnOnce = 1; | ||
387 | GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "getloadavg"); | ||
388 | } | ||
389 | return GNUNET_SYSERR; | ||
390 | } | 388 | } |
389 | return GNUNET_SYSERR; | ||
390 | } | ||
391 | else | 391 | else |
392 | { | 392 | { |
393 | /* success with getloadavg */ | 393 | /* success with getloadavg */ |
394 | currentCPULoad = (int)(100 * loadavg); | 394 | currentCPULoad = (int) (100 * loadavg); |
395 | currentIOLoad = -1; /* FIXME */ | 395 | currentIOLoad = -1; /* FIXME */ |
396 | return GNUNET_OK; | 396 | return GNUNET_OK; |
397 | } | 397 | } |
398 | } | 398 | } |
399 | #endif | 399 | #endif |
400 | 400 | ||
@@ -412,56 +412,56 @@ ABORT_KSTAT: | |||
412 | * that lock has already been obtained. | 412 | * that lock has already been obtained. |
413 | */ | 413 | */ |
414 | static void | 414 | static void |
415 | updateAgedLoad() | 415 | updateAgedLoad () |
416 | { | 416 | { |
417 | static struct GNUNET_TIME_Absolute lastCall; | 417 | static struct GNUNET_TIME_Absolute lastCall; |
418 | struct GNUNET_TIME_Relative age; | 418 | struct GNUNET_TIME_Relative age; |
419 | 419 | ||
420 | age = GNUNET_TIME_absolute_get_duration(lastCall); | 420 | age = GNUNET_TIME_absolute_get_duration (lastCall); |
421 | if ((agedCPULoad == -1) | 421 | if ((agedCPULoad == -1) |
422 | || (age.rel_value_us > 500000)) | 422 | || (age.rel_value_us > 500000)) |
423 | { | ||
424 | /* use smoothing, but do NOT update lastRet at frequencies higher | ||
425 | than 500ms; this makes the smoothing (mostly) independent from | ||
426 | the frequency at which getCPULoad is called (and we don't spend | ||
427 | more time measuring CPU than actually computing something). */ | ||
428 | lastCall = GNUNET_TIME_absolute_get (); | ||
429 | updateUsage (); | ||
430 | if (currentCPULoad == -1) | ||
423 | { | 431 | { |
424 | /* use smoothing, but do NOT update lastRet at frequencies higher | 432 | agedCPULoad = -1; |
425 | than 500ms; this makes the smoothing (mostly) independent from | 433 | } |
426 | the frequency at which getCPULoad is called (and we don't spend | 434 | else |
427 | more time measuring CPU than actually computing something). */ | 435 | { |
428 | lastCall = GNUNET_TIME_absolute_get(); | 436 | if (agedCPULoad == -1) |
429 | updateUsage(); | 437 | { |
430 | if (currentCPULoad == -1) | 438 | agedCPULoad = currentCPULoad; |
431 | { | 439 | } |
432 | agedCPULoad = -1; | ||
433 | } | ||
434 | else | 440 | else |
435 | { | 441 | { |
436 | if (agedCPULoad == -1) | 442 | /* for CPU, we don't do the 'fast increase' since CPU is much |
437 | { | 443 | more jitterish to begin with */ |
438 | agedCPULoad = currentCPULoad; | 444 | agedCPULoad = (agedCPULoad * 31 + currentCPULoad) / 32; |
439 | } | 445 | } |
440 | else | 446 | } |
441 | { | 447 | if (currentIOLoad == -1) |
442 | /* for CPU, we don't do the 'fast increase' since CPU is much | 448 | { |
443 | more jitterish to begin with */ | 449 | agedIOLoad = -1; |
444 | agedCPULoad = (agedCPULoad * 31 + currentCPULoad) / 32; | 450 | } |
445 | } | 451 | else |
446 | } | 452 | { |
447 | if (currentIOLoad == -1) | 453 | if (agedIOLoad == -1) |
448 | { | 454 | { |
449 | agedIOLoad = -1; | 455 | agedIOLoad = currentIOLoad; |
450 | } | 456 | } |
451 | else | 457 | else |
452 | { | 458 | { |
453 | if (agedIOLoad == -1) | 459 | /* for IO, we don't do the 'fast increase' since IO is much |
454 | { | 460 | more jitterish to begin with */ |
455 | agedIOLoad = currentIOLoad; | 461 | agedIOLoad = (agedIOLoad * 31 + currentIOLoad) / 32; |
456 | } | 462 | } |
457 | else | ||
458 | { | ||
459 | /* for IO, we don't do the 'fast increase' since IO is much | ||
460 | more jitterish to begin with */ | ||
461 | agedIOLoad = (agedIOLoad * 31 + currentIOLoad) / 32; | ||
462 | } | ||
463 | } | ||
464 | } | 463 | } |
464 | } | ||
465 | } | 465 | } |
466 | 466 | ||
467 | /** | 467 | /** |
@@ -470,10 +470,10 @@ updateAgedLoad() | |||
470 | * (100 is equivalent to full load) | 470 | * (100 is equivalent to full load) |
471 | */ | 471 | */ |
472 | static int | 472 | static int |
473 | cpu_get_load() | 473 | cpu_get_load () |
474 | { | 474 | { |
475 | updateAgedLoad(); | 475 | updateAgedLoad (); |
476 | return (int)agedCPULoad; | 476 | return (int) agedCPULoad; |
477 | } | 477 | } |
478 | 478 | ||
479 | 479 | ||
@@ -483,10 +483,10 @@ cpu_get_load() | |||
483 | * (100 is equivalent to full load) | 483 | * (100 is equivalent to full load) |
484 | */ | 484 | */ |
485 | static int | 485 | static int |
486 | disk_get_load() | 486 | disk_get_load () |
487 | { | 487 | { |
488 | updateAgedLoad(); | 488 | updateAgedLoad (); |
489 | return (int)agedIOLoad; | 489 | return (int) agedIOLoad; |
490 | } | 490 | } |
491 | 491 | ||
492 | /** | 492 | /** |
@@ -495,13 +495,13 @@ disk_get_load() | |||
495 | * @return the percentage of memory used | 495 | * @return the percentage of memory used |
496 | */ | 496 | */ |
497 | static unsigned int | 497 | static unsigned int |
498 | mem_get_usage() | 498 | mem_get_usage () |
499 | { | 499 | { |
500 | double percentage; | 500 | double percentage; |
501 | 501 | ||
502 | meminfo(); | 502 | meminfo (); |
503 | percentage = (((double)kb_main_used) / ((double)kb_main_total) * 100.0); | 503 | percentage = (((double) kb_main_used) / ((double) kb_main_total) * 100.0); |
504 | return (unsigned int)percentage; | 504 | return (unsigned int) percentage; |
505 | } | 505 | } |
506 | 506 | ||
507 | 507 | ||
@@ -513,29 +513,29 @@ mem_get_usage() | |||
513 | * @return the number of processes | 513 | * @return the number of processes |
514 | */ | 514 | */ |
515 | static unsigned int | 515 | static unsigned int |
516 | get_nproc() | 516 | get_nproc () |
517 | { | 517 | { |
518 | DIR *dir; | 518 | DIR *dir; |
519 | struct dirent *ent; | 519 | struct dirent *ent; |
520 | unsigned int nproc; | 520 | unsigned int nproc; |
521 | 521 | ||
522 | dir = opendir("/proc"); | 522 | dir = opendir ("/proc"); |
523 | if (NULL == dir) | 523 | if (NULL == dir) |
524 | return 0; | 524 | return 0; |
525 | nproc = 0; | 525 | nproc = 0; |
526 | while (NULL != (ent = readdir(dir))) | 526 | while (NULL != (ent = readdir (dir))) |
527 | { | 527 | { |
528 | if ((*ent->d_name > '0') && (*ent->d_name <= '9')) | 528 | if ((*ent->d_name > '0') && (*ent->d_name <= '9')) |
529 | nproc++; | 529 | nproc++; |
530 | } | 530 | } |
531 | closedir(dir); | 531 | closedir (dir); |
532 | return nproc; | 532 | return nproc; |
533 | } | 533 | } |
534 | #endif | 534 | #endif |
535 | 535 | ||
536 | 536 | ||
537 | static void | 537 | static void |
538 | sample_load_task(void *cls) | 538 | sample_load_task (void *cls) |
539 | { | 539 | { |
540 | struct GNUNET_TIME_Absolute now; | 540 | struct GNUNET_TIME_Absolute now; |
541 | char *str; | 541 | char *str; |
@@ -546,31 +546,32 @@ sample_load_task(void *cls) | |||
546 | unsigned int nproc; | 546 | unsigned int nproc; |
547 | 547 | ||
548 | sample_load_task_id = NULL; | 548 | sample_load_task_id = NULL; |
549 | ld_cpu = cpu_get_load(); | 549 | ld_cpu = cpu_get_load (); |
550 | ld_disk = disk_get_load(); | 550 | ld_disk = disk_get_load (); |
551 | if ((-1 == ld_cpu) || (-1 == ld_disk)) | 551 | if ((-1 == ld_cpu) || (-1 == ld_disk)) |
552 | goto reschedule; | 552 | goto reschedule; |
553 | mem_usage = mem_get_usage(); | 553 | mem_usage = mem_get_usage (); |
554 | #ifdef LINUX | 554 | #ifdef LINUX |
555 | nproc = get_nproc(); | 555 | nproc = get_nproc (); |
556 | #else | 556 | #else |
557 | nproc = 0; | 557 | nproc = 0; |
558 | #endif | 558 | #endif |
559 | now = GNUNET_TIME_absolute_get(); | 559 | now = GNUNET_TIME_absolute_get (); |
560 | nbs = GNUNET_asprintf(&str, "%llu %d %d %u %u\n", now.abs_value_us / 1000LL / 1000LL, | 560 | nbs = GNUNET_asprintf (&str, "%llu %d %d %u %u\n", now.abs_value_us / 1000LL |
561 | ld_cpu, ld_disk, mem_usage, nproc); | 561 | / 1000LL, |
562 | ld_cpu, ld_disk, mem_usage, nproc); | ||
562 | if (0 < nbs) | 563 | if (0 < nbs) |
563 | { | 564 | { |
564 | GNUNET_BIO_write(bw, str, nbs); | 565 | GNUNET_BIO_write (bw, str, nbs); |
565 | } | 566 | } |
566 | else | 567 | else |
567 | GNUNET_break(0); | 568 | GNUNET_break (0); |
568 | GNUNET_free(str); | 569 | GNUNET_free (str); |
569 | 570 | ||
570 | reschedule: | 571 | reschedule: |
571 | sample_load_task_id = | 572 | sample_load_task_id = |
572 | GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, | 573 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, |
573 | &sample_load_task, NULL); | 574 | &sample_load_task, NULL); |
574 | } | 575 | } |
575 | 576 | ||
576 | 577 | ||
@@ -580,7 +581,7 @@ reschedule: | |||
580 | * generated from the hostname and the process's PID. | 581 | * generated from the hostname and the process's PID. |
581 | */ | 582 | */ |
582 | void | 583 | void |
583 | GST_stats_init(const struct GNUNET_CONFIGURATION_Handle *cfg) | 584 | GST_stats_init (const struct GNUNET_CONFIGURATION_Handle *cfg) |
584 | { | 585 | { |
585 | char *hostname; | 586 | char *hostname; |
586 | char *stats_dir; | 587 | char *stats_dir; |
@@ -588,42 +589,42 @@ GST_stats_init(const struct GNUNET_CONFIGURATION_Handle *cfg) | |||
588 | size_t len; | 589 | size_t len; |
589 | 590 | ||
590 | if (GNUNET_OK != | 591 | if (GNUNET_OK != |
591 | GNUNET_CONFIGURATION_get_value_filename(cfg, "testbed", | 592 | GNUNET_CONFIGURATION_get_value_filename (cfg, "testbed", |
592 | "STATS_DIR", &stats_dir)) | 593 | "STATS_DIR", &stats_dir)) |
593 | return; | 594 | return; |
594 | len = GNUNET_OS_get_hostname_max_length(); | 595 | len = GNUNET_OS_get_hostname_max_length (); |
595 | hostname = GNUNET_malloc(len); | 596 | hostname = GNUNET_malloc (len); |
596 | if (0 != gethostname(hostname, len)) | 597 | if (0 != gethostname (hostname, len)) |
597 | { | 598 | { |
598 | GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "gethostname"); | 599 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "gethostname"); |
599 | GNUNET_free(stats_dir); | 600 | GNUNET_free (stats_dir); |
600 | GNUNET_free(hostname); | 601 | GNUNET_free (hostname); |
601 | return; | 602 | return; |
602 | } | 603 | } |
603 | fn = NULL; | 604 | fn = NULL; |
604 | (void)GNUNET_asprintf(&fn, "%s/%.*s-%jd.dat", stats_dir, len, | 605 | (void) GNUNET_asprintf (&fn, "%s/%.*s-%jd.dat", stats_dir, len, |
605 | hostname, (intmax_t)getpid()); | 606 | hostname, (intmax_t) getpid ()); |
606 | GNUNET_free(stats_dir); | 607 | GNUNET_free (stats_dir); |
607 | GNUNET_free(hostname); | 608 | GNUNET_free (hostname); |
608 | if (NULL == (bw = GNUNET_BIO_write_open(fn))) | 609 | if (NULL == (bw = GNUNET_BIO_write_open (fn))) |
609 | { | 610 | { |
610 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, | 611 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
611 | _("Cannot open %s for writing load statistics. " | 612 | _ ("Cannot open %s for writing load statistics. " |
612 | "Not logging load statistics\n"), fn); | 613 | "Not logging load statistics\n"), fn); |
613 | GNUNET_free(fn); | 614 | GNUNET_free (fn); |
614 | return; | 615 | return; |
615 | } | 616 | } |
616 | GNUNET_free(fn); | 617 | GNUNET_free (fn); |
617 | sample_load_task_id = GNUNET_SCHEDULER_add_now(&sample_load_task, NULL); | 618 | sample_load_task_id = GNUNET_SCHEDULER_add_now (&sample_load_task, NULL); |
618 | #ifdef LINUX | 619 | #ifdef LINUX |
619 | proc_stat = fopen("/proc/stat", "r"); | 620 | proc_stat = fopen ("/proc/stat", "r"); |
620 | if (NULL == proc_stat) | 621 | if (NULL == proc_stat) |
621 | GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, | 622 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, |
622 | "fopen", "/proc/stat"); | 623 | "fopen", "/proc/stat"); |
623 | #elif OSX | 624 | #elif OSX |
624 | initMachCpuStats(); | 625 | initMachCpuStats (); |
625 | #endif | 626 | #endif |
626 | updateUsage(); /* initialize */ | 627 | updateUsage (); /* initialize */ |
627 | } | 628 | } |
628 | 629 | ||
629 | 630 | ||
@@ -631,25 +632,25 @@ GST_stats_init(const struct GNUNET_CONFIGURATION_Handle *cfg) | |||
631 | * Shutdown the status calls module. | 632 | * Shutdown the status calls module. |
632 | */ | 633 | */ |
633 | void | 634 | void |
634 | GST_stats_destroy() | 635 | GST_stats_destroy () |
635 | { | 636 | { |
636 | if (NULL == bw) | 637 | if (NULL == bw) |
637 | return; | 638 | return; |
638 | #ifdef LINUX | 639 | #ifdef LINUX |
639 | if (proc_stat != NULL) | 640 | if (proc_stat != NULL) |
640 | { | 641 | { |
641 | fclose(proc_stat); | 642 | fclose (proc_stat); |
642 | proc_stat = NULL; | 643 | proc_stat = NULL; |
643 | } | 644 | } |
644 | #elif OSX | 645 | #elif OSX |
645 | GNUNET_free_non_null(prev_cpu_load); | 646 | GNUNET_free_non_null (prev_cpu_load); |
646 | #endif | 647 | #endif |
647 | if (NULL != sample_load_task_id) | 648 | if (NULL != sample_load_task_id) |
648 | { | 649 | { |
649 | GNUNET_SCHEDULER_cancel(sample_load_task_id); | 650 | GNUNET_SCHEDULER_cancel (sample_load_task_id); |
650 | sample_load_task_id = NULL; | 651 | sample_load_task_id = NULL; |
651 | } | 652 | } |
652 | GNUNET_break(GNUNET_OK == GNUNET_BIO_write_close(bw)); | 653 | GNUNET_break (GNUNET_OK == GNUNET_BIO_write_close (bw)); |
653 | bw = NULL; | 654 | bw = NULL; |
654 | } | 655 | } |
655 | 656 | ||