39 #if HAVE_SYS_SYSINFO_H
40 #include <sys/sysinfo.h>
53 #include <mach/mach.h>
55 static processor_cpu_load_info_t prev_cpu_load;
58 #define DEBUG_STATUSCALLS GNUNET_NO
61 static FILE *proc_stat;
92 unsigned int cpu_count;
93 processor_cpu_load_info_t cpu_load;
94 mach_msg_type_number_t cpu_msg_count;
98 kret = host_processor_info (mach_host_self (),
99 PROCESSOR_CPU_LOAD_INFO,
101 (processor_info_array_t *) &cpu_load,
103 if (kret != KERN_SUCCESS)
108 prev_cpu_load =
GNUNET_malloc (cpu_count *
sizeof(*prev_cpu_load));
109 for (i = 0; i < cpu_count; i++)
111 for (j = 0; j < CPU_STATE_MAX; j++)
113 prev_cpu_load[i].cpu_ticks[j] = cpu_load[i].cpu_ticks[j];
116 vm_deallocate (mach_task_self (),
117 (vm_address_t) cpu_load,
118 (vm_size_t) (cpu_msg_count *
sizeof(*cpu_load)));
140 if (proc_stat != NULL)
142 static unsigned long long last_cpu_results[5] = { 0, 0, 0, 0, 0 };
146 unsigned long long user_read, system_read, nice_read, idle_read,
148 unsigned long long user, system, nice,
idle, iowait;
149 unsigned long long usage_time = 0, total_time = 1;
154 if (NULL == fgets (
line, 256, proc_stat))
157 "fgets",
"/proc/stat");
163 ret = sscanf (
line,
"%*s %llu %llu %llu %llu %llu",
165 &system_read, &nice_read, &idle_read, &iowait_read);
169 "fgets-sscanf",
"/proc/stat");
177 user = user_read - last_cpu_results[0];
178 system = system_read - last_cpu_results[1];
179 nice = nice_read - last_cpu_results[2];
180 idle = idle_read - last_cpu_results[3];
181 iowait = iowait_read - last_cpu_results[4];
183 usage_time = user + system + nice;
184 total_time = usage_time +
idle + iowait;
185 if ((total_time > 0) && (have_last_cpu ==
GNUNET_YES))
194 last_cpu_results[0] = user_read;
195 last_cpu_results[1] = system_read;
196 last_cpu_results[2] = nice_read;
197 last_cpu_results[3] = idle_read;
198 last_cpu_results[4] = iowait_read;
208 unsigned int cpu_count;
209 processor_cpu_load_info_t cpu_load;
210 mach_msg_type_number_t cpu_msg_count;
211 unsigned long long t_sys, t_user, t_nice, t_idle, t_total;
212 unsigned long long t_idle_all, t_total_all;
216 t_idle_all = t_total_all = 0;
217 kret = host_processor_info (mach_host_self (), PROCESSOR_CPU_LOAD_INFO,
219 (processor_info_array_t *) &cpu_load,
221 if (kret == KERN_SUCCESS)
223 for (i = 0; i < cpu_count; i++)
225 if (cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM] >=
226 prev_cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM])
228 t_sys = cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM]
229 - prev_cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM];
233 t_sys = cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM]
234 + (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_SYSTEM]
238 if (cpu_load[i].cpu_ticks[CPU_STATE_USER] >=
239 prev_cpu_load[i].cpu_ticks[CPU_STATE_USER])
241 t_user = cpu_load[i].cpu_ticks[CPU_STATE_USER]
242 - prev_cpu_load[i].cpu_ticks[CPU_STATE_USER];
246 t_user = cpu_load[i].cpu_ticks[CPU_STATE_USER]
247 + (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_USER]
251 if (cpu_load[i].cpu_ticks[CPU_STATE_NICE] >=
252 prev_cpu_load[i].cpu_ticks[CPU_STATE_NICE])
254 t_nice = cpu_load[i].cpu_ticks[CPU_STATE_NICE]
255 - prev_cpu_load[i].cpu_ticks[CPU_STATE_NICE];
259 t_nice = cpu_load[i].cpu_ticks[CPU_STATE_NICE]
260 + (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_NICE]
264 if (cpu_load[i].cpu_ticks[CPU_STATE_IDLE] >=
265 prev_cpu_load[i].cpu_ticks[CPU_STATE_IDLE])
267 t_idle = cpu_load[i].cpu_ticks[CPU_STATE_IDLE]
268 - prev_cpu_load[i].cpu_ticks[CPU_STATE_IDLE];
272 t_idle = cpu_load[i].cpu_ticks[CPU_STATE_IDLE]
273 + (ULONG_MAX - prev_cpu_load[i].cpu_ticks[CPU_STATE_IDLE]
276 t_total = t_sys + t_user + t_nice + t_idle;
277 t_idle_all += t_idle;
278 t_total_all += t_total;
280 for (i = 0; i < cpu_count; i++)
282 for (j = 0; j < CPU_STATE_MAX; j++)
284 prev_cpu_load[i].cpu_ticks[j] = cpu_load[i].cpu_ticks[j];
291 vm_deallocate (mach_task_self (),
292 (vm_address_t) cpu_load,
293 (vm_size_t) (cpu_msg_count *
sizeof(*cpu_load)));
305 #if SOLARIS && HAVE_KSTAT_H && HAVE_SYS_SYSINFO_H
307 static long long last_idlecount;
308 static long long last_totalcount;
309 static int kstat_once;
314 long long totalcount;
316 long long deltatotal;
329 for (khelper = kc->kc_chain; khelper != NULL; khelper = khelper->ks_next)
333 if (0 != strncmp (khelper->ks_name,
"cpu_stat", strlen (
"cpu_stat")))
335 if (khelper->ks_data_size >
sizeof(cpu_stat_t))
337 if (-1 != kstat_read (kc, khelper, &
stats))
339 idlecount +=
stats.cpu_sysinfo.cpu[CPU_IDLE];
341 +=
stats.cpu_sysinfo.cpu[CPU_IDLE]
342 +
stats.cpu_sysinfo.cpu[CPU_USER]
343 +
stats.cpu_sysinfo.cpu[CPU_KERNEL]
344 +
stats.cpu_sysinfo.cpu[CPU_WAIT];
347 if (0 != kstat_close (kc))
349 if ((idlecount == 0) && (totalcount == 0))
351 deltaidle = idlecount - last_idlecount;
352 deltatotal = totalcount - last_totalcount;
353 if ((deltatotal > 0) && (last_totalcount > 0))
365 last_idlecount = idlecount;
366 last_totalcount = totalcount;
380 static int warnOnce = 0;
382 if (1 != getloadavg (&loadavg, 1))
508 return (
unsigned int) percentage;
526 dir = opendir (
"/proc");
530 while (NULL != (ent = readdir (
dir)))
532 if ((*ent->d_name >
'0') && (*ent->d_name <=
'9'))
551 unsigned int mem_usage;
557 if ((-1 == ld_cpu) || (-1 == ld_disk))
561 nproc = get_nproc ();
568 ld_cpu, ld_disk, mem_usage, nproc);
599 "STATS_DIR", &stats_dir))
618 _ (
"Cannot open %s for writing load statistics. "
619 "Not logging load statistics\n"), fn);
626 proc_stat = fopen (
"/proc/stat",
"r");
627 if (NULL == proc_stat)
629 "fopen",
"/proc/stat");
646 if (proc_stat != NULL)
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
static int ret
Return value of the commandline.
static char * dir
Set to the directory where runtime files are stored.
static char * line
Desired phone line (string to be converted to a hash).
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
static void idle(void *cls)
Initial task to startup application.
struct GNUNET_STATISTICS_Handle * stats
Handle to the statistics service.
static char * hostname
Our hostname; we give this to all the peers we start.
static void sample_load_task(void *cls)
static int currentCPULoad
Current CPU load, as percentage of CPU cycles not idle or blocked on IO.
static int disk_get_load()
Get the load of the CPU relative to what is allowed.
static int cpu_get_load()
Get the load of the CPU relative to what is allowed.
static int currentIOLoad
Current IO load, as percentage of CPU cycles blocked on IO.
struct GNUNET_SCHEDULER_Task * sample_load_task_id
static double agedCPULoad
void GST_stats_init(const struct GNUNET_CONFIGURATION_Handle *cfg)
Initialize logging CPU and IO statisticfs.
void GST_stats_destroy()
Shutdown the status calls module.
static int updateUsage()
Update the currentCPU and currentIO load (and on Linux, memory) values.
static unsigned int mem_get_usage()
Get the percentage of memory used.
static void updateAgedLoad()
Update load values (if enough time has expired), including computation of averages.
struct GNUNET_BIO_WriteHandle * bw
handle to the file to write the load statistics to
unsigned long kb_main_used
unsigned long kb_main_total
enum GNUNET_GenericReturnValue GNUNET_BIO_write_close(struct GNUNET_BIO_WriteHandle *h, char **emsg)
Close an IO handle.
enum GNUNET_GenericReturnValue GNUNET_BIO_write(struct GNUNET_BIO_WriteHandle *h, const char *what, const void *buffer, size_t n)
Write a buffer to a handle.
struct GNUNET_BIO_WriteHandle * GNUNET_BIO_write_open_file(const char *fn)
Open a file for writing.
enum GNUNET_GenericReturnValue 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.
#define GNUNET_log(kind,...)
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
@ GNUNET_ERROR_TYPE_WARNING
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
#define GNUNET_OS_get_hostname_max_length()
Get maximum string length returned by gethostname()
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.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
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.
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...
#define GNUNET_TIME_UNIT_SECONDS
One second.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Handle for buffered writing.
Entry in list of pending tasks.
Time for absolute times used by GNUnet, in microseconds.
uint64_t abs_value_us
The actual value.
Time for relative time used by GNUnet, in microseconds.
uint64_t rel_value_us
The actual value.