GNUnet  0.10.x
Macros | Functions | Variables
gnunet-service-testbed_cpustatus.c File Reference

calls to determine current CPU load More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet-service-testbed_meminfo.h"
Include dependency graph for gnunet-service-testbed_cpustatus.c:

Go to the source code of this file.

Macros

#define DEBUG_STATUSCALLS   GNUNET_NO
 

Functions

static int updateUsage ()
 Update the currentCPU and currentIO load (and on Linux, memory) values. More...
 
static void updateAgedLoad ()
 Update load values (if enough time has expired), including computation of averages. More...
 
static int cpu_get_load ()
 Get the load of the CPU relative to what is allowed. More...
 
static int disk_get_load ()
 Get the load of the CPU relative to what is allowed. More...
 
static unsigned int mem_get_usage ()
 Get the percentage of memory used. More...
 
static void sample_load_task (void *cls)
 
void GST_stats_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
 Initialize logging CPU and IO statisticfs. More...
 
void GST_stats_destroy ()
 Shutdown the status calls module. More...
 

Variables

static int currentCPULoad
 Current CPU load, as percentage of CPU cycles not idle or blocked on IO. More...
 
static double agedCPULoad = -1
 
static int currentIOLoad
 Current IO load, as percentage of CPU cycles blocked on IO. More...
 
static double agedIOLoad = -1
 
struct GNUNET_BIO_WriteHandlebw
 hanlde to the file to write the load statistics to More...
 
struct GNUNET_SCHEDULER_Tasksample_load_task_id
 

Detailed Description

calls to determine current CPU load

Author
Tzvetan Horozov
Christian Grothoff
Igor Wronsky
Alex Harper (OS X portion)
Sree Harsha Totakura

Definition in file gnunet-service-testbed_cpustatus.c.

Macro Definition Documentation

◆ DEBUG_STATUSCALLS

#define DEBUG_STATUSCALLS   GNUNET_NO

Definition at line 61 of file gnunet-service-testbed_cpustatus.c.

Function Documentation

◆ updateUsage()

static int updateUsage ( )
static

Update the currentCPU and currentIO load (and on Linux, memory) values.

Before its first invocation the method initStatusCalls() must be called. If there is an error the method returns -1.

Definition at line 133 of file gnunet-service-testbed_cpustatus.c.

References currentCPULoad, currentIOLoad, GNNtQuerySystemInformation, GNUNET_ERROR_TYPE_WARNING, GNUNET_log, GNUNET_log_strerror, GNUNET_log_strerror_file, GNUNET_NO, GNUNET_OK, GNUNET_SYSERR, GNUNET_YES, line, once, ret, and stats.

Referenced by GST_stats_init(), and updateAgedLoad().

