]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/BASE/AliHLTComponentHandler.cxx
minor corrections in documentation and build system, bugfix in agent to return config...
[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
0c0c9d99 25#if __GNUC__>= 3
f23a6e1a 26using namespace std;
27#endif
53feaef5 28//#undef HAVE_DLFCN_H
29#ifdef HAVE_DLFCN_H
f23a6e1a 30#include <dlfcn.h>
53feaef5 31#else
32//#include <Riostream.h>
33#include <TSystem.h>
34#endif //HAVE_DLFCN_H
5df0cbb9 35//#include "AliHLTStdIncludes.h"
f23a6e1a 36#include "AliHLTComponentHandler.h"
37#include "AliHLTComponent.h"
38#include "AliHLTDataTypes.h"
5df0cbb9 39//#include "AliHLTSystem.h"
40#include "TString.h"
f23a6e1a 41
9ce4bf4a 42// the standard components
242bb794 43// #include "AliHLTFilePublisher.h"
a742f6f8 44// #include "AliHLTFileWriter.h"
242bb794 45// #include "AliHLTRootFilePublisherComponent.h"
46// #include "AliHLTRootFileWriterComponent.h"
9ce4bf4a 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(),
57 fStandardList()
f23a6e1a 58{
70ed7d01 59 // see header file for class documentation
60 // or
61 // refer to README to build package
62 // or
63 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
85869391 64 memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
9ce4bf4a 65 AddStandardComponents();
f23a6e1a 66}
67
3cde846d 68AliHLTComponentHandler::AliHLTComponentHandler(AliHLTComponentEnvironment* pEnv)
69 :
70 fComponentList(),
71 fScheduleList(),
72 fLibraryList(),
73 fEnvironment(),
74 fStandardList()
75{
70ed7d01 76 // see header file for class documentation
3cde846d 77 if (pEnv) {
78 memcpy(&fEnvironment, pEnv, sizeof(AliHLTComponentEnvironment));
fa274626 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
83 // for redirection
84 AliHLTLogging::Init(pEnv->fLoggingFunc);
85 }
3cde846d 86 } else
87 memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
88 AddStandardComponents();
89}
90
5df0cbb9 91AliHLTComponentHandler::AliHLTComponentHandler(const AliHLTComponentHandler&)
92 :
93 fComponentList(),
94 fScheduleList(),
95 fLibraryList(),
96 fEnvironment(),
97 fStandardList()
98{
99 // see header file for class documentation
100 HLTFatal("copy constructor untested");
101}
102
103AliHLTComponentHandler& AliHLTComponentHandler::operator=(const AliHLTComponentHandler&)
104{
105 // see header file for class documentation
106 HLTFatal("assignment operator untested");
107 return *this;
108}
109
f23a6e1a 110AliHLTComponentHandler::~AliHLTComponentHandler()
111{
70ed7d01 112 // see header file for class documentation
9ce4bf4a 113 DeleteStandardComponents();
a742f6f8 114 UnloadLibraries();
f23a6e1a 115}
116
fa760045 117int AliHLTComponentHandler::AnnounceVersion()
118{
70ed7d01 119 // see header file for class documentation
fa760045 120 int iResult=0;
121#ifdef PACKAGE_STRING
122 void HLTbaseCompileInfo( char*& date, char*& time);
123 char* date="";
124 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);
129#else
a742f6f8 130 HLTInfo("ALICE High Level Trigger build on %s (%s) (embedded AliRoot build)", __DATE__, __TIME__);
fa760045 131#endif
132 return iResult;
133}
134
f23a6e1a 135Int_t AliHLTComponentHandler::RegisterComponent(AliHLTComponent* pSample)
136{
70ed7d01 137 // see header file for class documentation
f23a6e1a 138 Int_t iResult=0;
139 if (pSample) {
140 if (FindComponent(pSample->GetComponentID())==NULL) {
141 iResult=InsertComponent(pSample);
142 if (iResult>=0) {
85465857 143 HLTInfo("component %s registered", pSample->GetComponentID());
f23a6e1a 144 }
145 } else {
146 // component already registered
fa760045 147 HLTDebug("component %s already registered, skipped", pSample->GetComponentID());
f23a6e1a 148 iResult=-EEXIST;
149 }
150 } else {
151 iResult=-EINVAL;
152 }
153 return iResult;
154}
155
156int AliHLTComponentHandler::DeregisterComponent( const char* componentID )
157{
70ed7d01 158 // see header file for class documentation
5df0cbb9 159
53feaef5 160 int iResult=0;
161 if (componentID) {
5df0cbb9 162 HLTWarning("not yet implemented, please notify the developers if you need this function");
53feaef5 163 } else {
164 iResult=-EINVAL;
165 }
166 return iResult;
f23a6e1a 167}
168
169Int_t AliHLTComponentHandler::ScheduleRegister(AliHLTComponent* pSample)
170{
70ed7d01 171 // see header file for class documentation
f23a6e1a 172 Int_t iResult=0;
173 if (pSample) {
174 fScheduleList.push_back(pSample);
175 } else {
176 iResult=-EINVAL;
177 }
178 return iResult;
179}
180
2d7ff710 181int AliHLTComponentHandler::CreateComponent(const char* componentID, void* pEnvParam, int argc, const char** argv, AliHLTComponent*& component )
f23a6e1a 182{
70ed7d01 183 // see header file for class documentation
f23a6e1a 184 int iResult=0;
185 if (componentID) {
186 AliHLTComponent* pSample=FindComponent(componentID);
187 if (pSample!=NULL) {
188 component=pSample->Spawn();
189 if (component) {
85465857 190 HLTDebug("component \"%s\" created (%p)", componentID, component);
2d7ff710 191 if ((iResult=component->Init(&fEnvironment, pEnvParam, argc, argv))!=0) {
84645eb0 192 HLTError("Initialization of component \"%s\" failed with error %d", componentID, iResult);
193 delete component;
194 component=NULL;
195 }
5ec8e281 196 } else {
85465857 197 HLTError("can not spawn component \"%s\"", componentID);
5ec8e281 198 iResult=-ENOENT;
f23a6e1a 199 }
5ec8e281 200 } else {
85465857 201 HLTWarning("can not find component \"%s\"", componentID);
5ec8e281 202 iResult=-ENOENT;
f23a6e1a 203 }
204 } else {
205 iResult=-EINVAL;
206 }
207 return iResult;
208}
209
db16520a 210Int_t AliHLTComponentHandler::FindComponentIndex(const char* componentID)
f23a6e1a 211{
70ed7d01 212 // see header file for class documentation
f23a6e1a 213 Int_t iResult=0;
214 if (componentID) {
215 vector<AliHLTComponent*>::iterator element=fComponentList.begin();
216 while (element!=fComponentList.end() && iResult>=0) {
217 if (strcmp(componentID, (*element)->GetComponentID())==0) {
218 break;
219 }
220 element++;
221 iResult++;
222 }
223 if (element==fComponentList.end()) iResult=-ENOENT;
224 } else {
225 iResult=-EINVAL;
226 }
227 return iResult;
228}
229
db16520a 230AliHLTComponent* AliHLTComponentHandler::FindComponent(const char* componentID)
f23a6e1a 231{
70ed7d01 232 // see header file for class documentation
f23a6e1a 233 AliHLTComponent* pSample=NULL;
234 Int_t index=FindComponentIndex(componentID);
235 if (index>=0) {
236 pSample=(AliHLTComponent*)fComponentList.at(index);
237 }
238 return pSample;
239}
240
241Int_t AliHLTComponentHandler::InsertComponent(AliHLTComponent* pSample)
242{
70ed7d01 243 // see header file for class documentation
f23a6e1a 244 Int_t iResult=0;
245 if (pSample!=NULL) {
246 fComponentList.push_back(pSample);
247 } else {
248 iResult=-EINVAL;
249 }
250 return iResult;
251}
252
70ed7d01 253void AliHLTComponentHandler::List()
254{
255 // see header file for class documentation
f23a6e1a 256 vector<AliHLTComponent*>::iterator element=fComponentList.begin();
257 int index=0;
258 while (element!=fComponentList.end()) {
85465857 259 HLTInfo("%d. %s", index++, (*element++)->GetComponentID());
f23a6e1a 260 }
261}
262
70ed7d01 263void AliHLTComponentHandler::SetEnvironment(AliHLTComponentEnvironment* pEnv)
264{
265 // see header file for class documentation
f23a6e1a 266 if (pEnv) {
267 memcpy(&fEnvironment, pEnv, sizeof(AliHLTComponentEnvironment));
fa274626 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
272 // for redirection
273 AliHLTLogging::Init(fEnvironment.fLoggingFunc);
274 }
f23a6e1a 275 }
276}
277
278int AliHLTComponentHandler::LoadLibrary( const char* libraryPath )
279{
70ed7d01 280 // see header file for class documentation
f23a6e1a 281 int iResult=0;
282 if (libraryPath) {
283 AliHLTComponent::SetGlobalComponentHandler(this);
c215072c 284 AliHLTLibHandle hLib;
85f0cede 285 const char* loadtype="";
53feaef5 286#ifdef HAVE_DLFCN_H
287 // use interface to the dynamic linking loader
5df0cbb9 288 hLib.fHandle=dlopen(libraryPath, RTLD_NOW);
85f0cede 289 loadtype="dlopen";
53feaef5 290#else
291 // use ROOT dynamic loader
c215072c 292 // check if the library was already loaded, as Load returns
293 // 'failure' if the library was already loaded
85f0cede 294 AliHLTLibHandle* pLib=FindLibrary(libraryPath);
295 if (pLib) {
5df0cbb9 296 int* pRootHandle=reinterpret_cast<int*>(pLib->fHandle);
c215072c 297 (*pRootHandle)++;
298 HLTDebug("instance %d of library %s loaded", (*pRootHandle), libraryPath);
5df0cbb9 299 hLib.fHandle=pRootHandle;
c215072c 300 }
301
5df0cbb9 302 if (hLib.fHandle==NULL && gSystem->Load(libraryPath)==0) {
c215072c 303 int* pRootHandle=new int;
304 if (pRootHandle) *pRootHandle=1;
5df0cbb9 305 hLib.fHandle=pRootHandle;
85f0cede 306 //HLTDebug("library %s loaded via gSystem", libraryPath);
53feaef5 307 }
85f0cede 308 loadtype="gSystem";
53feaef5 309#endif //HAVE_DLFCN_H
5df0cbb9 310 if (hLib.fHandle!=NULL) {
c215072c 311 // create TString object to store library path and use pointer as handle
5df0cbb9 312 hLib.fName=new TString(libraryPath);
85f0cede 313 HLTInfo("library %s loaded (%s)", libraryPath, loadtype);
c215072c 314 fLibraryList.insert(fLibraryList.begin(), hLib);
9ce4bf4a 315 iResult=RegisterScheduledComponents();
f23a6e1a 316 } else {
85465857 317 HLTError("can not load library %s", libraryPath);
53feaef5 318#ifdef HAVE_DLFCN_H
85465857 319 HLTError("dlopen error: %s", dlerror());
53feaef5 320#endif //HAVE_DLFCN_H
0fe88043 321#ifdef __APPLE__
322 iResult=-EFTYPE;
323#else
f23a6e1a 324 iResult=-ELIBACC;
0fe88043 325#endif
f23a6e1a 326 }
2bbbadd1 327 AliHLTComponent::UnsetGlobalComponentHandler();
f23a6e1a 328 } else {
329 iResult=-EINVAL;
330 }
331 return iResult;
332}
333
334int AliHLTComponentHandler::UnloadLibrary( const char* libraryPath )
335{
70ed7d01 336 // see header file for class documentation
f23a6e1a 337 int iResult=0;
53feaef5 338 if (libraryPath) {
a742f6f8 339 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
340 while (element!=fLibraryList.end()) {
5df0cbb9 341 TString* pName=reinterpret_cast<TString*>((*element).fName);
a742f6f8 342 if (pName->CompareTo(libraryPath)==0) {
343 UnloadLibrary(*element);
344 fLibraryList.erase(element);
345 break;
346 }
347 element++;
348 }
53feaef5 349 } else {
350 iResult=-EINVAL;
351 }
f23a6e1a 352 return iResult;
353}
354
a742f6f8 355int AliHLTComponentHandler::UnloadLibrary(AliHLTComponentHandler::AliHLTLibHandle &handle)
f23a6e1a 356{
70ed7d01 357 // see header file for class documentation
f23a6e1a 358 int iResult=0;
a742f6f8 359 fgAliLoggingFunc=NULL;
5df0cbb9 360 TString* pName=reinterpret_cast<TString*>(handle.fName);
53feaef5 361#ifdef HAVE_DLFCN_H
5df0cbb9 362 dlclose(handle.fHandle);
53feaef5 363#else
5df0cbb9 364 int* pCount=reinterpret_cast<int*>(handle.fHandle);
a742f6f8 365 if (--(*pCount)==0) {
c215072c 366 if (pName) {
a7222a6d 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
372 * used.
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
378 * deleted)
379 * root [0] AliHLTSystem* gHLT=new AliHLTSystem
380 *
381 * For that reason, the libraries are not unloaded here, even though there
382 * will be memory leaks.
a742f6f8 383 gSystem->Unload(pName->Data());
a7222a6d 384 */
a742f6f8 385 }
386 else {
387 HLTError("missing library name, can not unload");
c215072c 388 }
a742f6f8 389 delete pCount;
390 }
391#endif //HAVE_DLFCN_H
5df0cbb9 392 handle.fName=NULL;
393 handle.fHandle=NULL;
a742f6f8 394 if (pName) {
395 HLTDebug("unload library %s", pName->Data());
396 delete pName;
397 } else {
398 HLTWarning("missing name for unloaded library");
399 }
400 pName=NULL;
401 return iResult;
402}
403
404int AliHLTComponentHandler::UnloadLibraries()
405{
406 // see header file for class documentation
407 int iResult=0;
408 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
409 while (element!=fLibraryList.end()) {
410 UnloadLibrary(*element);
c215072c 411 fLibraryList.erase(element);
412 element=fLibraryList.begin();
f23a6e1a 413 }
414 return iResult;
415}
9ce4bf4a 416
85f0cede 417void* AliHLTComponentHandler::FindSymbol(const char* library, const char* symbol)
418{
419 // see header file for class documentation
420 AliHLTLibHandle* hLib=FindLibrary(library);
421 if (hLib==NULL) return NULL;
422 void* pFunc=NULL;
423#ifdef HAVE_DLFCN_H
5df0cbb9 424 pFunc=dlsym(hLib->fHandle, symbol);
85f0cede 425#else
5df0cbb9 426 TString* name=reinterpret_cast<TString*>(hLib->fName);
85f0cede 427 pFunc=gSystem->DynFindSymbol(name->Data(), symbol);
428#endif
429 return pFunc;
430}
431
432AliHLTComponentHandler::AliHLTLibHandle* AliHLTComponentHandler::FindLibrary(const char* library)
433{
434 // see header file for class documentation
435 AliHLTLibHandle* hLib=NULL;
436 vector<AliHLTLibHandle>::iterator element=fLibraryList.begin();
437 while (element!=fLibraryList.end()) {
5df0cbb9 438 TString* name=reinterpret_cast<TString*>((*element).fName);
85f0cede 439 if (name->CompareTo(library)==0) {
440 hLib=&(*element);
441 break;
442 }
443 element++;
444 }
445 return hLib;
446}
447
9ce4bf4a 448int AliHLTComponentHandler::AddStandardComponents()
449{
70ed7d01 450 // see header file for class documentation
9ce4bf4a 451 int iResult=0;
452 AliHLTComponent::SetGlobalComponentHandler(this);
242bb794 453// fStandardList.push_back(new AliHLTFilePublisher);
a742f6f8 454// fStandardList.push_back(new AliHLTFileWriter);
242bb794 455// fStandardList.push_back(new AliHLTRootFilePublisherComponent);
456// fStandardList.push_back(new AliHLTRootFileWriterComponent);
9ce4bf4a 457 AliHLTComponent::UnsetGlobalComponentHandler();
458 iResult=RegisterScheduledComponents();
459 return iResult;
460}
461
462int AliHLTComponentHandler::RegisterScheduledComponents()
463{
70ed7d01 464 // see header file for class documentation
9ce4bf4a 465 int iResult=0;
466 vector<AliHLTComponent*>::iterator element=fScheduleList.begin();
467 int iLocalResult=0;
468 while (element!=fScheduleList.end()) {
469 iLocalResult=RegisterComponent(*element);
470 if (iResult==0) iResult=iLocalResult;
471 fScheduleList.erase(element);
472 element=fScheduleList.begin();
473 }
474 return iResult;
475}
476
477int AliHLTComponentHandler::DeleteStandardComponents()
478{
70ed7d01 479 // see header file for class documentation
9ce4bf4a 480 int iResult=0;
481 vector<AliHLTComponent*>::iterator element=fStandardList.begin();
482 while (element!=fStandardList.end()) {
5df0cbb9 483 //DeregisterComponent((*element)->GetComponentID());
9ce4bf4a 484 delete(*element);
485 fStandardList.erase(element);
486 element=fStandardList.begin();
487 }
488 return iResult;
489}