-// @(#) $Id$
+//-*- Mode: C++ -*-
+// $Id$
#ifndef ALIHLTDATABUFFER_H
#define ALIHLTDATABUFFER_H
-/* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
- * See cxx source for full Copyright notice */
-
-/** @file AliHLTDataBuffer.h
- @author Matthias Richter
- @date
- @brief Handling of Data Buffers for HLT components.
- @note The class is used in Offline (AliRoot) context
-*/
-
-// see below for class documentation
-// or
-// refer to README to build package
-// or
-// visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
+//* This file is property of and copyright by the *
+//* ALICE Experiment at CERN, All rights reserved. *
+//* See cxx source for full Copyright notice *
+
+// @file AliHLTDataBuffer.h
+// @author Matthias Richter
+// @date
+// @brief Handling of Data Buffers for HLT components.
+// @note The class is used in Offline (AliRoot) context
#include <vector>
#include "TObject.h"
#include "AliHLTDataTypes.h"
#include "AliHLTComponent.h"
+using std::vector;
+
class AliHLTConsumerDescriptor;
class AliHLTTask;
int Release(AliHLTComponentBlockData* pBlockDesc, const AliHLTComponent* pConsumer,
const AliHLTTask* pOwnerTask);
+ /**
+ * Release a forwarded data block.
+ */
+ int ReleaseForwardedBlock(AliHLTComponentBlockData* pBlockDesc,
+ const AliHLTTask* pOwnerTask);
+
/**
* Register an input data block for forwarding.
* Consumer of this data buffer subscribe to forwarded data blocks in te same way.
* @return: pointer to target buffer if
*/
AliHLTUInt8_t* GetTargetBuffer(int iMinSize);
+ static unsigned int GetMaxBufferSize();
/**
* Set the segments for the data buffer.
//int GetTotalSize();
/**
- * Get the number of segments
+ * Get the number of segments including the forwarded data blocks.
* @return number of segments
*/
- int GetNofSegments();
+ int GetNofSegments() const;
/**
* Get the total number of consumers.
* This gives the number of consumers regardless of their state.
* @return number of consumers
*/
- int GetNofConsumers();
+ int GetNofConsumers() const;
/**
* Get the number of consumers which still need to be processed during
* the current event.
* @return number of consumers
*/
- int GetNofPendingConsumers();
+ int GetNofPendingConsumers() const;
/**
* Get the number of consumers currently under processing.
* @return number of active consumers
*/
- int GetNofActiveConsumers();
+ int GetNofActiveConsumers() const;
/**
* Check if a consumer is already in the list
* search only in fConsumer list if 0
* @return 1 if found, 0 if not
*/
- int FindConsumer(AliHLTComponent* pConsumer, int bAllLists=1);
+ int FindConsumer(const AliHLTComponent* pConsumer, int bAllLists=1);
/**
* Public method to reset the buffer.
*/
int Reset();
+ /**
+ * Print info about the buffer
+ */
+ virtual void Print(const char* option) const;
+
/**
* Set local logging level
* logging filter for individual object
void SetLocalLoggingLevel(AliHLTComponentLogSeverity level)
{fgLogging.SetLocalLoggingLevel(level); AliHLTLogging::SetLocalLoggingLevel(level);}
+ /**
+ * Print summary of the global buffer management.
+ */
+ static int PrintStatistics();
+
+ /**
+ * Set the global event count.
+ * The event count is deployed to find buffers which have not been used
+ * for a while. In such a case to policy to find an appropriate buffer is
+ * adjusted.
+ */
+ static int SetGlobalEventCount(AliHLTUInt32_t eventCount) {fgEventCount=eventCount; return 0;}
+
/**
* @class AliHLTDataSegment
* @brief Descriptor of a data segment within the buffer.
*/
class AliHLTDataSegment {
- friend class AliHLTDataBuffer;
- friend class AliHLTConsumerDescriptor;
+ friend class AliHLTDataBuffer; // TODO: implement some getters/setters
public:
AliHLTDataSegment()
:
AliHLTDataSegment(void* ptr, AliHLTUInt32_t offset, AliHLTUInt32_t size)
:
fDataType(kAliHLTVoidDataType),
- fPtr((AliHLTUInt8_t*)ptr),
+ fPtr(reinterpret_cast<AliHLTUInt8_t*>(ptr)),
fSegmentOffset(offset),
fSegmentSize(size),
fSpecification(0)
AliHLTDataSegment(void* ptr, AliHLTUInt32_t offset, AliHLTUInt32_t size, AliHLTComponentDataType dt, AliHLTUInt32_t spec)
:
fDataType(dt),
- fPtr((AliHLTUInt8_t*)ptr),
+ fPtr(reinterpret_cast<AliHLTUInt8_t*>(ptr)),
fSegmentOffset(offset),
fSegmentSize(size),
fSpecification(spec)
{
}
+ AliHLTDataSegment(const AliHLTDataSegment& src)
+ :
+ fDataType(src.fDataType),
+ fPtr(src.fPtr),
+ fSegmentOffset(src.fSegmentOffset),
+ fSegmentSize(src.fSegmentSize),
+ fSpecification(src.fSpecification)
+ {
+ // AliHLTDataSegment just stores external pointers and properties
+ }
+
+ AliHLTDataSegment& operator=(const AliHLTDataSegment& src)
+ {
+ // AliHLTDataSegment just stores external pointers and properties
+ if (this==&src) return *this;
+ fDataType=src.fDataType;
+ fPtr=src.fPtr;
+ fSegmentOffset=src.fSegmentOffset;
+ fSegmentSize=src.fSegmentSize;
+ fSpecification=src.fSpecification;
+ return *this;
+ }
+
+ virtual ~AliHLTDataSegment() {}
+
AliHLTUInt8_t* GetPtr() const {return (AliHLTUInt8_t*)*this;}
AliHLTUInt32_t GetSize() const {return fSegmentSize;}
}
operator AliHLTUInt8_t*() const {return fPtr+fSegmentOffset;}
+ virtual void Print(const char* option) const;
+
private:
/** the data type of this segment */
AliHLTComponentDataType fDataType; // see above
AliHLTUInt32_t fSegmentSize; // see above
/** data specification */
AliHLTUInt32_t fSpecification; // see above
+
+ };
+
+ /**
+ * @class AliHLTForwardedDataSegment
+ * @brief Descriptor of a forwarded data segment.
+ * Contains in addition information about the parent of this forwarded
+ * block and the original data type and specification
+ */
+ class AliHLTForwardedDataSegment : public AliHLTDataSegment {
+ friend class AliHLTDataBuffer; // TODO: implement some getters/setters
+ public:
+ AliHLTForwardedDataSegment()
+ : AliHLTDataSegment()
+ , fParentSegment()
+ , fParentTask(NULL)
+ {
+ }
+
+ AliHLTForwardedDataSegment(AliHLTDataSegment& mySegment, AliHLTDataSegment& parentSegment, AliHLTTask* parentTask)
+ : AliHLTDataSegment(mySegment)
+ , fParentSegment(parentSegment)
+ , fParentTask(parentTask)
+ {
+ }
+
+ AliHLTForwardedDataSegment(const AliHLTForwardedDataSegment& src)
+ : AliHLTDataSegment(src),
+ fParentSegment(src.fParentSegment),
+ fParentTask(src.fParentTask)
+ {
+ // AliHLTForwardedDataSegment just stores external pointers and properties
+ }
+
+ AliHLTForwardedDataSegment& operator=(const AliHLTForwardedDataSegment& src)
+ {
+ // AliHLTForwardedDataSegment just stores external pointers and properties
+ AliHLTDataSegment::operator=(src);
+ fParentSegment=src.fParentSegment;
+ fParentTask=src.fParentTask;
+ return *this;
+ }
+
+ virtual ~AliHLTForwardedDataSegment() {}
+
+ virtual void Print(const char* option) const;
+
+ private:
+ /// description of the original segment
+ AliHLTDataSegment fParentSegment; // see above
+ /// the parent task
+ AliHLTTask* fParentTask; //!transient
+ };
+
+ typedef vector<AliHLTDataBuffer::AliHLTDataSegment> AliHLTDataSegmentList;
+
+ class AliHLTRawBuffer;
+ typedef vector<AliHLTRawBuffer*> AliHLTRawBufferPList;
+
+ /**
+ * @class AliHLTRawPage
+ * Memory allocation is organized in pages of a fixed size. Within a
+ * page, AliHLTRawBuffer chunks are created.
+ */
+ class AliHLTRawPage : public AliHLTLogging {
+ public:
+ /** standard constructor */
+ AliHLTRawPage() : fSize(0), fPtr(NULL), fFreeBuffers(), fUsedBuffers() {}
+ /** constructor */
+ AliHLTRawPage(AliHLTUInt32_t pagesize);
+ /** destructor */
+ virtual ~AliHLTRawPage();
+
+ /** alloc a buffer of specified size from the global pages*/
+ static AliHLTRawBuffer* GlobalAlloc(AliHLTUInt32_t size, int verbosity=0);
+ /** find buffer in the global pages */
+ static AliHLTRawPage* FindPage(AliHLTRawBuffer* buffer);
+ /** cleanup the global pages */
+ static int GlobalClean();
+ /** adjust global page size */
+ static void SetGlobalPageSize(AliHLTUInt32_t size) {fgGlobalPageSize=size;}
+ static AliHLTUInt32_t GetGlobalPageSize() {return fgGlobalPageSize;}
+ /** find next page after prev, or first page */
+ static AliHLTRawPage* NextPage(const AliHLTRawPage* prev=NULL);
+
+ /** alloc a buffer of specified size */
+ AliHLTRawBuffer* Alloc(AliHLTUInt32_t size);
+ /** free a buffer and merge consecutive free buffers */
+ int Free(AliHLTRawBuffer* pBuffer);
+ /** set the size of a raw buffer and release the remaining part */
+ int SetSize(const AliHLTRawBuffer* pBuffer, AliHLTUInt32_t size);
+ /// check if the buffer is in this page
+ bool HasBuffer(const AliHLTRawBuffer* pBuffer);
+
+ AliHLTUInt32_t Size() const {return fSize;}
+ AliHLTUInt32_t Capacity() const;
+ bool IsUsed() const {return fUsedBuffers.size()>0;}
+ bool IsFragmented() const {return (fFreeBuffers.size()+fUsedBuffers.size())>1;}
+
+ /**
+ * Print page information
+ */
+ virtual void Print(const char* option);
+
+ private:
+ /** copy constructor prohibited */
+ AliHLTRawPage(const AliHLTRawPage&);
+ /** assignment operator prohibited */
+ AliHLTRawPage& operator=(const AliHLTRawPage&);
+
+ /// list of global pages
+ static vector<AliHLTDataBuffer::AliHLTRawPage*> fgGlobalPages; //! transient
+ /// pages size of global pages
+ static AliHLTUInt32_t fgGlobalPageSize; //! transient
+
+ /** page size */
+ AliHLTUInt32_t fSize; // see above
+ /** the memory segment */
+ AliHLTUInt8_t* fPtr; //! transient
+
+ /** list of free buffers */
+ AliHLTRawBufferPList fFreeBuffers; //! transient
+ /** list of used buffers */
+ AliHLTRawBufferPList fUsedBuffers; //! transient
};
/**
class AliHLTRawBuffer {
public:
/** standard constructor */
- AliHLTRawBuffer() : fSize(0), fTotalSize(0), fPtr(NULL) {}
+ AliHLTRawBuffer() : fSize(0), fTotalSize(0), fExternalPtr(NULL), fPtr(NULL), fLastEventCount(0) {}
/** constructor */
AliHLTRawBuffer(AliHLTUInt32_t size);
+ /** constructor */
+ AliHLTRawBuffer(AliHLTUInt32_t size, AliHLTUInt8_t* buffer);
/** destructor */
virtual ~AliHLTRawBuffer();
*/
AliHLTUInt8_t* UseBuffer(AliHLTUInt32_t size);
+ /**
+ * split a buffer at specified size
+ * only possible for buffers with external memory
+ */
+ AliHLTRawBuffer* Split(AliHLTUInt32_t size);
+
/**
* Check whether buffer fits for a request.
+ * A buffer fits if it is at least of the requested size and at most
+ * the requested size plus a margin. The margin increases with the
+ * number of events the buffer has not been used.
* @param size size of the request in bytes
* @return 1 if buffer is big enough, 0 if not
*/
*/
int Reset();
- int operator==(void*) const;
+ /*
+ * Merge buffer with succeeding buffer.
+ * Only possible if the buffers are consecutive with out any gap.
+ */
+ int Merge(const AliHLTRawBuffer& succ);
+
+ /**
+ * Print buffer information
+ */
+ virtual void Print(const char* option) const;
+
+ int operator==(void* ptr) const;
int operator==(AliHLTUInt8_t* ptr) const {return fPtr==ptr;}
- int operator<=(void*) const;
- int operator>(void*) const;
- int operator-(void*) const;
+ int operator<(void* ptr) const;
+ int operator<=(void* ptr) const;
+ int operator>(void* ptr) const;
+ int operator-(void* ptr) const;
+ int operator<(const AliHLTRawBuffer& op) const;
+ int operator<=(const AliHLTRawBuffer& op) const;
+ int operator>(const AliHLTRawBuffer& op) const;
operator void*() const {return fPtr;}
operator AliHLTUInt8_t*() const {return fPtr;}
AliHLTUInt32_t fSize; // see above
/** total size of the buffer, including safety margin */
AliHLTUInt32_t fTotalSize; // see above
- /** the buffer */
+ /** optional external buffer */
+ AliHLTUInt8_t* fExternalPtr; //! transient
+ /** the buffer, external or allocated */
AliHLTUInt8_t* fPtr; //! transient
+ /** last event count where the buffer has been used */
+ AliHLTUInt32_t fLastEventCount; //! transient
};
private:
* -EINVAL invalid parameter <br>
*/
int FindMatchingDataSegments(const AliHLTComponent* pConsumer,
- vector<AliHLTDataBuffer::AliHLTDataSegment>& tgtList);
+ AliHLTDataSegmentList& tgtList);
+ protected:
+ // 2010-02-01 make function protected in order to be used from unit test
/**
* Reset the data buffer.
* Removes all consumers back to the @ref fConsumers list, deletes
* segments and releases the Raw Buffer.
*/
int ResetDataBuffer();
+ private:
//////////////////////////////////////////////////////////////////////////////
*/
static AliHLTRawBuffer* CreateRawBuffer(AliHLTUInt32_t size);
+ /**
+ * Set the data size of a raw buffer after it has been filled by
+ * the component.
+ */
+ int SetRawBufferDataSize(AliHLTRawBuffer* pBuffer, AliHLTUInt32_t size) const;
+
/**
* Mark a buffer as free.
* After the Data Buffer has finnished using the raw buffer, it is released
/** the safety pattern */
static const char fgkSafetyPattern[]; //!transient
+ static AliHLTUInt32_t fgEventCount; //!transient
+
//////////////////////////////////////////////////////////////////////////////
// internal helper functions
*/
int CleanupConsumerList();
- ClassDef(AliHLTDataBuffer, 0)
+ ClassDef(AliHLTDataBuffer, 1)
};
#endif // ALIHLTDATABUFFER_H