134 {
135  currentIOLoad = -1;
136  currentCPULoad = -1;
137 #ifdef LINUX
138  /* under linux, first try %idle/usage using /proc/stat;
139  if that does not work, disable /proc/stat for the future
140  by closing the file and use the next-best method. */
141  if (proc_stat != NULL)
142  {
143  static unsigned long long last_cpu_results[5] = { 0, 0, 0, 0, 0 };
144  static int have_last_cpu = GNUNET_NO;
145  int ret;
146  char line[256];
147  unsigned long long user_read, system_read, nice_read, idle_read,
148  iowait_read;
149  unsigned long long user, system, nice, idle, iowait;
150  unsigned long long usage_time = 0, total_time = 1;
151 
152  /* Get the first line with the data */
153  rewind (proc_stat);
154  fflush (proc_stat);
155  if (NULL == fgets (line, 256, proc_stat))
156  {
158  "fgets", "/proc/stat");
159  proc_stat = NULL; /* don't try again */
160  }
161  else
162  {
163  iowait_read = 0;
164  ret = sscanf (line, "%*s %llu %llu %llu %llu %llu",
165  &user_read,
166  &system_read, &nice_read, &idle_read, &iowait_read);
167  if (ret < 4)
168  {
170  "fgets-sscanf", "/proc/stat");
171  fclose (proc_stat);
172  proc_stat = NULL; /* don't try again */
173  have_last_cpu = GNUNET_NO;
174  }
175  else
176  {
177  /* Store the current usage */
178  user = user_read - last_cpu_results[0];
179  system = system_read - last_cpu_results[1];
180  nice = nice_read - last_cpu_results[2];
181  idle = idle_read - last_cpu_results[3];
182  iowait = iowait_read - last_cpu_results[4];
183  /* Calculate the % usage */
184  usage_time = user + system + nice;
185  total_time = usage_time + idle + iowait;
186  if ((total_time > 0) && (have_last_cpu == GNUNET_YES))
187  {
188  currentCPULoad = (int) (100L * usage_time / total_time);
189  if (ret > 4)
190  currentIOLoad = (int) (100L * iowait / total_time);
191  else
192  currentIOLoad = -1; /* 2.4 kernel */
193  }
194  /* Store the values for the next calculation */
195  last_cpu_results[0] = user_read;
196  last_cpu_results[1] = system_read;
197  last_cpu_results[2] = nice_read;
198  last_cpu_results[3] = idle_read;
199  last_cpu_results[4] = iowait_read;
200  have_last_cpu = GNUNET_YES;
201  return GNUNET_OK;
202  }
203  }
204  }
205 #endif
206 
207 #ifdef OSX
208  {
209  unsigned int cpu_count;
210  processor_cpu_load_info_t cpu_load;
211  mach_msg_type_number_t cpu_msg_count;
212  unsigned long long t_sys, t_user, t_nice, t_idle, t_total;
213  unsigned long long t_idle_all, t_total_all;
214  kern_return_t kret;
215  int i, j;
216 
217  t_idle_all = t_total_all = 0;
218  kret = host_processor_info (mach_host_self (), PROCESSOR_CPU_LOAD_INFO,
219  &cpu_count,
220  (processor_info_array_t *) & cpu_load,
221  &cpu_msg_count);
222  if (kret == KERN_SUCCESS)
223  {
224  for (i = 0; i < cpu_count; i++)
225  {
226  if (cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM] >=
227  prev_cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM])
228  {
229  t_sys = cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM] -
230  prev_cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM];
231  }
232  else
233  {
234  t_sys = cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM] +
235  (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM] +
236  1);
237  }
238 
239  if (cpu_load[i].cpu_ticks[CPU_STATE_USER] >=
240  prev_cpu_load[i].cpu_ticks[CPU_STATE_USER])
241  {
242  t_user = cpu_load[i].cpu_ticks[CPU_STATE_USER] -
243  prev_cpu_load[i].cpu_ticks[CPU_STATE_USER];
244  }
245  else
246  {
247  t_user = cpu_load[i].cpu_ticks[CPU_STATE_USER] +
248  (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_USER] +
249  1);
250  }
251 
252  if (cpu_load[i].cpu_ticks[CPU_STATE_NICE] >=
253  prev_cpu_load[i].cpu_ticks[CPU_STATE_NICE])
254  {
255  t_nice = cpu_load[i].cpu_ticks[CPU_STATE_NICE] -
256  prev_cpu_load[i].cpu_ticks[CPU_STATE_NICE];
257  }
258  else
259  {
260  t_nice = cpu_load[i].cpu_ticks[CPU_STATE_NICE] +
261  (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_NICE] +
262  1);
263  }
264 
265  if (cpu_load[i].cpu_ticks[CPU_STATE_IDLE] >=
266  prev_cpu_load[i].cpu_ticks[CPU_STATE_IDLE])
267  {
268  t_idle = cpu_load[i].cpu_ticks[CPU_STATE_IDLE] -
269  prev_cpu_load[i].cpu_ticks[CPU_STATE_IDLE];
270  }
271  else
272  {
273  t_idle = cpu_load[i].cpu_ticks[CPU_STATE_IDLE] +
274  (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_IDLE] +
275  1);
276  }
277  t_total = t_sys + t_user + t_nice + t_idle;
278  t_idle_all += t_idle;
279  t_total_all += t_total;
280  }
281  for (i = 0; i < cpu_count; i++)
282  {
283  for (j = 0; j < CPU_STATE_MAX; j++)
284  {
285  prev_cpu_load[i].cpu_ticks[j] = cpu_load[i].cpu_ticks[j];
286  }
287  }
288  if (t_total_all > 0)
289  currentCPULoad = 100 - (100 * t_idle_all) / t_total_all;
290  else
291  currentCPULoad = -1;
292  vm_deallocate (mach_task_self (),
293  (vm_address_t) cpu_load,
294  (vm_size_t) (cpu_msg_count * sizeof (*cpu_load)));
295  currentIOLoad = -1; /* FIXME-OSX! */
296  return GNUNET_OK;
297  }
298  else
299  {
300  GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "host_processor_info failed.");
301  return GNUNET_SYSERR;
302  }
303  }
304 #endif
305  /* try kstat (Solaris only) */
306 #if SOLARIS && HAVE_KSTAT_H && HAVE_SYS_SYSINFO_H
307  {
308  static long long last_idlecount;
309  static long long last_totalcount;
310  static int kstat_once; /* if open fails, don't keep
311  trying */
312  kstat_ctl_t *kc;
313  kstat_t *khelper;
314  long long idlecount;
315  long long totalcount;
316  long long deltaidle;
317  long long deltatotal;
318 
319  if (kstat_once == 1)
320  goto ABORT_KSTAT;
321  kc = kstat_open ();
322  if (kc == NULL)
323  {
325  goto ABORT_KSTAT;
326  }
327 
328  idlecount = 0;
329  totalcount = 0;
330  for (khelper = kc->kc_chain; khelper != NULL; khelper = khelper->ks_next)
331  {
332  cpu_stat_t stats;
333 
334  if (0 != strncmp (khelper->ks_name, "cpu_stat", strlen ("cpu_stat")))
335  continue;
336  if (khelper->ks_data_size > sizeof (cpu_stat_t))
337  continue; /* better save then sorry! */
338  if (-1 != kstat_read (kc, khelper, &stats))
339  {
340  idlecount += stats.cpu_sysinfo.cpu[CPU_IDLE];
341  totalcount
342  += stats.cpu_sysinfo.cpu[CPU_IDLE] +
343  stats.cpu_sysinfo.cpu[CPU_USER] +
344  stats.cpu_sysinfo.cpu[CPU_KERNEL] +
345  stats.cpu_sysinfo.cpu[CPU_WAIT];
346  }
347  }
348  if (0 != kstat_close (kc))
350  if ((idlecount == 0) && (totalcount == 0))
351  goto ABORT_KSTAT; /* no stats found => abort */
352  deltaidle = idlecount - last_idlecount;
353  deltatotal = totalcount - last_totalcount;
354  if ((deltatotal > 0) && (last_totalcount > 0))
355  {
356  currentCPULoad = (unsigned int) (100.0 * deltaidle / deltatotal);
357  if (currentCPULoad > 100)
358  currentCPULoad = 100; /* odd */
359  if (currentCPULoad < 0)
360  currentCPULoad = 0; /* odd */
361  currentCPULoad = 100 - currentCPULoad; /* computed idle-load before! */
362  }
363  else
364  currentCPULoad = -1;
365  currentIOLoad = -1; /* FIXME-SOLARIS! */
366  last_idlecount = idlecount;
367  last_totalcount = totalcount;
368  return GNUNET_OK;
369  ABORT_KSTAT:
370  kstat_once = 1; /* failed, don't try again */
371  return GNUNET_SYSERR;
372  }
373 #endif
374 
375  /* insert methods better than getloadavg for
376  other platforms HERE! */
377 
378  /* ok, maybe we have getloadavg on this platform */
379 #if HAVE_GETLOADAVG
380  {
381  static int warnOnce = 0;
382  double loadavg;
383  if (1 != getloadavg (&loadavg, 1))
384  {
385  /* only warn once, if there is a problem with
386  getloadavg, we're going to hit it frequently... */
387  if (warnOnce == 0)
388  {
389  warnOnce = 1;
391  }
392  return GNUNET_SYSERR;
393  }
394  else
395  {
396  /* success with getloadavg */
397  currentCPULoad = (int) (100 * loadavg);
398  currentIOLoad = -1; /* FIXME */
399  return GNUNET_OK;
400  }
401  }
402 #endif
403 
404 #if MINGW
405  /* Win NT? */
407  {
408  static double dLastKernel;
409  static double dLastIdle;
410  static double dLastUser;
411  double dKernel;
412  double dIdle;
413  double dUser;
414  double dDiffKernel;
415  double dDiffIdle;
416  double dDiffUser;
417  SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION theInfo;
418 
419  if (GNNtQuerySystemInformation (SystemProcessorPerformanceInformation,
420  &theInfo,
421  sizeof (theInfo), NULL) == NO_ERROR)
422  {
423  /* PORT-ME MINGW: Multi-processor? */
424  dKernel = Li2Double (theInfo.KernelTime);
425  dIdle = Li2Double (theInfo.IdleTime);
426  dUser = Li2Double (theInfo.UserTime);
427  dDiffKernel = dKernel - dLastKernel;
428  dDiffIdle = dIdle - dLastIdle;
429  dDiffUser = dUser - dLastUser;
430 
431  if (((dDiffKernel + dDiffUser) > 0) &&
432  (dLastIdle + dLastKernel + dLastUser > 0))
434  100.0 - (dDiffIdle / (dDiffKernel + dDiffUser)) * 100.0;
435  else
436  currentCPULoad = -1; /* don't know (yet) */
437 
438  dLastKernel = dKernel;
439  dLastIdle = dIdle;
440  dLastUser = dUser;
441 
442  currentIOLoad = -1; /* FIXME-MINGW */
443  return GNUNET_OK;
444  }
445  else
446  {
447  /* only warn once, if there is a problem with
448  NtQuery..., we're going to hit it frequently... */
449  static int once;
450  if (once == 0)
451  {
452  once = 1;
454  "Cannot query the CPU usage (Windows NT).\n");
455  }
456  return GNUNET_SYSERR;
457  }
458  }
459  else
460  { /* Win 9x */
461  HKEY hKey;
462  DWORD dwDataSize, dwType, dwDummy;
463 
464  /* Start query */
465  if (RegOpenKeyEx (HKEY_DYN_DATA,
466  "PerfStats\\StartSrv",
467  0, KEY_ALL_ACCESS, &hKey) != ERROR_SUCCESS)
468  {
469  /* only warn once */
470  static int once = 0;
471  if (once == 0)
472  {
473  once = 1;
475  "Cannot query the CPU usage (Win 9x)\n");
476  }
477  }
478 
479  RegOpenKeyEx (HKEY_DYN_DATA,
480  "PerfStats\\StartStat", 0, KEY_ALL_ACCESS, &hKey);
481  dwDataSize = sizeof (dwDummy);
482  RegQueryValueEx (hKey,
483  "KERNEL\\CPUUsage",
484  NULL, &dwType, (LPBYTE) & dwDummy, &dwDataSize);
485  RegCloseKey (hKey);
486 
487  /* Get CPU usage */
488  RegOpenKeyEx (HKEY_DYN_DATA,
489  "PerfStats\\StatData", 0, KEY_ALL_ACCESS, &hKey);
490  dwDataSize = sizeof (currentCPULoad);
491  RegQueryValueEx (hKey,
492  "KERNEL\\CPUUsage",
493  NULL, &dwType, (LPBYTE) & currentCPULoad, &dwDataSize);
494  RegCloseKey (hKey);
495  currentIOLoad = -1; /* FIXME-MINGW! */
496 
497  /* Stop query */
498  RegOpenKeyEx (HKEY_DYN_DATA,
499  "PerfStats\\StopStat", 0, KEY_ALL_ACCESS, &hKey);
500  RegOpenKeyEx (HKEY_DYN_DATA,
501  "PerfStats\\StopSrv", 0, KEY_ALL_ACCESS, &hKey);
502  dwDataSize = sizeof (dwDummy);
503  RegQueryValueEx (hKey,
504  "KERNEL\\CPUUsage",
505  NULL, &dwType, (LPBYTE) & dwDummy, &dwDataSize);
506  RegCloseKey (hKey);
507 
508  return GNUNET_OK;
509  }
510 #endif
511 
512  /* loadaverage not defined and no platform
513  specific alternative defined
514  => default: error
515  */
516  return GNUNET_SYSERR;
517 }
static struct GNUNET_STATISTICS_Handle * stats
Handle for statistics.
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
static int ret
Final status code.
Definition: gnunet-arm.c:89
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
static char * line
Desired phone line (string to be converted to a hash).
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
static int currentIOLoad
Current IO load, as percentage of CPU cycles blocked on IO.
static int once
Global to mark if we&#39;ve run the initialization.
Definition: gnsrecord.c:68
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
static int currentCPULoad
Current CPU load, as percentage of CPU cycles not idle or blocked on IO.
TNtQuerySystemInformation GNNtQuerySystemInformation
#define GNUNET_log(kind,...)
#define GNUNET_YES
Definition: gnunet_common.h:80
Here is the caller graph for this function:

