/********************************************//** * @file UDPStandardImplementation.cpp * @short does all the functions for a receiver, set/get parameters, start/stop etc. ***********************************************/ #include "UDPStandardImplementation.h" #include "moench02ModuleData.h" #include "gotthardModuleData.h" #include "gotthardShortModuleData.h" #include // SIGINT #include // stat #include // socket(), bind(), listen(), accept(), shut down #include // sock_addr_in, htonl, INADDR_ANY #include // exit() #include //set precision #include //munmap #include #include using namespace std; #define EIGER_32BIT_INITIAL_CONSTANT 0x17c UDPStandardImplementation::UDPStandardImplementation() //: //thread_started(0), //eth(NULL), //latestData(NULL), //guiFileName(NULL), //guiFrameNumber(0), //tengigaEnable(0) { thread_started = 0; eth = NULL; latestData = NULL; guiFileName = NULL; guiFrameNumber = NULL; tengigaEnable = 0; for(int i=0;i /proc/sys/net/core/rmem_max")) cout << "\nWARNING: Could not change socket receiver buffer size in file /proc/sys/net/core/rmem_max" << endl; else if(system("echo 250000 > /proc/sys/net/core/netdev_max_backlog")) cout << "\nWARNING: Could not change max length of input queue in file /proc/sys/net/core/netdev_max_backlog" << endl; /** permanent setting heiner net.core.rmem_max = 104857600 # 100MiB net.core.netdev_max_backlog = 250000 sysctl -p // from the manual sysctl -w net.core.rmem_max=16777216 sysctl -w net.core.netdev_max_backlog=250000 */ } void UDPStandardImplementation::configure(map config_map){ FILE_LOG(logWARNING) << __AT__ << " called"; map::const_iterator pos; pos = config_map.find("mode"); if (pos != config_map.end() ){ int b; if(!sscanf(pos->second.c_str(), "%d", &b)){ cout << "Warning: Could not parse mode. Assuming top mode." << endl; b = 0; } bottom = b!= 0; cout << "bottom:"<< bottom << endl; } }; void UDPStandardImplementation::initializeMembers(){ myDetectorType = GENERIC; maxPacketsPerFile = 0; enableFileWrite = 1; overwrite = 1; fileIndex = 0; scanTag = 0; frameIndexNeeded = 0; acqStarted = false; measurementStarted = false; startFrameIndex = 0; frameIndex = 0; packetsCaught = 0; totalPacketsCaught = 0; packetsInFile = 0; startAcquisitionIndex = 0; acquisitionIndex = 0; packetsPerFrame = 0; frameIndexMask = 0; packetIndexMask = 0; frameIndexOffset = 0; acquisitionPeriod = SAMPLE_TIME_IN_NS; numberOfFrames = 0; dynamicRange = 16; shortFrame = -1; currframenum = 0; prevframenum = 0; frameSize = 0; bufferSize = 0; onePacketSize = 0; guiDataReady = 0; nFrameToGui = 0; fifosize = 0; numJobsPerThread = -1; dataCompression = false; numListeningThreads = 1; numWriterThreads = 1; thread_started = 0; currentListeningThreadIndex = -1; currentWriterThreadIndex = -1; for(int i=0;i=0) fileIndex = i; return getFileIndex(); } */ /* int UDPStandardImplementation::setFrameIndexNeeded(int i){ frameIndexNeeded = i; return frameIndexNeeded; } */ /* int UDPStandardImplementation::getEnableFileWrite() const{ return enableFileWrite; } */ /* int UDPStandardImplementation::setEnableFileWrite(int i){ enableFileWrite=i; return getEnableFileWrite(); } */ /* int UDPStandardImplementation::getEnableOverwrite() const{ return overwrite; } */ /* int UDPStandardImplementation::setEnableOverwrite(int i){ overwrite=i; return getEnableOverwrite(); } */ /*other parameters*/ slsReceiverDefs::runStatus UDPStandardImplementation::getStatus() const{ FILE_LOG(logDEBUG) << __AT__ << " called, status: " << status; return status; } void UDPStandardImplementation::initialize(const char *detectorHostName){ if(strlen(detectorHostName)) strcpy(detHostname,detectorHostName); } char *UDPStandardImplementation::getDetectorHostname() const{ return (char*)detHostname; } void UDPStandardImplementation::setEthernetInterface(char* c){ FILE_LOG(logDEBUG) << __AT__ << " called"; strcpy(eth,c); } void UDPStandardImplementation::setUDPPortNo(int p){ FILE_LOG(logDEBUG) << __AT__ << " called"; server_port[0] = p; } void UDPStandardImplementation::setUDPPortNo2(int p){ FILE_LOG(logDEBUG) << __AT__ << " called"; server_port[1] = p; } int UDPStandardImplementation::getNumberOfFrames() const { return numberOfFrames; } int32_t UDPStandardImplementation::setNumberOfFrames(int32_t fnum){ FILE_LOG(logDEBUG) << __AT__ << " called"; if(fnum >= 0) numberOfFrames = fnum; return getNumberOfFrames(); } int UDPStandardImplementation::getScanTag() const{ return scanTag; } int32_t UDPStandardImplementation::setScanTag(int32_t stag){ FILE_LOG(logDEBUG) << __AT__ << " called"; if(stag >= 0) scanTag = stag; return getScanTag(); } int UDPStandardImplementation::getDynamicRange() const{ return dynamicRange; } int32_t UDPStandardImplementation::setDynamicRange(int32_t dr){ FILE_LOG(logDEBUG) << __AT__ << " called"; cout << "Setting Dynamic Range" << endl; int olddr = dynamicRange; if(dr >= 0){ dynamicRange = dr; if(myDetectorType == EIGER){ if(!tengigaEnable) packetsPerFrame = EIGER_ONE_GIGA_CONSTANT * dynamicRange * EIGER_MAX_PORTS; else packetsPerFrame = EIGER_TEN_GIGA_CONSTANT * dynamicRange * EIGER_MAX_PORTS; frameSize = onePacketSize * packetsPerFrame; bufferSize = (frameSize/EIGER_MAX_PORTS) + EIGER_HEADER_LENGTH;//everything one port gets (img header plus packets) maxPacketsPerFile = EIGER_MAX_FRAMES_PER_FILE * packetsPerFrame; if(olddr != dr){ //del if(thread_started){ createListeningThreads(true); createWriterThreads(true); } for(int i=0;i=0){ nFrameToGui = i; setupFifoStructure(); } return nFrameToGui; } int64_t UDPStandardImplementation::setAcquisitionPeriod(int64_t index){ FILE_LOG(logDEBUG) << __AT__ << " called"; if(index >= 0){ if(index != acquisitionPeriod){ acquisitionPeriod = index; setupFifoStructure(); } } return acquisitionPeriod; } bool UDPStandardImplementation::getDataCompression(){ FILE_LOG(logDEBUG) << __AT__ << " called"; return dataCompression;} int UDPStandardImplementation::enableDataCompression(bool enable){ FILE_LOG(logDEBUG) << __AT__ << " called"; cout << "Data compression "; if(enable) cout << "enabled" << endl; else cout << "disabled" << endl; #ifdef MYROOT1 cout << " WITH ROOT" << endl; #else cout << " WITHOUT ROOT" << endl; #endif //delete filter for the current number of threads deleteFilter(); dataCompression = enable; pthread_mutex_lock(&status_mutex); writerthreads_mask = 0x0; pthread_mutex_unlock(&(status_mutex)); createWriterThreads(true); if(enable) numWriterThreads = MAX_NUM_WRITER_THREADS; else numWriterThreads = 1; if(createWriterThreads() == FAIL){ cprintf(BG_RED,"ERROR: Could not create writer threads\n"); return FAIL; } setThreadPriorities(); if(enable) setupFilter(); return OK; } /*other functions*/ void UDPStandardImplementation::deleteFilter(){ FILE_LOG(logDEBUG) << __AT__ << " called"; int i; cmSub=NULL; for(i=0;i(receiverdata[i], csize, sigma, sign, cmSub); } //LEO: it is not clear to me.. void UDPStandardImplementation::setupFifoStructure(){ FILE_LOG(logDEBUG) << __AT__ << " called"; int64_t i; int oldn = numJobsPerThread; //if every nth frame mode if(nFrameToGui) numJobsPerThread = nFrameToGui; //random nth frame mode else{ if(!acquisitionPeriod) i = SAMPLE_TIME_IN_NS; else i = SAMPLE_TIME_IN_NS/acquisitionPeriod; if (i > MAX_JOBS_PER_THREAD) numJobsPerThread = MAX_JOBS_PER_THREAD; else if (i < 1) numJobsPerThread = 1; else numJobsPerThread = i; } //if same, return if(oldn == numJobsPerThread) return; if(myDetectorType == EIGER) numJobsPerThread = 1; //otherwise memory too much if numjobsperthread is at max = 1000 fifosize = GOTTHARD_FIFO_SIZE; if(myDetectorType == MOENCH) fifosize = MOENCH_FIFO_SIZE; else if(myDetectorType == EIGER) fifosize = EIGER_FIFO_SIZE; if(fifosize % numJobsPerThread) fifosize = (fifosize/numJobsPerThread)+1; else fifosize = fifosize/numJobsPerThread; cout << "Number of Frames per buffer:" << numJobsPerThread << endl; cout << "Fifo Size:" << fifosize << endl; /* //for testing numJobsPerThread = 3; fifosize = 11; */ for(int i=0;iisEmpty()) fifoFree[i]->pop(buffer[i]); #ifdef FIFO_DEBUG //cprintf(GREEN,"%d fifostructure popped from fifofree %x\n", i, (void*)(buffer[i])); #endif delete fifoFree[i]; } if(fifo[i]) delete fifo[i]; if(mem0[i]) free(mem0[i]); fifoFree[i] = new CircularFifo(fifosize); fifo[i] = new CircularFifo(fifosize); //allocate memory mem0[i]=(char*)malloc((bufferSize * numJobsPerThread + HEADER_SIZE_NUM_TOT_PACKETS)*fifosize); /** shud let the client know about this */ if (mem0[i]==NULL){ cout<<"++++++++++++++++++++++ COULD NOT ALLOCATE MEMORY FOR LISTENING !!!!!!!+++++++++++++++++++++" << endl; exit(-1); } buffer[i]=mem0[i]; //push the addresses into freed fifoFree and writingFifoFree while (buffer[i]<(mem0[i]+(bufferSize * numJobsPerThread + HEADER_SIZE_NUM_TOT_PACKETS)*(fifosize-1))) { fifoFree[i]->push(buffer[i]); #ifdef FIFO_DEBUG cprintf(BLUE,"%d fifostructure free pushed into fifofree %x\n", i, (void*)(buffer[i])); #endif buffer[i]+=(bufferSize * numJobsPerThread + HEADER_SIZE_NUM_TOT_PACKETS); } } cout << "Fifo structure(s) reconstructed" << endl; } /** acquisition functions */ void UDPStandardImplementation::readFrame(char* c,char** raw, uint32_t &fnum, uint32_t &startAcquisitionIndex, uint32_t &startFrameIndex){ FILE_LOG(logDEBUG) << __AT__ << " called"; //point to gui data if (guiData == NULL){ guiData = latestData; #ifdef VERY_VERY_DEBUG cout << "gui data not null anymore" << endl; #endif } //copy data and filename strcpy(c,guiFileName); fnum = guiFrameNumber; startAcquisitionIndex = getStartAcquisitionIndex(); startFrameIndex = getStartFrameIndex(); //could not get gui data if(!guiDataReady){ #ifdef VERY_VERY_DEBUG cout << "gui data not ready" << endl; #endif *raw = NULL; } //data ready, set guidata to receive new data else{ #ifdef VERY_VERY_DEBUG cout << "gui data ready" << endl; #endif *raw = guiData; guiData = NULL; /*pthread_mutex_lock(&dataReadyMutex); WHY WAS THIS HERE IN THE FIRST PLACE guiDataReady = 0; pthread_mutex_unlock(&dataReadyMutex);*/ if((nFrameToGui) && (writerthreads_mask)){ #ifdef VERY_VERY_DEBUG cout << "gonna post" << endl; #endif /*if(nFrameToGui){*/ //release after getting data sem_post(&smp); } #ifdef VERY_VERY_DEBUG cout << "done post" << endl; #endif } } void UDPStandardImplementation::copyFrameToGui(char* startbuf[], uint32_t fnum, char* buf){ FILE_LOG(logDEBUG) << __AT__ << " called"; #ifdef VERY_VERY_DEBUG cout << "copyframe" << endl; #endif //random read when gui not ready , also command line doesnt have nthframetogui //else guidata always null as guidataready is always 1 after 1st frame, and seccond data never gets copied if((!nFrameToGui) && (!guiData)){ #ifdef VERY_VERY_DEBUG cout << "doing nothing" << endl; #endif pthread_mutex_lock(&dataReadyMutex); guiDataReady=0; pthread_mutex_unlock(&dataReadyMutex); } //random read or nth frame read, gui needs data now or it is the first frame else{ #ifdef VERY_VERY_DEBUG cout << "gui needs data now or 1st frame" << endl; #endif pthread_mutex_lock(&dataReadyMutex); guiDataReady=0; #ifdef VERY_VERY_DEBUG cout << "guidataready is 0, copying data" << endl; #endif //eiger if(startbuf != NULL){ int offset = 0; int size = frameSize/EIGER_MAX_PORTS; for(int j=0;jgetErrorStatus(); if(!iret) cout << "UDP port opened at port " << port[i] << endl; else{ #ifdef VERBOSE cprintf(BG_RED,"Could not create UDP socket on port %d error: %d\n", port[i], iret); #endif return FAIL; } } return OK; } int UDPStandardImplementation::shutDownUDPSockets(){ FILE_LOG(logDEBUG) << __AT__ << " called"; for(int i=0;iShutDownSocket(); delete udpSocket[i]; udpSocket[i] = NULL; } } return OK; } // TODO: add a destroyListeningThreads int UDPStandardImplementation::createListeningThreads(bool destroy){ FILE_LOG(logDEBUG) << __AT__ << " called"; int i; void* status; killAllListeningThreads = 0; pthread_mutex_lock(&status_mutex); listeningthreads_mask = 0x0; pthread_mutex_unlock(&(status_mutex)); FILE_LOG(logDEBUG) << "Starting " << __func__ << endl; if(!destroy){ //start listening threads cout << "Creating Listening Threads(s)"; currentListeningThreadIndex = -1; for(i = 0; i < numListeningThreads; ++i){ sem_init(&listensmp[i],1,0); thread_started = 0; currentListeningThreadIndex = i; if(pthread_create(&listening_thread[i], NULL,startListeningThread, (void*) this)){ cout << "Could not create listening thread with index " << i << endl; return FAIL; } while(!thread_started); cout << "."; cout << flush; } #ifdef VERBOSE cout << "Listening thread(s) created successfully." << endl; #else cout << endl; #endif }else{ cout<<"Destroying Listening Thread(s)"<initEventTree(temp, &iframe); //resets the pedestalSubtraction array and the commonModeSubtraction singlePhotonDet[ithr]->newDataSet(); if(myFile[ithr]==NULL){ cout<<"file null"<IsOpen()){ cout<<"file not open"< DO_NOTHING){ //close if(sfilefd){ fclose(sfilefd); sfilefd = NULL; } //open file if(!overwrite){ if (NULL == (sfilefd = fopen((const char *) (savefilename), "wx"))){ cprintf(BG_RED,"Error: Could not create new file %s\n",savefilename); return FAIL; } }else if (NULL == (sfilefd = fopen((const char *) (savefilename), "w"))){ cprintf(BG_RED,"Error: Could not create file %s\n",savefilename); return FAIL; } //setting buffer setvbuf(sfilefd,NULL,_IOFBF,BUF_SIZE); //printing packet losses and file names if(!packetsCaught) cout << savefilename << endl; else{ cout << savefilename << "\tpacket loss " << setw(4)<GetCurrentFile(); if(myFile[ithr]->Write()) //->Write(tall->GetName(),TObject::kOverwrite); cout << "Thread " << ithr <<": wrote frames to file" << endl; else cout << "Thread " << ithr << ": could not write frames to file" << endl; }else cout << "Thread " << ithr << ": could not write frames to file: No file or No Tree" << endl; //close file if(myTree[ithr] && myFile[ithr]) myFile[ithr] = myTree[ithr]->GetCurrentFile(); if(myFile[ithr] != NULL) myFile[ithr]->Close(); myFile[ithr] = NULL; myTree[ithr] = NULL; pthread_mutex_unlock(&write_mutex); #endif } } /** * Pre: * Post: eiger req. time for 32bit before acq start * */ int UDPStandardImplementation::startReceiver(char message[]){ FILE_LOG(logDEBUG) << __AT__ << " called"; int i; // #ifdef VERBOSE cout << "Starting Receiver" << endl; //#endif //reset listening thread variables measurementStarted = false; //should be set to zero as its added to get next start frame indices for scans for eiger if(!acqStarted) currframenum = 0; startFrameIndex = 0; for(int i = 0; i < numListeningThreads; ++i) totalListeningFrameCount[i] = 0; //udp socket if(createUDPSockets() == FAIL){ strcpy(message,"Could not create UDP Socket(s).\n"); cout << endl << message << endl; return FAIL; } cout << "UDP socket(s) created successfully." << endl; if(setupWriter() == FAIL){ //stop udp socket shutDownUDPSockets(); sprintf(message,"Could not create file %s.\n",savefilename); return FAIL; } cout << "Successfully created file(s)" << endl; //done to give the gui some proper name instead of always the last file name if(dataCompression) sprintf(savefilename, "%s/%s_fxxx_%d_xx.root", filePath,fileName,fileIndex); //initialize semaphore sem_init(&smp,1,0); //status pthread_mutex_lock(&status_mutex); status = RUNNING; for(i=0;istartListening(); return this_pointer; } void* UDPStandardImplementation::startWritingThread(void* this_pointer){ FILE_LOG(logDEBUG) << __AT__ << " called"; ((UDPStandardImplementation*)this_pointer)->startWriting(); return this_pointer; } int UDPStandardImplementation::startListening(){ FILE_LOG(logDEBUG) << __AT__ << " called"; int ithread = currentListeningThreadIndex; #ifdef VERYVERBOSE cout << "In startListening() " << endl; #endif thread_started = 1; int total; int lastpacketoffset, expected, rc,packetcount, maxBufferSize, carryonBufferSize; uint32_t lastframeheader;// for moench to check for all the packets in last frame char* tempchar = NULL; int imageheader = 0; if(myDetectorType==EIGER) imageheader = EIGER_IMAGE_HEADER_SIZE; while(1){ //variables that need to be checked/set before each acquisition carryonBufferSize = 0; //if more than 1 listening thread, listen one packet at a time, else need to interleaved frame later maxBufferSize = bufferSize * numJobsPerThread; #ifdef VERYDEBUG cout << " maxBufferSize:" << maxBufferSize << ",carryonBufferSize:" << carryonBufferSize << endl; #endif if(tempchar) {delete [] tempchar;tempchar = NULL;} if(myDetectorType != EIGER) tempchar = new char[onePacketSize * ((packetsPerFrame/numListeningThreads) - 1)]; //gotthard: 1packet size, moench:39 packet size while((1<pop(buffer[ithread]); #ifdef FIFO_DEBUG cprintf(GREEN,"%d listener popped from fifofree %x\n", ithread, (void*)(buffer[ithread])); #endif //receive if(udpSocket[ithread] == NULL){ rc = 0; cout << ithread << "UDP Socket is NULL" << endl; } //normal listening else if(!carryonBufferSize){ /* if(!ithread){*/ rc = udpSocket[ithread]->ReceiveDataOnly(buffer[ithread] + HEADER_SIZE_NUM_TOT_PACKETS, maxBufferSize); //cout<<"value:"<fnum)<> frameIndexOffset)<ReceiveDataOnly((buffer[ithread] + HEADER_SIZE_NUM_TOT_PACKETS + carryonBufferSize),maxBufferSize - carryonBufferSize); expected = maxBufferSize - carryonBufferSize; } #ifdef EIGER_DEBUG cout << ithread << " *** rc:" << dec << rc << ". expected:" << dec << expected << endl; #endif /* //start indices for each start of scan/acquisition - eiger does it before if((!measurementStarted) && (rc > 0) && (!ithread)) startFrameIndices(ithread); */ //problem in receiving or end of acquisition if((rc < expected)||(rc <= 0)){ if(myDetectorType != EIGER){ //start indices for each start of scan/acquisition - this should be done earlier for normal detectors if((!measurementStarted) && (rc > 0) && (!ithread)) startFrameIndices(ithread); } stopListening(ithread,rc,packetcount,total); continue; } ///* //eiger - start indices for each start of scan/acquisition - this should be done after to ignore first incomplete frames if((!measurementStarted) && (rc > 0) && (!ithread)) startFrameIndices(ithread); //*/ //reset packetcount = (packetsPerFrame/numListeningThreads) * numJobsPerThread; carryonBufferSize = 0; //check if last packet valid and calculate packet count switch(myDetectorType){ case MOENCH: lastpacketoffset = (((numJobsPerThread * packetsPerFrame - 1) * onePacketSize) + HEADER_SIZE_NUM_TOT_PACKETS); #ifdef VERYDEBUG cout <<"first packet:"<< ((((uint32_t)(*((uint32_t*)(buffer[ithread]+HEADER_SIZE_NUM_TOT_PACKETS))))) & (packetIndexMask)) << endl; cout <<"first header:"<< (((((uint32_t)(*((uint32_t*)(buffer[ithread]+HEADER_SIZE_NUM_TOT_PACKETS))))) & (frameIndexMask)) >> frameIndexOffset) << endl; cout << "last packet offset:" << lastpacketoffset << endl; cout <<"last packet:"<< ((((uint32_t)(*((uint32_t*)(buffer[ithread]+lastpacketoffset))))) & (packetIndexMask)) << endl; cout <<"last header:"<< (((((uint32_t)(*((uint32_t*)(buffer[ithread]+lastpacketoffset))))) & (frameIndexMask)) >> frameIndexOffset) << endl; #endif //moench last packet value is 0 if( ((((uint32_t)(*((uint32_t*)(buffer[ithread]+lastpacketoffset))))) & (packetIndexMask))){ lastframeheader = ((((uint32_t)(*((uint32_t*)(buffer[ithread]+lastpacketoffset))))) & (frameIndexMask)) >> frameIndexOffset; carryonBufferSize += onePacketSize; lastpacketoffset -= onePacketSize; --packetcount; while (lastframeheader == (((((uint32_t)(*((uint32_t*)(buffer[ithread]+lastpacketoffset))))) & (frameIndexMask)) >> frameIndexOffset)){ carryonBufferSize += onePacketSize; lastpacketoffset -= onePacketSize; --packetcount; } memcpy(tempchar, buffer[ithread]+(lastpacketoffset+onePacketSize), carryonBufferSize); #ifdef VERYDEBUG cout << "tempchar header:" << (((((uint32_t)(*((uint32_t*)(tempchar))))) & (frameIndexMask)) >> frameIndexOffset) << endl; cout <<"tempchar packet:"<< ((((uint32_t)(*((uint32_t*)(tempchar))))) & (packetIndexMask)) << endl; #endif } break; case GOTTHARD: if(shortFrame == -1){ lastpacketoffset = (((numJobsPerThread * packetsPerFrame - 1) * onePacketSize) + HEADER_SIZE_NUM_TOT_PACKETS); #ifdef VERYDEBUG cout << "last packet offset:" << lastpacketoffset << endl; #endif if((unsigned int)(packetsPerFrame -1) != ((((uint32_t)(*((uint32_t*)(buffer[ithread]+lastpacketoffset))))+1) & (packetIndexMask))){ memcpy(tempchar,buffer[ithread]+lastpacketoffset, onePacketSize); #ifdef VERYDEBUG cout << "tempchar header:" << (((((uint32_t)(*((uint32_t*)(tempchar))))+1) & (frameIndexMask)) >> frameIndexOffset) << endl; #endif carryonBufferSize = onePacketSize; --packetcount; } } #ifdef VERYDEBUG cout << "header:" << (((((uint32_t)(*((uint32_t*)(buffer[ithread] + HEADER_SIZE_NUM_TOT_PACKETS))))+1) & (frameIndexMask)) >> frameIndexOffset) << endl; #endif break; default: break; } // cout<<"*********** "<fnum)<push(buffer[ithread])); #ifdef FIFO_DEBUG //if(!ithread) cprintf(RED, "%d listener pushed into fifo %x\n",ithread, (void*)(buffer[ithread])); #endif } sem_wait(&listensmp[ithread]); //make sure its not exiting thread if(killAllListeningThreads){ cout << ithread << " good bye listening thread" << endl; if(tempchar) {delete [] tempchar;tempchar = NULL;} pthread_exit(NULL); } if(tempchar) {delete [] tempchar;tempchar = NULL;} } return OK; } int UDPStandardImplementation::startWriting(){ FILE_LOG(logDEBUG) << __AT__ << " called"; int ithread = currentWriterThreadIndex; #ifdef VERYVERBOSE cout << ithread << "In startWriting()" <pop(wbuf[i]); #ifdef FIFO_DEBUG cprintf(MAGENTA,"%d writer poped from fifo %x\n", ithread, (void*)(wbuf[i])); #endif numpackets = (uint16_t)(*((uint16_t*)wbuf[i])); #ifdef VERYDEBUG cout << i << " numpackets:" << dec << numpackets << "for fifo :"<< i << endl; #endif } //last dummy packet if(numpackets == 0xFFFF){ #ifdef VERYDEBUG cout << "**LAST dummy packet" << endl; #endif stopWriting(ithread,wbuf); continue; } #ifdef VERYDEBUG else cout <<"**NOT a dummy packet"<fnum); else tempframenum = htonl(*(unsigned int*)((eiger_image_header32 *)((char*)(wbuf[ithread] + HEADER_SIZE_NUM_TOT_PACKETS)))->fnum); tempframenum += (startFrameIndex-1); //eiger frame numbers start at 1, so need to -1 //tempframenum = ((tempframenum / EIGER_32BIT_INITIAL_CONSTANT) + startFrameIndex)-1;//eiger 32 bit mode is a multiple of 17c. +startframeindex for scans }else if ((myDetectorType == GOTTHARD) && (shortFrame == -1)) tempframenum = (((((uint32_t)(*((uint32_t*)(wbuf[ithread] + HEADER_SIZE_NUM_TOT_PACKETS))))+1)& (frameIndexMask)) >> frameIndexOffset); else tempframenum = ((((uint32_t)(*((uint32_t*)(wbuf[ithread] + HEADER_SIZE_NUM_TOT_PACKETS))))& (frameIndexMask)) >> frameIndexOffset); if(numWriterThreads == 1) currframenum = tempframenum; else{ pthread_mutex_lock(&progress_mutex); if(tempframenum > currframenum) currframenum = tempframenum; pthread_mutex_unlock(&progress_mutex); } #ifdef EIGER_DEBUG2 cout << endl < 0){ for(j=0;jnum1)) = currframenum; //for 32 bit,port number needs to be changed and packet number reconstructed if(dynamicRange == 32){ for (i = 0; i < packetsPerFrame/4; i++){ //new packet number that has space for 16 bit (*(uint16_t*)(((eiger_packet_header *)((char*)(wbuf[j] + totalheader + EIGER_ONE_GIGA_ONE_PACKET_SIZE*i)))->num2)) = ((*(uint8_t*)(((eiger_packet_header *)((char*)(wbuf[j] + totalheader + EIGER_ONE_GIGA_ONE_PACKET_SIZE*i)))->num4))); //new port number as its the same everywhere for 32 bit!! if(!j) (*(uint8_t*)(((eiger_packet_header *)((char*)(wbuf[j] + totalheader + EIGER_ONE_GIGA_ONE_PACKET_SIZE*i)))->num3)) = 0x00; #ifdef VERYDEBUG cprintf(RED, "%d - 0x%x - %d - %d\n", i, (*(uint8_t*)(((eiger_packet_header *)((char*)(wbuf[j] + totalheader +i*EIGER_ONE_GIGA_ONE_PACKET_SIZE)))->num3)), (*(uint8_t*)(((eiger_packet_header *)((char*)(wbuf[j] + totalheader +i*EIGER_ONE_GIGA_ONE_PACKET_SIZE)))->num4)), (*(uint16_t*)(((eiger_packet_header *)((char*)(wbuf[j] + totalheader +i*EIGER_ONE_GIGA_ONE_PACKET_SIZE)))->num2))); #endif } for (i = packetsPerFrame/4; i < packetsPerFrame/2; i++){ //new packet number that has space for 16 bit (*(uint16_t*)(((eiger_packet_header *)((char*)(wbuf[j] + totalheader + EIGER_ONE_GIGA_ONE_PACKET_SIZE*i)))->num2)) = ((*(uint8_t*)(((eiger_packet_header *)((char*)(wbuf[j] + totalheader + EIGER_ONE_GIGA_ONE_PACKET_SIZE*i)))->num4))+(packetsPerFrame/4)); //new port number as its the same everywhere for 32 bit!! if(!j) (*(uint8_t*)(((eiger_packet_header *)((char*)(wbuf[j] + totalheader + EIGER_ONE_GIGA_ONE_PACKET_SIZE*i)))->num3)) = 0x00; #ifdef VERYDEBUG cprintf(RED, "%d -0x%x - %d - %d\n", i, (*(uint8_t*)(((eiger_packet_header *)((char*)(wbuf[j] + totalheader +i*EIGER_ONE_GIGA_ONE_PACKET_SIZE)))->num3)), (*(uint8_t*)(((eiger_packet_header *)((char*)(wbuf[j] + totalheader +i*EIGER_ONE_GIGA_ONE_PACKET_SIZE)))->num4)), (*(uint16_t*)(((eiger_packet_header *)((char*)(wbuf[j] + totalheader +i*EIGER_ONE_GIGA_ONE_PACKET_SIZE)))->num2))); #endif } } } #endif writeToFile_withoutCompression(wbuf[j], numpackets,currframenum); } #ifdef VERYDEBUG cout << "written everyting" << endl; #endif } if(myDetectorType == EIGER) { #ifdef VERYDEBUG cout << "gonna copy frame" << endl; #endif copyFrameToGui(wbuf,currframenum); #ifdef VERYDEBUG cout << "copied frame" << endl; #endif for(i=0;ipush(wbuf[i])); #ifdef FIFO_DEBUG cprintf(BLUE,"%d writer freed pushed into fifofree %x for listener %d\n",ithread, (void*)(wbuf[i]),i); #endif } } else{ //copy to gui if(numpackets >= packetsPerFrame){//min 1 frame, but neednt be //if(numpackets == packetsPerFrame * numJobsPerThread){ //only full frames copyFrameToGui(NULL,-1,wbuf[0]+HEADER_SIZE_NUM_TOT_PACKETS); #ifdef VERYVERBOSE cout << ithread << " finished copying" << endl; #endif }//else cout << "unfinished buffersize" << endl; while(!fifoFree[0]->push(wbuf[0])); #ifdef FIFO_DEBUG cprintf(BLUE,"%d writer freed pushed into fifofree %x for listener 0\n",ithread, (void*)(wbuf[0])); #endif } } //data compression else handleDataCompression(ithread,wbuf,numpackets,d, xmax, ymax, nf); } #ifdef VERYVERBOSE cout << ithread << " gonna wait for 1st sem" << endl; #endif //wait sem_wait(&writersmp[ithread]); if(killAllWritingThreads){ cout << ithread << " good bye writing thread" << endl; closeFile(ithread); pthread_exit(NULL); } #ifdef VERYVERBOSE cout << ithread << " got 1st post" << endl; #endif if((1<fnum); else startFrameIndex = htonl(*(unsigned int*)((eiger_image_header *)((char*)(buffer[ithread] + HEADER_SIZE_NUM_TOT_PACKETS)))->fnum); } //gotthard has +1 for frame number and not a short frame else if ((myDetectorType == GOTTHARD) && (shortFrame == -1)) startFrameIndex = (((((uint32_t)(*((uint32_t*)(buffer[ithread] + HEADER_SIZE_NUM_TOT_PACKETS))))+1) & (frameIndexMask)) >> frameIndexOffset); else startFrameIndex = ((((uint32_t)(*((uint32_t*)(buffer[ithread]+HEADER_SIZE_NUM_TOT_PACKETS)))) & (frameIndexMask)) >> frameIndexOffset); //start of acquisition if(!acqStarted){ startAcquisitionIndex=startFrameIndex; currframenum = startAcquisitionIndex; acqStarted = true; cout << "startAcquisitionIndex:" << hex << startAcquisitionIndex<push(buffer[ithread]); #ifdef FIFO_DEBUG cprintf(BLUE,"%d listener not txm free pushed into fifofree %x\n", ithread,(void*)(buffer[ithread])); #endif exit(-1); } //free buffer if(rc <= 0){ cout << ithread << "Discarding empty frame/ End of acquisition" << endl; fifoFree[ithread]->push(buffer[ithread]); #ifdef FIFO_DEBUG cprintf(BLUE,"%d listener empty buffer pushed into fifofree %x\n", ithread, (void*)(buffer[ithread])); #endif } //push the last buffer into fifo else{ //eiger (incomplete frames) - throw away if((myDetectorType == EIGER) && (rc < (bufferSize * numJobsPerThread)) ){ if(rc == 266240) cprintf(GREEN, "%d Start of detector: Received test frame of 266240 bytes.\n",ithread); cout << ithread << "Discarding incomplete frame" << endl; fifoFree[ithread]->push(buffer[ithread]); #ifdef FIFO_DEBUG cprintf(BLUE,"%d listener last buffer free pushed into fifofree %x\n", ithread,(void*)(buffer[ithread])); #endif } //eiger (complete frames) + other detectors else{ pc = (rc/onePacketSize); #ifdef VERYDEBUG cout << ithread << " last rc:"<push(buffer[ithread])); #ifdef FIFO_DEBUG cprintf(RED,"%d listener last buffer pushed into fifo %x\n", ithread,(void*)(buffer[ithread])); #endif } } //push dummy buffer to all writer threads for(i=0;ipop(buffer[ithread]); #ifdef FIFO_DEBUG cprintf(GREEN,"%d listener popped dummy buffer from fifofree %x\n", ithread,(void*)(buffer[ithread])); #endif (*((uint16_t*)(buffer[ithread]))) = 0xFFFF; #ifdef VERYDEBUG cout << ithread << " dummy buffer num packets:"<< (*((uint16_t*)(buffer[ithread]))) << endl; #endif while(!fifo[ithread]->push(buffer[ithread])); #ifdef FIFO_DEBUG cprintf(RED,"%d listener pushed dummy buffer into fifo %x\n", ithread,(void*)(buffer[ithread])); #endif } //reset mask and exit loop pthread_mutex_lock(&status_mutex); listeningthreads_mask^=(1< 1) cout << "Waiting for listening to be done.. current mask:" << hex << listeningthreads_mask << endl; #endif while(listeningthreads_mask) usleep(5000); #ifdef VERYDEBUG t = 0; for(i=0;ipush(wbuffer[i])); #ifdef FIFO_DEBUG cprintf(BLUE,"%d writer free dummy pushed into fifofree %x for listener %d\n", ithread,(void*)(wbuffer[i]),i); #endif } //all threads need to close file, reset mask and exit loop closeFile(ithread); pthread_mutex_lock(&status_mutex); writerthreads_mask^=(1<num1))); cprintf(RED, "p1:0x%x\n", (*(uint8_t*)(((eiger_packet_header *)((char*)(buf + offset+k*1040)))->num3))); cprintf(RED, "p0 num:%d - %d\n", k, (*(uint8_t*)(((eiger_packet_header *)((char*)(buf + offset+k*1040)))->num4))); k = 1; cprintf(RED, "p2 fnum:0x%x\n", (*(unsigned int*)(((eiger_packet_header *)((char*)(buf + offset +k*1040)))->num1))); cprintf(RED, "p2:0x%x\n", (*(uint8_t*)(((eiger_packet_header *)((char*)(buf + offset +k*1040)))->num3))); cprintf(RED, "p1 num:%d - %d\n", k,(*(uint8_t*)(((eiger_packet_header *)((char*)(buf + offset +k*1040)))->num4))); k = 2; cprintf(RED, "p3 fnum:0x%x\n", (*(unsigned int*)(((eiger_packet_header *)((char*)(buf + offset + k*1040)))->num1))); cprintf(RED, "p3:0x%x\n", (*(uint8_t*)(((eiger_packet_header *)((char*)(buf + offset +k*1040)))->num3))); cprintf(RED, "p2 num:%d - %d\n", k,(*(uint8_t*)(((eiger_packet_header *)((char*)(buf + offset +k*1040)))->num4))); }else{ k = 0; cprintf(RED, "\np1 fnum:0x%x\n", (*(unsigned int*)(((eiger_packet_header *)((char*)(buf + offset+k*1040)))->num1))); cprintf(RED, "p1:0x%x\n", (*(uint8_t*)(((eiger_packet_header *)((char*)(buf + offset+k*1040)))->num3))); cprintf(RED, "p0 num:%d - %d\n", k, (*(uint16_t*)(((eiger_packet_header *)((char*)(buf + offset+k*1040)))->num2))); k = 1; cprintf(RED, "p2 fnum:0x%x\n", (*(unsigned int*)(((eiger_packet_header *)((char*)(buf + offset +k*1040)))->num1))); cprintf(RED, "p2:0x%x\n", (*(uint8_t*)(((eiger_packet_header *)((char*)(buf + offset +k*1040)))->num3))); cprintf(RED, "p1 num:%d - %d\n", k, (*(uint16_t*)(((eiger_packet_header *)((char*)(buf + offset +k*1040)))->num2))); k = 2; cprintf(RED, "p3 fnum:0x%x\n", (*(unsigned int*)(((eiger_packet_header *)((char*)(buf + offset + k*1040)))->num1))); cprintf(RED, "p3:0x%x\n", (*(uint8_t*)(((eiger_packet_header *)((char*)(buf + offset +k*1040)))->num3))); cprintf(RED, "p2 num:%d - %d\n", k, (*(uint16_t*)(((eiger_packet_header *)((char*)(buf + offset +k*1040)))->num2))); k = 256; cprintf(RED, "p257 fnum:0x%x\n", (*(unsigned int*)(((eiger_packet_header *)((char*)(buf + offset + k*1040)))->num1))); cprintf(RED, "p257:0x%x\n", (*(uint8_t*)(((eiger_packet_header *)((char*)(buf + offset +k*1040)))->num3))); cprintf(RED, "p256 num:%d - %d\n", k, (*(uint16_t*)(((eiger_packet_header *)((char*)(buf + offset +k*1040)))->num2))); k = 512; cprintf(RED, "p513 fnum:0x%x\n", (*(unsigned int*)(((eiger_packet_header *)((char*)(buf + offset + k*1040)))->num1))); cprintf(RED, "p513:0x%x\n", (*(uint8_t*)(((eiger_packet_header *)((char*)(buf + offset +k*1040)))->num3))); cprintf(RED, "p512 num:%d - %d\n", k, (*(uint16_t*)(((eiger_packet_header *)((char*)(buf + offset +k*1040)))->num2))); k = 768; cprintf(RED, "p769 fnum:0x%x\n", (*(unsigned int*)(((eiger_packet_header *)((char*)(buf + offset + k*1040)))->num1))); cprintf(RED, "p769:0x%x\n", (*(uint8_t*)(((eiger_packet_header *)((char*)(buf + offset +k*1040)))->num3))); cprintf(RED, "p768 num:%d - %d\n", k,(*(uint16_t*)(((eiger_packet_header *)((char*)(buf + offset +k*1040)))->num2))); } #endif #endif } while(numpackets > 0){ //for progress and packet loss calculation(new files) if(myDetectorType == EIGER); else if ((myDetectorType == GOTTHARD) && (shortFrame == -1)) tempframenum = (((((uint32_t)(*((uint32_t*)(buf + HEADER_SIZE_NUM_TOT_PACKETS))))+1)& (frameIndexMask)) >> frameIndexOffset); else tempframenum = ((((uint32_t)(*((uint32_t*)(buf + HEADER_SIZE_NUM_TOT_PACKETS))))& (frameIndexMask)) >> frameIndexOffset); if(numWriterThreads == 1) currframenum = tempframenum; else{ if(tempframenum > currframenum) currframenum = tempframenum; } #ifdef VERYDEBUG cout << "tempframenum:" << dec << tempframenum << " curframenum:" << currframenum << endl; #endif //lock if(numWriterThreads > 1) pthread_mutex_lock(&write_mutex); //to create new file when max reached packetsToSave = maxPacketsPerFile - packetsInFile; if(packetsToSave > numpackets) packetsToSave = numpackets; /**next time offset is still plus header length*/ fwrite(buf+offset, 1, packetsToSave * onePacketSize, sfilefd); packetsInFile += packetsToSave; packetsCaught += packetsToSave; totalPacketsCaught += packetsToSave; #ifdef VERYDEBUG cout << "/totalPacketsCaught:" << dec << totalPacketsCaught <= maxPacketsPerFile){ //for packet loss lastpacket = (((packetsToSave - 1) * onePacketSize) + offset); if(myDetectorType == EIGER); else if ((myDetectorType == GOTTHARD) && (shortFrame == -1)) tempframenum = (((((uint32_t)(*((uint32_t*)(buf + lastpacket))))+1)& (frameIndexMask)) >> frameIndexOffset); else tempframenum = ((((uint32_t)(*((uint32_t*)(buf + lastpacket))))& (frameIndexMask)) >> frameIndexOffset); if(numWriterThreads == 1) currframenum = tempframenum; else{ if(tempframenum > currframenum) currframenum = tempframenum; } #ifdef VERYDEBUG cout << "tempframenum:" << dec << tempframenum << " curframenum:" << currframenum << endl; #endif //create createNewFile(); } //unlock if(numWriterThreads > 1) pthread_mutex_unlock(&write_mutex); offset += (packetsToSave * onePacketSize); numpackets -= packetsToSave; } } else{ if(numWriterThreads > 1) pthread_mutex_lock(&write_mutex); packetsInFile += numpackets; packetsCaught += numpackets; totalPacketsCaught += numpackets; if(numWriterThreads > 1) pthread_mutex_unlock(&write_mutex); } } void UDPStandardImplementation::handleDataCompression(int ithread, char* wbuffer[], int &npackets, char* data, int xmax, int ymax, int &nf){ FILE_LOG(logDEBUG) << __AT__ << " called"; #if defined(MYROOT1) && defined(ALLFILE_DEBUG) writeToFile_withoutCompression(wbuf[0], numpackets,currframenum); #endif eventType thisEvent = PEDESTAL; int ndata; char* buff = 0; data = wbuffer[0]+ HEADER_SIZE_NUM_TOT_PACKETS; int remainingsize = npackets * onePacketSize; int np; int once = 0; double tot, tl, tr, bl, br; int xmin = 1, ymin = 1, ix, iy; while(buff = receiverdata[ithread]->findNextFrame(data,ndata,remainingsize)){ np = ndata/onePacketSize; //cout<<"buff framnum:"<> frameIndexOffset)<newFrame(); //only for moench if(commonModeSubtractionEnable){ for(ix = xmin - 1; ix < xmax+1; ix++){ for(iy = ymin - 1; iy < ymax+1; iy++){ thisEvent = singlePhotonDet[ithread]->getEventType(buff, ix, iy, 0); } } } for(ix = xmin - 1; ix < xmax+1; ix++) for(iy = ymin - 1; iy < ymax+1; iy++){ thisEvent=singlePhotonDet[ithread]->getEventType(buff, ix, iy, commonModeSubtractionEnable); if (nf>1000) { tot=0; tl=0; tr=0; bl=0; br=0; if (thisEvent==PHOTON_MAX) { receiverdata[ithread]->getFrameNumber(buff); //iFrame=receiverdata[ithread]->getFrameNumber(buff); #ifdef MYROOT1 myTree[ithread]->Fill(); //cout << "Fill in event: frmNr: " << iFrame << " ix " << ix << " iy " << iy << " type " << thisEvent << endl; #else pthread_mutex_lock(&write_mutex); if((enableFileWrite) && (sfilefd)) singlePhotonDet[ithread]->writeCluster(sfilefd); pthread_mutex_unlock(&write_mutex); #endif } } } nf++; #ifndef ALLFILE pthread_mutex_lock(&progress_mutex); packetsInFile += packetsPerFrame; packetsCaught += packetsPerFrame; totalPacketsCaught += packetsPerFrame; if(packetsInFile >= maxPacketsPerFile) createNewFile(); pthread_mutex_unlock(&progress_mutex); #endif if(!once){ copyFrameToGui(NULL,-1,buff); once = 1; } } remainingsize -= ((buff + ndata) - data); data = buff + ndata; if(data > (wbuffer[0] + HEADER_SIZE_NUM_TOT_PACKETS + npackets * onePacketSize) ) cprintf(BG_RED,"ERROR SHOULD NOT COME HERE, Error 142536!\n"); } while(!fifoFree[0]->push(wbuffer[0])); #ifdef FIFO_DEBUG cprintf(BLUE,"%d writer compression free pushed into fifofree %x for listerner 0\n", ithread, (void*)(wbuffer[0])); #endif } int UDPStandardImplementation::enableTenGiga(int enable){ FILE_LOG(logDEBUG) << __AT__ << " called"; cout << "Enabling 10Gbe to" << enable << endl; int oldtengiga = tengigaEnable; if(enable >= 0){ tengigaEnable = enable; if(myDetectorType == EIGER){ if(!tengigaEnable){ packetsPerFrame = EIGER_ONE_GIGA_CONSTANT * dynamicRange * EIGER_MAX_PORTS; onePacketSize = EIGER_ONE_GIGA_ONE_PACKET_SIZE; maxPacketsPerFile = EIGER_MAX_FRAMES_PER_FILE * packetsPerFrame; }else{ packetsPerFrame = EIGER_TEN_GIGA_CONSTANT * dynamicRange * EIGER_MAX_PORTS; onePacketSize = EIGER_TEN_GIGA_ONE_PACKET_SIZE; maxPacketsPerFile = EIGER_MAX_FRAMES_PER_FILE * packetsPerFrame*4; } frameSize = onePacketSize * packetsPerFrame; bufferSize = (frameSize/EIGER_MAX_PORTS) + EIGER_HEADER_LENGTH;//everything one port gets (img header plus packets) //maxPacketsPerFile = EIGER_MAX_FRAMES_PER_FILE * packetsPerFrame; cout<<"packetsPerFrame:"<