diff options
author | Sree Harsha Totakura <totakura@in.tum.de> | 2013-04-04 13:29:32 +0000 |
---|---|---|
committer | Sree Harsha Totakura <totakura@in.tum.de> | 2013-04-04 13:29:32 +0000 |
commit | ff1c4e766d43c2343a7204918840b953329787d7 (patch) | |
tree | f3de96b00dcd8d07c46ed4ede0bd750e7a8f4810 /src/testbed/gnunet-service-testbed_cpustatus.c | |
parent | 84d17ce6a280971018ca01dbd71c1cd0ae0ea53b (diff) | |
download | gnunet-ff1c4e766d43c2343a7204918840b953329787d7.tar.gz gnunet-ff1c4e766d43c2343a7204918840b953329787d7.zip |
- log load statistics to file
Diffstat (limited to 'src/testbed/gnunet-service-testbed_cpustatus.c')
-rw-r--r-- | src/testbed/gnunet-service-testbed_cpustatus.c | 126 |
1 files changed, 95 insertions, 31 deletions
diff --git a/src/testbed/gnunet-service-testbed_cpustatus.c b/src/testbed/gnunet-service-testbed_cpustatus.c index 7692a1389..bb187e4d3 100644 --- a/src/testbed/gnunet-service-testbed_cpustatus.c +++ b/src/testbed/gnunet-service-testbed_cpustatus.c | |||
@@ -75,6 +75,15 @@ static int currentIOLoad; | |||
75 | 75 | ||
76 | static double agedIOLoad = -1; | 76 | static double agedIOLoad = -1; |
77 | 77 | ||
78 | |||
79 | /** | ||
80 | * hanlde to the file to write the load statistics to | ||
81 | */ | ||
82 | struct GNUNET_BIO_WriteHandle *bw; | ||
83 | |||
84 | GNUNET_SCHEDULER_TaskIdentifier sample_load_task_id; | ||
85 | |||
86 | |||
78 | #ifdef OSX | 87 | #ifdef OSX |
79 | static int | 88 | static int |
80 | initMachCpuStats () | 89 | initMachCpuStats () |
@@ -92,9 +101,7 @@ initMachCpuStats () | |||
92 | &cpu_msg_count); | 101 | &cpu_msg_count); |
93 | if (kret != KERN_SUCCESS) | 102 | if (kret != KERN_SUCCESS) |
94 | { | 103 | { |
95 | GNUNET_GE_LOG (NULL, | 104 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "host_processor_info failed."); |
96 | GNUNET_GE_ERROR | GNUNET_GE_USER | GNUNET_GE_ADMIN | | ||
97 | GNUNET_GE_BULK, "host_processor_info failed."); | ||
98 | return GNUNET_SYSERR; | 105 | return GNUNET_SYSERR; |
99 | } | 106 | } |
100 | prev_cpu_load = GNUNET_malloc (cpu_count * sizeof (*prev_cpu_load)); | 107 | prev_cpu_load = GNUNET_malloc (cpu_count * sizeof (*prev_cpu_load)); |
@@ -286,9 +293,7 @@ updateUsage () | |||
286 | } | 293 | } |
287 | else | 294 | else |
288 | { | 295 | { |
289 | GNUNET_GE_LOG (NULL, | 296 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "host_processor_info failed."); |
290 | GNUNET_GE_ERROR | GNUNET_GE_USER | GNUNET_GE_ADMIN | | ||
291 | GNUNET_GE_BULK, "host_processor_info failed."); | ||
292 | return GNUNET_SYSERR; | 297 | return GNUNET_SYSERR; |
293 | } | 298 | } |
294 | } | 299 | } |
@@ -312,10 +317,7 @@ updateUsage () | |||
312 | kc = kstat_open (); | 317 | kc = kstat_open (); |
313 | if (kc == NULL) | 318 | if (kc == NULL) |
314 | { | 319 | { |
315 | GNUNET_GE_LOG_STRERROR (NULL, | 320 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kstat_close"); |
316 | GNUNET_GE_ERROR | GNUNET_GE_USER | | ||
317 | GNUNET_GE_ADMIN | GNUNET_GE_BULK, | ||
318 | "kstat_open"); | ||
319 | goto ABORT_KSTAT; | 321 | goto ABORT_KSTAT; |
320 | } | 322 | } |
321 | 323 | ||
@@ -340,9 +342,7 @@ updateUsage () | |||
340 | } | 342 | } |
341 | } | 343 | } |
342 | if (0 != kstat_close (kc)) | 344 | if (0 != kstat_close (kc)) |
343 | GNUNET_GE_LOG_STRERROR (NULL, | 345 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kstat_close"); |
344 | GNUNET_GE_ERROR | GNUNET_GE_ADMIN | | ||
345 | GNUNET_GE_USER | GNUNET_GE_BULK, "kstat_close"); | ||
346 | if ((idlecount == 0) && (totalcount == 0)) | 346 | if ((idlecount == 0) && (totalcount == 0)) |
347 | goto ABORT_KSTAT; /* no stats found => abort */ | 347 | goto ABORT_KSTAT; /* no stats found => abort */ |
348 | deltaidle = idlecount - last_idlecount; | 348 | deltaidle = idlecount - last_idlecount; |
@@ -446,10 +446,8 @@ updateUsage () | |||
446 | if (once == 0) | 446 | if (once == 0) |
447 | { | 447 | { |
448 | once = 1; | 448 | once = 1; |
449 | GNUNET_GE_LOG (NULL, | 449 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
450 | GNUNET_GE_ERROR | GNUNET_GE_USER | | 450 | "Cannot query the CPU usage (Windows NT).\n"); |
451 | GNUNET_GE_ADMIN | GNUNET_GE_BULK, | ||
452 | _("Cannot query the CPU usage (Windows NT).\n")); | ||
453 | } | 451 | } |
454 | return GNUNET_SYSERR; | 452 | return GNUNET_SYSERR; |
455 | } | 453 | } |
@@ -469,10 +467,8 @@ updateUsage () | |||
469 | if (once == 0) | 467 | if (once == 0) |
470 | { | 468 | { |
471 | once = 1; | 469 | once = 1; |
472 | GNUNET_GE_LOG (NULL, | 470 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
473 | GNUNET_GE_USER | GNUNET_GE_ADMIN | | 471 | "Cannot query the CPU usage (Win 9x)\n"); |
474 | GNUNET_GE_ERROR | GNUNET_GE_BULK, | ||
475 | _("Cannot query the CPU usage (Win 9x)\n")); | ||
476 | } | 472 | } |
477 | } | 473 | } |
478 | 474 | ||
@@ -580,8 +576,8 @@ updateAgedLoad () | |||
580 | * @return the CPU load as a percentage of allowed | 576 | * @return the CPU load as a percentage of allowed |
581 | * (100 is equivalent to full load) | 577 | * (100 is equivalent to full load) |
582 | */ | 578 | */ |
583 | int | 579 | static int |
584 | GST_cpu_get_load () | 580 | cpu_get_load () |
585 | { | 581 | { |
586 | updateAgedLoad (); | 582 | updateAgedLoad (); |
587 | return (int) agedCPULoad; | 583 | return (int) agedCPULoad; |
@@ -593,21 +589,85 @@ GST_cpu_get_load () | |||
593 | * @return the CPU load as a percentage of allowed | 589 | * @return the CPU load as a percentage of allowed |
594 | * (100 is equivalent to full load) | 590 | * (100 is equivalent to full load) |
595 | */ | 591 | */ |
596 | int | 592 | static int |
597 | GST_disk_get_load () | 593 | disk_get_load () |
598 | { | 594 | { |
599 | updateAgedLoad (); | 595 | updateAgedLoad (); |
600 | return (int) agedIOLoad; | 596 | return (int) agedIOLoad; |
601 | } | 597 | } |
602 | 598 | ||
599 | |||
600 | static void | ||
601 | sample_load_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
602 | { | ||
603 | struct GNUNET_TIME_Absolute now; | ||
604 | char *str; | ||
605 | int nbs; | ||
606 | int ld_cpu; | ||
607 | int ld_disk; | ||
608 | |||
609 | sample_load_task_id = GNUNET_SCHEDULER_NO_TASK; | ||
610 | if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason)) | ||
611 | return; | ||
612 | ld_cpu = cpu_get_load (); | ||
613 | ld_disk = disk_get_load (); | ||
614 | if ( (-1 == ld_cpu) || (-1 == ld_disk) ) | ||
615 | goto reschedule; | ||
616 | now = GNUNET_TIME_absolute_get (); | ||
617 | nbs = GNUNET_asprintf (&str, "%llu %d %d\n", now.abs_value / 1000, | ||
618 | cpu_get_load (), disk_get_load ()); | ||
619 | if (0 < nbs) | ||
620 | { | ||
621 | GNUNET_BIO_write (bw, str, nbs); | ||
622 | GNUNET_free (str); | ||
623 | } | ||
624 | else | ||
625 | GNUNET_break (0); | ||
626 | |||
627 | reschedule: | ||
628 | sample_load_task_id = | ||
629 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, | ||
630 | &sample_load_task, NULL); | ||
631 | } | ||
632 | |||
633 | |||
603 | /** | 634 | /** |
604 | * The following method is called in order to initialize the status calls | 635 | * Initialize logging CPU and IO statisticfs. Checks the configuration for |
605 | * routines. After that it is safe to call each of the status calls separately | 636 | * "STATS_DIR" and logs to a file in that directory. The file is name is |
606 | * @return GNUNET_OK on success and GNUNET_SYSERR on error (or calls errexit). | 637 | * generated from the hostname and the process's PID. |
607 | */ | 638 | */ |
608 | void | 639 | void |
609 | GST_stats_init () | 640 | GST_stats_init (const struct GNUNET_CONFIGURATION_Handle *cfg) |
610 | { | 641 | { |
642 | char *hostname; | ||
643 | char *stats_dir; | ||
644 | char *fn; | ||
645 | |||
646 | if (GNUNET_OK != | ||
647 | GNUNET_CONFIGURATION_get_value_string (cfg, "testbed", | ||
648 | "STATS_DIR", &stats_dir)) | ||
649 | return; | ||
650 | hostname = GNUNET_malloc (GNUNET_OS_get_hostname_max_length ()); | ||
651 | if (0 != gethostname (hostname, GNUNET_OS_get_hostname_max_length ())) | ||
652 | { | ||
653 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "gethostname"); | ||
654 | GNUNET_free (stats_dir); | ||
655 | return; | ||
656 | } | ||
657 | fn = NULL; | ||
658 | (void) GNUNET_asprintf (&fn, "%s/%s-%jd.dat", stats_dir, | ||
659 | hostname, (intmax_t) getpid()); | ||
660 | GNUNET_free (stats_dir); | ||
661 | if (NULL == (bw = GNUNET_BIO_write_open (fn))) | ||
662 | { | ||
663 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
664 | _("Cannot open %s for writing load statistics. " | ||
665 | "Not logging load statistics\n"), fn); | ||
666 | GNUNET_free (fn); | ||
667 | return; | ||
668 | } | ||
669 | GNUNET_free (fn); | ||
670 | sample_load_task_id = GNUNET_SCHEDULER_add_now (&sample_load_task, NULL); | ||
611 | #ifdef LINUX | 671 | #ifdef LINUX |
612 | proc_stat = fopen ("/proc/stat", "r"); | 672 | proc_stat = fopen ("/proc/stat", "r"); |
613 | if (NULL == proc_stat) | 673 | if (NULL == proc_stat) |
@@ -619,14 +679,18 @@ GST_stats_init () | |||
619 | InitWinEnv (NULL); | 679 | InitWinEnv (NULL); |
620 | #endif | 680 | #endif |
621 | updateUsage (); /* initialize */ | 681 | updateUsage (); /* initialize */ |
682 | |||
622 | } | 683 | } |
623 | 684 | ||
685 | |||
624 | /** | 686 | /** |
625 | * Shutdown the status calls module. | 687 | * Shutdown the status calls module. |
626 | */ | 688 | */ |
627 | void | 689 | void |
628 | GST_stats_destroy () | 690 | GST_stats_destroy () |
629 | { | 691 | { |
692 | if (NULL == bw) | ||
693 | return; | ||
630 | #ifdef LINUX | 694 | #ifdef LINUX |
631 | if (proc_stat != NULL) | 695 | if (proc_stat != NULL) |
632 | { | 696 | { |
@@ -638,8 +702,8 @@ GST_stats_destroy () | |||
638 | #elif MINGW | 702 | #elif MINGW |
639 | ShutdownWinEnv (); | 703 | ShutdownWinEnv (); |
640 | #endif | 704 | #endif |
705 | GNUNET_break (GNUNET_OK == GNUNET_BIO_write_close (bw)); | ||
706 | bw = NULL; | ||
641 | } | 707 | } |
642 | 708 | ||
643 | char *GST_stats_dir; | ||
644 | |||
645 | /* end of cpustatus.c */ | 709 | /* end of cpustatus.c */ |