]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/BASE/AliHLTComponentHandler.cxx
Ignoring temporary files
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTComponentHandler.cxx
CommitLineData
f23a6e1a 1// $Id$
2
3/**************************************************************************
9be2600f 4 * This file is property of and copyright by the ALICE HLT Project *
5 * ALICE Experiment at CERN, All rights reserved. *
f23a6e1a 6 * *
9be2600f 7 * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 * Timm Steinbeck <timm@kip.uni-heidelberg.de> *
9 * for The ALICE HLT Project. *
f23a6e1a 10 * *
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 **************************************************************************/
19
b22e91eb 20/** @file AliHLTComponentHandler.cxx
21 @author Matthias Richter, Timm Steinbeck
22 @date
23 @brief Implementation of HLT component handler. */
f23a6e1a 24
7bcd6cad 25// see header file for class documentation
26// or
27// refer to README to build package
28// or
29// visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
30
0c0c9d99 31#if __GNUC__>= 3
f23a6e1a 32using namespace std;
33#endif
53feaef5 34//#undef HAVE_DLFCN_H
35#ifdef HAVE_DLFCN_H
f23a6e1a 36#include <dlfcn.h>
53feaef5 37#else
38//#include <Riostream.h>
39#include <TSystem.h>
40#endif //HAVE_DLFCN_H
5df0cbb9 41//#include "AliHLTStdIncludes.h"
f23a6e1a 42#include "AliHLTComponentHandler.h"
43#include "AliHLTComponent.h"
44#include "AliHLTDataTypes.h"
f3506ea2 45#include "AliHLTModuleAgent.h"
5df0cbb9 46#include "TString.h"
f23a6e1a 47
b22e91eb 48/** ROOT macro for the implementation of ROOT specific class methods */
f23a6e1a 49ClassImp(AliHLTComponentHandler)
50
51AliHLTComponentHandler::AliHLTComponentHandler()
85869391 52 :
53 fComponentList(),
54 fScheduleList(),
55 fLibraryList(),
9ce4bf4a 56 fEnvironment(),
620fcee6 57 fOwnedComponents(),
58 fLibraryMode(kDynamic)
f23a6e1a 59{
70ed7d01 60 // see header file for class documentation
61 // or
62 // refer to README to build package
63 // or
64 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
85869391 65 memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
9ce4bf4a 66 AddStandardComponents();
f23a6e1a 67}
68
3cde846d 69AliHLTComponentHandler::AliHLTComponentHandler(AliHLTComponentEnvironment* pEnv)
70 :
298ef463 71 AliHLTLogging(),
3cde846d 72 fComponentList(),
73 fScheduleList(),
74 fLibraryList(),
75 fEnvironment(),
620fcee6 76 fOwnedComponents(),
77 fLibraryMode(kDynamic)
3cde846d 78{
70ed7d01 79 // see header file for class documentation
3cde846d 80 if (pEnv) {
81 memcpy(&fEnvironment, pEnv, sizeof(AliHLTComponentEnvironment));
fa274626 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
86 // for redirection
87 AliHLTLogging::Init(pEnv->fLoggingFunc);
88 }
f3506ea2 89 } else {
3cde846d 90 memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
f3506ea2 91 }
92 //#ifndef __DEBUG
93 //SetLocalLoggingLevel(kHLTLogError);
94 //#else
95 //SetLocalLoggingLevel(kHLTLogInfo);
96 //#endif
97
3cde846d 98 AddStandardComponents();
99}
100
f23a6e1a 101AliHLTComponentHandler::~AliHLTComponentHandler()
102{
70ed7d01 103 // see header file for class documentation
cbd84228 104 DeleteOwnedComponents();
a742f6f8 105 UnloadLibraries();
f23a6e1a 106}
107
fa760045 108int AliHLTComponentHandler::AnnounceVersion()
109{
70ed7d01 110 // see header file for class documentation
fa760045 111 int iResult=0;
112#ifdef PACKAGE_STRING
113 void HLTbaseCompileInfo( char*& date, char*& time);
114 char* date="";
115 char* time="";
116 HLTbaseCompileInfo(date, time);
117 if (!date) date="unknown";
118 if (!time) time="unknown";
b2065764 119 HLTImportant("%s build on %s (%s)", PACKAGE_STRING, date, time);
fa760045 120#else
b2065764 121 HLTImportant("ALICE High Level Trigger build on %s (%s) (embedded AliRoot build)", __DATE__, __TIME__);
fa760045 122#endif
123 return iResult;
124}
125
f3506ea2 126Int_t AliHLTComponentHandler::AddComponent(AliHLTComponent* pSample)
127{
128 // see header file for class documentation
129 Int_t iResult=0;
cbd84228 130 if (pSample==NULL) return -EINVAL;
f3506ea2 131 if ((iResult=RegisterComponent(pSample))>=0) {
cbd84228 132 //HLTDebug("sample %s (%p) managed by handler", pSample->GetComponentID(), pSample);
f3506ea2 133 fOwnedComponents.push_back(pSample);
134 }
135 return iResult;
136}
137
f23a6e1a 138Int_t AliHLTComponentHandler::RegisterComponent(AliHLTComponent* pSample)
139{
70ed7d01 140 // see header file for class documentation
f23a6e1a 141 Int_t iResult=0;
142 if (pSample) {
143 if (FindComponent(pSample->GetComponentID())==NULL) {
144 iResult=InsertComponent(pSample);
145 if (iResult>=0) {
85465857 146 HLTInfo("component %s registered", pSample->GetComponentID());
f23a6e1a 147 }
148 } else {
149 // component already registered
fa760045 150 HLTDebug("component %s already registered, skipped", pSample->GetComponentID());
f23a6e1a 151 iResult=-EEXIST;
152 }
153 } else {
154 iResult=-EINVAL;
155 }
156 return iResult;
157}
158
159int AliHLTComponentHandler::DeregisterComponent( const char* componentID )
160{
70ed7d01 161 // see header file for class documentation
5df0cbb9 162
53feaef5 163 int iResult=0;
164 if (componentID) {
5df0cbb9 165 HLTWarning("not yet implemented, please notify the developers if you need this function");
53feaef5 166 } else {
167 iResult=-EINVAL;
168 }
169 return iResult;
f23a6e1a 170}
171
172Int_t AliHLTComponentHandler::ScheduleRegister(AliHLTComponent* pSample)
173{
70ed7d01 174 // see header file for class documentation
f23a6e1a 175 Int_t iResult=0;
176 if (pSample) {
177 fScheduleList.push_back(pSample);
178 } else {
179 iResult=-EINVAL;
180 }
181 return iResult;
182}
183
b2065764 184int AliHLTComponentHandler::CreateComponent(const char* componentID, void* pEnvParam, int argc, const char** argv, AliHLTComponent*& component, const char* cdbPath )
f23a6e1a 185{
70ed7d01 186 // see header file for class documentation
f23a6e1a 187 int iResult=0;
188 if (componentID) {
189 AliHLTComponent* pSample=FindComponent(componentID);
190 if (pSample!=NULL) {
191 component=pSample->Spawn();
192 if (component) {
85465857 193 HLTDebug("component \"%s\" created (%p)", componentID, component);
703bcca6 194 component->InitCDB(cdbPath, this);
2d7ff710 195 if ((iResult=component->Init(&fEnvironment, pEnvParam, argc, argv))!=0) {
84645eb0 196 HLTError("Initialization of component \"%s\" failed with error %d", componentID, iResult);
197 delete component;
198 component=NULL;
199 }
5ec8e281 200 } else {
85465857 201 HLTError("can not spawn component \"%s\"", componentID);
5ec8e281 202 iResult=-ENOENT;
f23a6e1a 203 }
5ec8e281 204 } else {
85465857 205 HLTWarning("can not find component \"%s\"", componentID);
5ec8e281 206 iResult=-ENOENT;
f23a6e1a 207 }
208 } else {
209 iResult=-EINVAL;
210 }
211 return iResult;
212}
213
db16520a 214Int_t AliHLTComponentHandler::FindComponentIndex(const char* componentID)
f23a6e1a 215{
70ed7d01 216 // see header file for class documentation
f23a6e1a 217 Int_t iResult=0;
218 if (componentID) {
7bcd6cad 219 AliHLTComponentPList::iterator element=fComponentList.begin();
f23a6e1a 220 while (element!=fComponentList.end() && iResult>=0) {
221 if (strcmp(componentID, (*element)->GetComponentID())==0) {
222 break;
223 }
224 element++;
225 iResult++;
226 }
227 if (element==fComponentList.end()) iResult=-ENOENT;
228 } else {
229 iResult=-EINVAL;
230 }
231 return iResult;
232}
233
db16520a 234AliHLTComponent* AliHLTComponentHandler::FindComponent(const char* componentID)
f23a6e1a 235{
70ed7d01 236 // see header file for class documentation
f23a6e1a 237 AliHLTComponent* pSample=NULL;
238 Int_t index=FindComponentIndex(componentID);
239 if (index>=0) {
240 pSample=(AliHLTComponent*)fComponentList.at(index);
241 }
242 return pSample;
243}
244
245Int_t AliHLTComponentHandler::InsertComponent(AliHLTComponent* pSample)
246{
70ed7d01 247 // see header file for class documentation
f23a6e1a 248 Int_t iResult=0;
249 if (pSample!=NULL) {
250 fComponentList.push_back(pSample);
251 } else {
252 iResult=-EINVAL;
253 }
254 return iResult;
255}
256
70ed7d01 257void AliHLTComponentHandler::List()
258{
259 // see header file for class documentation
7bcd6cad 260 AliHLTComponentPList::iterator element=fComponentList.begin();
f23a6e1a 261 int index=0;
262 while (element!=fComponentList.end()) {
85465857 263 HLTInfo("%d. %s", index++, (*element++)->GetComponentID());
f23a6e1a 264 }
265}
266
f3506ea2 267int AliHLTComponentHandler::HasOutputData( const char* componentID)
268{
269 // see header file for class documentation
270 int iResult=0;
271 AliHLTComponent* pSample=FindComponent(componentID);
272 if (pSample) {
273 AliHLTComponent::TComponentType ct=AliHLTComponent::kUnknown;
274 ct=pSample->GetComponentType();
275 iResult=(ct==AliHLTComponent::kSource || ct==AliHLTComponent::kProcessor);
276 } else {
277 iResult=-ENOENT;
278 }
279 return iResult;
280}
281
70ed7d01 282void AliHLTComponentHandler::SetEnvironment(AliHLTComponentEnvironment* pEnv)
283{
284 // see header file for class documentation
f23a6e1a 285 if (pEnv) {
286 memcpy(&fEnvironment, pEnv, sizeof(AliHLTComponentEnvironment));
fa274626 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
291 // for redirection
292 AliHLTLogging::Init(fEnvironment.fLoggingFunc);
293 }
f23a6e1a 294 }
295}
296
dba03d72 297AliHLTComponentHandler::TLibraryMode AliHLTComponentHandler::SetLibraryMode(TLibraryMode mode)
298{
299 // see header file for class documentation
300 TLibraryMode old=fLibraryMode;
301 fLibraryMode=mode;
302 return old;
303}
304
f3506ea2 305int AliHLTComponentHandler::LoadLibrary( const char* libraryPath, int bActivateAgents)
f23a6e1a 306{
70ed7d01 307 // see header file for class documentation
f23a6e1a 308 int iResult=0;
309 if (libraryPath) {
f3506ea2 310 // first activate all agents which are already loaded
311 if (bActivateAgents) ActivateAgents();
312
313 // set the global component handler for static component registration
f23a6e1a 314 AliHLTComponent::SetGlobalComponentHandler(this);
f3506ea2 315
c215072c 316 AliHLTLibHandle hLib;
85f0cede 317 const char* loadtype="";
53feaef5 318#ifdef HAVE_DLFCN_H
319 // use interface to the dynamic linking loader
13398559 320
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
324 /*try*/ {
f3506ea2 325 hLib.fHandle=dlopen(libraryPath, RTLD_NOW);
326 loadtype="dlopen";
327 }
13398559 328 /*
f3506ea2 329 catch (...) {
330 // error message printed further down
331 loadtype="dlopen exeption";
332 }
13398559 333 */
53feaef5 334#else
335 // use ROOT dynamic loader
c215072c 336 // check if the library was already loaded, as Load returns
337 // 'failure' if the library was already loaded
13398559 338 /*try*/ {
85f0cede 339 AliHLTLibHandle* pLib=FindLibrary(libraryPath);
340 if (pLib) {
5df0cbb9 341 int* pRootHandle=reinterpret_cast<int*>(pLib->fHandle);
c215072c 342 (*pRootHandle)++;
343 HLTDebug("instance %d of library %s loaded", (*pRootHandle), libraryPath);
5df0cbb9 344 hLib.fHandle=pRootHandle;
c215072c 345 }
346
62ff1e23 347 if (hLib.fHandle==NULL && gSystem->Load(libraryPath)>=0) {
c215072c 348 int* pRootHandle=new int;
349 if (pRootHandle) *pRootHandle=1;
5df0cbb9 350 hLib.fHandle=pRootHandle;
85f0cede 351 //HLTDebug("library %s loaded via gSystem", libraryPath);
53feaef5 352 }
85f0cede 353 loadtype="gSystem";
f3506ea2 354 }
13398559 355 /*
f3506ea2 356 catch (...) {
357 // error message printed further down
358 loadtype="gSystem exeption";
359 }
13398559 360 */
53feaef5 361#endif //HAVE_DLFCN_H
5df0cbb9 362 if (hLib.fHandle!=NULL) {
c215072c 363 // create TString object to store library path and use pointer as handle
5df0cbb9 364 hLib.fName=new TString(libraryPath);
dba03d72 365 hLib.fMode=fLibraryMode;
b2065764 366 HLTImportant("library %s loaded (%s%s)", libraryPath, hLib.fMode==kStatic?"persistent, ":"", loadtype);
c215072c 367 fLibraryList.insert(fLibraryList.begin(), hLib);
842fd76a 368 typedef void (*CompileInfo)( char*& date, char*& time);
369 CompileInfo fctInfo=(CompileInfo)FindSymbol(libraryPath, "CompileInfo");
370 if (fctInfo) {
371 char* date="";
372 char* time="";
373 (*fctInfo)(date, time);
374 if (!date) date="unknown";
375 if (!time) time="unknown";
b2065764 376 HLTImportant("build on %s (%s)", date, time);
842fd76a 377 } else {
b2065764 378 HLTImportant("no build info available (possible AliRoot embedded build)");
842fd76a 379 }
f3506ea2 380
381 // static registration of components when library is loaded
9ce4bf4a 382 iResult=RegisterScheduledComponents();
f3506ea2 383
f23a6e1a 384 } else {
f3506ea2 385 HLTError("can not load library %s (%s)", libraryPath, loadtype);
53feaef5 386#ifdef HAVE_DLFCN_H
85465857 387 HLTError("dlopen error: %s", dlerror());
53feaef5 388#endif //HAVE_DLFCN_H
0fe88043 389#ifdef __APPLE__
390 iResult=-EFTYPE;
391#else
f23a6e1a 392 iResult=-ELIBACC;
0fe88043 393#endif
f23a6e1a 394 }
2bbbadd1 395 AliHLTComponent::UnsetGlobalComponentHandler();
f3506ea2 396
397 if (iResult>=0) {
398 // alternative dynamic registration by library agents
399 // !!! has to be done after UnsetGlobalComponentHandler
400 if (bActivateAgents) ActivateAgents();
401 }
402
f23a6e1a 403 } else {
404 iResult=-EINVAL;
405 }
406 return iResult;
407}
408
409int AliHLTComponentHandler::UnloadLibrary( const char* libraryPath )
410{
70ed7d01 411 // see header file for class documentation
f23a6e1a 412 int iResult=0;
53feaef5 413 if (libraryPath) {
a742f6f8 414 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
415 while (element!=fLibraryList.end()) {
5df0cbb9 416 TString* pName=reinterpret_cast<TString*>((*element).fName);
a742f6f8 417 if (pName->CompareTo(libraryPath)==0) {
418 UnloadLibrary(*element);
419 fLibraryList.erase(element);
420 break;
421 }
422 element++;
423 }
53feaef5 424 } else {
425 iResult=-EINVAL;
426 }
f23a6e1a 427 return iResult;
428}
429
a742f6f8 430int AliHLTComponentHandler::UnloadLibrary(AliHLTComponentHandler::AliHLTLibHandle &handle)
f23a6e1a 431{
70ed7d01 432 // see header file for class documentation
f23a6e1a 433 int iResult=0;
a742f6f8 434 fgAliLoggingFunc=NULL;
5df0cbb9 435 TString* pName=reinterpret_cast<TString*>(handle.fName);
dba03d72 436 if (handle.fMode!=kStatic) {
53feaef5 437#ifdef HAVE_DLFCN_H
13398559 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
441
442 /*try*/ {
f3506ea2 443 dlclose(handle.fHandle);
444 }
13398559 445 /*
f3506ea2 446 catch (...) {
447 HLTError("exeption caught during dlclose of library %s", pName!=NULL?pName->Data():"");
448 }
13398559 449 */
53feaef5 450#else
5df0cbb9 451 int* pCount=reinterpret_cast<int*>(handle.fHandle);
a742f6f8 452 if (--(*pCount)==0) {
c215072c 453 if (pName) {
a7222a6d 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
459 * used.
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
465 * deleted)
466 * root [0] AliHLTSystem* gHLT=new AliHLTSystem
467 *
468 * For that reason, the libraries are not unloaded here, even though there
469 * will be memory leaks.
a742f6f8 470 gSystem->Unload(pName->Data());
a7222a6d 471 */
a742f6f8 472 }
473 else {
474 HLTError("missing library name, can not unload");
c215072c 475 }
a742f6f8 476 delete pCount;
477 }
478#endif //HAVE_DLFCN_H
a742f6f8 479 if (pName) {
480 HLTDebug("unload library %s", pName->Data());
a742f6f8 481 } else {
482 HLTWarning("missing name for unloaded library");
483 }
dba03d72 484 }
485 handle.fName=NULL;
486 handle.fHandle=NULL;
487 if (pName) {
488 delete pName;
489 }
a742f6f8 490 pName=NULL;
491 return iResult;
492}
493
494int AliHLTComponentHandler::UnloadLibraries()
495{
496 // see header file for class documentation
497 int iResult=0;
498 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
499 while (element!=fLibraryList.end()) {
500 UnloadLibrary(*element);
c215072c 501 fLibraryList.erase(element);
502 element=fLibraryList.begin();
f23a6e1a 503 }
504 return iResult;
505}
9ce4bf4a 506
85f0cede 507void* AliHLTComponentHandler::FindSymbol(const char* library, const char* symbol)
508{
509 // see header file for class documentation
510 AliHLTLibHandle* hLib=FindLibrary(library);
511 if (hLib==NULL) return NULL;
512 void* pFunc=NULL;
513#ifdef HAVE_DLFCN_H
5df0cbb9 514 pFunc=dlsym(hLib->fHandle, symbol);
85f0cede 515#else
5df0cbb9 516 TString* name=reinterpret_cast<TString*>(hLib->fName);
85f0cede 517 pFunc=gSystem->DynFindSymbol(name->Data(), symbol);
518#endif
519 return pFunc;
520}
521
522AliHLTComponentHandler::AliHLTLibHandle* AliHLTComponentHandler::FindLibrary(const char* library)
523{
524 // see header file for class documentation
525 AliHLTLibHandle* hLib=NULL;
526 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
527 while (element!=fLibraryList.end()) {
5df0cbb9 528 TString* name=reinterpret_cast<TString*>((*element).fName);
85f0cede 529 if (name->CompareTo(library)==0) {
530 hLib=&(*element);
531 break;
532 }
533 element++;
534 }
535 return hLib;
536}
537
9ce4bf4a 538int AliHLTComponentHandler::AddStandardComponents()
539{
70ed7d01 540 // see header file for class documentation
9ce4bf4a 541 int iResult=0;
542 AliHLTComponent::SetGlobalComponentHandler(this);
9ce4bf4a 543 AliHLTComponent::UnsetGlobalComponentHandler();
544 iResult=RegisterScheduledComponents();
545 return iResult;
546}
547
548int AliHLTComponentHandler::RegisterScheduledComponents()
549{
70ed7d01 550 // see header file for class documentation
9ce4bf4a 551 int iResult=0;
7bcd6cad 552 AliHLTComponentPList::iterator element=fScheduleList.begin();
9ce4bf4a 553 int iLocalResult=0;
554 while (element!=fScheduleList.end()) {
555 iLocalResult=RegisterComponent(*element);
556 if (iResult==0) iResult=iLocalResult;
557 fScheduleList.erase(element);
558 element=fScheduleList.begin();
559 }
560 return iResult;
561}
562
f3506ea2 563int AliHLTComponentHandler::ActivateAgents(const AliHLTModuleAgent** blackList, int size)
564{
565 // see header file for class documentation
566 int iResult=0;
567 AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent();
568 while (pAgent && iResult>=0) {
569 if (blackList) {
570 int i=0;
571 for (; i<size; i++) {
572 if (blackList[i]==pAgent) break;
573 }
574 if (i<size) {
575 // this agent was in the list
576 pAgent=AliHLTModuleAgent::GetNextAgent();
577 continue;
578 }
579 }
580
581 pAgent->ActivateComponentHandler(this);
582 pAgent=AliHLTModuleAgent::GetNextAgent();
583 }
584 return iResult;
585}
586
cbd84228 587int AliHLTComponentHandler::DeleteOwnedComponents()
9ce4bf4a 588{
70ed7d01 589 // see header file for class documentation
9ce4bf4a 590 int iResult=0;
7bcd6cad 591 AliHLTComponentPList::iterator element=fOwnedComponents.begin();
f3506ea2 592 while (element!=fOwnedComponents.end()) {
5df0cbb9 593 //DeregisterComponent((*element)->GetComponentID());
13398559 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
597 /*try*/ {
cbd84228 598 delete *element;
599 }
13398559 600 /*
cbd84228 601 catch (...) {
602 HLTError("delete managed sample %p", *element);
603 }
13398559 604 */
f3506ea2 605 fOwnedComponents.erase(element);
606 element=fOwnedComponents.begin();
9ce4bf4a 607 }
608 return iResult;
609}