◆ updateAgedLoad()

static void updateAgedLoad ( )
static

Update load values (if enough time has expired), including computation of averages.

Code assumes that lock has already been obtained.

Definition at line 526 of file gnunet-service-testbed_cpustatus.c.

References agedCPULoad, agedIOLoad, currentCPULoad, currentIOLoad, GNUNET_TIME_absolute_get(), GNUNET_TIME_absolute_get_duration(), and updateUsage().

Referenced by cpu_get_load(), and disk_get_load().

527 {
528  static struct GNUNET_TIME_Absolute lastCall;
529  struct GNUNET_TIME_Relative age;
530 
531  age = GNUNET_TIME_absolute_get_duration (lastCall);
532  if ( (agedCPULoad == -1)
533  || (age.rel_value_us > 500000) )
534  {
535  /* use smoothing, but do NOT update lastRet at frequencies higher
536  than 500ms; this makes the smoothing (mostly) independent from
537  the frequency at which getCPULoad is called (and we don't spend
538  more time measuring CPU than actually computing something). */
539  lastCall = GNUNET_TIME_absolute_get ();
540  updateUsage ();
541  if (currentCPULoad == -1)
542  {
543  agedCPULoad = -1;
544  }
545  else
546  {
547  if (agedCPULoad == -1)
548  {
550  }
551  else
552  {
553  /* for CPU, we don't do the 'fast increase' since CPU is much
554  more jitterish to begin with */
555  agedCPULoad = (agedCPULoad * 31 + currentCPULoad) / 32;
556  }
557  }
558  if (currentIOLoad == -1)
559  {
560  agedIOLoad = -1;
561  }
562  else
563  {
564  if (agedIOLoad == -1)
565  {
567  }
568  else
569  {
570  /* for IO, we don't do the 'fast increase' since IO is much
571  more jitterish to begin with */
572  agedIOLoad = (agedIOLoad * 31 + currentIOLoad) / 32;
573  }
574  }
575  }
576 }
static double agedIOLoad
static int currentIOLoad
Current IO load, as percentage of CPU cycles blocked on IO.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:118
static int currentCPULoad
Current CPU load, as percentage of CPU cycles not idle or blocked on IO.
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_duration(struct GNUNET_TIME_Absolute whence)
Get the duration of an operation as the difference of the current time and the given start time "henc...
Definition: time.c:373
Time for absolute times used by GNUnet, in microseconds.
static int updateUsage()
Update the currentCPU and currentIO load (and on Linux, memory) values.
static double agedCPULoad
Time for relative time used by GNUnet, in microseconds.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ cpu_get_load()

