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"
33 //#include "AliHLTSystem.h"
35 /** ROOT macro for the implementation of ROOT specific class methods */
36 ClassImp(AliHLTDataBuffer)
38 AliHLTDataBuffer::AliHLTDataBuffer()
47 // see header file for class documentation
49 // refer to README to build package
51 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
54 fActiveConsumers.empty();
55 fReleasedConsumers.empty();
59 AliHLTDataBuffer::AliHLTDataBuffer(const AliHLTDataBuffer&)
70 // see header file for function documentation
71 HLTFatal("copy constructor untested");
74 AliHLTDataBuffer& AliHLTDataBuffer::operator=(const AliHLTDataBuffer&)
76 // see header file for function documentation
77 HLTFatal("assignment operator untested");
81 int AliHLTDataBuffer::fgNofInstances=0;
82 vector<AliHLTDataBuffer::AliHLTRawBuffer*> AliHLTDataBuffer::fgFreeBuffers;
83 vector<AliHLTDataBuffer::AliHLTRawBuffer*> AliHLTDataBuffer::fgActiveBuffers;
84 AliHLTUInt32_t AliHLTDataBuffer::fgMargin=1024;
85 AliHLTLogging AliHLTDataBuffer::fgLogging;
86 const Int_t AliHLTDataBuffer::fgkSafetyPatternSize=16;
87 const char AliHLTDataBuffer::fgkSafetyPattern[]={0x28, 0x63, 0x29, 0x4d, 0x52, 0x49, 0x43, 0x48, 0x54, 0x45, 0x52, 0x20, 0x32, 0x30, 0x30, 0x37};
89 AliHLTDataBuffer::~AliHLTDataBuffer()
91 // see header file for function documentation
92 if (--fgNofInstances<=0) {
95 CleanupConsumerList();
98 int AliHLTDataBuffer::SetConsumer(AliHLTComponent* pConsumer)
100 // see header file for function documentation
103 if (FindConsumer(pConsumer)) {
104 HLTWarning("consumer %s (%p) already set to data buffer %p", pConsumer->GetComponentID(), pConsumer, this);
106 AliHLTConsumerDescriptor* pDesc=new AliHLTConsumerDescriptor(pConsumer);
108 fConsumers.push_back(pDesc);
109 HLTDebug("set consumer %s (%p) to data buffer %p", pConsumer->GetComponentID(), pConsumer, this);
111 HLTError("memory allocation failed");
115 HLTError("invalid parameter: consumer component (nil)");
121 int AliHLTDataBuffer::FindMatchingDataBlocks(const AliHLTComponent* pConsumer, vector<AliHLTComponentDataType>* tgtList)
123 // see header file for function documentation
126 vector<AliHLTDataBuffer::AliHLTDataSegment> segments;
127 if ((iResult=FindMatchingDataSegments(pConsumer, segments))>=0) {
129 vector<AliHLTDataBuffer::AliHLTDataSegment>::iterator segment=segments.begin();
130 while (segment!=segments.end()) {
131 tgtList->push_back((*segment).fDataType);
135 iResult=segments.size();
143 int AliHLTDataBuffer::FindMatchingDataSegments(const AliHLTComponent* pConsumer, vector<AliHLTDataBuffer::AliHLTDataSegment>& tgtList)
145 // see header file for function documentation
148 vector<AliHLTComponentDataType> dtlist;
149 ((AliHLTComponent*)pConsumer)->GetInputDataTypes(dtlist);
150 vector<AliHLTDataBuffer::AliHLTDataSegment>::iterator segment=fSegments.begin();
151 while (segment!=fSegments.end()) {
152 vector<AliHLTComponentDataType>::iterator type=dtlist.begin();
153 while (type!=dtlist.end()) {
154 if ((*segment).fDataType==(*type) ||
155 (*type)==kAliHLTAnyDataType) {
156 tgtList.push_back(*segment);
170 int AliHLTDataBuffer::Subscribe(const AliHLTComponent* pConsumer, AliHLTComponentBlockData* arrayBlockDesc, int iArraySize)
172 // see header file for function documentation
174 if (pConsumer && arrayBlockDesc) {
176 AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fConsumers);
178 vector<AliHLTDataBuffer::AliHLTDataSegment> tgtList;
179 /* TODO: think about a good policy for this check
180 * is it enough that at least one segment is available, or have all to be available?
181 * or is it possible to have optional segments?
183 if ((iResult=FindMatchingDataSegments(pConsumer, tgtList))>0) {
185 vector<AliHLTDataBuffer::AliHLTDataSegment>::iterator segment=tgtList.begin();
186 while (segment!=tgtList.end() && i<iArraySize) {
187 // fill the block data descriptor
188 arrayBlockDesc[i].fStructSize=sizeof(AliHLTComponentBlockData);
189 // the shared memory key is not used in AliRoot
190 arrayBlockDesc[i].fShmKey.fStructSize=sizeof(AliHLTComponentShmData);
191 arrayBlockDesc[i].fShmKey.fShmType=gkAliHLTComponentInvalidShmType;
192 arrayBlockDesc[i].fShmKey.fShmID=gkAliHLTComponentInvalidShmID;
193 arrayBlockDesc[i].fOffset=(*segment).fSegmentOffset;
194 arrayBlockDesc[i].fPtr=fpBuffer->fPtr;
195 arrayBlockDesc[i].fSize=(*segment).fSegmentSize;
196 arrayBlockDesc[i].fDataType=(*segment).fDataType;
197 arrayBlockDesc[i].fSpecification=(*segment).fSpecification;
198 pDesc->SetActiveDataSegment(arrayBlockDesc[i].fOffset, arrayBlockDesc[i].fSize);
199 HLTDebug("component %p (%s) subscribed to segment #%d offset %d", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), i, arrayBlockDesc[i].fOffset);
203 // move this consumer to the active list
204 if (ChangeConsumerState(pDesc, fConsumers, fActiveConsumers)>=0) {
205 HLTDebug("component %p (%s) subscribed to data buffer %p", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), this);
207 // TODO: cleanup the consumer descriptor correctly
208 memset(arrayBlockDesc, 0, iArraySize*sizeof(AliHLTComponentBlockData));
209 HLTError("can not activate consumer %p for data buffer %p", pConsumer, this);
213 HLTError("unresolved data segment(s) for component %p (%s)", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID());
217 HLTError("component %p is not a data consumer of data buffer %s", pConsumer, this);
221 HLTError("data buffer %p is empty", this);
225 HLTError("invalid parameter");
231 int AliHLTDataBuffer::Release(AliHLTComponentBlockData* pBlockDesc, const AliHLTComponent* pConsumer)
233 // see header file for function documentation
235 if (pBlockDesc && pConsumer) {
236 AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fActiveConsumers);
238 if ((iResult=pDesc->CheckActiveDataSegment(pBlockDesc->fOffset, pBlockDesc->fSize))!=1) {
239 HLTWarning("data segment missmatch, component %p has not subscribed to a segment with offset %#x and size %d", pConsumer, pBlockDesc->fOffset, pBlockDesc->fSize);
240 // TODO: appropriate error handling, but so far optional
243 pDesc->ReleaseActiveDataSegment(pBlockDesc->fOffset, pBlockDesc->fSize);
244 pBlockDesc->fOffset=0;
245 pBlockDesc->fPtr=NULL;
248 if (pDesc->GetNofActiveSegments()==0) {
249 if ((iResult=ChangeConsumerState(pDesc, fActiveConsumers, fReleasedConsumers))>=0) {
250 if (GetNofActiveConsumers()==0) {
251 // this is the last consumer, reset the consumer list and release the raw buffer
255 HLTError("can not deactivate consumer %p for data buffer %p", pConsumer, this);
260 HLTWarning("component %p has currently not subscribed to the data buffer %p", pConsumer, this);
264 HLTError("inavalid parameter: pBlockDesc=%p pConsumer=%p", pBlockDesc, pConsumer);
270 AliHLTUInt8_t* AliHLTDataBuffer::GetTargetBuffer(int iMinSize)
272 // see header file for function documentation
273 AliHLTUInt8_t* pTargetBuffer=NULL;
274 fpBuffer=CreateRawBuffer(iMinSize);
276 pTargetBuffer=(AliHLTUInt8_t*)fpBuffer->fPtr;
278 HLTError("can not create raw buffer");
280 return pTargetBuffer;
283 int AliHLTDataBuffer::SetSegments(AliHLTUInt8_t* pTgt, AliHLTComponentBlockData* arrayBlockData, int iSize)
285 // see header file for function documentation
287 if (pTgt && arrayBlockData && iSize>=0) {
289 if (fpBuffer->fPtr==(void*)pTgt) {
290 AliHLTDataBuffer::AliHLTDataSegment segment;
291 memset(&segment, 0, sizeof(AliHLTDataBuffer::AliHLTDataSegment));
292 for (int i=0; i<iSize; i++) {
293 if (arrayBlockData[i].fOffset+arrayBlockData[i].fSize<=fpBuffer->fSize) {
294 segment.fSegmentOffset=arrayBlockData[i].fOffset;
295 segment.fSegmentSize=arrayBlockData[i].fSize;
296 segment.fDataType=arrayBlockData[i].fDataType;
297 segment.fSpecification=arrayBlockData[i].fSpecification;
298 fSegments.push_back(segment);
299 HLTDebug("set segment %s with size %d at offset %d", AliHLTComponent::DataType2Text(segment.fDataType).data(), segment.fSegmentSize, segment.fSegmentOffset);
301 HLTError("block data specification %#d (%s) exceeds size of data buffer", i, AliHLTComponent::DataType2Text(arrayBlockData[i].fDataType).data());
302 HLTError("block offset=%d, block size=%d, buffer size=%d", arrayBlockData[i].fOffset, arrayBlockData[i].fSize, fpBuffer->fSize);
307 HLTError("this data buffer (%p) does not match the internal data buffer %p of raw buffer %p", pTgt, fpBuffer->fPtr, fpBuffer);
311 HLTFatal("internal data structur missmatch");
315 HLTError("invalid parameter: pTgtBuffer=%p arrayBlockData=%p", pTgt, arrayBlockData);
321 int AliHLTDataBuffer::IsEmpty()
323 // see header file for function documentation
324 int iResult=fpBuffer==NULL || GetNofSegments()==0;
328 int AliHLTDataBuffer::GetNofSegments()
330 // see header file for function documentation
331 int iResult=fSegments.size();
335 int AliHLTDataBuffer::GetNofConsumers()
337 // see header file for function documentation
338 int iResult=fConsumers.size() + GetNofActiveConsumers() + fReleasedConsumers.size();
342 int AliHLTDataBuffer::GetNofActiveConsumers()
344 // see header file for function documentation
345 int iResult=fActiveConsumers.size();
349 AliHLTDataBuffer::AliHLTRawBuffer* AliHLTDataBuffer::CreateRawBuffer(AliHLTUInt32_t size)
351 // see header file for function documentation
352 AliHLTRawBuffer* pRawBuffer=NULL;
353 unsigned int reqSize=size+fgkSafetyPatternSize;
354 vector<AliHLTRawBuffer*>::iterator buffer=fgFreeBuffers.begin();
355 while (buffer!=fgFreeBuffers.end() && pRawBuffer==NULL) {
356 if ((*buffer)->fTotalSize>=reqSize && ((*buffer)->fTotalSize-reqSize)<fgMargin) {
357 // assign this element
359 pRawBuffer->fSize=size;
360 fgFreeBuffers.erase(buffer);
361 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);
362 fgActiveBuffers.push_back(pRawBuffer);
367 if (pRawBuffer==NULL) {
368 // no buffer found, create a new one
369 pRawBuffer=new AliHLTRawBuffer;
371 memset(pRawBuffer, 0, sizeof(AliHLTRawBuffer));
372 pRawBuffer->fPtr=malloc(reqSize);
373 if (pRawBuffer->fPtr) {
374 pRawBuffer->fSize=size;
375 pRawBuffer->fTotalSize=reqSize;
376 fgActiveBuffers.push_back(pRawBuffer);
377 fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "new raw buffer %p of size %d created (container %p)", pRawBuffer->fPtr, pRawBuffer->fTotalSize, pRawBuffer);
378 if (fgkSafetyPatternSize>0) {
379 memcpy(((char*)pRawBuffer->fPtr)+pRawBuffer->fSize, fgkSafetyPattern, fgkSafetyPatternSize);
384 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
387 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
393 int AliHLTDataBuffer::ReleaseRawBuffer(AliHLTRawBuffer* pBuffer)
395 // see header file for function documentation
398 vector<AliHLTRawBuffer*>::iterator buffer=fgActiveBuffers.begin();
399 while (buffer!=fgActiveBuffers.end() && (*buffer)!=pBuffer) {
402 if (buffer!=fgActiveBuffers.end()) {
403 if (fgkSafetyPatternSize>0) {
404 if (memcmp(((char*)(*buffer)->fPtr)+(*buffer)->fSize, fgkSafetyPattern, fgkSafetyPatternSize)!=0) {
405 fgLogging.Logging(kHLTLogFatal, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "component has written beyond end of data buffer %p size %d", (*buffer)->fPtr, (*buffer)->fSize);
409 fgFreeBuffers.push_back(*buffer);
410 fgActiveBuffers.erase(buffer);
412 fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "can not find raw buffer container %p in the list of active containers", pBuffer);
416 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "invalid parameter");
423 int AliHLTDataBuffer::DeleteRawBuffers()
425 // see header file for function documentation
427 vector<AliHLTRawBuffer*>::iterator buffer=fgFreeBuffers.begin();
428 while (buffer!=fgFreeBuffers.end()) {
429 free((*buffer)->fPtr);
431 fgFreeBuffers.erase(buffer);
432 buffer=fgFreeBuffers.begin();
434 buffer=fgActiveBuffers.begin();
435 while (buffer!=fgActiveBuffers.end()) {
436 fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "request to delete active raw buffer container (raw buffer %p, size %d)", (*buffer)->fPtr, (*buffer)->fTotalSize);
437 free((*buffer)->fPtr);
439 fgActiveBuffers.erase(buffer);
440 buffer=fgActiveBuffers.begin();
445 AliHLTConsumerDescriptor* AliHLTDataBuffer::FindConsumer(const AliHLTComponent* pConsumer, vector<AliHLTConsumerDescriptor*> &list) const
447 // see header file for function documentation
448 AliHLTConsumerDescriptor* pDesc=NULL;
449 vector<AliHLTConsumerDescriptor*>::iterator desc=list.begin();
450 while (desc!=list.end() && pDesc==NULL) {
451 if ((pConsumer==NULL || (*desc)->GetComponent()==pConsumer)) {
459 int AliHLTDataBuffer::ResetDataBuffer()
461 // see header file for function documentation
463 AliHLTRawBuffer* pBuffer=fpBuffer;
466 // cleanup consumer states
467 vector<AliHLTConsumerDescriptor*>::iterator desc=fReleasedConsumers.begin();
468 while (desc!=fReleasedConsumers.end()) {
469 AliHLTConsumerDescriptor* pDesc=*desc;
470 fReleasedConsumers.erase(desc);
471 desc=fReleasedConsumers.begin();
472 fConsumers.push_back(pDesc);
474 desc=fActiveConsumers.begin();
475 while (desc!=fActiveConsumers.end()) {
476 AliHLTConsumerDescriptor* pDesc=*desc;
477 HLTWarning("consumer %p was not released", pDesc);
478 fActiveConsumers.erase(desc);
479 desc=fActiveConsumers.begin();
480 fConsumers.push_back(pDesc);
484 vector<AliHLTDataBuffer::AliHLTDataSegment>::iterator segment=fSegments.begin();
485 while (segment!=fSegments.end()) {
486 fSegments.erase(segment);
487 segment=fSegments.begin();
490 // cleanup raw buffer
492 ReleaseRawBuffer(pBuffer);
497 int AliHLTDataBuffer::Reset()
499 // see header file for function documentation
500 return ResetDataBuffer();
503 // this is the version which works on lists of components instead of consumer descriptors
504 // int AliHLTDataBuffer::ChangeConsumerState(AliHLTComponent* pConsumer, vector<AliHLTComponent*> &srcList, vector<AliHLTComponent*> &tgtList)
508 // vector<AliHLTComponent*>::iterator desc=srcList.begin();
509 // while (desc!=srcList.end()) {
510 // if ((*desc)==pConsumer) {
511 // srcList.erase(desc);
512 // tgtList.push_back(pConsumer);
517 // if (desc==srcList.end()) {
518 // HLTError("can not find consumer component %p in list", pConsumer);
522 // HLTError("invalid parameter");
528 int AliHLTDataBuffer::ChangeConsumerState(AliHLTConsumerDescriptor* pDesc, vector<AliHLTConsumerDescriptor*> &srcList, vector<AliHLTConsumerDescriptor*> &tgtList)
530 // see header file for function documentation
533 vector<AliHLTConsumerDescriptor*>::iterator desc=srcList.begin();
534 while (desc!=srcList.end()) {
535 if ((*desc)==pDesc) {
537 tgtList.push_back(pDesc);
544 HLTError("can not find consumer descriptor %p in list", pDesc);
547 HLTError("invalid parameter");
553 int AliHLTDataBuffer::CleanupConsumerList()
555 // see header file for function documentation
558 vector<AliHLTConsumerDescriptor*>::iterator desc=fConsumers.begin();
559 while (desc!=fConsumers.end()) {
561 fConsumers.erase(desc);
562 desc=fConsumers.begin();
567 int AliHLTDataBuffer::FindConsumer(AliHLTComponent* pConsumer, int bAllLists)
569 // see header file for function documentation
570 vector<AliHLTConsumerDescriptor*>::iterator desc=fConsumers.begin();
571 while (desc!=fConsumers.end()) {
572 if ((*desc)->GetComponent()==pConsumer)
576 if (bAllLists==0) return 0;
578 desc=fActiveConsumers.begin();
579 while (desc!=fActiveConsumers.end()) {
580 if ((*desc)->GetComponent()==pConsumer)
584 desc=fReleasedConsumers.begin();
585 while (desc!=fReleasedConsumers.end()) {
586 if ((*desc)->GetComponent()==pConsumer)