3 /**************************************************************************
4 * This file is property of and copyright by the ALICE HLT Project *
5 * ALICE Experiment at CERN, All rights reserved. *
7 * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 * for The ALICE HLT Project. *
10 * Permission to use, copy, modify and distribute this software and its *
11 * documentation strictly for non-commercial purposes is hereby granted *
12 * without fee, provided that the above copyright notice appears in all *
13 * copies and that both the copyright notice and this permission notice *
14 * appear in the supporting documentation. The authors make no claims *
15 * about the suitability of this software for any purpose. It is *
16 * provided "as is" without express or implied warranty. *
17 **************************************************************************/
19 /** @file AliHLTDataBuffer.cxx
20 @author Matthias Richter
22 @brief Handling of Data Buffers for HLT components.
25 // see header file for class documentation
27 // refer to README to build package
29 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
35 #include "AliHLTDataBuffer.h"
36 #include "AliHLTConsumerDescriptor.h"
37 #include "AliHLTComponent.h"
40 //#include "AliHLTSystem.h"
42 /** ROOT macro for the implementation of ROOT specific class methods */
43 ClassImp(AliHLTDataBuffer)
45 AliHLTDataBuffer::AliHLTDataBuffer()
54 // see header file for class documentation
56 // refer to README to build package
58 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
61 fActiveConsumers.empty();
62 fReleasedConsumers.empty();
66 AliHLTDataBuffer::AliHLTDataBuffer(const AliHLTDataBuffer&)
77 // see header file for function documentation
78 HLTFatal("copy constructor untested");
81 AliHLTDataBuffer& AliHLTDataBuffer::operator=(const AliHLTDataBuffer&)
83 // see header file for function documentation
84 HLTFatal("assignment operator untested");
88 int AliHLTDataBuffer::fgNofInstances=0;
89 vector<AliHLTDataBuffer::AliHLTRawBuffer*> AliHLTDataBuffer::fgFreeBuffers;
90 vector<AliHLTDataBuffer::AliHLTRawBuffer*> AliHLTDataBuffer::fgActiveBuffers;
91 AliHLTUInt32_t AliHLTDataBuffer::fgMargin=1024;
92 AliHLTLogging AliHLTDataBuffer::fgLogging;
93 const Int_t AliHLTDataBuffer::fgkSafetyPatternSize=16;
94 const char AliHLTDataBuffer::fgkSafetyPattern[]={0x28, 0x63, 0x29, 0x4d, 0x52, 0x49, 0x43, 0x48, 0x54, 0x45, 0x52, 0x20, 0x32, 0x30, 0x30, 0x37};
96 AliHLTDataBuffer::~AliHLTDataBuffer()
98 // see header file for function documentation
99 if (--fgNofInstances<=0) {
102 CleanupConsumerList();
105 int AliHLTDataBuffer::SetConsumer(AliHLTComponent* pConsumer)
107 // see header file for function documentation
110 if (FindConsumer(pConsumer)) {
111 HLTWarning("consumer %s (%p) already set to data buffer %p", pConsumer->GetComponentID(), pConsumer, this);
113 AliHLTConsumerDescriptor* pDesc=new AliHLTConsumerDescriptor(pConsumer);
115 fConsumers.push_back(pDesc);
116 HLTDebug("set consumer %s (%p) to data buffer %p", pConsumer->GetComponentID(), pConsumer, this);
118 HLTError("memory allocation failed");
122 HLTError("invalid parameter: consumer component (nil)");
128 int AliHLTDataBuffer::FindMatchingDataBlocks(const AliHLTComponent* pConsumer, vector<AliHLTComponentDataType>* tgtList)
130 // see header file for function documentation
133 vector<AliHLTDataBuffer::AliHLTDataSegment> segments;
134 if ((iResult=FindMatchingDataSegments(pConsumer, segments))>=0) {
136 vector<AliHLTDataBuffer::AliHLTDataSegment>::iterator segment=segments.begin();
137 while (segment!=segments.end()) {
138 tgtList->push_back((*segment).fDataType);
142 iResult=segments.size();
150 int AliHLTDataBuffer::FindMatchingDataSegments(const AliHLTComponent* pConsumer, vector<AliHLTDataBuffer::AliHLTDataSegment>& tgtList)
152 // see header file for function documentation
155 vector<AliHLTComponentDataType> dtlist;
156 ((AliHLTComponent*)pConsumer)->GetInputDataTypes(dtlist);
157 vector<AliHLTDataBuffer::AliHLTDataSegment>::iterator segment=fSegments.begin();
158 while (segment!=fSegments.end()) {
159 vector<AliHLTComponentDataType>::iterator type=dtlist.begin();
160 while (type!=dtlist.end()) {
161 if ((*segment).fDataType==(*type) ||
162 (*type)==kAliHLTAnyDataType) {
163 tgtList.push_back(*segment);
177 int AliHLTDataBuffer::Subscribe(const AliHLTComponent* pConsumer, AliHLTComponentBlockData* arrayBlockDesc, int iArraySize)
179 // see header file for function documentation
181 if (pConsumer && arrayBlockDesc) {
183 AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fConsumers);
185 vector<AliHLTDataBuffer::AliHLTDataSegment> tgtList;
186 /* TODO: think about a good policy for this check
187 * is it enough that at least one segment is available, or have all to be available?
188 * or is it possible to have optional segments?
190 if ((iResult=FindMatchingDataSegments(pConsumer, tgtList))>0) {
192 vector<AliHLTDataBuffer::AliHLTDataSegment>::iterator segment=tgtList.begin();
193 while (segment!=tgtList.end() && i<iArraySize) {
194 // fill the block data descriptor
195 arrayBlockDesc[i].fStructSize=sizeof(AliHLTComponentBlockData);
196 // the shared memory key is not used in AliRoot
197 arrayBlockDesc[i].fShmKey.fStructSize=sizeof(AliHLTComponentShmData);
198 arrayBlockDesc[i].fShmKey.fShmType=gkAliHLTComponentInvalidShmType;
199 arrayBlockDesc[i].fShmKey.fShmID=gkAliHLTComponentInvalidShmID;
200 arrayBlockDesc[i].fOffset=(*segment).fSegmentOffset;
201 arrayBlockDesc[i].fPtr=*fpBuffer;
202 arrayBlockDesc[i].fSize=(*segment).fSegmentSize;
203 arrayBlockDesc[i].fDataType=(*segment).fDataType;
204 arrayBlockDesc[i].fSpecification=(*segment).fSpecification;
205 pDesc->SetActiveDataSegment(arrayBlockDesc[i].fOffset, arrayBlockDesc[i].fSize);
206 HLTDebug("component %p (%s) subscribed to segment #%d offset %d size %d data type %s %#x",
207 pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), i, arrayBlockDesc[i].fOffset,
208 arrayBlockDesc[i].fSize, (AliHLTComponent::DataType2Text(arrayBlockDesc[i].fDataType)).c_str(),
209 arrayBlockDesc[i].fSpecification);
213 // move this consumer to the active list
214 if (ChangeConsumerState(pDesc, fConsumers, fActiveConsumers)>=0) {
215 HLTDebug("component %p (%s) subscribed to data buffer %p", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), this);
217 // TODO: cleanup the consumer descriptor correctly
218 memset(arrayBlockDesc, 0, iArraySize*sizeof(AliHLTComponentBlockData));
219 HLTError("can not activate consumer %p for data buffer %p", pConsumer, this);
223 HLTError("unresolved data segment(s) for component %p (%s)", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID());
227 HLTError("component %p is not a data consumer of data buffer %s", pConsumer, this);
231 HLTError("data buffer %p is empty", this);
235 HLTError("invalid parameter");
241 int AliHLTDataBuffer::Release(AliHLTComponentBlockData* pBlockDesc, const AliHLTComponent* pConsumer)
243 // see header file for function documentation
245 if (pBlockDesc && pConsumer) {
246 AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fActiveConsumers);
248 if ((iResult=pDesc->CheckActiveDataSegment(pBlockDesc->fOffset, pBlockDesc->fSize))!=1) {
249 HLTWarning("data segment missmatch, component %p has not subscribed to a segment with offset %#x and size %d", pConsumer, pBlockDesc->fOffset, pBlockDesc->fSize);
250 // TODO: appropriate error handling, but so far optional
253 pDesc->ReleaseActiveDataSegment(pBlockDesc->fOffset, pBlockDesc->fSize);
254 pBlockDesc->fOffset=0;
255 pBlockDesc->fPtr=NULL;
258 if (pDesc->GetNofActiveSegments()==0) {
259 if ((iResult=ChangeConsumerState(pDesc, fActiveConsumers, fReleasedConsumers))>=0) {
260 if (GetNofActiveConsumers()==0) {
261 // this is the last consumer, reset the consumer list and release the raw buffer
265 HLTError("can not deactivate consumer %p for data buffer %p", pConsumer, this);
270 HLTWarning("component %p has currently not subscribed to the data buffer %p", pConsumer, this);
274 HLTError("inavalid parameter: pBlockDesc=%p pConsumer=%p", pBlockDesc, pConsumer);
280 AliHLTUInt8_t* AliHLTDataBuffer::GetTargetBuffer(int iMinSize)
282 // see header file for function documentation
283 AliHLTUInt8_t* pTargetBuffer=NULL;
284 if (fpBuffer!=NULL) {
285 HLTWarning("data buffer not properly reset, possible memory leak\n");
287 fpBuffer=CreateRawBuffer(iMinSize);
289 pTargetBuffer=*fpBuffer;
291 HLTError("can not create raw buffer");
293 return pTargetBuffer;
296 int AliHLTDataBuffer::SetSegments(AliHLTUInt8_t* pTgt, AliHLTComponentBlockData* arrayBlockData, int iSize)
298 // see header file for function documentation
300 if (pTgt && arrayBlockData && iSize>=0) {
302 if (*fpBuffer==pTgt) {
303 AliHLTDataBuffer::AliHLTDataSegment segment;
304 for (int i=0; i<iSize; i++) {
305 if ((*fpBuffer)<=arrayBlockData[i].fPtr && (*fpBuffer)>arrayBlockData[i].fPtr) {
306 int ptrOffset=(*fpBuffer)-arrayBlockData[i].fPtr;
307 if (arrayBlockData[i].fOffset+ptrOffset+arrayBlockData[i].fSize<=fpBuffer->fSize) {
308 segment.fSegmentOffset=arrayBlockData[i].fOffset+ptrOffset;
309 segment.fSegmentSize=arrayBlockData[i].fSize;
310 segment.fDataType=arrayBlockData[i].fDataType;
311 segment.fSpecification=arrayBlockData[i].fSpecification;
312 fSegments.push_back(segment);
313 HLTDebug("set segment %s with size %d at offset %d", AliHLTComponent::DataType2Text(segment.fDataType).data(), segment.fSegmentSize, segment.fSegmentOffset);
315 HLTError("block data specification %#d (%s) exceeds size of data buffer", i, AliHLTComponent::DataType2Text(arrayBlockData[i].fDataType).data());
316 HLTError("block offset=%d, block size=%d, buffer size=%d", arrayBlockData[i].fOffset, arrayBlockData[i].fSize, fpBuffer->fSize);
320 HLTError("invalid pointer (%p) in block data specification (buffer %p size %d)", arrayBlockData[i].fPtr, fpBuffer->fPtr, fpBuffer->fSize);
325 HLTError("this data buffer (%p) does not match the internal data buffer %p of raw buffer %p", pTgt, fpBuffer->fPtr, fpBuffer);
329 HLTFatal("internal data structur missmatch");
333 HLTError("invalid parameter: pTgtBuffer=%p arrayBlockData=%p", pTgt, arrayBlockData);
339 int AliHLTDataBuffer::IsEmpty()
341 // see header file for function documentation
342 int iResult=fpBuffer==NULL || GetNofSegments()==0;
346 int AliHLTDataBuffer::GetNofSegments()
348 // see header file for function documentation
349 int iResult=fSegments.size();
353 int AliHLTDataBuffer::GetNofConsumers()
355 // see header file for function documentation
356 int iResult=fConsumers.size() + GetNofActiveConsumers() + fReleasedConsumers.size();
360 int AliHLTDataBuffer::GetNofActiveConsumers()
362 // see header file for function documentation
363 int iResult=fActiveConsumers.size();
367 AliHLTDataBuffer::AliHLTRawBuffer* AliHLTDataBuffer::CreateRawBuffer(AliHLTUInt32_t size)
369 // see header file for function documentation
370 AliHLTRawBuffer* pRawBuffer=NULL;
371 unsigned int reqSize=size+fgkSafetyPatternSize;
372 vector<AliHLTRawBuffer*>::iterator buffer=fgFreeBuffers.begin();
373 while (buffer!=fgFreeBuffers.end() && pRawBuffer==NULL) {
374 if ((*buffer)->fTotalSize>=reqSize && ((*buffer)->fTotalSize-reqSize)<fgMargin) {
375 // assign this element
377 pRawBuffer->fSize=size;
378 fgFreeBuffers.erase(buffer);
379 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);
380 fgActiveBuffers.push_back(pRawBuffer);
385 if (pRawBuffer==NULL) {
386 // no buffer found, create a new one
387 pRawBuffer=new AliHLTRawBuffer;
389 pRawBuffer->fPtr=static_cast<AliHLTUInt8_t*>(malloc(reqSize));
390 if (pRawBuffer->fPtr) {
391 pRawBuffer->fSize=size;
392 pRawBuffer->fTotalSize=reqSize;
393 fgActiveBuffers.push_back(pRawBuffer);
394 fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "new raw buffer %p of size %d created (container %p)", pRawBuffer->fPtr, pRawBuffer->fTotalSize, pRawBuffer);
395 if (fgkSafetyPatternSize>0) {
396 memcpy(((char*)pRawBuffer->fPtr)+pRawBuffer->fSize, fgkSafetyPattern, fgkSafetyPatternSize);
401 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
404 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
410 int AliHLTDataBuffer::ReleaseRawBuffer(AliHLTRawBuffer* pBuffer)
412 // see header file for function documentation
415 vector<AliHLTRawBuffer*>::iterator buffer=fgActiveBuffers.begin();
416 while (buffer!=fgActiveBuffers.end() && (*buffer)!=pBuffer) {
419 if (buffer!=fgActiveBuffers.end()) {
420 if (fgkSafetyPatternSize>0) {
421 if (memcmp(((char*)(*buffer)->fPtr)+(*buffer)->fSize, fgkSafetyPattern, fgkSafetyPatternSize)!=0) {
422 fgLogging.Logging(kHLTLogFatal, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "component has written beyond end of data buffer %p size %d", (*buffer)->fPtr, (*buffer)->fSize);
426 fgFreeBuffers.push_back(*buffer);
427 fgActiveBuffers.erase(buffer);
429 fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "can not find raw buffer container %p in the list of active containers", pBuffer);
433 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "invalid parameter");
440 int AliHLTDataBuffer::DeleteRawBuffers()
442 // see header file for function documentation
444 vector<AliHLTRawBuffer*>::iterator buffer=fgFreeBuffers.begin();
445 while (buffer!=fgFreeBuffers.end()) {
446 free((*buffer)->fPtr);
448 fgFreeBuffers.erase(buffer);
449 buffer=fgFreeBuffers.begin();
451 buffer=fgActiveBuffers.begin();
452 while (buffer!=fgActiveBuffers.end()) {
453 fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "request to delete active raw buffer container (raw buffer %p, size %d)", (*buffer)->fPtr, (*buffer)->fTotalSize);
454 free((*buffer)->fPtr);
456 fgActiveBuffers.erase(buffer);
457 buffer=fgActiveBuffers.begin();
462 AliHLTConsumerDescriptor* AliHLTDataBuffer::FindConsumer(const AliHLTComponent* pConsumer, vector<AliHLTConsumerDescriptor*> &list) const
464 // see header file for function documentation
465 AliHLTConsumerDescriptor* pDesc=NULL;
466 vector<AliHLTConsumerDescriptor*>::iterator desc=list.begin();
467 while (desc!=list.end() && pDesc==NULL) {
468 if ((pConsumer==NULL || (*desc)->GetComponent()==pConsumer)) {
476 int AliHLTDataBuffer::ResetDataBuffer()
478 // see header file for function documentation
480 AliHLTRawBuffer* pBuffer=fpBuffer;
483 // cleanup consumer states
484 vector<AliHLTConsumerDescriptor*>::iterator desc=fReleasedConsumers.begin();
485 while (desc!=fReleasedConsumers.end()) {
486 AliHLTConsumerDescriptor* pDesc=*desc;
487 fReleasedConsumers.erase(desc);
488 desc=fReleasedConsumers.begin();
489 fConsumers.push_back(pDesc);
491 desc=fActiveConsumers.begin();
492 while (desc!=fActiveConsumers.end()) {
493 AliHLTConsumerDescriptor* pDesc=*desc;
494 HLTWarning("consumer %p was not released", pDesc);
495 fActiveConsumers.erase(desc);
496 desc=fActiveConsumers.begin();
497 fConsumers.push_back(pDesc);
501 vector<AliHLTDataBuffer::AliHLTDataSegment>::iterator segment=fSegments.begin();
502 while (segment!=fSegments.end()) {
503 fSegments.erase(segment);
504 segment=fSegments.begin();
507 // cleanup raw buffer
509 ReleaseRawBuffer(pBuffer);
514 int AliHLTDataBuffer::Reset()
516 // see header file for function documentation
517 return ResetDataBuffer();
520 // this is the version which works on lists of components instead of consumer descriptors
521 // int AliHLTDataBuffer::ChangeConsumerState(AliHLTComponent* pConsumer, vector<AliHLTComponent*> &srcList, vector<AliHLTComponent*> &tgtList)
525 // vector<AliHLTComponent*>::iterator desc=srcList.begin();
526 // while (desc!=srcList.end()) {
527 // if ((*desc)==pConsumer) {
528 // srcList.erase(desc);
529 // tgtList.push_back(pConsumer);
534 // if (desc==srcList.end()) {
535 // HLTError("can not find consumer component %p in list", pConsumer);
539 // HLTError("invalid parameter");
545 int AliHLTDataBuffer::ChangeConsumerState(AliHLTConsumerDescriptor* pDesc, vector<AliHLTConsumerDescriptor*> &srcList, vector<AliHLTConsumerDescriptor*> &tgtList)
547 // see header file for function documentation
550 vector<AliHLTConsumerDescriptor*>::iterator desc=srcList.begin();
551 while (desc!=srcList.end()) {
552 if ((*desc)==pDesc) {
554 tgtList.push_back(pDesc);
561 HLTError("can not find consumer descriptor %p in list", pDesc);
564 HLTError("invalid parameter");
570 int AliHLTDataBuffer::CleanupConsumerList()
572 // see header file for function documentation
575 vector<AliHLTConsumerDescriptor*>::iterator desc=fConsumers.begin();
576 while (desc!=fConsumers.end()) {
578 fConsumers.erase(desc);
579 desc=fConsumers.begin();
584 int AliHLTDataBuffer::FindConsumer(AliHLTComponent* pConsumer, int bAllLists)
586 // see header file for function documentation
587 vector<AliHLTConsumerDescriptor*>::iterator desc=fConsumers.begin();
588 while (desc!=fConsumers.end()) {
589 if ((*desc)->GetComponent()==pConsumer)
593 if (bAllLists==0) return 0;
595 desc=fActiveConsumers.begin();
596 while (desc!=fActiveConsumers.end()) {
597 if ((*desc)->GetComponent()==pConsumer)
601 desc=fReleasedConsumers.begin();
602 while (desc!=fReleasedConsumers.end()) {
603 if ((*desc)->GetComponent()==pConsumer)
610 int AliHLTDataBuffer::AliHLTRawBuffer::operator==(void* ptr)
612 return fPtr == static_cast<AliHLTUInt8_t*>(ptr);
615 int AliHLTDataBuffer::AliHLTRawBuffer::operator<=(void* ptr)
617 int iResult=fPtr <= static_cast<AliHLTUInt8_t*>(ptr);
618 //printf("%p: %p <= %p (%d)\n", this, fPtr, ptr, iResult);
622 int AliHLTDataBuffer::AliHLTRawBuffer::operator>(void* ptr)
624 int iResult=fPtr+fSize > static_cast<AliHLTUInt8_t*>(ptr);
625 //printf("%p: %p + %d > %p (%d)\n", this, fPtr, fSize, ptr, iResult);
629 int AliHLTDataBuffer::AliHLTRawBuffer::operator-(void* ptr)
631 return static_cast<int>(static_cast<AliHLTUInt8_t*>(ptr)-fPtr);