]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - HLT/BASE/AliHLTComponentHandler.cxx
ALIROOT-5433 Transition to CDHv3 in HLT
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTComponentHandler.cxx
index 55d9ee6d0516a6f345fafbde524b2cd00c725680..0d833c6390e9650572cc1d70b0ae6fb8f99b31a7 100644 (file)
@@ -1,36 +1,28 @@
 // $Id$
 
-/**************************************************************************
- * This file is property of and copyright by the ALICE HLT Project        * 
- * ALICE Experiment at CERN, All rights reserved.                         *
- *                                                                        *
- * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
- *                  Timm Steinbeck <timm@kip.uni-heidelberg.de>           *
- *                  for The ALICE HLT Project.                            *
- *                                                                        *
- * Permission to use, copy, modify and distribute this software and its   *
- * documentation strictly for non-commercial purposes is hereby granted   *
- * without fee, provided that the above copyright notice appears in all   *
- * copies and that both the copyright notice and this permission notice   *
- * appear in the supporting documentation. The authors make no claims     *
- * about the suitability of this software for any purpose. It is          *
- * provided "as is" without express or implied warranty.                  *
- **************************************************************************/
-
-/** @file   AliHLTComponentHandler.cxx
-    @author Matthias Richter, Timm Steinbeck
-    @date   
-    @brief  Implementation of HLT component handler. */
-
-// see header file for class documentation
-// or
-// refer to README to build package
-// or
-// visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
-
-#if __GNUC__>= 3
-using namespace std;
-#endif
+//**************************************************************************
+//* 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>        *
+//*                  Timm Steinbeck <timm@kip.uni-heidelberg.de>           *
+//*                  for The ALICE HLT Project.                            *
+//*                                                                        *
+//* Permission to use, copy, modify and distribute this software and its   *
+//* documentation strictly for non-commercial purposes is hereby granted   *
+//* without fee, provided that the above copyright notice appears in all   *
+//* copies and that both the copyright notice and this permission notice   *
+//* appear in the supporting documentation. The authors make no claims     *
+//* about the suitability of this software for any purpose. It is          *
+//* provided "as is" without express or implied warranty.                  *
+//**************************************************************************
+
+/// @file   AliHLTComponentHandler.cxx
+/// @author Matthias Richter, Timm Steinbeck
+/// @date   
+/// @brief  Implementation of HLT component handler.
+///
+
 //#undef HAVE_DLFCN_H
 #ifdef HAVE_DLFCN_H
 #include <dlfcn.h>
@@ -38,7 +30,6 @@ using namespace std;
 //#include <Riostream.h>
 #include <TSystem.h>
 #endif //HAVE_DLFCN_H
-//#include "AliHLTStdIncludes.h"
 #include "AliHLTComponentHandler.h"
 #include "AliHLTComponent.h"
 #include "AliHLTDataTypes.h"
@@ -55,18 +46,21 @@ AliHLTComponentHandler::AliHLTComponentHandler()
   fLibraryList(),
   fEnvironment(),
   fOwnedComponents(),
-  fLibraryMode(kDynamic)
+  fLibraryMode(kDynamic),
+  fRunDesc(kAliHLTVoidRunDesc),
+  fRunType(NULL)
 {
   // see header file for class documentation
   // or
   // refer to README to build package
   // or
   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
-  memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
+  memset(&fEnvironment, 0, sizeof(AliHLTAnalysisEnvironment));
+  fEnvironment.fStructSize=sizeof(AliHLTAnalysisEnvironment);
   AddStandardComponents();
 }
 
-AliHLTComponentHandler::AliHLTComponentHandler(AliHLTComponentEnvironment* pEnv)
+AliHLTComponentHandler::AliHLTComponentHandler(AliHLTAnalysisEnvironment* pEnv)
   :
   AliHLTLogging(),
   fComponentList(),
