3 /**************************************************************************
4 * This file is property of and copyright by the ALICE HLT Project *
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. */
25 // see header file for class documentation
27 // refer to README to build package
29 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
38 //#include <Riostream.h>
41 //#include "AliHLTStdIncludes.h"
42 #include "AliHLTComponentHandler.h"
43 #include "AliHLTComponent.h"
44 #include "AliHLTDataTypes.h"
45 #include "AliHLTModuleAgent.h"
48 /** ROOT macro for the implementation of ROOT specific class methods */
49 ClassImp(AliHLTComponentHandler)
51 AliHLTComponentHandler::AliHLTComponentHandler()
58 fLibraryMode(kDynamic)
60 // see header file for class documentation
62 // refer to README to build package
64 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
65 memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
66 AddStandardComponents();
69 AliHLTComponentHandler::AliHLTComponentHandler(AliHLTComponentEnvironment* pEnv)
77 fLibraryMode(kDynamic)
79 // see header file for class documentation
81 memcpy(&fEnvironment, pEnv, sizeof(AliHLTComponentEnvironment));
82 if (pEnv->fLoggingFunc) {
83 // the AliHLTLogging::Init method also sets the stream output
84 // and notification handler to AliLog. This should only be done
85 // if the logging environment contains a logging function
87 AliHLTLogging::Init(pEnv->fLoggingFunc);
90 memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
93 //SetLocalLoggingLevel(kHLTLogError);
95 //SetLocalLoggingLevel(kHLTLogInfo);
98 AddStandardComponents();
101 AliHLTComponentHandler::~AliHLTComponentHandler()
103 // see header file for class documentation
104 DeleteOwnedComponents();
108 int AliHLTComponentHandler::AnnounceVersion()
110 // see header file for class documentation
112 #ifdef PACKAGE_STRING
113 void HLTbaseCompileInfo( char*& date, char*& time);
116 HLTbaseCompileInfo(date, time);
117 if (!date) date="unknown";
118 if (!time) time="unknown";
119 HLTImportant("%s build on %s (%s)", PACKAGE_STRING, date, time);
121 HLTImportant("ALICE High Level Trigger build on %s (%s) (embedded AliRoot build)", __DATE__, __TIME__);
126 Int_t AliHLTComponentHandler::AddComponent(AliHLTComponent* pSample)
128 // see header file for class documentation
130 if (pSample==NULL) return -EINVAL;
131 if ((iResult=RegisterComponent(pSample))>=0) {
132 //HLTDebug("sample %s (%p) managed by handler", pSample->GetComponentID(), pSample);
133 fOwnedComponents.push_back(pSample);
138 Int_t AliHLTComponentHandler::RegisterComponent(AliHLTComponent* pSample)
140 // see header file for class documentation
143 if (FindComponent(pSample->GetComponentID())==NULL) {
144 iResult=InsertComponent(pSample);
146 HLTInfo("component %s registered", pSample->GetComponentID());
149 // component already registered
150 HLTDebug("component %s already registered, skipped", pSample->GetComponentID());
159 int AliHLTComponentHandler::DeregisterComponent( const char* componentID )
161 // see header file for class documentation
165 HLTWarning("not yet implemented, please notify the developers if you need this function");
172 Int_t AliHLTComponentHandler::ScheduleRegister(AliHLTComponent* pSample)
174 // see header file for class documentation
177 fScheduleList.push_back(pSample);
184 int AliHLTComponentHandler::CreateComponent(const char* componentID, void* pEnvParam, int argc, const char** argv, AliHLTComponent*& component, const char* cdbPath )
186 // see header file for class documentation
189 AliHLTComponent* pSample=FindComponent(componentID);
191 component=pSample->Spawn();
193 HLTDebug("component \"%s\" created (%p)", componentID, component);
194 component->InitCDB(cdbPath, this);
195 if ((iResult=component->Init(&fEnvironment, pEnvParam, argc, argv))!=0) {
196 HLTError("Initialization of component \"%s\" failed with error %d", componentID, iResult);
201 HLTError("can not spawn component \"%s\"", componentID);
205 HLTWarning("can not find component \"%s\"", componentID);
214 Int_t AliHLTComponentHandler::FindComponentIndex(const char* componentID)
216 // see header file for class documentation
219 AliHLTComponentPList::iterator element=fComponentList.begin();
220 while (element!=fComponentList.end() && iResult>=0) {
221 if (strcmp(componentID, (*element)->GetComponentID())==0) {
227 if (element==fComponentList.end()) iResult=-ENOENT;
234 AliHLTComponent* AliHLTComponentHandler::FindComponent(const char* componentID)
236 // see header file for class documentation
237 AliHLTComponent* pSample=NULL;
238 Int_t index=FindComponentIndex(componentID);
240 pSample=(AliHLTComponent*)fComponentList.at(index);
245 Int_t AliHLTComponentHandler::InsertComponent(AliHLTComponent* pSample)
247 // see header file for class documentation
250 fComponentList.push_back(pSample);
257 void AliHLTComponentHandler::List()
259 // see header file for class documentation
260 AliHLTComponentPList::iterator element=fComponentList.begin();
262 while (element!=fComponentList.end()) {
263 HLTInfo("%d. %s", index++, (*element++)->GetComponentID());
267 int AliHLTComponentHandler::HasOutputData( const char* componentID)
269 // see header file for class documentation
271 AliHLTComponent* pSample=FindComponent(componentID);
273 AliHLTComponent::TComponentType ct=AliHLTComponent::kUnknown;
274 ct=pSample->GetComponentType();
275 iResult=(ct==AliHLTComponent::kSource || ct==AliHLTComponent::kProcessor);
282 void AliHLTComponentHandler::SetEnvironment(AliHLTComponentEnvironment* pEnv)
284 // see header file for class documentation
286 memcpy(&fEnvironment, pEnv, sizeof(AliHLTComponentEnvironment));
287 if (fEnvironment.fLoggingFunc) {
288 // the AliHLTLogging::Init method also sets the stream output
289 // and notification handler to AliLog. This should only be done
290 // if the logging environment contains a logging function
292 AliHLTLogging::Init(fEnvironment.fLoggingFunc);
297 AliHLTComponentHandler::TLibraryMode AliHLTComponentHandler::SetLibraryMode(TLibraryMode mode)
299 // see header file for class documentation
300 TLibraryMode old=fLibraryMode;
305 int AliHLTComponentHandler::LoadLibrary( const char* libraryPath, int bActivateAgents)
307 // see header file for class documentation
310 // first activate all agents which are already loaded
311 if (bActivateAgents) ActivateAgents();
313 // set the global component handler for static component registration
314 AliHLTComponent::SetGlobalComponentHandler(this);
316 AliHLTLibHandle hLib;
317 const char* loadtype="";
319 // use interface to the dynamic linking loader
321 // exeption does not help in Root context, the Root exeption
322 // handler always catches the exeption before. Have to find out
323 // how exeptions can be used in Root
325 hLib.fHandle=dlopen(libraryPath, RTLD_NOW);
330 // error message printed further down
331 loadtype="dlopen exeption";
335 // use ROOT dynamic loader
336 // check if the library was already loaded, as Load returns
337 // 'failure' if the library was already loaded
339 AliHLTLibHandle* pLib=FindLibrary(libraryPath);
341 int* pRootHandle=reinterpret_cast<int*>(pLib->fHandle);
343 HLTDebug("instance %d of library %s loaded", (*pRootHandle), libraryPath);
344 hLib.fHandle=pRootHandle;
347 if (hLib.fHandle==NULL && gSystem->Load(libraryPath)>=0) {
348 int* pRootHandle=new int;
349 if (pRootHandle) *pRootHandle=1;
350 hLib.fHandle=pRootHandle;
351 //HLTDebug("library %s loaded via gSystem", libraryPath);
357 // error message printed further down
358 loadtype="gSystem exeption";
361 #endif //HAVE_DLFCN_H
362 if (hLib.fHandle!=NULL) {
363 // create TString object to store library path and use pointer as handle
364 hLib.fName=new TString(libraryPath);
365 hLib.fMode=fLibraryMode;
366 HLTImportant("library %s loaded (%s%s)", libraryPath, hLib.fMode==kStatic?"persistent, ":"", loadtype);
367 fLibraryList.insert(fLibraryList.begin(), hLib);
368 typedef void (*CompileInfo)( char*& date, char*& time);
369 CompileInfo fctInfo=(CompileInfo)FindSymbol(libraryPath, "CompileInfo");
373 (*fctInfo)(date, time);
374 if (!date) date="unknown";
375 if (!time) time="unknown";
376 HLTImportant("build on %s (%s)", date, time);
378 HLTImportant("no build info available (possible AliRoot embedded build)");
381 // static registration of components when library is loaded
382 iResult=RegisterScheduledComponents();
385 HLTError("can not load library %s (%s)", libraryPath, loadtype);
387 HLTError("dlopen error: %s", dlerror());
388 #endif //HAVE_DLFCN_H
395 AliHLTComponent::UnsetGlobalComponentHandler();
398 // alternative dynamic registration by library agents
399 // !!! has to be done after UnsetGlobalComponentHandler
400 if (bActivateAgents) ActivateAgents();
409 int AliHLTComponentHandler::UnloadLibrary( const char* libraryPath )
411 // see header file for class documentation
414 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
415 while (element!=fLibraryList.end()) {
416 TString* pName=reinterpret_cast<TString*>((*element).fName);
417 if (pName->CompareTo(libraryPath)==0) {
418 UnloadLibrary(*element);
419 fLibraryList.erase(element);
430 int AliHLTComponentHandler::UnloadLibrary(AliHLTComponentHandler::AliHLTLibHandle &handle)
432 // see header file for class documentation
434 fgAliLoggingFunc=NULL;
435 TString* pName=reinterpret_cast<TString*>(handle.fName);
436 if (handle.fMode!=kStatic) {
438 // exeption does not help in Root context, the Root exeption
439 // handler always catches the exeption before. Have to find out
440 // how exeptions can be used in Root
443 dlclose(handle.fHandle);
447 HLTError("exeption caught during dlclose of library %s", pName!=NULL?pName->Data():"");
451 int* pCount=reinterpret_cast<int*>(handle.fHandle);
452 if (--(*pCount)==0) {
454 /** Matthias 26.04.2007
455 * I spent about a week to investigate a bug which seems to be in ROOT.
456 * Under certain circumstances, TSystem::Unload crashes. The crash occured
457 * for the first time, when libAliHLTUtil was loaded from AliHLTSystem right
458 * after the ComponentHandler was created. It does not occur when dlopen is
460 * It has most likely to do with the garbage collection and automatic
461 * cleanup in ROOT. The crash occurs when ROOT is terminated and before
462 * an instance of AliHLTSystem was created.
463 * root [0] AliHLTSystem gHLT
464 * It does not occur when the instance was created dynamically (but not even
466 * root [0] AliHLTSystem* gHLT=new AliHLTSystem
468 * For that reason, the libraries are not unloaded here, even though there
469 * will be memory leaks.
470 gSystem->Unload(pName->Data());
474 HLTError("missing library name, can not unload");
478 #endif //HAVE_DLFCN_H
480 HLTDebug("unload library %s", pName->Data());
482 HLTWarning("missing name for unloaded library");
494 int AliHLTComponentHandler::UnloadLibraries()
496 // see header file for class documentation
498 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
499 while (element!=fLibraryList.end()) {
500 UnloadLibrary(*element);
501 fLibraryList.erase(element);
502 element=fLibraryList.begin();
507 void* AliHLTComponentHandler::FindSymbol(const char* library, const char* symbol)
509 // see header file for class documentation
510 AliHLTLibHandle* hLib=FindLibrary(library);
511 if (hLib==NULL) return NULL;
514 pFunc=dlsym(hLib->fHandle, symbol);
516 TString* name=reinterpret_cast<TString*>(hLib->fName);
517 pFunc=gSystem->DynFindSymbol(name->Data(), symbol);
522 AliHLTComponentHandler::AliHLTLibHandle* AliHLTComponentHandler::FindLibrary(const char* library)
524 // see header file for class documentation
525 AliHLTLibHandle* hLib=NULL;
526 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
527 while (element!=fLibraryList.end()) {
528 TString* name=reinterpret_cast<TString*>((*element).fName);
529 if (name->CompareTo(library)==0) {
538 int AliHLTComponentHandler::AddStandardComponents()
540 // see header file for class documentation
542 AliHLTComponent::SetGlobalComponentHandler(this);
543 AliHLTComponent::UnsetGlobalComponentHandler();
544 iResult=RegisterScheduledComponents();
548 int AliHLTComponentHandler::RegisterScheduledComponents()
550 // see header file for class documentation
552 AliHLTComponentPList::iterator element=fScheduleList.begin();
554 while (element!=fScheduleList.end()) {
555 iLocalResult=RegisterComponent(*element);
556 if (iResult==0) iResult=iLocalResult;
557 fScheduleList.erase(element);
558 element=fScheduleList.begin();
563 int AliHLTComponentHandler::ActivateAgents(const AliHLTModuleAgent** blackList, int size)
565 // see header file for class documentation
567 AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent();
568 while (pAgent && iResult>=0) {
571 for (; i<size; i++) {
572 if (blackList[i]==pAgent) break;
575 // this agent was in the list
576 pAgent=AliHLTModuleAgent::GetNextAgent();
581 pAgent->ActivateComponentHandler(this);
582 pAgent=AliHLTModuleAgent::GetNextAgent();
587 int AliHLTComponentHandler::DeleteOwnedComponents()
589 // see header file for class documentation
591 AliHLTComponentPList::iterator element=fOwnedComponents.begin();
592 while (element!=fOwnedComponents.end()) {
593 //DeregisterComponent((*element)->GetComponentID());
594 // exeption does not help in Root context, the Root exeption
595 // handler always catches the exeption before. Have to find out
596 // how exeptions can be used in Root
602 HLTError("delete managed sample %p", *element);
605 fOwnedComponents.erase(element);
606 element=fOwnedComponents.begin();