aboutsummaryrefslogtreecommitdiff
path: root/src/testbed/gnunet-service-testbed_cpustatus.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testbed/gnunet-service-testbed_cpustatus.c')
-rw-r--r--src/testbed/gnunet-service-testbed_cpustatus.c363
1 files changed, 181 insertions, 182 deletions
diff --git a/src/testbed/gnunet-service-testbed_cpustatus.c b/src/testbed/gnunet-service-testbed_cpustatus.c
index d715036c7..a8796ea11 100644
--- a/src/testbed/gnunet-service-testbed_cpustatus.c
+++ b/src/testbed/gnunet-service-testbed_cpustatus.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file testbed/gnunet-service-testbed_cpustatus.c 22 * @file testbed/gnunet-service-testbed_cpustatus.c
@@ -90,7 +90,7 @@ struct GNUNET_SCHEDULER_Task * sample_load_task_id;
90 90
91#ifdef OSX 91#ifdef OSX
92static int 92static int
93initMachCpuStats () 93initMachCpuStats()
94{ 94{
95 unsigned int cpu_count; 95 unsigned int cpu_count;
96 processor_cpu_load_info_t cpu_load; 96 processor_cpu_load_info_t cpu_load;
@@ -98,17 +98,17 @@ initMachCpuStats ()
98 kern_return_t kret; 98 kern_return_t kret;
99 int i, j; 99 int i, j;
100 100
101 kret = host_processor_info (mach_host_self (), 101 kret = host_processor_info(mach_host_self(),
102 PROCESSOR_CPU_LOAD_INFO, 102 PROCESSOR_CPU_LOAD_INFO,
103 &cpu_count, 103 &cpu_count,
104 (processor_info_array_t *) & cpu_load, 104 (processor_info_array_t *)&cpu_load,
105 &cpu_msg_count); 105 &cpu_msg_count);
106 if (kret != KERN_SUCCESS) 106 if (kret != KERN_SUCCESS)
107 { 107 {
108 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "host_processor_info failed."); 108 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "host_processor_info failed.");
109 return GNUNET_SYSERR; 109 return GNUNET_SYSERR;
110 } 110 }
111 prev_cpu_load = GNUNET_malloc (cpu_count * sizeof (*prev_cpu_load)); 111 prev_cpu_load = GNUNET_malloc(cpu_count * sizeof(*prev_cpu_load));
112 for (i = 0; i < cpu_count; i++) 112 for (i = 0; i < cpu_count; i++)
113 { 113 {
114 for (j = 0; j < CPU_STATE_MAX; j++) 114 for (j = 0; j < CPU_STATE_MAX; j++)
@@ -116,9 +116,9 @@ initMachCpuStats ()
116 prev_cpu_load[i].cpu_ticks[j] = cpu_load[i].cpu_ticks[j]; 116 prev_cpu_load[i].cpu_ticks[j] = cpu_load[i].cpu_ticks[j];
117 } 117 }
118 } 118 }
119 vm_deallocate (mach_task_self (), 119 vm_deallocate(mach_task_self(),
120 (vm_address_t) cpu_load, 120 (vm_address_t)cpu_load,
121 (vm_size_t) (cpu_msg_count * sizeof (*cpu_load))); 121 (vm_size_t)(cpu_msg_count * sizeof(*cpu_load)));
122 return GNUNET_OK; 122 return GNUNET_OK;
123} 123}
124#endif 124#endif
@@ -130,7 +130,7 @@ initMachCpuStats ()
130 * If there is an error the method returns -1. 130 * If there is an error the method returns -1.
131 */ 131 */
132static int 132static int
133updateUsage () 133updateUsage()
134{ 134{
135 currentIOLoad = -1; 135 currentIOLoad = -1;
136 currentCPULoad = -1; 136 currentCPULoad = -1;
@@ -145,30 +145,30 @@ updateUsage ()
145 int ret; 145 int ret;
146 char line[256]; 146 char line[256];
147 unsigned long long user_read, system_read, nice_read, idle_read, 147 unsigned long long user_read, system_read, nice_read, idle_read,
148 iowait_read; 148 iowait_read;
149 unsigned long long user, system, nice, idle, iowait; 149 unsigned long long user, system, nice, idle, iowait;
150 unsigned long long usage_time = 0, total_time = 1; 150 unsigned long long usage_time = 0, total_time = 1;
151 151
152 /* Get the first line with the data */ 152 /* Get the first line with the data */
153 rewind (proc_stat); 153 rewind(proc_stat);
154 fflush (proc_stat); 154 fflush(proc_stat);
155 if (NULL == fgets (line, 256, proc_stat)) 155 if (NULL == fgets(line, 256, proc_stat))
156 { 156 {
157 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, 157 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING,
158 "fgets", "/proc/stat"); 158 "fgets", "/proc/stat");
159 proc_stat = NULL; /* don't try again */ 159 proc_stat = NULL; /* don't try again */
160 } 160 }
161 else 161 else
162 { 162 {
163 iowait_read = 0; 163 iowait_read = 0;
164 ret = sscanf (line, "%*s %llu %llu %llu %llu %llu", 164 ret = sscanf(line, "%*s %llu %llu %llu %llu %llu",
165 &user_read, 165 &user_read,
166 &system_read, &nice_read, &idle_read, &iowait_read); 166 &system_read, &nice_read, &idle_read, &iowait_read);
167 if (ret < 4) 167 if (ret < 4)
168 { 168 {
169 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, 169 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING,
170 "fgets-sscanf", "/proc/stat"); 170 "fgets-sscanf", "/proc/stat");
171 fclose (proc_stat); 171 fclose(proc_stat);
172 proc_stat = NULL; /* don't try again */ 172 proc_stat = NULL; /* don't try again */
173 have_last_cpu = GNUNET_NO; 173 have_last_cpu = GNUNET_NO;
174 } 174 }
@@ -185,9 +185,9 @@ updateUsage ()
185 total_time = usage_time + idle + iowait; 185 total_time = usage_time + idle + iowait;
186 if ((total_time > 0) && (have_last_cpu == GNUNET_YES)) 186 if ((total_time > 0) && (have_last_cpu == GNUNET_YES))
187 { 187 {
188 currentCPULoad = (int) (100L * usage_time / total_time); 188 currentCPULoad = (int)(100L * usage_time / total_time);
189 if (ret > 4) 189 if (ret > 4)
190 currentIOLoad = (int) (100L * iowait / total_time); 190 currentIOLoad = (int)(100L * iowait / total_time);
191 else 191 else
192 currentIOLoad = -1; /* 2.4 kernel */ 192 currentIOLoad = -1; /* 2.4 kernel */
193 } 193 }
@@ -215,10 +215,10 @@ updateUsage ()
215 int i, j; 215 int i, j;
216 216
217 t_idle_all = t_total_all = 0; 217 t_idle_all = t_total_all = 0;
218 kret = host_processor_info (mach_host_self (), PROCESSOR_CPU_LOAD_INFO, 218 kret = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO,
219 &cpu_count, 219 &cpu_count,
220 (processor_info_array_t *) & cpu_load, 220 (processor_info_array_t *)&cpu_load,
221 &cpu_msg_count); 221 &cpu_msg_count);
222 if (kret == KERN_SUCCESS) 222 if (kret == KERN_SUCCESS)
223 { 223 {
224 for (i = 0; i < cpu_count; i++) 224 for (i = 0; i < cpu_count; i++)
@@ -227,52 +227,52 @@ updateUsage ()
227 prev_cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM]) 227 prev_cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM])
228 { 228 {
229 t_sys = cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM] - 229 t_sys = cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM] -
230 prev_cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM]; 230 prev_cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM];
231 } 231 }
232 else 232 else
233 { 233 {
234 t_sys = cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM] + 234 t_sys = cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM] +
235 (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM] + 235 (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM] +
236 1); 236 1);
237 } 237 }
238 238
239 if (cpu_load[i].cpu_ticks[CPU_STATE_USER] >= 239 if (cpu_load[i].cpu_ticks[CPU_STATE_USER] >=
240 prev_cpu_load[i].cpu_ticks[CPU_STATE_USER]) 240 prev_cpu_load[i].cpu_ticks[CPU_STATE_USER])
241 { 241 {
242 t_user = cpu_load[i].cpu_ticks[CPU_STATE_USER] - 242 t_user = cpu_load[i].cpu_ticks[CPU_STATE_USER] -
243 prev_cpu_load[i].cpu_ticks[CPU_STATE_USER]; 243 prev_cpu_load[i].cpu_ticks[CPU_STATE_USER];
244 } 244 }
245 else 245 else
246 { 246 {
247 t_user = cpu_load[i].cpu_ticks[CPU_STATE_USER] + 247 t_user = cpu_load[i].cpu_ticks[CPU_STATE_USER] +
248 (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_USER] + 248 (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_USER] +
249 1); 249 1);
250 } 250 }
251 251
252 if (cpu_load[i].cpu_ticks[CPU_STATE_NICE] >= 252 if (cpu_load[i].cpu_ticks[CPU_STATE_NICE] >=
253 prev_cpu_load[i].cpu_ticks[CPU_STATE_NICE]) 253 prev_cpu_load[i].cpu_ticks[CPU_STATE_NICE])
254 { 254 {
255 t_nice = cpu_load[i].cpu_ticks[CPU_STATE_NICE] - 255 t_nice = cpu_load[i].cpu_ticks[CPU_STATE_NICE] -
256 prev_cpu_load[i].cpu_ticks[CPU_STATE_NICE]; 256 prev_cpu_load[i].cpu_ticks[CPU_STATE_NICE];
257 } 257 }
258 else 258 else
259 { 259 {
260 t_nice = cpu_load[i].cpu_ticks[CPU_STATE_NICE] + 260 t_nice = cpu_load[i].cpu_ticks[CPU_STATE_NICE] +
261 (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_NICE] + 261 (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_NICE] +
262 1); 262 1);
263 } 263 }
264 264
265 if (cpu_load[i].cpu_ticks[CPU_STATE_IDLE] >= 265 if (cpu_load[i].cpu_ticks[CPU_STATE_IDLE] >=
266 prev_cpu_load[i].cpu_ticks[CPU_STATE_IDLE]) 266 prev_cpu_load[i].cpu_ticks[CPU_STATE_IDLE])
267 { 267 {
268 t_idle = cpu_load[i].cpu_ticks[CPU_STATE_IDLE] - 268 t_idle = cpu_load[i].cpu_ticks[CPU_STATE_IDLE] -
269 prev_cpu_load[i].cpu_ticks[CPU_STATE_IDLE]; 269 prev_cpu_load[i].cpu_ticks[CPU_STATE_IDLE];
270 } 270 }
271 else 271 else
272 { 272 {
273 t_idle = cpu_load[i].cpu_ticks[CPU_STATE_IDLE] + 273 t_idle = cpu_load[i].cpu_ticks[CPU_STATE_IDLE] +
274 (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_IDLE] + 274 (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_IDLE] +
275 1); 275 1);
276 } 276 }
277 t_total = t_sys + t_user + t_nice + t_idle; 277 t_total = t_sys + t_user + t_nice + t_idle;
278 t_idle_all += t_idle; 278 t_idle_all += t_idle;
@@ -289,15 +289,15 @@ updateUsage ()
289 currentCPULoad = 100 - (100 * t_idle_all) / t_total_all; 289 currentCPULoad = 100 - (100 * t_idle_all) / t_total_all;
290 else 290 else
291 currentCPULoad = -1; 291 currentCPULoad = -1;
292 vm_deallocate (mach_task_self (), 292 vm_deallocate(mach_task_self(),
293 (vm_address_t) cpu_load, 293 (vm_address_t)cpu_load,
294 (vm_size_t) (cpu_msg_count * sizeof (*cpu_load))); 294 (vm_size_t)(cpu_msg_count * sizeof(*cpu_load)));
295 currentIOLoad = -1; /* FIXME-OSX! */ 295 currentIOLoad = -1; /* FIXME-OSX! */
296 return GNUNET_OK; 296 return GNUNET_OK;
297 } 297 }
298 else 298 else
299 { 299 {
300 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "host_processor_info failed."); 300 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "host_processor_info failed.");
301 return GNUNET_SYSERR; 301 return GNUNET_SYSERR;
302 } 302 }
303 } 303 }
@@ -318,10 +318,10 @@ updateUsage ()
318 318
319 if (kstat_once == 1) 319 if (kstat_once == 1)
320 goto ABORT_KSTAT; 320 goto ABORT_KSTAT;
321 kc = kstat_open (); 321 kc = kstat_open();
322 if (kc == NULL) 322 if (kc == NULL)
323 { 323 {
324 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kstat_close"); 324 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kstat_close");
325 goto ABORT_KSTAT; 325 goto ABORT_KSTAT;
326 } 326 }
327 327
@@ -331,29 +331,29 @@ updateUsage ()
331 { 331 {
332 cpu_stat_t stats; 332 cpu_stat_t stats;
333 333
334 if (0 != strncmp (khelper->ks_name, "cpu_stat", strlen ("cpu_stat"))) 334 if (0 != strncmp(khelper->ks_name, "cpu_stat", strlen("cpu_stat")))
335 continue; 335 continue;
336 if (khelper->ks_data_size > sizeof (cpu_stat_t)) 336 if (khelper->ks_data_size > sizeof(cpu_stat_t))
337 continue; /* better save then sorry! */ 337 continue; /* better save then sorry! */
338 if (-1 != kstat_read (kc, khelper, &stats)) 338 if (-1 != kstat_read(kc, khelper, &stats))
339 { 339 {
340 idlecount += stats.cpu_sysinfo.cpu[CPU_IDLE]; 340 idlecount += stats.cpu_sysinfo.cpu[CPU_IDLE];
341 totalcount 341 totalcount
342 += stats.cpu_sysinfo.cpu[CPU_IDLE] + 342 += stats.cpu_sysinfo.cpu[CPU_IDLE] +
343 stats.cpu_sysinfo.cpu[CPU_USER] + 343 stats.cpu_sysinfo.cpu[CPU_USER] +
344 stats.cpu_sysinfo.cpu[CPU_KERNEL] + 344 stats.cpu_sysinfo.cpu[CPU_KERNEL] +
345 stats.cpu_sysinfo.cpu[CPU_WAIT]; 345 stats.cpu_sysinfo.cpu[CPU_WAIT];
346 } 346 }
347 } 347 }
348 if (0 != kstat_close (kc)) 348 if (0 != kstat_close(kc))
349 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kstat_close"); 349 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kstat_close");
350 if ((idlecount == 0) && (totalcount == 0)) 350 if ((idlecount == 0) && (totalcount == 0))
351 goto ABORT_KSTAT; /* no stats found => abort */ 351 goto ABORT_KSTAT; /* no stats found => abort */
352 deltaidle = idlecount - last_idlecount; 352 deltaidle = idlecount - last_idlecount;
353 deltatotal = totalcount - last_totalcount; 353 deltatotal = totalcount - last_totalcount;
354 if ((deltatotal > 0) && (last_totalcount > 0)) 354 if ((deltatotal > 0) && (last_totalcount > 0))
355 { 355 {
356 currentCPULoad = (unsigned int) (100.0 * deltaidle / deltatotal); 356 currentCPULoad = (unsigned int)(100.0 * deltaidle / deltatotal);
357 if (currentCPULoad > 100) 357 if (currentCPULoad > 100)
358 currentCPULoad = 100; /* odd */ 358 currentCPULoad = 100; /* odd */
359 if (currentCPULoad < 0) 359 if (currentCPULoad < 0)
@@ -366,7 +366,7 @@ updateUsage ()
366 last_idlecount = idlecount; 366 last_idlecount = idlecount;
367 last_totalcount = totalcount; 367 last_totalcount = totalcount;
368 return GNUNET_OK; 368 return GNUNET_OK;
369 ABORT_KSTAT: 369ABORT_KSTAT:
370 kstat_once = 1; /* failed, don't try again */ 370 kstat_once = 1; /* failed, don't try again */
371 return GNUNET_SYSERR; 371 return GNUNET_SYSERR;
372 } 372 }
@@ -380,21 +380,21 @@ updateUsage ()
380 { 380 {
381 static int warnOnce = 0; 381 static int warnOnce = 0;
382 double loadavg; 382 double loadavg;
383 if (1 != getloadavg (&loadavg, 1)) 383 if (1 != getloadavg(&loadavg, 1))
384 { 384 {
385 /* only warn once, if there is a problem with 385 /* only warn once, if there is a problem with
386 getloadavg, we're going to hit it frequently... */ 386 getloadavg, we're going to hit it frequently... */
387 if (warnOnce == 0) 387 if (warnOnce == 0)
388 { 388 {
389 warnOnce = 1; 389 warnOnce = 1;
390 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "getloadavg"); 390 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "getloadavg");
391 } 391 }
392 return GNUNET_SYSERR; 392 return GNUNET_SYSERR;
393 } 393 }
394 else 394 else
395 { 395 {
396 /* success with getloadavg */ 396 /* success with getloadavg */
397 currentCPULoad = (int) (100 * loadavg); 397 currentCPULoad = (int)(100 * loadavg);
398 currentIOLoad = -1; /* FIXME */ 398 currentIOLoad = -1; /* FIXME */
399 return GNUNET_OK; 399 return GNUNET_OK;
400 } 400 }
@@ -416,14 +416,14 @@ updateUsage ()
416 double dDiffUser; 416 double dDiffUser;
417 SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION theInfo; 417 SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION theInfo;
418 418
419 if (GNNtQuerySystemInformation (SystemProcessorPerformanceInformation, 419 if (GNNtQuerySystemInformation(SystemProcessorPerformanceInformation,
420 &theInfo, 420 &theInfo,
421 sizeof (theInfo), NULL) == NO_ERROR) 421 sizeof(theInfo), NULL) == NO_ERROR)
422 { 422 {
423 /* PORT-ME MINGW: Multi-processor? */ 423 /* PORT-ME MINGW: Multi-processor? */
424 dKernel = Li2Double (theInfo.KernelTime); 424 dKernel = Li2Double(theInfo.KernelTime);
425 dIdle = Li2Double (theInfo.IdleTime); 425 dIdle = Li2Double(theInfo.IdleTime);
426 dUser = Li2Double (theInfo.UserTime); 426 dUser = Li2Double(theInfo.UserTime);
427 dDiffKernel = dKernel - dLastKernel; 427 dDiffKernel = dKernel - dLastKernel;
428 dDiffIdle = dIdle - dLastIdle; 428 dDiffIdle = dIdle - dLastIdle;
429 dDiffUser = dUser - dLastUser; 429 dDiffUser = dUser - dLastUser;
@@ -450,8 +450,8 @@ updateUsage ()
450 if (once == 0) 450 if (once == 0)
451 { 451 {
452 once = 1; 452 once = 1;
453 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 453 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
454 "Cannot query the CPU usage (Windows NT).\n"); 454 "Cannot query the CPU usage (Windows NT).\n");
455 } 455 }
456 return GNUNET_SYSERR; 456 return GNUNET_SYSERR;
457 } 457 }
@@ -462,48 +462,48 @@ updateUsage ()
462 DWORD dwDataSize, dwType, dwDummy; 462 DWORD dwDataSize, dwType, dwDummy;
463 463
464 /* Start query */ 464 /* Start query */
465 if (RegOpenKeyEx (HKEY_DYN_DATA, 465 if (RegOpenKeyEx(HKEY_DYN_DATA,
466 "PerfStats\\StartSrv", 466 "PerfStats\\StartSrv",
467 0, KEY_ALL_ACCESS, &hKey) != ERROR_SUCCESS) 467 0, KEY_ALL_ACCESS, &hKey) != ERROR_SUCCESS)
468 { 468 {
469 /* only warn once */ 469 /* only warn once */
470 static int once = 0; 470 static int once = 0;
471 if (once == 0) 471 if (once == 0)
472 { 472 {
473 once = 1; 473 once = 1;
474 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 474 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
475 "Cannot query the CPU usage (Win 9x)\n"); 475 "Cannot query the CPU usage (Win 9x)\n");
476 } 476 }
477 } 477 }
478 478
479 RegOpenKeyEx (HKEY_DYN_DATA, 479 RegOpenKeyEx(HKEY_DYN_DATA,
480 "PerfStats\\StartStat", 0, KEY_ALL_ACCESS, &hKey); 480 "PerfStats\\StartStat", 0, KEY_ALL_ACCESS, &hKey);
481 dwDataSize = sizeof (dwDummy); 481 dwDataSize = sizeof(dwDummy);
482 RegQueryValueEx (hKey, 482 RegQueryValueEx(hKey,
483 "KERNEL\\CPUUsage", 483 "KERNEL\\CPUUsage",
484 NULL, &dwType, (LPBYTE) & dwDummy, &dwDataSize); 484 NULL, &dwType, (LPBYTE)&dwDummy, &dwDataSize);
485 RegCloseKey (hKey); 485 RegCloseKey(hKey);
486 486
487 /* Get CPU usage */ 487 /* Get CPU usage */
488 RegOpenKeyEx (HKEY_DYN_DATA, 488 RegOpenKeyEx(HKEY_DYN_DATA,
489 "PerfStats\\StatData", 0, KEY_ALL_ACCESS, &hKey); 489 "PerfStats\\StatData", 0, KEY_ALL_ACCESS, &hKey);
490 dwDataSize = sizeof (currentCPULoad); 490 dwDataSize = sizeof(currentCPULoad);
491 RegQueryValueEx (hKey, 491 RegQueryValueEx(hKey,
492 "KERNEL\\CPUUsage", 492 "KERNEL\\CPUUsage",
493 NULL, &dwType, (LPBYTE) & currentCPULoad, &dwDataSize); 493 NULL, &dwType, (LPBYTE)&currentCPULoad, &dwDataSize);
494 RegCloseKey (hKey); 494 RegCloseKey(hKey);
495 currentIOLoad = -1; /* FIXME-MINGW! */ 495 currentIOLoad = -1; /* FIXME-MINGW! */
496 496
497 /* Stop query */ 497 /* Stop query */
498 RegOpenKeyEx (HKEY_DYN_DATA, 498 RegOpenKeyEx(HKEY_DYN_DATA,
499 "PerfStats\\StopStat", 0, KEY_ALL_ACCESS, &hKey); 499 "PerfStats\\StopStat", 0, KEY_ALL_ACCESS, &hKey);
500 RegOpenKeyEx (HKEY_DYN_DATA, 500 RegOpenKeyEx(HKEY_DYN_DATA,
501 "PerfStats\\StopSrv", 0, KEY_ALL_ACCESS, &hKey); 501 "PerfStats\\StopSrv", 0, KEY_ALL_ACCESS, &hKey);
502 dwDataSize = sizeof (dwDummy); 502 dwDataSize = sizeof(dwDummy);
503 RegQueryValueEx (hKey, 503 RegQueryValueEx(hKey,
504 "KERNEL\\CPUUsage", 504 "KERNEL\\CPUUsage",
505 NULL, &dwType, (LPBYTE) & dwDummy, &dwDataSize); 505 NULL, &dwType, (LPBYTE)&dwDummy, &dwDataSize);
506 RegCloseKey (hKey); 506 RegCloseKey(hKey);
507 507
508 return GNUNET_OK; 508 return GNUNET_OK;
509 } 509 }
@@ -523,21 +523,21 @@ updateUsage ()
523 * that lock has already been obtained. 523 * that lock has already been obtained.
524 */ 524 */
525static void 525static void
526updateAgedLoad () 526updateAgedLoad()
527{ 527{
528 static struct GNUNET_TIME_Absolute lastCall; 528 static struct GNUNET_TIME_Absolute lastCall;
529 struct GNUNET_TIME_Relative age; 529 struct GNUNET_TIME_Relative age;
530 530
531 age = GNUNET_TIME_absolute_get_duration (lastCall); 531 age = GNUNET_TIME_absolute_get_duration(lastCall);
532 if ( (agedCPULoad == -1) 532 if ((agedCPULoad == -1)
533 || (age.rel_value_us > 500000) ) 533 || (age.rel_value_us > 500000))
534 { 534 {
535 /* use smoothing, but do NOT update lastRet at frequencies higher 535 /* use smoothing, but do NOT update lastRet at frequencies higher
536 than 500ms; this makes the smoothing (mostly) independent from 536 than 500ms; this makes the smoothing (mostly) independent from
537 the frequency at which getCPULoad is called (and we don't spend 537 the frequency at which getCPULoad is called (and we don't spend
538 more time measuring CPU than actually computing something). */ 538 more time measuring CPU than actually computing something). */
539 lastCall = GNUNET_TIME_absolute_get (); 539 lastCall = GNUNET_TIME_absolute_get();
540 updateUsage (); 540 updateUsage();
541 if (currentCPULoad == -1) 541 if (currentCPULoad == -1)
542 { 542 {
543 agedCPULoad = -1; 543 agedCPULoad = -1;
@@ -581,10 +581,10 @@ updateAgedLoad ()
581 * (100 is equivalent to full load) 581 * (100 is equivalent to full load)
582 */ 582 */
583static int 583static int
584cpu_get_load () 584cpu_get_load()
585{ 585{
586 updateAgedLoad (); 586 updateAgedLoad();
587 return (int) agedCPULoad; 587 return (int)agedCPULoad;
588} 588}
589 589
590 590
@@ -594,10 +594,10 @@ cpu_get_load ()
594 * (100 is equivalent to full load) 594 * (100 is equivalent to full load)
595 */ 595 */
596static int 596static int
597disk_get_load () 597disk_get_load()
598{ 598{
599 updateAgedLoad (); 599 updateAgedLoad();
600 return (int) agedIOLoad; 600 return (int)agedIOLoad;
601} 601}
602 602
603/** 603/**
@@ -606,13 +606,13 @@ disk_get_load ()
606 * @return the percentage of memory used 606 * @return the percentage of memory used
607 */ 607 */
608static unsigned int 608static unsigned int
609mem_get_usage () 609mem_get_usage()
610{ 610{
611 double percentage; 611 double percentage;
612 612
613 meminfo (); 613 meminfo();
614 percentage = ( ((double) kb_main_used) / ((double) kb_main_total) * 100.0 ); 614 percentage = (((double)kb_main_used) / ((double)kb_main_total) * 100.0);
615 return (unsigned int) percentage; 615 return (unsigned int)percentage;
616} 616}
617 617
618 618
@@ -624,29 +624,29 @@ mem_get_usage ()
624 * @return the number of processes 624 * @return the number of processes
625 */ 625 */
626static unsigned int 626static unsigned int
627get_nproc () 627get_nproc()
628{ 628{
629 DIR *dir; 629 DIR *dir;
630 struct dirent *ent; 630 struct dirent *ent;
631 unsigned int nproc; 631 unsigned int nproc;
632 632
633 dir = opendir ("/proc"); 633 dir = opendir("/proc");
634 if (NULL == dir) 634 if (NULL == dir)
635 return 0; 635 return 0;
636 nproc = 0; 636 nproc = 0;
637 while (NULL != (ent = readdir (dir))) 637 while (NULL != (ent = readdir(dir)))
638 { 638 {
639 if((*ent->d_name > '0') && (*ent->d_name <= '9')) 639 if ((*ent->d_name > '0') && (*ent->d_name <= '9'))
640 nproc++; 640 nproc++;
641 } 641 }
642 closedir (dir); 642 closedir(dir);
643 return nproc; 643 return nproc;
644} 644}
645#endif 645#endif
646 646
647 647
648static void 648static void
649sample_load_task (void *cls) 649sample_load_task(void *cls)
650{ 650{
651 struct GNUNET_TIME_Absolute now; 651 struct GNUNET_TIME_Absolute now;
652 char *str; 652 char *str;
@@ -657,31 +657,31 @@ sample_load_task (void *cls)
657 unsigned int nproc; 657 unsigned int nproc;
658 658
659 sample_load_task_id = NULL; 659 sample_load_task_id = NULL;
660 ld_cpu = cpu_get_load (); 660 ld_cpu = cpu_get_load();
661 ld_disk = disk_get_load (); 661 ld_disk = disk_get_load();
662 if ( (-1 == ld_cpu) || (-1 == ld_disk) ) 662 if ((-1 == ld_cpu) || (-1 == ld_disk))
663 goto reschedule; 663 goto reschedule;
664 mem_usage = mem_get_usage (); 664 mem_usage = mem_get_usage();
665#ifdef LINUX 665#ifdef LINUX
666 nproc = get_nproc (); 666 nproc = get_nproc();
667#else 667#else
668 nproc = 0; 668 nproc = 0;
669#endif 669#endif
670 now = GNUNET_TIME_absolute_get (); 670 now = GNUNET_TIME_absolute_get();
671 nbs = GNUNET_asprintf (&str, "%llu %d %d %u %u\n", now.abs_value_us / 1000LL / 1000LL, 671 nbs = GNUNET_asprintf(&str, "%llu %d %d %u %u\n", now.abs_value_us / 1000LL / 1000LL,
672 ld_cpu, ld_disk, mem_usage, nproc); 672 ld_cpu, ld_disk, mem_usage, nproc);
673 if (0 < nbs) 673 if (0 < nbs)
674 { 674 {
675 GNUNET_BIO_write (bw, str, nbs); 675 GNUNET_BIO_write(bw, str, nbs);
676 } 676 }
677 else 677 else
678 GNUNET_break (0); 678 GNUNET_break(0);
679 GNUNET_free (str); 679 GNUNET_free(str);
680 680
681 reschedule: 681reschedule:
682 sample_load_task_id = 682 sample_load_task_id =
683 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, 683 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
684 &sample_load_task, NULL); 684 &sample_load_task, NULL);
685} 685}
686 686
687 687
@@ -691,7 +691,7 @@ sample_load_task (void *cls)
691 * generated from the hostname and the process's PID. 691 * generated from the hostname and the process's PID.
692 */ 692 */
693void 693void
694GST_stats_init (const struct GNUNET_CONFIGURATION_Handle *cfg) 694GST_stats_init(const struct GNUNET_CONFIGURATION_Handle *cfg)
695{ 695{
696 char *hostname; 696 char *hostname;
697 char *stats_dir; 697 char *stats_dir;
@@ -699,49 +699,48 @@ GST_stats_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
699 size_t len; 699 size_t len;
700 700
701#if MINGW 701#if MINGW
702 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 702 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
703 "Load statistics logging now available for windows\n"); 703 "Load statistics logging now available for windows\n");
704 return; /* No logging on windows for now :( */ 704 return; /* No logging on windows for now :( */
705#endif 705#endif
706 706
707 if (GNUNET_OK != 707 if (GNUNET_OK !=
708 GNUNET_CONFIGURATION_get_value_filename (cfg, "testbed", 708 GNUNET_CONFIGURATION_get_value_filename(cfg, "testbed",
709 "STATS_DIR", &stats_dir)) 709 "STATS_DIR", &stats_dir))
710 return; 710 return;
711 len = GNUNET_OS_get_hostname_max_length (); 711 len = GNUNET_OS_get_hostname_max_length();
712 hostname = GNUNET_malloc (len); 712 hostname = GNUNET_malloc(len);
713 if (0 != gethostname (hostname, len)) 713 if (0 != gethostname(hostname, len))
714 { 714 {
715 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "gethostname"); 715 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "gethostname");
716 GNUNET_free (stats_dir); 716 GNUNET_free(stats_dir);
717 GNUNET_free (hostname); 717 GNUNET_free(hostname);
718 return; 718 return;
719 } 719 }
720 fn = NULL; 720 fn = NULL;
721 (void) GNUNET_asprintf (&fn, "%s/%.*s-%jd.dat", stats_dir, len, 721 (void)GNUNET_asprintf(&fn, "%s/%.*s-%jd.dat", stats_dir, len,
722 hostname, (intmax_t) getpid()); 722 hostname, (intmax_t)getpid());
723 GNUNET_free (stats_dir); 723 GNUNET_free(stats_dir);
724 GNUNET_free (hostname); 724 GNUNET_free(hostname);
725 if (NULL == (bw = GNUNET_BIO_write_open (fn))) 725 if (NULL == (bw = GNUNET_BIO_write_open(fn)))
726 { 726 {
727 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 727 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
728 _("Cannot open %s for writing load statistics. " 728 _("Cannot open %s for writing load statistics. "
729 "Not logging load statistics\n"), fn); 729 "Not logging load statistics\n"), fn);
730 GNUNET_free (fn); 730 GNUNET_free(fn);
731 return; 731 return;
732 } 732 }
733 GNUNET_free (fn); 733 GNUNET_free(fn);
734 sample_load_task_id = GNUNET_SCHEDULER_add_now (&sample_load_task, NULL); 734 sample_load_task_id = GNUNET_SCHEDULER_add_now(&sample_load_task, NULL);
735#ifdef LINUX 735#ifdef LINUX
736 proc_stat = fopen ("/proc/stat", "r"); 736 proc_stat = fopen("/proc/stat", "r");
737 if (NULL == proc_stat) 737 if (NULL == proc_stat)
738 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, 738 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING,
739 "fopen", "/proc/stat"); 739 "fopen", "/proc/stat");
740#elif OSX 740#elif OSX
741 initMachCpuStats (); 741 initMachCpuStats();
742#endif 742#endif
743 updateUsage (); /* initialize */ 743 updateUsage(); /* initialize */
744
745} 744}
746 745
747 746
@@ -749,7 +748,7 @@ GST_stats_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
749 * Shutdown the status calls module. 748 * Shutdown the status calls module.
750 */ 749 */
751void 750void
752GST_stats_destroy () 751GST_stats_destroy()
753{ 752{
754#if MINGW 753#if MINGW
755 return; 754 return;
@@ -759,18 +758,18 @@ GST_stats_destroy ()
759#ifdef LINUX 758#ifdef LINUX
760 if (proc_stat != NULL) 759 if (proc_stat != NULL)
761 { 760 {
762 fclose (proc_stat); 761 fclose(proc_stat);
763 proc_stat = NULL; 762 proc_stat = NULL;
764 } 763 }
765#elif OSX 764#elif OSX
766 GNUNET_free_non_null (prev_cpu_load); 765 GNUNET_free_non_null(prev_cpu_load);
767#endif 766#endif
768 if (NULL != sample_load_task_id) 767 if (NULL != sample_load_task_id)
769 { 768 {
770 GNUNET_SCHEDULER_cancel (sample_load_task_id); 769 GNUNET_SCHEDULER_cancel(sample_load_task_id);
771 sample_load_task_id = NULL; 770 sample_load_task_id = NULL;
772 } 771 }
773 GNUNET_break (GNUNET_OK == GNUNET_BIO_write_close (bw)); 772 GNUNET_break(GNUNET_OK == GNUNET_BIO_write_close(bw));
774 bw = NULL; 773 bw = NULL;
775} 774}
776 775