native version of CpuPerc.java

This commit is contained in:
Doug MacEachern 2008-03-12 06:20:43 +00:00
parent 93db8c99cd
commit 043afd2608
2 changed files with 46 additions and 0 deletions

View File

@ -19,6 +19,19 @@
#ifndef SIGAR_FORMAT_H #ifndef SIGAR_FORMAT_H
#define SIGAR_FORMAT_H #define SIGAR_FORMAT_H
typedef struct {
double user;
double sys;
double nice;
double idle;
double wait;
double combined;
} sigar_cpu_perc_t;
SIGAR_DECLARE(int) sigar_cpu_perc_calculate(sigar_cpu_t *prev,
sigar_cpu_t *curr,
sigar_cpu_perc_t *perc);
SIGAR_DECLARE(int) sigar_uptime_string(sigar_t *sigar, SIGAR_DECLARE(int) sigar_uptime_string(sigar_t *sigar,
sigar_uptime_t *uptime, sigar_uptime_t *uptime,
char *buffer, char *buffer,

View File

@ -22,6 +22,7 @@
#include "sigar_private.h" #include "sigar_private.h"
#include "sigar_util.h" #include "sigar_util.h"
#include "sigar_os.h" #include "sigar_os.h"
#include "sigar_format.h"
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
@ -604,3 +605,35 @@ SIGAR_DECLARE(char *)sigar_net_services_name_get(sigar_t *sigar,
return NULL; return NULL;
} }
} }
SIGAR_DECLARE(int) sigar_cpu_perc_calculate(sigar_cpu_t *prev,
sigar_cpu_t *curr,
sigar_cpu_perc_t *perc)
{
double diff_user, diff_sys, diff_nice, diff_idle, diff_wait, diff_total;
diff_user = curr->user - prev->user;
diff_sys = curr->sys - prev->sys;
diff_nice = curr->nice - prev->nice;
diff_idle = curr->idle - prev->idle;
diff_wait = curr->wait - prev->wait;
diff_user = diff_user < 0 ? 0 : diff_user;
diff_sys = diff_sys < 0 ? 0 : diff_sys;
diff_nice = diff_nice < 0 ? 0 : diff_nice;
diff_idle = diff_idle < 0 ? 0 : diff_idle;
diff_wait = diff_wait < 0 ? 0 : diff_wait;
diff_total =
diff_user + diff_sys + diff_nice + diff_idle + diff_wait;
perc->user = diff_user / diff_total;
perc->sys = diff_sys / diff_total;
perc->nice = diff_nice / diff_total;
perc->idle = diff_idle / diff_total;
perc->wait = diff_wait / diff_total;
perc->combined =
perc->user + perc->sys + perc->nice + perc->wait;
return SIGAR_OK;
}