Remove cpu_total_buffer global and all related baggage and create a new
global status array that will be updated every 5 seconds through a helper
function

Signed-off-by: Carlo Marcelo Arenas Belon <[EMAIL PROTECTED]>
---
 trunk/monitor-core/libmetrics/aix/metrics.c |  195 +++++++++++----------------
 1 files changed, 81 insertions(+), 114 deletions(-)

diff --git a/trunk/monitor-core/libmetrics/aix/metrics.c 
b/trunk/monitor-core/libmetrics/aix/metrics.c
index fcd08c8..ed2e0a7 100644
--- a/trunk/monitor-core/libmetrics/aix/metrics.c
+++ b/trunk/monitor-core/libmetrics/aix/metrics.c
@@ -84,21 +84,11 @@ struct product {
 
 #define MAX_CPUS  64
 
-#define INFO_TIMEOUT   10
-#define CPU_INFO_TIMEOUT INFO_TIMEOUT
+#define INFO_TIMEOUT    (5.0)
+#define CPUINFO_TIMEOUT INFO_TIMEOUT
 
 #define MEM_KB_PER_PAGE (4096/1024)
 
-
-struct cpu_info {
-  time_t timestamp;  
-  u_longlong_t total_ticks;
-  u_longlong_t user;        /*  raw total number of clock ticks spent in user 
mode */
-  u_longlong_t sys;         /* raw total number of clock ticks spent in system 
mode */
-  u_longlong_t idle;        /* raw total number of clock ticks spent idle */
-  u_longlong_t wait;        /* raw total number of clock ticks spent waiting 
for I/O */
-};
-
 struct net_stat{
   double ipackets;
   double opackets;
@@ -106,24 +96,16 @@ struct net_stat{
   double obytes;
 } cur_net_stat;
 
-
-
-
-
-
-int ci_flag=0;
 int ni_flag=0;
 
-perfstat_cpu_total_t cpu_total_buffer;
 perfstat_netinterface_total_t ninfo[2],*last_ninfo, *cur_ninfo ;
 
-
-struct cpu_info cpu_info[2], 
-  *last_cpu_info,
-  *cur_cpu_info;
-
-
-  
+struct cpuinfo_stats {
+   double user;
+   double sys;
+   double idle;
+   double wait;
+} current_cpuinfo;
 
 int aixver, aixrel, aixlev, aixfix;
 struct utsname unames;
@@ -132,7 +114,6 @@ struct utsname unames;
 /* Prototypes
  */
 void  update_ifdata(void);
-void get_cpuinfo(void);
 int bos_level(int *aix_version, int *aix_release, int *aix_level, int 
*aix_fix);
 
 
@@ -148,19 +129,9 @@ metric_init(void)
 {
    g_val_t val;
 
-
-   last_cpu_info = &cpu_info[ci_flag];
-   ci_flag^=1;
-   cur_cpu_info  = &cpu_info[ci_flag];
-   cur_cpu_info->total_ticks = 0;
-   
    update_ifdata();
    uname(&unames);
    
-   get_cpuinfo();
-   sleep(CPU_INFO_TIMEOUT+1);
-   get_cpuinfo();
-
    update_ifdata();
 
    bos_level(&aixver, &aixrel, &aixlev, &aixfix);
@@ -250,39 +221,83 @@ os_release_func ( void )
    return val;
 }        
 
+/*
+ * AIX  defines:
+ * CPU_USER, CPU_SYS, CPU_IDLE, CPU_WAIT
+ * so no metrics for cpu_nice, or cpu_aidle
+ */
 
-/* AIX  defines
-   CPU_IDLE, CPU_USER, CPU_SYS(CPU_KERNEL), CPU_WAIT
-   so no metrics for cpu_nice, or cpu_aidle
-*/
-
+void
+update_cpuinfo( void )
+{
+   perfstat_cpu_total_t c;
+   static u_longlong_t last_user_ticks = 0L;
+   static u_longlong_t last_sys_ticks = 0L;
+   static u_longlong_t last_idle_ticks = 0L;
+   static u_longlong_t last_wait_ticks = 0L;
+   static u_longlong_t last_total_ticks = 0L;
+   u_longlong_t user_ticks, sys_ticks, idle_ticks, wait_ticks,
+                total_ticks, diff;
+   static double last_time = 0.0;
+   double now;
+   struct timeval timeValue;
+   struct timezone timeZone;
+
+   gettimeofday( &timeValue, &timeZone );
+   now = (double) (timeValue.tv_sec - boottime) + (timeValue.tv_usec / 
1000000.0);
+
+/* Do nothing if we are called too soon after the last call */
+   if (now - last_time < CPUINFO_TIMEOUT) return;
+
+   last_time = now;
+
+/* obtain CPU parameters */
+   if (perfstat_cpu_total( NULL, &c, sizeof( perfstat_cpu_total_t ), 1 ) != -1)
+   {
+      user_ticks = c.user;
+      sys_ticks = c.sys;
+      idle_ticks = c.idle;
+      wait_ticks = c.wait;
+      total_ticks = c.user + c.sys + c.idle + c.wait;
+
+      diff = total_ticks - last_total_ticks;
+
+      if ( diff )
+      {
+         current_cpuinfo.user = 100.0 * (user_ticks - last_user_ticks) / diff;
+         current_cpuinfo.sys = 100.0 * (sys_ticks - last_sys_ticks) / diff;
+         current_cpuinfo.idle = 100.0 * (idle_ticks - last_idle_ticks) / diff;
+         current_cpuinfo.wait = 100.0 * (wait_ticks - last_wait_ticks) / diff;
+      }
 
-#define CALC_CPUINFO(type) ((100.0*(cur_cpu_info->type - 
last_cpu_info->type))/(1.0*(cur_cpu_info->total_ticks - 
last_cpu_info->total_ticks)))
+      last_user_ticks = user_ticks;
+      last_sys_ticks = sys_ticks;
+      last_idle_ticks = idle_ticks;
+      last_wait_ticks = wait_ticks;
+      last_total_ticks = total_ticks;
+   }
+}
 
 g_val_t
 cpu_user_func ( void )
 {
    g_val_t val;
    
+   update_cpuinfo();
    
-   get_cpuinfo();
-   
-   val.f = CALC_CPUINFO(user);
+   val.f = current_cpuinfo.user;
 
-   if(val.f < 0) val.f = 0.0;
    return val;
 }
 
