]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - HLT/BASE/AliHLTDataBuffer.cxx
Intermediary MUONDA
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTDataBuffer.cxx
index 36a9d83f2982dc17796e50e6487e100f47fed016..4b6ffba367c22d2dc47b8576bbb2bd92e7d38d65 100644 (file)
@@ -1,7 +1,7 @@
 // $Id$
 
 //**************************************************************************
-//* This file is property of and copyright by the ALICE HLT Project        * 
+//* This file is property of and copyright by the                          * 
 //* ALICE Experiment at CERN, All rights reserved.                         *
 //*                                                                        *
 //* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
 //* provided "as is" without express or implied warranty.                  *
 //**************************************************************************
 
-/** @file   AliHLTDataBuffer.cxx
-    @author Matthias Richter
-    @date   
-    @brief  Handling of Data Buffers for HLT components.
-*/
-
-#if __GNUC__>= 3
-using namespace std;
-#endif
+//  @file   AliHLTDataBuffer.cxx
+//  @author Matthias Richter
+//  @date   
+//  @brief  Handling of Data Buffers for HLT components.
+//  @note   Only used in the AliRoot framework
 
 #include "AliHLTDataBuffer.h"
 #include "AliHLTConsumerDescriptor.h"
@@ -35,9 +31,7 @@ using namespace std;
 //#include <string>
 //#include "AliHLTSystem.h"
 
-#define USE_ALIHLTRAWPAGE
-
-typedef vector<AliHLTDataBuffer::AliHLTDataSegment> AliHLTDataSegmentList;
+using std::cout;
 
 /** ROOT macro for the implementation of ROOT specific class methods */
 ClassImp(AliHLTDataBuffer)
@@ -129,7 +123,8 @@ int AliHLTDataBuffer::FindMatchingDataBlocks(const AliHLTComponent* pConsumer, A
   return iResult;
 }
 
-int AliHLTDataBuffer::FindMatchingDataSegments(const AliHLTComponent* pConsumer, vector<AliHLTDataBuffer::AliHLTDataSegment>& tgtList)
+int AliHLTDataBuffer::FindMatchingDataSegments(const AliHLTComponent* /*pConsumer*/,
+                                              AliHLTDataSegmentList& tgtList)
 {
   // see header file for function documentation
   int iResult=0;
@@ -143,27 +138,6 @@ int AliHLTDataBuffer::FindMatchingDataSegments(const AliHLTComponent* pConsumer,
   tgtList.insert(tgtList.begin(), fForwardedSegments.begin(), fForwardedSegments.end());
   iResult=tgtList.size();
   return iResult;
-  
-  if (pConsumer) {
-    AliHLTComponentDataTypeList dtlist;
-    ((AliHLTComponent*)pConsumer)->GetInputDataTypes(dtlist);
-    AliHLTDataSegmentList::iterator segment=fSegments.begin();
-    while (segment!=fSegments.end()) {
-      AliHLTComponentDataTypeList::iterator type=dtlist.begin();
-      while (type!=dtlist.end()) {
-       if ((*segment).fDataType==(*type)) {
-         tgtList.push_back(*segment);
-         iResult++;
-         break;
-       }
-       type++;
-      }
-      segment++;
-    }
-  } else {
-    iResult=-EINVAL;
-  }
-  return iResult;
 }
 
 int AliHLTDataBuffer::Subscribe(const AliHLTComponent* pConsumer, AliHLTComponentBlockDataList& blockDescList)
@@ -401,9 +375,7 @@ int AliHLTDataBuffer::SetSegments(AliHLTUInt8_t* pTgt, AliHLTComponentBlockData*
          }
        }
        // to be enabled if unit test is ready
-#ifdef USE_ALIHLTRAWPAGE
        iResult=SetRawBufferDataSize(fpBuffer, maxSize);        
-#endif //USE_ALIHLTRAWPAGE
       } else {
        HLTError("this data buffer (%p) does not match the internal data buffer %p of raw buffer %p", pTgt, fpBuffer->GetPointer(), fpBuffer);
        iResult=-EINVAL;
@@ -426,28 +398,28 @@ int AliHLTDataBuffer::IsEmpty()
   return iResult;
 }
 
-int AliHLTDataBuffer::GetNofSegments()
+int AliHLTDataBuffer::GetNofSegments() const
 {
   // see header file for function documentation
   int iResult=fSegments.size() + fForwardedSegments.size();
   return iResult;
 }
 
