CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FUResource.cc
Go to the documentation of this file.
1 //
3 // FUResource
4 // ----------
5 //
6 // 12/10/2006 Philipp Schieferdecker <philipp.schieferdecker@cern.ch>
7 // 20/01/2012 Andrei Spataru <aspataru@cern.ch>
9 
12 
14 #include "interface/shared/i2oXFunctionCodes.h"
15 #include "interface/evb/i2oEVBMsgs.h"
16 #include "EvffedFillerRB.h"
17 #include "toolbox/mem/Reference.h"
18 #include "xcept/tools.h"
19 
20 #include <sstream>
21 #include <sys/shm.h>
22 
23 using namespace std;
24 using namespace evf;
25 
26 //#define DEBUG_FU_RES
27 
28 
30 // initialize static members
32 
33 //______________________________________________________________________________
34 bool FUResource::doFedIdCheck_ = true;
35 bool FUResource::useEvmBoard_ = true;
37 unsigned int FUResource::gtpDaqId_ = FEDNumbering::MAXTriggerGTPFEDID;
39 
41 // construction/destruction
43 
44 //______________________________________________________________________________
45 FUResource::FUResource(UInt_t fuResourceId, log4cplus::Logger logger,
46  EvffedFillerRB *frb, xdaq::Application *app) :
47  log_(logger), fuResourceId_(fuResourceId), superFragHead_(0),
48  superFragTail_(0), nbBytes_(0), superFragSize_(0), shmCell_(0),
49  frb_(frb), app_(app), nextEventWillHaveCRCError_(false) {
50  //release();
51 }
52 
53 //______________________________________________________________________________
55 
56 }
57 
59 // implementation of member functions
61 
62 //______________________________________________________________________________
64  //release();
65  shmCell_ = shmCell;
66  shmCell_->clear();
68  //UPDATED
71  nFedMax_ = shmCell_->nFed();
73  /*
74  cout << "shmCell = " << shmCell_ << " shm cell furesourceId = "
75  << fuResourceId_ << " payload size = " << shmCell_->payloadSize()
76  << " nFed max = " << shmCell_->nFed() << " nSuperFragMax_ = "
77  << shmCell_->nSuperFrag() << endl;
78  */
79 }
80 
81 //______________________________________________________________________________
82 void FUResource::release(bool detachResource) {
83  doCrcCheck_ = false;
84  fatalError_ = false;
85 
86  buResourceId_ = 0xffffffff;
87  evtNumber_ = 0xffffffff;
88 
89  if (0 != superFragHead_) {
90  try {
91  superFragHead_->release();
92  } catch (xcept::Exception& e) {
93  LOG4CPLUS_ERROR(
94  log_,
95  "Failed to release superFragHead: "
96  << xcept::stdformat_exception_history(e));
97  }
98  }
99 
100  superFragHead_ = 0;
101  superFragTail_ = 0;
102 
103  iBlock_ = 0;
104  nBlock_ = 0xffffffff;
105  iSuperFrag_ = 0;
106  nSuperFrag_ = 0xffffffff;
107 
108  nbSent_ = 0;
109 
110  nbErrors_ = 0;
111  nbCrcErrors_ = 0;
112 
113  for (UInt_t i = 0; i < 1024; i++)
114  fedSize_[i] = 0;
115  eventSize_ = 0;
116 
117  if (0 != shmCell_) {
118  shmCell_ = 0;
119  if (detachResource)
120  shmdt(shmCell_);
121  }
122 
123 }
124 
125 //______________________________________________________________________________
127  if (fatalError()) {
128  LOG4CPLUS_ERROR(log_, "THIS SHOULD *NEVER* HAPPEN!."); // DEBUG
129  bufRef->release();
130  return;
131  }
132 #ifdef DEBUG_FU_RES
133  std::cout << "Started process() for bufRef: " << bufRef<< std::endl;
134 #endif
135  MemRef_t* itBufRef = bufRef;
136  while (0 != itBufRef && !fatalError()) {
137  MemRef_t* next = itBufRef->getNextReference();
138  itBufRef->setNextReference(0);
139  try {
140 
141  ResourceChecker resCheck(this);
142  resCheck.processDataBlock(itBufRef);
143 
144  } catch (xcept::Exception& e) {
145  LOG4CPLUS_ERROR(log_,
146  "EVENT LOST:" << xcept::stdformat_exception_history(e));
147  fatalError_ = true;
148  itBufRef->setNextReference(next);
149  }
150 
151  itBufRef = next;
152  }
153  if (isComplete()) {
155  frb_->putTrailer();
156  fedSize_[frb_->fedId()] = frb_->size();
157  UChar_t *startPos = shmCell_->writeData(frb_->getPayload(),
158  frb_->size());
159  superFragSize_ = frb_->size();
160  if (!shmCell_->markSuperFrag(iSuperFrag_, superFragSize_, startPos)) {
161  nbErrors_++;
162  stringstream oss;
163  oss << "Failed to mark super fragment in shared mem buffer."
164  << " fuResourceId:" << fuResourceId_ << " evtNumber:"
165  << evtNumber_ << " iSuperFrag:" << iSuperFrag_;
166  XCEPT_RAISE(evf::Exception, oss.str());
167  }
168 
169  if (!shmCell_->markFed(frb_->fedId(), frb_->size(), startPos)) {
170  nbErrors_++;
171  stringstream oss;
172  oss << "Failed to mark fed in buffer." << " evtNumber:"
173  << evtNumber_ << " fedId:" << frb_->fedId() << " fedSize:"
174  << frb_->size() << " fedAddr:0x" << hex
175  << (unsigned long) frb_->getPayload() << dec;
176  XCEPT_RAISE(evf::Exception, oss.str());
177  }
178 
179  }
180  return;
181 }
182 
183 //______________________________________________________________________________
185  if (0 == superFragHead_) {
186  superFragHead_ = bufRef;
187  superFragTail_ = bufRef;
188  } else {
189  superFragTail_->setNextReference(bufRef);
190  superFragTail_ = bufRef;
191  }
192  return;
193 }
194 
195 //______________________________________________________________________________
197  if (0 == superFragHead_) {
198  //nothing to do... why did we get here then ???
199  } else if (superFragHead_ == superFragTail_) {
200  superFragHead_ = 0;
201  superFragTail_ = 0;
202  } else {
203  MemRef_t *next = 0;
205  while ((next = current->getNextReference()) != superFragTail_) {
206  current = next;
207  //get to the next-to-last block
208  }
210  current->setNextReference(0);
211  }
212  return;
213 }
214 
215 //______________________________________________________________________________
217  UChar_t *blockAddr = 0;
218  UChar_t *frlHeaderAddr = 0;
219  frlh_t *frlHeader = 0;
220 
221  superFragSize_ = 0;
222 
223  UInt_t frameSize = sizeof(I2O_EVENT_DATA_BLOCK_MESSAGE_FRAME);
224  MemRef_t* bufRef = superFragHead_;
225 
226  while (0 != bufRef) {
227  blockAddr = (UChar_t*) bufRef->getDataLocation();
228  frlHeaderAddr = blockAddr + frameSize;
229  frlHeader = (frlh_t*) frlHeaderAddr;
230  superFragSize_ += frlHeader->segsize & FRL_SEGSIZE_MASK;
231  bufRef = bufRef->getNextReference();
232  }
233 
235 
237  nbErrors_++;
238  stringstream oss;
239  oss << "Event size exceeds maximum size." << " fuResourceId:"
240  << fuResourceId_ << " evtNumber:" << evtNumber_
241  << " iSuperFrag:" << iSuperFrag_ << " eventSize:" << eventSize_
242  << " eventPayloadSize:" << eventPayloadSize_;
243  XCEPT_RAISE(evf::Exception, oss.str());
244  }
245 }
246 
247 //______________________________________________________________________________
249  UChar_t *blockAddr = 0;
250  UChar_t *frlHeaderAddr = 0;
251  UChar_t *fedAddr = 0;
252  UInt_t nbBytes = 0;
253  UInt_t nbBytesTot = 0;
254  frlh_t *frlHeader = 0;
255  UChar_t *bufferPos = 0;
256  UChar_t *startPos = 0;
257 
258  MemRef_t* bufRef = superFragHead_;
259  while (bufRef != 0) {
260  blockAddr = (UChar_t*) bufRef->getDataLocation();
261  frlHeaderAddr = blockAddr + sizeof(I2O_EVENT_DATA_BLOCK_MESSAGE_FRAME);
262  fedAddr = frlHeaderAddr + sizeof(frlh_t);
263  frlHeader = (frlh_t*) frlHeaderAddr;
264  nbBytes = frlHeader->segsize & FRL_SEGSIZE_MASK;
265  nbBytesTot += nbBytes;
266 
267  // check if still within limits
268  if (nbBytesTot > superFragSize_) {
269  nbErrors_++;
270  stringstream oss;
271  oss << "Reached end of buffer." << " fuResourceId:"
272  << fuResourceId_ << " evtNumber:" << evtNumber_
273  << " iSuperFrag:" << iSuperFrag_;
274  XCEPT_RAISE(evf::Exception, oss.str());
275  }
276 
277  bufferPos = shmCell_->writeData(fedAddr, nbBytes);
278  if (0 == startPos)
279  startPos = bufferPos;
280 
281  nbBytes_ += nbBytes;
282  bufRef = bufRef->getNextReference();
283  }
284 
285  if (!shmCell_->markSuperFrag(iSuperFrag_, superFragSize_, startPos)) {
286  nbErrors_++;
287  stringstream oss;
288  oss << "Failed to mark super fragment in shared mem buffer."
289  << " fuResourceId:" << fuResourceId_ << " evtNumber:"
290  << evtNumber_ << " iSuperFrag:" << iSuperFrag_;
291  XCEPT_RAISE(evf::Exception, oss.str());
292  }
293 
294  return;
295 }
296 
297 //______________________________________________________________________________
299  if (0 == superFragHead_)
300  return;
301  superFragHead_->release(); // throws xcept::Exception
302  superFragHead_ = 0;
303  superFragTail_ = 0;
304  return;
305 }
306 
307 //______________________________________________________________________________
310  if (reset)
311  nbErrors_ = 0;
312  return result;
313 }
314 
315 //______________________________________________________________________________
318  if (reset)
319  nbCrcErrors_ = 0;
320  return result;
321 }
322 
323 //______________________________________________________________________________
326  if (reset)
327  nbBytes_ = 0;
328  return result;
329 }
int i
Definition: DBlmapReader.cc:9
UInt_t nbErrors_
Definition: FUResource.h:183
void process(MemRef_t *bufRef)
Definition: FUResource.cc:126
UInt_t fuResourceId_
Definition: FUResource.h:165
void releaseSuperFrag()
Definition: FUResource.cc:298
EvffedFillerRB * frb_
Definition: FUResource.h:192
unsigned int size() const
bool fatalError() const
Definition: FUResource.h:110
virtual ~FUResource()
Definition: FUResource.cc:54
void appendBlockToSuperFrag(MemRef_t *bufRef)
Definition: FUResource.cc:184
UInt_t nbErrors(bool reset=true)
Definition: FUResource.cc:308
UInt_t nbBytes(bool reset=true)
Definition: FUResource.cc:324
unsigned char * writeData(unsigned char *data, unsigned int dataSize)
std::ostream & logger()
Definition: fwLog.cc:41
toolbox::mem::Reference MemRef_t
Definition: FUTypes.h:10
void processDataBlock(MemRef_t *bufRef)
void fillSuperFragPayload()
Definition: FUResource.cc:248
bool markFed(unsigned int i, unsigned int size, unsigned char *addr)
void release(bool detachResource)
Definition: FUResource.cc:82
MemRef_t * superFragHead_
Definition: FUResource.h:169
unsigned int fedId() const
log4cplus::Logger log_
Definition: FUResource.h:156
evf::FUShmRawCell * shmCell_
Definition: FUResource.h:191
MemRef_t * superFragTail_
Definition: FUResource.h:170
void removeLastAppendedBlockFromSuperFrag()
Definition: FUResource.cc:196
UInt_t evtNumber_
Definition: FUResource.h:167
unsigned char UChar_t
Definition: FUTypes.h:14
tuple result
Definition: query.py:137
void superFragSize()
Definition: FUResource.cc:216
void setEventTypeData()
Definition: FUShmRawCell.h:57
UInt_t fedSize_[1024]
Definition: FUResource.h:187
bool markSuperFrag(unsigned int i, unsigned int size, unsigned char *addr)
bool isComplete() const
Definition: FUResource.h:221
UInt_t iSuperFrag_
Definition: FUResource.h:178
unsigned int payloadSize() const
Definition: FUShmRawCell.h:30
constexpr unsigned int gtpeId_
Definition: DaqSource.cc:51
UInt_t eventPayloadSize_
Definition: FUResource.h:172
unsigned int UInt_t
Definition: FUTypes.h:12
evf::FUShmRawCell * shmCell()
Definition: FUResource.h:145
unsigned char *const getPayload()
void setFuResourceId(unsigned int id)
Definition: FUShmRawCell.h:43
UInt_t nbCrcErrors_
Definition: FUResource.h:184
UInt_t superFragSize_
Definition: FUResource.h:188
UInt_t buResourceId_
Definition: FUResource.h:166
unsigned int nFed() const
Definition: FUShmRawCell.h:33
UInt_t nSuperFrag_
Definition: FUResource.h:179
UInt_t eventSize_
Definition: FUResource.h:189
unsigned int nSuperFrag() const
Definition: FUShmRawCell.h:37
UInt_t nbCrcErrors(bool reset=true)
Definition: FUResource.cc:316
tuple cout
Definition: gather_cfg.py:121
UInt_t nSuperFragMax_
Definition: FUResource.h:174
constexpr unsigned int gtpEvmId_
void reset(double vett[256])
Definition: TPedValues.cc:11
void allocate(FUShmRawCell *shmCell)
Definition: FUResource.cc:63
void putHeader(unsigned int l1id, unsigned int bxid)