#include #include #include #include #include #include #include #include #include #include //-- for gcc 4.3 #include #include #include #include #include #include extern int errno; #include #include "proc_read.hh" #include "shmem_roc.h" #include "shmem_srv.hh" //---- after shmem_depfet.h !!!! int fork2(); //-------------- my vme -------------------------- #ifdef USEVME extern "C" { #include "rc_vme.h" } FADC250 fadc250[Nfa250+1]; #endif //-------------------------------------------- static FILE *KeyFile=NULL; extern FILE *tmplog; extern char *printf_buffer; int send_rcm_msg(int cmd_socket, char* message); void process_msg (char *buf, char **tk, int *ntk, int max_tok ); int field_cmd(char *msg); int TCP_CONNECT(char *HOST, int PORT); int STREQ(char *s1,const char *s2); int get_stat(char *sndstr,int lenstr); int get_slot(char *sndstr,int lenstr, int slot); extern "C" { void printlog2(const char* msg, int var) { //printf("%s %d tmplog=%p sig_hup=%d\n",msg,var,tmplog,sig_hup); if (tmplog) { fprintf(tmplog,"%s %ld\n",msg,var); fflush(tmplog); } } } //------------------------------------------------------------- int strcasecmp2(const char *s1, const char *s2) { while ( (*s1 != '\0') && (tolower(*(unsigned char *) s1) == tolower(*(unsigned char *) s2)) ) { s1++; s2++; } if (*s1!='\0' && *s2!='\0') return -2; else if (*s1!='\0' && *s2=='\0') return 1; else if (*s1=='\0' && *s2!='\0') return -1; else if (*s1=='\0' && *s2=='\0') return 0; else {printf("it may not be !!!!\n"); exit(1); } } //------------------------------------------------------------- // Global VARs //------------------------------------------------------------- int semid=0; int shmid=0; roc_shmem *shmem_ptr=NULL; int cmd_socket=0; LogBuffer *logbuf=NULL; const int LNAME=128; struct ADDRESS { int FLAG; int SOCK; char NAME[LNAME]; char HOST[LNAME]; }; struct ADDRESS addr_in; struct ADDRESS addr_out; char HOST_NAME[80]; char PROC_NAME[80]; //------------------------------------------------------------- //static void *run_control (void* arg) { int run_control (struct HOST* host) { char *Buffer, *substr1, *substr2; int Result, Bytes=130000; // struct HOST *host = (struct HOST*) arg; gethostname(HOST_NAME, 80); substr1=strstr(HOST_NAME,"."); if (substr1) { printf("HOST=%s ptr=%p substr1=%s ptr=%p diff=%d\n",HOST_NAME,HOST_NAME,substr1,substr1,substr1-HOST_NAME); //substr1[0]=0; strncpy(PROC_NAME,HOST_NAME,(substr1-HOST_NAME)); substr2=strstr(PROC_NAME,"-"); //-- remove dash from gluonraid3-daq if (substr2) { *substr2=0; } //sprintf(PROC_NAME,"%s","NODE"); } else { printf("HOST=%s ptr=%p \n",HOST_NAME,HOST_NAME); strncpy(PROC_NAME,HOST_NAME,80); substr2=strstr(PROC_NAME,"-"); //-- remove dash from gluonraid3-daq if (substr2) { *substr2=0; } } shmem_ptr=host->shmem_ptr; semid=host->semid; shmid=host->shmid; logbuf=host->logbuffer_ptr; int RUN_LOOP=1; while(RUN_LOOP) { //------------------------------------------------------------------- // TCP connection init //------------------------------------------------------------------- printf("run_control()::try to connect to host=%s port=%d\n",host->NAME,host->PORT); cmd_socket=TCP_CONNECT(host->NAME, host->PORT); printf("run_control():: Connected to cmd host sock=%d !!!\n",cmd_socket); //------------------------------------------------------------------- // TCP connection loop //------------------------------------------------------------------- Buffer = (char *)malloc(Bytes); if (Buffer == 0x0) { printf("run_control()::could not malloc buffer of byte length %d\n",Bytes); exit (102); } while (1) { memset(Buffer, 0, Bytes); printf("run_control():: new recv...sock=%d\n",cmd_socket); if ((Result=recv(cmd_socket, (void *)Buffer, Bytes, 0)) < 0) { perror ("run_control()::Can't recv"); break; } else if (Result == 0) { printf ("EOF\n"); close(cmd_socket); cmd_socket=TCP_CONNECT(host->NAME, host->PORT); } printf("run_control():: get string len=%d str=%.40s\n",Result,Buffer); //for (int i=0;ih_addr))->s_addr; /* if (inet_aton(HOST, &Addr.sin_addr) == 0x0) { printf("could not get ip address for %s\n", HOST); exit (101); } */ if ((Socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf ("TCP_CONNECT():: Can't open datagram socket"); exit (106); } while(connect(Socket, (struct sockaddr *)&Addr, sizeof(Addr)) < 0) { printf ("TCP_CONNECT()::Can't connect to %s %d wait 10 sec ...\n",HOST,PORT); sleep(10); } printf ("TCP_CONNECT()::Connected to host=%s port=%d socket=%d INFO:: %s\n",HOST,PORT,Socket,INFO); if ((ret=send(Socket, (void *)INFO, strlen(INFO), 0))!=(int)strlen(INFO)) { printf("Error Send INFO ret=%d of (%d)\n",ret,(int)strlen(INFO)); perror("send to RC"); } printf ("Send status host=%s port=%d socket=%d STATUS:: %s\n",HOST,PORT,Socket,STATUS); usleep(1000); if ((ret=send(Socket, (void *)STATUS, strlen(STATUS), 0))!=(int)strlen(STATUS)) { printf("Error Send STATUS ret=%d of (%d)\n",ret,(int)strlen(STATUS)); perror("send to RC"); } return Socket; } //------------------------------------------------------------------------------------ // //------------------------------------------------------------------------------------ int system2( char *cmd_string) { pid_t pid; //int status; if (cmd_string == NULL ) return(1); if ( (pid = fork2() ) < 0 ) return (-1); if ( pid == 0 ) { printf("execl: %s \n",cmd_string); execl("/bin/sh","sh", "-c", cmd_string, (char*) 0); exit(-1); } else { //wait (&status); return pid; } } /******************************************************************************** void process_msg (char *buf, char **tk, int *ntk) This new version uses strsep instead of strtok that can't deal correctly with empty strings. ********************************************************************************/ void process_msg_v1 (char *buf, char **tk, int *ntk,int MAX_TOKENS) { int n = 0, len; char *ptr; *ntk=0; ptr=buf; len=strlen(buf); for (int i=0;i0 && len>=i && ptr[i]==0x20 && ptr[i-1]==0x20) { //printf(" m:%d:%d:%d \n",i,len,len-i); memcpy(&ptr[i-1],&ptr[i],len-i); ptr[len-1]=0; len--; goto loop; } //printf(" %2d=%02x ",i,ptr[i]); }; if (ptr[len-1]==0x20) ptr[len-1]=0; len=strlen(buf); // printf("new BUF LEN=%d \n",len); for ( n = 0 ; n < MAX_TOKENS ; n++ ) { tk[n] = NULL; tk[n] = strsep(&buf, " \t\n\r"); if ( tk[n] == NULL ) break; // printf("p_m: tk[%d] = %s\n",n, tk[n]); } *ntk = n; //for (int i=0;i0 && len>=i && ptr[i]==0x20 && ptr[i-1]==0x20) { //printf(" m:%d:%d:%d \n",i,len,len-i); memcpy(&ptr[i-1],&ptr[i],len-i); ptr[len-1]=0; len--; goto loop; } //printf(" %2d=%02x ",i,ptr[i]); }; if (ptr[len-1]==0x20) ptr[len-1]=0; // len=strlen(buf); printf("new BUF LEN=%d \n",len); str=buf; // tk[0] = strtok_r(&buf," ", &saveptr); for ( n = 0 ; n < MAX_TOKENS ; n++ ) { tk[n] = NULL; // tk[n] = strsep(&buf, " \t\n\r"); tk[n] = strtok_r(str," ", &saveptr); str=NULL; if ( tk[n] == NULL ) break; // printf("p_m: tk[%d] = %s\n",n, tk[n]); } *ntk = n; // for (int i=0;i use ALL strncpy(addr_in.NAME,"ALL",LNAME); strncpy(addr_out.NAME,"ALL",LNAME); ret=0; } if (strcasecmp2(addr_out.NAME,"ALL")==0 || strcasecmp2(PROC_NAME,addr_out.NAME)>=0 ) { if (addr_in.SOCK>0) { sprintf(sndstr,"MSG:%d:%s:LM%06d: \n",addr_in.SOCK,PROC_NAME,0); sprintf(msg_header,"MSG:%d:%s",addr_in.SOCK,PROC_NAME); } else { sprintf(sndstr,"MSG:%s:%s:LM%06d: \n",addr_in.NAME,PROC_NAME,0); sprintf(msg_header,"MSG:%s:%s",addr_in.NAME,PROC_NAME); } printf("found MY address: (%s,%s,%d) new_adr=|%s| msg_header=|%s|\n",PROC_NAME,addr_out.NAME,strcasecmp2(PROC_NAME,addr_out.NAME),sndstr,msg_header); } else { //-- not my address, skip return 1; /* ignored */ } } else { //-- no prefix (CMD,RCM,MSG) no address .... skip return 1; /* Unknown command ignored */ } /*------------------------------------------------------------------------------------------*/ /* H E L P */ /*------------------------------------------------------------------------------------------*/ if(STREQ(tk[1],"HELP")) { //printf(" >>>>>>>>>>>>>>>>>>>>> RCM HELP <<<<<<<<<<<<<<<<<<<\n"); //printf("=================> sizeof(sndstr)=%d\n",sizeof(sndstr)); sprintf(&sndstr[strlen(sndstr)],"-----------> ROC Specific Commands <--------------\n"); sprintf(&sndstr[strlen(sndstr)]," CMD[:roc] HELP \n"); sprintf(&sndstr[strlen(sndstr)]," CMD[:roc] HIST [plot/reset] [rol1/rol2/err] \n"); sprintf(&sndstr[strlen(sndstr)]," CMD[:roc] STAT \n"); sprintf(&sndstr[strlen(sndstr)]," CMD[:roc] START_CODA_PROC [cmd line] \n"); sprintf(&sndstr[strlen(sndstr)]," CMD[:roc] GETLOG \n"); sprintf(&sndstr[strlen(sndstr)]," CMD[:roc] VME stat \n"); sprintf(&sndstr[strlen(sndstr)]," CMD[:roc] VME READSLOT[RS] \n"); sprintf(&sndstr[strlen(sndstr)]," CMD[:roc] VME set125offset[s125o] [window_size] \n"); sprintf(&sndstr[strlen(sndstr)]," CMD[:roc] VME set250offset[s250o] [window_size] \n"); sprintf(&sndstr[strlen(sndstr)]," CMD[:roc] EXIT \n"); //puts(sndstr); //ret=send(cmd_socket, (void *)sndstr, strlen(sndstr), 0); ret=send_rcm_msg(cmd_socket, sndstr); } if(STREQ(tk[1],"EXIT")) { //printf(" >>>>>>>>>>>>>>>>>>>>> RCM EXIT <<<<<<<<<<<<<<<<<<<\n"); sprintf(&sndstr[strlen(sndstr)],"-----------> %s accept cmd %s <--------------\n",PROC_NAME,tk[1]); //ret=send(cmd_socket, (void *)sndstr, strlen(sndstr), 0); ret=send_rcm_msg(cmd_socket, sndstr); exit(0); } /*------------------------------------------------------------------------------------------*/ if(STREQ(tk[1],"HIST")) { /* plot hist */ #ifdef USE_HIST unsigned int * hist; char* chist; char htitle[256]; #define LHBUF 65536 char hist_buf[LHBUF]; int i1=0,i2=0,icnt=0, plot_empty=0; SHM_HIST *histp; if( ntk<3 || STREQ(tk[2],"plot")) { //--------------------------------- if( ntk<4 || STREQ(tk[3],"rol1")) { histp=shmem_ptr->H_rol1; if( ntk<5 ) { //--- ID, or plot all --- i1=0; i2=H_rol1_NH; //--- #define } else { int ih=atoi(tk[4]); if (0H_rol2; if( ntk<5 ) { //--- ID, or plot all --- i1=0; i2=H_rol2_NH; //--- #define } else { int ih=atoi(tk[4]); if (0H_err; if( ntk<5 ) { //--- ID, or plot all --- i1=0; i2=H_err_NH; //--- #define } else { int ih=atoi(tk[4]); if (02 && STREQ(tk[2],"reset")) { int rflg=0; if( ntk<4 ) { hreset0(shmem_ptr->H_rol1, H_rol1_NH); hreset0(shmem_ptr->H_rol2, H_rol2_NH); hreset0(shmem_ptr->H_err, H_err_NH); mreset(&shmem_ptr->h_shm_rol1[0][0],h_shm_rol1_L); mreset(&shmem_ptr->h_shm_rol2[0][0],h_shm_rol2_L); mreset(&shmem_ptr->h_shm_evb[0][0],h_shm_evb_L); mreset(&shmem_ptr->h_shm_err[0][0],h_shm_err_L); } else if(STREQ(tk[3],"rol1")) { hreset0(shmem_ptr->H_rol1, H_rol1_NH); mreset(&shmem_ptr->h_shm_rol1[0][0],h_shm_rol1_L); } else if(STREQ(tk[3],"rol2")) { hreset0(shmem_ptr->H_rol2, H_rol2_NH); mreset(&shmem_ptr->h_shm_rol2[0][0],h_shm_rol2_L); } else if(STREQ(tk[3],"err")) { hreset0(shmem_ptr->H_err, H_err_NH); mreset(&shmem_ptr->h_shm_err[0][0],h_shm_err_L); } else if(STREQ(tk[3],"ROC")) { rflg+=100; } sprintf(&sndstr[strlen(sndstr)]," Reset hist: (rol1/rol2/err) = %3d \n",rflg); //ret=send(cmd_socket, (void *)sndstr, strlen(sndstr), 0); ret=send_rcm_msg(cmd_socket, sndstr); } #else sprintf(&sndstr[strlen(sndstr)],"ROC:: histogram is not compiled in \n"); //ret=send(cmd_socket, (void *)sndstr, strlen(sndstr), 0); ret=send_rcm_msg(cmd_socket, sndstr); #endif } /*------------------------------------------------------------------------------------------*/ if(STREQ(tk[1],"STAT")) { /* get ROC status */ //printf(" >>>>>>>>>>>>>>>>>>>>> RCM STATUS <<<<<<<<<<<<<<<<<<<\n"); get_stat(&sndstr[strlen(sndstr)],sizeof(sndstr)); //ret=send(cmd_socket, (void *)sndstr, strlen(sndstr), 0); ret=send_rcm_msg(cmd_socket, sndstr); //printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n"); } /*------------------------------------------------------------------------------------------*/ if(STREQ(tk[1],"START_CODA_PROC")) { /* Start coda ROC */ char keyname[1024]; sprintf( keyname, "%s/%s", getenv("HOME"), ".daqrc" ); if((KeyFile=fopen(keyname,"r")) == NULL) { printf("Can not open file %s \n",keyname); sprintf(&sndstr[strlen(sndstr)],"ERROR:: Can not open file %s \n",keyname); ret=send_rcm_msg(cmd_socket, sndstr); return 0; } else { char runkey[256]; fgets(runkey,255,KeyFile); fclose(KeyFile); KeyFile=NULL; printf("found key:: %s\n",runkey); int keylen=strlen(runkey)-strlen(strchr(runkey,'\n')); if(strncmp(tk[2],runkey,keylen)) { printf("wrong key |%s| |%s| .\n",tk[2],runkey); sprintf(&sndstr[strlen(sndstr)],"ERROR:: wrong key %s %s \n \n",tk[2],runkey); ret=send_rcm_msg(cmd_socket, sndstr); return 0; } else { printf(" key OK |%s| |%s| .\n",tk[2],runkey); } } char cmd_line[1024]; //printf("\n sndstr:1: %s len=%d \n",sndstr,strlen(sndstr)); sprintf(&sndstr[strlen(sndstr)],"RCM start: %s %s on %s ",tk[3],tk[4],PROC_NAME); //printf("\n sndstr:2: %s len=%d \n",sndstr,strlen(sndstr)); sprintf(cmd_line,"/bin/tcsh -c \" %s \" ",strstr(msg,tk[3]) ); //sprintf(cmd_line," %s ",strstr(msg,tk[3]) ); char *NL=NULL; do { NL = strstr(cmd_line,"\n"); if ( NL!=NULL ) { NL[0]=' '; printf("found new line at pos=%d reststr=%s \n",NL-cmd_line,NL); } } while (NL!=NULL); do { NL = strstr(cmd_line,"\r"); if ( NL!=NULL ) { NL[0]=' '; printf("found new line at pos=%d reststr=%s \n",NL-cmd_line,NL); } } while (NL!=NULL); printf("\n\nsndstr:: %s \n",sndstr); printf("\n--> Exec: %s \n",cmd_line); /* for (int i=1; i>>>>>>>>>>>>>>>>>>>> CMD GETLOG <<<<<<<<<<<<<<<<<<<\n"); int rd_len=200; //--- bytes if( ntk>=3 ) { rd_len=atoi(tk[2]); } int left=LSS-strlen(sndstr)-1; if (rd_len>left) { rd_len=left; printf("rc_client():: cmd=GETLOG:: requested length too large, use=%d\n",rd_len); } printf("Read Log BUF ..... rd_len=%d strlen=%d left=%d ........\n",rd_len,strlen(sndstr),left); int rsize=logbuf->read(log_buf,rd_len); printf("Read Log BUF .done.. rd_len=%d left=%d rsize=%d log len=%d\n",rd_len, left, rsize, strlen(log_buf)); log_buf[rsize]=0; if (rsize==0) { printf("READ buffer error: fill=%d\n",*logbuf->fill_level); } else printf("READ buffer: size=%d fill=%d log=%.80s \n",rsize, logbuf->level(),log_buf); printf("rc_client():: LogBuf wr=%d rd=%d \n",shmem_ptr->LogBuf.wr_ptr,shmem_ptr->LogBuf.rd_ptr); sprintf(mtitle,"%s =============================================================================%s\n",BOLDRED,RESET); sprintf(&mtitle[strlen(mtitle)],"%s ============================> STDOUT from %.10s <=========================%s \n",BOLDRED,PROC_NAME,RESET); sprintf(&mtitle[strlen(mtitle)],"%s ============================================================================= %s\n",BOLDRED,RESET); //sprintf(&msg_header[strlen(msg_header)],":LM%06d:",strlen(mtitle)+strlen(log_buf)+strlen(msg_header)+10+1+2); //sprintf(sndstr,"%s \n%s ",msg_header,mtitle); sprintf(&sndstr[strlen(sndstr)],"%s %s",mtitle,log_buf); //ret=send(cmd_socket, (void *)sndstr, strlen(sndstr), 0); ret=send_rcm_msg(cmd_socket, sndstr); printf("rc_client():: LogBuf sent:: ret=%d msg_len=%d msg=%.60s\n",ret,strlen(sndstr),sndstr); } /*------------------------------------------------------------------------------------------*/ /* accept only VME commands */ if (STREQ(tk[1],"VME")) { #ifndef USEVME sprintf(&sndstr[strlen(sndstr)],"VME: command ignored \n"); //puts(sndstr); ret=send(cmd_socket, (void *)sndstr, strlen(sndstr), 0); ret=send_rcm_msg(cmd_socket, sndstr); return 1; #endif } else { //printf("VME::rc_client():: Not VME Command, Ignored %s \n",msg); return 1; /* Unknown command ignored */ } /*------------------------------------------------------------------------------------------*/ if(STREQ(tk[2],"stat") ) { printf(" >>>>>>>>>>>>>>>>>>>>> RCM CRATE Command <<<<<<<<<<<<<<<<<<<\n"); #ifdef USEVME const char *stype[]={"","F250","F125","F1TDC","TI","CTP","DSC","SD","TS","TD"}; //---- FADC INFO //putchar(0); int nfadc = crate_info(&sndstr[strlen(sndstr)],LSS-strlen(sndstr),fadc250); //fflush(stdout); usleep(10000); fflush(stdout); //if ((LSS-strlen(sndstr))>>>>>>>>>>>>>>>> slot % d of %d >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n",slot,Nfa250); sprintf(&sndstr[strlen(sndstr)],"%.10s:: Crate_SL_%02d SN=%9s F_Rev=0x%05x B_Rev=0x%05x TYPE:%5s\n" ,PROC_NAME, slot, fadc250[slot].SerNum, fadc250[slot].F_REV, fadc250[slot].B_REV,stype[fadc250[slot].TYPE]); if ((LSS-strlen(sndstr))<100) { sprintf(&sndstr[strlen(sndstr)],"STRING TOO SHORT: len=%d\n",strlen(sndstr)); break; } } int slot=21; sprintf(&sndstr[strlen(sndstr)],"%.10s:: TI_SL_%02d SN=%9s Rev=0x%010x TYPE:%5s\n" ,PROC_NAME, slot, fadc250[slot].SerNum, fadc250[slot].F_REV,stype[fadc250[slot].TYPE]); //puts(sndstr); ret=send(cmd_socket, (void *)sndstr, strlen(sndstr), 0); ret=send_rcm_msg(cmd_socket, sndstr); printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n"); #endif } /*------------------------------------------------------------------------------------------*/ if(STREQ(tk[2],"set125offset") || STREQ(tk[2],"s125o") ) { printf(" >>>>>>>>>>>>>>>>>>>>> RCM s125o Command <<<<<<<<<<<<<<<<<<<\n"); #ifdef USEVME int width=100; if (ntk>3) { int offset=atoi(tk[3]); if (ntk>4) width=atoi(tk[4]); putchar(0); int rc=fa125Offset(offset,width); fflush(stdout); usleep(10000); fflush(stdout); strncpy(sndstr,printf_buffer,LSS-strlen(sndstr)); if (rc==0) sprintf(&sndstr[strlen(sndstr)],"Set 125 offset=%d width=%d mode=8, nsb=3 nsa=6 npeak=1\n", offset,width); else sprintf(&sndstr[strlen(sndstr)],"Error Set125 offset=%d \n", offset); } else { sprintf(&sndstr[strlen(sndstr)],"Need parameters: offset width \n"); } #endif //puts(sndstr); ret=send(cmd_socket, (void *)sndstr, strlen(sndstr), 0); ret=send_rcm_msg(cmd_socket, sndstr); } /*------------------------------------------------------------------------------------------*/ if(STREQ(tk[2],"set250offset") || STREQ(tk[2],"s250o") ) { #ifdef USEVME int width=100; if (ntk>3) { int offset=atoi(tk[3]); if (ntk>4) width=atoi(tk[4]); putchar(0); int rc=fa250Offset(offset,width); fflush(stdout); usleep(10000); fflush(stdout); strncpy(sndstr,printf_buffer,LSS-strlen(sndstr)); if (rc==0) sprintf(&sndstr[strlen(sndstr)],"Set250 offset=%d width=%d mode=8 nsb=3 nsa=6 npeak=1\n", offset,width); else sprintf(&sndstr[strlen(sndstr)],"Error Set250 offset=%d \n", offset); } else { sprintf(&sndstr[strlen(sndstr)],"Need parameters: offset width \n"); } #endif //puts(sndstr); ret=send(cmd_socket, (void *)sndstr, strlen(sndstr), 0); ret=send_rcm_msg(cmd_socket, sndstr); } /*------------------------------------------------------------------------------------------*/ if(STREQ(tk[2],"READSLOT") || STREQ(tk[2],"RS") ) { printf(" >>>>>>>>>>>>>>>>>>>>> RCM SLOT Command <<<<<<<<<<<<<<<<<<<\n"); int VAL=0,VALOK=0; if (ntk>3) { VAL=atoi(tk[3]); if (VAL>=0 && VAL<=21) { VALOK=1; // sprintf(&sndstr[strlen(sndstr)],"VME:SLOT=%d accepted\n",VAL); printf("cmd::READSLOT call get_slot() LSS=%d sizeof(sndstr)=%d diff=%d\n",LSS,strlen(sndstr),LSS-strlen(sndstr)); get_slot(&sndstr[strlen(sndstr)],LSS-strlen(sndstr),VAL); } else { sprintf(&sndstr[strlen(sndstr)],"VME:SLOT=%d ignored \n",VAL); } } else { sprintf(&sndstr[strlen(sndstr)],"VME: command ignored \n"); } //puts(sndstr); ret=send(cmd_socket, (void *)sndstr, strlen(sndstr), 0); ret=send_rcm_msg(cmd_socket, sndstr); printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n"); } /*------------------------------------------------------------------------------------------*/ if(STREQ(tk[2],"SET")) { printf(" >>>>>>>>>>>>>>>>>>>>> RCM Set Command <<<<<<<<<<<<<<<<<<<\n"); int VAL=0,VALOK=0; if (ntk>4) { //-- access to shmem -- sem_wait(semid); // SEM if (STREQ(tk[3],"RUNNUM")) { VAL=atoi(tk[4]); if (VAL>=0) { VALOK=1; shmem_ptr->RUN_Number=VAL; get_run_num(shmem_ptr->RUN_Number); sprintf(&sndstr[strlen(sndstr)],"ROC:RUN_Number=%d \n",RUN_Number); } } sem_post(semid); // SEM //-- end of access to shmem -- } puts(sndstr); //ret=send(cmd_socket, (void *)sndstr, strlen(sndstr), 0); sprintf(&sndstr[strlen(sndstr)]," SET %s to %s VALOK=%d\n",tk[3],tk[4],VALOK); //puts(sndstr); ret=send(cmd_socket, (void *)sndstr, strlen(sndstr), 0); ret=send_rcm_msg(cmd_socket, sndstr); printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n"); } /*------------------------------------------------------------------------------------------*/ /*------------------------------------------------------------------------------------------*/ return 0; } //----------------------------------------------------------------------------------------- // CPU usage //----------------------------------------------------------------------------------------- static unsigned long long lastTotalUser, lastTotalUserLow, lastTotalSys, lastTotalIdle; void proc_init(){ FILE* file = fopen("/proc/stat", "r"); fscanf(file, "cpu %Ld %Ld %Ld %Ld", &lastTotalUser, &lastTotalUserLow, &lastTotalSys, &lastTotalIdle); fclose(file); } double getCurrentValue(){ double percent; FILE* file; unsigned long long totalUser, totalUserLow, totalSys, totalIdle, total; file = fopen("/proc/stat", "r"); fscanf(file, "cpu %Ld %Ld %Ld %Ld", &totalUser, &totalUserLow, &totalSys, &totalIdle); fclose(file); if (totalUser < lastTotalUser || totalUserLow < lastTotalUserLow || totalSys < lastTotalSys || totalIdle < lastTotalIdle){ //Overflow detection. Just skip this value. percent = -1.0; } else{ total = (totalUser - lastTotalUser) + (totalUserLow - lastTotalUserLow) + (totalSys - lastTotalSys); percent = total; total += (totalIdle - lastTotalIdle); percent /= total; percent *= 100; } lastTotalUser = totalUser; lastTotalUserLow = totalUserLow; lastTotalSys = totalSys; lastTotalIdle = totalIdle; return percent; } //----------------------------------------------------------------------------------------- // Collect Slot Statistics //----------------------------------------------------------------------------------------- int get_slot(char *sndstr,int lenstr, int slot) { #ifdef USEVME //---- SLOT INFO sprintf(&sndstr[strlen(sndstr)],"%.10s:: =============================> Slot=%d <=================\n",PROC_NAME,slot); printf("get_slot(%s,%d,%d) \n",sndstr,lenstr,slot); int ret = slot_info(sndstr,lenstr,slot); #endif return 0; } //----------------------------------------------------------------------------------------- // Collect Crate Statistics //----------------------------------------------------------------------------------------- int get_stat(char *sndstr,int lenstr) { // int sysinfo(struct sysinfo *info); struct sysinfo memInfo; // Total Virtual Memory: sysinfo (&memInfo); long long totalVirtualMem = memInfo.totalram; //Add other values in next statement to avoid int overflow on right hand side... // totalVirtualMem += memInfo.totalswap; totalVirtualMem *= memInfo.mem_unit; // Virtual Memory currently used: long long virtualMemUsed = memInfo.totalram - memInfo.freeram; //Add other values in next statement to avoid int overflow on right hand side... //virtualMemUsed += memInfo.totalswap - memInfo.freeswap; virtualMemUsed *= memInfo.mem_unit; long long virtualMemFree = memInfo.freeram; virtualMemFree *= memInfo.mem_unit; //printf("MEM:: %lld %llu \n",totalVirtualMem, memInfo.mem_unit); proc_init(); usleep(100000); //-- 100 ms int cpuTotal=(int)getCurrentValue(); float cpu1=memInfo.loads[0]/(float)(1 << SI_LOAD_SHIFT)*100.; float cpu5=memInfo.loads[1]/(float)(1 << SI_LOAD_SHIFT)*100.; float cpu15=memInfo.loads[2]/(float)(1 << SI_LOAD_SHIFT)*100.; //-------------------------------------------------------------- // char *sndstr=shmem_ptr->Message; // int lenstr=LMessage; // static char sndstr[4096]; const int MB=1024*1024; char host_name[80]; gethostname(host_name, 80); //-- access to shmem -- sem_wait(semid); // SEM struct timeval tv1; gettimeofday(&tv1, NULL); time_t time_now=tv1.tv_usec+1000000*tv1.tv_sec; int shmem_error = shmem_ptr->shmem_error; int rol2_hist = shmem_ptr->rol2_hist; sem_post(semid); // SEM //-- end of access to shmem -- #ifdef USE_HIST float Mean_EX = mstati(2,&shmem_ptr->h_shm_rol1[0][0],h_shm_rol1_L1); float Mean_RECV = mstati(2,&shmem_ptr->h_shm_rol2[0][0],h_shm_rol2_L1); float RMS_EX = mstati(3,&shmem_ptr->h_shm_rol1[0][0],h_shm_rol1_L1); float RMS_RECV = mstati(3,&shmem_ptr->h_shm_rol2[0][0],h_shm_rol2_L1); #endif //memset(sndstr, 0, 1); sprintf(&sndstr[strlen(sndstr)],"%.10s:: !----------------> proc_name=%s <------------------ \n",PROC_NAME,host_name); sprintf(&sndstr[strlen(sndstr)],"%.10s:: MEM_total=%5llu MB \n",PROC_NAME,totalVirtualMem/MB); sprintf(&sndstr[strlen(sndstr)],"%.10s:: MEM_used=%5llu MB \n",PROC_NAME,virtualMemUsed/MB); sprintf(&sndstr[strlen(sndstr)],"%.10s:: MEM_free=%5llu MB \n",PROC_NAME,virtualMemFree/MB); sprintf(&sndstr[strlen(sndstr)],"%.10s:: CPU_load=%d \%% 1m(%2.0f\%%) 5m(%2.0f\%%) 15m(%2.0f\%%) \n" ,PROC_NAME,cpuTotal, cpu1, cpu5, cpu15); sprintf(&sndstr[strlen(sndstr)],"%.10s:: shmem_error=%d\n",PROC_NAME,shmem_error); sprintf(&sndstr[strlen(sndstr)],"%.10s:: rol2_hist=%d\n",PROC_NAME,rol2_hist); #ifdef USE_HIST sprintf(&sndstr[strlen(sndstr)],"%.10s:: Shm.Lat.SEND:: Mean=%5.1f usec RMS=%5.1f\n",PROC_NAME,Mean_EX,RMS_EX); sprintf(&sndstr[strlen(sndstr)],"%.10s:: Shm.Lat.RECV:: Mean=%5.1f usec RMS=%5.1f\n",PROC_NAME,Mean_RECV,RMS_RECV); #endif //======================== check CODA processes ========================== std::vector coda_ts = GetPIDbyName("coda_ts") ; std::vector coda_roc = GetPIDbyName("coda_roc") ; std::vector coda_all = GetPIDbyName("coda_") ; std::vector coda_pl = GetPIDbyName("platform") ; std::vector coda_fwu = GetPIDbyName("FirmwareU",0,0); //-- search substring -- std::vector coda_cdaq = GetPIDbyName("event-builder",0,0); //-- search substring -- printf("ts_size=%d roc_size=%d cdaq_size=%d\n", coda_ts.size(), coda_roc.size(), coda_cdaq.size()); std::string tmp; //---- number_of_coda_ts sprintf(&sndstr[strlen(sndstr)],"%.10s:: number_of_coda_ts=%d ",PROC_NAME,coda_ts.size()); for (uint ip=0; iplenstr) printf("Error length of string is %d, max=%d \n",(int)strlen(sndstr),lenstr); else printf("Used length of string is %d, of max=%d \n",(int)strlen(sndstr),lenstr); return 0; } /*====================================================================*/ /* fork2() - like fork, but the new process is immediately orphaned * (won't leave a zombie when it exits) * Returns 1 to the parent, not any meaningful pid. * The parent cannot wait() for the new process (it's unrelated). */ /* This version assumes that you *haven't* caught or ignored SIGCHLD. */ /* If you have, then you should just be using fork() instead anyway. */ int fork2() { pid_t pid; int status; if (!(pid = fork())) { switch (fork()) { case 0: return 0; case -1: _exit(errno); /* assumes all errnos are <256 */ default: _exit(0); } } if (pid < 0 || waitpid(pid,&status,0) < 0) return -1; if (WIFEXITED(status)) if (WEXITSTATUS(status) == 0) return 1; else errno = WEXITSTATUS(status); else errno = EINTR; /* well, sort of :-) */ return -1; } /*====================================================================*/