-int AliHLTDataBuffer::GetNofConsumers()
+int AliHLTDataBuffer::GetNofConsumers() const
 {
   // see header file for function documentation
   int iResult=fConsumers.size() + GetNofActiveConsumers() + fReleasedConsumers.size();
   return iResult;
 }
 
-int AliHLTDataBuffer::GetNofPendingConsumers()
+int AliHLTDataBuffer::GetNofPendingConsumers() const
 {
   // see header file for function documentation
   int iResult=fConsumers.size();
   return iResult;
 }
 
-int AliHLTDataBuffer::GetNofActiveConsumers()
+int AliHLTDataBuffer::GetNofActiveConsumers() const
 {
   // see header file for function documentation
   int iResult=fActiveConsumers.size();
@@ -459,43 +431,10 @@ AliHLTDataBuffer::AliHLTRawBuffer* AliHLTDataBuffer::CreateRawBuffer(AliHLTUInt3
   // see header file for function documentation
   AliHLTRawBuffer* pRawBuffer=NULL;
   unsigned int reqSize=size+fgkSafetyPatternSize;
-#ifndef USE_ALIHLTRAWPAGE
-  AliHLTRawBufferPList::iterator buffer=fgFreeBuffers.begin();
-  while (buffer!=fgFreeBuffers.end() && pRawBuffer==NULL) {
-    if ((*buffer)->CheckSize(reqSize)) {
-      // assign this element
-      pRawBuffer=*buffer;
-      pRawBuffer->UseBuffer(size);
-      fgFreeBuffers.erase(buffer);
-      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());
-      fgActiveBuffers.push_back(pRawBuffer);
-      break;
-    }
-    buffer++;
-  }
-  if (pRawBuffer==NULL) {
-    // no buffer found, create a new one
-    pRawBuffer=new AliHLTRawBuffer(reqSize);
-    if (pRawBuffer) {
-      if (pRawBuffer->GetPointer()) {
-       pRawBuffer->UseBuffer(size);
-       fgActiveBuffers.push_back(pRawBuffer);
-       fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "new raw buffer %p of size %d created (container %p)", pRawBuffer->GetPointer(), pRawBuffer->GetTotalSize(), pRawBuffer);
-      } else {
-       delete pRawBuffer;
-       pRawBuffer=NULL;
-       fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
-      } 
-    } else {
-      fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "memory allocation failed");
-    }
-  }
-#else
   pRawBuffer=AliHLTDataBuffer::AliHLTRawPage::GlobalAlloc(reqSize);
   if (pRawBuffer) {
     pRawBuffer->UseBuffer(size);
   }
-#endif
   if (pRawBuffer!=NULL && fgkSafetyPatternSize>0) {
     //fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::CreateRawBuffer", "data buffer handling", "writing safety pattern to %p offset %d", (*buffer)->GetPointer(), (*buffer)->GetUsedSize());
     pRawBuffer->WritePattern(fgkSafetyPattern, fgkSafetyPatternSize);
@@ -518,7 +457,6 @@ int AliHLTDataBuffer::SetRawBufferDataSize(AliHLTRawBuffer* pBuffer, AliHLTUInt3
     }
   }
   // shrink the buffer and write new pattern at the end
