int main() { char *eof; char line[100]; int customer; cshmInit(); while(1) { printf("l cust u cust h[elp] p[rint] q[uit]:"); eof=fgets(line, 100, stdin); if(eof==NULL) break; if(line[0]=='q') break; if(line[0]=='l') { customer= atoi(&line[2]); lockBakery(&ctpshmbase->swtriggers, customer); } else if(line[0]=='u') { customer= atoi(&line[2]); unlockBakery(&ctpshmbase->swtriggers, customer); } else if(line[0]=='p') { printBakery(&ctpshmbase->swtriggers); } else if(line[0]=='h') { printf("Now l/u/p concerns swtriggers. bakery alg. used for\n\ 2 groups of tasks (initialised in main_ctp.c):\n\ initBakery(swtriggers,4): 0:SOD/EOD 1:gcalib 2:ctp.exe 3:dims\n\ initBakery(ccread,5): 0:proxy 1:dims 2:ctp+busytool 3:smaq 4:inputs\n\ "); } else { continue;
/*------------------------------------*/ int main(int argc, char **argv) { int rc,ads,ix; char msg[100]; /* if(argc<3) { printf("Usage: ltuserver LTU_name base\n\ where LTU_name is detector name\n\ base is the base address of LTU (e.g. 0x811000)\n\ "); exit(8); }; if(argc>1) { if(strcmp(argv[1],"no1min")==0) { dimsflags=dimsflags | NO1MINFLAG; }; };*/ setlinebuf(stdout); signal(SIGUSR1, gotsignal); siginterrupt(SIGUSR1, 0); signal(SIGQUIT, gotsignal); siginterrupt(SIGQUIT, 0); signal(SIGINT, gotsignal); siginterrupt(SIGINT, 0); signal(SIGBUS, gotsignal); siginterrupt(SIGBUS, 0); sprintf(msg, "gcalib starting, ver 2 %s %s...", __DATE__, __TIME__); prtLog(msg); rc= vmeopen("0x820000", "0xd000"); if(rc!=0) { printf("vmeopen CTP vme:%d\n", rc); exit(8); }; cshmInit(); unlockBakery(&ctpshmbase->swtriggers,swtriggers_gcalib); initDET(); // has to be after cshmInit() DFS= new DataFiles(); checkCTP(); printf("No initCTP. initCTP left to be done by main ctp-proxy when started\n"); //initCTP(); registerDIM(); beammode= get_DIMW32("CTPDIM/BEAMMODE"); //cannot be used inside callback ads= shmupdateDETs(); // added from 18.11.2010 if(ads>0){ if(threadactive==0) { startThread(); } else { printf("ads:%d but threadactive is 1 at the start", ads); //cannot happen }; }; #ifdef SIMVME printf("srand(73), (SIMVME)...\n"); srand(73); #endif printDETS(); while(1) { /* the activity of calthread is checked here: if threadactive==1 & heartbeat did not change, the calthread is not active in spite of threadactive is claiming it is active! */ if(threadactive==1) { if(heartbeat == last_heartbeat) { prtLog("ERROR: heartbeat is quiet, setting threadactive to 0."); threadactive=0; }; }; //printf("sleeping 40secs...\n"); last_heartbeat= heartbeat; /*dtq_sleep(2); */ sleep(40); // should be more than max. cal.trig period (33s for muon_trg) /*if(detectfile("gcalrestart", 0) >=0) { char msg[200]; sprintf(msg,"gcalrestart exists"); prtLog(msg); system("rm gcalrestart"); sprintf(msg,"main: gcalrestart removed, exiting..."); prtLog(msg); quit=8; }; */ if(quit>0) break; beammode= get_DIMW32("CTPDIM/BEAMMODE"); //cannot be used inside callback //ds_update(); }; sprintf(msg, "Exiting gcalib. quit:%d...\n",quit); prtLog(msg); stopDIM(); // stop all active dets: for(ix=0; ix<NDETEC; ix++) { rc= delDET(ix); if(rc!=0) { printf("delDET(%d) rc:%d", ix, rc); }; }; delete DFS; cshmDetach(); vmeclose(); exit(0); }
/* ------------------------------------------------------------ GenSwtrg orbitn[0]: orbit number read just before the first trigger generation, i.e. it is always <= of ORBITID of the sw. trigger event orbitn[1,2]: tsec tusec -ONLY IN CASE OF 'c' trigger ! RC: number of L2a successfully generated, or 12345678: cal. triggers stopped because det. is not in global run or 0xffffffxx: where xx is flag (see above). ONLY IN CASE of one 'c' trigger */ int GenSwtrg(int ntriggers,char trigtype, int roc, w32 BC,w32 detectors, int customer, w32 *orbitn ){ int flag,itr=0; int l0=0,l1=0,l2a=0,l2r=0, lm=0; w32 status, orbitnloc[3]; if(l0C0()) { status=vmer32(L0_TCSTATUS); } else { status=vmer32(L0_TCSTATUS); }; if((status&0x10)==0x10){ printf(" GenSwtrg: TC busy. L0 TC_STATUS:0x%x\n", status); return 0; } if(detectors & 0x20000) { printf("GenSwtrg: CTP (ECS number 17) cannot be sw triggered, bit17 removed\n"); detectors= detectors & (~0x20000); }; if(trigtype=='c') { w32 det2runn[NDETEC]; // not used here, just to satisfy next call... status= cshmGlobalDets(det2runn); //if(strcmp("ALICE", getenv("VMESITE"))==0) if(strcmp("ALICE", "ALICE")==0) { if((status & detectors)!=detectors) { printf("GenSwtrg: calibrated dets:%x but dets in global run(s):%x\n", detectors,status); return 12345678; //magic used in ctp/testclass.py }; } else { status= detectors; if(msgpres==0) { printf("Presence of dets in glob. run not checked!!!\n"); msgpres++; }; }; }; lockBakery(&ctpshmbase->swtriggers, customer); if( setswtrig(trigtype,roc,BC,detectors)!=0) { l2a=0; flag=11; goto RELEASERET; }; while(((itr<ntriggers) && ((flag=startswtrig(orbitnloc))))){ if(itr==0) { *orbitn= orbitnloc[0]; if(trigtype == 'c') { // also time in case of cal. trigger orbitn[1]= orbitnloc[1]; orbitn[2]= orbitnloc[2]; }; }; if(flag == 1)l0++; else if(flag == 2)l1++; else if(flag == 3)l2r++; else if(flag == 4)l2a++; else if(flag ==10)lm++; else { printf(" GenSwtrg: unexpected flag %i\n",flag); goto RELEASERET; } itr++; //usleep(60000000); }; if(DBGswtrg3==1) { int ifo; for(ifo=0;ifo<NFO;ifo++){ // set all FOs always //printf("FO:%d\n",ifo); if((notInCrate(ifo+FO1BOARD)==0)) { w32 vmeaddr,val; vmeaddr= FO_TESTCLUSTER+BSP*(ifo+1); val=vmer32(vmeaddr); //if((DBGswtrg3==1)&&(val!=0)) printf("setswtrig FOr:%d Raddr: 0x%x data: 0x%x\n", if(val!=ifoglob[ifo]) printf("ERROR setswtrig FOr:%d Raddr: 0x%x dataw r: 0x%x 0x%x\n", ifo, vmeaddr, ifoglob[ifo], val); } }; }; RELEASERET: unlockBakery(&ctpshmbase->swtriggers, customer); if(DBGswtrg4) { printf(" GenSwtrg: %i %c-triggers generated for detectors:0x%x.\n", itr,trigtype, detectors); printf("flag lm/l0,l1,l2r,l2a: %d %i %i %i %i \n",flag,l0,l1,l2r,l2a); }; TRIGTYPE='.'; if((trigtype=='c') && (ntriggers==1)) { // 1 calib. event, i.e. special RC: 0xffffff00 + flag // flag 0x4: ok -full cal. event successfully generated // see startswtrig() for possible flags return(0xffffff00 | flag); } else { return l2a; }; }
/*----------------------------------------*/ int main(int argc, char **argv) { int rc; int ssmcr_tries=0; infolog_SetFacility("CTP"); infolog_SetStream("",0); #ifdef PQWAY printf("main_ctp: opening rec/send queues...\n"); mq_rec= pq_open(); if(mq_rec==(mqd_t)-1) { infolog_trgboth(LOG_FATAL, "posix mq_rec not created"); exit(8); } mq_sendmsg= pq_connect(); if(mq_sendmsg==(mqd_t)-1) { infolog_trgboth(LOG_FATAL, "posix mq_sendmsg not connected"); exit(8); } #endif signal(SIGUSR1, gotsignal); siginterrupt(SIGUSR1, 0); signal(SIGQUIT, gotsignal); siginterrupt(SIGQUIT, 0); signal(SIGKILL, gotsignal); siginterrupt(SIGKILL, 0); // -9 signal(SIGTERM, gotsignal); siginterrupt(SIGTERM, 0); // kill pid signal(SIGINT, gotsignal); siginterrupt(SIGINT, 0); // CTRL C 2 partmode[0]='\0'; printf("cshmInit i.e. initBakery(swtriggers/ccread/ssmcr ONLY once, when shm allocated)...\n"); /*printf("initBakery(swtriggers,4): 0:SOD/EOD 1:gcalib 2:ctp.exe 3:dims\n"); printf("initBakery(ccread,5): 0:proxy 1:dims 2:ctp+busytool 3:smaq 4:inputs\n"); */ cshmInit(); setglobalflags(argc, argv); /* changed in aug2016 (initBakery only once from now, when shm allocated) printf("initBakery(swtriggers,4): 0:SOD/EOD 1:gcalib 2:ctp.exe 3:dims\n"); initBakery(&ctpshmbase->swtriggers, "swtriggers", swtriggers_N); printf("initBakery(ccread,6): 0:proxy 1:dims 2:ctp+busytool 3:smaq 4:inputs 5:orbitddl2\n"); initBakery(&ctpshmbase->ccread, "ccread", ccread_N); printf("initBakery(ssmcr,4): 0:smaq 1:orbitddl2 2:ctp 3:inputs\n"); initBakery(&ctpshmbase->ssmcr, "ssmcr", ssmcr_N); */ printBakery(&ctpshmbase->swtriggers); printBakery(&ctpshmbase->ccread); printBakery(&ctpshmbase->ssmcr); unlockBakery(&ctpshmbase->swtriggers,swtriggers_ctpproxy); unlockBakery(&ctpshmbase->ccread,ccread_ctpproxy); unlockBakery(&ctpshmbase->ssmcr,ssmcr_ctpproxy); /* Let' synchronise with smcr only, i.e. - ccread_dims,... can go in parallel with ctp_Initproxy, orbitddl2: should use ccread_orbitddl2 customer when reading counters - swtriggers_gcalib/_dims cannot appear (no global runs becasue ctpproxy being restarted) */ while(1) { // do not allow SSM usage (smaq) because ctp_Initproxy is initialising it char msg[100]; rc= lockBakeryTimeout(&ctpshmbase->ssmcr,ssmcr_ctpproxy, 10); if(rc==1) { if(ssmcr_tries>0) { sprintf(msg, "Got ssmcr resource after %d attempts", ssmcr_tries); infolog_trgboth(LOG_INFO, msg); }; break; // ssmcr reserved for me now }; ssmcr_tries++; sprintf(msg,"%d secs Waiting for ssmcr resource. Is smaq stopped?", ssmcr_tries*10); infolog_trgboth(LOG_WARNING, msg); }; if(isArg(argc, argv, "configrunset")) { /* do we need before orbitddl2.py INT/L2 orbit in SYNC (automatic with L2a)? */ int rc, reslen; char cmd[200]; char result[1000]=""; // ~ 10 lines char orbchanged[]="Warning: orbitoffset changed:"; if(envcmp("VMESITE", "PRIVATE")==0) { strcpy(cmd, "who"); } else { strcpy(cmd, "orbitddl2.py configrunset"); }; infolog_trgboth(LOG_INFO, "Starting L0 orbit calibration (30s...)"); rc= popenread(cmd, result, 1000); // opens vme... reslen= strlen(result); if((rc==EXIT_FAILURE) || (reslen<=1)) { infolog_trgboth(LOG_ERROR, "L0 orbit calibration problem"); } else { int ixr= reslen; if(result[ixr-1]=='\n') { //remove last NL character if present result[ixr-1]='\0'; ixr= reslen-2; } else { ixr= reslen-1; // pointer to the last non-NEWLINE character }; while(ixr>=0) { // find last line if(result[ixr]=='\n') { ixr++; break; }; ixr--; }; sprintf(cmd, "L0 orbit calibration: %s", &result[ixr]); if((strcmp(&result[ixr], "Everything ok")==0) || (strncmp(&result[ixr], orbchanged, strlen(orbchanged))==0) ) { infolog_trgboth(LOG_INFO, cmd); } else { infolog_trgboth(LOG_ERROR, cmd); infolog_trgboth(LOG_ERROR, "ctpproxy not started"); unlockBakery(&ctpshmbase->ssmcr,ssmcr_ctpproxy); rc=8; goto STP; }; }; }; // init CTP after calibration (e.g. to repair modifications done by orbitddl2.py) rc=ctp_Initproxy(); unlockBakery(&ctpshmbase->ssmcr,ssmcr_ctpproxy); if(rc!=0) goto STP; // DIM services not registered here (see ctpdims.c), they run in separae task: // ds_register(); strcpy(obj,argv[1]); smi_attach(obj, SMI_handle_command); printf("CTP attached to: %s\n",obj); /* if smi_volatile: stops the ctp_proxy in case TRIGGER domain is down smi_volatile(); */ strcpy(errorReason,"not set"); smi_set_parER(); strcpy(ORBIT_NUMBER,""); smi_set_par("ORBIT_NUMBER",ORBIT_NUMBER,STRING); smi_setState("RUNNING"); while(1) { #ifdef PQWAY executectp("wait"); #else usleep(1000000); #endif if(quit>9) break; }; rc= ctp_Endproxy(); STP: #ifdef PQWAY pq_close(mq_sendmsg, 0); pq_close(mq_rec, 1); #endif printf("Calling cshmDetach()...\n"); cshmDetach(); return (rc); }