2 This file is part of GNUnet.
3 Copyright (C) 2008--2013 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
17 * @file testbed/gnunet-service-testbed_cpustatus.c
18 * @brief calls to determine current CPU load
19 * @author Tzvetan Horozov
20 * @author Christian Grothoff
21 * @author Igor Wronsky
22 * @author Alex Harper (OS X portion)
23 * @author Sree Harsha Totakura
27 #include "gnunet_util_lib.h"
28 #include "gnunet-service-testbed_meminfo.h"
34 #if HAVE_SYS_SYSINFO_H
35 #include <sys/sysinfo.h>
48 #include <mach/mach.h>
50 static processor_cpu_load_info_t prev_cpu_load;
56 #define DEBUG_STATUSCALLS GNUNET_NO
59 static FILE *proc_stat;
63 * Current CPU load, as percentage of CPU cycles not idle or
66 static int currentCPULoad;
68 static double agedCPULoad = -1;
71 * Current IO load, as percentage of CPU cycles blocked on IO.
73 static int currentIOLoad;
75 static double agedIOLoad = -1;
79 * hanlde to the file to write the load statistics to
81 struct GNUNET_BIO_WriteHandle *bw;
83 struct GNUNET_SCHEDULER_Task * sample_load_task_id;
90 unsigned int cpu_count;
91 processor_cpu_load_info_t cpu_load;
92 mach_msg_type_number_t cpu_msg_count;
96 kret = host_processor_info (mach_host_self (),
97 PROCESSOR_CPU_LOAD_INFO,
99 (processor_info_array_t *) & cpu_load,
101 if (kret != KERN_SUCCESS)
103 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "host_processor_info failed.");
104 return GNUNET_SYSERR;
106 prev_cpu_load = GNUNET_malloc (cpu_count * sizeof (*prev_cpu_load));
107 for (i = 0; i < cpu_count; i++)
109 for (j = 0; j < CPU_STATE_MAX; j++)
111 prev_cpu_load[i].cpu_ticks[j] = cpu_load[i].cpu_ticks[j];
114 vm_deallocate (mach_task_self (),
115 (vm_address_t) cpu_load,
116 (vm_size_t) (cpu_msg_count * sizeof (*cpu_load)));
122 * Update the currentCPU and currentIO load (and on Linux, memory) values.
124 * Before its first invocation the method initStatusCalls() must be called.
125 * If there is an error the method returns -1.
133 /* under linux, first try %idle/usage using /proc/stat;
134 if that does not work, disable /proc/stat for the future
135 by closing the file and use the next-best method. */
136 if (proc_stat != NULL)
138 static unsigned long long last_cpu_results[5] = { 0, 0, 0, 0, 0 };
139 static int have_last_cpu = GNUNET_NO;
142 unsigned long long user_read, system_read, nice_read, idle_read,
144 unsigned long long user, system, nice, idle, iowait;
145 unsigned long long usage_time = 0, total_time = 1;
147 /* Get the first line with the data */
150 if (NULL == fgets (line, 256, proc_stat))
152 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
153 "fgets", "/proc/stat");
154 proc_stat = NULL; /* don't try again */
159 ret = sscanf (line, "%*s %llu %llu %llu %llu %llu",
161 &system_read, &nice_read, &idle_read, &iowait_read);
164 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
165 "fgets-sscanf", "/proc/stat");
167 proc_stat = NULL; /* don't try again */
168 have_last_cpu = GNUNET_NO;
172 /* Store the current usage */
173 user = user_read - last_cpu_results[0];
174 system = system_read - last_cpu_results[1];
175 nice = nice_read - last_cpu_results[2];
176 idle = idle_read - last_cpu_results[3];
177 iowait = iowait_read - last_cpu_results[4];
178 /* Calculate the % usage */
179 usage_time = user + system + nice;
180 total_time = usage_time + idle + iowait;
181 if ((total_time > 0) && (have_last_cpu == GNUNET_YES))
183 currentCPULoad = (int) (100L * usage_time / total_time);
185 currentIOLoad = (int) (100L * iowait / total_time);
187 currentIOLoad = -1; /* 2.4 kernel */
189 /* Store the values for the next calculation */
190 last_cpu_results[0] = user_read;
191 last_cpu_results[1] = system_read;
192 last_cpu_results[2] = nice_read;
193 last_cpu_results[3] = idle_read;
194 last_cpu_results[4] = iowait_read;
195 have_last_cpu = GNUNET_YES;
204 unsigned int cpu_count;
205 processor_cpu_load_info_t cpu_load;
206 mach_msg_type_number_t cpu_msg_count;
207 unsigned long long t_sys, t_user, t_nice, t_idle, t_total;
208 unsigned long long t_idle_all, t_total_all;
212 t_idle_all = t_total_all = 0;
213 kret = host_processor_info (mach_host_self (), PROCESSOR_CPU_LOAD_INFO,
215 (processor_info_array_t *) & cpu_load,
217 if (kret == KERN_SUCCESS)
219 for (i = 0; i < cpu_count; i++)
221 if (cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM] >=
222 prev_cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM])
224 t_sys = cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM] -
225 prev_cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM];
229 t_sys = cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM] +
230 (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM] +
234 if (cpu_load[i].cpu_ticks[CPU_STATE_USER] >=
235 prev_cpu_load[i].cpu_ticks[CPU_STATE_USER])
237 t_user = cpu_load[i].cpu_ticks[CPU_STATE_USER] -
238 prev_cpu_load[i].cpu_ticks[CPU_STATE_USER];
242 t_user = cpu_load[i].cpu_ticks[CPU_STATE_USER] +
243 (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_USER] +
247 if (cpu_load[i].cpu_ticks[CPU_STATE_NICE] >=
248 prev_cpu_load[i].cpu_ticks[CPU_STATE_NICE])
250 t_nice = cpu_load[i].cpu_ticks[CPU_STATE_NICE] -
251 prev_cpu_load[i].cpu_ticks[CPU_STATE_NICE];
255 t_nice = cpu_load[i].cpu_ticks[CPU_STATE_NICE] +
256 (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_NICE] +
260 if (cpu_load[i].cpu_ticks[CPU_STATE_IDLE] >=
261 prev_cpu_load[i].cpu_ticks[CPU_STATE_IDLE])
263 t_idle = cpu_load[i].cpu_ticks[CPU_STATE_IDLE] -
264 prev_cpu_load[i].cpu_ticks[CPU_STATE_IDLE];
268 t_idle = cpu_load[i].cpu_ticks[CPU_STATE_IDLE] +
269 (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_IDLE] +
272 t_total = t_sys + t_user + t_nice + t_idle;
273 t_idle_all += t_idle;
274 t_total_all += t_total;
276 for (i = 0; i < cpu_count; i++)
278 for (j = 0; j < CPU_STATE_MAX; j++)
280 prev_cpu_load[i].cpu_ticks[j] = cpu_load[i].cpu_ticks[j];
284 currentCPULoad = 100 - (100 * t_idle_all) / t_total_all;
287 vm_deallocate (mach_task_self (),
288 (vm_address_t) cpu_load,
289 (vm_size_t) (cpu_msg_count * sizeof (*cpu_load)));
290 currentIOLoad = -1; /* FIXME-OSX! */
295 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "host_processor_info failed.");
296 return GNUNET_SYSERR;
300 /* try kstat (Solaris only) */
301 #if SOLARIS && HAVE_KSTAT_H && HAVE_SYS_SYSINFO_H
303 static long long last_idlecount;
304 static long long last_totalcount;
305 static int kstat_once; /* if open fails, don't keep
310 long long totalcount;
312 long long deltatotal;
319 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kstat_close");
325 for (khelper = kc->kc_chain; khelper != NULL; khelper = khelper->ks_next)
329 if (0 != strncmp (khelper->ks_name, "cpu_stat", strlen ("cpu_stat")))
331 if (khelper->ks_data_size > sizeof (cpu_stat_t))
332 continue; /* better save then sorry! */
333 if (-1 != kstat_read (kc, khelper, &stats))
335 idlecount += stats.cpu_sysinfo.cpu[CPU_IDLE];
337 += stats.cpu_sysinfo.cpu[CPU_IDLE] +
338 stats.cpu_sysinfo.cpu[CPU_USER] +
339 stats.cpu_sysinfo.cpu[CPU_KERNEL] +
340 stats.cpu_sysinfo.cpu[CPU_WAIT];
343 if (0 != kstat_close (kc))
344 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kstat_close");
345 if ((idlecount == 0) && (totalcount == 0))
346 goto ABORT_KSTAT; /* no stats found => abort */
347 deltaidle = idlecount - last_idlecount;
348 deltatotal = totalcount - last_totalcount;
349 if ((deltatotal > 0) && (last_totalcount > 0))
351 currentCPULoad = (unsigned int) (100.0 * deltaidle / deltatotal);
352 if (currentCPULoad > 100)
353 currentCPULoad = 100; /* odd */
354 if (currentCPULoad < 0)
355 currentCPULoad = 0; /* odd */
356 currentCPULoad = 100 - currentCPULoad; /* computed idle-load before! */
360 currentIOLoad = -1; /* FIXME-SOLARIS! */
361 last_idlecount = idlecount;
362 last_totalcount = totalcount;
365 kstat_once = 1; /* failed, don't try again */
366 return GNUNET_SYSERR;
370 /* insert methods better than getloadavg for
371 other platforms HERE! */
373 /* ok, maybe we have getloadavg on this platform */
376 static int warnOnce = 0;
378 if (1 != getloadavg (&loadavg, 1))
380 /* only warn once, if there is a problem with
381 getloadavg, we're going to hit it frequently... */
385 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "getloadavg");
387 return GNUNET_SYSERR;
391 /* success with getloadavg */
392 currentCPULoad = (int) (100 * loadavg);
393 currentIOLoad = -1; /* FIXME */
401 if (GNNtQuerySystemInformation)
403 static double dLastKernel;
404 static double dLastIdle;
405 static double dLastUser;
412 SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION theInfo;
414 if (GNNtQuerySystemInformation (SystemProcessorPerformanceInformation,
416 sizeof (theInfo), NULL) == NO_ERROR)
418 /* PORT-ME MINGW: Multi-processor? */
419 dKernel = Li2Double (theInfo.KernelTime);
420 dIdle = Li2Double (theInfo.IdleTime);
421 dUser = Li2Double (theInfo.UserTime);
422 dDiffKernel = dKernel - dLastKernel;
423 dDiffIdle = dIdle - dLastIdle;
424 dDiffUser = dUser - dLastUser;
426 if (((dDiffKernel + dDiffUser) > 0) &&
427 (dLastIdle + dLastKernel + dLastUser > 0))
429 100.0 - (dDiffIdle / (dDiffKernel + dDiffUser)) * 100.0;
431 currentCPULoad = -1; /* don't know (yet) */
433 dLastKernel = dKernel;
437 currentIOLoad = -1; /* FIXME-MINGW */
442 /* only warn once, if there is a problem with
443 NtQuery..., we're going to hit it frequently... */
448 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
449 "Cannot query the CPU usage (Windows NT).\n");
451 return GNUNET_SYSERR;
457 DWORD dwDataSize, dwType, dwDummy;
460 if (RegOpenKeyEx (HKEY_DYN_DATA,
461 "PerfStats\\StartSrv",
462 0, KEY_ALL_ACCESS, &hKey) != ERROR_SUCCESS)
469 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
470 "Cannot query the CPU usage (Win 9x)\n");
474 RegOpenKeyEx (HKEY_DYN_DATA,
475 "PerfStats\\StartStat", 0, KEY_ALL_ACCESS, &hKey);
476 dwDataSize = sizeof (dwDummy);
477 RegQueryValueEx (hKey,
479 NULL, &dwType, (LPBYTE) & dwDummy, &dwDataSize);
483 RegOpenKeyEx (HKEY_DYN_DATA,
484 "PerfStats\\StatData", 0, KEY_ALL_ACCESS, &hKey);
485 dwDataSize = sizeof (currentCPULoad);
486 RegQueryValueEx (hKey,
488 NULL, &dwType, (LPBYTE) & currentCPULoad, &dwDataSize);
490 currentIOLoad = -1; /* FIXME-MINGW! */
493 RegOpenKeyEx (HKEY_DYN_DATA,
494 "PerfStats\\StopStat", 0, KEY_ALL_ACCESS, &hKey);
495 RegOpenKeyEx (HKEY_DYN_DATA,
496 "PerfStats\\StopSrv", 0, KEY_ALL_ACCESS, &hKey);
497 dwDataSize = sizeof (dwDummy);
498 RegQueryValueEx (hKey,
500 NULL, &dwType, (LPBYTE) & dwDummy, &dwDataSize);
507 /* loadaverage not defined and no platform
508 specific alternative defined
511 return GNUNET_SYSERR;
516 * Update load values (if enough time has expired),
517 * including computation of averages. Code assumes
518 * that lock has already been obtained.
523 static struct GNUNET_TIME_Absolute lastCall;
524 struct GNUNET_TIME_Relative age;
526 age = GNUNET_TIME_absolute_get_duration (lastCall);
527 if ( (agedCPULoad == -1)
528 || (age.rel_value_us > 500000) )
530 /* use smoothing, but do NOT update lastRet at frequencies higher
531 than 500ms; this makes the smoothing (mostly) independent from
532 the frequency at which getCPULoad is called (and we don't spend
533 more time measuring CPU than actually computing something). */
534 lastCall = GNUNET_TIME_absolute_get ();
536 if (currentCPULoad == -1)
542 if (agedCPULoad == -1)
544 agedCPULoad = currentCPULoad;
548 /* for CPU, we don't do the 'fast increase' since CPU is much
549 more jitterish to begin with */
550 agedCPULoad = (agedCPULoad * 31 + currentCPULoad) / 32;
553 if (currentIOLoad == -1)
559 if (agedIOLoad == -1)
561 agedIOLoad = currentIOLoad;
565 /* for IO, we don't do the 'fast increase' since IO is much
566 more jitterish to begin with */
567 agedIOLoad = (agedIOLoad * 31 + currentIOLoad) / 32;
574 * Get the load of the CPU relative to what is allowed.
575 * @return the CPU load as a percentage of allowed
576 * (100 is equivalent to full load)
582 return (int) agedCPULoad;
587 * Get the load of the CPU relative to what is allowed.
588 * @return the CPU load as a percentage of allowed
589 * (100 is equivalent to full load)
595 return (int) agedIOLoad;
599 * Get the percentage of memory used
601 * @return the percentage of memory used
609 percentage = ( ((double) kb_main_used) / ((double) kb_main_total) * 100.0 );
610 return (unsigned int) percentage;
617 * Returns the number of processes
619 * @return the number of processes
628 dir = opendir ("/proc");
632 while (NULL != (ent = readdir (dir)))
634 if((*ent->d_name > '0') && (*ent->d_name <= '9'))
644 sample_load_task (void *cls)
646 struct GNUNET_TIME_Absolute now;
651 unsigned int mem_usage;
654 sample_load_task_id = NULL;
655 ld_cpu = cpu_get_load ();
656 ld_disk = disk_get_load ();
657 if ( (-1 == ld_cpu) || (-1 == ld_disk) )
659 mem_usage = mem_get_usage ();
661 nproc = get_nproc ();
665 now = GNUNET_TIME_absolute_get ();
666 nbs = GNUNET_asprintf (&str, "%llu %d %d %u %u\n", now.abs_value_us / 1000LL / 1000LL,
667 ld_cpu, ld_disk, mem_usage, nproc);
670 GNUNET_BIO_write (bw, str, nbs);
677 sample_load_task_id =
678 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
679 &sample_load_task, NULL);
684 * Initialize logging CPU and IO statisticfs. Checks the configuration for
685 * "STATS_DIR" and logs to a file in that directory. The file is name is
686 * generated from the hostname and the process's PID.
689 GST_stats_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
697 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
698 "Load statistics logging now available for windows\n");
699 return; /* No logging on windows for now :( */
703 GNUNET_CONFIGURATION_get_value_filename (cfg, "testbed",
704 "STATS_DIR", &stats_dir))
706 len = GNUNET_OS_get_hostname_max_length ();
707 hostname = GNUNET_malloc (len);
708 if (0 != gethostname (hostname, len))
710 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "gethostname");
711 GNUNET_free (stats_dir);
712 GNUNET_free (hostname);
716 (void) GNUNET_asprintf (&fn, "%s/%.*s-%jd.dat", stats_dir, len,
717 hostname, (intmax_t) getpid());
718 GNUNET_free (stats_dir);
719 GNUNET_free (hostname);
720 if (NULL == (bw = GNUNET_BIO_write_open (fn)))
722 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
723 _("Cannot open %s for writing load statistics. "
724 "Not logging load statistics\n"), fn);
729 sample_load_task_id = GNUNET_SCHEDULER_add_now (&sample_load_task, NULL);
731 proc_stat = fopen ("/proc/stat", "r");
732 if (NULL == proc_stat)
733 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
734 "fopen", "/proc/stat");
738 updateUsage (); /* initialize */
744 * Shutdown the status calls module.
755 if (proc_stat != NULL)
761 GNUNET_free_non_null (prev_cpu_load);
763 if (NULL != sample_load_task_id)
765 GNUNET_SCHEDULER_cancel (sample_load_task_id);
766 sample_load_task_id = NULL;
768 GNUNET_break (GNUNET_OK == GNUNET_BIO_write_close (bw));
772 /* end of cpustatus.c */