-#ifdef USE_ALIHLTRAWPAGE
   AliHLTDataBuffer::AliHLTRawPage* rawpage=AliHLTDataBuffer::AliHLTRawPage::FindPage(pBuffer);
   if (rawpage) {
     pBuffer->UseBuffer(size);
@@ -532,9 +470,6 @@ int AliHLTDataBuffer::SetRawBufferDataSize(AliHLTRawBuffer* pBuffer, AliHLTUInt3
     fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::SetRawBufferDataSize", "data buffer handling", "can not find raw page for buffer %p", pBuffer);
     iResult=-ENOENT;
   }
-#else //!USE_ALIHLTRAWPAGE
-  pBuffer->UseBuffer(size);
-#endif //USE_ALIHLTRAWPAGE
   if (fgkSafetyPatternSize>0) {
     pBuffer->WritePattern(fgkSafetyPattern, fgkSafetyPatternSize);
   }
@@ -546,16 +481,8 @@ int AliHLTDataBuffer::ReleaseRawBuffer(AliHLTRawBuffer* pBuffer)
   // see header file for function documentation
   int iResult=0;
   if (pBuffer) {
-#ifdef USE_ALIHLTRAWPAGE
     AliHLTDataBuffer::AliHLTRawPage* rawpage=AliHLTDataBuffer::AliHLTRawPage::FindPage(pBuffer);
     if (rawpage)
-#else //!USE_ALIHLTRAWPAGE
-    AliHLTRawBufferPList::iterator buffer=fgActiveBuffers.begin();
-    while (buffer!=fgActiveBuffers.end() && (*buffer)!=pBuffer) {
-      buffer++;
-    }
-    if (buffer!=fgActiveBuffers.end())
-#endif //USE_ALIHLTRAWPAGE
     {
       if (fgkSafetyPatternSize>0) {
        //fgLogging.Logging(kHLTLogDebug, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "comparing safety pattern at %p offset %d", pBuffer->GetPointer(), reinterpret_cast<AliHLTUInt32_t>(pBuffer));
@@ -564,21 +491,12 @@ int AliHLTDataBuffer::ReleaseRawBuffer(AliHLTRawBuffer* pBuffer)
        }
       }
       pBuffer->Reset();
-#ifdef USE_ALIHLTRAWPAGE
       if (rawpage->Free(pBuffer)==0) {
       } else {
        fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "failed to release raw buffer %p", pBuffer);
       }
-#else //!USE_ALIHLTRAWPAGE
-      fgFreeBuffers.push_back(pBuffer);
-      fgActiveBuffers.erase(buffer);
-#endif //USE_ALIHLTRAWPAGE
     } else {
-#ifdef USE_ALIHLTRAWPAGE
       fgLogging.Logging(kHLTLogError, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "can not find raw page for buffer %p", pBuffer);
-#else //!USE_ALIHLTRAWPAGE
-      fgLogging.Logging(kHLTLogWarning, "AliHLTDataBuffer::ReleaseRawBuffer", "data buffer handling", "can not find raw buffer container %p in the list of active containers", pBuffer);
-#endif //USE_ALIHLTRAWPAGE
       iResult=-ENOENT;
     }
   } else {
@@ -623,7 +541,6 @@ int AliHLTDataBuffer::PrintStatistics()
 {
   // see header file for function documentation
   int iResult=0;
-#ifdef USE_ALIHLTRAWPAGE
   int nofPages=0;
   AliHLTUInt32_t totalSize=0;
   for (AliHLTDataBuffer::AliHLTRawPage* rawpage=AliHLTDataBuffer::AliHLTRawPage::NextPage(NULL);
@@ -631,22 +548,11 @@ int AliHLTDataBuffer::PrintStatistics()
        rawpage=AliHLTDataBuffer::AliHLTRawPage::NextPage(rawpage)) {
     nofPages++;
     totalSize+=rawpage->Size();
+    if (fgLogging.CheckFilter(kHLTLogDebug)) rawpage->Print("");
   }
   //if (rawpage) rawpage->Print("global");
   fgLogging.Logging(kHLTLogInfo, "AliHLTDataBuffer::PrintStatistics", "data buffer handling", "total number of memory pages: %d   total size %d", nofPages, totalSize);
 
-#else //! USE_ALIHLTRAWPAGE
-  int iFree=0;
-  int iActive=0;
-  AliHLTRawBufferPList::iterator buffer;;
-  for (buffer=fgFreeBuffers.begin(); buffer!=fgFreeBuffers.end(); buffer++) {
-    iFree+=(*buffer)->GetTotalSize();
-  }
-  for (buffer=fgActiveBuffers.begin(); buffer!=fgActiveBuffers.end(); buffer++) {
-    iActive+=(*buffer)->GetTotalSize();
-  }
-  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);
-#endif // USE_ALIHLTRAWPAGE
   return iResult;
 }
 
