From 3aed15d3b0e337d3d20fb194951c66c18bc949b1 Mon Sep 17 00:00:00 2001 From: hadaq Date: Thu, 2 Sep 2010 17:34:23 +0000 Subject: [PATCH] PID, core Nr, CPU added. Sergey. --- ebctrl/ioc/ebctrlApp/src/netmem.c | 126 +++++++++++++++++++++++++++++- 1 file changed, 124 insertions(+), 2 deletions(-) diff --git a/ebctrl/ioc/ebctrlApp/src/netmem.c b/ebctrl/ioc/ebctrlApp/src/netmem.c index 5708309..d4ba742 100644 --- a/ebctrl/ioc/ebctrlApp/src/netmem.c +++ b/ebctrl/ioc/ebctrlApp/src/netmem.c @@ -1,6 +1,8 @@ #include #include +#include #include +#include #include #include @@ -17,10 +19,48 @@ long netmem_init( struct genSubRecord *pgsub ) return(0); } +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 netmem_proc( struct genSubRecord *pgsub ) { - uint32_t *out[8]; + uint32_t *out[11]; unsigned long tmp; int i; @@ -32,14 +72,24 @@ long netmem_proc( struct genSubRecord *pgsub ) out[5] = (uint32_t *)pgsub->valf; /* msgsDis = messages discarded */ out[6] = (uint32_t *)pgsub->valg; /* bytercv = bytes received */ out[7] = (uint32_t *)pgsub->valh; /* byteRRate = bytes received rate */ + out[8] = (uint32_t *)pgsub->vali; /* PID */ + out[9] = (uint32_t *)pgsub->valj; /* core number */ + out[10] = (uint32_t *)pgsub->valk; /* CPU usage */ - for( i=0; i<8; i++ ) + for( i=0; i<11; i++ ) *out[i] = 0; char bufmem[_POSIX_PATH_MAX]; sprintf( bufmem, "%s%s", "daq_netmem", getenv("EBNUM") ); + + /* + * *************** nrOfMsgs *************** + */ + if( Worker_getStatistic( bufmem, "nrOfMsgs", &tmp ) == -1) { + *out[0] = 0; + if(netmemDebug) printf(" netmem.c: Worker_getStatistic failed for nrOfMsgs!\n"); } @@ -50,6 +100,10 @@ long netmem_proc( struct genSubRecord *pgsub ) printf(" netmem.c: Worker_getStatistic::nrOfMsgs = %lu\n", tmp); } + /* + * *************** netmemBuff *************** + */ + char buf[100]; for( i=0; i<(*out[0]); i++ ) { @@ -57,6 +111,8 @@ long netmem_proc( struct genSubRecord *pgsub ) sprintf( buf, "%s%d", "netmemBuff", i ); if( Worker_getStatistic( bufmem, buf, &tmp ) == -1) { + *out[1] = 0; + if(netmemDebug) printf(" netmem.c: Worker_getStatistic failed for %s!\n", buf); } @@ -70,11 +126,17 @@ long netmem_proc( struct genSubRecord *pgsub ) } } + /* + * *************** pktsReceived *************** + */ + for( i=0; i<(*out[0]); i++ ) { sprintf( buf, "%s%d", "pktsReceived", i ); if( Worker_getStatistic( bufmem, buf, &tmp ) == -1) { + *out[2] = 0; + if(netmemDebug) printf(" netmem.c: Worker_getStatistic failed for %s!\n", buf); } @@ -88,11 +150,17 @@ long netmem_proc( struct genSubRecord *pgsub ) } } + /* + * *************** msgsReceived *************** + */ + for( i=0; i<(*out[0]); i++ ) { sprintf( buf, "%s%d", "msgsReceived", i ); if( Worker_getStatistic( bufmem, buf, &tmp ) == -1) { + *out[3] = 0; + if(netmemDebug) printf(" netmem.c: Worker_getStatistic failed for %s!\n", buf); } @@ -106,11 +174,17 @@ long netmem_proc( struct genSubRecord *pgsub ) } } + /* + * *************** pktsDiscarded *************** + */ + for( i=0; i<(*out[0]); i++ ) { sprintf( buf, "%s%d", "pktsDiscarded", i ); if( Worker_getStatistic( bufmem, buf, &tmp ) == -1) { + *out[4] = 0; + if(netmemDebug) printf(" netmem.c: Worker_getStatistic failed for %s!\n", buf); } @@ -124,11 +198,17 @@ long netmem_proc( struct genSubRecord *pgsub ) } } + /* + * *************** msgsDiscarded *************** + */ + for( i=0; i<(*out[0]); i++ ) { sprintf( buf, "%s%d", "msgsDiscarded", i ); if( Worker_getStatistic( bufmem, buf, &tmp ) == -1) { + *out[5] = 0; + if(netmemDebug) printf(" netmem.c: Worker_getStatistic failed for %s!\n", buf); } @@ -142,11 +222,17 @@ long netmem_proc( struct genSubRecord *pgsub ) } } + /* + * *************** bytesReceived *************** + */ + for( i=0; i<(*out[0]); i++ ) { sprintf( buf, "%s%d", "bytesReceived", i ); if( Worker_getStatistic( bufmem, buf, &tmp ) == -1) { + *out[6] = 0; + if(netmemDebug) printf(" netmem.c: Worker_getStatistic failed for %s!\n", buf); } @@ -161,11 +247,17 @@ long netmem_proc( struct genSubRecord *pgsub ) } } + /* + * *************** bytesReceivedRate *************** + */ + for( i=0; i<(*out[0]); i++ ) { sprintf( buf, "%s%d", "bytesReceivedRate", i ); if( Worker_getStatistic( bufmem, buf, &tmp ) == -1) { + *out[7] = 0; + if(netmemDebug) printf(" netmem.c: Worker_getStatistic failed for %s!\n", buf); } @@ -180,6 +272,36 @@ long netmem_proc( struct genSubRecord *pgsub ) } } + /* + * ************** PID of netmem *************** + */ + + if( Worker_getStatistic( bufmem, "PID", &tmp ) == -1) { + *out[8] = 0; + *out[9] = 0; + + if(netmemDebug) + printf(" netmem.c: Worker_getStatistic failed for PID!\n"); + } + else { + *out[8] = (uint32_t)tmp; + + *out[9] = getCPU_usage(tmp); + } + + /* + * ************** core number *************** + */ + + if( Worker_getStatistic( bufmem, "coreNr", &tmp ) == -1) { + *out[10] = 0; + + if(netmemDebug) + printf(" netmem.c: Worker_getStatistic failed for coreNr!\n"); + } + else { + *out[10] = (uint32_t)tmp; + } return(0); } -- 2.43.0