static int cpu_get_load ( )
static

Get the load of the CPU relative to what is allowed.

Returns
the CPU load as a percentage of allowed (100 is equivalent to full load)

Definition at line 584 of file gnunet-service-testbed_cpustatus.c.

References agedCPULoad, and updateAgedLoad().

Referenced by sample_load_task().

585 {
586  updateAgedLoad ();
587  return (int) agedCPULoad;
588 }
static void updateAgedLoad()
Update load values (if enough time has expired), including computation of averages.
static double agedCPULoad
Here is the call graph for this function:
Here is the caller graph for this function:

◆ disk_get_load()

static int disk_get_load ( )
static

Get the load of the CPU relative to what is allowed.

Returns
the CPU load as a percentage of allowed (100 is equivalent to full load)

Definition at line 597 of file gnunet-service-testbed_cpustatus.c.

References agedIOLoad, and updateAgedLoad().

Referenced by sample_load_task().

598 {
599  updateAgedLoad ();
600  return (int) agedIOLoad;
601 }
static void updateAgedLoad()
Update load values (if enough time has expired), including computation of averages.
static double agedIOLoad
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mem_get_usage()

static unsigned int mem_get_usage ( )
static

Get the percentage of memory used.

Returns
the percentage of memory used

Definition at line 609 of file gnunet-service-testbed_cpustatus.c.