@@ -842,6 +748,7 @@ AliHLTDataBuffer::AliHLTRawBuffer::AliHLTRawBuffer(AliHLTUInt32_t size, AliHLTUI
 
 AliHLTDataBuffer::AliHLTRawBuffer::~AliHLTRawBuffer()
 {
+  // see header file for class documentation
   if (fExternalPtr==NULL && fPtr) {
     free(fPtr);
   }
@@ -907,10 +814,11 @@ int AliHLTDataBuffer::AliHLTRawBuffer::operator>(const AliHLTRawBuffer& op) cons
 AliHLTUInt8_t* AliHLTDataBuffer::AliHLTRawBuffer::UseBuffer(AliHLTUInt32_t size)
 {
   // mark a portion of the buffer as used
-  if (size>0 && fTotalSize>=size) {
+  if (fTotalSize>=size) {
     fSize=size;
     fLastEventCount=AliHLTDataBuffer::fgEventCount;
-    return fPtr;
+    // only return pointer if there is a portion of the buffer used
+    if (size>0) return fPtr;
   }
   return NULL;
 }
@@ -920,15 +828,13 @@ AliHLTDataBuffer::AliHLTRawBuffer* AliHLTDataBuffer::AliHLTRawBuffer::Split(AliH
   // split a buffer at specified size
   // only possible for buffers with external memory
   if (fTotalSize>size && 
-      (fSize==0 || fSize<=size) &&
+      (fSize==0 || fSize<=size) && // used size must fit into the first part
       fExternalPtr!=NULL) {
     AliHLTRawBuffer* part2=new AliHLTRawBuffer(fTotalSize-size, fPtr+size);
     if (part2) {
       fTotalSize=size;
     }
     return part2;
-  } else {
-    cout << "can not split fTotalSize=" << fTotalSize << "  fSize=" << fSize << "  at size=" << size << endl; 
   }
   return NULL;
 }
@@ -1019,7 +925,7 @@ int AliHLTDataBuffer::AliHLTRawBuffer::Merge(const AliHLTDataBuffer::AliHLTRawBu
   return -EINVAL;
 }
 
-void AliHLTDataBuffer::AliHLTRawBuffer::Print(const char* option)
+void AliHLTDataBuffer::AliHLTRawBuffer::Print(const char* option) const
 {
   /// print buffer information
   if (strcmp(option, "min")!=0) {
@@ -1184,7 +1090,7 @@ int AliHLTDataBuffer::AliHLTRawPage::Free(AliHLTRawBuffer* pBuffer)
   return 1;
 }
 
-int AliHLTDataBuffer::AliHLTRawPage::SetSize(AliHLTDataBuffer::AliHLTRawBuffer* pBuffer, AliHLTUInt32_t size)
+int AliHLTDataBuffer::AliHLTRawPage::SetSize(const AliHLTDataBuffer::AliHLTRawBuffer* pBuffer, AliHLTUInt32_t size)
 {
   /// set the size of a raw buffer and release the remaining part
   int iResult=0;
@@ -1211,7 +1117,7 @@ int AliHLTDataBuffer::AliHLTRawPage::SetSize(AliHLTDataBuffer::AliHLTRawBuffer*
   return 1;
 }
 
-bool AliHLTDataBuffer::AliHLTRawPage::HasBuffer(AliHLTDataBuffer::AliHLTRawBuffer* pBuffer)
+bool AliHLTDataBuffer::AliHLTRawPage::HasBuffer(const AliHLTDataBuffer::AliHLTRawBuffer* pBuffer)
 {
   /// check if the buffer is in this page
   for (AliHLTRawBufferPList::iterator iter=fUsedBuffers.begin();
@@ -1266,7 +1172,7 @@ void AliHLTDataBuffer::AliHLTRawPage::Print(const char* option)
 
 vector<AliHLTDataBuffer::AliHLTRawPage*> AliHLTDataBuffer::AliHLTRawPage::fgGlobalPages;
 
-AliHLTUInt32_t AliHLTDataBuffer::AliHLTRawPage::fgGlobalPageSize=1024*1024*10;
+AliHLTUInt32_t AliHLTDataBuffer::AliHLTRawPage::fgGlobalPageSize=30*1024*1024;
 
 AliHLTDataBuffer::AliHLTRawBuffer* AliHLTDataBuffer::AliHLTRawPage::GlobalAlloc(AliHLTUInt32_t size, int verbosity)
 {
@@ -1286,9 +1192,8 @@ AliHLTDataBuffer::AliHLTRawBuffer* AliHLTDataBuffer::AliHLTRawPage::GlobalAlloc(
   if (!rawbuffer) {
     AliHLTUInt32_t rawPageSize=fgGlobalPageSize;
     if (rawPageSize<size) {
-      if (rawPageSize*10<size ||
-         rawPageSize*10>1024*1024*1024) {
-       log.Logging(kHLTLogError, "AliHLTDataBuffer::AliHLTRawPage::GlobalAlloc", "data buffer handling", "refusing to allocate buffer of size", size);
+      if (rawPageSize*10<size) {
+       log.Logging(kHLTLogError, "AliHLTDataBuffer::AliHLTRawPage::GlobalAlloc", "data buffer handling", "refusing to allocate buffer of size %d", size);
        return NULL;
       }
       rawPageSize=size;
@@ -1298,6 +1203,14 @@ AliHLTDataBuffer::AliHLTRawBuffer* AliHLTDataBuffer::AliHLTRawPage::GlobalAlloc(
       log.Logging(kHLTLogError, "AliHLTDataBuffer::AliHLTRawPage::GlobalAlloc", "data buffer handling", "can not create raw page");
       return NULL;
     }
+
+    // check is there is at least one unused page which can be replaced by the newly created one
+    for (page=fgGlobalPages.begin(); page!=fgGlobalPages.end(); page++) {
+      if ((*page)->IsUsed()) continue;
+      delete *page;
+      fgGlobalPages.erase(page);
+      break; // delete only one page to be replaced by the new page
+    }
     fgGlobalPages.push_back(rawpage);
     if ((rawbuffer=rawpage->Alloc(size))!=NULL) {
       if (verbosity>1) {
@@ -1342,7 +1255,7 @@ int AliHLTDataBuffer::AliHLTRawPage::GlobalClean()
   return 0;
 }
 
-AliHLTDataBuffer::AliHLTRawPage* AliHLTDataBuffer::AliHLTRawPage::NextPage(AliHLTDataBuffer::AliHLTRawPage* prev)
+AliHLTDataBuffer::AliHLTRawPage* AliHLTDataBuffer::AliHLTRawPage::NextPage(const AliHLTDataBuffer::AliHLTRawPage* prev)
 {
   // get next global page
   vector<AliHLTDataBuffer::AliHLTRawPage*>::iterator page=fgGlobalPages.begin();
@@ -1354,3 +1267,70 @@ AliHLTDataBuffer::AliHLTRawPage* AliHLTDataBuffer::AliHLTRawPage::NextPage(AliHL
   }
   return NULL;
 }
+
+void AliHLTDataBuffer::AliHLTDataSegment::Print(const char* /*option*/) const
+{
+  // print info for data segment
+  cout << "AliHLTDataSegment " << this 
+       << " " << AliHLTComponent::DataType2Text(fDataType)
+       << " " << hex << fSpecification << dec
+       << " Ptr " << (void*)fPtr
+       << " offset " << fSegmentOffset
+       << " size " << fSegmentSize
+       << endl;
+}
+
+void AliHLTDataBuffer::AliHLTForwardedDataSegment::Print(const char* option) const
+{
+  // print info for data segment
+  cout << "AliHLTForwardeDataSegment " << this << endl;
+  cout << "    my    : "; AliHLTDataSegment::Print(option);
+  cout << "    parent: "; fParentSegment.Print(option);
+  cout << "    task  : "; 
+  if (fParentTask) fParentTask->Print("");
+  else cout << "nil" << endl;
+}
+
+void AliHLTDataBuffer::Print(const char* option) const
+{
+  // print info for data buffer
+  unsigned i=0;
+  cout << "AliHLTDataBuffer " << this << endl;
+  cout << " raw buffer " << fpBuffer << endl;
+  if (fpBuffer) {
+    cout << " ";
+    fpBuffer->Print(option);
+  }
+
+  cout << " total segments: " << GetNofSegments() << endl;
+  cout << "   data segments: " << fSegments.size() << endl;
+  for (i=0; i<fSegments.size(); i++) {
+    cout << "     ";
+    fSegments[i].Print(option);
+  }
+
+  cout << "   forwarded segments: " << fForwardedSegments.size() << endl;
+  for (i=0; i<fForwardedSegments.size(); i++) {
+    cout << "     ";
+    fForwardedSegments[i].Print(option);
+  }
+
+  cout << " consumers: " << GetNofConsumers() << endl;
+  for (i=0; i<fConsumers.size(); i++) {
+    cout << "   ";
+    fConsumers[i]->Print(option);
+  }
+
+  cout << " active consumers: " << GetNofActiveConsumers() << endl;
+  for (i=0; i<fActiveConsumers.size(); i++) {
+    cout << "   ";
+    fActiveConsumers[i]->Print(option);
+  }
+
+  cout << " released consumers: " << fReleasedConsumers.size() << endl;
+  for (i=0; i<fReleasedConsumers.size(); i++) {
+    cout << "   ";
+    fReleasedConsumers[i]->Print(option);
+  }
+
+}