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),
59 fRunDesc(kAliHLTVoidRunDesc),
62 // see header file for class documentation
64 // refer to README to build package
66 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
67 memset(&fEnvironment, 0, sizeof(AliHLTAnalysisEnvironment));
68 fEnvironment.fStructSize=sizeof(AliHLTAnalysisEnvironment);
69 AddStandardComponents();
72 AliHLTComponentHandler::AliHLTComponentHandler(AliHLTAnalysisEnvironment* pEnv)
80 fLibraryMode(kDynamic),
81 fRunDesc(kAliHLTVoidRunDesc),
84 // see header file for class documentation
86 memcpy(&fEnvironment, pEnv, sizeof(AliHLTAnalysisEnvironment));
87 if (pEnv->fLoggingFunc) {
88 // the AliHLTLogging::Init method also sets the stream output
89 // and notification handler to AliLog. This should only be done
90 // if the logging environment contains a logging function
92 AliHLTLogging::Init(pEnv->fLoggingFunc);
95 memset(&fEnvironment, 0, sizeof(AliHLTAnalysisEnvironment));
96 fEnvironment.fStructSize=sizeof(AliHLTAnalysisEnvironment);
99 //SetLocalLoggingLevel(kHLTLogError);
101 //SetLocalLoggingLevel(kHLTLogInfo);
104 AddStandardComponents();
107 AliHLTComponentHandler::~AliHLTComponentHandler()
109 // see header file for class documentation
110 DeleteOwnedComponents();
112 if (fRunType) delete [] fRunType;
116 AliHLTComponentHandler* AliHLTComponentHandler::fgpInstance=NULL;
117 int AliHLTComponentHandler::fgNofInstances=0;
119 AliHLTComponentHandler* AliHLTComponentHandler::CreateHandler()
121 // see header file for class documentation
122 if (!fgpInstance) fgpInstance=new AliHLTComponentHandler;
127 int AliHLTComponentHandler::Destroy()
129 // see header file for class documentation
131 if (fgpInstance==this) {
132 nofInstances=fgNofInstances--;
134 if (nofInstances==0) delete this;
138 int AliHLTComponentHandler::AnnounceVersion()
140 // see header file for class documentation
142 #ifdef PACKAGE_STRING
143 void HLTbaseCompileInfo( char*& date, char*& time);
146 HLTbaseCompileInfo(date, time);
147 if (!date) date="unknown";
148 if (!time) time="unknown";
149 HLTImportant("%s build on %s (%s)", PACKAGE_STRING, date, time);
151 HLTImportant("ALICE High Level Trigger build on %s (%s) (embedded AliRoot build)", __DATE__, __TIME__);
156 Int_t AliHLTComponentHandler::AddComponent(AliHLTComponent* pSample)
158 // see header file for class documentation
160 if (pSample==NULL) return -EINVAL;
161 if ((iResult=RegisterComponent(pSample))>=0) {
162 //HLTDebug("sample %s (%p) managed by handler", pSample->GetComponentID(), pSample);
163 fOwnedComponents.push_back(pSample);
168 Int_t AliHLTComponentHandler::RegisterComponent(AliHLTComponent* pSample)
170 // see header file for class documentation
173 if (FindComponent(pSample->GetComponentID())==NULL) {
174 iResult=InsertComponent(pSample);
176 HLTInfo("component %s registered", pSample->GetComponentID());
179 // component already registered
180 HLTDebug("component %s already registered, skipped", pSample->GetComponentID());
189 int AliHLTComponentHandler::DeregisterComponent( const char* componentID )
191 // see header file for class documentation
195 HLTWarning("not yet implemented, please notify the developers if you need this function");
202 Int_t AliHLTComponentHandler::ScheduleRegister(AliHLTComponent* pSample)
204 // see header file for class documentation
207 fScheduleList.push_back(pSample);
214 int AliHLTComponentHandler::CreateComponent(const char* componentID, void* pEnvParam, int argc, const char** argv, AliHLTComponent*& component)
216 // see header file for class documentation
217 int iResult=CreateComponent(componentID, component);
218 if (iResult>=0 && component) {
219 HLTDebug("component \"%s\" created (%p)", componentID, component);
220 if ((iResult=component->Init(&fEnvironment, pEnvParam, argc, argv))!=0) {
221 HLTError("Initialization of component \"%s\" failed with error %d", componentID, iResult);
229 int AliHLTComponentHandler::CreateComponent(const char* componentID, AliHLTComponent*& component )
231 // see header file for class documentation
234 AliHLTComponent* pSample=FindComponent(componentID);
236 component=pSample->Spawn();
238 HLTDebug("component \"%s\" created (%p)", componentID, component);
240 HLTError("can not spawn component \"%s\"", componentID);
244 HLTWarning("can not find component \"%s\"", componentID);
253 Int_t AliHLTComponentHandler::FindComponentIndex(const char* componentID)
255 // see header file for class documentation
258 AliHLTComponentPList::iterator element=fComponentList.begin();
259 while (element!=fComponentList.end() && iResult>=0) {
260 if (strcmp(componentID, (*element)->GetComponentID())==0) {
266 if (element==fComponentList.end()) iResult=-ENOENT;
273 AliHLTComponent* AliHLTComponentHandler::FindComponent(const char* componentID)
275 // see header file for class documentation
276 AliHLTComponent* pSample=NULL;
277 Int_t index=FindComponentIndex(componentID);
279 pSample=(AliHLTComponent*)fComponentList.at(index);
284 Int_t AliHLTComponentHandler::InsertComponent(AliHLTComponent* pSample)
286 // see header file for class documentation
289 fComponentList.push_back(pSample);
296 void AliHLTComponentHandler::List()
298 // see header file for class documentation
299 AliHLTComponentPList::iterator element=fComponentList.begin();
301 while (element!=fComponentList.end()) {
302 HLTInfo("%d. %s", index++, (*element++)->GetComponentID());
306 int AliHLTComponentHandler::HasOutputData( const char* componentID)
308 // see header file for class documentation
310 AliHLTComponent* pSample=FindComponent(componentID);
312 AliHLTComponent::TComponentType ct=AliHLTComponent::kUnknown;
313 ct=pSample->GetComponentType();
314 iResult=(ct==AliHLTComponent::kSource || ct==AliHLTComponent::kProcessor);
321 void AliHLTComponentHandler::SetEnvironment(AliHLTAnalysisEnvironment* pEnv)
323 // see header file for class documentation
325 memset(&fEnvironment, 0, sizeof(AliHLTAnalysisEnvironment));
326 memcpy(&fEnvironment, pEnv, pEnv->fStructSize<sizeof(AliHLTAnalysisEnvironment)?pEnv->fStructSize:sizeof(AliHLTAnalysisEnvironment));
327 fEnvironment.fStructSize=sizeof(AliHLTAnalysisEnvironment);
328 if (fEnvironment.fLoggingFunc) {
329 // the AliHLTLogging::Init method also sets the stream output
330 // and notification handler to AliLog. This should only be done
331 // if the logging environment contains a logging function
333 AliHLTLogging::Init(fEnvironment.fLoggingFunc);
338 AliHLTComponentHandler::TLibraryMode AliHLTComponentHandler::SetLibraryMode(TLibraryMode mode)
340 // see header file for class documentation
341 TLibraryMode old=fLibraryMode;
346 int AliHLTComponentHandler::LoadLibrary( const char* libraryPath, int bActivateAgents)
348 // see header file for class documentation
351 // first activate all agents which are already loaded
352 if (bActivateAgents) ActivateAgents();
354 // set the global component handler for static component registration
355 AliHLTComponent::SetGlobalComponentHandler(this);
357 AliHLTLibHandle hLib;
358 AliHLTLibHandle* phSearch=FindLibrary(libraryPath);
359 const char* loadtype="";
361 // use interface to the dynamic linking loader
363 // exeption does not help in Root context, the Root exeption
364 // handler always catches the exeption before. Have to find out
365 // how exeptions can be used in Root
367 hLib.fHandle=dlopen(libraryPath, RTLD_NOW);
372 // error message printed further down
373 loadtype="dlopen exeption";
377 // use ROOT dynamic loader
378 // check if the library was already loaded, as Load returns
379 // 'failure' if the library was already loaded
382 int* pRootHandle=reinterpret_cast<int*>(phSearch->fHandle);
384 HLTDebug("instance %d of library %s loaded", (*pRootHandle), libraryPath);
385 hLib.fHandle=pRootHandle;
388 if (hLib.fHandle==NULL && gSystem->Load(libraryPath)>=0) {
389 int* pRootHandle=new int;
390 if (pRootHandle) *pRootHandle=1;
391 hLib.fHandle=pRootHandle;
392 //HLTDebug("library %s loaded via gSystem", libraryPath);
398 // error message printed further down
399 loadtype="gSystem exeption";
402 #endif //HAVE_DLFCN_H
403 if (hLib.fHandle!=NULL) {
404 // create TString object to store library path and use pointer as handle
405 hLib.fName=new TString(libraryPath);
406 hLib.fMode=fLibraryMode;
407 fLibraryList.insert(fLibraryList.begin(), hLib);
409 HLTImportant("library %s loaded (%s%s)", libraryPath, hLib.fMode==kStatic?"persistent, ":"", loadtype);
410 typedef void (*CompileInfo)( char*& date, char*& time);
411 CompileInfo fctInfo=(CompileInfo)FindSymbol(libraryPath, "CompileInfo");
415 (*fctInfo)(date, time);
416 if (!date) date="unknown";
417 if (!time) time="unknown";
418 HLTImportant("build on %s (%s)", date, time);
420 HLTImportant("no build info available (possible AliRoot embedded build)");
424 // static registration of components when library is loaded
425 iResult=RegisterScheduledComponents();
428 HLTError("can not load library %s (%s)", libraryPath, loadtype);
430 HLTError("dlopen error: %s", dlerror());
431 #endif //HAVE_DLFCN_H
438 AliHLTComponent::UnsetGlobalComponentHandler();
441 // alternative dynamic registration by library agents
442 // !!! has to be done after UnsetGlobalComponentHandler
443 if (bActivateAgents) ActivateAgents();
452 int AliHLTComponentHandler::UnloadLibrary( const char* libraryPath )
454 // see header file for class documentation
457 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
458 while (element!=fLibraryList.end()) {
459 TString* pName=reinterpret_cast<TString*>((*element).fName);
460 if (pName->CompareTo(libraryPath)==0) {
461 UnloadLibrary(*element);
462 fLibraryList.erase(element);
473 int AliHLTComponentHandler::UnloadLibrary(AliHLTComponentHandler::AliHLTLibHandle &handle)
475 // see header file for class documentation
477 fgAliLoggingFunc=NULL;
478 TString* pName=reinterpret_cast<TString*>(handle.fName);
479 if (handle.fMode!=kStatic) {
481 // exeption does not help in Root context, the Root exeption
482 // handler always catches the exeption before. Have to find out
483 // how exeptions can be used in Root
486 dlclose(handle.fHandle);
490 HLTError("exeption caught during dlclose of library %s", pName!=NULL?pName->Data():"");
494 int* pCount=reinterpret_cast<int*>(handle.fHandle);
495 if (--(*pCount)==0) {
497 /** Matthias 26.04.2007
498 * I spent about a week to investigate a bug which seems to be in ROOT.
499 * Under certain circumstances, TSystem::Unload crashes. The crash occured
500 * for the first time, when libAliHLTUtil was loaded from AliHLTSystem right
501 * after the ComponentHandler was created. It does not occur when dlopen is
503 * It has most likely to do with the garbage collection and automatic
504 * cleanup in ROOT. The crash occurs when ROOT is terminated and before
505 * an instance of AliHLTSystem was created.
506 * root [0] AliHLTSystem gHLT
507 * It does not occur when the instance was created dynamically (but not even
509 * root [0] AliHLTSystem* gHLT=new AliHLTSystem
511 * For that reason, the libraries are not unloaded here, even though there
512 * will be memory leaks.
513 gSystem->Unload(pName->Data());
517 HLTError("missing library name, can not unload");
521 #endif //HAVE_DLFCN_H
523 HLTDebug("unload library %s", pName->Data());
525 HLTWarning("missing name for unloaded library");
537 int AliHLTComponentHandler::UnloadLibraries()
539 // see header file for class documentation
541 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
542 while (element!=fLibraryList.end()) {
543 UnloadLibrary(*element);
544 fLibraryList.erase(element);
545 element=fLibraryList.begin();
550 void* AliHLTComponentHandler::FindSymbol(const char* library, const char* symbol)
552 // see header file for class documentation
553 AliHLTLibHandle* hLib=FindLibrary(library);
554 if (hLib==NULL) return NULL;
557 pFunc=dlsym(hLib->fHandle, symbol);
559 TString* name=reinterpret_cast<TString*>(hLib->fName);
560 pFunc=gSystem->DynFindSymbol(name->Data(), symbol);
565 AliHLTComponentHandler::AliHLTLibHandle* AliHLTComponentHandler::FindLibrary(const char* library)
567 // see header file for class documentation
568 AliHLTLibHandle* hLib=NULL;
569 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
570 while (element!=fLibraryList.end()) {
571 TString* name=reinterpret_cast<TString*>((*element).fName);
572 if (name->CompareTo(library)==0) {
581 int AliHLTComponentHandler::AddStandardComponents()
583 // see header file for class documentation
585 AliHLTComponent::SetGlobalComponentHandler(this);
586 AliHLTComponent::UnsetGlobalComponentHandler();
587 iResult=RegisterScheduledComponents();
591 int AliHLTComponentHandler::RegisterScheduledComponents()
593 // see header file for class documentation
595 AliHLTComponentPList::iterator element=fScheduleList.begin();
597 while (element!=fScheduleList.end()) {
598 iLocalResult=RegisterComponent(*element);
599 if (iResult==0) iResult=iLocalResult;
600 fScheduleList.erase(element);
601 element=fScheduleList.begin();
606 int AliHLTComponentHandler::ActivateAgents(const AliHLTModuleAgent** blackList, int size)
608 // see header file for class documentation
610 AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent();
611 while (pAgent && iResult>=0) {
614 for (; i<size; i++) {
615 if (blackList[i]==pAgent) break;
618 // this agent was in the list
619 pAgent=AliHLTModuleAgent::GetNextAgent();
624 pAgent->ActivateComponentHandler(this);
625 pAgent=AliHLTModuleAgent::GetNextAgent();
630 int AliHLTComponentHandler::DeleteOwnedComponents()
632 // see header file for class documentation
634 AliHLTComponentPList::iterator element=fOwnedComponents.begin();
635 while (element!=fOwnedComponents.end()) {
636 //DeregisterComponent((*element)->GetComponentID());
637 // exeption does not help in Root context, the Root exeption
638 // handler always catches the exeption before. Have to find out
639 // how exeptions can be used in Root
645 HLTError("delete managed sample %p", *element);
648 fOwnedComponents.erase(element);
649 element=fOwnedComponents.begin();
654 int AliHLTComponentHandler::SetRunDescription(const AliHLTRunDesc* desc, const char* runType)
656 // see header file for class documentation
657 if (!desc) return -EINVAL;
658 if (desc->fStructSize!=sizeof(AliHLTRunDesc)) {
659 HLTError("invalid size of RunDesc struct (%ul)", desc->fStructSize);
663 memcpy(&fRunDesc, desc, sizeof(AliHLTRunDesc));
665 if (fRunType) delete [] fRunType;
666 fRunType=new char[sizeof(runType)+1];
668 strcpy(fRunType, runType);