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 "AliHLTSystem.h"
42 // the standard components
43 // #include "AliHLTFilePublisher.h"
44 // #include "AliHLTFileWriter.h"
45 // #include "AliHLTRootFilePublisherComponent.h"
46 // #include "AliHLTRootFileWriterComponent.h"
48 /** ROOT macro for the implementation of ROOT specific class methods */
49 ClassImp(AliHLTComponentHandler)
51 AliHLTComponentHandler::AliHLTComponentHandler()
59 // see header file for class documentation
61 // refer to README to build package
63 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
64 memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
65 AddStandardComponents();
68 AliHLTComponentHandler::AliHLTComponentHandler(AliHLTComponentEnvironment* pEnv)
76 // see header file for class documentation
78 memcpy(&fEnvironment, pEnv, sizeof(AliHLTComponentEnvironment));
79 if (pEnv->fLoggingFunc) {
80 // the AliHLTLogging::Init method also sets the stream output
81 // and notification handler to AliLog. This should only be done
82 // if the logging environment contains a logging function
84 AliHLTLogging::Init(pEnv->fLoggingFunc);
87 memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
88 AddStandardComponents();
91 AliHLTComponentHandler::AliHLTComponentHandler(const AliHLTComponentHandler&)
99 // see header file for class documentation
100 HLTFatal("copy constructor untested");
103 AliHLTComponentHandler& AliHLTComponentHandler::operator=(const AliHLTComponentHandler&)
105 // see header file for class documentation
106 HLTFatal("assignment operator untested");
110 AliHLTComponentHandler::~AliHLTComponentHandler()
112 // see header file for class documentation
113 DeleteStandardComponents();
117 int AliHLTComponentHandler::AnnounceVersion()
119 // see header file for class documentation
121 #ifdef PACKAGE_STRING
122 void HLTbaseCompileInfo( char*& date, char*& time);
125 HLTbaseCompileInfo(date, time);
126 if (!date) date="unknown";
127 if (!time) time="unknown";
128 HLTInfo("%s build on %s (%s)", PACKAGE_STRING, date, time);
130 HLTInfo("ALICE High Level Trigger build on %s (%s) (embedded AliRoot build)", __DATE__, __TIME__);
135 Int_t AliHLTComponentHandler::RegisterComponent(AliHLTComponent* pSample)
137 // see header file for class documentation
140 if (FindComponent(pSample->GetComponentID())==NULL) {
141 iResult=InsertComponent(pSample);
143 HLTInfo("component %s registered", pSample->GetComponentID());
146 // component already registered
147 HLTDebug("component %s already registered, skipped", pSample->GetComponentID());
156 int AliHLTComponentHandler::DeregisterComponent( const char* componentID )
158 // see header file for class documentation
162 HLTWarning("not yet implemented, please notify the developers if you need this function");
169 Int_t AliHLTComponentHandler::ScheduleRegister(AliHLTComponent* pSample)
171 // see header file for class documentation
174 fScheduleList.push_back(pSample);
181 int AliHLTComponentHandler::CreateComponent(const char* componentID, void* pEnvParam, int argc, const char** argv, AliHLTComponent*& component )
183 // see header file for class documentation
186 AliHLTComponent* pSample=FindComponent(componentID);
188 component=pSample->Spawn();
190 HLTDebug("component \"%s\" created (%p)", componentID, component);
191 if ((iResult=component->Init(&fEnvironment, pEnvParam, argc, argv))!=0) {
192 HLTError("Initialization of component \"%s\" failed with error %d", componentID, iResult);
197 HLTError("can not spawn component \"%s\"", componentID);
201 HLTWarning("can not find component \"%s\"", componentID);
210 Int_t AliHLTComponentHandler::FindComponentIndex(const char* componentID)
212 // see header file for class documentation
215 vector<AliHLTComponent*>::iterator element=fComponentList.begin();
216 while (element!=fComponentList.end() && iResult>=0) {
217 if (strcmp(componentID, (*element)->GetComponentID())==0) {
223 if (element==fComponentList.end()) iResult=-ENOENT;
230 AliHLTComponent* AliHLTComponentHandler::FindComponent(const char* componentID)
232 // see header file for class documentation
233 AliHLTComponent* pSample=NULL;
234 Int_t index=FindComponentIndex(componentID);
236 pSample=(AliHLTComponent*)fComponentList.at(index);
241 Int_t AliHLTComponentHandler::InsertComponent(AliHLTComponent* pSample)
243 // see header file for class documentation
246 fComponentList.push_back(pSample);
253 void AliHLTComponentHandler::List()
255 // see header file for class documentation
256 vector<AliHLTComponent*>::iterator element=fComponentList.begin();
258 while (element!=fComponentList.end()) {
259 HLTInfo("%d. %s", index++, (*element++)->GetComponentID());
263 void AliHLTComponentHandler::SetEnvironment(AliHLTComponentEnvironment* pEnv)
265 // see header file for class documentation
267 memcpy(&fEnvironment, pEnv, sizeof(AliHLTComponentEnvironment));
268 if (fEnvironment.fLoggingFunc) {
269 // the AliHLTLogging::Init method also sets the stream output
270 // and notification handler to AliLog. This should only be done
271 // if the logging environment contains a logging function
273 AliHLTLogging::Init(fEnvironment.fLoggingFunc);
278 int AliHLTComponentHandler::LoadLibrary( const char* libraryPath )
280 // see header file for class documentation
283 AliHLTComponent::SetGlobalComponentHandler(this);
284 AliHLTLibHandle hLib;
285 const char* loadtype="";
287 // use interface to the dynamic linking loader
288 hLib.fHandle=dlopen(libraryPath, RTLD_NOW);
291 // use ROOT dynamic loader
292 // check if the library was already loaded, as Load returns
293 // 'failure' if the library was already loaded
294 AliHLTLibHandle* pLib=FindLibrary(libraryPath);
296 int* pRootHandle=reinterpret_cast<int*>(pLib->fHandle);
298 HLTDebug("instance %d of library %s loaded", (*pRootHandle), libraryPath);
299 hLib.fHandle=pRootHandle;
302 if (hLib.fHandle==NULL && gSystem->Load(libraryPath)==0) {
303 int* pRootHandle=new int;
304 if (pRootHandle) *pRootHandle=1;
305 hLib.fHandle=pRootHandle;
306 //HLTDebug("library %s loaded via gSystem", libraryPath);
309 #endif //HAVE_DLFCN_H
310 if (hLib.fHandle!=NULL) {
311 // create TString object to store library path and use pointer as handle
312 hLib.fName=new TString(libraryPath);
313 HLTInfo("library %s loaded (%s)", libraryPath, loadtype);
314 fLibraryList.insert(fLibraryList.begin(), hLib);
315 iResult=RegisterScheduledComponents();
317 HLTError("can not load library %s", libraryPath);
319 HLTError("dlopen error: %s", dlerror());
320 #endif //HAVE_DLFCN_H
327 AliHLTComponent::UnsetGlobalComponentHandler();
334 int AliHLTComponentHandler::UnloadLibrary( const char* libraryPath )
336 // see header file for class documentation
339 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
340 while (element!=fLibraryList.end()) {
341 TString* pName=reinterpret_cast<TString*>((*element).fName);
342 if (pName->CompareTo(libraryPath)==0) {
343 UnloadLibrary(*element);
344 fLibraryList.erase(element);
355 int AliHLTComponentHandler::UnloadLibrary(AliHLTComponentHandler::AliHLTLibHandle &handle)
357 // see header file for class documentation
359 fgAliLoggingFunc=NULL;
360 TString* pName=reinterpret_cast<TString*>(handle.fName);
362 dlclose(handle.fHandle);
364 int* pCount=reinterpret_cast<int*>(handle.fHandle);
365 if (--(*pCount)==0) {
367 /** Matthias 26.04.2007
368 * I spent about a week to investigate a bug which seems to be in ROOT.
369 * Under certain circumstances, TSystem::Unload crashes. The crash occured
370 * for the first time, when libAliHLTUtil was loaded from AliHLTSystem right
371 * after the ComponentHandler was created. It does not occur when dlopen is
373 * It has most likely to do with the garbage collection and automatic
374 * cleanup in ROOT. The crash occurs when ROOT is terminated and before
375 * an instance of AliHLTSystem was created.
376 * root [0] AliHLTSystem gHLT
377 * It does not occur when the instance was created dynamically (but not even
379 * root [0] AliHLTSystem* gHLT=new AliHLTSystem
381 * For that reason, the libraries are not unloaded here, even though there
382 * will be memory leaks.
383 gSystem->Unload(pName->Data());
387 HLTError("missing library name, can not unload");
391 #endif //HAVE_DLFCN_H
395 HLTDebug("unload library %s", pName->Data());
398 HLTWarning("missing name for unloaded library");
404 int AliHLTComponentHandler::UnloadLibraries()
406 // see header file for class documentation
408 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
409 while (element!=fLibraryList.end()) {
410 UnloadLibrary(*element);
411 fLibraryList.erase(element);
412 element=fLibraryList.begin();
417 void* AliHLTComponentHandler::FindSymbol(const char* library, const char* symbol)
419 // see header file for class documentation
420 AliHLTLibHandle* hLib=FindLibrary(library);
421 if (hLib==NULL) return NULL;
424 pFunc=dlsym(hLib->fHandle, symbol);
426 TString* name=reinterpret_cast<TString*>(hLib->fName);
427 pFunc=gSystem->DynFindSymbol(name->Data(), symbol);
432 AliHLTComponentHandler::AliHLTLibHandle* AliHLTComponentHandler::FindLibrary(const char* library)
434 // see header file for class documentation
435 AliHLTLibHandle* hLib=NULL;
436 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
437 while (element!=fLibraryList.end()) {
438 TString* name=reinterpret_cast<TString*>((*element).fName);
439 if (name->CompareTo(library)==0) {
448 int AliHLTComponentHandler::AddStandardComponents()
450 // see header file for class documentation
452 AliHLTComponent::SetGlobalComponentHandler(this);
453 // fStandardList.push_back(new AliHLTFilePublisher);
454 // fStandardList.push_back(new AliHLTFileWriter);
455 // fStandardList.push_back(new AliHLTRootFilePublisherComponent);
456 // fStandardList.push_back(new AliHLTRootFileWriterComponent);
457 AliHLTComponent::UnsetGlobalComponentHandler();
458 iResult=RegisterScheduledComponents();
462 int AliHLTComponentHandler::RegisterScheduledComponents()
464 // see header file for class documentation
466 vector<AliHLTComponent*>::iterator element=fScheduleList.begin();
468 while (element!=fScheduleList.end()) {
469 iLocalResult=RegisterComponent(*element);
470 if (iResult==0) iResult=iLocalResult;
471 fScheduleList.erase(element);
472 element=fScheduleList.begin();
477 int AliHLTComponentHandler::DeleteStandardComponents()
479 // see header file for class documentation
481 vector<AliHLTComponent*>::iterator element=fStandardList.begin();
482 while (element!=fStandardList.end()) {
483 //DeregisterComponent((*element)->GetComponentID());
485 fStandardList.erase(element);
486 element=fStandardList.begin();