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. */
32 //#include <Riostream.h>
35 //#include "AliHLTStdIncludes.h"
36 #include "AliHLTComponentHandler.h"
37 #include "AliHLTComponent.h"
38 #include "AliHLTDataTypes.h"
39 #include "AliHLTModuleAgent.h"
42 /** ROOT macro for the implementation of ROOT specific class methods */
43 ClassImp(AliHLTComponentHandler)
45 AliHLTComponentHandler::AliHLTComponentHandler()
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(AliHLTComponentEnvironment));
59 AddStandardComponents();
62 AliHLTComponentHandler::AliHLTComponentHandler(AliHLTComponentEnvironment* pEnv)
71 // see header file for class documentation
73 memcpy(&fEnvironment, pEnv, sizeof(AliHLTComponentEnvironment));
74 if (pEnv->fLoggingFunc) {
75 // the AliHLTLogging::Init method also sets the stream output
76 // and notification handler to AliLog. This should only be done
77 // if the logging environment contains a logging function
79 AliHLTLogging::Init(pEnv->fLoggingFunc);
82 memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
85 //SetLocalLoggingLevel(kHLTLogError);
87 //SetLocalLoggingLevel(kHLTLogInfo);
90 AddStandardComponents();
93 AliHLTComponentHandler::~AliHLTComponentHandler()
95 // see header file for class documentation
96 DeleteStandardComponents();
100 int AliHLTComponentHandler::AnnounceVersion()
102 // see header file for class documentation
104 #ifdef PACKAGE_STRING
105 void HLTbaseCompileInfo( char*& date, char*& time);
108 HLTbaseCompileInfo(date, time);
109 if (!date) date="unknown";
110 if (!time) time="unknown";
111 HLTInfo("%s build on %s (%s)", PACKAGE_STRING, date, time);
113 HLTInfo("ALICE High Level Trigger build on %s (%s) (embedded AliRoot build)", __DATE__, __TIME__);
118 Int_t AliHLTComponentHandler::AddComponent(AliHLTComponent* pSample)
120 // see header file for class documentation
122 if ((iResult=RegisterComponent(pSample))>=0) {
123 fOwnedComponents.push_back(pSample);
128 Int_t AliHLTComponentHandler::RegisterComponent(AliHLTComponent* pSample)
130 // see header file for class documentation
133 if (FindComponent(pSample->GetComponentID())==NULL) {
134 iResult=InsertComponent(pSample);
136 HLTInfo("component %s registered", pSample->GetComponentID());
139 // component already registered
140 HLTDebug("component %s already registered, skipped", pSample->GetComponentID());
149 int AliHLTComponentHandler::DeregisterComponent( const char* componentID )
151 // see header file for class documentation
155 HLTWarning("not yet implemented, please notify the developers if you need this function");
162 Int_t AliHLTComponentHandler::ScheduleRegister(AliHLTComponent* pSample)
164 // see header file for class documentation
167 fScheduleList.push_back(pSample);
174 int AliHLTComponentHandler::CreateComponent(const char* componentID, void* pEnvParam, int argc, const char** argv, AliHLTComponent*& component )
176 // see header file for class documentation
179 AliHLTComponent* pSample=FindComponent(componentID);
181 component=pSample->Spawn();
183 HLTDebug("component \"%s\" created (%p)", componentID, component);
184 if ((iResult=component->Init(&fEnvironment, pEnvParam, argc, argv))!=0) {
185 HLTError("Initialization of component \"%s\" failed with error %d", componentID, iResult);
190 HLTError("can not spawn component \"%s\"", componentID);
194 HLTWarning("can not find component \"%s\"", componentID);
203 Int_t AliHLTComponentHandler::FindComponentIndex(const char* componentID)
205 // see header file for class documentation
208 vector<AliHLTComponent*>::iterator element=fComponentList.begin();
209 while (element!=fComponentList.end() && iResult>=0) {
210 if (strcmp(componentID, (*element)->GetComponentID())==0) {
216 if (element==fComponentList.end()) iResult=-ENOENT;
223 AliHLTComponent* AliHLTComponentHandler::FindComponent(const char* componentID)
225 // see header file for class documentation
226 AliHLTComponent* pSample=NULL;
227 Int_t index=FindComponentIndex(componentID);
229 pSample=(AliHLTComponent*)fComponentList.at(index);
234 Int_t AliHLTComponentHandler::InsertComponent(AliHLTComponent* pSample)
236 // see header file for class documentation
239 fComponentList.push_back(pSample);
246 void AliHLTComponentHandler::List()
248 // see header file for class documentation
249 vector<AliHLTComponent*>::iterator element=fComponentList.begin();
251 while (element!=fComponentList.end()) {
252 HLTInfo("%d. %s", index++, (*element++)->GetComponentID());
256 int AliHLTComponentHandler::HasOutputData( const char* componentID)
258 // see header file for class documentation
260 AliHLTComponent* pSample=FindComponent(componentID);
262 AliHLTComponent::TComponentType ct=AliHLTComponent::kUnknown;
263 ct=pSample->GetComponentType();
264 iResult=(ct==AliHLTComponent::kSource || ct==AliHLTComponent::kProcessor);
271 void AliHLTComponentHandler::SetEnvironment(AliHLTComponentEnvironment* pEnv)
273 // see header file for class documentation
275 memcpy(&fEnvironment, pEnv, sizeof(AliHLTComponentEnvironment));
276 if (fEnvironment.fLoggingFunc) {
277 // the AliHLTLogging::Init method also sets the stream output
278 // and notification handler to AliLog. This should only be done
279 // if the logging environment contains a logging function
281 AliHLTLogging::Init(fEnvironment.fLoggingFunc);
286 int AliHLTComponentHandler::LoadLibrary( const char* libraryPath, int bActivateAgents)
288 // see header file for class documentation
291 // first activate all agents which are already loaded
292 if (bActivateAgents) ActivateAgents();
294 // set the global component handler for static component registration
295 AliHLTComponent::SetGlobalComponentHandler(this);
297 AliHLTLibHandle hLib;
298 const char* loadtype="";
300 // use interface to the dynamic linking loader
302 hLib.fHandle=dlopen(libraryPath, RTLD_NOW);
306 // error message printed further down
307 loadtype="dlopen exeption";
310 // use ROOT dynamic loader
311 // check if the library was already loaded, as Load returns
312 // 'failure' if the library was already loaded
314 AliHLTLibHandle* pLib=FindLibrary(libraryPath);
316 int* pRootHandle=reinterpret_cast<int*>(pLib->fHandle);
318 HLTDebug("instance %d of library %s loaded", (*pRootHandle), libraryPath);
319 hLib.fHandle=pRootHandle;
322 if (hLib.fHandle==NULL && gSystem->Load(libraryPath)==0) {
323 int* pRootHandle=new int;
324 if (pRootHandle) *pRootHandle=1;
325 hLib.fHandle=pRootHandle;
326 //HLTDebug("library %s loaded via gSystem", libraryPath);
331 // error message printed further down
332 loadtype="gSystem exeption";
334 #endif //HAVE_DLFCN_H
335 if (hLib.fHandle!=NULL) {
336 // create TString object to store library path and use pointer as handle
337 hLib.fName=new TString(libraryPath);
338 HLTInfo("library %s loaded (%s)", libraryPath, loadtype);
339 fLibraryList.insert(fLibraryList.begin(), hLib);
340 typedef void (*CompileInfo)( char*& date, char*& time);
341 CompileInfo fctInfo=(CompileInfo)FindSymbol(libraryPath, "CompileInfo");
345 (*fctInfo)(date, time);
346 if (!date) date="unknown";
347 if (!time) time="unknown";
348 HLTInfo("build on %s (%s)", date, time);
350 HLTInfo("no build info available (possible AliRoot embedded build)");
353 // static registration of components when library is loaded
354 iResult=RegisterScheduledComponents();
357 HLTError("can not load library %s (%s)", libraryPath, loadtype);
359 HLTError("dlopen error: %s", dlerror());
360 #endif //HAVE_DLFCN_H
367 AliHLTComponent::UnsetGlobalComponentHandler();
370 // alternative dynamic registration by library agents
371 // !!! has to be done after UnsetGlobalComponentHandler
372 if (bActivateAgents) ActivateAgents();
381 int AliHLTComponentHandler::UnloadLibrary( const char* libraryPath )
383 // see header file for class documentation
386 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
387 while (element!=fLibraryList.end()) {
388 TString* pName=reinterpret_cast<TString*>((*element).fName);
389 if (pName->CompareTo(libraryPath)==0) {
390 UnloadLibrary(*element);
391 fLibraryList.erase(element);
402 int AliHLTComponentHandler::UnloadLibrary(AliHLTComponentHandler::AliHLTLibHandle &handle)
404 // see header file for class documentation
406 fgAliLoggingFunc=NULL;
407 TString* pName=reinterpret_cast<TString*>(handle.fName);
410 dlclose(handle.fHandle);
413 HLTError("exeption caught during dlclose of library %s", pName!=NULL?pName->Data():"");
416 int* pCount=reinterpret_cast<int*>(handle.fHandle);
417 if (--(*pCount)==0) {
419 /** Matthias 26.04.2007
420 * I spent about a week to investigate a bug which seems to be in ROOT.
421 * Under certain circumstances, TSystem::Unload crashes. The crash occured
422 * for the first time, when libAliHLTUtil was loaded from AliHLTSystem right
423 * after the ComponentHandler was created. It does not occur when dlopen is
425 * It has most likely to do with the garbage collection and automatic
426 * cleanup in ROOT. The crash occurs when ROOT is terminated and before
427 * an instance of AliHLTSystem was created.
428 * root [0] AliHLTSystem gHLT
429 * It does not occur when the instance was created dynamically (but not even
431 * root [0] AliHLTSystem* gHLT=new AliHLTSystem
433 * For that reason, the libraries are not unloaded here, even though there
434 * will be memory leaks.
435 gSystem->Unload(pName->Data());
439 HLTError("missing library name, can not unload");
443 #endif //HAVE_DLFCN_H
447 HLTDebug("unload library %s", pName->Data());
450 HLTWarning("missing name for unloaded library");
456 int AliHLTComponentHandler::UnloadLibraries()
458 // see header file for class documentation
460 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
461 while (element!=fLibraryList.end()) {
462 UnloadLibrary(*element);
463 fLibraryList.erase(element);
464 element=fLibraryList.begin();
469 void* AliHLTComponentHandler::FindSymbol(const char* library, const char* symbol)
471 // see header file for class documentation
472 AliHLTLibHandle* hLib=FindLibrary(library);
473 if (hLib==NULL) return NULL;
476 pFunc=dlsym(hLib->fHandle, symbol);
478 TString* name=reinterpret_cast<TString*>(hLib->fName);
479 pFunc=gSystem->DynFindSymbol(name->Data(), symbol);
484 AliHLTComponentHandler::AliHLTLibHandle* AliHLTComponentHandler::FindLibrary(const char* library)
486 // see header file for class documentation
487 AliHLTLibHandle* hLib=NULL;
488 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
489 while (element!=fLibraryList.end()) {
490 TString* name=reinterpret_cast<TString*>((*element).fName);
491 if (name->CompareTo(library)==0) {
500 int AliHLTComponentHandler::AddStandardComponents()
502 // see header file for class documentation
504 AliHLTComponent::SetGlobalComponentHandler(this);
505 AliHLTComponent::UnsetGlobalComponentHandler();
506 iResult=RegisterScheduledComponents();
510 int AliHLTComponentHandler::RegisterScheduledComponents()
512 // see header file for class documentation
514 vector<AliHLTComponent*>::iterator element=fScheduleList.begin();
516 while (element!=fScheduleList.end()) {
517 iLocalResult=RegisterComponent(*element);
518 if (iResult==0) iResult=iLocalResult;
519 fScheduleList.erase(element);
520 element=fScheduleList.begin();
525 int AliHLTComponentHandler::ActivateAgents(const AliHLTModuleAgent** blackList, int size)
527 // see header file for class documentation
529 AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent();
530 while (pAgent && iResult>=0) {
533 for (; i<size; i++) {
534 if (blackList[i]==pAgent) break;
537 // this agent was in the list
538 pAgent=AliHLTModuleAgent::GetNextAgent();
543 pAgent->ActivateComponentHandler(this);
544 pAgent=AliHLTModuleAgent::GetNextAgent();
549 int AliHLTComponentHandler::DeleteStandardComponents()
551 // see header file for class documentation
553 vector<AliHLTComponent*>::iterator element=fOwnedComponents.begin();
554 while (element!=fOwnedComponents.end()) {
555 //DeregisterComponent((*element)->GetComponentID());
557 fOwnedComponents.erase(element);
558 element=fOwnedComponents.begin();