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.
28 #include "AliHLTDataBuffer.h"
29 #include "AliHLTConsumerDescriptor.h"
30 #include "AliHLTComponent.h"
32 #include "AliHLTSystem.h"
34 /** ROOT macro for the implementation of ROOT specific class methods */
35 ClassImp(AliHLTDataBuffer)
37 AliHLTDataBuffer::AliHLTDataBuffer()
46 // see header file for class documentation
48 // refer to README to build package
50 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
53 fActiveConsumers.empty();
54 fReleasedConsumers.empty();
58 AliHLTDataBuffer::AliHLTDataBuffer(const AliHLTDataBuffer&)
69 // see header file for function documentation
70 HLTFatal("copy constructor untested");
73 AliHLTDataBuffer& AliHLTDataBuffer::operator=(const AliHLTDataBuffer&)
75 // see header file for function documentation
76 HLTFatal("assignment operator untested");
80 int AliHLTDataBuffer::fgNofInstances=0;
81 vector<AliHLTDataBuffer::AliHLTRawBuffer*> AliHLTDataBuffer::fgFreeBuffers;
82 vector<AliHLTDataBuffer::AliHLTRawBuffer*> AliHLTDataBuffer::fgActiveBuffers;
83 AliHLTUInt32_t AliHLTDataBuffer::fgMargin=1024;
84 AliHLTLogging AliHLTDataBuffer::fgLogging;
85 const Int_t AliHLTDataBuffer::fgkSafetyPatternSize=16;
86 const char AliHLTDataBuffer::fgkSafetyPattern[]={0x28, 0x63, 0x29, 0x4d, 0x52, 0x49, 0x43, 0x48, 0x54, 0x45, 0x52, 0x20, 0x32, 0x30, 0x30, 0x37};
88 AliHLTDataBuffer::~AliHLTDataBuffer()
90 // see header file for function documentation
91 if (--fgNofInstances<=0) {
94 CleanupConsumerList();
97 int AliHLTDataBuffer::SetConsumer(AliHLTComponent* pConsumer)
99 // see header file for function documentation
102 if (FindConsumer(pConsumer)) {
103 HLTWarning("consumer %s (%p) already set to data buffer %p", pConsumer->GetComponentID(), pConsumer, this);
105 AliHLTConsumerDescriptor* pDesc=new AliHLTConsumerDescriptor(pConsumer);
107 fConsumers.push_back(pDesc);
108 HLTDebug("set consumer %s (%p) to data buffer %p", pConsumer->GetComponentID(), pConsumer, this);
110 HLTError("memory allocation failed");
114 HLTError("invalid parameter: consumer component (nil)");
120 int AliHLTDataBuffer::FindMatchingDataBlocks(const AliHLTComponent* pConsumer, vector<AliHLTComponentDataType>* tgtList)
122 // see header file for function documentation
125 vector<AliHLTDataBuffer::AliHLTDataSegment> segments;
126 if ((iResult=FindMatchingDataSegments(pConsumer, segments))>=0) {
128 vector<AliHLTDataBuffer::AliHLTDataSegment>::iterator segment=segments.begin();
129 while (segment!=segments.end()) {
130 tgtList->push_back((*segment).fDataType);
134 iResult=segments.size();
142 int AliHLTDataBuffer::FindMatchingDataSegments(const AliHLTComponent* pConsumer, vector<AliHLTDataBuffer::AliHLTDataSegment>& tgtList)
144 // see header file for function documentation
147 vector<AliHLTComponentDataType> dtlist;
148 ((AliHLTComponent*)pConsumer)->GetInputDataTypes(dtlist);
149 vector<AliHLTDataBuffer::AliHLTDataSegment>::iterator segment=fSegments.begin();
150 while (segment!=fSegments.end()) {
151 vector<AliHLTComponentDataType>::iterator type=dtlist.begin();
152 while (type!=dtlist.end()) {
153 if ((*segment).fDataType==(*type) ||
154 (*type)==kAliHLTAnyDataType) {
155 tgtList.push_back(*segment);
169 int AliHLTDataBuffer::Subscribe(const AliHLTComponent* pConsumer, AliHLTComponentBlockData* arrayBlockDesc, int iArraySize)
171 // see header file for function documentation
173 if (pConsumer && arrayBlockDesc) {
175 AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fConsumers);
177 vector<AliHLTDataBuffer::AliHLTDataSegment> tgtList;
178 /* TODO: think about a good policy for this check
179 * is it enough that at least one segment is available, or have all to be available?
180 * or is it possible to have optional segments?
182 if ((iResult=FindMatchingDataSegments(pConsumer, tgtList))>0) {
184 vector<AliHLTDataBuffer::AliHLTDataSegment>::iterator segment=tgtList.begin();
185 while (segment!=tgtList.end() && i<iArraySize) {
186 // fill the block data descriptor
187 arrayBlockDesc[i].fStructSize=sizeof(AliHLTComponentBlockData);
188 // the shared memory key is not used in AliRoot
189 arrayBlockDesc[i].fShmKey.fStructSize=sizeof(AliHLTComponentShmData);
190 arrayBlockDesc[i].fShmKey.fShmType=gkAliHLTComponentInvalidShmType;
191 arrayBlockDesc[i].fShmKey.fShmID=gkAliHLTComponentInvalidShmID;
192 arrayBlockDesc[i].fOffset=(*segment).fSegmentOffset;
193 arrayBlockDesc[i].fPtr=fpBuffer->fPtr;
194 arrayBlockDesc[i].fSize=(*segment).fSegmentSize;
195 arrayBlockDesc[i].fDataType=(*segment).fDataType;
196 arrayBlockDesc[i].fSpecification=(*segment).fSpecification;
197 pDesc->SetActiveDataSegment(arrayBlockDesc[i].fOffset, arrayBlockDesc[i].fSize);
198 HLTDebug("component %p (%s) subscribed to segment #%d offset %d", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), i, arrayBlockDesc[i].fOffset);
202 // move this consumer to the active list
203 if (ChangeConsumerState(pDesc, fConsumers, fActiveConsumers)>=0) {
204 HLTDebug("component %p (%s) subscribed to data buffer %p", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), this);
206 // TODO: cleanup the consumer descriptor correctly
207 memset(arrayBlockDesc, 0, iArraySize*sizeof(AliHLTComponentBlockData));
208 HLTError("can not activate consumer %p for data buffer %p", pConsumer, this);
212 HLTError("unresolved data segment(s) for component %p (%s)", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID());
216 HLTError("component %p is not a data consumer of data buffer %s", pConsumer, this);
220 HLTError("data buffer %p is empty", this);
224 HLTError("invalid parameter");
230 int AliHLTDataBuffer::Release(AliHLTComponentBlockData* pBlockDesc, const AliHLTComponent* pConsumer)
232 // see header file for function documentation
234 if (pBlockDesc && pConsumer) {
235 AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fActiveConsumers);
237 if ((iResult=pDesc->CheckActiveDataSegment(pBlockDesc->fOffset, pBlockDesc->fSize))!=1) {
238 HLTWarning("data segment missmatch, component %p has not subscribed to a segment with offset %#x and size %d", pConsumer, pBlockDesc->fOffset, pBlockDesc->fSize);
239 // TODO: appropriate error handling, but so far optional
242 pDesc->ReleaseActiveDataSegment(pBlockDesc->fOffset, pBlockDesc->fSize);
243 pBlockDesc->fOffset=0;
244 pBlockDesc->fPtr=NULL;
247 if (pDesc->GetNofActiveSegments()==0) {
248 if ((iResult=ChangeConsumerState(pDesc, fActiveConsumers, fReleasedConsumers))>=0) {
249 if (GetNofActiveConsumers()==0) {
250 // this is the last consumer, reset the consumer list and release the raw buffer
254 HLTError("can not deactivate consumer %p for data buffer %p", pConsumer, this);
259 HLTWarning("component %p has currently not subscribed to the data buffer %p", pConsumer, this);
263 HLTError("inavalid parameter: pBlockDesc=%p pConsumer=%p", pBlockDesc, pConsumer);
269 AliHLTUInt8_t* AliHLTDataBuffer::GetTargetBuffer(int iMinSize)
271 // see header file for function documentation
272 AliHLTUInt8_t* pTargetBuffer=NULL;
273 fpBuffer=CreateRawBuffer(iMinSize);
275 pTargetBuffer=(AliHLTUInt8_t*)fpBuffer->fPtr;
277 HLTError("can not create raw buffer");
279 return pTargetBuffer;
282 int AliHLTDataBuffer::SetSegments(AliHLTUInt8_t* pTgt, AliHLTComponentBlockData* arrayBlockData, int iSize)
284 // see header file for function documentation
286 if (pTgt && arrayBlockData && iSize>=0) {
288 if (fpBuffer->fPtr==(void*)pTgt) {
289 AliHLTDataBuffer::AliHLTDataSegment segment;
290 memset(&segment, 0, sizeof(AliHLTDataBuffer::AliHLTDataSegment));
291 for (int i=0; i<iSize; i++) {
292 if (arrayBlockData[i].fOffset+arrayBlockData[i].fSize<=fpBuffer->fSize) {
293 segment.fSegmentOffset=arrayBlockData[i].fOffset;
294 segment.fSegmentSize=arrayBlockData[i].fSize;
295 segment.fDataType=arrayBlockData[i].fDataType;
296 segment.fSpecification=arrayBlockData[i].fSpecification;
297 fSegments.push_back(segment);
298 HLTDebug("set segment %s with size %d at offset %d", AliHLTComponent::DataType2Text(segment.fDataType).data(), segment.fSegmentSize, segment.fSegmentOffset);
300 HLTError("block data specification %#d (%s) exceeds size of data buffer", i, AliHLTComponent::DataType2Text(arrayBlockData[i].fDataType).data());
301 HLTError("block offset=%d, block size=%d, buffer size=%d", arrayBlockData[i].fOffset, arrayBlockData[i].fSize, fpBuffer->fSize);
306 HLTError("this data buffer (%p) does not match the internal data buffer %p of raw buffer %p", pTgt, fpBuffer->fPtr, fpBuffer);
310 HLTFatal("internal data structur missmatch");
314 HLTError("invalid parameter: pTgtBuffer=%p arrayBlockData=%p", pTgt, arrayBlockData);
320 int AliHLTDataBuffer::IsEmpty()
322 // see header file for function documentation
323 int iResult=fpBuffer==NULL || GetNofSegments()==0;
327 int AliHLTDataBuffer::GetNofSegments()
329 // see header file for function documentation
330 int iResult=fSegments.size();
334 int AliHLTDataBuffer::GetNofConsumers()
336 // see header file for function documentation
337 int iResult=fConsumers.size() + GetNofActiveConsumers() + fReleasedConsumers.size();
341 int AliHLTDataBuffer::GetNofActiveConsumers()
343 // see header file for function documentation
344 int iResult=fActiveConsumers.size();
348 AliHLTDataBuffer::AliHLTRawBuffer* AliHLTDataBuffer::CreateRawBuffer(AliHLTUInt32_t size)
350 // see header file for function documentation
351 AliHLTRawBuffer* pRawBuffer=NULL;
352 int reqSize=size+fgkSafetyPatternSize;
353 vector<AliHLTRawBuffer*>::iterator buffer=fgFreeBuffers.begin();
354 while (buffer!=fgFreeBuffers.end() && pRawBuffer==NULL) {
355 if ((*buffer)->fTotalSize>=reqSize && ((*buffer)->fTotalSize-reqSize)<fgMargin) {
356 // assign this element
358 pRawBuffer->fSize=size;
359 fgFreeBuffers.erase(buffer);
360 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);
361 fgActiveBuffers.push_back(pRawBuffer);
366 if (pRawBuffer==NULL) {
367 // no buffer found, create a new one
368 pRawBuffer=new AliHLTRawBuffer;
370 memset(pRawBuffer, 0, sizeof(AliHLTRawBuffer));
371 pRawBuffer->fPtr=malloc(reqSize);
372 if (pRawBuffer->fPtr) {
373 pRawBuffer->fSize=size;
374 pRawBuffer->fTotalSize=reqSize;
375 fgActiveBuffers.push_back(pRawBuffer);
376 fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "new raw buffer %p of size %d created (container %p)", pRawBuffer->fPtr, pRawBuffer->fTotalSize, pRawBuffer);
377 if (fgkSafetyPatternSize>0) {
378 memcpy(((char*)pRawBuffer->fPtr)+pRawBuffer->fSize, fgkSafetyPattern, fgkSafetyPatternSize);
383 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
386 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
392 int AliHLTDataBuffer::ReleaseRawBuffer(AliHLTRawBuffer* pBuffer)
394 // see header file for function documentation
397 vector<AliHLTRawBuffer*>::iterator buffer=fgActiveBuffers.begin();
398 while (buffer!=fgActiveBuffers.end() && (*buffer)!=pBuffer) {
401 if (buffer!=fgActiveBuffers.end()) {
402 if (fgkSafetyPatternSize>0) {
403 if (memcmp(((char*)(*buffer)->fPtr)+(*buffer)->fSize, fgkSafetyPattern, fgkSafetyPatternSize)!=0) {
404 fgLogging.Logging(kHLTLogFatal, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "component has written beyond end of data buffer %p size %d", (*buffer)->fPtr, (*buffer)->fSize);
408 fgFreeBuffers.push_back(*buffer);
409 fgActiveBuffers.erase(buffer);
411 fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "can not find raw buffer container %p in the list of active containers", pBuffer);
415 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "invalid parameter");
422 int AliHLTDataBuffer::DeleteRawBuffers()
424 // see header file for function documentation
426 vector<AliHLTRawBuffer*>::iterator buffer=fgFreeBuffers.begin();
427 while (buffer!=fgFreeBuffers.end()) {
428 free((*buffer)->fPtr);
430 fgFreeBuffers.erase(buffer);
431 buffer=fgFreeBuffers.begin();
433 buffer=fgActiveBuffers.begin();
434 while (buffer!=fgActiveBuffers.end()) {
435 fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "request to delete active raw buffer container (raw buffer %p, size %d)", (*buffer)->fPtr, (*buffer)->fTotalSize);
436 free((*buffer)->fPtr);
438 fgActiveBuffers.erase(buffer);
439 buffer=fgActiveBuffers.begin();
444 AliHLTConsumerDescriptor* AliHLTDataBuffer::FindConsumer(const AliHLTComponent* pConsumer, vector<AliHLTConsumerDescriptor*> &list) const
446 // see header file for function documentation
447 AliHLTConsumerDescriptor* pDesc=NULL;
448 vector<AliHLTConsumerDescriptor*>::iterator desc=list.begin();
449 while (desc!=list.end() && pDesc==NULL) {
450 if ((pConsumer==NULL || (*desc)->GetComponent()==pConsumer)) {
458 int AliHLTDataBuffer::ResetDataBuffer()
460 // see header file for function documentation
462 AliHLTRawBuffer* pBuffer=fpBuffer;
465 // cleanup consumer states
466 vector<AliHLTConsumerDescriptor*>::iterator desc=fReleasedConsumers.begin();
467 while (desc!=fReleasedConsumers.end()) {
468 AliHLTConsumerDescriptor* pDesc=*desc;
469 fReleasedConsumers.erase(desc);
470 desc=fReleasedConsumers.begin();
471 fConsumers.push_back(pDesc);
473 desc=fActiveConsumers.begin();
474 while (desc!=fActiveConsumers.end()) {
475 AliHLTConsumerDescriptor* pDesc=*desc;
476 HLTWarning("consumer %p was not released", pDesc);
477 fActiveConsumers.erase(desc);
478 desc=fActiveConsumers.begin();
479 fConsumers.push_back(pDesc);
483 vector<AliHLTDataBuffer::AliHLTDataSegment>::iterator segment=fSegments.begin();
484 while (segment!=fSegments.end()) {
485 fSegments.erase(segment);
486 segment=fSegments.begin();
489 // cleanup raw buffer
491 ReleaseRawBuffer(pBuffer);
496 int AliHLTDataBuffer::Reset()
498 // see header file for function documentation
499 return ResetDataBuffer();
502 // this is the version which works on lists of components instead of consumer descriptors
503 // int AliHLTDataBuffer::ChangeConsumerState(AliHLTComponent* pConsumer, vector<AliHLTComponent*> &srcList, vector<AliHLTComponent*> &tgtList)
507 // vector<AliHLTComponent*>::iterator desc=srcList.begin();
508 // while (desc!=srcList.end()) {
509 // if ((*desc)==pConsumer) {
510 // srcList.erase(desc);
511 // tgtList.push_back(pConsumer);
516 // if (desc==srcList.end()) {
517 // HLTError("can not find consumer component %p in list", pConsumer);
521 // HLTError("invalid parameter");
527 int AliHLTDataBuffer::ChangeConsumerState(AliHLTConsumerDescriptor* pDesc, vector<AliHLTConsumerDescriptor*> &srcList, vector<AliHLTConsumerDescriptor*> &tgtList)
529 // see header file for function documentation
532 vector<AliHLTConsumerDescriptor*>::iterator desc=srcList.begin();
533 while (desc!=srcList.end()) {
534 if ((*desc)==pDesc) {
536 tgtList.push_back(pDesc);
543 HLTError("can not find consumer descriptor %p in list", pDesc);
546 HLTError("invalid parameter");
552 int AliHLTDataBuffer::CleanupConsumerList()
554 // see header file for function documentation
557 vector<AliHLTConsumerDescriptor*>::iterator desc=fConsumers.begin();
558 while (desc!=fConsumers.end()) {
560 fConsumers.erase(desc);
561 desc=fConsumers.begin();
566 int AliHLTDataBuffer::FindConsumer(AliHLTComponent* pConsumer, int bAllLists)
568 // see header file for function documentation
569 vector<AliHLTConsumerDescriptor*>::iterator desc=fConsumers.begin();
570 while (desc!=fConsumers.end()) {
571 if ((*desc)->GetComponent()==pConsumer)
575 if (bAllLists==0) return 0;
577 desc=fActiveConsumers.begin();
578 while (desc!=fActiveConsumers.end()) {
579 if ((*desc)->GetComponent()==pConsumer)
583 desc=fReleasedConsumers.begin();
584 while (desc!=fReleasedConsumers.end()) {
585 if ((*desc)->GetComponent()==pConsumer)