@@ -74,11 +68,13 @@ AliHLTComponentHandler::AliHLTComponentHandler(AliHLTComponentEnvironment* pEnv)
   fLibraryList(),
   fEnvironment(),
   fOwnedComponents(),
-  fLibraryMode(kDynamic)
+  fLibraryMode(kDynamic),
+  fRunDesc(kAliHLTVoidRunDesc),
+  fRunType(NULL)
 {
-  // see header file for class documentation
+  // constructor with environment
   if (pEnv) {
-    memcpy(&fEnvironment, pEnv, sizeof(AliHLTComponentEnvironment));
+    memcpy(&fEnvironment, pEnv, sizeof(AliHLTAnalysisEnvironment));
     if (pEnv->fLoggingFunc) {
       // the AliHLTLogging::Init method also sets the stream output
       // and notification handler to AliLog. This should only be done
@@ -87,7 +83,8 @@ AliHLTComponentHandler::AliHLTComponentHandler(AliHLTComponentEnvironment* pEnv)
       AliHLTLogging::Init(pEnv->fLoggingFunc);
     }
   }  else {
-    memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
+    memset(&fEnvironment, 0, sizeof(AliHLTAnalysisEnvironment));
+    fEnvironment.fStructSize=sizeof(AliHLTAnalysisEnvironment);
   }
   //#ifndef __DEBUG
   //SetLocalLoggingLevel(kHLTLogError);
@@ -100,9 +97,11 @@ AliHLTComponentHandler::AliHLTComponentHandler(AliHLTComponentEnvironment* pEnv)
 
 AliHLTComponentHandler::~AliHLTComponentHandler()
 {
-  // see header file for class documentation
+  // destructor
   DeleteOwnedComponents();
   UnloadLibraries();
+  if (fRunType) delete [] fRunType;
+  fRunType=NULL;
 }
 
 AliHLTComponentHandler* AliHLTComponentHandler::fgpInstance=NULL;
@@ -110,7 +109,7 @@ int AliHLTComponentHandler::fgNofInstances=0;
 
 AliHLTComponentHandler* AliHLTComponentHandler::CreateHandler()
 {
-  // see header file for class documentation
+  // create global instance of handler
   if (!fgpInstance) fgpInstance=new AliHLTComponentHandler;
   fgNofInstances++;
   return fgpInstance;
@@ -118,23 +117,26 @@ AliHLTComponentHandler* AliHLTComponentHandler::CreateHandler()
 
 int AliHLTComponentHandler::Destroy()
 {
-  // see header file for class documentation
+  // destroy/delete 'this', checks if 'this' pointer is the global instance,
+  // reduce the instance counter and delete if there are no instances left
+  // IMPORTANT: the object must be considered self-destroyed after the function
   int nofInstances=0;
   if (fgpInstance==this) {
-    nofInstances=fgNofInstances--;
+    nofInstances=--fgNofInstances;
   }
+  if (fgNofInstances==0) fgpInstance=NULL;
   if (nofInstances==0) delete this;
   return nofInstances;
 }
 
 int AliHLTComponentHandler::AnnounceVersion()
 {
-  // see header file for class documentation
+  // printout for version
   int iResult=0;
 #ifdef PACKAGE_STRING
-  void HLTbaseCompileInfo( char*& date, char*& time);
-  char* date="";
-  char* time="";
+  extern void HLTbaseCompileInfo( const char*& date, const char*& time);
+  const char* date="";
+  const char* time="";
   HLTbaseCompileInfo(date, time);
   if (!date) date="unknown";
   if (!time) time="unknown";
@@ -147,7 +149,7 @@ int AliHLTComponentHandler::AnnounceVersion()
 
 Int_t AliHLTComponentHandler::AddComponent(AliHLTComponent* pSample)
 {
-  // see header file for class documentation
+  // add and register a component, handler becomes owner
   Int_t iResult=0;
   if (pSample==NULL) return -EINVAL;
   if ((iResult=RegisterComponent(pSample))>=0) {
@@ -159,7 +161,7 @@ Int_t AliHLTComponentHandler::AddComponent(AliHLTComponent* pSample)
 
 Int_t AliHLTComponentHandler::RegisterComponent(AliHLTComponent* pSample)
 {
-  // see header file for class documentation
+  // register a component, handler creates clone of sample
   Int_t iResult=0;
   if (pSample) {
     if (FindComponent(pSample->GetComponentID())==NULL) {
@@ -180,7 +182,7 @@ Int_t AliHLTComponentHandler::RegisterComponent(AliHLTComponent* pSample)
 
 int AliHLTComponentHandler::DeregisterComponent( const char* componentID )
 {
-  // see header file for class documentation
+  // deregister component
 
   int iResult=0;
   if (componentID) {
@@ -193,7 +195,7 @@ int AliHLTComponentHandler::DeregisterComponent( const char* componentID )
 
 Int_t AliHLTComponentHandler::ScheduleRegister(AliHLTComponent* pSample)
 {
-  // see header file for class documentation
+  // schedule registration
   Int_t iResult=0;
   if (pSample) {
     fScheduleList.push_back(pSample);
@@ -203,9 +205,24 @@ Int_t AliHLTComponentHandler::ScheduleRegister(AliHLTComponent* pSample)
   return iResult;
 }
 
-int AliHLTComponentHandler::CreateComponent(const char* componentID, void* pEnvParam, int argc, const char** argv, AliHLTComponent*& component, const char* cdbPath )
+int AliHLTComponentHandler::CreateComponent(const char* componentID, void* pEnvParam, int argc, const char** argv, AliHLTComponent*& component)
 {
-  // see header file for class documentation
+  // create a component
+  int iResult=CreateComponent(componentID, component);
+  if (iResult>=0 && component) {
+       HLTDebug("component \"%s\" created (%p)", componentID, component);
+       if ((iResult=component->Init(&fEnvironment, pEnvParam, argc, argv))!=0) {
+         HLTError("Initialization of component \"%s\" failed with error %d", componentID, iResult);
+         delete component;
+         component=NULL;
+       }
+  }
+  return iResult;
+}
+
+int AliHLTComponentHandler::CreateComponent(const char* componentID, AliHLTComponent*& component )
+{
+  // create a component
   int iResult=0;
   if (componentID) {
     AliHLTComponent* pSample=FindComponent(componentID);
@@ -213,12 +230,6 @@ int AliHLTComponentHandler::CreateComponent(const char* componentID, void* pEnvP
       component=pSample->Spawn();
       if (component) {
        HLTDebug("component \"%s\" created (%p)", componentID, component);
-       component->InitCDB(cdbPath, this);
-       if ((iResult=component->Init(&fEnvironment, pEnvParam, argc, argv))!=0) {
-         HLTError("Initialization of component \"%s\" failed with error %d", componentID, iResult);
-         delete component;
-         component=NULL;
-       }
       } else {
        HLTError("can not spawn component \"%s\"", componentID);
        iResult=-ENOENT;
@@ -235,7 +246,7 @@ int AliHLTComponentHandler::CreateComponent(const char* componentID, void* pEnvP
 
 Int_t AliHLTComponentHandler::FindComponentIndex(const char* componentID)
 {
-  // see header file for class documentation
+  // find component by ID in the list and return index
   Int_t iResult=0;
   if (componentID) {
     AliHLTComponentPList::iterator element=fComponentList.begin();
@@ -255,7 +266,7 @@ Int_t AliHLTComponentHandler::FindComponentIndex(const char* componentID)
 
 AliHLTComponent* AliHLTComponentHandler::FindComponent(const char* componentID)
 {
-  // see header file for class documentation
+  // find component sample by ID
   AliHLTComponent* pSample=NULL;
   Int_t index=FindComponentIndex(componentID);
   if (index>=0) {
@@ -266,7 +277,7 @@ AliHLTComponent* AliHLTComponentHandler::FindComponent(const char* componentID)
 
 Int_t AliHLTComponentHandler::InsertComponent(AliHLTComponent* pSample)
 {
-  // see header file for class documentation
+  // insert a component sample in the list
   Int_t iResult=0;
   if (pSample!=NULL) {
     fComponentList.push_back(pSample);
@@ -278,7 +289,8 @@ Int_t AliHLTComponentHandler::InsertComponent(AliHLTComponent* pSample)
 
 void AliHLTComponentHandler::List() 
 {
-  // see header file for class documentation
+  // print list content
+  // TODO: implement Print()
   AliHLTComponentPList::iterator element=fComponentList.begin();
   int index=0;
   while (element!=fComponentList.end()) {
@@ -288,7 +300,7 @@ void AliHLTComponentHandler::List()
 
 int AliHLTComponentHandler::HasOutputData( const char* componentID)
 {
-  // see header file for class documentation
+  // check if a component has output data
   int iResult=0;
   AliHLTComponent* pSample=FindComponent(componentID);
   if (pSample) {
@@ -301,11 +313,13 @@ int AliHLTComponentHandler::HasOutputData( const char* componentID)
   return iResult;
 }
 
-void AliHLTComponentHandler::SetEnvironment(AliHLTComponentEnvironment* pEnv) 
+void AliHLTComponentHandler::SetEnvironment(AliHLTAnalysisEnvironment* pEnv) 
 {
-  // see header file for class documentation
+  // set global environment
   if (pEnv) {
-    memcpy(&fEnvironment, pEnv, sizeof(AliHLTComponentEnvironment));
+    memset(&fEnvironment, 0, sizeof(AliHLTAnalysisEnvironment));
+    memcpy(&fEnvironment, pEnv, pEnv->fStructSize<sizeof(AliHLTAnalysisEnvironment)?pEnv->fStructSize:sizeof(AliHLTAnalysisEnvironment));
+    fEnvironment.fStructSize=sizeof(AliHLTAnalysisEnvironment);
     if (fEnvironment.fLoggingFunc) {
       // the AliHLTLogging::Init method also sets the stream output
       // and notification handler to AliLog. This should only be done
@@ -318,7 +332,7 @@ void AliHLTComponentHandler::SetEnvironment(AliHLTComponentEnvironment* pEnv)
 
 AliHLTComponentHandler::TLibraryMode AliHLTComponentHandler::SetLibraryMode(TLibraryMode mode)
 {
-  // see header file for class documentation
+  // set library mode
   TLibraryMode old=fLibraryMode;
   fLibraryMode=mode;
   return old;
@@ -326,11 +340,15 @@ AliHLTComponentHandler::TLibraryMode AliHLTComponentHandler::SetLibraryMode(TLib
 
 int AliHLTComponentHandler::LoadLibrary( const char* libraryPath, int bActivateAgents)
 {
-  // see header file for class documentation
+  // load a library
   int iResult=0;
   if (libraryPath) {
-    // first activate all agents which are already loaded
-    if (bActivateAgents) ActivateAgents();
+    TString libName=libraryPath;
+    int slash=-1;
+    while ((slash=libName.Index("/"))>=0) {
+      libName=libName.Remove(0, slash+1);
+    }
+    libName.ReplaceAll(".so","");
 
     // set the global component handler for static component registration
     AliHLTComponent::SetGlobalComponentHandler(this);
@@ -387,19 +405,18 @@ int AliHLTComponentHandler::LoadLibrary( const char* libraryPath, int bActivateA
       hLib.fMode=fLibraryMode;
       fLibraryList.insert(fLibraryList.begin(), hLib);
       if (!phSearch) {
-      HLTImportant("library %s loaded (%s%s)", libraryPath, hLib.fMode==kStatic?"persistent, ":"", loadtype);
-      typedef void (*CompileInfo)( char*& date, char*& time);
+      typedef void (*CompileInfo)(const char*& date, const char*& time);
       CompileInfo fctInfo=(CompileInfo)FindSymbol(libraryPath, "CompileInfo");
+      const char* date="";
+      const char* time="";
+      const char* buildOn="";
       if (fctInfo) {
-       char* date="";
-       char* time="";
+       buildOn=" build on ";
        (*fctInfo)(date, time);
        if (!date) date="unknown";
        if (!time) time="unknown";
-       HLTImportant("build on %s (%s)", date, time);
-      } else {
-       HLTImportant("no build info available (possible AliRoot embedded build)");
       }
+      HLTImportant("using %s plugin%s%s %s (%s%s)", libraryPath, buildOn, date, time, hLib.fMode==kStatic?"persistent, ":"", loadtype);
       }
 
       // static registration of components when library is loaded
@@ -421,7 +438,7 @@ int AliHLTComponentHandler::LoadLibrary( const char* libraryPath, int bActivateA
     if (iResult>=0) {
       // alternative dynamic registration by library agents
       // !!! has to be done after UnsetGlobalComponentHandler
-      if (bActivateAgents) ActivateAgents();
+      if (bActivateAgents) ActivateAgents(libName.Data());
     }
 
   } else {
@@ -432,7 +449,7 @@ int AliHLTComponentHandler::LoadLibrary( const char* libraryPath, int bActivateA
 
 int AliHLTComponentHandler::UnloadLibrary( const char* libraryPath )
 {
-  // see header file for class documentation
+  // unload a library
   int iResult=0;
   if (libraryPath) {
     vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
@@ -453,7 +470,7 @@ int AliHLTComponentHandler::UnloadLibrary( const char* libraryPath )
 
 int AliHLTComponentHandler::UnloadLibrary(AliHLTComponentHandler::AliHLTLibHandle &handle)
 {
-  // see header file for class documentation
+  // unload a library
   int iResult=0;
   fgAliLoggingFunc=NULL;
   TString* pName=reinterpret_cast<TString*>(handle.fName);
@@ -517,7 +534,7 @@ int AliHLTComponentHandler::UnloadLibrary(AliHLTComponentHandler::AliHLTLibHandl
 
 int AliHLTComponentHandler::UnloadLibraries()
 {
-  // see header file for class documentation
+  // unload all libraries
   int iResult=0;
   vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
   while (element!=fLibraryList.end()) {
@@ -528,24 +545,24 @@ int AliHLTComponentHandler::UnloadLibraries()
   return iResult;
 }
 
-void* AliHLTComponentHandler::FindSymbol(const char* library, const char* symbol)
+AliHLTfctVoid AliHLTComponentHandler::FindSymbol(const char* library, const char* symbol)
 {
-  // see header file for class documentation
+  // find symbol in library
   AliHLTLibHandle* hLib=FindLibrary(library);
   if (hLib==NULL) return NULL;
-  void* pFunc=NULL;
+  void (*pFunc)()=NULL;
 #ifdef HAVE_DLFCN_H
-  pFunc=dlsym(hLib->fHandle, symbol);
+  pFunc=(void (*)())dlsym(hLib->fHandle, symbol);
 #else
   TString* name=reinterpret_cast<TString*>(hLib->fName);
-  pFunc=gSystem->DynFindSymbol(name->Data(), symbol);
+  pFunc=(void (*)())gSystem->DynFindSymbol(name->Data(), symbol);
 #endif
   return pFunc;
 }
 
 AliHLTComponentHandler::AliHLTLibHandle* AliHLTComponentHandler::FindLibrary(const char* library)
 {
-  // see header file for class documentation
+  // find a library by name
   AliHLTLibHandle* hLib=NULL;
   vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
   while (element!=fLibraryList.end()) {
@@ -561,7 +578,7 @@ AliHLTComponentHandler::AliHLTLibHandle* AliHLTComponentHandler::FindLibrary(con
 
 int AliHLTComponentHandler::AddStandardComponents()
 {
-  // see header file for class documentation
+  // TODO: not quite clear what was the meaning behind this function
   int iResult=0;
   AliHLTComponent::SetGlobalComponentHandler(this);
   AliHLTComponent::UnsetGlobalComponentHandler();
@@ -571,7 +588,7 @@ int AliHLTComponentHandler::AddStandardComponents()
 
 int AliHLTComponentHandler::RegisterScheduledComponents()
 {
-  // see header file for class documentation
+  // register all scheduled components
   int iResult=0;
   AliHLTComponentPList::iterator element=fScheduleList.begin();
   int iLocalResult=0;
@@ -584,33 +601,47 @@ int AliHLTComponentHandler::RegisterScheduledComponents()
   return iResult;
 }
 
-int AliHLTComponentHandler::ActivateAgents(const AliHLTModuleAgent** blackList, int size)
+int AliHLTComponentHandler::ActivateAgents(const char* library, const char* blackList)
 {
-  // see header file for class documentation
-  int iResult=0;
-  AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent();
-  while (pAgent && iResult>=0) {
-    if (blackList) {
-      int i=0;
-      for (; i<size; i++) {
-       if (blackList[i]==pAgent) break;
+  // activate module agents
+  vector<AliHLTModuleAgent*> agents;
+  for (AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent(); 
+       pAgent!=NULL;
+       pAgent=AliHLTModuleAgent::GetNextAgent()) {
+
+    // check if we found the agent for the specified library
+    if (library) {
+      TString check="libAliHLT"; check+=pAgent->GetModuleId();
+      if (check.CompareTo(library)==0) {
+       agents.clear();
+       agents.push_back(pAgent);
+       break;
       }
-      if (i<size) {
-       // this agent was in the list
-       pAgent=AliHLTModuleAgent::GetNextAgent();
-       continue;
+    }
+
+    // check if the current agent is in the black list
+    if (blackList) {
+      const char* found=strstr(blackList, pAgent->GetModuleId());
+      if (found) {
+       found+=strlen(pAgent->GetModuleId());
+       // skip this agent as it is in the blacklist
+       if (*found==0 or *found==' ') continue;
       }
     }
+    agents.push_back(pAgent);
+  }
 
-    pAgent->ActivateComponentHandler(this);
-    pAgent=AliHLTModuleAgent::GetNextAgent();
+  for (vector<AliHLTModuleAgent*>::iterator element=agents.begin();
+       element!=agents.end(); element++) {
+    (*element)->ActivateComponentHandler(this);
   }
-  return iResult;
+
+  return agents.size();
 }
 
 int AliHLTComponentHandler::DeleteOwnedComponents()
 {
-  // see header file for class documentation
+  // delete all component samples owned by the handler
   int iResult=0;
   AliHLTComponentPList::iterator element=fOwnedComponents.begin();
   while (element!=fOwnedComponents.end()) {
@@ -631,3 +662,23 @@ int AliHLTComponentHandler::DeleteOwnedComponents()
   }
   return iResult;
 }
+
+int AliHLTComponentHandler::SetRunDescription(const AliHLTRunDesc* desc, const char* runType)
+{
+  // set global run description
+  if (!desc) return -EINVAL;
+  if (desc->fStructSize!=sizeof(AliHLTRunDesc)) {
+    HLTError("invalid size of RunDesc struct (%ul)", desc->fStructSize);
+    return -EINVAL;
+  }
+
+  memcpy(&fRunDesc, desc, sizeof(AliHLTRunDesc));
+  if (runType) {
+    if (fRunType) delete [] fRunType;
+    fRunType=new char[sizeof(runType)+1];
+    if (fRunType) {
+      strcpy(fRunType, runType);
+    }
+  }
+  return 0;
+}