X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Futil%2Fos_load.c;h=f7b5dff96c96dfd7dd3297ce2f12c53ba74b3d77;hb=0ea8e006d5f5ef84e31e000607bd24a23f8fc1ed;hp=21e74eb49d0c7aca097823de07806f01dc5101ca;hpb=7bc466bbdb8b64cac68c5ee59eb7ab6b9d85c420;p=oweals%2Fgnunet.git diff --git a/src/util/os_load.c b/src/util/os_load.c index 21e74eb49..f7b5dff96 100644 --- a/src/util/os_load.c +++ b/src/util/os_load.c @@ -19,7 +19,7 @@ */ /** - * @file util/os_load_cpu.c + * @file util/os_load.c * @brief calls to determine current CPU load * @author Tzvetan Horozov * @author Christian Grothoff @@ -49,7 +49,7 @@ #endif #endif -#ifdef OSX +#ifdef DARWIN #include static processor_cpu_load_info_t prev_cpu_load; @@ -58,7 +58,16 @@ static processor_cpu_load_info_t prev_cpu_load; #define DEBUG_STATUSCALLS GNUNET_NO #ifdef LINUX +/** + * File descriptor for reading /proc/stat (or NULL) + */ static FILE *proc_stat; + +/** + * Is this the first time we're trying to open /proc/stat? If + * not, we don't try again if we failed... + */ +static int first_time; #endif /** @@ -76,7 +85,7 @@ static int currentIOLoad; static double agedIOLoad = -1; -#ifdef OSX +#ifdef DARWIN static int initMachCpuStats () { @@ -112,6 +121,7 @@ initMachCpuStats () } #endif + /** * Update the currentCPU and currentIO load values. * @@ -127,6 +137,13 @@ updateUsage () /* under linux, first try %idle/usage using /proc/stat; if that does not work, disable /proc/stat for the future by closing the file and use the next-best method. */ + if (0 == first_time) + { + first_time = 1; + proc_stat = fopen ("/proc/stat", "r"); + if (NULL == proc_stat) + GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", "/proc/stat"); + } if (proc_stat != NULL) { static unsigned long long last_cpu_results[5] = { 0, 0, 0, 0, 0 }; @@ -196,7 +213,7 @@ updateUsage () } #endif -#ifdef OSX +#ifdef DARWIN { unsigned int cpu_count; processor_cpu_load_info_t cpu_load; @@ -284,7 +301,7 @@ updateUsage () vm_deallocate (mach_task_self (), (vm_address_t) cpu_load, (vm_size_t) (cpu_msg_count * sizeof (*cpu_load))); - currentIOLoad = -1; /* FIXME-OSX! */ + currentIOLoad = -1; /* There's no IO load meter on darwin */ return GNUNET_OK; } else @@ -586,6 +603,8 @@ GNUNET_OS_load_cpu_get (const struct GNUNET_CONFIGURATION_Handle *cfg) GNUNET_CONFIGURATION_get_value_number (cfg, "LOAD", "MAXCPULOAD", &maxCPULoad)) return GNUNET_SYSERR; + if (maxCPULoad == 0) + return 100; return (100 * ret) / maxCPULoad; } @@ -609,6 +628,8 @@ GNUNET_OS_load_disk_get (const struct GNUNET_CONFIGURATION_Handle *cfg) GNUNET_CONFIGURATION_get_value_number (cfg, "LOAD", "MAXIOLOAD", &maxIOLoad)) return GNUNET_SYSERR; + if (maxIOLoad == 0) + return 100; return (100 * ret) / maxIOLoad; } @@ -620,15 +641,24 @@ GNUNET_OS_load_disk_get (const struct GNUNET_CONFIGURATION_Handle *cfg) void __attribute__ ((constructor)) GNUNET_cpustats_ltdl_init () { #ifdef LINUX - proc_stat = fopen ("/proc/stat", "r"); - if (NULL == proc_stat) - GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", "/proc/stat"); -#elif OSX + updateUsage (); /* initialize */ + /* Most GNUnet processes don't really need this, so close the FD, but allow + re-opening it (unless we failed) */ + if (proc_stat != NULL) + { + GNUNET_break (0 == fclose (proc_stat)); + proc_stat = NULL; + first_time = 0; + } +#elif defined(DARWIN) initMachCpuStats (); + updateUsage (); /* initialize */ #elif MINGW InitWinEnv (NULL); -#endif updateUsage (); /* initialize */ +#else + updateUsage (); /* initialize */ +#endif } /** @@ -642,7 +672,7 @@ void __attribute__ ((destructor)) GNUNET_cpustats_ltdl_fini () fclose (proc_stat); proc_stat = NULL; } -#elif OSX +#elif defined(DARWIN) GNUNET_free_non_null (prev_cpu_load); #elif MINGW ShutdownWinEnv (); @@ -650,4 +680,4 @@ void __attribute__ ((destructor)) GNUNET_cpustats_ltdl_fini () } -/* end of os_load_cpu.c */ +/* end of os_load.c */