*/
/**
- * @file util/os_load_cpu.c
+ * @file util/os_load.c
* @brief calls to determine current CPU load
* @author Tzvetan Horozov
* @author Christian Grothoff
#endif
#endif
-#ifdef OSX
+#ifdef DARWIN
#include <mach/mach.h>
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
/**
static double agedIOLoad = -1;
-#ifdef OSX
+#ifdef DARWIN
static int
initMachCpuStats ()
{
}
#endif
+
/**
* Update the currentCPU and currentIO load values.
*
/* 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 };
unsigned long long usage_time = 0, total_time = 1;
/* Get the first line with the data */
+ memset (line, 0, sizeof (line));
rewind (proc_stat);
fflush (proc_stat);
- if (NULL == fgets (line, 256, proc_stat))
+ if (NULL == fgets (line, sizeof (line), proc_stat))
{
GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR |
GNUNET_ERROR_TYPE_BULK,
}
#endif
-#ifdef OSX
+#ifdef DARWIN
{
unsigned int cpu_count;
processor_cpu_load_info_t cpu_load;
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
* that lock has already been obtained.
*/
static void
-updateAgedLoad (struct GNUNET_CONFIGURATION_Handle *cfg)
+updateAgedLoad (const struct GNUNET_CONFIGURATION_Handle *cfg)
{
static struct GNUNET_TIME_Absolute lastCall;
* (100 is equivalent to full load)
*/
int
-GNUNET_OS_load_cpu_get (struct GNUNET_CONFIGURATION_Handle *cfg)
+GNUNET_OS_load_cpu_get (const struct GNUNET_CONFIGURATION_Handle *cfg)
{
unsigned long long maxCPULoad;
int ret;
GNUNET_CONFIGURATION_get_value_number (cfg, "LOAD", "MAXCPULOAD",
&maxCPULoad))
return GNUNET_SYSERR;
+ if (maxCPULoad == 0)
+ return 100;
return (100 * ret) / maxCPULoad;
}
* (100 is equivalent to full load)
*/
int
-GNUNET_OS_load_disk_get (struct GNUNET_CONFIGURATION_Handle *cfg)
+GNUNET_OS_load_disk_get (const struct GNUNET_CONFIGURATION_Handle *cfg)
{
unsigned long long maxIOLoad;
int ret;
GNUNET_CONFIGURATION_get_value_number (cfg, "LOAD", "MAXIOLOAD",
&maxIOLoad))
return GNUNET_SYSERR;
+ if (maxIOLoad == 0)
+ return 100;
return (100 * ret) / maxIOLoad;
}
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
}
/**
fclose (proc_stat);
proc_stat = NULL;
}
-#elif OSX
+#elif defined(DARWIN)
GNUNET_free_non_null (prev_cpu_load);
#elif MINGW
ShutdownWinEnv ();
}
-/* end of os_load_cpu.c */
+/* end of os_load.c */