References dir, kb_main_total, kb_main_used, and meminfo().

Referenced by sample_load_task().

610 {
611  double percentage;
612 
613  meminfo ();
614  percentage = ( ((double) kb_main_used) / ((double) kb_main_total) * 100.0 );
615  return (unsigned int) percentage;
616 }
unsigned long kb_main_used
unsigned long kb_main_total
void meminfo(void)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sample_load_task()

static void sample_load_task ( void *  cls)
static

Definition at line 649 of file gnunet-service-testbed_cpustatus.c.

References GNUNET_TIME_Absolute::abs_value_us, cpu_get_load(), disk_get_load(), GNUNET_asprintf(), GNUNET_BIO_write(), GNUNET_break, GNUNET_free, GNUNET_SCHEDULER_add_delayed(), GNUNET_TIME_absolute_get(), GNUNET_TIME_UNIT_SECONDS, and mem_get_usage().

Referenced by GST_stats_init().

650 {
651  struct GNUNET_TIME_Absolute now;
652  char *str;
653  int nbs;
654  int ld_cpu;
655  int ld_disk;
656  unsigned int mem_usage;
657  unsigned int nproc;
658 
659  sample_load_task_id = NULL;
660  ld_cpu = cpu_get_load ();
661  ld_disk = disk_get_load ();
662  if ( (-1 == ld_cpu) || (-1 == ld_disk) )
663  goto reschedule;
664  mem_usage = mem_get_usage ();
665 #ifdef LINUX
666  nproc = get_nproc ();
667 #else
668  nproc = 0;
669 #endif
670  now = GNUNET_TIME_absolute_get ();
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);
673  if (0 < nbs)
674  {
675  GNUNET_BIO_write (bw, str, nbs);
676  }
677  else
678  GNUNET_break (0);
679  GNUNET_free (str);
680 
681  reschedule:
684  &sample_load_task, NULL);
685 }
static int cpu_get_load()
Get the load of the CPU relative to what is allowed.
static void sample_load_task(void *cls)
struct GNUNET_BIO_WriteHandle * bw
hanlde to the file to write the load statistics to
#define GNUNET_TIME_UNIT_SECONDS
One second.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1246
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:118
struct GNUNET_SCHEDULER_Task * sample_load_task_id
Time for absolute times used by GNUnet, in microseconds.
int GNUNET_BIO_write(struct GNUNET_BIO_WriteHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition: bio.c:505
static unsigned int mem_get_usage()
Get the percentage of memory used.
static int disk_get_load()
Get the load of the CPU relative to what is allowed.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GST_stats_init()

void GST_stats_init ( const struct GNUNET_CONFIGURATION_Handle cfg)

Initialize logging CPU and IO statisticfs.

Checks the configuration for "STATS_DIR" and logs to a file in that directory. The file is name is generated from the hostname and the process's PID.

Definition at line 694 of file gnunet-service-testbed_cpustatus.c.

References _, fn, GNUNET_asprintf(), GNUNET_BIO_write_open(), GNUNET_CONFIGURATION_get_value_filename(), GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log, GNUNET_log_strerror, GNUNET_log_strerror_file, GNUNET_malloc, GNUNET_OK, GNUNET_OS_get_hostname_max_length, GNUNET_SCHEDULER_add_now(), hostname, len, sample_load_task(), and updateUsage().

Referenced by testbed_run().

695 {
696  char *hostname;
697  char *stats_dir;
698  char *fn;
699  size_t len;
700 
701 #if MINGW
703  "Load statistics logging now available for windows\n");
704  return; /* No logging on windows for now :( */
705 #endif
706 
707  if (GNUNET_OK !=
709  "STATS_DIR", &stats_dir))
710  return;
712  hostname = GNUNET_malloc (len);
713  if (0 != gethostname (hostname, len))
714  {
716  GNUNET_free (stats_dir);
717  GNUNET_free (hostname);
718  return;
719  }
720  fn = NULL;
721  (void) GNUNET_asprintf (&fn, "%s/%.*s-%jd.dat", stats_dir, len,
722  hostname, (intmax_t) getpid());
723  GNUNET_free (stats_dir);
724  GNUNET_free (hostname);
725  if (NULL == (bw = GNUNET_BIO_write_open (fn)))
726  {
728  _("Cannot open %s for writing load statistics. "
729  "Not logging load statistics\n"), fn);
730  GNUNET_free (fn);
731  return;
732  }
733  GNUNET_free (fn);
735 #ifdef LINUX
736  proc_stat = fopen ("/proc/stat", "r");
737  if (NULL == proc_stat)
739  "fopen", "/proc/stat");
740 #elif OSX
741  initMachCpuStats ();
742 #endif
743  updateUsage (); /* initialize */
744 
745 }
static void sample_load_task(void *cls)
struct GNUNET_BIO_WriteHandle * bw
hanlde to the file to write the load statistics to
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
#define GNUNET_OS_get_hostname_max_length()
Get maximum string length returned by gethostname()
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
static char * fn
Filename of the unique file.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1273
struct GNUNET_SCHEDULER_Task * sample_load_task_id
static char * hostname
Our hostname; we give this to all the peers we start.
struct GNUNET_BIO_WriteHandle * GNUNET_BIO_write_open(const char *fn)
Open a file for writing.
Definition: bio.c:430
#define GNUNET_log(kind,...)
int GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
static int updateUsage()
Update the currentCPU and currentIO load (and on Linux, memory) values.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GST_stats_destroy()

