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;
86 AliHLTDataBuffer::~AliHLTDataBuffer()
88 // see header file for function documentation
89 if (--fgNofInstances<=0) {
92 CleanupConsumerList();
95 int AliHLTDataBuffer::SetConsumer(AliHLTComponent* pConsumer)
97 // see header file for function documentation
100 if (FindConsumer(pConsumer)) {
101 HLTWarning("consumer %s (%p) already set to data buffer %p", pConsumer->GetComponentID(), pConsumer, this);
103 AliHLTConsumerDescriptor* pDesc=new AliHLTConsumerDescriptor(pConsumer);
105 fConsumers.push_back(pDesc);
106 HLTDebug("set consumer %s (%p) to data buffer %p", pConsumer->GetComponentID(), pConsumer, this);
108 HLTError("memory allocation failed");
112 HLTError("invalid parameter: consumer component (nil)");
118 int AliHLTDataBuffer::FindMatchingDataBlocks(const AliHLTComponent* pConsumer, vector<AliHLTComponentDataType>* tgtList)
120 // see header file for function documentation
123 vector<AliHLTDataBuffer::AliHLTDataSegment> segments;
124 if ((iResult=FindMatchingDataSegments(pConsumer, segments))>=0) {
126 vector<AliHLTDataBuffer::AliHLTDataSegment>::iterator segment=segments.begin();
127 while (segment!=segments.end()) {
128 tgtList->push_back((*segment).fDataType);
132 iResult=segments.size();
140 int AliHLTDataBuffer::FindMatchingDataSegments(const AliHLTComponent* pConsumer, vector<AliHLTDataBuffer::AliHLTDataSegment>& tgtList)
142 // see header file for function documentation
145 vector<AliHLTComponentDataType> dtlist;
146 ((AliHLTComponent*)pConsumer)->GetInputDataTypes(dtlist);
147 vector<AliHLTDataBuffer::AliHLTDataSegment>::iterator segment=fSegments.begin();
148 while (segment!=fSegments.end()) {
149 vector<AliHLTComponentDataType>::iterator type=dtlist.begin();
150 while (type!=dtlist.end()) {
151 if ((*segment).fDataType==(*type) ||
152 (*type)==kAliHLTAnyDataType) {
153 tgtList.push_back(*segment);
167 int AliHLTDataBuffer::Subscribe(const AliHLTComponent* pConsumer, AliHLTComponentBlockData* arrayBlockDesc, int iArraySize)
169 // see header file for function documentation
171 if (pConsumer && arrayBlockDesc) {
173 AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fConsumers);
175 vector<AliHLTDataBuffer::AliHLTDataSegment> tgtList;
176 /* TODO: think about a good policy for this check
177 * is it enough that at least one segment is available, or have all to be available?
178 * or is it possible to have optional segments?
180 if ((iResult=FindMatchingDataSegments(pConsumer, tgtList))>0) {
182 vector<AliHLTDataBuffer::AliHLTDataSegment>::iterator segment=tgtList.begin();
183 while (segment!=tgtList.end() && i<iArraySize) {
184 // fill the block data descriptor
185 arrayBlockDesc[i].fStructSize=sizeof(AliHLTComponentBlockData);
186 // the shared memory key is not used in AliRoot
187 arrayBlockDesc[i].fShmKey.fStructSize=sizeof(AliHLTComponentShmData);
188 arrayBlockDesc[i].fShmKey.fShmType=gkAliHLTComponentInvalidShmType;
189 arrayBlockDesc[i].fShmKey.fShmID=gkAliHLTComponentInvalidShmID;
190 arrayBlockDesc[i].fOffset=(*segment).fSegmentOffset;
191 arrayBlockDesc[i].fPtr=fpBuffer->fPtr;
192 arrayBlockDesc[i].fSize=(*segment).fSegmentSize;
193 arrayBlockDesc[i].fDataType=(*segment).fDataType;
194 arrayBlockDesc[i].fSpecification=(*segment).fSpecification;
195 pDesc->SetActiveDataSegment(arrayBlockDesc[i].fOffset, arrayBlockDesc[i].fSize);
196 HLTDebug("component %p (%s) subscribed to segment #%d offset %d", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), i, arrayBlockDesc[i].fOffset);
200 // move this consumer to the active list
201 if (ChangeConsumerState(pDesc, fConsumers, fActiveConsumers)>=0) {
202 HLTDebug("component %p (%s) subscribed to data buffer %p", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), this);
204 // TODO: cleanup the consumer descriptor correctly
205 memset(arrayBlockDesc, 0, iArraySize*sizeof(AliHLTComponentBlockData));
206 HLTError("can not activate consumer %p for data buffer %p", pConsumer, this);
210 HLTError("unresolved data segment(s) for component %p (%s)", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID());
214 HLTError("component %p is not a data consumer of data buffer %s", pConsumer, this);
218 HLTError("data buffer %p is empty", this);
222 HLTError("invalid parameter");
228 int AliHLTDataBuffer::Release(AliHLTComponentBlockData* pBlockDesc, const AliHLTComponent* pConsumer)
230 // see header file for function documentation
232 if (pBlockDesc && pConsumer) {
233 AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fActiveConsumers);
235 if ((iResult=pDesc->CheckActiveDataSegment(pBlockDesc->fOffset, pBlockDesc->fSize))!=1) {
236 HLTWarning("data segment missmatch, component %p has not subscribed to a segment with offset %#x and size %d", pConsumer, pBlockDesc->fOffset, pBlockDesc->fSize);
237 // TODO: appropriate error handling, but so far optional
240 pDesc->ReleaseActiveDataSegment(pBlockDesc->fOffset, pBlockDesc->fSize);
241 pBlockDesc->fOffset=0;
242 pBlockDesc->fPtr=NULL;
245 if (pDesc->GetNofActiveSegments()==0) {
246 if ((iResult=ChangeConsumerState(pDesc, fActiveConsumers, fReleasedConsumers))>=0) {
247 if (GetNofActiveConsumers()==0) {
248 // this is the last consumer, reset the consumer list and release the raw buffer
252 HLTError("can not deactivate consumer %p for data buffer %p", pConsumer, this);
257 HLTWarning("component %p has currently not subscribed to the data buffer %p", pConsumer, this);
261 HLTError("inavalid parameter: pBlockDesc=%p pConsumer=%p", pBlockDesc, pConsumer);
267 AliHLTUInt8_t* AliHLTDataBuffer::GetTargetBuffer(int iMinSize)
269 // see header file for function documentation
270 AliHLTUInt8_t* pTargetBuffer=NULL;
271 fpBuffer=CreateRawBuffer(iMinSize);
273 pTargetBuffer=(AliHLTUInt8_t*)fpBuffer->fPtr;
275 HLTError("can not create raw buffer");
277 return pTargetBuffer;
280 int AliHLTDataBuffer::SetSegments(AliHLTUInt8_t* pTgt, AliHLTComponentBlockData* arrayBlockData, int iSize)
282 // see header file for function documentation
284 if (pTgt && arrayBlockData && iSize>=0) {
286 if (fpBuffer->fPtr==(void*)pTgt) {
287 AliHLTDataBuffer::AliHLTDataSegment segment;
288 memset(&segment, 0, sizeof(AliHLTDataBuffer::AliHLTDataSegment));
289 for (int i=0; i<iSize; i++) {
290 if (arrayBlockData[i].fOffset+arrayBlockData[i].fSize<=fpBuffer->fSize) {
291 segment.fSegmentOffset=arrayBlockData[i].fOffset;
292 segment.fSegmentSize=arrayBlockData[i].fSize;
293 segment.fDataType=arrayBlockData[i].fDataType;
294 segment.fSpecification=arrayBlockData[i].fSpecification;
295 fSegments.push_back(segment);
296 HLTDebug("set segment %s with size %d at offset %d", AliHLTComponent::DataType2Text(segment.fDataType).data(), segment.fSegmentSize, segment.fSegmentOffset);
298 HLTError("block data specification %#d (%s) exceeds size of data buffer", i, AliHLTComponent::DataType2Text(arrayBlockData[i].fDataType).data());
299 HLTError("block offset=%d, block size=%d, buffer size=%d", arrayBlockData[i].fOffset, arrayBlockData[i].fSize, fpBuffer->fSize);
303 HLTError("this data buffer (%p) does not match the internal data buffer %p of raw buffer %p", pTgt, fpBuffer->fPtr, fpBuffer);
306 HLTFatal("internal data structur missmatch");
310 HLTError("invalid parameter: pTgtBuffer=%p arrayBlockData=%p", pTgt, arrayBlockData);
316 int AliHLTDataBuffer::IsEmpty()
318 // see header file for function documentation
319 int iResult=fpBuffer==NULL || GetNofSegments()==0;
323 int AliHLTDataBuffer::GetNofSegments()
325 // see header file for function documentation
326 int iResult=fSegments.size();
330 int AliHLTDataBuffer::GetNofConsumers()
332 // see header file for function documentation
333 int iResult=fConsumers.size() + GetNofActiveConsumers() + fReleasedConsumers.size();
337 int AliHLTDataBuffer::GetNofActiveConsumers()
339 // see header file for function documentation
340 int iResult=fActiveConsumers.size();
344 AliHLTDataBuffer::AliHLTRawBuffer* AliHLTDataBuffer::CreateRawBuffer(AliHLTUInt32_t size)
346 // see header file for function documentation
347 AliHLTRawBuffer* pRawBuffer=NULL;
348 vector<AliHLTRawBuffer*>::iterator buffer=fgFreeBuffers.begin();
349 while (buffer!=fgFreeBuffers.end() && pRawBuffer==NULL) {
350 if ((*buffer)->fTotalSize>=size && ((*buffer)->fTotalSize-size)<fgMargin) {
351 // assign this element
353 pRawBuffer->fSize=size;
354 fgFreeBuffers.erase(buffer);
355 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);
356 fgActiveBuffers.push_back(pRawBuffer);
361 if (pRawBuffer==NULL) {
362 // no buffer found, create a new one
363 pRawBuffer=new AliHLTRawBuffer;
365 memset(pRawBuffer, 0, sizeof(AliHLTRawBuffer));
366 pRawBuffer->fPtr=malloc(size);
367 if (pRawBuffer->fPtr) {
368 pRawBuffer->fSize=size;
369 pRawBuffer->fTotalSize=size;
370 fgActiveBuffers.push_back(pRawBuffer);
371 fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "new raw buffer %p of size %d created (container %p)", pRawBuffer->fPtr, pRawBuffer->fTotalSize, pRawBuffer);
375 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
378 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
384 int AliHLTDataBuffer::ReleaseRawBuffer(AliHLTRawBuffer* pBuffer)
386 // see header file for function documentation
389 vector<AliHLTRawBuffer*>::iterator buffer=fgActiveBuffers.begin();
390 while (buffer!=fgActiveBuffers.end() && (*buffer)!=pBuffer) {
393 if (buffer!=fgActiveBuffers.end()) {
395 fgFreeBuffers.push_back(*buffer);
396 fgActiveBuffers.erase(buffer);
398 fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "can not find raw buffer container %p in the list of active containers", pBuffer);
402 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "invalid parameter");
409 int AliHLTDataBuffer::DeleteRawBuffers()
411 // see header file for function documentation
413 vector<AliHLTRawBuffer*>::iterator buffer=fgFreeBuffers.begin();
414 while (buffer!=fgFreeBuffers.end()) {
415 free((*buffer)->fPtr);
417 fgFreeBuffers.erase(buffer);
418 buffer=fgFreeBuffers.begin();
420 buffer=fgActiveBuffers.begin();
421 while (buffer!=fgActiveBuffers.end()) {
422 fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "request to delete active raw buffer container (raw buffer %p, size %d)", (*buffer)->fPtr, (*buffer)->fTotalSize);
423 free((*buffer)->fPtr);
425 fgActiveBuffers.erase(buffer);
426 buffer=fgActiveBuffers.begin();
431 AliHLTConsumerDescriptor* AliHLTDataBuffer::FindConsumer(const AliHLTComponent* pConsumer, vector<AliHLTConsumerDescriptor*> &list) const
433 // see header file for function documentation
434 AliHLTConsumerDescriptor* pDesc=NULL;
435 vector<AliHLTConsumerDescriptor*>::iterator desc=list.begin();
436 while (desc!=list.end() && pDesc==NULL) {
437 if ((pConsumer==NULL || (*desc)->GetComponent()==pConsumer)) {
445 int AliHLTDataBuffer::ResetDataBuffer()
447 // see header file for function documentation
449 AliHLTRawBuffer* pBuffer=fpBuffer;
452 // cleanup consumer states
453 vector<AliHLTConsumerDescriptor*>::iterator desc=fReleasedConsumers.begin();
454 while (desc!=fReleasedConsumers.end()) {
455 AliHLTConsumerDescriptor* pDesc=*desc;
456 fReleasedConsumers.erase(desc);
457 desc=fReleasedConsumers.begin();
458 fConsumers.push_back(pDesc);
460 desc=fActiveConsumers.begin();
461 while (desc!=fActiveConsumers.end()) {
462 AliHLTConsumerDescriptor* pDesc=*desc;
463 HLTWarning("consumer %p was not released", pDesc);
464 fActiveConsumers.erase(desc);
465 desc=fActiveConsumers.begin();
466 fConsumers.push_back(pDesc);
470 vector<AliHLTDataBuffer::AliHLTDataSegment>::iterator segment=fSegments.begin();
471 while (segment!=fSegments.end()) {
472 fSegments.erase(segment);
473 segment=fSegments.begin();
476 // cleanup raw buffer
478 ReleaseRawBuffer(pBuffer);
483 int AliHLTDataBuffer::Reset()
485 // see header file for function documentation
486 return ResetDataBuffer();
489 // this is the version which works on lists of components instead of consumer descriptors
490 // int AliHLTDataBuffer::ChangeConsumerState(AliHLTComponent* pConsumer, vector<AliHLTComponent*> &srcList, vector<AliHLTComponent*> &tgtList)
494 // vector<AliHLTComponent*>::iterator desc=srcList.begin();
495 // while (desc!=srcList.end()) {
496 // if ((*desc)==pConsumer) {
497 // srcList.erase(desc);
498 // tgtList.push_back(pConsumer);
503 // if (desc==srcList.end()) {
504 // HLTError("can not find consumer component %p in list", pConsumer);
508 // HLTError("invalid parameter");
514 int AliHLTDataBuffer::ChangeConsumerState(AliHLTConsumerDescriptor* pDesc, vector<AliHLTConsumerDescriptor*> &srcList, vector<AliHLTConsumerDescriptor*> &tgtList)
516 // see header file for function documentation
519 vector<AliHLTConsumerDescriptor*>::iterator desc=srcList.begin();
520 while (desc!=srcList.end()) {
521 if ((*desc)==pDesc) {
523 tgtList.push_back(pDesc);
530 HLTError("can not find consumer descriptor %p in list", pDesc);
533 HLTError("invalid parameter");
539 int AliHLTDataBuffer::CleanupConsumerList()
541 // see header file for function documentation
544 vector<AliHLTConsumerDescriptor*>::iterator desc=fConsumers.begin();
545 while (desc!=fConsumers.end()) {
547 fConsumers.erase(desc);
548 desc=fConsumers.begin();
553 int AliHLTDataBuffer::FindConsumer(AliHLTComponent* pConsumer, int bAllLists)
555 // see header file for function documentation
556 vector<AliHLTConsumerDescriptor*>::iterator desc=fConsumers.begin();
557 while (desc!=fConsumers.end()) {
558 if ((*desc)->GetComponent()==pConsumer)
562 if (bAllLists==0) return 0;
564 desc=fActiveConsumers.begin();
565 while (desc!=fActiveConsumers.end()) {
566 if ((*desc)->GetComponent()==pConsumer)
570 desc=fReleasedConsumers.begin();
571 while (desc!=fReleasedConsumers.end()) {
572 if ((*desc)->GetComponent()==pConsumer)