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 HLTInfo("%s build on %s (%s)", PACKAGE_STRING, date, time);
121 HLTInfo("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 )
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 if ((iResult=component->Init(&fEnvironment, pEnvParam, argc, argv))!=0) {
195 HLTError("Initialization of component \"%s\" failed with error %d", componentID, iResult);
200 HLTError("can not spawn component \"%s\"", componentID);
204 HLTWarning("can not find component \"%s\"", componentID);
213 Int_t AliHLTComponentHandler::FindComponentIndex(const char* componentID)
215 // see header file for class documentation
218 AliHLTComponentPList::iterator element=fComponentList.begin();
219 while (element!=fComponentList.end() && iResult>=0) {
220 if (strcmp(componentID, (*element)->GetComponentID())==0) {
226 if (element==fComponentList.end()) iResult=-ENOENT;
233 AliHLTComponent* AliHLTComponentHandler::FindComponent(const char* componentID)
235 // see header file for class documentation
236 AliHLTComponent* pSample=NULL;
237 Int_t index=FindComponentIndex(componentID);
239 pSample=(AliHLTComponent*)fComponentList.at(index);
244 Int_t AliHLTComponentHandler::InsertComponent(AliHLTComponent* pSample)
246 // see header file for class documentation
249 fComponentList.push_back(pSample);
256 void AliHLTComponentHandler::List()
258 // see header file for class documentation
259 AliHLTComponentPList::iterator element=fComponentList.begin();
261 while (element!=fComponentList.end()) {
262 HLTInfo("%d. %s", index++, (*element++)->GetComponentID());
266 int AliHLTComponentHandler::HasOutputData( const char* componentID)
268 // see header file for class documentation
270 AliHLTComponent* pSample=FindComponent(componentID);
272 AliHLTComponent::TComponentType ct=AliHLTComponent::kUnknown;
273 ct=pSample->GetComponentType();
274 iResult=(ct==AliHLTComponent::kSource || ct==AliHLTComponent::kProcessor);
281 void AliHLTComponentHandler::SetEnvironment(AliHLTComponentEnvironment* pEnv)
283 // see header file for class documentation
285 memcpy(&fEnvironment, pEnv, sizeof(AliHLTComponentEnvironment));
286 if (fEnvironment.fLoggingFunc) {
287 // the AliHLTLogging::Init method also sets the stream output
288 // and notification handler to AliLog. This should only be done
289 // if the logging environment contains a logging function
291 AliHLTLogging::Init(fEnvironment.fLoggingFunc);
296 AliHLTComponentHandler::TLibraryMode AliHLTComponentHandler::SetLibraryMode(TLibraryMode mode)
298 // see header file for class documentation
299 TLibraryMode old=fLibraryMode;
304 int AliHLTComponentHandler::LoadLibrary( const char* libraryPath, int bActivateAgents)
306 // see header file for class documentation
309 // first activate all agents which are already loaded
310 if (bActivateAgents) ActivateAgents();
312 // set the global component handler for static component registration
313 AliHLTComponent::SetGlobalComponentHandler(this);
315 AliHLTLibHandle hLib;
316 const char* loadtype="";
318 // use interface to the dynamic linking loader
320 hLib.fHandle=dlopen(libraryPath, RTLD_NOW);
324 // error message printed further down
325 loadtype="dlopen exeption";
328 // use ROOT dynamic loader
329 // check if the library was already loaded, as Load returns
330 // 'failure' if the library was already loaded
332 AliHLTLibHandle* pLib=FindLibrary(libraryPath);
334 int* pRootHandle=reinterpret_cast<int*>(pLib->fHandle);
336 HLTDebug("instance %d of library %s loaded", (*pRootHandle), libraryPath);
337 hLib.fHandle=pRootHandle;
340 if (hLib.fHandle==NULL && gSystem->Load(libraryPath)==0) {
341 int* pRootHandle=new int;
342 if (pRootHandle) *pRootHandle=1;
343 hLib.fHandle=pRootHandle;
344 //HLTDebug("library %s loaded via gSystem", libraryPath);
349 // error message printed further down
350 loadtype="gSystem exeption";
352 #endif //HAVE_DLFCN_H
353 if (hLib.fHandle!=NULL) {
354 // create TString object to store library path and use pointer as handle
355 hLib.fName=new TString(libraryPath);
356 hLib.fMode=fLibraryMode;
357 HLTInfo("library %s loaded (%s%s)", libraryPath, hLib.fMode==kStatic?"persistent, ":"", loadtype);
358 fLibraryList.insert(fLibraryList.begin(), hLib);
359 typedef void (*CompileInfo)( char*& date, char*& time);
360 CompileInfo fctInfo=(CompileInfo)FindSymbol(libraryPath, "CompileInfo");
364 (*fctInfo)(date, time);
365 if (!date) date="unknown";
366 if (!time) time="unknown";
367 HLTInfo("build on %s (%s)", date, time);
369 HLTInfo("no build info available (possible AliRoot embedded build)");
372 // static registration of components when library is loaded
373 iResult=RegisterScheduledComponents();
376 HLTError("can not load library %s (%s)", libraryPath, loadtype);
378 HLTError("dlopen error: %s", dlerror());
379 #endif //HAVE_DLFCN_H
386 AliHLTComponent::UnsetGlobalComponentHandler();
389 // alternative dynamic registration by library agents
390 // !!! has to be done after UnsetGlobalComponentHandler
391 if (bActivateAgents) ActivateAgents();
400 int AliHLTComponentHandler::UnloadLibrary( const char* libraryPath )
402 // see header file for class documentation
405 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
406 while (element!=fLibraryList.end()) {
407 TString* pName=reinterpret_cast<TString*>((*element).fName);
408 if (pName->CompareTo(libraryPath)==0) {
409 UnloadLibrary(*element);
410 fLibraryList.erase(element);
421 int AliHLTComponentHandler::UnloadLibrary(AliHLTComponentHandler::AliHLTLibHandle &handle)
423 // see header file for class documentation
425 fgAliLoggingFunc=NULL;
426 TString* pName=reinterpret_cast<TString*>(handle.fName);
427 if (handle.fMode!=kStatic) {
430 dlclose(handle.fHandle);
433 HLTError("exeption caught during dlclose of library %s", pName!=NULL?pName->Data():"");
436 int* pCount=reinterpret_cast<int*>(handle.fHandle);
437 if (--(*pCount)==0) {
439 /** Matthias 26.04.2007
440 * I spent about a week to investigate a bug which seems to be in ROOT.
441 * Under certain circumstances, TSystem::Unload crashes. The crash occured
442 * for the first time, when libAliHLTUtil was loaded from AliHLTSystem right
443 * after the ComponentHandler was created. It does not occur when dlopen is
445 * It has most likely to do with the garbage collection and automatic
446 * cleanup in ROOT. The crash occurs when ROOT is terminated and before
447 * an instance of AliHLTSystem was created.
448 * root [0] AliHLTSystem gHLT
449 * It does not occur when the instance was created dynamically (but not even
451 * root [0] AliHLTSystem* gHLT=new AliHLTSystem
453 * For that reason, the libraries are not unloaded here, even though there
454 * will be memory leaks.
455 gSystem->Unload(pName->Data());
459 HLTError("missing library name, can not unload");
463 #endif //HAVE_DLFCN_H
465 HLTDebug("unload library %s", pName->Data());
467 HLTWarning("missing name for unloaded library");
479 int AliHLTComponentHandler::UnloadLibraries()
481 // see header file for class documentation
483 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
484 while (element!=fLibraryList.end()) {
485 UnloadLibrary(*element);
486 fLibraryList.erase(element);
487 element=fLibraryList.begin();
492 void* AliHLTComponentHandler::FindSymbol(const char* library, const char* symbol)
494 // see header file for class documentation
495 AliHLTLibHandle* hLib=FindLibrary(library);
496 if (hLib==NULL) return NULL;
499 pFunc=dlsym(hLib->fHandle, symbol);
501 TString* name=reinterpret_cast<TString*>(hLib->fName);
502 pFunc=gSystem->DynFindSymbol(name->Data(), symbol);
507 AliHLTComponentHandler::AliHLTLibHandle* AliHLTComponentHandler::FindLibrary(const char* library)
509 // see header file for class documentation
510 AliHLTLibHandle* hLib=NULL;
511 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
512 while (element!=fLibraryList.end()) {
513 TString* name=reinterpret_cast<TString*>((*element).fName);
514 if (name->CompareTo(library)==0) {
523 int AliHLTComponentHandler::AddStandardComponents()
525 // see header file for class documentation
527 AliHLTComponent::SetGlobalComponentHandler(this);
528 AliHLTComponent::UnsetGlobalComponentHandler();
529 iResult=RegisterScheduledComponents();
533 int AliHLTComponentHandler::RegisterScheduledComponents()
535 // see header file for class documentation
537 AliHLTComponentPList::iterator element=fScheduleList.begin();
539 while (element!=fScheduleList.end()) {
540 iLocalResult=RegisterComponent(*element);
541 if (iResult==0) iResult=iLocalResult;
542 fScheduleList.erase(element);
543 element=fScheduleList.begin();
548 int AliHLTComponentHandler::ActivateAgents(const AliHLTModuleAgent** blackList, int size)
550 // see header file for class documentation
552 AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent();
553 while (pAgent && iResult>=0) {
556 for (; i<size; i++) {
557 if (blackList[i]==pAgent) break;
560 // this agent was in the list
561 pAgent=AliHLTModuleAgent::GetNextAgent();
566 pAgent->ActivateComponentHandler(this);
567 pAgent=AliHLTModuleAgent::GetNextAgent();
572 int AliHLTComponentHandler::DeleteOwnedComponents()
574 // see header file for class documentation
576 AliHLTComponentPList::iterator element=fOwnedComponents.begin();
577 while (element!=fOwnedComponents.end()) {
578 //DeregisterComponent((*element)->GetComponentID());
583 HLTError("delete managed sample %p", *element);
585 fOwnedComponents.erase(element);
586 element=fOwnedComponents.begin();