3 //**************************************************************************
4 //* This file is property of and copyright by the *
5 //* ALICE Experiment at CERN, All rights reserved. *
7 //* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 //* Timm Steinbeck <timm@kip.uni-heidelberg.de> *
9 //* for The ALICE HLT Project. *
11 //* Permission to use, copy, modify and distribute this software and its *
12 //* documentation strictly for non-commercial purposes is hereby granted *
13 //* without fee, provided that the above copyright notice appears in all *
14 //* copies and that both the copyright notice and this permission notice *
15 //* appear in the supporting documentation. The authors make no claims *
16 //* about the suitability of this software for any purpose. It is *
17 //* provided "as is" without express or implied warranty. *
18 //**************************************************************************
20 /// @file AliHLTComponentHandler.cxx
21 /// @author Matthias Richter, Timm Steinbeck
23 /// @brief Implementation of HLT component handler.
30 //#include <Riostream.h>
33 #include "AliHLTComponentHandler.h"
34 #include "AliHLTComponent.h"
35 #include "AliHLTDataTypes.h"
36 #include "AliHLTModuleAgent.h"
39 /** ROOT macro for the implementation of ROOT specific class methods */
40 ClassImp(AliHLTComponentHandler)
42 AliHLTComponentHandler::AliHLTComponentHandler()
49 fLibraryMode(kDynamic),
50 fRunDesc(kAliHLTVoidRunDesc),
53 // see header file for class documentation
55 // refer to README to build package
57 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
58 memset(&fEnvironment, 0, sizeof(AliHLTAnalysisEnvironment));
59 fEnvironment.fStructSize=sizeof(AliHLTAnalysisEnvironment);
60 AddStandardComponents();
63 AliHLTComponentHandler::AliHLTComponentHandler(AliHLTAnalysisEnvironment* pEnv)
71 fLibraryMode(kDynamic),
72 fRunDesc(kAliHLTVoidRunDesc),
75 // constructor with environment
77 memcpy(&fEnvironment, pEnv, sizeof(AliHLTAnalysisEnvironment));
78 if (pEnv->fLoggingFunc) {
79 // the AliHLTLogging::Init method also sets the stream output
80 // and notification handler to AliLog. This should only be done
81 // if the logging environment contains a logging function
83 AliHLTLogging::Init(pEnv->fLoggingFunc);
86 memset(&fEnvironment, 0, sizeof(AliHLTAnalysisEnvironment));
87 fEnvironment.fStructSize=sizeof(AliHLTAnalysisEnvironment);
90 //SetLocalLoggingLevel(kHLTLogError);
92 //SetLocalLoggingLevel(kHLTLogInfo);
95 AddStandardComponents();
98 AliHLTComponentHandler::~AliHLTComponentHandler()
101 DeleteOwnedComponents();
103 if (fRunType) delete [] fRunType;
107 AliHLTComponentHandler* AliHLTComponentHandler::fgpInstance=NULL;
108 int AliHLTComponentHandler::fgNofInstances=0;
110 AliHLTComponentHandler* AliHLTComponentHandler::CreateHandler()
112 // create global instance of handler
113 if (!fgpInstance) fgpInstance=new AliHLTComponentHandler;
118 int AliHLTComponentHandler::Destroy()
120 // destroy/delete 'this', checks if 'this' pointer is the global instance,
121 // reduce the instance counter and delete if there are no instances left
122 // IMPORTANT: the object must be considered self-destroyed after the function
124 if (fgpInstance==this) {
125 nofInstances=--fgNofInstances;
127 if (fgNofInstances==0) fgpInstance=NULL;
128 if (nofInstances==0) delete this;
132 int AliHLTComponentHandler::AnnounceVersion()
134 // printout for version
136 #ifdef PACKAGE_STRING
137 extern void HLTbaseCompileInfo( const char*& date, const char*& time);
140 HLTbaseCompileInfo(date, time);
141 if (!date) date="unknown";
142 if (!time) time="unknown";
143 HLTImportant("%s build on %s (%s)", PACKAGE_STRING, date, time);
145 HLTImportant("ALICE High Level Trigger build on %s (%s) (embedded AliRoot build)", __DATE__, __TIME__);
150 Int_t AliHLTComponentHandler::AddComponent(AliHLTComponent* pSample)
152 // add and register a component, handler becomes owner
154 if (pSample==NULL) return -EINVAL;
155 if ((iResult=RegisterComponent(pSample))>=0) {
156 //HLTDebug("sample %s (%p) managed by handler", pSample->GetComponentID(), pSample);
157 fOwnedComponents.push_back(pSample);
162 Int_t AliHLTComponentHandler::RegisterComponent(AliHLTComponent* pSample)
164 // register a component, handler creates clone of sample
167 if (FindComponent(pSample->GetComponentID())==NULL) {
168 iResult=InsertComponent(pSample);
170 HLTInfo("component %s registered", pSample->GetComponentID());
173 // component already registered
174 HLTDebug("component %s already registered, skipped", pSample->GetComponentID());
183 int AliHLTComponentHandler::DeregisterComponent( const char* componentID )
185 // deregister component
189 HLTWarning("not yet implemented, please notify the developers if you need this function");
196 Int_t AliHLTComponentHandler::ScheduleRegister(AliHLTComponent* pSample)
198 // schedule registration
201 fScheduleList.push_back(pSample);
208 int AliHLTComponentHandler::CreateComponent(const char* componentID, void* pEnvParam, int argc, const char** argv, AliHLTComponent*& component)
210 // create a component
211 int iResult=CreateComponent(componentID, component);
212 if (iResult>=0 && component) {
213 HLTDebug("component \"%s\" created (%p)", componentID, component);
214 if ((iResult=component->Init(&fEnvironment, pEnvParam, argc, argv))!=0) {
215 HLTError("Initialization of component \"%s\" failed with error %d", componentID, iResult);
223 int AliHLTComponentHandler::CreateComponent(const char* componentID, AliHLTComponent*& component )
225 // create a component
228 AliHLTComponent* pSample=FindComponent(componentID);
230 component=pSample->Spawn();
232 HLTDebug("component \"%s\" created (%p)", componentID, component);
234 HLTError("can not spawn component \"%s\"", componentID);
238 HLTWarning("can not find component \"%s\"", componentID);
247 Int_t AliHLTComponentHandler::FindComponentIndex(const char* componentID)
249 // find component by ID in the list and return index
252 AliHLTComponentPList::iterator element=fComponentList.begin();
253 while (element!=fComponentList.end() && iResult>=0) {
254 if (strcmp(componentID, (*element)->GetComponentID())==0) {
260 if (element==fComponentList.end()) iResult=-ENOENT;
267 AliHLTComponent* AliHLTComponentHandler::FindComponent(const char* componentID)
269 // find component sample by ID
270 AliHLTComponent* pSample=NULL;
271 Int_t index=FindComponentIndex(componentID);
273 pSample=(AliHLTComponent*)fComponentList.at(index);
278 Int_t AliHLTComponentHandler::InsertComponent(AliHLTComponent* pSample)
280 // insert a component sample in the list
283 fComponentList.push_back(pSample);
290 void AliHLTComponentHandler::List()
292 // print list content
293 // TODO: implement Print()
294 AliHLTComponentPList::iterator element=fComponentList.begin();
296 while (element!=fComponentList.end()) {
297 HLTInfo("%d. %s", index++, (*element++)->GetComponentID());
301 int AliHLTComponentHandler::HasOutputData( const char* componentID)
303 // check if a component has output data
305 AliHLTComponent* pSample=FindComponent(componentID);
307 AliHLTComponent::TComponentType ct=AliHLTComponent::kUnknown;
308 ct=pSample->GetComponentType();
309 iResult=(ct==AliHLTComponent::kSource || ct==AliHLTComponent::kProcessor);
316 void AliHLTComponentHandler::SetEnvironment(AliHLTAnalysisEnvironment* pEnv)
318 // set global environment
320 memset(&fEnvironment, 0, sizeof(AliHLTAnalysisEnvironment));
321 memcpy(&fEnvironment, pEnv, pEnv->fStructSize<sizeof(AliHLTAnalysisEnvironment)?pEnv->fStructSize:sizeof(AliHLTAnalysisEnvironment));
322 fEnvironment.fStructSize=sizeof(AliHLTAnalysisEnvironment);
323 if (fEnvironment.fLoggingFunc) {
324 // the AliHLTLogging::Init method also sets the stream output
325 // and notification handler to AliLog. This should only be done
326 // if the logging environment contains a logging function
328 AliHLTLogging::Init(fEnvironment.fLoggingFunc);
333 AliHLTComponentHandler::TLibraryMode AliHLTComponentHandler::SetLibraryMode(TLibraryMode mode)
336 TLibraryMode old=fLibraryMode;
341 int AliHLTComponentHandler::LoadLibrary( const char* libraryPath, int bActivateAgents)
346 TString libName=libraryPath;
348 while ((slash=libName.Index("/"))>=0) {
349 libName=libName.Remove(0, slash+1);
351 libName.ReplaceAll(".so","");
353 // set the global component handler for static component registration
354 AliHLTComponent::SetGlobalComponentHandler(this);
356 AliHLTLibHandle hLib;
357 AliHLTLibHandle* phSearch=FindLibrary(libraryPath);
358 const char* loadtype="";
360 // use interface to the dynamic linking loader
362 // exeption does not help in Root context, the Root exeption
363 // handler always catches the exeption before. Have to find out
364 // how exeptions can be used in Root
366 hLib.fHandle=dlopen(libraryPath, RTLD_NOW);
371 // error message printed further down
372 loadtype="dlopen exeption";
376 // use ROOT dynamic loader
377 // check if the library was already loaded, as Load returns
378 // 'failure' if the library was already loaded
381 int* pRootHandle=reinterpret_cast<int*>(phSearch->fHandle);
383 HLTDebug("instance %d of library %s loaded", (*pRootHandle), libraryPath);
384 hLib.fHandle=pRootHandle;
387 if (hLib.fHandle==NULL && gSystem->Load(libraryPath)>=0) {
388 int* pRootHandle=new int;
389 if (pRootHandle) *pRootHandle=1;
390 hLib.fHandle=pRootHandle;
391 //HLTDebug("library %s loaded via gSystem", libraryPath);
397 // error message printed further down
398 loadtype="gSystem exeption";
401 #endif //HAVE_DLFCN_H
402 if (hLib.fHandle!=NULL) {
403 // create TString object to store library path and use pointer as handle
404 hLib.fName=new TString(libraryPath);
405 hLib.fMode=fLibraryMode;
406 fLibraryList.insert(fLibraryList.begin(), hLib);
408 typedef void (*CompileInfo)(const char*& date, const char*& time);
409 CompileInfo fctInfo=(CompileInfo)FindSymbol(libraryPath, "CompileInfo");
412 const char* buildOn="";
414 buildOn=" build on ";
415 (*fctInfo)(date, time);
416 if (!date) date="unknown";
417 if (!time) time="unknown";
419 HLTImportant("using %s plugin%s%s %s (%s%s)", libraryPath, buildOn, date, time, hLib.fMode==kStatic?"persistent, ":"", loadtype);
422 // static registration of components when library is loaded
423 iResult=RegisterScheduledComponents();
426 HLTError("can not load library %s (%s)", libraryPath, loadtype);
428 HLTError("dlopen error: %s", dlerror());
429 #endif //HAVE_DLFCN_H
436 AliHLTComponent::UnsetGlobalComponentHandler();
439 // alternative dynamic registration by library agents
440 // !!! has to be done after UnsetGlobalComponentHandler
441 if (bActivateAgents) ActivateAgents(libName.Data());
450 int AliHLTComponentHandler::UnloadLibrary( const char* libraryPath )
455 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
456 while (element!=fLibraryList.end()) {
457 TString* pName=reinterpret_cast<TString*>((*element).fName);
458 if (pName->CompareTo(libraryPath)==0) {
459 UnloadLibrary(*element);
460 fLibraryList.erase(element);
471 int AliHLTComponentHandler::UnloadLibrary(AliHLTComponentHandler::AliHLTLibHandle &handle)
475 fgAliLoggingFunc=NULL;
476 TString* pName=reinterpret_cast<TString*>(handle.fName);
477 if (handle.fMode!=kStatic) {
479 // exeption does not help in Root context, the Root exeption
480 // handler always catches the exeption before. Have to find out
481 // how exeptions can be used in Root
484 dlclose(handle.fHandle);
488 HLTError("exeption caught during dlclose of library %s", pName!=NULL?pName->Data():"");
492 int* pCount=reinterpret_cast<int*>(handle.fHandle);
493 if (--(*pCount)==0) {
495 /** Matthias 26.04.2007
496 * I spent about a week to investigate a bug which seems to be in ROOT.
497 * Under certain circumstances, TSystem::Unload crashes. The crash occured
498 * for the first time, when libAliHLTUtil was loaded from AliHLTSystem right
499 * after the ComponentHandler was created. It does not occur when dlopen is
501 * It has most likely to do with the garbage collection and automatic
502 * cleanup in ROOT. The crash occurs when ROOT is terminated and before
503 * an instance of AliHLTSystem was created.
504 * root [0] AliHLTSystem gHLT
505 * It does not occur when the instance was created dynamically (but not even
507 * root [0] AliHLTSystem* gHLT=new AliHLTSystem
509 * For that reason, the libraries are not unloaded here, even though there
510 * will be memory leaks.
511 gSystem->Unload(pName->Data());
515 HLTError("missing library name, can not unload");
519 #endif //HAVE_DLFCN_H
521 HLTDebug("unload library %s", pName->Data());
523 HLTWarning("missing name for unloaded library");
535 int AliHLTComponentHandler::UnloadLibraries()
537 // unload all libraries
539 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
540 while (element!=fLibraryList.end()) {
541 UnloadLibrary(*element);
542 fLibraryList.erase(element);
543 element=fLibraryList.begin();
548 AliHLTfctVoid AliHLTComponentHandler::FindSymbol(const char* library, const char* symbol)
550 // find symbol in library
551 AliHLTLibHandle* hLib=FindLibrary(library);
552 if (hLib==NULL) return NULL;
553 void (*pFunc)()=NULL;
555 pFunc=(void (*)())dlsym(hLib->fHandle, symbol);
557 TString* name=reinterpret_cast<TString*>(hLib->fName);
558 pFunc=(void (*)())gSystem->DynFindSymbol(name->Data(), symbol);
563 AliHLTComponentHandler::AliHLTLibHandle* AliHLTComponentHandler::FindLibrary(const char* library)
565 // find a library by name
566 AliHLTLibHandle* hLib=NULL;
567 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
568 while (element!=fLibraryList.end()) {
569 TString* name=reinterpret_cast<TString*>((*element).fName);
570 if (name->CompareTo(library)==0) {
579 int AliHLTComponentHandler::AddStandardComponents()
581 // TODO: not quite clear what was the meaning behind this function
583 AliHLTComponent::SetGlobalComponentHandler(this);
584 AliHLTComponent::UnsetGlobalComponentHandler();
585 iResult=RegisterScheduledComponents();
589 int AliHLTComponentHandler::RegisterScheduledComponents()
591 // register all scheduled components
593 AliHLTComponentPList::iterator element=fScheduleList.begin();
595 while (element!=fScheduleList.end()) {
596 iLocalResult=RegisterComponent(*element);
597 if (iResult==0) iResult=iLocalResult;
598 fScheduleList.erase(element);
599 element=fScheduleList.begin();
604 int AliHLTComponentHandler::ActivateAgents(const char* library, const char* blackList)
606 // activate module agents
607 vector<AliHLTModuleAgent*> agents;
608 for (AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent();
610 pAgent=AliHLTModuleAgent::GetNextAgent()) {
612 // check if we found the agent for the specified library
614 TString check="libAliHLT"; check+=pAgent->GetModuleId();
615 if (check.CompareTo(library)==0) {
617 agents.push_back(pAgent);
622 // check if the current agent is in the black list
624 const char* found=strstr(blackList, pAgent->GetModuleId());
626 found+=strlen(pAgent->GetModuleId());
627 // skip this agent as it is in the blacklist
628 if (*found==0 or *found==' ') continue;
631 agents.push_back(pAgent);
634 for (vector<AliHLTModuleAgent*>::iterator element=agents.begin();
635 element!=agents.end(); element++) {
636 (*element)->ActivateComponentHandler(this);
639 return agents.size();
642 int AliHLTComponentHandler::DeleteOwnedComponents()
644 // delete all component samples owned by the handler
646 AliHLTComponentPList::iterator element=fOwnedComponents.begin();
647 while (element!=fOwnedComponents.end()) {
648 //DeregisterComponent((*element)->GetComponentID());
649 // exeption does not help in Root context, the Root exeption
650 // handler always catches the exeption before. Have to find out
651 // how exeptions can be used in Root
657 HLTError("delete managed sample %p", *element);
660 fOwnedComponents.erase(element);
661 element=fOwnedComponents.begin();
666 int AliHLTComponentHandler::SetRunDescription(const AliHLTRunDesc* desc, const char* runType)
668 // set global run description
669 if (!desc) return -EINVAL;
670 if (desc->fStructSize!=sizeof(AliHLTRunDesc)) {
671 HLTError("invalid size of RunDesc struct (%ul)", desc->fStructSize);
675 memcpy(&fRunDesc, desc, sizeof(AliHLTRunDesc));
677 if (fRunType) delete [] fRunType;
678 fRunType=new char[sizeof(runType)+1];
680 strcpy(fRunType, runType);