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 "AliHLTComponent.h"
31 #include "AliHLTSystem.h"
33 AliHLTConsumerDescriptor::AliHLTConsumerDescriptor()
41 AliHLTConsumerDescriptor::AliHLTConsumerDescriptor(AliHLTComponent* pConsumer)
43 fpConsumer(pConsumer),
49 AliHLTConsumerDescriptor::AliHLTConsumerDescriptor(const AliHLTConsumerDescriptor& desc)
53 fpConsumer(desc.fpConsumer),
56 // we can simply transfer the pointer to th new object since there are no
57 // release actions in the destructor
60 AliHLTConsumerDescriptor& AliHLTConsumerDescriptor::operator=(const AliHLTConsumerDescriptor& desc)
62 // we can simply transfer the pointer to th new object since there are no
63 // release actions in the destructor
64 fpConsumer=desc.fpConsumer;
68 AliHLTConsumerDescriptor::~AliHLTConsumerDescriptor()
70 if (fSegments.size()>0) {
71 //HLTWarning("unreleased data segments found");
75 int AliHLTConsumerDescriptor::SetActiveDataSegment(AliHLTUInt32_t offset, AliHLTUInt32_t size)
78 AliHLTDataSegment segment(offset, size);
79 fSegments.push_back(segment);
80 //HLTDebug("set active segment (%d:%d) for consumer %p", offset, size, this);
84 int AliHLTConsumerDescriptor::CheckActiveDataSegment(AliHLTUInt32_t offset, AliHLTUInt32_t size)
87 if (fSegments.size()>0) {
88 vector<AliHLTDataSegment>::iterator segment=fSegments.begin();
89 while (segment!=fSegments.end()) {
90 if (iResult=((*segment).fSegmentOffset==offset && (*segment).fSegmentSize==size)) {
96 //HLTWarning("no data segment active for consumer %p", this);
102 int AliHLTConsumerDescriptor::ReleaseActiveDataSegment(AliHLTUInt32_t offset, AliHLTUInt32_t size)
105 if (fSegments.size()>0) {
106 vector<AliHLTDataSegment>::iterator segment=fSegments.begin();
107 while (segment!=fSegments.end()) {
108 if (iResult=((*segment).fSegmentOffset==offset && (*segment).fSegmentSize==size)) {
109 fSegments.erase(segment);
115 //HLTWarning("no data segment (%d:%d) active for consumer %p", offset, size, this);
119 //HLTWarning("no data segment active for consumer %p", this);
125 /** ROOT macro for the implementation of ROOT specific class methods */
126 ClassImp(AliHLTDataBuffer)
128 AliHLTDataBuffer::AliHLTDataBuffer()
133 fReleasedConsumers(),
139 fActiveConsumers.empty();
140 fReleasedConsumers.empty();
144 AliHLTDataBuffer::AliHLTDataBuffer(const AliHLTDataBuffer&)
151 fReleasedConsumers(),
155 HLTFatal("copy constructor untested");
158 AliHLTDataBuffer& AliHLTDataBuffer::operator=(const AliHLTDataBuffer&)
160 HLTFatal("assignment operator untested");
164 int AliHLTDataBuffer::fNofInstances=0;
165 vector<AliHLTRawBuffer*> AliHLTDataBuffer::fFreeBuffers;
166 vector<AliHLTRawBuffer*> AliHLTDataBuffer::fActiveBuffers;
167 AliHLTUInt32_t AliHLTDataBuffer::fMargin=1024;
168 AliHLTLogging AliHLTDataBuffer::fgLogging;
170 AliHLTDataBuffer::~AliHLTDataBuffer()
172 if (--fNofInstances<=0) {
175 CleanupConsumerList();
178 int AliHLTDataBuffer::SetConsumer(AliHLTComponent* pConsumer)
182 AliHLTConsumerDescriptor* pDesc=new AliHLTConsumerDescriptor(pConsumer);
184 fConsumers.push_back(pDesc);
186 HLTError("memory allocation failed");
190 HLTError("invalid parameter");
196 int AliHLTDataBuffer::FindMatchingDataBlocks(const AliHLTComponent* pConsumer, vector<AliHLTComponentDataType>* tgtList)
200 vector<AliHLTDataSegment> segments;
201 if ((iResult=FindMatchingDataSegments(pConsumer, segments))>=0) {
203 vector<AliHLTDataSegment>::iterator segment=segments.begin();
204 while (segment!=segments.end()) {
205 tgtList->push_back((*segment).fDataType);
209 iResult=segments.size();
217 int AliHLTDataBuffer::FindMatchingDataSegments(const AliHLTComponent* pConsumer, vector<AliHLTDataSegment>& tgtList)
221 vector<AliHLTComponentDataType> dtlist;
222 ((AliHLTComponent*)pConsumer)->GetInputDataTypes(dtlist);
223 vector<AliHLTDataSegment>::iterator segment=fSegments.begin();
224 while (segment!=fSegments.end()) {
225 vector<AliHLTComponentDataType>::iterator type=dtlist.begin();
226 while (type!=dtlist.end()) {
227 if ((*segment).fDataType==(*type)) {
228 tgtList.push_back(*segment);
242 int AliHLTDataBuffer::Subscribe(const AliHLTComponent* pConsumer, AliHLTComponentBlockData* arrayBlockDesc, int iArraySize)
245 if (pConsumer && arrayBlockDesc) {
247 AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fConsumers);
249 vector<AliHLTDataSegment> tgtList;
250 /* TODO: think about a good policy for this check
251 * is it enough that at least one segment is available, or have all to be available?
252 * or is it possible to have optional segments?
254 if ((iResult=FindMatchingDataSegments(pConsumer, tgtList))>0) {
256 vector<AliHLTDataSegment>::iterator segment=tgtList.begin();
257 while (segment!=tgtList.end() && i<iArraySize) {
258 // fill the block data descriptor
259 arrayBlockDesc[i].fStructSize=sizeof(AliHLTComponentBlockData);
260 // the shared memory key is not used in AliRoot
261 arrayBlockDesc[i].fShmKey.fStructSize=sizeof(AliHLTComponentShmData);
262 arrayBlockDesc[i].fShmKey.fShmType=gkAliHLTComponentInvalidShmType;
263 arrayBlockDesc[i].fShmKey.fShmID=gkAliHLTComponentInvalidShmID;
264 arrayBlockDesc[i].fOffset=(*segment).fSegmentOffset;
265 arrayBlockDesc[i].fPtr=fpBuffer->fPtr;
266 arrayBlockDesc[i].fSize=(*segment).fSegmentSize;
267 arrayBlockDesc[i].fDataType=(*segment).fDataType;
268 arrayBlockDesc[i].fSpecification=(*segment).fSpecification;
269 pDesc->SetActiveDataSegment(arrayBlockDesc[i].fOffset, arrayBlockDesc[i].fSize);
270 HLTDebug("component %p (%s) subscribed to segment #%d offset %d", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), i, arrayBlockDesc[i].fOffset);
274 // move this consumer to the active list
275 if (ChangeConsumerState(pDesc, fConsumers, fActiveConsumers)>=0) {
276 HLTDebug("component %p (%s) subscribed to data buffer %p", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), this);
278 // TODO: cleanup the consumer descriptor correctly
279 memset(arrayBlockDesc, 0, iArraySize*sizeof(AliHLTComponentBlockData));
280 HLTError("can not activate consumer %p for data buffer %p", pConsumer, this);
284 HLTError("unresolved data segment(s) for component %p (%s)", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID());
288 HLTError("component %p is not a data consumer of data buffer %s", pConsumer, this);
292 HLTError("data buffer %p is empty", this);
296 HLTError("invalid parameter");
302 int AliHLTDataBuffer::Release(AliHLTComponentBlockData* pBlockDesc, const AliHLTComponent* pConsumer)
305 if (pBlockDesc && pConsumer) {
306 AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fActiveConsumers);
308 if ((iResult=pDesc->CheckActiveDataSegment(pBlockDesc->fOffset, pBlockDesc->fSize))!=1) {
309 HLTWarning("data segment missmatch, component %p has not subscribed to a segment with offset %#x and size %d", pConsumer, pBlockDesc->fOffset, pBlockDesc->fSize);
310 // TODO: appropriate error handling, but so far optional
313 pDesc->ReleaseActiveDataSegment(pBlockDesc->fOffset, pBlockDesc->fSize);
314 pBlockDesc->fOffset=0;
315 pBlockDesc->fPtr=NULL;
318 if (pDesc->GetNofActiveSegments()==0) {
319 if ((iResult=ChangeConsumerState(pDesc, fActiveConsumers, fReleasedConsumers))>=0) {
320 if (GetNofActiveConsumers()==0) {
321 // this is the last consumer, reset the consumer list and release the raw buffer
325 HLTError("can not deactivate consumer %p for data buffer %p", pConsumer, this);
330 HLTWarning("component %p has currently not subscribed to the data buffer %p", pConsumer, this);
334 HLTError("inavalid parameter: pBlockDesc=%p pConsumer=%p", pBlockDesc, pConsumer);
340 AliHLTUInt8_t* AliHLTDataBuffer::GetTargetBuffer(int iMinSize)
342 AliHLTUInt8_t* pTargetBuffer=NULL;
343 fpBuffer=CreateRawBuffer(iMinSize);
344 pTargetBuffer=(AliHLTUInt8_t*)fpBuffer;
345 return pTargetBuffer;
348 int AliHLTDataBuffer::SetSegments(AliHLTUInt8_t* pTgt, AliHLTComponentBlockData* arrayBlockData, int iSize)
351 if (pTgt && arrayBlockData && iSize>=0) {
353 if (fpBuffer->fPtr==(void*)pTgt) {
354 AliHLTDataSegment segment;
355 memset(&segment, 0, sizeof(AliHLTDataSegment));
356 for (int i=0; i<iSize; i++) {
357 if (arrayBlockData[i].fOffset+arrayBlockData[i].fSize<fpBuffer->fSize) {
358 segment.fSegmentOffset=arrayBlockData[i].fOffset;
359 segment.fSegmentSize=arrayBlockData[i].fSize;
360 segment.fDataType=arrayBlockData[i].fDataType;
361 segment.fSpecification=arrayBlockData[i].fSpecification;
362 fSegments.push_back(segment);
364 HLTError("block data specification #%d (%s@%s) exceeds size of data buffer", i, arrayBlockData[i].fDataType.fOrigin, arrayBlockData[i].fDataType.fID);
368 HLTError("this data buffer (%p) does not match the internal data buffer %p of raw buffer %p", pTgt, fpBuffer->fPtr, fpBuffer);
371 HLTFatal("internal data structur missmatch");
375 HLTError("invalid parameter: pTgtBuffer=%p arrayBlockData=%p", pTgt, arrayBlockData);
381 int AliHLTDataBuffer::IsEmpty()
383 int iResult=fpBuffer==NULL || GetNofSegments()==0;
387 int AliHLTDataBuffer::GetNofSegments()
389 int iResult=fSegments.size();
393 int AliHLTDataBuffer::GetNofConsumers()
395 int iResult=fConsumers.size() + GetNofActiveConsumers() + fReleasedConsumers.size();
399 int AliHLTDataBuffer::GetNofActiveConsumers()
401 int iResult=fActiveConsumers.size();
405 AliHLTRawBuffer* AliHLTDataBuffer::CreateRawBuffer(AliHLTUInt32_t size)
407 AliHLTRawBuffer* pRawBuffer=NULL;
408 vector<AliHLTRawBuffer*>::iterator buffer=fFreeBuffers.begin();
409 while (buffer!=fFreeBuffers.end() && pRawBuffer==NULL) {
410 if ((*buffer)->fTotalSize>=size && ((*buffer)->fTotalSize-size)<fMargin) {
411 // assign this element
413 pRawBuffer->fSize=size;
414 fFreeBuffers.erase(buffer);
415 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);
416 fActiveBuffers.push_back(pRawBuffer);
421 if (pRawBuffer==NULL) {
422 // no buffer found, create a new one
423 pRawBuffer=new AliHLTRawBuffer;
425 memset(pRawBuffer, 0, sizeof(AliHLTRawBuffer));
426 pRawBuffer->fPtr=malloc(size);
427 if (pRawBuffer->fPtr) {
428 pRawBuffer->fSize=size;
429 pRawBuffer->fTotalSize=size;
430 fActiveBuffers.push_back(pRawBuffer);
431 fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "new raw buffer %p of size %d created (container %p)", pRawBuffer->fPtr, pRawBuffer->fTotalSize, pRawBuffer);
435 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
438 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
444 int AliHLTDataBuffer::ReleaseRawBuffer(AliHLTRawBuffer* pBuffer)
448 vector<AliHLTRawBuffer*>::iterator buffer=fActiveBuffers.begin();
449 while (buffer!=fActiveBuffers.end() && (*buffer)!=pBuffer) {
452 if (buffer!=fActiveBuffers.end()) {
454 fFreeBuffers.push_back(*buffer);
455 fActiveBuffers.erase(buffer);
457 fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "can not find raw buffer container %p in the list of active containers", pBuffer);
461 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "invalid parameter");
468 int AliHLTDataBuffer::DeleteRawBuffers()
471 vector<AliHLTRawBuffer*>::iterator buffer=fFreeBuffers.begin();
472 while (buffer!=fFreeBuffers.end()) {
473 free((*buffer)->fPtr);
475 fFreeBuffers.erase(buffer);
476 buffer=fFreeBuffers.begin();
478 buffer=fActiveBuffers.begin();
479 while (buffer!=fFreeBuffers.end()) {
480 fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "request to delete active raw buffer container (raw buffer %p, size %d)", (*buffer)->fPtr, (*buffer)->fTotalSize);
481 free((*buffer)->fPtr);
483 fActiveBuffers.erase(buffer);
484 buffer=fActiveBuffers.begin();
489 AliHLTConsumerDescriptor* AliHLTDataBuffer::FindConsumer(const AliHLTComponent* pConsumer, vector<AliHLTConsumerDescriptor*> &list)
491 AliHLTConsumerDescriptor* pDesc=NULL;
492 vector<AliHLTConsumerDescriptor*>::iterator desc=list.begin();
493 while (desc!=list.end() && pDesc==NULL) {
494 if ((pConsumer==NULL || (*desc)->GetComponent()==pConsumer)) {
502 int AliHLTDataBuffer::ResetDataBuffer()
505 AliHLTRawBuffer* pBuffer=fpBuffer;
507 vector<AliHLTConsumerDescriptor*>::iterator desc=fReleasedConsumers.begin();
508 while (desc!=fReleasedConsumers.end()) {
509 AliHLTConsumerDescriptor* pDesc=*desc;
510 fReleasedConsumers.erase(desc);
511 desc=fReleasedConsumers.begin();
512 fConsumers.push_back(pDesc);
514 desc=fActiveConsumers.begin();
515 while (desc!=fActiveConsumers.end()) {
516 AliHLTConsumerDescriptor* pDesc=*desc;
517 HLTWarning("consumer %p was not released", pDesc);
518 fActiveConsumers.erase(desc);
519 desc=fActiveConsumers.begin();
520 fConsumers.push_back(pDesc);
522 ReleaseRawBuffer(pBuffer);
526 // this is the version which works on lists of components instead of consumer descriptors
527 // int AliHLTDataBuffer::ChangeConsumerState(AliHLTComponent* pConsumer, vector<AliHLTComponent*> &srcList, vector<AliHLTComponent*> &tgtList)
531 // vector<AliHLTComponent*>::iterator desc=srcList.begin();
532 // while (desc!=srcList.end()) {
533 // if ((*desc)==pConsumer) {
534 // srcList.erase(desc);
535 // tgtList.push_back(pConsumer);
540 // if (desc==srcList.end()) {
541 // HLTError("can not find consumer component %p in list", pConsumer);
545 // HLTError("invalid parameter");
551 int AliHLTDataBuffer::ChangeConsumerState(AliHLTConsumerDescriptor* pDesc, vector<AliHLTConsumerDescriptor*> &srcList, vector<AliHLTConsumerDescriptor*> &tgtList)
555 vector<AliHLTConsumerDescriptor*>::iterator desc=srcList.begin();
556 while (desc!=srcList.end()) {
557 if ((*desc)==pDesc) {
559 tgtList.push_back(pDesc);
564 if (desc==srcList.end()) {
565 HLTError("can not find consumer descriptor %p in list", pDesc);
569 HLTError("invalid parameter");
575 int AliHLTDataBuffer::CleanupConsumerList() {
578 vector<AliHLTConsumerDescriptor*>::iterator desc=fConsumers.begin();
579 while (desc!=fConsumers.end()) {
581 fConsumers.erase(desc);
582 desc=fConsumers.begin();