3 /**************************************************************************
4 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
6 * Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
7 * for The ALICE Off-line Project. *
9 * Permission to use, copy, modify and distribute this software and its *
10 * documentation strictly for non-commercial purposes is hereby granted *
11 * without fee, provided that the above copyright notice appears in all *
12 * copies and that both the copyright notice and this permission notice *
13 * appear in the supporting documentation. The authors make no claims *
14 * about the suitability of this software for any purpose. It is *
15 * provided "as is" without express or implied warranty. *
16 **************************************************************************/
18 /** @file AliHLTDataBuffer.cxx
19 @author Matthias Richter
21 @brief Handling of Data Buffers for HLT components.
24 // see header file for class documentation
26 // refer to README to build package
28 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
34 #include "AliHLTDataBuffer.h"
35 #include "AliHLTConsumerDescriptor.h"
36 #include "AliHLTComponent.h"
39 //#include "AliHLTSystem.h"
41 /** ROOT macro for the implementation of ROOT specific class methods */
42 ClassImp(AliHLTDataBuffer)
44 AliHLTDataBuffer::AliHLTDataBuffer()
53 // see header file for class documentation
55 // refer to README to build package
57 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
60 fActiveConsumers.empty();
61 fReleasedConsumers.empty();
65 AliHLTDataBuffer::AliHLTDataBuffer(const AliHLTDataBuffer&)
76 // see header file for function documentation
77 HLTFatal("copy constructor untested");
80 AliHLTDataBuffer& AliHLTDataBuffer::operator=(const AliHLTDataBuffer&)
82 // see header file for function documentation
83 HLTFatal("assignment operator untested");
87 int AliHLTDataBuffer::fgNofInstances=0;
88 vector<AliHLTDataBuffer::AliHLTRawBuffer*> AliHLTDataBuffer::fgFreeBuffers;
89 vector<AliHLTDataBuffer::AliHLTRawBuffer*> AliHLTDataBuffer::fgActiveBuffers;
90 AliHLTUInt32_t AliHLTDataBuffer::fgMargin=1024;
91 AliHLTLogging AliHLTDataBuffer::fgLogging;
92 const Int_t AliHLTDataBuffer::fgkSafetyPatternSize=16;
93 const char AliHLTDataBuffer::fgkSafetyPattern[]={0x28, 0x63, 0x29, 0x4d, 0x52, 0x49, 0x43, 0x48, 0x54, 0x45, 0x52, 0x20, 0x32, 0x30, 0x30, 0x37};
95 AliHLTDataBuffer::~AliHLTDataBuffer()
97 // see header file for function documentation
98 if (--fgNofInstances<=0) {
101 CleanupConsumerList();
104 int AliHLTDataBuffer::SetConsumer(AliHLTComponent* pConsumer)
106 // see header file for function documentation
109 if (FindConsumer(pConsumer)) {
110 HLTWarning("consumer %s (%p) already set to data buffer %p", pConsumer->GetComponentID(), pConsumer, this);
112 AliHLTConsumerDescriptor* pDesc=new AliHLTConsumerDescriptor(pConsumer);
114 fConsumers.push_back(pDesc);
115 HLTDebug("set consumer %s (%p) to data buffer %p", pConsumer->GetComponentID(), pConsumer, this);
117 HLTError("memory allocation failed");
121 HLTError("invalid parameter: consumer component (nil)");
127 int AliHLTDataBuffer::FindMatchingDataBlocks(const AliHLTComponent* pConsumer, vector<AliHLTComponentDataType>* tgtList)
129 // see header file for function documentation
132 vector<AliHLTDataBuffer::AliHLTDataSegment> segments;
133 if ((iResult=FindMatchingDataSegments(pConsumer, segments))>=0) {
135 vector<AliHLTDataBuffer::AliHLTDataSegment>::iterator segment=segments.begin();
136 while (segment!=segments.end()) {
137 tgtList->push_back((*segment).fDataType);
141 iResult=segments.size();
149 int AliHLTDataBuffer::FindMatchingDataSegments(const AliHLTComponent* pConsumer, vector<AliHLTDataBuffer::AliHLTDataSegment>& tgtList)
151 // see header file for function documentation
154 vector<AliHLTComponentDataType> dtlist;
155 ((AliHLTComponent*)pConsumer)->GetInputDataTypes(dtlist);
156 vector<AliHLTDataBuffer::AliHLTDataSegment>::iterator segment=fSegments.begin();
157 while (segment!=fSegments.end()) {
158 vector<AliHLTComponentDataType>::iterator type=dtlist.begin();
159 while (type!=dtlist.end()) {
160 if ((*segment).fDataType==(*type) ||
161 (*type)==kAliHLTAnyDataType) {
162 tgtList.push_back(*segment);
176 int AliHLTDataBuffer::Subscribe(const AliHLTComponent* pConsumer, AliHLTComponentBlockData* arrayBlockDesc, int iArraySize)
178 // see header file for function documentation
180 if (pConsumer && arrayBlockDesc) {
182 AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fConsumers);
184 vector<AliHLTDataBuffer::AliHLTDataSegment> tgtList;
185 /* TODO: think about a good policy for this check
186 * is it enough that at least one segment is available, or have all to be available?
187 * or is it possible to have optional segments?
189 if ((iResult=FindMatchingDataSegments(pConsumer, tgtList))>0) {
191 vector<AliHLTDataBuffer::AliHLTDataSegment>::iterator segment=tgtList.begin();
192 while (segment!=tgtList.end() && i<iArraySize) {
193 // fill the block data descriptor
194 arrayBlockDesc[i].fStructSize=sizeof(AliHLTComponentBlockData);
195 // the shared memory key is not used in AliRoot
196 arrayBlockDesc[i].fShmKey.fStructSize=sizeof(AliHLTComponentShmData);
197 arrayBlockDesc[i].fShmKey.fShmType=gkAliHLTComponentInvalidShmType;
198 arrayBlockDesc[i].fShmKey.fShmID=gkAliHLTComponentInvalidShmID;
199 arrayBlockDesc[i].fOffset=(*segment).fSegmentOffset;
200 arrayBlockDesc[i].fPtr=fpBuffer->fPtr;
201 arrayBlockDesc[i].fSize=(*segment).fSegmentSize;
202 arrayBlockDesc[i].fDataType=(*segment).fDataType;
203 arrayBlockDesc[i].fSpecification=(*segment).fSpecification;
204 pDesc->SetActiveDataSegment(arrayBlockDesc[i].fOffset, arrayBlockDesc[i].fSize);
205 HLTDebug("component %p (%s) subscribed to segment #%d offset %d", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), i, arrayBlockDesc[i].fOffset);
209 // move this consumer to the active list
210 if (ChangeConsumerState(pDesc, fConsumers, fActiveConsumers)>=0) {
211 HLTDebug("component %p (%s) subscribed to data buffer %p", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), this);
213 // TODO: cleanup the consumer descriptor correctly
214 memset(arrayBlockDesc, 0, iArraySize*sizeof(AliHLTComponentBlockData));
215 HLTError("can not activate consumer %p for data buffer %p", pConsumer, this);
219 HLTError("unresolved data segment(s) for component %p (%s)", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID());
223 HLTError("component %p is not a data consumer of data buffer %s", pConsumer, this);
227 HLTError("data buffer %p is empty", this);
231 HLTError("invalid parameter");
237 int AliHLTDataBuffer::Release(AliHLTComponentBlockData* pBlockDesc, const AliHLTComponent* pConsumer)
239 // see header file for function documentation
241 if (pBlockDesc && pConsumer) {
242 AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fActiveConsumers);
244 if ((iResult=pDesc->CheckActiveDataSegment(pBlockDesc->fOffset, pBlockDesc->fSize))!=1) {
245 HLTWarning("data segment missmatch, component %p has not subscribed to a segment with offset %#x and size %d", pConsumer, pBlockDesc->fOffset, pBlockDesc->fSize);
246 // TODO: appropriate error handling, but so far optional
249 pDesc->ReleaseActiveDataSegment(pBlockDesc->fOffset, pBlockDesc->fSize);
250 pBlockDesc->fOffset=0;
251 pBlockDesc->fPtr=NULL;
254 if (pDesc->GetNofActiveSegments()==0) {
255 if ((iResult=ChangeConsumerState(pDesc, fActiveConsumers, fReleasedConsumers))>=0) {
256 if (GetNofActiveConsumers()==0) {
257 // this is the last consumer, reset the consumer list and release the raw buffer
261 HLTError("can not deactivate consumer %p for data buffer %p", pConsumer, this);
266 HLTWarning("component %p has currently not subscribed to the data buffer %p", pConsumer, this);
270 HLTError("inavalid parameter: pBlockDesc=%p pConsumer=%p", pBlockDesc, pConsumer);
276 AliHLTUInt8_t* AliHLTDataBuffer::GetTargetBuffer(int iMinSize)
278 // see header file for function documentation
279 AliHLTUInt8_t* pTargetBuffer=NULL;
280 fpBuffer=CreateRawBuffer(iMinSize);
282 pTargetBuffer=(AliHLTUInt8_t*)fpBuffer->fPtr;
284 HLTError("can not create raw buffer");
286 return pTargetBuffer;
289 int AliHLTDataBuffer::SetSegments(AliHLTUInt8_t* pTgt, AliHLTComponentBlockData* arrayBlockData, int iSize)
291 // see header file for function documentation
293 if (pTgt && arrayBlockData && iSize>=0) {
295 if (fpBuffer->fPtr==(void*)pTgt) {
296 AliHLTDataBuffer::AliHLTDataSegment segment;
297 for (int i=0; i<iSize; i++) {
298 if (arrayBlockData[i].fOffset+arrayBlockData[i].fSize<=fpBuffer->fSize) {
299 segment.fSegmentOffset=arrayBlockData[i].fOffset;
300 segment.fSegmentSize=arrayBlockData[i].fSize;
301 segment.fDataType=arrayBlockData[i].fDataType;
302 segment.fSpecification=arrayBlockData[i].fSpecification;
303 fSegments.push_back(segment);
304 HLTDebug("set segment %s with size %d at offset %d", AliHLTComponent::DataType2Text(segment.fDataType).data(), segment.fSegmentSize, segment.fSegmentOffset);
306 HLTError("block data specification %#d (%s) exceeds size of data buffer", i, AliHLTComponent::DataType2Text(arrayBlockData[i].fDataType).data());
307 HLTError("block offset=%d, block size=%d, buffer size=%d", arrayBlockData[i].fOffset, arrayBlockData[i].fSize, fpBuffer->fSize);
312 HLTError("this data buffer (%p) does not match the internal data buffer %p of raw buffer %p", pTgt, fpBuffer->fPtr, fpBuffer);
316 HLTFatal("internal data structur missmatch");
320 HLTError("invalid parameter: pTgtBuffer=%p arrayBlockData=%p", pTgt, arrayBlockData);
326 int AliHLTDataBuffer::IsEmpty()
328 // see header file for function documentation
329 int iResult=fpBuffer==NULL || GetNofSegments()==0;
333 int AliHLTDataBuffer::GetNofSegments()
335 // see header file for function documentation
336 int iResult=fSegments.size();
340 int AliHLTDataBuffer::GetNofConsumers()
342 // see header file for function documentation
343 int iResult=fConsumers.size() + GetNofActiveConsumers() + fReleasedConsumers.size();
347 int AliHLTDataBuffer::GetNofActiveConsumers()
349 // see header file for function documentation
350 int iResult=fActiveConsumers.size();
354 AliHLTDataBuffer::AliHLTRawBuffer* AliHLTDataBuffer::CreateRawBuffer(AliHLTUInt32_t size)
356 // see header file for function documentation
357 AliHLTRawBuffer* pRawBuffer=NULL;
358 unsigned int reqSize=size+fgkSafetyPatternSize;
359 vector<AliHLTRawBuffer*>::iterator buffer=fgFreeBuffers.begin();
360 while (buffer!=fgFreeBuffers.end() && pRawBuffer==NULL) {
361 if ((*buffer)->fTotalSize>=reqSize && ((*buffer)->fTotalSize-reqSize)<fgMargin) {
362 // assign this element
364 pRawBuffer->fSize=size;
365 fgFreeBuffers.erase(buffer);
366 fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "raw buffer container %p provided for request of %d bytes (total %d available in buffer %p)", pRawBuffer, size, pRawBuffer->fTotalSize, pRawBuffer->fPtr);
367 fgActiveBuffers.push_back(pRawBuffer);
372 if (pRawBuffer==NULL) {
373 // no buffer found, create a new one
374 pRawBuffer=new AliHLTRawBuffer;
376 pRawBuffer->fPtr=malloc(reqSize);
377 if (pRawBuffer->fPtr) {
378 pRawBuffer->fSize=size;
379 pRawBuffer->fTotalSize=reqSize;
380 fgActiveBuffers.push_back(pRawBuffer);
381 fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "new raw buffer %p of size %d created (container %p)", pRawBuffer->fPtr, pRawBuffer->fTotalSize, pRawBuffer);
382 if (fgkSafetyPatternSize>0) {
383 memcpy(((char*)pRawBuffer->fPtr)+pRawBuffer->fSize, fgkSafetyPattern, fgkSafetyPatternSize);
388 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
391 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
397 int AliHLTDataBuffer::ReleaseRawBuffer(AliHLTRawBuffer* pBuffer)
399 // see header file for function documentation
402 vector<AliHLTRawBuffer*>::iterator buffer=fgActiveBuffers.begin();
403 while (buffer!=fgActiveBuffers.end() && (*buffer)!=pBuffer) {
406 if (buffer!=fgActiveBuffers.end()) {
407 if (fgkSafetyPatternSize>0) {
408 if (memcmp(((char*)(*buffer)->fPtr)+(*buffer)->fSize, fgkSafetyPattern, fgkSafetyPatternSize)!=0) {
409 fgLogging.Logging(kHLTLogFatal, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "component has written beyond end of data buffer %p size %d", (*buffer)->fPtr, (*buffer)->fSize);
413 fgFreeBuffers.push_back(*buffer);
414 fgActiveBuffers.erase(buffer);
416 fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "can not find raw buffer container %p in the list of active containers", pBuffer);
420 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "invalid parameter");
427 int AliHLTDataBuffer::DeleteRawBuffers()
429 // see header file for function documentation
431 vector<AliHLTRawBuffer*>::iterator buffer=fgFreeBuffers.begin();
432 while (buffer!=fgFreeBuffers.end()) {
433 free((*buffer)->fPtr);
435 fgFreeBuffers.erase(buffer);
436 buffer=fgFreeBuffers.begin();
438 buffer=fgActiveBuffers.begin();
439 while (buffer!=fgActiveBuffers.end()) {
440 fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "request to delete active raw buffer container (raw buffer %p, size %d)", (*buffer)->fPtr, (*buffer)->fTotalSize);
441 free((*buffer)->fPtr);
443 fgActiveBuffers.erase(buffer);
444 buffer=fgActiveBuffers.begin();
449 AliHLTConsumerDescriptor* AliHLTDataBuffer::FindConsumer(const AliHLTComponent* pConsumer, vector<AliHLTConsumerDescriptor*> &list) const
451 // see header file for function documentation
452 AliHLTConsumerDescriptor* pDesc=NULL;
453 vector<AliHLTConsumerDescriptor*>::iterator desc=list.begin();
454 while (desc!=list.end() && pDesc==NULL) {
455 if ((pConsumer==NULL || (*desc)->GetComponent()==pConsumer)) {
463 int AliHLTDataBuffer::ResetDataBuffer()
465 // see header file for function documentation
467 AliHLTRawBuffer* pBuffer=fpBuffer;
470 // cleanup consumer states
471 vector<AliHLTConsumerDescriptor*>::iterator desc=fReleasedConsumers.begin();
472 while (desc!=fReleasedConsumers.end()) {
473 AliHLTConsumerDescriptor* pDesc=*desc;
474 fReleasedConsumers.erase(desc);
475 desc=fReleasedConsumers.begin();
476 fConsumers.push_back(pDesc);
478 desc=fActiveConsumers.begin();
479 while (desc!=fActiveConsumers.end()) {
480 AliHLTConsumerDescriptor* pDesc=*desc;
481 HLTWarning("consumer %p was not released", pDesc);
482 fActiveConsumers.erase(desc);
483 desc=fActiveConsumers.begin();
484 fConsumers.push_back(pDesc);
488 vector<AliHLTDataBuffer::AliHLTDataSegment>::iterator segment=fSegments.begin();
489 while (segment!=fSegments.end()) {
490 fSegments.erase(segment);
491 segment=fSegments.begin();
494 // cleanup raw buffer
496 ReleaseRawBuffer(pBuffer);
501 int AliHLTDataBuffer::Reset()
503 // see header file for function documentation
504 return ResetDataBuffer();
507 // this is the version which works on lists of components instead of consumer descriptors
508 // int AliHLTDataBuffer::ChangeConsumerState(AliHLTComponent* pConsumer, vector<AliHLTComponent*> &srcList, vector<AliHLTComponent*> &tgtList)
512 // vector<AliHLTComponent*>::iterator desc=srcList.begin();
513 // while (desc!=srcList.end()) {
514 // if ((*desc)==pConsumer) {
515 // srcList.erase(desc);
516 // tgtList.push_back(pConsumer);
521 // if (desc==srcList.end()) {
522 // HLTError("can not find consumer component %p in list", pConsumer);
526 // HLTError("invalid parameter");
532 int AliHLTDataBuffer::ChangeConsumerState(AliHLTConsumerDescriptor* pDesc, vector<AliHLTConsumerDescriptor*> &srcList, vector<AliHLTConsumerDescriptor*> &tgtList)
534 // see header file for function documentation
537 vector<AliHLTConsumerDescriptor*>::iterator desc=srcList.begin();
538 while (desc!=srcList.end()) {
539 if ((*desc)==pDesc) {
541 tgtList.push_back(pDesc);
548 HLTError("can not find consumer descriptor %p in list", pDesc);
551 HLTError("invalid parameter");
557 int AliHLTDataBuffer::CleanupConsumerList()
559 // see header file for function documentation
562 vector<AliHLTConsumerDescriptor*>::iterator desc=fConsumers.begin();
563 while (desc!=fConsumers.end()) {
565 fConsumers.erase(desc);
566 desc=fConsumers.begin();
571 int AliHLTDataBuffer::FindConsumer(AliHLTComponent* pConsumer, int bAllLists)
573 // see header file for function documentation
574 vector<AliHLTConsumerDescriptor*>::iterator desc=fConsumers.begin();
575 while (desc!=fConsumers.end()) {
576 if ((*desc)->GetComponent()==pConsumer)
580 if (bAllLists==0) return 0;
582 desc=fActiveConsumers.begin();
583 while (desc!=fActiveConsumers.end()) {
584 if ((*desc)->GetComponent()==pConsumer)
588 desc=fReleasedConsumers.begin();
589 while (desc!=fReleasedConsumers.end()) {
590 if ((*desc)->GetComponent()==pConsumer)