]> jspc29.x-matter.uni-frankfurt.de Git - daqdata.git/commitdiff
Eventbuilder EPICS: bugfix in cpu/process load evaluation:
authorJörn Adamczewski-Musch <j.adamczewski@gsi.de>
Wed, 2 Apr 2014 11:03:41 +0000 (13:03 +0200)
committerJörn Adamczewski-Musch <j.adamczewski@gsi.de>
Wed, 2 Apr 2014 11:03:41 +0000 (13:03 +0200)
- zero deltatime problem
- static functions showing strange variable changes, turned into non-static
(JAM)

ebctrl/ioc/ebctrlApp/src/cpu.c

index 07c60178af8acdcb6d7aa5477f3f4a4f725bd95d..e79663a81ce90b310817cb4c0bc109dfda2866f5 100644 (file)
@@ -12,6 +12,7 @@
 #include <epicsExport.h>
 #include <genSubRecord.h>
 #include <stringinRecord.h>
+#include <sys/stat.h>
 
 #include "worker.h"
 
@@ -28,7 +29,8 @@ typedef struct TheStatS {
   unsigned long cwctime;    /* wall clock time for core CPU usage */
 } TheStat;
 
-static void initStat(TheStat *my)
+/*static void initStat(TheStat *my)*/
+void initStat(TheStat *my)
 {
   my->utime   = 0;
   my->stime   = 0;
@@ -40,7 +42,8 @@ static void initStat(TheStat *my)
   my->cwctime = 0;
 }
 
-static void initProcStat(TheStat *my)
+/*static */
+void initProcStat(TheStat *my)
 {
   my->utime   = 0;
   my->stime   = 0;
@@ -49,7 +52,8 @@ static void initProcStat(TheStat *my)
   my->pwctime = 0;
 }
 
-static void parseCpuStat( TheStat *theStat, uint32_t coreNr )
+/*static*/ 
+void parseCpuStat( TheStat *theStat, uint32_t coreNr )
 {
   int fd;
   char filename[24];
@@ -107,7 +111,8 @@ static void parseCpuStat( TheStat *theStat, uint32_t coreNr )
   }  
 }
 
-static int parseProcStat( TheStat *theStat, pid_t pid )
+/*static*/ 
+int parseProcStat( TheStat *theStat, pid_t pid )
 {
   int fd;
   char filename[24];
@@ -124,7 +129,8 @@ static int parseProcStat( TheStat *theStat, pid_t pid )
   struct stat statBufS, *statBuf = &statBufS;
 
   snprintf (dir, sizeof(dir), "/proc/%d", (int) pid);
-
+  if(cpuDebug)
+      printf("<E> cpu.c: parseProcStat directory %s before stat - pid=%d\n",dir, (int) pid);
   stat(dir, statBuf);
 
   /* Return if the process is gone */
@@ -133,6 +139,8 @@ static int parseProcStat( TheStat *theStat, pid_t pid )
     gettimeofday(&wctime, NULL);
     /* Convert to micro seconds */
     theStat->pwctime = (int) 1000. * (double)wctime.tv_sec + (double)wctime.tv_usec / 1000000.0;
+    if(cpuDebug)
+      printf("<E> cpu.c: parseProcStat sees no directory %s!\n",dir);
     return 0;
   }  
 
@@ -182,22 +190,58 @@ static int parseProcStat( TheStat *theStat, pid_t pid )
   return 0;
 }
 
-static int getProcCpuUsage(TheStat *theStat, TheStat *theStat_old)
+/*static*/ 
+int getProcCpuUsage(TheStat *theStat, TheStat *theStat_old)
 {
-  int procCPU = 100 * ((theStat->utime + theStat->stime + 
-                       theStat->cutime + theStat->cstime) - 
-                      (theStat_old->utime + theStat_old->stime + 
-                       theStat_old->cutime + theStat_old->cstime)) /
-                       (theStat->pwctime - theStat_old->pwctime);
+  int procCPU=0;
+  double deltaT=theStat->pwctime - theStat_old->pwctime;
+   if(cpuDebug)
+      printf("<E> cpu.c: getProcCpuUsage - delta T=%e us!\n",deltaT);
+  if(deltaT>0)
+    {
+      
+      
+      
+    procCPU= 100 * ((theStat->utime + theStat->stime + 
+                          theStat->cutime + theStat->cstime) - 
+                        (theStat_old->utime + theStat_old->stime + 
+                           theStat_old->cutime + theStat_old->cstime)) /
+                        deltaT;
+                        
+                        
+    }
+  else
+  {
+   
+    if(cpuDebug)
+      printf("<E> cpu.c: Worker_getProcCpuUsage with zero delta T !\n");
+  }
 
+  if(cpuDebug)
+      printf("<E> cpu.c:procCPU:%d utime:%ld oldutime:%ld , stime:%ld oldstime:%ld , cutime%ld oldcutime:%ld, cstime:%ld oldcstime:%ld !\n",
+        procCPU, theStat->utime, theStat_old->utime, theStat->stime, theStat_old->stime, theStat->cutime,  theStat_old->cutime,
+        theStat->cstime, theStat_old->cstime);
+  
   return procCPU;
 }
 
-static int getCoreCpuUsage(TheStat *theStat, TheStat * theStat_old)
+/*static*/ 
+int getCoreCpuUsage(TheStat *theStat, TheStat * theStat_old)
 {
-  int coreCPU = 100 * (theStat->cputime - theStat_old->cputime) / 
-                      (theStat->cwctime - theStat_old->cwctime);
-
+  int coreCPU=0;
+  double deltaT=theStat->cwctime - theStat_old->cwctime;
+  if(deltaT>0)
+    {
+      coreCPU = 100 * (theStat->cputime - theStat_old->cputime) / 
+                      deltaT;
+    }
+  else
+    {
+    if(cpuDebug)
+          printf("<E> cpu.c: Worker_getCoreCpuUsage with zero delta T !\n");
+      
+      
+    }
   return coreCPU;
 }
 
@@ -240,6 +284,8 @@ long cpu_proc( struct genSubRecord *pgsub )
   }
   else {
     eb_pid = (uint32_t)tmp;
+       if(cpuDebug)
+      printf("<E> cpu.c: Worker_getStatistic has evtbuild PID %d\n", eb_pid);
   }
 
   /* Get core number of evtbuild */
@@ -302,6 +348,8 @@ long cpu_proc( struct genSubRecord *pgsub )
   }
   else {
     nm_pid = (uint32_t)tmp;
+     if(cpuDebug)
+      printf("<E> cpu.c: Worker_getStatistic has netmem PID %d\n", nm_pid);
   }
 
   /* Get core number of evtbuild */