void GST_stats_destroy ( void  )

Shutdown the status calls module.

Definition at line 752 of file gnunet-service-testbed_cpustatus.c.

References GNUNET_BIO_write_close(), GNUNET_break, GNUNET_free_non_null, GNUNET_OK, and GNUNET_SCHEDULER_cancel().

Referenced by shutdown_task().

753 {
754 #if MINGW
755  return;
756 #endif
757  if (NULL == bw)
758  return;
759 #ifdef LINUX
760  if (proc_stat != NULL)
761  {
762  fclose (proc_stat);
763  proc_stat = NULL;
764  }
765 #elif OSX
766  GNUNET_free_non_null (prev_cpu_load);
767 #endif
768  if (NULL != sample_load_task_id)
769  {
771  sample_load_task_id = NULL;
772  }
774  bw = NULL;
775 }
struct GNUNET_BIO_WriteHandle * bw
hanlde to the file to write the load statistics to
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
int GNUNET_BIO_write_close(struct GNUNET_BIO_WriteHandle *h)
Close an open file for writing.
Definition: bio.c:457
struct GNUNET_SCHEDULER_Task * sample_load_task_id
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:965
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ currentCPULoad

int currentCPULoad
static

Current CPU load, as percentage of CPU cycles not idle or blocked on IO.

Definition at line 71 of file gnunet-service-testbed_cpustatus.c.

Referenced by updateAgedLoad(), and updateUsage().

◆ agedCPULoad

double agedCPULoad = -1
static

Definition at line 73 of file gnunet-service-testbed_cpustatus.c.

Referenced by cpu_get_load(), and updateAgedLoad().

◆ currentIOLoad

int currentIOLoad
static

Current IO load, as percentage of CPU cycles blocked on IO.

Definition at line 78 of file gnunet-service-testbed_cpustatus.c.

Referenced by updateAgedLoad(), and updateUsage().

◆ agedIOLoad

double agedIOLoad = -1
static

Definition at line 80 of file gnunet-service-testbed_cpustatus.c.

Referenced by disk_get_load(), and updateAgedLoad().

◆ bw

hanlde to the file to write the load statistics to

Definition at line 86 of file gnunet-service-testbed_cpustatus.c.

Referenced by call_flush_completion().

◆ sample_load_task_id

struct GNUNET_SCHEDULER_Task* sample_load_task_id

Definition at line 88 of file gnunet-service-testbed_cpustatus.c.