]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/BASE/AliHLTDataBuffer.cxx
Adjust matching rules for queued output buffers which have not been used
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTDataBuffer.cxx
CommitLineData
3f2a1b1c 1// $Id$
2
c5123824 3//**************************************************************************
4//* This file is property of and copyright by the ALICE HLT Project *
5//* ALICE Experiment at CERN, All rights reserved. *
6//* *
7//* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8//* for The ALICE HLT Project. *
9//* *
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//**************************************************************************
3f2a1b1c 18
b22e91eb 19/** @file AliHLTDataBuffer.cxx
20 @author Matthias Richter
21 @date
22 @brief Handling of Data Buffers for HLT components.
23*/
3f2a1b1c 24
0c0c9d99 25#if __GNUC__>= 3
3f2a1b1c 26using namespace std;
27#endif
28
29#include "AliHLTDataBuffer.h"
6235cd38 30#include "AliHLTConsumerDescriptor.h"
b22e91eb 31#include "AliHLTComponent.h"
b46ca65e 32#include "AliHLTTask.h"
66043029 33#include <cerrno>
d6cbe999 34#include <cassert>
66043029 35//#include <string>
36//#include "AliHLTSystem.h"
3f2a1b1c 37
2be3f004 38typedef vector<AliHLTDataBuffer::AliHLTDataSegment> AliHLTDataSegmentList;
39typedef vector<AliHLTDataBuffer::AliHLTRawBuffer*> AliHLTRawBufferPList;
40
b22e91eb 41/** ROOT macro for the implementation of ROOT specific class methods */
3f2a1b1c 42ClassImp(AliHLTDataBuffer)
43
3f2a1b1c 44AliHLTDataBuffer::AliHLTDataBuffer()
85869391 45 :
46 fSegments(),
47 fConsumers(),
48 fActiveConsumers(),
49 fReleasedConsumers(),
50 fpBuffer(NULL),
b46ca65e 51 fFlags(0),
52 fForwardedSegmentSources(),
53 fForwardedSegments()
3f2a1b1c 54{
70ed7d01 55 // see header file for class documentation
56 // or
57 // refer to README to build package
58 // or
59 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
85869391 60 fSegments.empty();
61 fConsumers.empty();
62 fActiveConsumers.empty();
63 fReleasedConsumers.empty();
70ed7d01 64 fgNofInstances++;
3f2a1b1c 65}
66
70ed7d01 67int AliHLTDataBuffer::fgNofInstances=0;
2be3f004 68AliHLTRawBufferPList AliHLTDataBuffer::fgFreeBuffers;
69AliHLTRawBufferPList AliHLTDataBuffer::fgActiveBuffers;
70ed7d01 70AliHLTUInt32_t AliHLTDataBuffer::fgMargin=1024;
b22e91eb 71AliHLTLogging AliHLTDataBuffer::fgLogging;
8451168b 72const Int_t AliHLTDataBuffer::fgkSafetyPatternSize=16;
73const char AliHLTDataBuffer::fgkSafetyPattern[]={0x28, 0x63, 0x29, 0x4d, 0x52, 0x49, 0x43, 0x48, 0x54, 0x45, 0x52, 0x20, 0x32, 0x30, 0x30, 0x37};
0a7afbf0 74AliHLTUInt32_t AliHLTDataBuffer::fgEventCount=0;
b22e91eb 75
3f2a1b1c 76AliHLTDataBuffer::~AliHLTDataBuffer()
77{
70ed7d01 78 // see header file for function documentation
79 if (--fgNofInstances<=0) {
3f2a1b1c 80 DeleteRawBuffers();
81 }
82 CleanupConsumerList();
83}
84
0c0c9d99 85int AliHLTDataBuffer::SetConsumer(AliHLTComponent* pConsumer)
3f2a1b1c 86{
70ed7d01 87 // see header file for function documentation
3f2a1b1c 88 int iResult=0;
89 if (pConsumer) {
9ce4bf4a 90 if (FindConsumer(pConsumer)) {
91 HLTWarning("consumer %s (%p) already set to data buffer %p", pConsumer->GetComponentID(), pConsumer, this);
92 }
0c0c9d99 93 AliHLTConsumerDescriptor* pDesc=new AliHLTConsumerDescriptor(pConsumer);
3f2a1b1c 94 if (pDesc) {
95 fConsumers.push_back(pDesc);
9ce4bf4a 96 HLTDebug("set consumer %s (%p) to data buffer %p", pConsumer->GetComponentID(), pConsumer, this);
3f2a1b1c 97 } else {
98 HLTError("memory allocation failed");
99 iResult=-ENOMEM;
100 }
101 } else {
9ce4bf4a 102 HLTError("invalid parameter: consumer component (nil)");
3f2a1b1c 103 iResult=-EINVAL;
104 }
105 return iResult;
106}
107
2be3f004 108int AliHLTDataBuffer::FindMatchingDataBlocks(const AliHLTComponent* pConsumer, AliHLTComponentDataTypeList* tgtList)
0c0c9d99 109{
70ed7d01 110 // see header file for function documentation
0c0c9d99 111 int iResult=0;
112 if (pConsumer) {
2be3f004 113 AliHLTDataSegmentList segments;
0c0c9d99 114 if ((iResult=FindMatchingDataSegments(pConsumer, segments))>=0) {
115 if (tgtList) {
2be3f004 116 AliHLTDataSegmentList::iterator segment=segments.begin();
0c0c9d99 117 while (segment!=segments.end()) {
118 tgtList->push_back((*segment).fDataType);
119 segment++;
120 }
121 }
122 iResult=segments.size();
123 }
124 } else {
125 iResult=-EINVAL;
126 }
127 return iResult;
128}
129
6235cd38 130int AliHLTDataBuffer::FindMatchingDataSegments(const AliHLTComponent* pConsumer, vector<AliHLTDataBuffer::AliHLTDataSegment>& tgtList)
0c0c9d99 131{
70ed7d01 132 // see header file for function documentation
0c0c9d99 133 int iResult=0;
9b7fe12d 134
135 // Matthias 26.09.2007 relax the restriction to matching data blocks
136 // all blocks are passed to the consumer, which is the policy also in
137 // PubSub
138 tgtList.assign(fSegments.begin(), fSegments.end());
b46ca65e 139
140 // add all forwarded blocks
141 tgtList.insert(tgtList.begin(), fForwardedSegments.begin(), fForwardedSegments.end());
9b7fe12d 142 iResult=tgtList.size();
143 return iResult;
144
0c0c9d99 145 if (pConsumer) {
2be3f004 146 AliHLTComponentDataTypeList dtlist;
0c0c9d99 147 ((AliHLTComponent*)pConsumer)->GetInputDataTypes(dtlist);
2be3f004 148 AliHLTDataSegmentList::iterator segment=fSegments.begin();
0c0c9d99 149 while (segment!=fSegments.end()) {
2be3f004 150 AliHLTComponentDataTypeList::iterator type=dtlist.begin();
0c0c9d99 151 while (type!=dtlist.end()) {
d8f5c9fe 152 if ((*segment).fDataType==(*type)) {
0c0c9d99 153 tgtList.push_back(*segment);
154 iResult++;
155 break;
156 }
157 type++;
158 }
159 segment++;
160 }
161 } else {
162 iResult=-EINVAL;
163 }
164 return iResult;
165}
166
457ec821 167int AliHLTDataBuffer::Subscribe(const AliHLTComponent* pConsumer, AliHLTComponentBlockDataList& blockDescList)
3f2a1b1c 168{
70ed7d01 169 // see header file for function documentation
3f2a1b1c 170 int iResult=0;
457ec821 171 if (pConsumer) {
b46ca65e 172 if (1/*fpBuffer*/) {
0c0c9d99 173 AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fConsumers);
3f2a1b1c 174 if (pDesc) {
2be3f004 175 AliHLTDataSegmentList tgtList;
9b7fe12d 176 // Matthias 26.07.2007 AliHLTSystem should behave the same way as PubSub
177 // so it does not matter if there are matching data types or not, unless
178 // we implement such a check in PubSub
179 if ((iResult=FindMatchingDataSegments(pConsumer, tgtList))>=0) {
2be3f004 180 AliHLTDataSegmentList::iterator segment=tgtList.begin();
457ec821 181 while (segment!=tgtList.end()) {
3f2a1b1c 182 // fill the block data descriptor
457ec821 183 AliHLTComponentBlockData bd;
184 AliHLTComponent::FillBlockData(bd);
3294f81a 185 // This models the behavior of PubSub.
186 // For incoming data blocks, fOffset must be ignored by the
187 // processing component. It is set for bookkeeping in the framework.
188 // fPtr always points to the beginning of the data.
457ec821 189 bd.fOffset=0;
b46ca65e 190 AliHLTUInt8_t* pTgt=*segment;
457ec821 191 bd.fPtr=reinterpret_cast<void*>(pTgt);
192 bd.fSize=(*segment).fSegmentSize;
193 bd.fDataType=(*segment).fDataType;
194 bd.fSpecification=(*segment).fSpecification;
195 blockDescList.push_back(bd);
b46ca65e 196 pDesc->SetActiveDataSegment(*segment);
457ec821 197 HLTDebug("component %p (%s) subscribed to segment offset %d size %d data type %s %#x",
198 pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), bd.fOffset,
199 bd.fSize, (AliHLTComponent::DataType2Text(bd.fDataType)).c_str(),
200 bd.fSpecification);
0c0c9d99 201 segment++;
202 }
203 // move this consumer to the active list
457ec821 204 if (tgtList.size()==0) {
b46ca65e 205 ChangeConsumerState(pDesc, fConsumers, fReleasedConsumers);
206 HLTDebug("no input data for component %p (%s) available", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID());
207 } else if (ChangeConsumerState(pDesc, fConsumers, fActiveConsumers)>=0) {
0c0c9d99 208 HLTDebug("component %p (%s) subscribed to data buffer %p", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID(), this);
3f2a1b1c 209 } else {
0c0c9d99 210 // TODO: cleanup the consumer descriptor correctly
457ec821 211 segment=tgtList.begin();
212 while (segment!=tgtList.end()) {
213 blockDescList.pop_back();
214 segment++;
215 }
3f2a1b1c 216 HLTError("can not activate consumer %p for data buffer %p", pConsumer, this);
217 iResult=-EACCES;
218 }
219 } else {
0c0c9d99 220 HLTError("unresolved data segment(s) for component %p (%s)", pConsumer, ((AliHLTComponent*)pConsumer)->GetComponentID());
3f2a1b1c 221 iResult=-EBADF;
222 }
223 } else {
d4a18597 224 if (!FindConsumer(pConsumer)) {
225 HLTError("component %p is not a data consumer of data buffer %p", pConsumer, this);
226 } else {
227 HLTError("component %p is a valid data consumer of data buffer %p, but did not release it's buffer subscription", pConsumer, this);
228 }
3f2a1b1c 229 iResult=-ENOENT;
230 }
231 } else {
9b7fe12d 232 // Matthias 26.07.2007 until now, data had to be present for successful subscription
233 // in order to be consistent with the PubSub framework, this restiction has been
234 // removed
235 //HLTError("data buffer %p is empty", this);
236 //iResult=-ENODATA;
3f2a1b1c 237 }
238 } else {
239 HLTError("invalid parameter");
240 iResult=-EINVAL;
241 }
242 return iResult;
243}
244
b46ca65e 245int AliHLTDataBuffer::Release(AliHLTComponentBlockData* pBlockDesc,
246 const AliHLTComponent* pConsumer,
247 const AliHLTTask* pOwnerTask)
3f2a1b1c 248{
70ed7d01 249 // see header file for function documentation
3f2a1b1c 250 int iResult=0;
251 if (pBlockDesc && pConsumer) {
0c0c9d99 252 AliHLTConsumerDescriptor* pDesc=FindConsumer(pConsumer, fActiveConsumers);
253 if (pDesc) {
b46ca65e 254 if ((iResult=pDesc->CheckActiveDataSegment(AliHLTDataSegment(pBlockDesc->fPtr, pBlockDesc->fOffset, pBlockDesc->fSize)))!=1) {
c5123824 255 HLTWarning("data segment mismatch, component %p has not subscribed to a segment with offset %#x and size %d", pConsumer, pBlockDesc->fOffset, pBlockDesc->fSize);
0c0c9d99 256 // TODO: appropriate error handling, but so far optional
257 iResult=0;
258 } else {
b46ca65e 259 pDesc->ReleaseActiveDataSegment(AliHLTDataSegment(pBlockDesc->fPtr, pBlockDesc->fOffset, pBlockDesc->fSize));
260 }
261 if (GetNofPendingConsumers()==0 && fForwardedSegments.size()>0) {
262 // last consumer, release forwarded segments
d4a18597 263 ReleaseForwardedBlock(pBlockDesc, pOwnerTask);
264 }
265 pBlockDesc->fOffset=0;
266 pBlockDesc->fPtr=NULL;
267 pBlockDesc->fSize=0;
268 if (pDesc->GetNofActiveSegments()==0) {
269 if ((iResult=ChangeConsumerState(pDesc, fActiveConsumers, fReleasedConsumers))>=0) {
270 if (GetNofActiveConsumers()==0 && GetNofPendingConsumers()==0) {
271 // this is the last consumer, reset the consumer list and release the raw buffer
272 ResetDataBuffer();
273 }
274 } else {
275 HLTError("can not deactivate consumer %p for data buffer %p", pConsumer, this);
276 iResult=-EACCES;
277 }
278 }
279 } else {
280 HLTWarning("component %p has currently not subscribed to the data buffer %p", pConsumer, this);
281 iResult=-ENOENT;
282 }
283 } else {
284 HLTError("inavalid parameter: pBlockDesc=%p pConsumer=%p", pBlockDesc, pConsumer);
285 iResult=-EINVAL;
286 }
287 return iResult;
288}
289
290int AliHLTDataBuffer::ReleaseForwardedBlock(AliHLTComponentBlockData* pBlockDesc,
291 const AliHLTTask* pOwnerTask)
292{
293 // see header file for function documentation
294 int iResult=0;
295 if (pBlockDesc && pOwnerTask) {
b46ca65e 296 assert(fForwardedSegments.size()==fForwardedSegmentSources.size());
297 AliHLTDataSegmentList::iterator segment=fForwardedSegments.begin();
298 AliHLTTaskPList::iterator src=fForwardedSegmentSources.begin();
299 //HLTDebug("%p checking forwarded segments", this);
300 for (; segment!=fForwardedSegments.end(); segment++, src++) {
301 //HLTDebug("segment ptr=%p offset=%d size=%d\n"
302 // "block ptr=%p offset=%d size=%d", (*segment).fPtr, (*segment).fSegmentOffset, (*segment).fSegmentSize, pBlockDesc->fPtr, pBlockDesc->fOffset, pBlockDesc->fSize);
303 if ((*segment)==AliHLTDataSegment(pBlockDesc->fPtr, pBlockDesc->fOffset, pBlockDesc->fSize)) {
304 //HLTDebug("release segment of task %p", *src);
305 assert((*src)!=NULL);
306 if ((*src)!=NULL) {
307 if ((*src)->Release(pBlockDesc, pOwnerTask)>=0) {
308 HLTDebug("task %s (%p) released forwarded segment %p size %d of task %s (%p)",
309 pOwnerTask->GetName(), pOwnerTask, (*segment).GetPtr(), (*segment).GetSize(),
310 (*src)->GetName(), *src);
311 } else {
312 HLTError("task %s (%p) failed releasing forwarded segment %p size %d of task %s (%p)",
313 pOwnerTask->GetName(), pOwnerTask, (*segment).GetPtr(), (*segment).GetSize(),
314 (*src)->GetName(), *src);
315 }
316 }
317 fForwardedSegments.erase(segment);
318 fForwardedSegmentSources.erase(src);
319 break;
320 }
321 }
3f2a1b1c 322 } else {
d4a18597 323 HLTError("inavalid parameter: pBlockDesc=%p pOwnerTask=%p", pBlockDesc, pOwnerTask);
3f2a1b1c 324 iResult=-EINVAL;
325 }
326 return iResult;
327}
328
b46ca65e 329int AliHLTDataBuffer::Forward(AliHLTTask* pSrcTask, AliHLTComponentBlockData* pBlockDesc)
330{
331 // see header file for function documentation
332 if (pSrcTask==NULL || pBlockDesc==NULL) return -EINVAL;
333 assert(fForwardedSegments.size()==fForwardedSegmentSources.size());
334 if (fForwardedSegments.size()!=fForwardedSegmentSources.size()) return -EFAULT;
335 fForwardedSegmentSources.push_back(pSrcTask);
457ec821 336 fForwardedSegments.push_back(AliHLTDataSegment(pBlockDesc->fPtr, pBlockDesc->fOffset, pBlockDesc->fSize, pBlockDesc->fDataType, pBlockDesc->fSpecification));
b46ca65e 337 return 0;
338}
339
3f2a1b1c 340AliHLTUInt8_t* AliHLTDataBuffer::GetTargetBuffer(int iMinSize)
341{
70ed7d01 342 // see header file for function documentation
3f2a1b1c 343 AliHLTUInt8_t* pTargetBuffer=NULL;
b6800be0 344 if (fpBuffer!=NULL) {
345 HLTWarning("data buffer not properly reset, possible memory leak\n");
346 }
3f2a1b1c 347 fpBuffer=CreateRawBuffer(iMinSize);
9ce4bf4a 348 if (fpBuffer) {
1e6e67ec 349 pTargetBuffer=*fpBuffer;
9ce4bf4a 350 } else {
351 HLTError("can not create raw buffer");
352 }
3f2a1b1c 353 return pTargetBuffer;
354}
355
8ede8717 356int AliHLTDataBuffer::SetSegments(AliHLTUInt8_t* pTgt, AliHLTComponentBlockData* arrayBlockData, int iSize)
3f2a1b1c 357{
70ed7d01 358 // see header file for function documentation
3f2a1b1c 359 int iResult=0;
360 if (pTgt && arrayBlockData && iSize>=0) {
0c0c9d99 361 if (fpBuffer) {
1e6e67ec 362 if (*fpBuffer==pTgt) {
6235cd38 363 AliHLTDataBuffer::AliHLTDataSegment segment;
0c0c9d99 364 for (int i=0; i<iSize; i++) {
3294f81a 365 // This function has to model the behavior of PubSub
366 // For output blocks only the fOffset value is used, this must be the offset
367 // relative to the output pointer. fPtr must be either NULL or the output
b46ca65e 368 // pointer. In either case it is 'ignored' and set to the beginning of the
369 // data buffer
6434d28a 370 if (arrayBlockData[i].fPtr==NULL ||
3294f81a 371 arrayBlockData[i].fPtr==*fpBuffer) {
b46ca65e 372 arrayBlockData[i].fPtr=*fpBuffer;
0ad4ebd1 373 if ((arrayBlockData[i].fOffset+arrayBlockData[i].fSize<=fpBuffer->GetUsedSize()) ||
374 ((arrayBlockData[i].fOffset==~(AliHLTUInt32_t)0) && arrayBlockData[i].fSize==0)) {
3294f81a 375 segment.fSegmentOffset=arrayBlockData[i].fOffset;
b46ca65e 376 segment.fPtr=(AliHLTUInt8_t*)arrayBlockData[i].fPtr;
1e6e67ec 377 segment.fSegmentSize=arrayBlockData[i].fSize;
378 segment.fDataType=arrayBlockData[i].fDataType;
379 segment.fSpecification=arrayBlockData[i].fSpecification;
380 fSegments.push_back(segment);
381 HLTDebug("set segment %s with size %d at offset %d", AliHLTComponent::DataType2Text(segment.fDataType).data(), segment.fSegmentSize, segment.fSegmentOffset);
382 } else {
383 HLTError("block data specification %#d (%s) exceeds size of data buffer", i, AliHLTComponent::DataType2Text(arrayBlockData[i].fDataType).data());
c0a2bfc2 384 HLTError("block offset=%d, block size=%d, buffer size=%d", arrayBlockData[i].fOffset, arrayBlockData[i].fSize, fpBuffer->GetUsedSize());
1e6e67ec 385 iResult=-E2BIG;
386 }
0c0c9d99 387 } else {
3294f81a 388 HLTError("invalid pointer (%p) in block data specification (buffer %p size %d)."
389 "please note: for output blocks only the fOffset value is valid and must "
c0a2bfc2 390 "be relative to the output buffer", arrayBlockData[i].fPtr, fpBuffer->GetPointer(), fpBuffer->GetUsedSize());
1e6e67ec 391 iResult=-ERANGE;
0c0c9d99 392 }
393 }
3f2a1b1c 394 } else {
c0a2bfc2 395 HLTError("this data buffer (%p) does not match the internal data buffer %p of raw buffer %p", pTgt, fpBuffer->GetPointer(), fpBuffer);
8451168b 396 iResult=-EINVAL;
3f2a1b1c 397 }
0c0c9d99 398 } else {
c5123824 399 HLTFatal("internal data structur mismatch");
0c0c9d99 400 iResult=-EFAULT;
3f2a1b1c 401 }
402 } else {
0c0c9d99 403 HLTError("invalid parameter: pTgtBuffer=%p arrayBlockData=%p", pTgt, arrayBlockData);
3f2a1b1c 404 iResult=-EINVAL;
405 }
406 return iResult;
407}
408
409int AliHLTDataBuffer::IsEmpty()
410{
70ed7d01 411 // see header file for function documentation
d4a18597 412 int iResult=(fpBuffer==NULL && fForwardedSegments.size()==0) || GetNofSegments()==0;
3f2a1b1c 413 return iResult;
414}
415
416int AliHLTDataBuffer::GetNofSegments()
417{
70ed7d01 418 // see header file for function documentation
d4a18597 419 int iResult=fSegments.size() + fForwardedSegments.size();
3f2a1b1c 420 return iResult;
421}
422
423int AliHLTDataBuffer::GetNofConsumers()
424{
70ed7d01 425 // see header file for function documentation
3f2a1b1c 426 int iResult=fConsumers.size() + GetNofActiveConsumers() + fReleasedConsumers.size();
427 return iResult;
428}
429
b426991e 430int AliHLTDataBuffer::GetNofPendingConsumers()
431{
432 // see header file for function documentation
433 int iResult=fConsumers.size();
434 return iResult;
435}
436
3f2a1b1c 437int AliHLTDataBuffer::GetNofActiveConsumers()
438{
70ed7d01 439 // see header file for function documentation
3f2a1b1c 440 int iResult=fActiveConsumers.size();
441 return iResult;
442}
443
6235cd38 444AliHLTDataBuffer::AliHLTRawBuffer* AliHLTDataBuffer::CreateRawBuffer(AliHLTUInt32_t size)
3f2a1b1c 445{
70ed7d01 446 // see header file for function documentation
3f2a1b1c 447 AliHLTRawBuffer* pRawBuffer=NULL;
7a5ccd96 448 unsigned int reqSize=size+fgkSafetyPatternSize;
2be3f004 449 AliHLTRawBufferPList::iterator buffer=fgFreeBuffers.begin();
70ed7d01 450 while (buffer!=fgFreeBuffers.end() && pRawBuffer==NULL) {
d6cbe999 451 if ((*buffer)->CheckSize(reqSize)) {
3f2a1b1c 452 // assign this element
453 pRawBuffer=*buffer;
d6cbe999 454 pRawBuffer->UseBuffer(size);
70ed7d01 455 fgFreeBuffers.erase(buffer);
c0a2bfc2 456 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->GetTotalSize(), pRawBuffer->GetPointer());
70ed7d01 457 fgActiveBuffers.push_back(pRawBuffer);
0c0c9d99 458 break;
3f2a1b1c 459 }
0c0c9d99 460 buffer++;
3f2a1b1c 461 }
462 if (pRawBuffer==NULL) {
0c0c9d99 463 // no buffer found, create a new one
d6cbe999 464 pRawBuffer=new AliHLTRawBuffer(reqSize);
3f2a1b1c 465 if (pRawBuffer) {
c0a2bfc2 466 if (pRawBuffer->GetPointer()) {
d6cbe999 467 pRawBuffer->UseBuffer(size);
70ed7d01 468 fgActiveBuffers.push_back(pRawBuffer);
c0a2bfc2 469 fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "new raw buffer %p of size %d created (container %p)", pRawBuffer->GetPointer(), pRawBuffer->GetTotalSize(), pRawBuffer);
3f2a1b1c 470 } else {
471 delete pRawBuffer;
472 pRawBuffer=NULL;
0c0c9d99 473 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
3f2a1b1c 474 }
475 } else {
0c0c9d99 476 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
3f2a1b1c 477 }
478 }
73ede1d3 479 if (pRawBuffer!=NULL && fgkSafetyPatternSize>0) {
c0a2bfc2 480 //fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "writing safety pattern to %p offset %d", (*buffer)->GetPointer(), (*buffer)->GetUsedSize());
49b01d04 481 pRawBuffer->WritePattern(fgkSafetyPattern, fgkSafetyPatternSize);
73ede1d3 482 }
3f2a1b1c 483 return pRawBuffer;
484}
485
486int AliHLTDataBuffer::ReleaseRawBuffer(AliHLTRawBuffer* pBuffer)
487{
70ed7d01 488 // see header file for function documentation
3f2a1b1c 489 int iResult=0;
490 if (pBuffer) {
2be3f004 491 AliHLTRawBufferPList::iterator buffer=fgActiveBuffers.begin();
70ed7d01 492 while (buffer!=fgActiveBuffers.end() && (*buffer)!=pBuffer) {
3f2a1b1c 493 buffer++;
494 }
70ed7d01 495 if (buffer!=fgActiveBuffers.end()) {
8451168b 496 if (fgkSafetyPatternSize>0) {
c0a2bfc2 497 //fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "comparing safety pattern at %p offset %d", (*buffer)->GetPointer(), reinterpret_cast<AliHLTUInt32_t>(*buffer));
d6cbe999 498 if ((*buffer)->CheckPattern(fgkSafetyPattern, fgkSafetyPatternSize)) {
c0a2bfc2 499 fgLogging.Logging(kHLTLogFatal, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "component has written beyond end of data buffer %p size %d", (*buffer)->GetPointer(), (*buffer)->GetUsedSize());
8451168b 500 }
501 }
d6cbe999 502 (*buffer)->Reset();
70ed7d01 503 fgFreeBuffers.push_back(*buffer);
504 fgActiveBuffers.erase(buffer);
3f2a1b1c 505 } else {
0c0c9d99 506 fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "can not find raw buffer container %p in the list of active containers", pBuffer);
3f2a1b1c 507 iResult=-ENOENT;
508 }
509 } else {
0c0c9d99 510 fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "invalid parameter");
3f2a1b1c 511 iResult=-EINVAL;
512 }
513 return iResult;
514}
515
516
517int AliHLTDataBuffer::DeleteRawBuffers()
518{
70ed7d01 519 // see header file for function documentation
3f2a1b1c 520 int iResult=0;
950a0e40 521#ifdef ALIHLTSYSTEM_PROFILING
522 int iTotalSize=0;
523 int iCount=fgFreeBuffers.size()+fgActiveBuffers.size();
524#endif //ALIHLTSYSTEM_PROFILING
dba03d72 525 AliHLTRawBufferPList::iterator buffer;;
526 while ((buffer=fgFreeBuffers.begin())!=fgFreeBuffers.end()) {
950a0e40 527#ifdef ALIHLTSYSTEM_PROFILING
528 iTotalSize+=(*buffer)->GetTotalSize();
529#endif //ALIHLTSYSTEM_PROFILING
3f2a1b1c 530 delete *buffer;
70ed7d01 531 fgFreeBuffers.erase(buffer);
3f2a1b1c 532 }
dba03d72 533 while ((buffer=fgActiveBuffers.begin())!=fgActiveBuffers.end()) {
950a0e40 534#ifdef ALIHLTSYSTEM_PROFILING
535 iTotalSize+=(*buffer)->GetTotalSize();
536#endif //ALIHLTSYSTEM_PROFILING
c0a2bfc2 537 fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "request to delete active raw buffer container (raw buffer %p, size %d)", (*buffer)->GetPointer(), (*buffer)->GetTotalSize());
3f2a1b1c 538 delete *buffer;
70ed7d01 539 fgActiveBuffers.erase(buffer);
3f2a1b1c 540 }
950a0e40 541#ifdef ALIHLTSYSTEM_PROFILING
542 fgLogging.Logging(kHLTLogImportant, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "Total memory allocation: %d byte in %d buffers", iTotalSize, iCount);
543#endif //ALIHLTSYSTEM_PROFILING
3f2a1b1c 544 return iResult;
545}
546
0a7afbf0 547int AliHLTDataBuffer::PrintStatistics()
548{
549 // see header file for function documentation
550 int iResult=0;
551 int iFree=0;
552 int iActive=0;
553 AliHLTRawBufferPList::iterator buffer;;
554 for (buffer=fgFreeBuffers.begin(); buffer!=fgFreeBuffers.end(); buffer++) {
555 iFree+=(*buffer)->GetTotalSize();
556 }
557 for (buffer=fgActiveBuffers.begin(); buffer!=fgActiveBuffers.end(); buffer++) {
558 iActive+=(*buffer)->GetTotalSize();
559 }
560 fgLogging.Logging(kHLTLogInfo, "AliHLTDataBuffer::PrintStatistics", "data buffer handling", "Total memory allocation: %d byte; %d free buffers (%d byte) - %d active buffers (%d byte) ", iFree+iActive, fgFreeBuffers.size(), iFree, fgActiveBuffers.size(), iActive);
561 return iResult;
562}
563
2be3f004 564AliHLTConsumerDescriptor* AliHLTDataBuffer::FindConsumer(const AliHLTComponent* pConsumer, AliHLTConsumerDescriptorPList &list) const
3f2a1b1c 565{
70ed7d01 566 // see header file for function documentation
3f2a1b1c 567 AliHLTConsumerDescriptor* pDesc=NULL;
2be3f004 568 AliHLTConsumerDescriptorPList::iterator desc=list.begin();
3f2a1b1c 569 while (desc!=list.end() && pDesc==NULL) {
0c0c9d99 570 if ((pConsumer==NULL || (*desc)->GetComponent()==pConsumer)) {
3f2a1b1c 571 pDesc=*desc;
572 }
0c0c9d99 573 desc++;
3f2a1b1c 574 }
575 return pDesc;
576}
577
0c0c9d99 578int AliHLTDataBuffer::ResetDataBuffer()
579{
70ed7d01 580 // see header file for function documentation
3f2a1b1c 581 int iResult=0;
0c0c9d99 582 AliHLTRawBuffer* pBuffer=fpBuffer;
3f2a1b1c 583 fpBuffer=NULL;
9ce4bf4a 584
b46ca65e 585 // cleanup forwarded segment lists
586 assert(fForwardedSegments.size()==0);
587 fForwardedSegments.clear();
588 fForwardedSegmentSources.clear();
589
9ce4bf4a 590 // cleanup consumer states
2be3f004 591 AliHLTConsumerDescriptorPList::iterator desc;
b426991e 592// if (GetNofPendingConsumers()>0) {
593// desc=fConsumers.begin();
594// while (desc!=fConsumers.end()) {
595// AliHLTComponent* pComp=(*desc)->GetComponent();
596// HLTError("internal error: consumer %p (%s %p) did not get data from data buffer %p", *desc, pComp?pComp->GetComponentID():"", pComp, this);
597// desc++;
598// }
599// }
600 desc=fReleasedConsumers.begin();
3f2a1b1c 601 while (desc!=fReleasedConsumers.end()) {
602 AliHLTConsumerDescriptor* pDesc=*desc;
603 fReleasedConsumers.erase(desc);
604 desc=fReleasedConsumers.begin();
605 fConsumers.push_back(pDesc);
606 }
607 desc=fActiveConsumers.begin();
608 while (desc!=fActiveConsumers.end()) {
609 AliHLTConsumerDescriptor* pDesc=*desc;
48abe484 610 HLTWarning("consumer %p (%s) was not released", pDesc, pDesc->GetComponent()?pDesc->GetComponent()->GetComponentID():"### invalid component ###");
3f2a1b1c 611 fActiveConsumers.erase(desc);
612 desc=fActiveConsumers.begin();
613 fConsumers.push_back(pDesc);
614 }
9ce4bf4a 615
616 // cleanup segments
2be3f004 617 AliHLTDataSegmentList::iterator segment=fSegments.begin();
9ce4bf4a 618 while (segment!=fSegments.end()) {
619 fSegments.erase(segment);
620 segment=fSegments.begin();
621 }
622
623 // cleanup raw buffer
624 if (pBuffer) {
625 ReleaseRawBuffer(pBuffer);
626 }
3f2a1b1c 627 return iResult;
628}
629
9ce4bf4a 630int AliHLTDataBuffer::Reset()
631{
70ed7d01 632 // see header file for function documentation
9ce4bf4a 633 return ResetDataBuffer();
634}
635
0c0c9d99 636// this is the version which works on lists of components instead of consumer descriptors
2be3f004 637// int AliHLTDataBuffer::ChangeConsumerState(AliHLTComponent* pConsumer, AliHLTComponentPList &srcList, AliHLTComponentPList &tgtList)
0c0c9d99 638// {
639// int iResult=0;
640// if (pDesc) {
2be3f004 641// AliHLTComponentPList::iterator desc=srcList.begin();
0c0c9d99 642// while (desc!=srcList.end()) {
643// if ((*desc)==pConsumer) {
644// srcList.erase(desc);
645// tgtList.push_back(pConsumer);
646// break;
647// }
648// desc++;
649// }
650// if (desc==srcList.end()) {
651// HLTError("can not find consumer component %p in list", pConsumer);
652// iResult=-ENOENT;
653// }
654// } else {
655// HLTError("invalid parameter");
656// iResult=-EINVAL;
657// }
658// return iResult;
659// }
660
2be3f004 661int AliHLTDataBuffer::ChangeConsumerState(AliHLTConsumerDescriptor* pDesc, AliHLTConsumerDescriptorPList &srcList, AliHLTConsumerDescriptorPList &tgtList)
3f2a1b1c 662{
70ed7d01 663 // see header file for function documentation
9ce4bf4a 664 int iResult=-ENOENT;
3f2a1b1c 665 if (pDesc) {
2be3f004 666 AliHLTConsumerDescriptorPList::iterator desc=srcList.begin();
3f2a1b1c 667 while (desc!=srcList.end()) {
668 if ((*desc)==pDesc) {
669 srcList.erase(desc);
670 tgtList.push_back(pDesc);
9ce4bf4a 671 iResult=0;
3f2a1b1c 672 break;
673 }
0c0c9d99 674 desc++;
3f2a1b1c 675 }
9ce4bf4a 676 if (iResult<0) {
3f2a1b1c 677 HLTError("can not find consumer descriptor %p in list", pDesc);
3f2a1b1c 678 }
679 } else {
680 HLTError("invalid parameter");
681 iResult=-EINVAL;
682 }
683 return iResult;
684}
685
70ed7d01 686int AliHLTDataBuffer::CleanupConsumerList()
687{
688 // see header file for function documentation
3f2a1b1c 689 int iResult=0;
690 ResetDataBuffer();
2be3f004 691 AliHLTConsumerDescriptorPList::iterator desc=fConsumers.begin();
3f2a1b1c 692 while (desc!=fConsumers.end()) {
693 delete *desc;
694 fConsumers.erase(desc);
695 desc=fConsumers.begin();
696 }
697 return iResult;
698}
9ce4bf4a 699
d4a18597 700int AliHLTDataBuffer::FindConsumer(const AliHLTComponent* pConsumer, int bAllLists)
70ed7d01 701{
702 // see header file for function documentation
2be3f004 703 AliHLTConsumerDescriptorPList::iterator desc=fConsumers.begin();
9ce4bf4a 704 while (desc!=fConsumers.end()) {
705 if ((*desc)->GetComponent()==pConsumer)
706 return 1;
707 desc++;
708 }
709 if (bAllLists==0) return 0;
710
711 desc=fActiveConsumers.begin();
712 while (desc!=fActiveConsumers.end()) {
713 if ((*desc)->GetComponent()==pConsumer)
714 return 1;
715 desc++;
716 }
717 desc=fReleasedConsumers.begin();
718 while (desc!=fReleasedConsumers.end()) {
719 if ((*desc)->GetComponent()==pConsumer)
720 return 1;
721 desc++;
722 }
723 return 0;
724}
1e6e67ec 725
d6cbe999 726AliHLTDataBuffer::AliHLTRawBuffer::AliHLTRawBuffer(AliHLTUInt32_t size)
727 :
c0a2bfc2 728 fSize(0),
d6cbe999 729 fTotalSize(size),
0a7afbf0 730 fPtr(static_cast<AliHLTUInt8_t*>(malloc(size))),
731 fLastEventCount(0)
d6cbe999 732{
733 // see header file for class documentation
734 // or
735 // refer to README to build package
736 // or
737 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
738 if (fPtr==NULL) {
739 fSize=0;
740 fTotalSize=0;
741 }
742}
743
744AliHLTDataBuffer::AliHLTRawBuffer::~AliHLTRawBuffer()
745{
746 if (fPtr) {
747 free(fPtr);
748 }
749 fPtr=NULL;
750 fSize=0;
751 fTotalSize=0;
752}
753
3a7c0444 754int AliHLTDataBuffer::AliHLTRawBuffer::operator==(void* ptr) const
1e6e67ec 755{
3a7c0444 756 // see header file for function documentation
1e6e67ec 757 return fPtr == static_cast<AliHLTUInt8_t*>(ptr);
758}
759
3a7c0444 760int AliHLTDataBuffer::AliHLTRawBuffer::operator<=(void* ptr) const
1e6e67ec 761{
3a7c0444 762 // see header file for function documentation
1e6e67ec 763 int iResult=fPtr <= static_cast<AliHLTUInt8_t*>(ptr);
764 //printf("%p: %p <= %p (%d)\n", this, fPtr, ptr, iResult);
765 return iResult;
766}
767
3a7c0444 768int AliHLTDataBuffer::AliHLTRawBuffer::operator>(void* ptr) const
1e6e67ec 769{
3a7c0444 770 // see header file for function documentation
1e6e67ec 771 int iResult=fPtr+fSize > static_cast<AliHLTUInt8_t*>(ptr);
772 //printf("%p: %p + %d > %p (%d)\n", this, fPtr, fSize, ptr, iResult);
773 return iResult;
774}
775
3a7c0444 776int AliHLTDataBuffer::AliHLTRawBuffer::operator-(void* ptr) const
1e6e67ec 777{
3a7c0444 778 // see header file for function documentation
1e6e67ec 779 return static_cast<int>(static_cast<AliHLTUInt8_t*>(ptr)-fPtr);
780}
d6cbe999 781
782AliHLTUInt8_t* AliHLTDataBuffer::AliHLTRawBuffer::UseBuffer(AliHLTUInt32_t size)
783{
784 // see header file for function documentation
dba03d72 785 if (size>0 && fTotalSize>=size) {
d6cbe999 786 fSize=size;
0a7afbf0 787 fLastEventCount=AliHLTDataBuffer::fgEventCount;
d6cbe999 788 return fPtr;
789 }
790 return NULL;
791}
792
793int AliHLTDataBuffer::AliHLTRawBuffer::CheckSize(AliHLTUInt32_t size) const
794{
795 // see header file for function documentation
0a7afbf0 796 if (fTotalSize<size) return 0;
797 unsigned adjust=0;
798 if (fLastEventCount+1<AliHLTDataBuffer::fgEventCount) {
799 adjust=AliHLTDataBuffer::fgEventCount-fLastEventCount;
800 }
801 return (adjust>2) || ((fTotalSize-size)<(fgMargin<<adjust));
d6cbe999 802}
803
804int AliHLTDataBuffer::AliHLTRawBuffer::Reset()
805{
806 // see header file for function documentation
807 fSize=0;
2be3f004 808 return 0;
d6cbe999 809}
810
811int AliHLTDataBuffer::AliHLTRawBuffer::WritePattern(const char* pattern, int size)
812{
813 // see header file for function documentation
814 int iResult=0;
815 if (pattern!=NULL && size>0) {
816 if (fSize+size<=fTotalSize) {
817 memcpy(((char*)fPtr)+fSize, pattern, size);
818 iResult=size;
819 } else {
820 iResult=-ENOSPC;
821 }
822 }
823 return iResult;
824}
825
826int AliHLTDataBuffer::AliHLTRawBuffer::CheckPattern(const char* pattern, int size) const
827{
828 // see header file for function documentation
829 int iResult=0;
830 if (pattern!=NULL && size>0) {
831 if (fSize+size<=fTotalSize) {
832 iResult=memcmp(((char*)fPtr)+fSize, pattern, size)!=0;
833 } else {
834 iResult=-ENOSPC;
835 }
836 }
837 return iResult;
838}