diff --git a/slsDetectorSoftware/slsReceiver/slsReceiverFunctionList.cpp b/slsDetectorSoftware/slsReceiver/slsReceiverFunctionList.cpp index 867f8d6cc..61213346d 100644 --- a/slsDetectorSoftware/slsReceiver/slsReceiverFunctionList.cpp +++ b/slsDetectorSoftware/slsReceiver/slsReceiverFunctionList.cpp @@ -20,13 +20,10 @@ #include // socket(), bind(), listen(), accept(), shut down #include // sock_addr_in, htonl, INADDR_ANY #include // exit() - #include //set precision +#include //munmap -//#include //sched_idle -//#include //posix_fadvice - #include #include using namespace std; @@ -248,7 +245,7 @@ int slsReceiverFunctionList::startReceiver(){ cout << "ERROR: Could not prioritize threads. You need to be super user for that." << endl; if (pthread_setschedparam(writing_thread, policy, &write_param) == EPERM) cout << "ERROR: Could not prioritize threads. You need to be super user for that." << endl; - if (pthread_setschedparam(pthread_self(),policy , &tcp_param) == EPERM) + if (pthread_setschedparam(pthread_self(),5 , &tcp_param) == EPERM) cout << "ERROR: Could not prioritize threads. You need to be super user for that." << endl; @@ -319,8 +316,9 @@ int slsReceiverFunctionList::startListening(){ // very end of the program. do { - if (strchr(eth,'.')!=NULL) strcpy(eth,""); + //creating udp socket + if (strchr(eth,'.')!=NULL) strcpy(eth,""); if(!strlen(eth)){ cout<<"warning:eth is empty.listening to all"<getErrorStatus()){ #ifdef VERBOSE std::cout<< "Could not create UDP socket "<< server_port << std::endl; @@ -344,7 +341,7 @@ int slsReceiverFunctionList::startListening(){ fifofree->pop(buffer); //receiver 2 half frames / 1 short frame / 40 moench frames - rc = udpSocket->ReceiveDataOnly(buffer,bufferSize);//sizeof(buffer)); + rc = udpSocket->ReceiveDataOnly(buffer,bufferSize); if( rc < 0) cerr << "recvfrom() failed" << endl; @@ -413,6 +410,9 @@ int slsReceiverFunctionList::startWriting(){ cout << "In startWriting()" <0){ - sfilefd = fopen((const char *) (savefilename), "w"); - cout << savefilename << endl; - //posix_fadvise(fileno(sfilefd),0,0,POSIX_FADV_DONTNEED|POSIX_FADV_SEQUENTIAL); - } - - + cout << "Ready!" << endl; while(listening_thread_running || (!fifo->isEmpty())){ - //when it reaches maxFramesPerFile,start writing new file - if (framesInFile == maxFramesPerFile) { + //start a new file + if ((framesInFile == maxFramesPerFile) || (strlen(savefilename) == 0)){ //create file name if(frameIndexNeeded==-1) sprintf(savefilename, "%s/%s_%d.raw", filePath,fileName,fileIndex); else sprintf(savefilename, "%s/%s_f%012d_%d.raw", filePath,fileName,framesCaught,fileIndex); - //start writing in new file if(enableFileWrite || cbAction>0){ - //fsync(fileno(sfilefd)); - fclose(sfilefd); - sfilefd = fopen((const char *) (savefilename), "w"); - //posix_fadvise(fileno(sfilefd),0,0,POSIX_FADV_DONTNEED|POSIX_FADV_SEQUENTIAL); + //sync file and close fd + if(sfilefd) { + msync(address,memsize, MS_ASYNC); + munmap(address,memsize); + fclose(sfilefd); + } + + //create file , truncate size and map file to memory + if (NULL == (sfilefd = fopen((const char *) (savefilename), "w+"))){ + cout << "Error: Could not create file " << savefilename << endl; + break; + } + if (-1 == ftruncate(fileno(sfilefd),memsize)) { + perror("Error:Could not truncate file:"); + break; + } + address = mmap(NULL,memsize,PROT_READ|PROT_WRITE,MAP_SHARED,fileno(sfilefd),0); + if(address == MAP_FAILED) + perror("Error: Could not map file to memory:"); } - //currframenum=(int)(*((int*)latestData)); - cout << savefilename << "\tpacket loss " << fixed << setprecision(4) << ((currframenum-prevframenum-(packetsPerFrame*framesInFile))/(double)(packetsPerFrame*framesInFile))*100.000 << "%\t\t" - "framenum " << currframenum << "\t\t" - "p " << prevframenum << endl; + //printing packet losses and file names + if(prevframenum == 0) + cout << savefilename << endl; + else{ + cout << savefilename << "\tpacket loss " << fixed << setprecision(4) << ((currframenum-prevframenum-(packetsPerFrame*framesInFile))/(double)(packetsPerFrame*framesInFile))*100.000 << "%\t\t" + "framenum " << currframenum << "\t\t" + "p " << prevframenum << endl; - prevframenum=currframenum; - framesInFile = 0; + prevframenum=currframenum; + framesInFile = 0; + } } - //actual writing from fifo + //pop fifo if(!fifo->isEmpty()){ if(fifo->pop(wbuf)){ framesCaught++; totalFramesCaught++; - currframenum = (int)(*((int*)wbuf));//cout<<"curreframenm:"<buffer; fifofree->push(wbuf); } - // delete dataWriteFrame; } else{//cout<<"************************fifo empty**********************************"<