source: Sophya/trunk/AddOn/TAcq/racqurw.cc@ 3635

Last change on this file since 3635 was 3635, checked in by ansari, 16 years ago

Amelioration/ correction diverses, introduction du programme de lecture / traitement multi-thread mcrd.cc - Reza 26/05/2009

File size: 15.0 KB
RevLine 
[3537]1//----------------------------------------------------------------
2// ---- classes de threads pour lecture (transfert DMA)
3// et ecriture disque pour acquisition BAORadio -----
4// LAL - R. Ansari - Juin/Juillet 2008
5//----------------------------------------------------------------
6
7#include "racqurw.h"
8
9#include <stdlib.h>
10#include <unistd.h>
11#include <fstream>
[3623]12#include <signal.h>
[3537]13#include "pexceptions.h"
14#include "timestamp.h"
15
16#include "pciewrap.h"
17#include "brpaqu.h"
18#include "minifits.h"
19
[3623]20#include "resusage.h" // Pour mesure temps elapsed/CPU ...
21#include "datatable.h" // Pour sauver les entetes de paquet
22#include <sys/time.h> // pour gettimeofday
[3537]23//-------------------------------------------------------
24// Classe thread de lecture PCI-Express
25//-------------------------------------------------------
26
[3628]27PCIEReader::PCIEReader(PCIEWrapperInterface &pciw,uint_4 sizeFrame,uint_4 packSize ,RAcqMemZoneMgr& mem,
[3623]28 uint_4 nmax, BRDataFmtConv swapall)
29 : memgr(mem) , pciw_ (pciw)
[3537]30{
[3623]31 nmax_ = nmax;
32 swapall_ = swapall; // select data swap/format conversion for BRPaquet
33 stop_ = false;
34 packSize_ = packSize;
35 sizeFr_ =sizeFrame;
36 // Pour la logique de gestion des paquets ds zone memoire
37 mid_ = -2;
38 targ_npaq_ = 0;
39
40 max_targ_npaq = memgr.NbPaquets();
41 mmbuf_ = NULL;
[3537]42}
43
[3623]44bool PCIEReader::MZoneManage(bool fgclean) // Retourne true si probleme
45{
46 /* Pour debug
47 cout << " PCIEReader::MZoneManage() mid_=" << mid_ << " arg_npaq_= " << targ_npaq_
48 << " max_targ_npaq=" << max_targ_npaq << endl;
49 */
50 if (mid_ >= 0) memgr.FreeMemZone(mid_, MemZS_Filled);
51 mmbuf_ = NULL; targ_npaq_ = 0; mid_ = -2;
52 if (fgclean) return false;
53 mid_ = memgr.FindMemZoneId(MemZA_Fill);
54 mmbuf_ = memgr.GetMemZone(mid_);
55 if (mmbuf_==NULL) return true;
56 return false;
57}
58
[3537]59void PCIEReader::run()
60{
[3623]61 struct sigaction act;
62 struct sigaction old_act;
63 //Precision insuffisante ResourceUsage ru; ru.Update(); // Pour recuperer le temps passe
64 struct timeval tv1, tv2;
65 gettimeofday(&tv1, NULL);
66
67 try{
68 cout << " PCIEReader::run() - Starting , NMaxMemZones=" << nmax_
69 << "memgr.NbPaquets()=" << memgr.NbPaquets() << endl;
[3537]70 setRC(1);
[3623]71
72 uint_4 cpt=0;
73 // sigaddset(&act.sa_mask,SIGINT); // pour proteger le transfert DMA
74 //sigaction(SIGINT,&act,NULL);
75
[3537]76 uint_4 paqsz = memgr.PaqSize();
[3623]77 uint_4 dmasz = pciw_.TransferSize();
78
79 Byte* Datas = NULL;
80 Byte* tampon = new Byte[paqsz];
81 Byte* nextpaq = NULL;
82
83 uint_4 off_acheval = 0;
84
85 int nerrdma = 0;
86 int maxerrdma = 10;
87 bool fgarret = false;
88
89 uint_4 npaqfait = 0; // Nb total de paquets traites (DMA + decode)
90
91 /// do{ si boucle infini
92 // for (uint_4 kmz=0; kmz<nmax_; kmz++) {
93 while (npaqfait < nmax_*memgr.NbPaquets()) {
94 if (stop_) break;
95 if (fgarret) break;
96 // On pointe vers le debut de la zone a remplir aver le prochain DMA
97 Datas=pciw_.DoTransferData();
98 if (Datas == NULL) { // No data Read in DMA
99 nerrdma ++;
100 cout << "PCIEReaderChecker/Erreur Waiting for datas ..." << endl;
101 pciw_.PrintStatus(cout);
102 if (nerrdma>=maxerrdma) { fgarret = true; break; }
103 }
104 else { // DMA reussi
105 uint_4 curoff = 0;
106 //1- On traite le paquet a cheval, rempli partiellement avec le DMA d'avant si necessaire
107 if (off_acheval > 0) {
108 if ((paqsz-off_acheval)< dmasz) {
109 memcpy((void *)(tampon+off_acheval), (void *)Datas, paqsz-off_acheval);
110 curoff = paqsz-off_acheval; off_acheval = 0;
111
112 if ((nextpaq=NextPaqTarget()) == NULL) {
113 cout << "2 PCIEReader::run()/Error NextPaqTarget() returned NULL ->STOP 9" << endl;
114 setRC(9); fgarret=true; break;
115 }
116 BRPaquet paq(tampon, nextpaq, paqsz, swapall_);
117 npaqfait++; // Ne pas oublier le compteur de paquets faits
118 }
119 else {
120 memcpy((void *)(tampon+off_acheval), (void *)Datas, dmasz);
121 curoff =dmasz;
122 off_acheval = (dmasz+off_acheval);
123 }
124 }
125 //2- On traite les paquets complet qui se trouvent dans la zone du DMA
126 while((curoff+paqsz)<=dmasz) {
127 // BRPaquet paq((Byte*)(Datas)+((paqsz*j)), nextdma+j*paqsz, paqsz, swapall_);
128 if ((nextpaq=NextPaqTarget()) == NULL) {
129 cout << "3 PCIEReader::run()/Error NextPaqTarget() returned NULL ->STOP 9" << endl;
130 setRC(9); fgarret=true; break;
131 }
132 BRPaquet paq(Datas+curoff, nextpaq, paqsz, swapall_);
133 curoff += paqsz; // On avance l'index dans le buffer du DMA
134 npaqfait++; // Ne pas oublier le compteur de paquets faits
135 } // -- FIN traitement des paquets complets ds un DMA
136 //3- On copie si besoin la fin du DMA dans la zone tampon
137 if (curoff < dmasz) {
138 if (fgarret) break; // pour sortir si l'on est passe par un STOP 9
139 off_acheval = dmasz-curoff;
140 memcpy(tampon, (void*)(Datas+curoff), off_acheval);
141 curoff += off_acheval;
142 }
143 } // Traitement d'un DMA OK
144 }
145// }while(!stop_);
146
147
148 gettimeofday(&tv2, NULL);
149 double tmelaps2 = (tv2.tv_sec-tv1.tv_sec)*1000.+(tv2.tv_usec-tv1.tv_usec)/1000.;
150 if (tmelaps2<0.1) tmelaps2=0.1;
151 cout << " ------------------ PCIEReader::run()-End summary -------------------" << endl;
152 cout << " PCIEReader/Info TotTransfer=" << pciw_.TotTransferBytes()/1024
153 << " kb , ElapsTime=" << tmelaps2 << " ms ->"
154 << (double)pciw_.TotTransferBytes()/tmelaps2 << " kb/s" << endl;
155 cout << " --------------------------------------------------------------------" << endl;
156
157 MZoneManage(true);
158 delete [] tampon;
159
160 }catch (PException& exc) {
161 cout << " PCIEREADER::run()/catched PException " << exc.Msg() << endl;
162 setRC(3);
163 return;
164 }
165 catch(...) {
166 cout << " PCIEREADER::run()/catched unknown ... exception " << endl;
167 setRC(4);
168 return;
169 }
[3537]170 setRC(0);
[3623]171
[3537]172 return;
173}
174
175
[3623]176void PCIEReader::Stop()
177{
178 // cout << " PCIEReader::Stop() -------------> STOP" <<endl;
179 stop_ = true;
180 return;
181}
[3537]182
[3623]183
[3537]184//-------------------------------------------------------
185// Classe thread de sauvegarde sur fichiers
186//-------------------------------------------------------
187
[3623]188DataSaver::DataSaver(RAcqMemZoneMgr& mem, string& path, uint_4 nfiles, uint_4 nblocperfile, bool savesig)
[3537]189 : memgr(mem)
190{
[3623]191 nfiles_ = nfiles;
192 nblocperfile_ = nblocperfile;
193 nmax_ = nblocperfile_*nfiles_;
194 savesig_ = savesig; // Si false, pas d'ecriture des fichiers FITS du signal
[3537]195 stop_ = false;
196 path_ = path;
197}
[3623]198void DataSaver::Stop()
199{
200 // cout<< " DataSaver:Stop ........ " << endl;
201 stop_=true;
[3537]202
[3623]203}
[3537]204void DataSaver::run()
205{
206 setRC(1);
[3623]207 BRPaqChecker pcheck; // Verification/comptage des paquets
208
[3537]209 try {
210 TimeStamp ts;
[3623]211 cout << " DataSaver::run() - Starting " << ts << " NbFiles=" << nfiles_ << " NBloc/File="
212 << nblocperfile_ << " NMaxMemZones=" << nmax_ << endl;
[3537]213 char fname[512];
[3623]214
[3537]215 sprintf(fname,"%s/saver.log",path_.c_str());
216 ofstream filog(fname);
217 filog << " DataProc::run() - starting log file " << ts << endl;
[3623]218 filog << " NbFiles=" << nfiles_ << " NBloc/File=" << nblocperfile_ << " NMaxMemZones=" << nmax_ << endl;
219
220 DataTable dt;
221 dt.AddLongColumn("TimeTag");
222 dt.AddIntegerColumn("FrameCounter");
223 dt.AddIntegerColumn("FrameLength");
224 dt.AddIntegerColumn("Num");
225 DataTableRow dtr = dt.EmptyRow();
226 uint_8 timtag = 0;
227 uint_4 numpaq = 0;
228 bool dthead = false; // Mettre a false pour ne pas remplir DataTable Headers
[3537]229
[3623]230
231 uint_4 fnum=0;
[3537]232 uint_4 paqsz = memgr.PaqSize();
[3623]233 for (uint_4 nbFile=0;nbFile<nfiles_ ;nbFile++) {
234 if (stop_ ) break;
235 sprintf(fname,"%s/HDRfits%d.txt",path_.c_str(),fnum);
236 ofstream header(fname);
237
238 BRPaquet paq0(NULL, NULL, paqsz);
239 uint_4 npaqperfile = memgr.NbPaquets()*nblocperfile_; // Nombre de paquets ecrits dans un fichier
240
241 MiniFITSFile mff;
242 if (savesig_) { //Reza - Ouverture conditionnel fichier
243 sprintf(fname,"%s/signal%d.fits",path_.c_str(),(int)fnum++);
244 mff.Open(fname,MF_Write); //Reza - Ouverture conditionnel fichier
245 mff.setDTypeNaxis(MF_Byte, paq0.DataSize()+paq0.HeaderSize(), npaqperfile);
[3537]246 }
[3623]247 else sprintf(fname,"MemDataBloc[%d]-NoDataFile",(int)fnum++);
248
249 for (uint_4 kmz=0; kmz<nblocperfile_; kmz++) {
250 if (stop_) break;
251 //DBG cout << " DataSaver::run()- nbFile=" << nbFile << " kmz=" << kmz << endl;
252 int mid = memgr.FindMemZoneId(MemZA_Save);
253 Byte* buff = memgr.GetMemZone(mid);
254 if (buff == NULL) {
255 cout << " DataSaver::run()/ERROR memgr.GetMemZone(" << mid << ") -> NULL" << endl;
256 setRC(2);
257 return;
258 }
259 for(uint_4 i=0; i<memgr.NbPaquets(); i++) {
260
261 BRPaquet paq(NULL, buff+i*paqsz, paqsz);
262 pcheck.Check(paq); // Verification du paquet / FrameCounter
263
264 header << hex << paq.HDRMarker() << " " << paq.TRLMarker() << " " << paq.TimeTag2()<< " "<< paq.TimeTag1()<< " " << paq.FrameCounter() << " " << paq.PaqLen() << endl;
265
266 if (dthead) { // Remplissage DataTable entetes paquets
267 timtag = (uint_8)paq.TimeTag2()*0x100000000ULL+paq.TimeTag1();
268 dtr[0] = timtag;
269 dtr[1] = paq.FrameCounter();
270 dtr[2] = paq.PaqLen();
271 dtr[3] = numpaq++;
272 dt.AddRow(dtr);
273 }
274
275 if (savesig_) // Reza - Ecriture conditionnel fichier fits signal
[3635]276 mff.WriteB(paq.Header(),paq.PaquetSize()); // ecriture tout le paquet (modif Mai 2009
[3623]277 // mff.WriteB(paq.Data1(), paq.DataSize());
278 mff.WriteB(paq.Header(),paq.DataSize()+paq.HeaderSize()); // ecriture datas + header
279 }
280 memgr.FreeMemZone(mid, MemZS_Saved);
[3537]281 }
[3623]282 ts.SetNow();
283 filog << ts << " : OK data file " << fname << endl;
284 cout << " DataSaver::run() " << ts << " : OK data file " << fname << endl;
[3537]285 }
[3623]286 if (dthead) {
287 cout << dt;
288 char fname2[256];
289 sprintf(fname2,"%s/headers.ppf",path_.c_str());
290 POutPersist po(fname2);
291 po << dt;
292 }
293 cout << " -------------------- DataSaver::run() -------------------- " << endl;
294 pcheck.Print(cout);
295 cout << " ---------------------------------------------------------- " << endl;
296 ts.SetNow();
297 pcheck.Print(filog);
298 filog << " DataSaver::run() - End of processing/run() " << ts << endl;
299
[3537]300 }
301 catch (MiniFITSException& exc) {
302 cout << " DataSaver::run()/catched MiniFITSException " << exc.Msg() << endl;
303 setRC(3);
304 return;
305 }
306 catch(...) {
307 cout << " DataSaver::run()/catched unknown ... exception " << endl;
308 setRC(4);
309 return;
310 }
311 setRC(0);
312 return;
313}
314
315
[3623]316
317
318//----------------------------------------------------------------------------------------------------------
319// Classe thread de lecture PCI-Express + Check pour tests de verification de debit/etc avec un seul thread
320//----------------------------------------------------------------------------------------------------------
321
[3628]322PCIEReaderChecker::PCIEReaderChecker(PCIEWrapperInterface &pciw,uint_4 sizeFrame,uint_4 packSize ,RAcqMemZoneMgr& mem,
[3623]323 uint_4 nmax, BRDataFmtConv swapall)
324 : memgr(mem) , pciw_ (pciw)
325{
326 nmax_ = nmax;
327 swapall_ = swapall; // select data swap/format conversion for BRPaquet
328 stop_ = false;
329 packSize_ = packSize;
330 sizeFr_ =sizeFrame;
331}
332void PCIEReaderChecker::run()
333{
334
335 struct timeval tv1, tv2;
336 gettimeofday(&tv1, NULL);
337
338 cout << " PCIEReaderChecker::run() - Starting , NMaxMemZones=" << nmax_
339 << " memgr.NbPaquets()=" << memgr.NbPaquets() << endl;
340 setRC(1);
341 cout << " ... RAcqMemZoneMgr not used - using s fixed memory location for packets decoding ..." << endl;
342
343 uint_4 cpt=0;
344 // sigaddset(&act.sa_mask,SIGINT); // pour proteger le transfert DMA
345 //sigaction(SIGINT,&act,NULL);
346 uint_4 paqsz = memgr.PaqSize();
347 uint_4 dmasz = pciw_.TransferSize();
348
349 BRPaqChecker pcheck; // Verification/comptage des paquets
350
351 Byte* Datas = NULL;
352 Byte* locdata = new Byte[paqsz];
353 Byte* tampon = new Byte[paqsz];
354
355 uint_4 off_acheval = 0;
356
357 int nerrdma = 0;
358 int maxerrdma = 10;
359 bool fgarret = false;
360 for (uint_4 kmz=0; kmz<nmax_; kmz++) {
361 if (stop_) break;
362 if (fgarret) break;
363
364 Byte* nextdma = locdata;
365 uint_4 npaqfait = 0;
366 // for (uint_4 i=0; i<memgr.NbPaquets(); i += pktInDMATr) { // attention pktInDMATr paquets dans 1 seul DMA
367 while (npaqfait < memgr.NbPaquets()) {
368 if (fgarret) break;
369 // On pointe vers le debut de la zone a remplir aver le prochain DMA
370 //-- Zone memoire locale Byte* nextdma = buff+i*paqsz;
371 Datas=pciw_.DoTransferData();
372
373 if (Datas == NULL) { // No data Read in DMA
374 nerrdma ++;
375 cout << "PCIEReaderChecker/Erreur Waiting for datas ..." << endl;
376 pciw_.PrintStatus(cout);
377 if (nerrdma>=maxerrdma) { fgarret = true; break; }
378 }
379 else {
380 uint_4 curoff = 0;
381 //1- On traite le paquet a cheval, rempli partiellement avec le DMA d'avant si necessaire
382 // if (off_acheval > 0) {
383 // memcpy((void *)(tampon+off_acheval), (void *)Datas, paqsz-off_acheval);
384 // curoff = paqsz-off_acheval; off_acheval = 0;
385
386 // BRPaquet paq(tampon, locdata, paqsz, swapall_);
387 // npaqfait++; // Ne pas oublier le compteur de paquets faits
388 // pcheck.Check(paq); // Verification du paquet / FrameCounter
389 //}
390 if (off_acheval > 0) {
391 if ((paqsz-off_acheval)< dmasz) {
392 memcpy((void *)(tampon+off_acheval), (void *)Datas, paqsz-off_acheval);
393 curoff = paqsz-off_acheval; off_acheval = 0;
394
395 BRPaquet paq(tampon, locdata, paqsz, swapall_);
396 npaqfait++; // Ne pas oublier le compteur de paquets faits
397 pcheck.Check(paq); // Verification du paquet / FrameCounter
398 }
399 else {
400 memcpy((void *)(tampon+off_acheval), (void *)Datas, dmasz);
401 curoff =dmasz;
402 off_acheval = (dmasz+off_acheval);
403 }
404 }
405 //2- On traite les paquets complet qui se trouvent dans la zone du DMA
406 while((curoff+paqsz)<=dmasz) {
407 // BRPaquet paq((Byte*)(Datas)+((paqsz*j)), nextdma+j*paqsz, paqsz, swapall_);
408 BRPaquet paq(Datas+curoff, locdata, paqsz, swapall_);
409 curoff += paqsz; // On avance l'index dans le buffer du DMA
410 npaqfait++; // Ne pas oublier le compteur de paquets faits
411 pcheck.Check(paq); // Verification du paquet / FrameCounter
412 } // -- FIN traitement des paquets complets ds un DMA
413 //3- On copie si besoin la fin du DMA dans la zone tampon
414 if (curoff < dmasz) {
415 off_acheval = dmasz-curoff;
416 memcpy(tampon, (void*)(Datas+curoff), off_acheval);
417 curoff += off_acheval;
418 }
419 } // Traitement d'un DMA OK
420
421 } // Fin boucle de remplissage d'une zone memoire
422 } // Fin boucle sur les zones
423
424 setRC(0);
425 gettimeofday(&tv2, NULL);
426 double tmelaps2 = (tv2.tv_sec-tv1.tv_sec)*1000.+(tv2.tv_usec-tv1.tv_usec)/1000.;
427 if (tmelaps2<0.1) tmelaps2=0.1;
428 cout << " ------------------ PCIEReaderChecker::run()-End summary -------------------" << endl;
429 cout << " PCIEReaderChecker/Info TotTransfer=" << pciw_.TotTransferBytes()/1024
430 << " kb , ElapsTime=" << tmelaps2 << " ms ->"
431 << (double)pciw_.TotTransferBytes()/tmelaps2 << " kb/s" << endl;
432 pcheck.Print(cout);
433 cout << " --------------------------------------------------------------------" << endl;
434
435 delete [] locdata;
436 delete [] tampon;
437
438 return;
439}
440void PCIEReaderChecker::Stop()
441{
442 // cout << " PCIEReaderChecker::stop() ........ STOP" <<endl;
443 stop_ = true;
444
445}
Note: See TracBrowser for help on using the repository browser.