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 * for The ALICE HLT Project. *
10 * Permission to use, copy, modify and distribute this software and its *
11 * documentation strictly for non-commercial purposes is hereby granted *
12 * without fee, provided that the above copyright notice appears in all *
13 * copies and that both the copyright notice and this permission notice *
14 * appear in the supporting documentation. The authors make no claims *
15 * about the suitability of this software for any purpose. It is *
16 * provided "as is" without express or implied warranty. *
17 **************************************************************************/
19 /** @file AliHLTSystem.cxx
20 @author Matthias Richter
22 @brief Implementation of HLT module management.
29 #include "AliHLTStdIncludes.h"
30 #include "AliHLTSystem.h"
31 #include "AliHLTComponentHandler.h"
32 #include "AliHLTComponent.h"
33 #include "AliHLTConfiguration.h"
34 #include "AliHLTConfigurationHandler.h"
35 #include "AliHLTTask.h"
36 #include "AliHLTModuleAgent.h"
37 #include "AliHLTOfflineInterface.h"
38 #include <TObjArray.h>
39 #include <TObjString.h>
41 #include <TStopwatch.h>
43 /** ROOT macro for the implementation of ROOT specific class methods */
44 ClassImp(AliHLTSystem)
46 AliHLTSystem::AliHLTSystem()
48 fpComponentHandler(new AliHLTComponentHandler()),
49 fpConfigurationHandler(new AliHLTConfigurationHandler()),
53 // see header file for class documentation
55 // refer to README to build package
57 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
59 if (fgNofInstances++>0)
60 HLTWarning("multiple instances of AliHLTSystem, you should not use more than one at a time");
62 SetGlobalLoggingLevel(kHLTLogDefault);
63 if (fpComponentHandler) {
64 AliHLTComponentEnvironment env;
65 memset(&env, 0, sizeof(AliHLTComponentEnvironment));
66 env.fAllocMemoryFunc=AliHLTSystem::AllocMemory;
67 env.fLoggingFunc=NULL;
68 AliHLTComponentLogSeverity loglevel=fpComponentHandler->GetLocalLoggingLevel();
69 fpComponentHandler->SetLocalLoggingLevel(kHLTLogError);
70 fpComponentHandler->SetEnvironment(&env);
71 fpComponentHandler->LoadLibrary("libAliHLTUtil.so");
72 fgAliLoggingFunc=(AliHLTLogging::AliHLTDynamicMessage)fpComponentHandler->FindSymbol("libAliHLTUtil.so", "AliDynamicMessage");
73 fpComponentHandler->SetLocalLoggingLevel(loglevel);
74 if (fgAliLoggingFunc==NULL) {
75 HLTError("symbol lookp failure: can not find AliDynamicMessage, switching to HLT logging system");
77 fpComponentHandler->AnnounceVersion();
79 HLTFatal("can not create Component Handler");
81 if (fpConfigurationHandler) {
82 AliHLTConfiguration::GlobalInit(fpConfigurationHandler);
84 HLTFatal("can not create Configuration Handler");
88 AliHLTSystem::AliHLTSystem(const AliHLTSystem&)
91 fpComponentHandler(NULL),
92 fpConfigurationHandler(NULL),
96 // see header file for class documentation
97 if (fgNofInstances++>0)
98 HLTWarning("multiple instances of AliHLTSystem, you should not use more than one at a time");
100 HLTFatal("copy constructor untested");
103 AliHLTSystem& AliHLTSystem::operator=(const AliHLTSystem&)
105 // see header file for class documentation
106 HLTFatal("assignment operator untested");
110 AliHLTSystem::~AliHLTSystem()
112 // see header file for class documentation
115 AliHLTConfiguration::GlobalDeinit(fpConfigurationHandler);
116 if (fpConfigurationHandler) {
117 delete fpConfigurationHandler;
119 fpConfigurationHandler=NULL;
121 if (fpComponentHandler) {
122 delete fpComponentHandler;
124 fpComponentHandler=NULL;
127 int AliHLTSystem::fgNofInstances=0;
129 int AliHLTSystem::AddConfiguration(AliHLTConfiguration* pConf)
131 // see header file for class documentation
140 int AliHLTSystem::InsertConfiguration(AliHLTConfiguration* pConf, AliHLTConfiguration* pPrec)
142 // see header file for class documentation
154 int AliHLTSystem::DeleteConfiguration(AliHLTConfiguration* pConf)
156 // see header file for class documentation
165 int AliHLTSystem::BuildTaskList(const char* id)
167 // see header file for class documentation
170 if (fpConfigurationHandler) {
171 AliHLTConfiguration* pConf=fpConfigurationHandler->FindConfiguration(id);
173 iResult=BuildTaskList(pConf);
175 HLTError("unknown configuration \"%s\"", id);
187 int AliHLTSystem::BuildTaskList(AliHLTConfiguration* pConf)
189 // see header file for class documentation
192 AliHLTTask* pTask=NULL;
193 if ((pTask=FindTask(pConf->GetName()))!=NULL) {
194 if (pTask->GetConf()!=pConf) {
195 HLTError("configuration missmatch, there is already a task with configuration name \"%s\", but it is different. Most likely configuration %p is not registered properly", pConf->GetName(), pConf);
199 } else if (pConf->SourcesResolved(1)!=1) {
200 HLTError("configuration \"%s\" has unresolved sources, aborting ...", pConf->GetName());
203 pTask=new AliHLTTask(pConf);
209 // check for circular dependencies
210 if ((iResult=pConf->FollowDependency(pConf->GetName()))>0) {
211 HLTError("detected circular dependency for configuration \"%s\"", pTask->GetName());
212 pTask->PrintDependencyTree(pTask->GetName(), 1/*use the configuration list*/);
213 HLTError("aborted ...");
217 // check whether all dependencies are already in the task list
218 // create the missing ones
219 // this step is an iterative process which calls this function again for the missing
220 // configurations, in order to avoid the currently processed task to be created
221 // again it is added to the list temporarily and removed afterwards
222 // This is of high importance to preserve the order of the tasks. Furthermore, the
223 // InsertTask method has to be used in order to set all the cross links right
224 fTaskList.Add(pTask);
225 AliHLTConfiguration* pDep=pConf->GetFirstSource();
226 while (pDep!=NULL && iResult>=0) {
227 if (FindTask(pDep->GetName())==NULL) {
228 iResult=BuildTaskList(pDep);
230 pDep=pConf->GetNextSource();
232 // remove the temporarily added task
233 fTaskList.Remove(pTask);
235 // insert the task and set the cross-links
237 iResult=InsertTask(pTask);
250 int AliHLTSystem::CleanTaskList()
252 // see header file for class documentation
255 while ((lnk=fTaskList.LastLink())!=NULL) {
256 delete (lnk->GetObject());
257 fTaskList.Remove(lnk);
262 int AliHLTSystem::InsertTask(AliHLTTask* pTask)
264 // see header file for class documentation
266 TObjLink *lnk = NULL;
267 if ((iResult=pTask->CheckDependencies())>0)
268 lnk=fTaskList.FirstLink();
269 while (lnk && iResult>0) {
270 AliHLTTask* pCurr = (AliHLTTask*)lnk->GetObject();
271 //HLTDebug("checking \"%s\"", pCurr->GetName());
272 iResult=pTask->Depends(pCurr);
274 iResult=pTask->SetDependency(pCurr);
275 pCurr->SetTarget(pTask);
276 HLTDebug("set dependency \"%s\" for configuration \"%s\"", pCurr->GetName(), pTask->GetName());
278 if (pCurr->Depends(pTask)) {
279 // circular dependency
280 HLTError("circular dependency: can not resolve dependencies for configuration \"%s\"", pTask->GetName());
282 } else if ((iResult=pTask->CheckDependencies())>0) {
288 fTaskList.AddAfter(lnk, pTask);
290 fTaskList.AddFirst(pTask);
292 HLTDebug("task \"%s\" (%p) inserted (size %d)", pTask->GetName(), pTask, sizeof(AliHLTTask));
293 } else if (iResult>0) {
294 HLTError("can not resolve dependencies for configuration \"%s\" (%d unresolved)", pTask->GetName(), iResult);
300 AliHLTTask* AliHLTSystem::FindTask(const char* id)
302 // see header file for class documentation
303 AliHLTTask* pTask=NULL;
305 pTask=(AliHLTTask*)fTaskList.FindObject(id);
310 void AliHLTSystem::PrintTaskList()
312 // see header file for class documentation
313 HLTLogKeyword("task list");
314 TObjLink *lnk = NULL;
315 HLTMessage("Task List");
316 lnk=fTaskList.FirstLink();
318 TObject* obj=lnk->GetObject();
320 HLTMessage(" %s - status:", obj->GetName());
321 AliHLTTask* pTask=(AliHLTTask*)obj;
322 pTask->PrintStatus();
329 int AliHLTSystem::Run(Int_t iNofEvents)
331 // see header file for class documentation
334 SetStatusFlags(kRunning);
335 TStopwatch StopwatchBase; StopwatchBase.Reset();
336 TStopwatch StopwatchDA; StopwatchDA.Reset();
337 TStopwatch StopwatchInput; StopwatchInput.Reset();
338 TStopwatch StopwatchOutput; StopwatchOutput.Reset();
339 TObjArray Stopwatches;
340 Stopwatches.AddAt(&StopwatchBase, (int)AliHLTComponent::kSWBase);
341 Stopwatches.AddAt(&StopwatchDA, (int)AliHLTComponent::kSWDA);
342 Stopwatches.AddAt(&StopwatchInput, (int)AliHLTComponent::kSWInput);
343 Stopwatches.AddAt(&StopwatchOutput, (int)AliHLTComponent::kSWOutput);
344 if ((iResult=InitTasks())>=0 && (iResult=InitBenchmarking(&Stopwatches))>=0) {
345 if ((iResult=StartTasks())>=0) {
346 for (int i=0; i<iNofEvents && iResult>=0; i++) {
347 iResult=ProcessTasks(i);
349 HLTInfo("Event %d successfully finished (%d)", i, iResult);
353 HLTError("Processing of event %d failed (%d)", i, iResult);
354 // TODO: define different running modes to either ignore errors in
355 // event processing or not
362 HLTError("can not start task list");
365 } else if (iResult!=-ENOENT) {
366 HLTError("can not initialize task list");
370 HLTInfo("HLT statistics:\n"
371 " base: R:%.3fs C:%.3fs\n"
372 " input: R:%.3fs C:%.3fs\n"
373 " output: R:%.3fs C:%.3fs\n"
374 " event processing : R:%.3fs C:%.3fs"
375 , StopwatchBase.RealTime(),StopwatchBase.CpuTime()
376 , StopwatchInput.RealTime(),StopwatchInput.CpuTime()
377 , StopwatchOutput.RealTime(),StopwatchOutput.CpuTime()
378 , StopwatchDA.RealTime(),StopwatchDA.CpuTime());
380 ClearStatusFlags(kRunning);
384 int AliHLTSystem::InitTasks()
386 // see header file for class documentation
388 TObjLink *lnk=fTaskList.FirstLink();
390 HLTWarning("Task list is empty, aborting ...");
393 while (lnk && iResult>=0) {
394 TObject* obj=lnk->GetObject();
396 AliHLTTask* pTask=(AliHLTTask*)obj;
397 iResult=pTask->Init(NULL, fpComponentHandler);
407 int AliHLTSystem::InitBenchmarking(TObjArray* pStopwatches)
409 // see header file for class documentation
410 if (pStopwatches==NULL) return -EINVAL;
413 TObjLink *lnk=fTaskList.FirstLink();
414 while (lnk && iResult>=0) {
415 TObject* obj=lnk->GetObject();
417 AliHLTTask* pTask=(AliHLTTask*)obj;
418 AliHLTComponent* pComp=NULL;
419 if (iResult>=0 && (pComp=pTask->GetComponent())!=NULL) {
420 switch (pComp->GetComponentType()) {
421 case AliHLTComponent::kProcessor:
422 pComp->SetStopwatches(pStopwatches);
424 case AliHLTComponent::kSource:
426 // this switch determines whether the time consumption of the
427 // AliHLTComponent base methods should be counted to the input
428 // stopwatch or base stopwatch.
429 //int inputBase=(int)AliHLTComponent::kSWBase;
430 int inputBase=(int)AliHLTComponent::kSWInput;
431 pComp->SetStopwatch(pStopwatches->At(inputBase), AliHLTComponent::kSWBase);
432 pComp->SetStopwatch(pStopwatches->At((int)AliHLTComponent::kSWInput), AliHLTComponent::kSWDA);
435 case AliHLTComponent::kSink:
437 // this switch determines whether the time consumption of the
438 // AliHLTComponent base methods should be counted to the output
439 // stopwatch or base stopwatch.
440 //int outputBase=(int)AliHLTComponent::kSWBase;
441 int outputBase=(int)AliHLTComponent::kSWOutput;
442 pComp->SetStopwatch(pStopwatches->At(outputBase), AliHLTComponent::kSWBase);
443 pComp->SetStopwatch(pStopwatches->At((int)AliHLTComponent::kSWOutput), AliHLTComponent::kSWDA);
447 HLTWarning("unknown component type %d", (int)pComp->GetComponentType());
457 int AliHLTSystem::StartTasks()
459 // see header file for class documentation
461 TObjLink *lnk=fTaskList.FirstLink();
462 while (lnk && iResult>=0) {
463 TObject* obj=lnk->GetObject();
465 AliHLTTask* pTask=(AliHLTTask*)obj;
466 iResult=pTask->StartRun();
476 int AliHLTSystem::ProcessTasks(Int_t eventNo)
478 // see header file for class documentation
480 HLTDebug("processing event no %d", eventNo);
481 TObjLink *lnk=fTaskList.FirstLink();
482 while (lnk && iResult>=0) {
483 TObject* obj=lnk->GetObject();
485 AliHLTTask* pTask=(AliHLTTask*)obj;
486 iResult=pTask->ProcessTask(eventNo);
487 HLTDebug("task %s finnished (%d)", pTask->GetName(), iResult);
495 int AliHLTSystem::StopTasks()
497 // see header file for class documentation
499 TObjLink *lnk=fTaskList.FirstLink();
500 while (lnk && iResult>=0) {
501 TObject* obj=lnk->GetObject();
503 AliHLTTask* pTask=(AliHLTTask*)obj;
504 iResult=pTask->EndRun();
512 int AliHLTSystem::DeinitTasks()
514 // see header file for class documentation
516 TObjLink *lnk=fTaskList.FirstLink();
517 while (lnk && iResult>=0) {
518 TObject* obj=lnk->GetObject();
520 AliHLTTask* pTask=(AliHLTTask*)obj;
521 iResult=pTask->Deinit();
529 void* AliHLTSystem::AllocMemory( void* param, unsigned long size )
531 // see header file for class documentation
533 // get rid of 'unused parameter' warning
535 return (void*)new char[size];
538 int AliHLTSystem::Reconstruct(int nofEvents, AliRunLoader* runLoader,
539 AliRawReader* rawReader)
541 // see header file for class documentation
544 HLTInfo("Run Loader %p, Raw Reader %p , %d events", runLoader, rawReader, nofEvents);
545 if (CheckStatus(kReady)) {
546 if ((iResult=AliHLTOfflineInterface::SetParamsToComponents(runLoader, rawReader))>=0) {
547 iResult=Run(nofEvents);
550 HLTError("wrong state %#x, required flags %#x", GetStatusFlags(), kReady);
553 HLTError("missing run loader instance");
559 int AliHLTSystem::FillESD(int eventNo, AliRunLoader* runLoader, AliESDEvent* esd)
561 // see header file for class documentation
564 HLTInfo("Event %d: Run Loader %p, ESD %p", eventNo, runLoader, esd);
565 iResult=AliHLTOfflineInterface::FillComponentESDs(eventNo, runLoader, esd);
567 HLTError("missing run loader/ESD instance(s)");
573 int AliHLTSystem::LoadComponentLibraries(const char* libraries)
575 // see header file for class documentation
578 if (fpComponentHandler) {
579 TString libs(libraries);
580 TObjArray* pTokens=libs.Tokenize(" ");
582 int iEntries=pTokens->GetEntries();
583 for (int i=0; i<iEntries && iResult>=0; i++) {
584 iResult=fpComponentHandler->LoadLibrary((((TObjString*)pTokens->At(i))->GetString()).Data());
589 SetStatusFlags(kLibrariesLoaded);
591 // lets see if we need this, probably not
592 //fpComponentHandler->UnloadLibraries();
593 ClearStatusFlags(kLibrariesLoaded);
597 HLTFatal("no component handler available");
605 int AliHLTSystem::Configure(AliRunLoader* runloader)
607 // see header file for class documentation
609 if (CheckStatus(kRunning)) {
610 HLTError("HLT system in running state, can not configure");
613 if (CheckFilter(kHLTLogDebug))
614 AliHLTModuleAgent::PrintStatus();
615 ClearStatusFlags(kConfigurationLoaded|kTaskListCreated);
616 iResult=LoadConfigurations(runloader);
618 SetStatusFlags(kConfigurationLoaded);
619 iResult=BuildTaskListsFromTopConfigurations(runloader);
621 SetStatusFlags(kTaskListCreated);
624 if (iResult<0) SetStatusFlags(kError);
629 int AliHLTSystem::Reset(int bForce)
631 // see header file for class documentation
633 if (!bForce && CheckStatus(kRunning)) {
634 HLTError("HLT system in running state, can not configure");
638 ClearStatusFlags(~kUninitialized);
642 int AliHLTSystem::LoadConfigurations(AliRunLoader* runloader)
644 // see header file for class documentation
645 if (CheckStatus(kRunning)) {
646 HLTError("HLT system in running state, can not configure");
650 AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent();
651 while (pAgent && iResult>=0) {
652 const char* deplibs=pAgent->GetRequiredComponentLibraries();
654 HLTDebug("load libraries \'%s\' for agent %s (%p)", deplibs, pAgent->GetName(), pAgent);
655 iResult=LoadComponentLibraries(deplibs);
658 HLTDebug("load configurations for agent %s (%p)", pAgent->GetName(), pAgent);
659 pAgent->CreateConfigurations(fpConfigurationHandler, runloader);
660 pAgent=AliHLTModuleAgent::GetNextAgent();
666 int AliHLTSystem::BuildTaskListsFromTopConfigurations(AliRunLoader* runloader)
668 // see header file for class documentation
669 if (CheckStatus(kRunning)) {
670 HLTError("HLT system in running state, can not configure");
673 if (!CheckStatus(kConfigurationLoaded)) {
674 HLTWarning("configurations not yet loaded");
679 AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent();
680 while (pAgent && iResult>=0) {
681 TString tops=pAgent->GetLocalRecConfigurations(runloader);
682 HLTDebug("top configurations for agent %s (%p): %s", pAgent->GetName(), pAgent, tops.Data());
683 TObjArray* pTokens=tops.Tokenize(" ");
685 int iEntries=pTokens->GetEntries();
686 for (int i=0; i<iEntries && iResult>=0; i++) {
687 const char* pCID=((TObjString*)pTokens->At(i))->GetString().Data();
688 AliHLTConfiguration* pConf=fpConfigurationHandler->FindConfiguration(pCID);
690 iResult=BuildTaskList(pConf);
692 HLTWarning("can not find top configuration %s", pCID);
698 pAgent=AliHLTModuleAgent::GetNextAgent();
700 if (iResult>=0) SetStatusFlags(kTaskListCreated);
705 int AliHLTSystem::CheckStatus(int flag)
707 // see header file for class documentation
708 if (flag==kUninitialized && flag==fState) return 1;
709 if ((fState&flag)==flag) return 1;
713 int AliHLTSystem::GetStatusFlags()
715 // see header file for class documentation
719 int AliHLTSystem::SetStatusFlags(int flags)
721 // see header file for class documentation
726 int AliHLTSystem::ClearStatusFlags(int flags)
728 // see header file for class documentation
733 void* AliHLTSystem::FindDynamicSymbol(const char* library, const char* symbol)
735 if (fpComponentHandler==NULL) return NULL;
736 return fpComponentHandler->FindSymbol(library, symbol);