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

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

Correction et ameliorations multiples - en particulier mcrd.cc, brproc.cc et brfitsrd.cc, Reza 27/05/2009

File size: 15.2 KB
Line 
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>
12#include <signal.h>
13#include "pexceptions.h"
14#include "timestamp.h"
15
16#include "pciewrap.h"
17#include "brpaqu.h"
18#include "minifits.h"
19
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
23//-------------------------------------------------------
24// Classe thread de lecture PCI-Express
25//-------------------------------------------------------
26
27PCIEReader::PCIEReader(PCIEWrapperInterface &pciw,uint_4 sizeFrame,uint_4 packSize ,RAcqMemZoneMgr& mem,
28 uint_4 nmax, BRDataFmtConv swapall)
29 : memgr(mem) , pciw_ (pciw)
30{
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;
42}
43
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
59void PCIEReader::run()
60{
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;
70 setRC(1);
71
72 uint_4 cpt=0;
73 // sigaddset(&act.sa_mask,SIGINT); // pour proteger le transfert DMA
74 //sigaction(SIGINT,&act,NULL);
75
76 uint_4 paqsz = memgr.PaqSize();
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 }
170 setRC(0);
171
172 return;
173}
174
175
176void PCIEReader::Stop()
177{
178 // cout << " PCIEReader::Stop() -------------> STOP" <<endl;
179 stop_ = true;
180 return;
181}
182
183
184//-------------------------------------------------------
185// Classe thread de sauvegarde sur fichiers
186//-------------------------------------------------------
187
188DataSaver::DataSaver(RAcqMemZoneMgr& mem, string& path, uint_4 nfiles, uint_4 nblocperfile, bool savesig)
189 : memgr(mem)
190{
191 nfiles_ = nfiles;
192 nblocperfile_ = nblocperfile;
193 nmax_ = nblocperfile_*nfiles_;
194 savesig_ = savesig; // Si false, pas d'ecriture des fichiers FITS du signal
195 stop_ = false;
196 path_ = path;
197}
198void DataSaver::Stop()
199{
200 // cout<< " DataSaver:Stop ........ " << endl;
201 stop_=true;
202
203}
204void DataSaver::run()
205{
206 setRC(1);
207 BRPaqChecker pcheck; // Verification/comptage des paquets
208
209 try {
210 TimeStamp ts;
211 cout << " DataSaver::run() - Starting " << ts << " NbFiles=" << nfiles_ << " NBloc/File="
212 << nblocperfile_ << " NMaxMemZones=" << nmax_ << endl;
213 char fname[512];
214
215 sprintf(fname,"%s/saver.log",path_.c_str());
216 ofstream filog(fname);
217 filog << " DataProc::run() - starting log file " << ts << endl;
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
229
230
231 uint_4 fnum=0;
232 uint_4 paqsz = memgr.PaqSize();
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 // Entete correspondant a l'ecriture tout le paquet - trailer compris (modif Mai 2009)
246 mff.setDTypeNaxis(MF_Byte, paq0.PaquetSize(), npaqperfile);
247 // Sans TRAILER de paquet mff.setDTypeNaxis(MF_Byte, paq0.DataSize()+paq0.HeaderSize(), npaqperfile);
248 }
249 else sprintf(fname,"MemDataBloc[%d]-NoDataFile",(int)fnum++);
250
251 for (uint_4 kmz=0; kmz<nblocperfile_; kmz++) {
252 if (stop_) break;
253 //DBG cout << " DataSaver::run()- nbFile=" << nbFile << " kmz=" << kmz << endl;
254 int mid = memgr.FindMemZoneId(MemZA_Save);
255 Byte* buff = memgr.GetMemZone(mid);
256 if (buff == NULL) {
257 cout << " DataSaver::run()/ERROR memgr.GetMemZone(" << mid << ") -> NULL" << endl;
258 setRC(2);
259 return;
260 }
261 for(uint_4 i=0; i<memgr.NbPaquets(); i++) {
262
263 BRPaquet paq(NULL, buff+i*paqsz, paqsz);
264 pcheck.Check(paq); // Verification du paquet / FrameCounter
265
266 header << hex << paq.HDRMarker() << " " << paq.TRLMarker() << " " << paq.TimeTag2()<< " "<< paq.TimeTag1()<< " " << paq.FrameCounter() << " " << paq.PaqLen() << endl;
267
268 if (dthead) { // Remplissage DataTable entetes paquets
269 timtag = (uint_8)paq.TimeTag2()*0x100000000ULL+paq.TimeTag1();
270 dtr[0] = timtag;
271 dtr[1] = paq.FrameCounter();
272 dtr[2] = paq.PaqLen();
273 dtr[3] = numpaq++;
274 dt.AddRow(dtr);
275 }
276
277 if (savesig_) // Reza - Ecriture conditionnel fichier fits signal
278 mff.WriteB(paq.Header(),paq.PaquetSize()); // ecriture tout le paquet (modif Mai 2009)
279 // mff.WriteB(paq.Data1(), paq.DataSize());
280 // mff.WriteB(paq.Header(),paq.DataSize()+paq.HeaderSize()); // ecriture datas + header
281 }
282 memgr.FreeMemZone(mid, MemZS_Saved);
283 }
284 ts.SetNow();
285 filog << ts << " : OK data file " << fname << endl;
286 cout << " DataSaver::run() " << ts << " : OK data file " << fname << endl;
287 }
288 if (dthead) {
289 cout << dt;
290 char fname2[256];
291 sprintf(fname2,"%s/headers.ppf",path_.c_str());
292 POutPersist po(fname2);
293 po << dt;
294 }
295 cout << " -------------------- DataSaver::run() -------------------- " << endl;
296 pcheck.Print(cout);
297 cout << " ---------------------------------------------------------- " << endl;
298 ts.SetNow();
299 pcheck.Print(filog);
300 filog << " DataSaver::run() - End of processing/run() " << ts << endl;
301
302 }
303 catch (MiniFITSException& exc) {
304 cout << " DataSaver::run()/catched MiniFITSException " << exc.Msg() << endl;
305 setRC(3);
306 return;
307 }
308 catch(...) {
309 cout << " DataSaver::run()/catched unknown ... exception " << endl;
310 setRC(4);
311 return;
312 }
313 setRC(0);
314 return;
315}
316
317
318
319
320//----------------------------------------------------------------------------------------------------------
321// Classe thread de lecture PCI-Express + Check pour tests de verification de debit/etc avec un seul thread
322//----------------------------------------------------------------------------------------------------------
323
324PCIEReaderChecker::PCIEReaderChecker(PCIEWrapperInterface &pciw,uint_4 sizeFrame,uint_4 packSize ,RAcqMemZoneMgr& mem,
325 uint_4 nmax, BRDataFmtConv swapall)
326 : memgr(mem) , pciw_ (pciw)
327{
328 nmax_ = nmax;
329 swapall_ = swapall; // select data swap/format conversion for BRPaquet
330 stop_ = false;
331 packSize_ = packSize;
332 sizeFr_ =sizeFrame;
333}
334void PCIEReaderChecker::run()
335{
336
337 struct timeval tv1, tv2;
338 gettimeofday(&tv1, NULL);
339
340 cout << " PCIEReaderChecker::run() - Starting , NMaxMemZones=" << nmax_
341 << " memgr.NbPaquets()=" << memgr.NbPaquets() << endl;
342 setRC(1);
343 cout << " ... RAcqMemZoneMgr not used - using s fixed memory location for packets decoding ..." << endl;
344
345 uint_4 cpt=0;
346 // sigaddset(&act.sa_mask,SIGINT); // pour proteger le transfert DMA
347 //sigaction(SIGINT,&act,NULL);
348 uint_4 paqsz = memgr.PaqSize();
349 uint_4 dmasz = pciw_.TransferSize();
350
351 BRPaqChecker pcheck; // Verification/comptage des paquets
352
353 Byte* Datas = NULL;
354 Byte* locdata = new Byte[paqsz];
355 Byte* tampon = new Byte[paqsz];
356
357 uint_4 off_acheval = 0;
358
359 int nerrdma = 0;
360 int maxerrdma = 10;
361 bool fgarret = false;
362 for (uint_4 kmz=0; kmz<nmax_; kmz++) {
363 if (stop_) break;
364 if (fgarret) break;
365
366 Byte* nextdma = locdata;
367 uint_4 npaqfait = 0;
368 // for (uint_4 i=0; i<memgr.NbPaquets(); i += pktInDMATr) { // attention pktInDMATr paquets dans 1 seul DMA
369 while (npaqfait < memgr.NbPaquets()) {
370 if (fgarret) break;
371 // On pointe vers le debut de la zone a remplir aver le prochain DMA
372 //-- Zone memoire locale Byte* nextdma = buff+i*paqsz;
373 Datas=pciw_.DoTransferData();
374
375 if (Datas == NULL) { // No data Read in DMA
376 nerrdma ++;
377 cout << "PCIEReaderChecker/Erreur Waiting for datas ..." << endl;
378 pciw_.PrintStatus(cout);
379 if (nerrdma>=maxerrdma) { fgarret = true; break; }
380 }
381 else {
382 uint_4 curoff = 0;
383 //1- On traite le paquet a cheval, rempli partiellement avec le DMA d'avant si necessaire
384 // if (off_acheval > 0) {
385 // memcpy((void *)(tampon+off_acheval), (void *)Datas, paqsz-off_acheval);
386 // curoff = paqsz-off_acheval; off_acheval = 0;
387
388 // BRPaquet paq(tampon, locdata, paqsz, swapall_);
389 // npaqfait++; // Ne pas oublier le compteur de paquets faits
390 // pcheck.Check(paq); // Verification du paquet / FrameCounter
391 //}
392 if (off_acheval > 0) {
393 if ((paqsz-off_acheval)< dmasz) {
394 memcpy((void *)(tampon+off_acheval), (void *)Datas, paqsz-off_acheval);
395 curoff = paqsz-off_acheval; off_acheval = 0;
396
397 BRPaquet paq(tampon, locdata, paqsz, swapall_);
398 npaqfait++; // Ne pas oublier le compteur de paquets faits
399 pcheck.Check(paq); // Verification du paquet / FrameCounter
400 }
401 else {
402 memcpy((void *)(tampon+off_acheval), (void *)Datas, dmasz);
403 curoff =dmasz;
404 off_acheval = (dmasz+off_acheval);
405 }
406 }
407 //2- On traite les paquets complet qui se trouvent dans la zone du DMA
408 while((curoff+paqsz)<=dmasz) {
409 // BRPaquet paq((Byte*)(Datas)+((paqsz*j)), nextdma+j*paqsz, paqsz, swapall_);
410 BRPaquet paq(Datas+curoff, locdata, paqsz, swapall_);
411 curoff += paqsz; // On avance l'index dans le buffer du DMA
412 npaqfait++; // Ne pas oublier le compteur de paquets faits
413 pcheck.Check(paq); // Verification du paquet / FrameCounter
414 } // -- FIN traitement des paquets complets ds un DMA
415 //3- On copie si besoin la fin du DMA dans la zone tampon
416 if (curoff < dmasz) {
417 off_acheval = dmasz-curoff;
418 memcpy(tampon, (void*)(Datas+curoff), off_acheval);
419 curoff += off_acheval;
420 }
421 } // Traitement d'un DMA OK
422
423 } // Fin boucle de remplissage d'une zone memoire
424 } // Fin boucle sur les zones
425
426 setRC(0);
427 gettimeofday(&tv2, NULL);
428 double tmelaps2 = (tv2.tv_sec-tv1.tv_sec)*1000.+(tv2.tv_usec-tv1.tv_usec)/1000.;
429 if (tmelaps2<0.1) tmelaps2=0.1;
430 cout << " ------------------ PCIEReaderChecker::run()-End summary -------------------" << endl;
431 cout << " PCIEReaderChecker/Info TotTransfer=" << pciw_.TotTransferBytes()/1024
432 << " kb , ElapsTime=" << tmelaps2 << " ms ->"
433 << (double)pciw_.TotTransferBytes()/tmelaps2 << " kb/s" << endl;
434 pcheck.Print(cout);
435 cout << " --------------------------------------------------------------------" << endl;
436
437 delete [] locdata;
438 delete [] tampon;
439
440 return;
441}
442void PCIEReaderChecker::Stop()
443{
444 // cout << " PCIEReaderChecker::stop() ........ STOP" <<endl;
445 stop_ = true;
446
447}
Note: See TracBrowser for help on using the repository browser.