From 02cf9fc5f0d3a07fd3e55a3e3c722dc0b2cc645a Mon Sep 17 00:00:00 2001 From: hadaq Date: Thu, 2 Sep 2010 16:51:58 +0000 Subject: [PATCH] CPU calc added. Sergey. --- ebctrl/ioc/ebctrlApp/src/evtbuild.c | 112 +++++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 2 deletions(-) diff --git a/ebctrl/ioc/ebctrlApp/src/evtbuild.c b/ebctrl/ioc/ebctrlApp/src/evtbuild.c index 868ac51..ed43a1e 100644 --- a/ebctrl/ioc/ebctrlApp/src/evtbuild.c +++ b/ebctrl/ioc/ebctrlApp/src/evtbuild.c @@ -28,6 +28,44 @@ static char *unit (unsigned long v) return retVal; } +static char* itoa(int val, int base){ + + static char buf[32] = {0}; + + int i = 30; + + for(; val && i ; --i, val /= base) + buf[i] = "0123456789abcdef"[val % base]; + + return &buf[i+1]; +} + +static uint32_t getCPU_usage(uint32_t pid) +{ + FILE *fp; + char ps[100]="ps -o pcpu -p "; + char cpu[100]; + + /* PID must be more than zero */ + if( ! (pid > 0) ) + return 0; + + strcat(ps, itoa(pid, 10)); + + fp=popen(ps,"r"); + + while (fgets(ps, 100, fp)!=NULL) + sprintf(cpu, "%s", ps); + + pclose(fp); + + if( strstr(cpu, "%CPU") != (char*) NULL ){ + return 0; + } + + return (uint32_t) atoi(cpu); +} + long evtbuild_init( struct genSubRecord *pgsub ) { return(0); @@ -36,7 +74,7 @@ long evtbuild_init( struct genSubRecord *pgsub ) long evtbuild_proc( struct genSubRecord *pgsub ) { - uint32_t *out[14]; + uint32_t *out[17]; unsigned long tmp; static unsigned long temp[10] = {0}; int i; @@ -55,11 +93,18 @@ long evtbuild_proc( struct genSubRecord *pgsub ) out[11] = (uint32_t *)pgsub->vall; /* dataMover = number of selected Data Mover */ out[12] = (uint32_t *)pgsub->valm; /* diskNr = disk number where data is being written */ out[13] = (uint32_t *)pgsub->valn; /* number of EB machine (1 = lxhadeb01) */ + out[14] = (uint32_t *)pgsub->valo; /* PID */ + out[15] = (uint32_t *)pgsub->valp; /* core number */ + out[16] = (uint32_t *)pgsub->valq; /* CPU usage */ /* pgsub->vall : bytesWrittenRate in stringin record (array of char's) */ - for( i=0; i<12; i++ ) + for( i=0; i<17; i++ ) *out[i] = 0; + /* + * *************** nrOfMsgs and status of EB *************** + */ + char buf[_POSIX_PATH_MAX]; sprintf( buf, "%s%s", "daq_evtbuild", getenv("EBNUM") ); @@ -77,6 +122,10 @@ long evtbuild_proc( struct genSubRecord *pgsub ) } } + /* + * ************** bytesWritten *************** + */ + if( Worker_getStatistic( buf, "bytesWritten", &tmp ) == -1) { *out[9] = 0; /* zero data rate */ if(evtbuildDebug) @@ -97,6 +146,10 @@ long evtbuild_proc( struct genSubRecord *pgsub ) *out[1] = (int)((*out[1])/1024./1024. + 0.5); } + /* + * ************** evtsComplete *************** + */ + if( Worker_getStatistic( buf, "evtsComplete", &tmp ) == -1) { *out[8] = 0; /* zero evtComp rate */ if(evtbuildDebug) @@ -113,6 +166,10 @@ long evtbuild_proc( struct genSubRecord *pgsub ) temp[2] = *out[2]; } + /* + * ************** evtsDiscarded *************** + */ + if( Worker_getStatistic( buf, "evtsDiscarded", &tmp ) == -1) { *out[10] = 0; /* zero evtDisc rate */ if(evtbuildDebug) @@ -129,6 +186,10 @@ long evtbuild_proc( struct genSubRecord *pgsub ) temp[3] = *out[3]; } + /* + * ************** evtsDataError *************** + */ + if( Worker_getStatistic( buf, "evtsDataError", &tmp ) == -1){ if(evtbuildDebug) printf(" evtbuild.c: Worker_getStatistic failed for evtsDataError!\n"); @@ -137,6 +198,10 @@ long evtbuild_proc( struct genSubRecord *pgsub ) *out[4] = (uint32_t)tmp; } + /* + * ************** evtsTagError *************** + */ + if( Worker_getStatistic( buf, "evtsTagError", &tmp ) == -1){ if(evtbuildDebug) printf(" evtbuild.c: Worker_getStatistic failed for evtsTagError!\n"); @@ -145,6 +210,10 @@ long evtbuild_proc( struct genSubRecord *pgsub ) *out[5] = (uint32_t)tmp; } + /* + * ************** dataMover *************** + */ + if( Worker_getStatistic( buf, "dataMover", &tmp ) == -1){ if(evtbuildDebug) printf(" evtbuild.c: Worker_getStatistic failed for dataMover!\n"); @@ -153,6 +222,9 @@ long evtbuild_proc( struct genSubRecord *pgsub ) *out[11] = (uint32_t)tmp; } + /* + * ************** diskNumEB *************** + */ if( Worker_getStatistic( buf, "diskNumEB", &tmp ) == -1){ if(evtbuildDebug) @@ -162,6 +234,10 @@ long evtbuild_proc( struct genSubRecord *pgsub ) *out[12] = (uint32_t)tmp; } + /* + * ************** evtbuildBuff *************** + */ + char ebbuf[100]; for( i=0; i<(*out[0]); i++ ) { @@ -181,6 +257,10 @@ long evtbuild_proc( struct genSubRecord *pgsub ) } } + /* + * ************** number from hostname *************** + */ + /* Get host name and extract the number from its name */ char hostname[50]; char *c_ptr; @@ -195,7 +275,35 @@ long evtbuild_proc( struct genSubRecord *pgsub ) else{ *out[13] = (uint32_t) strtoul(c_ptr, NULL, 0); } + + /* + * ************** PID of EB *************** + */ + if( Worker_getStatistic( buf, "PID", &tmp ) == -1) { + *out[14] = 0; + if(evtbuildDebug) + printf(" evtbuild.c: Worker_getStatistic failed for PID!\n"); + } + else { + *out[14] = (uint32_t)tmp; + + *out[15] = getCPU_usage(tmp); + } + + /* + * ************** core number *************** + */ + + if( Worker_getStatistic( buf, "coreNr", &tmp ) == -1) { + *out[16] = 0; + if(evtbuildDebug) + printf(" evtbuild.c: Worker_getStatistic failed for coreNr!\n"); + } + else { + *out[16] = (uint32_t)tmp; + } + /* * int bytedCoverted; * sprintf( buf, "%s", unit(*out[9])); -- 2.43.0