-
-/*
-** AIX does not have this
-** FIXME -- 
-*/
+/* FIXME? */
 g_val_t
 cpu_nice_func ( void )
 {
    g_val_t val;
+
    val.f = 0;
+
    return val;
 }
 
@@ -291,9 +306,9 @@ cpu_system_func ( void )
 {
    g_val_t val;
 
-   get_cpuinfo();
-   val.f = CALC_CPUINFO(sys) ;
-   if(val.f < 0) val.f = 0.0;
+   update_cpuinfo();
+   val.f = current_cpuinfo.sys;
+   
    return val;
 }
 g_val_t 
@@ -302,11 +317,9 @@ cpu_wio_func ( void )
 {
    g_val_t val;
    
-   get_cpuinfo();
-   val.f = CALC_CPUINFO(wait);
-
+   update_cpuinfo();
+   val.f = current_cpuinfo.wait;
 
-   if(val.f < 0) val.f = 0.0;
    return val;
 }
 
@@ -316,41 +329,35 @@ cpu_idle_func ( void )
    g_val_t val;
 
 
-   get_cpuinfo();
-   val.f = CALC_CPUINFO(idle);
+   update_cpuinfo();
+   val.f = current_cpuinfo.idle;
 
-
-   if(val.f < 0) val.f = 0.0;
    return val;
 }
 
-/*
-** AIX does not have this
-** FIXME -- 
-*/
+/* FIXME? */
 g_val_t 
 cpu_aidle_func ( void )
 {
    g_val_t val;
+
    val.f = 0.0;
+
    return val;
 }
 
-/*
-** Don't know what it is 
-** FIXME -- 
-*/
+/* FIXME? */
 g_val_t 
 cpu_intr_func ( void )
 {
    g_val_t val;
+
    val.f = 0.0;
+
    return val;
 }
 
-/* Don't know what it is 
-** FIXME -- 
-*/
+/* FIXME? */
 g_val_t 
 cpu_sintr_func ( void )
 {
@@ -719,46 +726,6 @@ mtu_func ( void )
    return val;
 }
 
-
-
-
-
-
-
-
-void get_cpuinfo() 
-{
-  u_longlong_t cpu_total;
-  time_t new_time;
-
-
-  new_time = time(NULL);
-
-  if (new_time - CPU_INFO_TIMEOUT > cur_cpu_info->timestamp ) 
-    {
-
-      perfstat_cpu_total(NULL,  &cpu_total_buffer, 
sizeof(perfstat_cpu_total_t), 1);
-
-
-      cpu_total = cpu_total_buffer.user +  cpu_total_buffer.sys  
-       +  cpu_total_buffer.idle +  cpu_total_buffer.wait;
-
-  
-      last_cpu_info=&cpu_info[ci_flag];
-      ci_flag^=1; 
-      cur_cpu_info=&cpu_info[ci_flag];
-
-      cur_cpu_info->timestamp   = new_time;
-      cur_cpu_info->total_ticks = cpu_total;
-      cur_cpu_info->user        = cpu_total_buffer.user;
-      cur_cpu_info->sys         = cpu_total_buffer.sys;
-      cur_cpu_info->idle        = cpu_total_buffer.idle;
-      cur_cpu_info->wait        = cpu_total_buffer.wait;
-    }
-} /*      get_cpuinfo  */
-  
-
-
 /* int bos_level(int *aix_version, int *aix_release, int *aix_level, int 
*aix_fix)
  *  is copied form 
  *
-- 
1.5.3.7


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Ganglia-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ganglia-developers

Reply via email to