]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/BASE/AliHLTDataBuffer.h
- abandon TPCLib backward compatibility check for AliRoot releases < v4-03
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTDataBuffer.h
CommitLineData
3f2a1b1c 1// @(#) $Id$
2
3#ifndef ALIHLTDATABUFFER_H
4#define ALIHLTDATABUFFER_H
5/* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
6 * See cxx source for full Copyright notice */
7
b22e91eb 8/** @file AliHLTDataBuffer.h
9 @author Matthias Richter
10 @date
11 @brief Handling of Data Buffers for HLT components.
12 @note The class is used in Offline (AliRoot) context
13*/
3f2a1b1c 14
30338a30 15// see below for class documentation
16// or
17// refer to README to build package
18// or
19// visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
20
8ede8717 21#include <vector>
2be3f004 22#include "TObject.h"
3f2a1b1c 23#include "AliHLTLogging.h"
24#include "AliHLTDataTypes.h"
2be3f004 25#include "AliHLTComponent.h"
3f2a1b1c 26
6235cd38 27class AliHLTConsumerDescriptor;
b46ca65e 28class AliHLTTask;
3f2a1b1c 29
2be3f004 30/** list of AliHLTConsumerDescriptor pointers */
31typedef vector<AliHLTConsumerDescriptor*> AliHLTConsumerDescriptorPList;
32
b46ca65e 33typedef AliHLTUInt8_t* AliHLTUInt8Pointer_t;
34
0c0c9d99 35/**
bfccbf68 36 * @class AliHLTDataBuffer
37 * @brief Handling of data buffers for the HLT.
b22e91eb 38 *
70ed7d01 39 * The class provides handling of data buffers for HLT tasks. Each task gets
40 * its own Data Buffer instance. The buffer is grouped into different data
41 * segments according to the output of the component.<br>
42 * The Data Buffer keeps control over the data requests of the 'child'
43 * components. Each component can subscribe to a certain segment of the data
44 * buffer. It's state is then changed from 'reserved' to 'active'. After the
45 * data processing, the component has to release the segment and it's state is
46 * set to 'processed'. If all components have requested and released their data,
47 * the Raw Buffer is released and pushed back in the list of available buffers.
b22e91eb 48 *
49 * @note This class is only used for the @ref alihlt_system.
50 *
51 * @ingroup alihlt_system
0c0c9d99 52 */
6235cd38 53class AliHLTDataBuffer : public TObject, public AliHLTLogging
54{
3f2a1b1c 55 public:
70ed7d01 56 //////////////////////////////////////////////////////////////////////////////
6235cd38 57 // constructors and destructors
0c0c9d99 58
59 /* standard constructor
60 */
3f2a1b1c 61 AliHLTDataBuffer();
85869391 62 /** destructor */
3f2a1b1c 63 virtual ~AliHLTDataBuffer();
64
70ed7d01 65 //////////////////////////////////////////////////////////////////////////////
3f2a1b1c 66 // initialization
67
0c0c9d99 68 /**
69 * Add component to the list of consumers
70 * @param pConsumer - a consumer of type AliHLTComponent
3f2a1b1c 71 */
0c0c9d99 72 int SetConsumer(AliHLTComponent* pConsumer);
3f2a1b1c 73
70ed7d01 74 //////////////////////////////////////////////////////////////////////////////
3f2a1b1c 75 // component to component communication
76
0c0c9d99 77 /**
70ed7d01 78 * Determine the number of matching data blocks for the component and a
79 * consumer component. <br>
80 * The first approach will support only one output data type for processing
81 * components.
0c0c9d99 82 * @param pConsumer the component which subscribes to the buffer
83 * @param tgtList (optional) the list to receive the data types
84 * @return: number of data blocks which match the input data types
85 * of the consumer, neg. error code if failed <br>
86 * -EINVAL invalid parameter <br>
3f2a1b1c 87 */
70ed7d01 88 int FindMatchingDataBlocks(const AliHLTComponent* pConsumer,
2be3f004 89 AliHLTComponentDataTypeList* tgtList=NULL);
0c0c9d99 90
91 /**
92 * Subscribe to a segment of the data buffer.
70ed7d01 93 * The function prepares the block descriptor for subsequent use with the
94 * AliHLTComponent::ProcessEvent method, the method can prepare several block
95 * descriptors up to the array size specified by iArraySize. The return value
96 * is independent from the array size the number of block descriptors which
97 * would have been prepared if there was enough space in the array<br>
0c0c9d99 98 * The method is used by the consumer component.
99 * @param pConsumer the component which subscribes to the buffer
e962f438 100 * @param blockDescList block descriptor vector to be filled
70ed7d01 101 * @return: number of matching data blocks, neg. error code if failed<br>
102 * -EACCESS the consumer state can't be changed (activated)
0c0c9d99 103 * -EBADF unresolved data segments <br>
104 * -ENOENT consumer component not found <br>
105 * -ENODATA data buffer does not have raw data <br>
106 * -EINVAL invalid parameter <br>
107 */
457ec821 108 int Subscribe(const AliHLTComponent* pConsumer,
109 AliHLTComponentBlockDataList& blockDescList);
110
0c0c9d99 111 /**
112 * Release an instance of the data buffer.
113 * Resets the variables of the block descriptor.
114 * If all buffer segments are released, the Data Buffer is reseted
115 * and the Raw Buffer released.<br>
116 * The method is used by the consumer component.
117 * @param pBlockDesc descriptor of the data segment
118 * @param pConsumer the component which subscribes to the buffer
b46ca65e 119 * @param pOwnerTask task owning this buffer
0c0c9d99 120 * @return: >0 if success, negative error code if failed <br>
70ed7d01 121 * -EACCESS the consumer state can not be changed (de-activated)
122 * -ENOENT consumer has not subscribed to the buffer <br>
0c0c9d99 123 * -EINVAL invalid parameter <br>
3f2a1b1c 124 */
b46ca65e 125 int Release(AliHLTComponentBlockData* pBlockDesc, const AliHLTComponent* pConsumer,
126 const AliHLTTask* pOwnerTask);
127
128 /**
129 * Register an input data block for forwarding.
130 * Consumer of this data buffer subscribe to forwarded data blocks in te same way.
131 * Forwarded data blocks are released when the last consumer has released the
132 * blocks.
133 * @param pSrcTask original source task of the data block
134 * @param pBlockDesc descriptor of the data segment
135 */
136 int Forward(AliHLTTask* pSrcTask, AliHLTComponentBlockData* pBlockDesc);
3f2a1b1c 137
0c0c9d99 138 /**
139 * Get a target buffer of minimum size iMinSize.
140 * The method is used by the component which owns the Data Buffer to
141 * allocate a buffer for the data it is going to produce.
142 * @param iMinSize minumum size of the requested buffer
143 * @return: pointer to target buffer if
3f2a1b1c 144 */
145 AliHLTUInt8_t* GetTargetBuffer(int iMinSize);
146
0c0c9d99 147 /**
148 * Set the segments for the data buffer.
149 * This is usually done after the component has written the data to the buffer,
150 * which was requested by the @ref GetTargetBuffer method. The component might
151 * produce different types of data, for each type a segment has to be defined
2d7ff710 152 * which describes the data inside the buffer.<br>
70ed7d01 153 * The @ref AliHLTComponentBlockData segment descriptor comes directly from
154 * the @ref AliHLTComponent::ProcessEvent method.
0c0c9d99 155 * @param pTgt the target buffer which the segments refer to
156 * @param arraySegments the output block descriptors of the component
157 * @param iSize size of the array
3f2a1b1c 158 */
8ede8717 159 int SetSegments(AliHLTUInt8_t* pTgt, AliHLTComponentBlockData* arraySegments, int iSize);
3f2a1b1c 160
0c0c9d99 161 /**
162 * Check if the data buffer is empty.
163 * @return 1 if empty, 0 if not
3f2a1b1c 164 */
165 int IsEmpty();
166
0c0c9d99 167 /**
168 * Get the total and maximum size of the buffer.
169 * Lets see if this is needed later
3f2a1b1c 170 */
171 //int GetTotalSize();
172
0c0c9d99 173 /**
174 * Get the number of segments
175 * @return number of segments
3f2a1b1c 176 */
177 int GetNofSegments();
178
0c0c9d99 179 /**
b426991e 180 * Get the total number of consumers.
181 * This gives the number of consumers regardless of their state.
0c0c9d99 182 * @return number of consumers
3f2a1b1c 183 */
184 int GetNofConsumers();
185
0c0c9d99 186 /**
b426991e 187 * Get the number of consumers which still need to be processed during
188 * the current event.
189 * @return number of consumers
190 */
191 int GetNofPendingConsumers();
192
193 /**
194 * Get the number of consumers currently under processing.
0c0c9d99 195 * @return number of active consumers
3f2a1b1c 196 */
197 int GetNofActiveConsumers();
198
9ce4bf4a 199 /**
200 * Check if a consumer is already in the list
201 * @param pConsumer pointer to consumer component
202 * @param bAllLists search in all lists if 1
203 * search only in fConsumer list if 0
204 * @return 1 if found, 0 if not
205 */
206 int FindConsumer(AliHLTComponent* pConsumer, int bAllLists=1);
207
208 /**
209 * Public method to reset the buffer.
210 * Eventually with some additional checks. In normal operation,
211 * an external reset should not be necessary.
212 */
213 int Reset();
214
dba03d72 215 /**
216 * Set local logging level
217 * logging filter for individual object
218 */
219 void SetLocalLoggingLevel(AliHLTComponentLogSeverity level)
220 {fgLogging.SetLocalLoggingLevel(level); AliHLTLogging::SetLocalLoggingLevel(level);}
221
6235cd38 222 /**
965919c8 223 * @class AliHLTDataSegment
6235cd38 224 * @brief Descriptor of a data segment within the buffer.
225 */
66043029 226 class AliHLTDataSegment {
b46ca65e 227 friend class AliHLTDataBuffer;
228 friend class AliHLTConsumerDescriptor;
66043029 229 public:
6235cd38 230 AliHLTDataSegment()
231 :
b46ca65e 232 fDataType(kAliHLTVoidDataType),
233 fPtr(NULL),
6235cd38 234 fSegmentOffset(0),
235 fSegmentSize(0),
236 fSpecification(0)
237 {
6235cd38 238 }
b46ca65e 239
240 AliHLTDataSegment(AliHLTUInt8_t* ptr, AliHLTUInt32_t offset, AliHLTUInt32_t size)
241 :
242 fDataType(kAliHLTVoidDataType),
243 fPtr(ptr),
244 fSegmentOffset(offset),
245 fSegmentSize(size),
246 fSpecification(0)
247 {
248 }
249
250 AliHLTDataSegment(void* ptr, AliHLTUInt32_t offset, AliHLTUInt32_t size)
6235cd38 251 :
b46ca65e 252 fDataType(kAliHLTVoidDataType),
253 fPtr((AliHLTUInt8_t*)ptr),
6235cd38 254 fSegmentOffset(offset),
255 fSegmentSize(size),
256 fSpecification(0)
257 {
6235cd38 258 }
2be3f004 259
b46ca65e 260 AliHLTDataSegment(void* ptr, AliHLTUInt32_t offset, AliHLTUInt32_t size, AliHLTComponentDataType dt, AliHLTUInt32_t spec)
261 :
262 fDataType(dt),
263 fPtr((AliHLTUInt8_t*)ptr),
264 fSegmentOffset(offset),
265 fSegmentSize(size),
266 fSpecification(spec)
267 {
268 }
269
270 AliHLTUInt8_t* GetPtr() const {return (AliHLTUInt8_t*)*this;}
271
272 AliHLTUInt32_t GetSize() const {return fSegmentSize;}
273
274 int operator==(const AliHLTDataSegment& seg) const
275 {
276 return (fPtr+fSegmentOffset==seg.fPtr+seg.fSegmentOffset) && (fSegmentSize==seg.fSegmentSize);
277 }
278 operator AliHLTUInt8_t*() const {return fPtr+fSegmentOffset;}
279
5df0cbb9 280 private:
6235cd38 281 /** the data type of this segment */
282 AliHLTComponentDataType fDataType; // see above
b46ca65e 283 /** pointer to the buffer */
284 AliHLTUInt8Pointer_t fPtr; //!transient
6235cd38 285 /** offset in byte within the data buffer */
286 AliHLTUInt32_t fSegmentOffset; // see above
287 /** size of the actual content */
288 AliHLTUInt32_t fSegmentSize; // see above
289 /** data specification */
290 AliHLTUInt32_t fSpecification; // see above
291 };
292
293 /**
965919c8 294 * @class AliHLTRawBuffer
6235cd38 295 * @brief Descriptor of the raw data buffer which can host several segments.
296 */
66043029 297 class AliHLTRawBuffer {
298 public:
299 /** standard constructor */
300 AliHLTRawBuffer() : fSize(0), fTotalSize(0), fPtr(NULL) {}
d6cbe999 301 /** constructor */
302 AliHLTRawBuffer(AliHLTUInt32_t size);
303 /** destructor */
304 virtual ~AliHLTRawBuffer();
305
306 /**
307 * Use a fraction of the buffer.
308 * @param size size in bytes to be used
309 * @return pointer to buffer
310 */
311 AliHLTUInt8_t* UseBuffer(AliHLTUInt32_t size);
312
313 /**
314 * Check whether buffer fits for a request.
315 * @param size size of the request in bytes
316 * @return 1 if buffer is big enough, 0 if not
317 */
318 int CheckSize(AliHLTUInt32_t size) const;
319
c0a2bfc2 320 /**
321 * Get used size of the buffer
322 */
323 AliHLTUInt32_t GetUsedSize() const {return fSize;}
324
d6cbe999 325 /**
326 * Get total size of the buffer
327 */
328 AliHLTUInt32_t GetTotalSize() const {return fTotalSize;}
329
c0a2bfc2 330 /**
331 * Get pointer of data buffer
332 */
333 AliHLTUInt8_t* GetPointer() const {return fPtr;}
334
d6cbe999 335 /**
336 * Write check pattern
337 */
338 int WritePattern(const char* pattern, int size);
339
340 /**
341 * Check pattern
342 */
343 int CheckPattern(const char* pattern, int size) const;
344
345 /**
346 * Reset buffer.
347 * Data buffer remains allocated, used size set to 0
348 */
349 int Reset();
1e6e67ec 350
3a7c0444 351 int operator==(void*) const;
352 int operator==(AliHLTUInt8_t* ptr) const {return fPtr==ptr;}
353 int operator<=(void*) const;
354 int operator>(void*) const;
355 int operator-(void*) const;
1e6e67ec 356
3a7c0444 357 operator void*() const {return fPtr;}
358 operator AliHLTUInt8_t*() const {return fPtr;}
1e6e67ec 359
5df0cbb9 360 private:
d6cbe999 361 /** copy constructor prohibited */
362 AliHLTRawBuffer(const AliHLTRawBuffer&);
363 /** assignment operator prohibited */
364 AliHLTRawBuffer& operator=(const AliHLTRawBuffer&);
365
6235cd38 366 /** size of the currently occupied partition of the buffer */
367 AliHLTUInt32_t fSize; // see above
368 /** total size of the buffer, including safety margin */
369 AliHLTUInt32_t fTotalSize; // see above
370 /** the buffer */
1e6e67ec 371 AliHLTUInt8_t* fPtr; //! transient
6235cd38 372 };
373
3f2a1b1c 374 private:
b426991e 375 /** copy constructor prohibited */
376 AliHLTDataBuffer(const AliHLTDataBuffer&);
377 /** assignment operator prohibited */
378 AliHLTDataBuffer& operator=(const AliHLTDataBuffer&);
379
0c0c9d99 380 /* lets see if this is needed
6235cd38 381 AliHLTDataSegment* FindDataSegment(AliHLTComponentDataType datatype);
0c0c9d99 382 */
383
384 /**
385 * Find those data segments which match the input types of a component.
386 * @param pConsumer the component which subscribes to the buffer
387 * @param tgtList the list to receive the data segment descriptors
388 * @return: number of data blocks which match the input data types
389 * of the consumer, neg. error code if failed <br>
390 * -EINVAL invalid parameter <br>
391 */
90ebac25 392 int FindMatchingDataSegments(const AliHLTComponent* pConsumer,
393 vector<AliHLTDataBuffer::AliHLTDataSegment>& tgtList);
3f2a1b1c 394
0c0c9d99 395 /**
396 * Reset the data buffer.
9ce4bf4a 397 * Removes all consumers back to the @ref fConsumers list, deletes
398 * segments and releases the Raw Buffer.
3f2a1b1c 399 */
400 int ResetDataBuffer();
401
70ed7d01 402 //////////////////////////////////////////////////////////////////////////////
403
3f2a1b1c 404 // the data description
3f2a1b1c 405
0c0c9d99 406 // the data segments within this buffer
70ed7d01 407 vector<AliHLTDataSegment> fSegments; // see above
3f2a1b1c 408
0c0c9d99 409 // the list of all consumers which are going to subscribe to the buffer
2be3f004 410 AliHLTConsumerDescriptorPList fConsumers; // see above
0c0c9d99 411 // the list of all consumers which are currently subscribed to the buffer
2be3f004 412 AliHLTConsumerDescriptorPList fActiveConsumers; // see above
0c0c9d99 413 // the list of all consumers which are already released for the current event
2be3f004 414 AliHLTConsumerDescriptorPList fReleasedConsumers; // see above
3f2a1b1c 415
0c0c9d99 416 // the buffer instance
70ed7d01 417 AliHLTRawBuffer* fpBuffer; //! transient
3f2a1b1c 418
0c0c9d99 419 // flags indicating the state of the buffer
70ed7d01 420 AliHLTUInt32_t fFlags; // see above
3f2a1b1c 421
b46ca65e 422 /** list of tasks with forwarded data blocks */
423 vector<AliHLTTask*> fForwardedSegmentSources; //! transient
424
425 /** list of forwarded block descriptors */
426 vector<AliHLTDataSegment> fForwardedSegments; //! transient
427
70ed7d01 428 //////////////////////////////////////////////////////////////////////////////
0c0c9d99 429 // global buffer handling, internal use only
430
431 /**
432 * Create a raw buffer of a certain size.
433 * The function tries to find a buffer of the given size (or a bit bigger by a
9ddaea75 434 * certain margin @ref fgMargin) from the list of free buffers.
0c0c9d99 435 * If no buffer is available, a new one is created and added to the buffer handling.
436 * @param size min. size of the requested buffer
437 * @return pointer to raw buffer
3f2a1b1c 438 */
439 static AliHLTRawBuffer* CreateRawBuffer(AliHLTUInt32_t size);
440
0c0c9d99 441 /**
442 * Mark a buffer as free.
70ed7d01 443 * After the Data Buffer has finnished using the raw buffer, it is released
444 * and added to the list of available buffers.
0c0c9d99 445 * @param pBuffer the raw buffer to release
446 * @return >=0 if succeeded, neg. error code if failed
3f2a1b1c 447 */
448 static int ReleaseRawBuffer(AliHLTRawBuffer* pBuffer);
449
0c0c9d99 450 /**
451 * Deletes all the raw buffers.
70ed7d01 452 * When the last Data Buffer object is destructed, all raw data buffers are
453 * relesed.
3f2a1b1c 454 */
455 static int DeleteRawBuffers();
456
0c0c9d99 457 /**
458 * Number of instances of AliHLTDataBuffer.
70ed7d01 459 * The statice variable is incremented and decremented in the constructor/
460 * destructor. All internal data structures are cleaned up when the last
461 * instance is exiting.
0c0c9d99 462 */
70ed7d01 463 static int fgNofInstances; // see above
0c0c9d99 464 /** global list of free raw buffers */
70ed7d01 465 static vector<AliHLTRawBuffer*> fgFreeBuffers; // see above
0c0c9d99 466 /** global list of currently active raw buffers */
70ed7d01 467 static vector<AliHLTRawBuffer*> fgActiveBuffers; // see above
0c0c9d99 468 /** determines the raw buffer size margin at buffer requests */
70ed7d01 469 static AliHLTUInt32_t fgMargin; // see above
3f2a1b1c 470
0c0c9d99 471 /** global instance to HLT logging class for static methods */
70ed7d01 472 static AliHLTLogging fgLogging; // see above
3f2a1b1c 473
8451168b 474 /** size of the safety pattern */
475 static const Int_t fgkSafetyPatternSize; // see above
476
477 /** the safety pattern */
478 static const char fgkSafetyPattern[]; //!transient
479
70ed7d01 480 //////////////////////////////////////////////////////////////////////////////
0c0c9d99 481 // internal helper functions
482
483 /**
484 * Find the consumer descriptor for a certain component and data type in
485 * a list of consumers.<br>
70ed7d01 486 * <b>Note:</b> There are three lists which contain the consumers in the
487 * different states.
0c0c9d99 488 * @param pConsumer pointer to consumer component
489 * @param list list where to search for the consumer
490 */
70ed7d01 491 AliHLTConsumerDescriptor* FindConsumer(const AliHLTComponent* pConsumer,
2be3f004 492 AliHLTConsumerDescriptorPList &list) const;
0c0c9d99 493
494 /**
495 * Change the state of a consumer.
70ed7d01 496 * The state of a consumer is determined by the list it is strored in, the
497 * method moves a consumer from the source to the target list.
0c0c9d99 498 * @param pDesc pointer to consumer descriptor
499 * @param srcList list where the consumer is currently to be found
500 * @param tgtList list where to move the consumer
501 */
70ed7d01 502 int ChangeConsumerState(AliHLTConsumerDescriptor* pDesc,
2be3f004 503 AliHLTConsumerDescriptorPList &srcList,
504 AliHLTConsumerDescriptorPList &tgtList);
3f2a1b1c 505
0c0c9d99 506 /**
507 * Cleanup a consumer list.
508 * Release all allocated data structures. <b>Note:</b> Not the component itself!
509 */
3f2a1b1c 510 int CleanupConsumerList();
511
512 ClassDef(AliHLTDataBuffer, 0)
513};
6235cd38 514
3f2a1b1c 515#endif // ALIHLTDATABUFFER_H