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.
30 #include "AliHLTStdIncludes.h"
31 #include "AliHLTSystem.h"
32 #include "AliHLTComponentHandler.h"
33 #include "AliHLTComponent.h"
34 #include "AliHLTConfiguration.h"
35 #include "AliHLTConfigurationHandler.h"
36 #include "AliHLTTask.h"
37 #include "AliHLTModuleAgent.h"
38 #include "AliHLTOfflineInterface.h"
39 #include "AliHLTDataSource.h"
40 #include "AliHLTOUT.h"
41 #include "AliHLTOUTHandler.h"
42 #include "AliHLTOUTTask.h"
43 #include "AliHLTControlTask.h"
44 #include <TObjArray.h>
45 #include <TObjString.h>
46 #include <TStopwatch.h>
47 //#include <TSystem.h>
49 //#include <TInterpreter.h>
51 /** HLT default component libraries */
52 const char* AliHLTSystem::fgkHLTDefaultLibs[]= {
56 // "libAliHLTSample.so",
61 "libAliHLTTrigger.so",
65 /** ROOT macro for the implementation of ROOT specific class methods */
66 ClassImp(AliHLTSystem)
68 AliHLTSystem::AliHLTSystem(AliHLTComponentLogSeverity loglevel, const char* name)
70 fpComponentHandler(AliHLTComponentHandler::CreateHandler()),
71 fpConfigurationHandler(AliHLTConfigurationHandler::CreateHandler()),
75 fStopwatches(new TObjArray),
78 fpChainHandlers(NULL),
80 fpProprietaryHandlers(NULL),
85 // see header file for class documentation
87 // refer to README to build package
89 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
91 if (fgNofInstances++>0) {
92 // July 2008: multiple instances are now allowed
93 // AliHLTSystem is used in multiple instances for the kChain HLTOUT handler
94 //HLTWarning("multiple instances of AliHLTSystem, you should not use more than one at a time");
97 SetGlobalLoggingLevel(loglevel);
98 SetFrameworkLog(loglevel);
99 if (fpComponentHandler) {
100 AliHLTAnalysisEnvironment env;
101 memset(&env, 0, sizeof(AliHLTAnalysisEnvironment));
102 env.fStructSize=sizeof(AliHLTAnalysisEnvironment);
103 env.fAllocMemoryFunc=AliHLTSystem::AllocMemory;
104 env.fLoggingFunc=NULL;
105 fpComponentHandler->SetEnvironment(&env);
106 InitAliLogFunc(fpComponentHandler);
107 if (fgNofInstances==1) {
108 fpComponentHandler->AnnounceVersion();
111 HLTFatal("can not create Component Handler");
113 if (fpConfigurationHandler) {
114 AliHLTConfiguration::GlobalInit(fpConfigurationHandler);
116 HLTFatal("can not create Configuration Handler");
120 AliHLTSystem::~AliHLTSystem()
122 // see header file for class documentation
126 AliHLTConfiguration::GlobalDeinit(fpConfigurationHandler);
127 if (fpConfigurationHandler) {
128 fpConfigurationHandler->Destroy();
130 fpConfigurationHandler=NULL;
132 if (fpComponentHandler) {
133 fpComponentHandler->Destroy();
135 fpComponentHandler=NULL;
137 // note: fpHLTOUTTask and fpControlTask are deleted by
141 int AliHLTSystem::fgNofInstances=0;
143 int AliHLTSystem::AddConfiguration(AliHLTConfiguration* pConf)
145 // see header file for class documentation
146 HLTLogKeyword("configuration handling");
149 HLTError("function not yet implemented");
157 int AliHLTSystem::InsertConfiguration(AliHLTConfiguration* pConf, AliHLTConfiguration* pPrec)
159 // see header file for class documentation
160 HLTLogKeyword("configuration handling");
165 HLTError("function not yet implemented");
174 int AliHLTSystem::DeleteConfiguration(AliHLTConfiguration* pConf)
176 // see header file for class documentation
177 HLTLogKeyword("configuration handling");
180 HLTError("function not yet implemented");
188 int AliHLTSystem::BuildTaskList(const char* id)
190 // see header file for class documentation
193 if (fpConfigurationHandler) {
194 AliHLTConfiguration* pConf=fpConfigurationHandler->FindConfiguration(id);
196 iResult=BuildTaskList(pConf);
198 HLTError("unknown configuration \"%s\"", id);
210 int AliHLTSystem::BuildTaskList(AliHLTConfiguration* pConf)
212 // see header file for class documentation
215 AliHLTTask* pTask=NULL;
216 if ((pTask=FindTask(pConf->GetName()))!=NULL) {
217 if (pTask->GetConf()!=pConf) {
218 HLTError("configuration mismatch, there is already a task with configuration name \"%s\", but it is different. Most likely configuration %p is not registered properly", pConf->GetName(), pConf);
221 // task for this configuration exists, terminate
223 } else if (pConf->SourcesResolved(1)!=1) {
224 HLTError("configuration \"%s\" has unresolved sources, aborting ...", pConf->GetName());
227 pTask=new AliHLTTask(pConf);
231 pTask->SetLocalLoggingLevel(GetLocalLoggingLevel());
234 static int iterationLevel=0;
235 if (pTask && iResult>=0) {
236 // check for circular dependencies
237 if ((iResult=pConf->FollowDependency(pConf->GetName()))>0) {
238 HLTError("detected circular dependency for configuration \"%s\"", pTask->GetName());
239 pTask->PrintDependencyTree(pTask->GetName(), 1/*use the configuration list*/);
240 HLTError("aborted ...");
244 // check whether all dependencies are already in the task list
245 // create the missing ones
246 // this step is an iterative process which calls this function again for the missing
247 // configurations, in order to avoid the currently processed task to be created
248 // again it is added to the list temporarily and removed afterwards
249 // This is of high importance to preserve the order of the tasks. Furthermore, the
250 // InsertTask method has to be used in order to set all the cross links right
251 fTaskList.Add(pTask);
252 AliHLTConfiguration* pDep=pConf->GetFirstSource();
253 while (pDep!=NULL && iResult>=0) {
254 HLTDebug("iteration %d: checking dependency %s (%p)", iterationLevel, pDep->GetName(), pDep);
255 if (FindTask(pDep->GetName())==NULL) {
256 HLTDebug("iteration %d: building task list for configuration %s (%p)", iterationLevel, pDep->GetName(), pDep);
258 iResult=BuildTaskList(pDep);
261 pDep=pConf->GetNextSource();
263 // remove the temporarily added task
264 fTaskList.Remove(pTask);
266 // insert the task and set the cross-links
268 HLTDebug("iteration %d: inserting task %s (%p)", iterationLevel, pTask->GetName(), pTask);
269 iResult=InsertTask(pTask);
282 int AliHLTSystem::CleanTaskList()
284 // see header file for class documentation
289 while ((lnk=fTaskList.LastLink())!=NULL) {
290 delete (lnk->GetObject());
291 fTaskList.Remove(lnk);
297 int AliHLTSystem::InsertTask(AliHLTTask* pTask)
299 // see header file for class documentation
301 if (fpControlTask==NULL) {
302 fpControlTask=new AliHLTControlTask;
303 if (!fpControlTask) return -ENOMEM;
304 fTaskList.AddFirst(fpControlTask);
306 TObjLink *controlLnk=NULL;
307 TObjLink *lnk = fTaskList.FirstLink();
308 assert(!lnk || lnk->GetObject()==fpControlTask || fpControlTask==NULL);
309 if (lnk && lnk->GetObject()==fpControlTask) {
310 if (pTask->GetConf() && pTask->GetConf()->GetFirstSource()==NULL) {
311 pTask->SetDependency(fpControlTask);
312 fpControlTask->SetTarget(pTask);
317 if ((iResult=pTask->CheckDependencies())<=0)
319 while (lnk && iResult>0) {
320 AliHLTTask* pCurr = (AliHLTTask*)lnk->GetObject();
321 //HLTDebug("checking \"%s\"", pCurr->GetName());
322 iResult=pTask->Depends(pCurr);
324 iResult=pTask->SetDependency(pCurr);
325 pCurr->SetTarget(pTask);
326 HLTDebug("set dependency \"%s\" for configuration \"%s\"", pCurr->GetName(), pTask->GetName());
328 if (pCurr->Depends(pTask)) {
329 // circular dependency
330 HLTError("circular dependency: can not resolve dependencies for configuration \"%s\"", pTask->GetName());
332 } else if ((iResult=pTask->CheckDependencies())>0) {
338 fTaskList.AddAfter(lnk, pTask);
339 } else if (controlLnk) {
340 fTaskList.AddAfter(controlLnk, pTask);
342 fTaskList.AddFirst(pTask);
344 HLTDebug("task \"%s\" (%p) inserted (size %d)", pTask->GetName(), pTask, sizeof(AliHLTTask));
345 } else if (iResult>0) {
346 HLTError("can not resolve dependencies for configuration \"%s\" (%d unresolved)", pTask->GetName(), iResult);
352 AliHLTTask* AliHLTSystem::FindTask(const char* id)
354 // see header file for class documentation
355 AliHLTTask* pTask=NULL;
357 pTask=dynamic_cast<AliHLTTask*>(fTaskList.FindObject(id));
362 void AliHLTSystem::PrintTaskList()
364 // see header file for class documentation
365 HLTLogKeyword("task list");
366 TObjLink *lnk = NULL;
367 HLTMessage("Task List");
368 lnk=fTaskList.FirstLink();
370 TObject* obj=lnk->GetObject();
372 HLTMessage(" %s - status:", obj->GetName());
373 AliHLTTask* pTask=(AliHLTTask*)obj;
374 pTask->PrintStatus();
381 int AliHLTSystem::Run(Int_t iNofEvents, int bStop)
383 // see header file for class documentation
386 SetStatusFlags(kRunning);
387 if (fEventCount>=0 || (iResult=InitTasks())>=0) {
388 if (fEventCount>=0 || (iResult=StartTasks())>=0) {
389 if (fEventCount==0) {
390 InitBenchmarking(fStopwatches);
392 // Matthias Oct 11 2008 this is a bug
393 // By resuming the stopwatches at this point, all continued counting, but the
394 // starting and stopping is controlled by the AliHLTStopwatchGuard
395 //ResumeBenchmarking(fStopwatches);
397 for (int i=fEventCount; i<fEventCount+iNofEvents && iResult>=0; i++) {
399 if (iNofEvents>1 && i==fEventCount) {
400 HLTWarning("can not add more than one event to the HLTOUT, skipping all but last block");
402 // reset and prepare for new data
403 fpHLTOUTTask->Reset();
405 if ((iResult=ProcessTasks(i))>=0) {
409 // TODO: define different running modes to either ignore errors in
410 // event processing or not
415 fEventCount+=iNofEvents;
416 if (bStop) StopTasks();
417 else PauseBenchmarking(fStopwatches);
419 if (bStop) DeinitTasks();
423 } else if (iResult==-126 /*ENOKEY*/) {
424 iResult=0; // do not propagate the error
426 ClearStatusFlags(kRunning);
430 int AliHLTSystem::InitTasks()
432 // see header file for class documentation
434 TObjLink *lnk=fTaskList.FirstLink();
437 HLTInfo("Task list is empty, skipping HLT");
438 return -126 /*ENOKEY*/;
440 while (lnk && iResult>=0) {
441 TObject* obj=lnk->GetObject();
443 AliHLTTask* pTask=(AliHLTTask*)obj;
444 iResult=pTask->Init(NULL, fpComponentHandler);
445 // ProcInfo_t ProcInfo;
446 // gSystem->GetProcInfo(&ProcInfo);
447 // HLTInfo("task %s initialized (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
453 HLTError("can not initialize task list, error %d", iResult);
459 int AliHLTSystem::InitBenchmarking(TObjArray* pStopwatches)
461 // see header file for class documentation
463 if (pStopwatches==NULL) return 0;
465 for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
466 TStopwatch* pStopwatch= new TStopwatch;
469 pStopwatches->AddAt(pStopwatch, i);
476 TObjLink *lnk=fTaskList.FirstLink();
477 while (lnk && iResult>=0) {
478 TObject* obj=lnk->GetObject();
480 AliHLTTask* pTask=(AliHLTTask*)obj;
481 AliHLTComponent* pComp=NULL;
482 if (iResult>=0 && (pComp=pTask->GetComponent())!=NULL) {
483 switch (pComp->GetComponentType()) {
484 case AliHLTComponent::kProcessor:
485 pComp->SetStopwatches(pStopwatches);
487 case AliHLTComponent::kSource:
489 // this switch determines whether the time consumption of the
490 // AliHLTComponent base methods should be counted to the input
491 // stopwatch or base stopwatch.
492 //int inputBase=(int)AliHLTComponent::kSWBase;
493 int inputBase=(int)AliHLTComponent::kSWInput;
494 pComp->SetStopwatch(pStopwatches->At(inputBase), AliHLTComponent::kSWBase);
495 pComp->SetStopwatch(pStopwatches->At((int)AliHLTComponent::kSWInput), AliHLTComponent::kSWDA);
498 case AliHLTComponent::kSink:
500 // this switch determines whether the time consumption of the
501 // AliHLTComponent base methods should be counted to the output
502 // stopwatch or base stopwatch.
503 //int outputBase=(int)AliHLTComponent::kSWBase;
504 int outputBase=(int)AliHLTComponent::kSWOutput;
505 pComp->SetStopwatch(pStopwatches->At(outputBase), AliHLTComponent::kSWBase);
506 pComp->SetStopwatch(pStopwatches->At((int)AliHLTComponent::kSWOutput), AliHLTComponent::kSWDA);
510 HLTWarning("unknown component type %d", (int)pComp->GetComponentType());
520 int AliHLTSystem::PauseBenchmarking(TObjArray* pStopwatches) const
522 // see header file for class documentation
523 if (pStopwatches==NULL) return 0;
525 for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
526 if (!pStopwatches->At(i)) continue;
527 TStopwatch* pSw=dynamic_cast<TStopwatch*>(pStopwatches->At(i));
528 if (pSw) pSw->Stop();
533 int AliHLTSystem::ResumeBenchmarking(TObjArray* pStopwatches) const
535 // see header file for class documentation
536 if (pStopwatches==NULL) return 0;
538 for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
539 if (!pStopwatches->At(i)) continue;
540 TStopwatch* pSw=dynamic_cast<TStopwatch*>(pStopwatches->At(i));
541 if (pSw) pSw->Continue();
546 int AliHLTSystem::PrintBenchmarking(TObjArray* pStopwatches, int bClean) const
548 // see header file for class documentation
550 if (pStopwatches==NULL) return 0;
552 for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
553 if (!dynamic_cast<TStopwatch*>(pStopwatches->At(i))) {
559 if (iInitialized!=0) {
560 HLTImportant("HLT statistics:\n"
561 " base: R:%.3fs C:%.3fs\n"
562 " input: R:%.3fs C:%.3fs\n"
563 " output: R:%.3fs C:%.3fs\n"
564 " event processing : R:%.3fs C:%.3fs"
565 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWBase))->RealTime()
566 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWBase))->CpuTime()
567 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWInput))->RealTime()
568 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWInput))->CpuTime()
569 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWOutput))->RealTime()
570 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWOutput))->CpuTime()
571 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWDA))->RealTime()
572 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWDA))->CpuTime()
577 for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
578 TObject* pObj=pStopwatches->RemoveAt(i);
579 if (pObj) delete pObj;
585 int AliHLTSystem::StartTasks()
587 // see header file for class documentation
589 TObjLink *lnk=fTaskList.FirstLink();
590 while (lnk && iResult>=0) {
591 TObject* obj=lnk->GetObject();
593 AliHLTTask* pTask=(AliHLTTask*)obj;
594 iResult=pTask->StartRun();
595 // ProcInfo_t ProcInfo;
596 // gSystem->GetProcInfo(&ProcInfo);
597 // HLTInfo("task %s started (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
603 HLTError("can not start task list, error %d", iResult);
605 SetStatusFlags(kStarted);
608 if ((iResult=SendControlEvent(kAliHLTDataTypeSOR))<0) {
609 HLTError("can not send SOR event: error %d", iResult);
615 int AliHLTSystem::ProcessTasks(Int_t eventNo)
617 // see header file for class documentation
619 HLTDebug("processing event no %d", eventNo);
620 TObjLink *lnk=fTaskList.FirstLink();
621 while (lnk && iResult>=0) {
622 TObject* obj=lnk->GetObject();
624 AliHLTTask* pTask=(AliHLTTask*)obj;
625 iResult=pTask->ProcessTask(eventNo);
626 // ProcInfo_t ProcInfo;
627 // gSystem->GetProcInfo(&ProcInfo);
628 // HLTInfo("task %s processed (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
635 HLTImportant("Event %d successfully finished (%d)", eventNo, iResult);
638 HLTError("Processing of event %d failed (%d)", eventNo, iResult);
644 int AliHLTSystem::StopTasks()
646 // see header file for class documentation
648 if ((iResult=SendControlEvent(kAliHLTDataTypeEOR))<0) {
649 HLTError("can not send EOR event");
652 // cleanup blocks from the last event. This is a bit awkward. All output
653 // blocks from the chains need to be stored in the HLTOUT task. Though,
654 // we do not know, whether HLTOUT is going to be processed or not.
656 fpHLTOUTTask->Reset();
658 TObjLink *lnk=fTaskList.FirstLink();
660 TObject* obj=lnk->GetObject();
662 AliHLTTask* pTask=(AliHLTTask*)obj;
663 int locResult=pTask->EndRun();
664 if (iResult>=0 && locResult<0) iResult=locResult;
665 // ProcInfo_t ProcInfo;
666 // gSystem->GetProcInfo(&ProcInfo);
667 // HLTInfo("task %s stopped (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
672 PrintBenchmarking(fStopwatches, 1 /*clean*/);
673 ClearStatusFlags(kStarted);
677 int AliHLTSystem::SendControlEvent(AliHLTComponentDataType dt)
679 // see header file for class documentation
682 AliHLTRunDesc runDesc;
683 memset(&runDesc, 0, sizeof(AliHLTRunDesc));
684 runDesc.fStructSize=sizeof(AliHLTRunDesc);
685 AliHLTControlTask::AliHLTControlEventGuard g(fpControlTask, dt, kAliHLTVoidDataSpec, (AliHLTUInt8_t*)&runDesc, sizeof(AliHLTRunDesc));
686 HLTDebug("sending event %s, run descriptor %p", AliHLTComponent::DataType2Text(dt).c_str(), &runDesc);
687 TObjLink *lnk=fTaskList.FirstLink();
688 while (lnk && iResult>=0) {
689 TObject* obj=lnk->GetObject();
691 AliHLTTask* pTask=(AliHLTTask*)obj;
692 AliHLTUInt32_t eventType=gkAliEventTypeUnknown;
693 if (dt==kAliHLTDataTypeSOR) eventType=gkAliEventTypeStartOfRun;
694 else if (dt==kAliHLTDataTypeEOR) eventType=gkAliEventTypeEndOfRun;
695 else HLTWarning("unknown control event %s", AliHLTComponent::DataType2Text(dt).c_str());
696 iResult=pTask->ProcessTask(-1, eventType);
702 // control events are not supposed to go into the HLTOUT
704 fpHLTOUTTask->Reset();
706 HLTDebug("event %s done (%d)", AliHLTComponent::DataType2Text(dt).c_str(), iResult);
710 int AliHLTSystem::DeinitTasks()
712 // see header file for class documentation
714 TObjLink *lnk=fTaskList.FirstLink();
715 while (lnk && iResult>=0) {
716 TObject* obj=lnk->GetObject();
718 AliHLTTask* pTask=(AliHLTTask*)obj;
719 iResult=pTask->Deinit();
720 // ProcInfo_t ProcInfo;
721 // gSystem->GetProcInfo(&ProcInfo);
722 // HLTInfo("task %s cleaned (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
733 int AliHLTSystem::CleanHLTOUT()
735 // see header file for class documentation
736 if (fpChainHandlers) {
737 AliHLTOUT::AliHLTOUTHandlerListEntryVector* pHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpChainHandlers);
738 fpChainHandlers=NULL;
740 AliHLTOUT::InvalidateBlocks(*pHandlers);
741 AliHLTOUT::RemoveEmptyDuplicateHandlers(*pHandlers);
743 assert(pHandlers->size()==0);
748 AliHLTOUT::AliHLTOUTHandlerListEntryVector* pHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpEsdHandlers);
751 AliHLTOUT::InvalidateBlocks(*pHandlers);
752 AliHLTOUT::RemoveEmptyDuplicateHandlers(*pHandlers);
754 assert(pHandlers->size()==0);
758 if (fpProprietaryHandlers) {
759 AliHLTOUT::AliHLTOUTHandlerListEntryVector* pHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpProprietaryHandlers);
760 fpProprietaryHandlers=NULL;
762 AliHLTOUT::InvalidateBlocks(*pHandlers);
763 AliHLTOUT::RemoveEmptyDuplicateHandlers(*pHandlers);
765 assert(pHandlers->size()==0);
771 void* AliHLTSystem::AllocMemory( void* /*param*/, unsigned long size )
773 // see header file for class documentation
776 p=(void*)new char[size];
780 log.LoggingVarargs(kHLTLogError, "AliHLTSystem" , "AllocMemory" , __FILE__ , __LINE__ , "exeption during memory allocation" );
785 int AliHLTSystem::Reconstruct(int nofEvents, AliRunLoader* runLoader,
786 AliRawReader* rawReader)
788 // see header file for class documentation
790 if (runLoader || rawReader || nofEvents==0) {
791 if (nofEvents>0) {HLTInfo("Run Loader %p, Raw Reader %p , %d event(s)", runLoader, rawReader, nofEvents);}
792 if (CheckStatus(kReady)) {
794 // special case to close the reconstruction
795 if (!CheckStatus(kError)) {
801 if ((iResult=AliHLTOfflineInterface::SetParamsToComponents(runLoader, rawReader))>=0) {
802 // the system always remains started after event processing, a specific
803 // call with nofEvents==0 is needed to execute the stop sequence
804 if ((iResult=Run(nofEvents, 0))<0) SetStatusFlags(kError);
808 HLTError("wrong state %#x, required flags %#x", GetStatusFlags(), kReady);
811 HLTError("missing RunLoader (%p)/RawReader (%p) instance", runLoader, rawReader);
817 int AliHLTSystem::FillESD(int eventNo, AliRunLoader* runLoader, AliESDEvent* esd)
819 // see header file for class documentation
821 if (runLoader || esd) {
822 HLTInfo("Event %d: Run Loader %p, ESD %p", eventNo, runLoader, esd);
823 iResult=AliHLTOfflineInterface::FillComponentESDs(eventNo, runLoader, esd);
825 HLTError("missing run loader/ESD instance(s)");
831 int AliHLTSystem::ProcessHLTOUT(AliHLTOUT* pHLTOUT, AliESDEvent* esd)
833 // see header file for class documentation
835 if (!pHLTOUT) return -EINVAL;
836 HLTDebug("processing %d HLT data blocks", pHLTOUT->GetNofDataBlocks());
838 // add the current HLTOUT task to the collection
840 AliHLTOUT* pTask=dynamic_cast<AliHLTOUT*>(fpHLTOUTTask);
841 if (pTask && (iResult=pTask->Init())>=0) {
842 if (pTask->GetNofDataBlocks()>0) {
843 pHLTOUT->AddSubCollection(pTask);
846 HLTWarning("can not initialize HLTOUT sub collection %s for reconstruction chain (%d), data blocks are lost", pTask?fpHLTOUTTask->GetName():"nil", iResult);
853 // process all kChain handlers first
855 if ((iResult=ProcessHLTOUTkChain(pHLTOUT))<0) {
856 HLTWarning("Processing of kChain-type data blocks failed with error code %d", iResult);
861 fpEsdHandlers=new AliHLTOUT::AliHLTOUTHandlerListEntryVector;
862 if (!fpProprietaryHandlers)
863 fpProprietaryHandlers=new AliHLTOUT::AliHLTOUTHandlerListEntryVector;
865 AliHLTOUT::AliHLTOUTHandlerListEntryVector* pEsdHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpEsdHandlers);
866 AliHLTOUT::AliHLTOUTHandlerListEntryVector* pProprietaryHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpProprietaryHandlers);
867 if (!pEsdHandlers || !pProprietaryHandlers) return -ENOMEM;
869 // invalidate all blocks
870 AliHLTOUT::InvalidateBlocks(*pEsdHandlers);
871 AliHLTOUT::InvalidateBlocks(*pProprietaryHandlers);
873 AliHLTComponentDataTypeList esdBlocks;
875 for (iResult=pHLTOUT->SelectFirstDataBlock();
877 iResult=pHLTOUT->SelectNextDataBlock()) {
878 AliHLTComponentDataType dt=kAliHLTVoidDataType;
879 AliHLTUInt32_t spec=kAliHLTVoidDataSpec;
880 pHLTOUT->GetDataBlockDescription(dt, spec);
881 AliHLTOUTHandler* pHandler=pHLTOUT->GetHandler();
882 AliHLTModuleAgent::AliHLTOUTHandlerType handlerType=pHLTOUT->GetDataBlockHandlerType();
884 // default handling for ESD data blocks does not require an explicite handler
885 if (!pHandler && (dt==kAliHLTDataTypeESDObject || dt==kAliHLTDataTypeESDTree)) {
886 handlerType=AliHLTModuleAgent::kEsd;
888 const char* pMsg="invalid";
889 switch (handlerType) {
890 case AliHLTModuleAgent::kEsd:
893 // schedule for later processing
894 pHLTOUT->InsertHandler(*pEsdHandlers, pHLTOUT->GetDataBlockHandlerDesc());
896 AliHLTComponentDataTypeList::iterator element=esdBlocks.begin();
897 for (; element!=esdBlocks.end(); element++) {
899 HLTWarning("multiple ESDs of identical data type %s, please add appropriate handler to merge ESDs", AliHLTComponent::DataType2Text(dt).c_str());
903 if (element==esdBlocks.end()) esdBlocks.push_back(dt);
906 const AliHLTUInt8_t* pBuffer=NULL;
907 AliHLTUInt32_t size=0;
908 if (pHLTOUT->GetDataBuffer(pBuffer, size)>=0) {
909 pHLTOUT->WriteESD(pBuffer, size, dt, esd);
910 pHLTOUT->ReleaseDataBuffer(pBuffer);
912 pHLTOUT->MarkDataBlockProcessed();
916 case AliHLTModuleAgent::kRawReader:
917 // handled in the AliRawReaderHLT
919 case AliHLTModuleAgent::kRawStream:
920 HLTWarning("HLTOUT handler type 'kRawStream' not yet implemented: agent %s, data type %s, specification %#x",
921 pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
922 AliHLTComponent::DataType2Text(dt).c_str(), spec);
924 case AliHLTModuleAgent::kChain:
925 HLTWarning("HLTOUT handler type 'kChain' has already been processed: agent %s, data type %s, specification %#x\n"
926 "New block of this type added by the chain? Skipping data block ...",
927 pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
928 AliHLTComponent::DataType2Text(dt).c_str(), spec);
930 case AliHLTModuleAgent::kProprietary:
931 HLTDebug("processing proprietary data: agent %s, data type %s, specification %#x",
932 pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
933 AliHLTComponent::DataType2Text(dt).c_str(), spec);
935 AliHLTOUT::AliHLTOUTLockGuard g(pHLTOUT);
936 int res=pHandler->ProcessData(pHLTOUT);
938 HLTWarning("processing proprietary data failed (%d): agent %s, data type %s, specification %#x",
939 res, pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
940 AliHLTComponent::DataType2Text(dt).c_str(), spec);
944 case AliHLTModuleAgent::kUnknownOutput:
946 // fall trough intended
948 HLTWarning("%s handler type: agent %s, data type %s, specification %#x, ... skipping data block",
949 pMsg, pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
950 AliHLTComponent::DataType2Text(dt).c_str(), spec);
953 // TODO: the return value of SelectFirst/NextDataBlock must be
954 // changed in order to avoid this check
955 if (iResult==-ENOENT) iResult=0;
957 AliHLTOUT::AliHLTOUTHandlerListEntryVector::iterator handler;
959 // process and write all esd data blocks
960 for (handler=pEsdHandlers->begin(); handler!=pEsdHandlers->end() && iResult>=0; handler++) {
961 AliHLTOUT::AliHLTOUTSelectionGuard g(pHLTOUT, &(*handler));
962 AliHLTOUTHandler* pHandler=*handler;
963 const AliHLTUInt8_t* pBuffer=NULL;
964 AliHLTUInt32_t size=0;
965 pHandler->ProcessData(pHLTOUT);
966 if ((size=pHandler->GetProcessedData(pBuffer))>0) {
967 AliHLTModuleAgent::AliHLTOUTHandlerDesc desc=*handler;
968 AliHLTComponentDataType dt=desc;
969 pHLTOUT->WriteESD(pBuffer, size, dt, esd);
970 pHandler->ReleaseProcessedData(pBuffer, size);
972 pHLTOUT->MarkDataBlocksProcessed(&(*handler));
975 // process all kProprietary data blocks
976 for (handler=pProprietaryHandlers->begin(); handler!=pProprietaryHandlers->end() && iResult>=0; handler++) {
977 AliHLTOUT::AliHLTOUTSelectionGuard g(pHLTOUT, &(*handler));
978 AliHLTOUTHandler* pHandler=*handler;
979 const AliHLTUInt8_t* pBuffer=NULL;
980 AliHLTUInt32_t size=0;
981 pHandler->ProcessData(pHLTOUT);
982 if ((size=pHandler->GetProcessedData(pBuffer))>0) {
983 HLTWarning("data produced by kProprietary handler ignored");
984 pHandler->ReleaseProcessedData(pBuffer, size);
986 pHLTOUT->MarkDataBlocksProcessed(&(*handler));
989 // remove all empty handlers form the list (handlers which did not get a block this time)
990 AliHLTOUT::RemoveEmptyDuplicateHandlers(*pEsdHandlers);
991 AliHLTOUT::RemoveEmptyDuplicateHandlers(*pProprietaryHandlers);
996 int AliHLTSystem::ProcessHLTOUTkChain(AliHLTOUT* pHLTOUT)
998 // see header file for class documentation
1000 if (!pHLTOUT) return -EINVAL;
1002 if (!fpChainHandlers)
1003 fpChainHandlers=new AliHLTOUT::AliHLTOUTHandlerListEntryVector;
1005 AliHLTOUT::AliHLTOUTHandlerListEntryVector* pChainHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpChainHandlers);
1006 if (!pChainHandlers) return -ENOMEM;
1008 // invalidate all blocks
1009 AliHLTOUT::InvalidateBlocks(*pChainHandlers);
1012 pHLTOUT->FillHandlerList(*pChainHandlers, AliHLTModuleAgent::kChain);
1014 // process all defined chain handlers
1015 AliHLTOUT::AliHLTOUTHandlerListEntryVector::iterator chainHandler;
1016 for (chainHandler=pChainHandlers->begin(); chainHandler!=pChainHandlers->end() && iResult>=0; chainHandler++) {
1017 if (chainHandler->IsEmpty()) continue;
1018 AliHLTOUT::AliHLTOUTSelectionGuard g(pHLTOUT, &(*chainHandler));
1019 AliHLTOUTHandler* pHandler=*chainHandler;
1020 const AliHLTUInt8_t* pBuffer=NULL;
1021 AliHLTUInt32_t size=0;
1022 pHandler->ProcessData(pHLTOUT);
1023 if ((size=pHandler->GetProcessedData(pBuffer))>0) {
1024 AliHLTModuleAgent::AliHLTOUTHandlerDesc desc=*chainHandler;
1025 AliHLTComponentDataType dt=desc;
1027 pHandler->ReleaseProcessedData(pBuffer, size);
1029 pHLTOUT->MarkDataBlocksProcessed(&(*chainHandler));
1032 // remove all empty handlers form the list (handlers which did not get a block this time)
1033 AliHLTOUT::RemoveEmptyDuplicateHandlers(*pChainHandlers);
1038 int AliHLTSystem::LoadComponentLibraries(const char* libraries)
1040 // see header file for class documentation
1043 if (fpComponentHandler) {
1044 TString libs(libraries);
1045 TObjArray* pTokens=libs.Tokenize(" ");
1047 int iEntries=pTokens->GetEntries();
1048 for (int i=0; i<iEntries && iResult>=0; i++) {
1049 iResult=fpComponentHandler->LoadLibrary((((TObjString*)pTokens->At(i))->GetString()).Data());
1054 SetStatusFlags(kLibrariesLoaded);
1056 // lets see if we need this, probably not
1057 //fpComponentHandler->UnloadLibraries();
1058 ClearStatusFlags(kLibrariesLoaded);
1062 HLTFatal("no component handler available");
1070 int AliHLTSystem::Configure(AliRunLoader* runloader)
1072 // see header file for class documentation
1073 return Configure(NULL, runloader);
1076 int AliHLTSystem::Configure(AliRawReader* rawReader, AliRunLoader* runloader)
1078 // see header file for class documentation
1080 if (CheckStatus(kRunning)) {
1081 HLTError("HLT system in running state, can not configure");
1084 ClearStatusFlags(kTaskListCreated);
1085 if (CheckFilter(kHLTLogDebug))
1086 AliHLTModuleAgent::PrintStatus();
1087 if (CheckStatus(kConfigurationLoaded)==0) {
1088 iResult=LoadConfigurations(rawReader, runloader);
1090 if (fChains.Length()==0) {
1091 HLTError("custom configuration(s) specified, but no configuration to run in local reconstruction, use \'chains=<chain,...>\' option");
1096 SetStatusFlags(kConfigurationLoaded);
1097 if (CheckFilter(kHLTLogDebug))
1098 fpConfigurationHandler->PrintConfigurations();
1099 iResult=BuildTaskListsFromReconstructionChains(rawReader, runloader);
1101 SetStatusFlags(kTaskListCreated);
1104 if (iResult<0) SetStatusFlags(kError);
1109 int AliHLTSystem::ScanOptions(const char* options)
1111 // see header file for class documentation
1114 //AliHLTComponentHandler::TLibraryMode libMode=AliHLTComponentHandler::kDynamic;
1116 TString alloptions(options);
1117 TObjArray* pTokens=alloptions.Tokenize(" ");
1119 int iEntries=pTokens->GetEntries();
1120 for (int i=0; i<iEntries; i++) {
1121 TString token=(((TObjString*)pTokens->At(i))->GetString());
1122 if (token.Contains("loglevel=")) {
1123 TString param=token.ReplaceAll("loglevel=", "");
1124 if (param.IsDigit()) {
1125 SetGlobalLoggingLevel((AliHLTComponentLogSeverity)param.Atoi());
1126 } else if (param.BeginsWith("0x") &&
1127 param.Replace(0,2,"",0).IsHex()) {
1129 sscanf(param.Data(),"%x", &severity);
1130 SetGlobalLoggingLevel((AliHLTComponentLogSeverity)severity);
1132 HLTWarning("wrong parameter for option \'loglevel=\', (hex) number expected");
1134 } else if (token.Contains("frameworklog=")) {
1135 TString param=token.ReplaceAll("frameworklog=", "");
1136 if (param.IsDigit()) {
1137 SetFrameworkLog((AliHLTComponentLogSeverity)param.Atoi());
1138 } else if (param.BeginsWith("0x") &&
1139 param.Replace(0,2,"",0).IsHex()) {
1141 sscanf(param.Data(),"%x", &severity);
1142 SetFrameworkLog((AliHLTComponentLogSeverity)severity);
1144 HLTWarning("wrong parameter for option \'loglevel=\', (hex) number expected");
1146 } else if (token.Contains("alilog=off")) {
1148 } else if (token.Contains("config=")) {
1149 TString param=token.ReplaceAll("config=", "");
1151 gROOT->Macro(param.Data(), &error);
1153 SetStatusFlags(kConfigurationLoaded);
1155 HLTError("can not execute macro \'%s\'", param.Data());
1158 } else if (token.Contains("chains=")) {
1159 TString param=token.ReplaceAll("chains=", "");
1160 fChains=param.ReplaceAll(",", " ");
1161 if (fChains.IsNull()) fChains=" "; // disable all chains
1162 } else if (token.Contains("libmode=")) {
1163 TString param=token.ReplaceAll("libmode=", "");
1164 param.ReplaceAll(",", " ");
1165 if (fpComponentHandler) {
1166 if (param.CompareTo("static")==0) {
1167 fpComponentHandler->SetLibraryMode(AliHLTComponentHandler::kStatic);
1168 } else if (param.CompareTo("dynamic")==0) {
1169 fpComponentHandler->SetLibraryMode(AliHLTComponentHandler::kDynamic);
1171 HLTWarning("wrong argument for option \'libmode=\', use \'static\' or \'dynamic\'");
1174 } else if (token.BeginsWith("lib") && token.EndsWith(".so")) {
1178 HLTWarning("unknown option \'%s\'", token.Data());
1185 if (libs.IsNull()) {
1186 const char** deflib=fgkHLTDefaultLibs;
1192 if ((!CheckStatus(AliHLTSystem::kLibrariesLoaded)) &&
1193 (LoadComponentLibraries(libs.Data())<0)) {
1194 HLTError("error while loading HLT libraries");
1202 int AliHLTSystem::Reset(int bForce)
1204 // see header file for class documentation
1206 if (!bForce && CheckStatus(kRunning)) {
1207 HLTError("HLT system in running state, can not configure");
1211 ClearStatusFlags(~kUninitialized);
1215 int AliHLTSystem::LoadConfigurations(AliRawReader* rawReader, AliRunLoader* runloader)
1217 // see header file for class documentation
1218 if (CheckStatus(kRunning)) {
1219 HLTError("HLT system in running state, can not configure");
1223 AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent();
1225 while (pAgent && iResult>=0) {
1226 const char* deplibs=pAgent->GetRequiredComponentLibraries();
1228 HLTDebug("required libraries \'%s\' for agent %s (%p)", deplibs, pAgent->GetName(), pAgent);
1232 HLTDebug("load configurations for agent %s (%p)", pAgent->GetName(), pAgent);
1233 pAgent->CreateConfigurations(fpConfigurationHandler, rawReader, runloader);
1234 pAgent=AliHLTModuleAgent::GetNextAgent();
1238 iResult=LoadComponentLibraries(extralibs.Data());
1244 int AliHLTSystem::BuildTaskListsFromReconstructionChains(AliRawReader* rawReader, AliRunLoader* runloader)
1246 // see header file for class documentation
1247 if (CheckStatus(kRunning)) {
1248 HLTError("HLT system in running state, can not configure");
1251 if (!CheckStatus(kConfigurationLoaded)) {
1252 HLTWarning("configurations not yet loaded");
1261 if (fChains.Length()>0) {
1263 HLTImportant("custom reconstruction chain: %s", chains.Data());
1265 AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent();
1266 while ((pAgent || fChains.Length()>0) && iResult>=0) {
1267 const char* agentchains=pAgent->GetReconstructionChains(rawReader, runloader);
1269 if (!chains.IsNull()) chains+=" ";
1270 chains+=agentchains;
1271 HLTInfo("reconstruction chains for agent %s (%p): %s", pAgent->GetName(), pAgent, agentchains);
1273 pAgent=AliHLTModuleAgent::GetNextAgent();
1277 // build task list for chains
1278 TObjArray* pTokens=chains.Tokenize(" ");
1280 int iEntries=pTokens->GetEntries();
1281 for (int i=0; i<iEntries && iResult>=0; i++) {
1282 const char* pCID=((TObjString*)pTokens->At(i))->GetString().Data();
1283 AliHLTConfiguration* pConf=fpConfigurationHandler->FindConfiguration(pCID);
1285 iResult=BuildTaskList(pConf);
1286 if (true) { // condition was deprecated but kept for sake of svn diff
1287 // bHaveOutput variable has to be set for both running modes
1288 // AliHLTSimulation and AliHLTReconstruction
1289 assert(fpComponentHandler!=NULL);
1290 TString cid=pConf->GetComponentID();
1291 if (runloader!=NULL && cid.CompareTo("HLTOUT")==0) {
1292 // remove from the input of a global HLTOUT configuration
1293 chains.ReplaceAll(pCID, "");
1294 } else if (bHaveOutput==0) {
1295 // check whether this configuration produces data output
1296 if ((bHaveOutput=fpComponentHandler->HasOutputData(cid.Data()))<0) {
1298 chains.ReplaceAll(pCID, "");
1303 HLTWarning("can not find configuration %s", pCID);
1309 // build HLTOUT for simulation
1310 if (iResult>=0 && runloader) {
1312 // there are components in the chain which produce data which need to be
1313 // piped to an HLTOUT
1314 if (fpComponentHandler->FindComponentIndex("HLTOUT")>=0 ||
1315 fpComponentHandler->LoadLibrary("libHLTsim.so")>=0) {
1316 AliHLTConfiguration globalout("_globalout_", "HLTOUT", chains.Data(), NULL);
1317 iResult=BuildTaskList("_globalout_");
1319 HLTError("can not load libHLTsim.so and HLTOUT component");
1325 // build HLTOUT task for reconstruction
1326 // Matthias 08.07.2008 the rawReader is never set when running embedded into
1327 // AliReconstruction. The system is configured during AliHLTReconstructor::Init
1328 // where the RawReader is not available. It is available in the first invocation
1331 // That means that policy is slightly changed:
1332 // - if the run loader is available -> AliSimulation
1333 // - no run loader available -> AliReconstruction
1334 if (iResult>=0 && !runloader) {
1336 // there are components in the chain which produce data which need to be
1337 // piped to an HLTOUT sub-collection
1338 if (!fpHLTOUTTask) {
1339 iResult=AddHLTOUTTask(chains.Data());
1344 if (iResult>=0) SetStatusFlags(kTaskListCreated);
1349 int AliHLTSystem::AddHLTOUTTask(const char* hltoutchains)
1351 // see header file for class documentation
1353 if (!hltoutchains || hltoutchains[0]==0) return 0;
1355 // check chains for output
1356 TString chains=hltoutchains;
1357 TObjArray* pTokens=chains.Tokenize(" ");
1359 int iEntries=pTokens->GetEntries();
1360 for (int i=0; i<iEntries && iResult>=0; i++) {
1361 const char* token=((TObjString*)pTokens->At(i))->GetString().Data();
1362 AliHLTConfiguration* pConf=fpConfigurationHandler->FindConfiguration(token);
1364 TString cid=pConf->GetComponentID();
1365 if (fpComponentHandler->HasOutputData(cid.Data())) {
1369 HLTWarning("can not find configuration %s", token);
1371 // remove from the list of hltout chains
1372 chains.ReplaceAll(token, "");
1377 // do not create the HLTOUT task if none of the chains have output
1378 if (chains.IsNull()) return 0;
1380 // indicate the task to be available
1384 if (strcmp(chains.Data(), fpHLTOUTTask->GetSourceChains())==0) {
1385 HLTWarning("HLTOUT task already added for chains \"%s\" %p", chains.Data(), fpHLTOUTTask);
1387 HLTError("HLTOUT task already added for chains \"%s\" %p, ignoring new chains \"%s\"",
1388 fpHLTOUTTask->GetSourceChains(), fpHLTOUTTask, chains.Data());
1393 fpHLTOUTTask=new AliHLTOUTTask(chains);
1395 if (fpHLTOUTTask->GetConf() && fpHLTOUTTask->GetConf()->SourcesResolved()>=0) {
1396 iResult=InsertTask(fpHLTOUTTask);
1398 HLTError("HLTOUT task (%s) sources not resolved", fpHLTOUTTask->GetName());
1403 delete fpHLTOUTTask;
1412 int AliHLTSystem::CheckStatus(int flag)
1414 // see header file for class documentation
1415 if (flag==kUninitialized && flag==fState) return 1;
1416 if ((fState&flag)==flag) return 1;
1420 int AliHLTSystem::GetStatusFlags()
1422 // see header file for class documentation
1426 int AliHLTSystem::SetStatusFlags(int flags)
1428 // see header file for class documentation
1433 int AliHLTSystem::ClearStatusFlags(int flags)
1435 // see header file for class documentation
1440 void (*AliHLTSystem::FindDynamicSymbol(const char* library, const char* symbol))()
1442 // see header file for class documentation
1443 if (fpComponentHandler==NULL) return NULL;
1444 return fpComponentHandler->FindSymbol(library, symbol);
1447 void AliHLTSystem::SetFrameworkLog(AliHLTComponentLogSeverity level)
1449 // see header file for class documentation
1450 SetLocalLoggingLevel(level);
1451 if (fpComponentHandler) fpComponentHandler->SetLocalLoggingLevel(level);
1452 if (fpConfigurationHandler) fpConfigurationHandler->SetLocalLoggingLevel(level);
1455 int AliHLTSystem::LoggingVarargs(AliHLTComponentLogSeverity severity,
1456 const char* originClass, const char* originFunc,
1457 const char* file, int line, ... ) const
1459 // see header file for function documentation
1463 va_start(args, line);
1465 if (!fName.IsNull())
1466 AliHLTLogging::SetLogString("%s (%p): ", fName.Data(), this);
1467 iResult=SendMessage(severity, originClass, originFunc, file, line, AliHLTLogging::BuildLogString(NULL, args, !fName.IsNull() /*append if non empty*/));