]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/BASE/AliHLTSystem.cxx
Fix loading of TPC clusters; SEGV occured with events generated with today's HEAD.
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTSystem.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 * for The ALICE HLT Project. *
f23a6e1a 9 * *
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 **************************************************************************/
18
b22e91eb 19/** @file AliHLTSystem.cxx
20 @author Matthias Richter
21 @date
22 @brief Implementation of HLT module management.
23*/
f23a6e1a 24
6a8e0bb4 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
34
f3506ea2 35#include <cassert>
85869391 36#include "AliHLTStdIncludes.h"
f23a6e1a 37#include "AliHLTSystem.h"
38#include "AliHLTComponentHandler.h"
39#include "AliHLTComponent.h"
5ec8e281 40#include "AliHLTConfiguration.h"
c38ba6f9 41#include "AliHLTConfigurationHandler.h"
42#include "AliHLTTask.h"
242bb794 43#include "AliHLTModuleAgent.h"
8451168b 44#include "AliHLTOfflineInterface.h"
457ec821 45#include "AliHLTDataSource.h"
242bb794 46#include <TObjArray.h>
47#include <TObjString.h>
90ebac25 48#include <TStopwatch.h>
dba03d72 49//#include <TSystem.h>
c043fa2c 50#include <TROOT.h>
6a8e0bb4 51//#include <TInterpreter.h>
f23a6e1a 52
83670b1d 53/** HLT default component libraries */
6a8e0bb4 54const char* AliHLTSystem::fgkHLTDefaultLibs[]= {
83670b1d 55 "libAliHLTUtil.so",
8f471af0 56 "libAliHLTRCU.so",
83670b1d 57 "libAliHLTTPC.so",
58 // "libAliHLTSample.so",
511b6d88 59 //"libAliHLTPHOS.so",
83670b1d 60 //"libAliHLTMUON.so",
61 "libAliHLTTRD.so",
62 NULL
63};
64
b22e91eb 65/** ROOT macro for the implementation of ROOT specific class methods */
f23a6e1a 66ClassImp(AliHLTSystem)
67
f23a6e1a 68AliHLTSystem::AliHLTSystem()
85869391 69 :
70 fpComponentHandler(new AliHLTComponentHandler()),
71 fpConfigurationHandler(new AliHLTConfigurationHandler()),
242bb794 72 fTaskList(),
c043fa2c 73 fState(0),
dee38f1b 74 fChains(),
75 fStopwatches(new TObjArray),
76 fEventCount(-1),
77 fGoodEvents(-1)
f23a6e1a 78{
70ed7d01 79 // see header file for class documentation
80 // or
81 // refer to README to build package
82 // or
83 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
5f5b708b 84
fc455fba 85 if (fgNofInstances++>0)
86 HLTWarning("multiple instances of AliHLTSystem, you should not use more than one at a time");
87
5f5b708b 88 SetGlobalLoggingLevel(kHLTLogDefault);
dba03d72 89 SetFrameworkLog(kHLTLogDefault);
f23a6e1a 90 if (fpComponentHandler) {
91 AliHLTComponentEnvironment env;
92 memset(&env, 0, sizeof(AliHLTComponentEnvironment));
9ce4bf4a 93 env.fAllocMemoryFunc=AliHLTSystem::AllocMemory;
fc455fba 94 env.fLoggingFunc=NULL;
f23a6e1a 95 fpComponentHandler->SetEnvironment(&env);
db95fec3 96 InitAliLogFunc(fpComponentHandler);
a742f6f8 97 fpComponentHandler->AnnounceVersion();
85465857 98 } else {
99 HLTFatal("can not create Component Handler");
100 }
85465857 101 if (fpConfigurationHandler) {
102 AliHLTConfiguration::GlobalInit(fpConfigurationHandler);
103 } else {
104 HLTFatal("can not create Configuration Handler");
f23a6e1a 105 }
106}
107
f23a6e1a 108AliHLTSystem::~AliHLTSystem()
109{
70ed7d01 110 // see header file for class documentation
fc455fba 111 fgNofInstances--;
2d7ff710 112 CleanTaskList();
fc455fba 113 AliHLTConfiguration::GlobalDeinit(fpConfigurationHandler);
2d7ff710 114 if (fpConfigurationHandler) {
115 delete fpConfigurationHandler;
116 }
117 fpConfigurationHandler=NULL;
118
119 if (fpComponentHandler) {
120 delete fpComponentHandler;
121 }
122 fpComponentHandler=NULL;
f23a6e1a 123}
124
fc455fba 125int AliHLTSystem::fgNofInstances=0;
126
5ec8e281 127int AliHLTSystem::AddConfiguration(AliHLTConfiguration* pConf)
128{
70ed7d01 129 // see header file for class documentation
6a8e0bb4 130 HLTLogKeyword("configuration handling");
5ec8e281 131 int iResult=0;
53feaef5 132 if (pConf) {
6a8e0bb4 133 HLTError("function not yet implemented");
134 iResult=-ENOSYS;
53feaef5 135 } else {
136 iResult=-EINVAL;
137 }
5ec8e281 138 return iResult;
139}
140
014d39ce 141int AliHLTSystem::InsertConfiguration(AliHLTConfiguration* pConf, AliHLTConfiguration* pPrec)
142{
70ed7d01 143 // see header file for class documentation
6a8e0bb4 144 HLTLogKeyword("configuration handling");
014d39ce 145 int iResult=0;
53feaef5 146 if (pConf) {
147 if (pPrec) {
148 // find the position
6a8e0bb4 149 HLTError("function not yet implemented");
150 iResult=-ENOSYS;
53feaef5 151 }
152 } else {
153 iResult=-EINVAL;
154 }
014d39ce 155 return iResult;
156}
5ec8e281 157
158int AliHLTSystem::DeleteConfiguration(AliHLTConfiguration* pConf)
159{
70ed7d01 160 // see header file for class documentation
6a8e0bb4 161 HLTLogKeyword("configuration handling");
5ec8e281 162 int iResult=0;
53feaef5 163 if (pConf) {
6a8e0bb4 164 HLTError("function not yet implemented");
165 iResult=-ENOSYS;
53feaef5 166 } else {
167 iResult=-EINVAL;
168 }
5ec8e281 169 return iResult;
170}
171
a742f6f8 172int AliHLTSystem::BuildTaskList(const char* id)
173{
174 // see header file for class documentation
175 int iResult=0;
176 if (id) {
177 if (fpConfigurationHandler) {
178 AliHLTConfiguration* pConf=fpConfigurationHandler->FindConfiguration(id);
179 if (pConf) {
180 iResult=BuildTaskList(pConf);
181 } else {
182 HLTError("unknown configuration \"%s\"", id);
183 iResult=-EEXIST;
184 }
185 } else {
186 iResult=-EFAULT;
187 }
188 } else {
189 iResult=-EINVAL;
190 }
191 return iResult;
192}
193
5ec8e281 194int AliHLTSystem::BuildTaskList(AliHLTConfiguration* pConf)
195{
70ed7d01 196 // see header file for class documentation
f23a6e1a 197 int iResult=0;
5ec8e281 198 if (pConf) {
199 AliHLTTask* pTask=NULL;
200 if ((pTask=FindTask(pConf->GetName()))!=NULL) {
201 if (pTask->GetConf()!=pConf) {
85465857 202 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);
5ec8e281 203 iResult=-EEXIST;
5ec8e281 204 }
c043fa2c 205 // task for this configuration exists, terminate
206 pTask=NULL;
5ec8e281 207 } else if (pConf->SourcesResolved(1)!=1) {
85465857 208 HLTError("configuration \"%s\" has unresolved sources, aborting ...", pConf->GetName());
5ec8e281 209 iResult=-ENOLINK;
210 } else {
53feaef5 211 pTask=new AliHLTTask(pConf);
5ec8e281 212 if (pTask==NULL) {
213 iResult=-ENOMEM;
dba03d72 214 } else {
215 pTask->SetLocalLoggingLevel(GetLocalLoggingLevel());
5ec8e281 216 }
217 }
c043fa2c 218 static int iterationLevel=0;
219 if (pTask && iResult>=0) {
3b35e87c 220 // check for circular dependencies
5ec8e281 221 if ((iResult=pConf->FollowDependency(pConf->GetName()))>0) {
3b35e87c 222 HLTError("detected circular dependency for configuration \"%s\"", pTask->GetName());
5ec8e281 223 pTask->PrintDependencyTree(pTask->GetName(), 1/*use the configuration list*/);
85465857 224 HLTError("aborted ...");
5ec8e281 225 iResult=-ELOOP;
226 }
227 if (iResult>=0) {
228 // check whether all dependencies are already in the task list
229 // create the missing ones
3b35e87c 230 // this step is an iterative process which calls this function again for the missing
231 // configurations, in order to avoid the currently processed task to be created
232 // again it is added to the list temporarily and removed afterwards
233 // This is of high importance to preserve the order of the tasks. Furthermore, the
234 // InsertTask method has to be used in order to set all the cross links right
5ec8e281 235 fTaskList.Add(pTask);
236 AliHLTConfiguration* pDep=pConf->GetFirstSource();
237 while (pDep!=NULL && iResult>=0) {
c043fa2c 238 HLTDebug("iteration %d: checking dependency %s (%p)", iterationLevel, pDep->GetName(), pDep);
5ec8e281 239 if (FindTask(pDep->GetName())==NULL) {
c043fa2c 240 HLTDebug("iteration %d: building task list for configuration %s (%p)", iterationLevel, pDep->GetName(), pDep);
241 iterationLevel++;
5ec8e281 242 iResult=BuildTaskList(pDep);
c043fa2c 243 iterationLevel--;
5ec8e281 244 }
245 pDep=pConf->GetNextSource();
246 }
3b35e87c 247 // remove the temporarily added task
5ec8e281 248 fTaskList.Remove(pTask);
249
250 // insert the task and set the cross-links
251 if (iResult>=0) {
c043fa2c 252 HLTDebug("iteration %d: inserting task %s (%p)", iterationLevel, pTask->GetName(), pTask);
5ec8e281 253 iResult=InsertTask(pTask);
254 }
255 } else {
256 delete pTask;
257 pTask=NULL;
258 }
259 }
260 } else {
261 iResult=-EINVAL;
f23a6e1a 262 }
f23a6e1a 263 return iResult;
264}
265
5ec8e281 266int AliHLTSystem::CleanTaskList()
267{
70ed7d01 268 // see header file for class documentation
5ec8e281 269 int iResult=0;
270 TObjLink* lnk=NULL;
a742f6f8 271 while ((lnk=fTaskList.LastLink())!=NULL) {
5ec8e281 272 delete (lnk->GetObject());
a742f6f8 273 fTaskList.Remove(lnk);
5ec8e281 274 }
275 return iResult;
276}
277
278int AliHLTSystem::InsertTask(AliHLTTask* pTask)
279{
70ed7d01 280 // see header file for class documentation
5ec8e281 281 int iResult=0;
282 TObjLink *lnk = NULL;
283 if ((iResult=pTask->CheckDependencies())>0)
284 lnk=fTaskList.FirstLink();
285 while (lnk && iResult>0) {
286 AliHLTTask* pCurr = (AliHLTTask*)lnk->GetObject();
85465857 287 //HLTDebug("checking \"%s\"", pCurr->GetName());
5ec8e281 288 iResult=pTask->Depends(pCurr);
289 if (iResult>0) {
290 iResult=pTask->SetDependency(pCurr);
291 pCurr->SetTarget(pTask);
85465857 292 HLTDebug("set dependency \"%s\" for configuration \"%s\"", pCurr->GetName(), pTask->GetName());
f23a6e1a 293 }
5ec8e281 294 if (pCurr->Depends(pTask)) {
3b35e87c 295 // circular dependency
296 HLTError("circular dependency: can not resolve dependencies for configuration \"%s\"", pTask->GetName());
5ec8e281 297 iResult=-ELOOP;
298 } else if ((iResult=pTask->CheckDependencies())>0) {
299 lnk = lnk->Next();
300 }
301 }
302 if (iResult==0) {
303 if (lnk) {
304 fTaskList.AddAfter(lnk, pTask);
305 } else {
306 fTaskList.AddFirst(pTask);
307 }
a742f6f8 308 HLTDebug("task \"%s\" (%p) inserted (size %d)", pTask->GetName(), pTask, sizeof(AliHLTTask));
5ec8e281 309 } else if (iResult>0) {
85465857 310 HLTError("can not resolve dependencies for configuration \"%s\" (%d unresolved)", pTask->GetName(), iResult);
5ec8e281 311 iResult=-ENOLINK;
f23a6e1a 312 }
5ec8e281 313 return iResult;
f23a6e1a 314}
315
5ec8e281 316AliHLTTask* AliHLTSystem::FindTask(const char* id)
317{
70ed7d01 318 // see header file for class documentation
5ec8e281 319 AliHLTTask* pTask=NULL;
320 if (id) {
f3506ea2 321 pTask=dynamic_cast<AliHLTTask*>(fTaskList.FindObject(id));
5ec8e281 322 }
323 return pTask;
324}
f23a6e1a 325
5ec8e281 326void AliHLTSystem::PrintTaskList()
327{
70ed7d01 328 // see header file for class documentation
85465857 329 HLTLogKeyword("task list");
5ec8e281 330 TObjLink *lnk = NULL;
85465857 331 HLTMessage("Task List");
5ec8e281 332 lnk=fTaskList.FirstLink();
333 while (lnk) {
334 TObject* obj=lnk->GetObject();
335 if (obj) {
85465857 336 HLTMessage(" %s - status:", obj->GetName());
5ec8e281 337 AliHLTTask* pTask=(AliHLTTask*)obj;
338 pTask->PrintStatus();
339 } else {
340 }
341 lnk = lnk->Next();
342 }
343}
014d39ce 344
dee38f1b 345int AliHLTSystem::Run(Int_t iNofEvents, int bStop)
014d39ce 346{
70ed7d01 347 // see header file for class documentation
014d39ce 348 int iResult=0;
242bb794 349 int iCount=0;
350 SetStatusFlags(kRunning);
dee38f1b 351 if (fEventCount>=0 || (iResult=InitTasks())>=0) {
352 if (fEventCount>=0 || (iResult=StartTasks())>=0) {
353 if (fEventCount==0) {
354 InitBenchmarking(fStopwatches);
355 } else {
356 //ResumeBenchmarking(fStopwatches);
357 }
358 for (int i=fEventCount; i<fEventCount+iNofEvents && iResult>=0; i++) {
359 if ((iResult=ProcessTasks(i))>=0) {
360 fGoodEvents++;
242bb794 361 iCount++;
9ce4bf4a 362 } else {
9ce4bf4a 363 // TODO: define different running modes to either ignore errors in
364 // event processing or not
365 // currently ignored
dee38f1b 366 iResult=0;
9ce4bf4a 367 }
2d7ff710 368 }
dee38f1b 369 fEventCount+=iNofEvents;
370 if (bStop) StopTasks();
371 //else PauseBenchmarking(fStopwatches);
53feaef5 372 }
dee38f1b 373 if (bStop) DeinitTasks();
9ce4bf4a 374 }
90ebac25 375 if (iResult>=0) {
376 iResult=iCount;
ba7f962b 377 } else if (iResult==-126 /*ENOKEY*/) {
83670b1d 378 iResult=0; // do not propagate the error
90ebac25 379 }
242bb794 380 ClearStatusFlags(kRunning);
9ce4bf4a 381 return iResult;
382}
383
384int AliHLTSystem::InitTasks()
385{
70ed7d01 386 // see header file for class documentation
9ce4bf4a 387 int iResult=0;
388 TObjLink *lnk=fTaskList.FirstLink();
dee38f1b 389
fc455fba 390 if (lnk==NULL) {
3d6633ff 391 HLTWarning("Task list is empty, skipping HLT");
ba7f962b 392 return -126 /*ENOKEY*/;
fc455fba 393 }
9ce4bf4a 394 while (lnk && iResult>=0) {
395 TObject* obj=lnk->GetObject();
396 if (obj) {
397 AliHLTTask* pTask=(AliHLTTask*)obj;
398 iResult=pTask->Init(NULL, fpComponentHandler);
dba03d72 399// ProcInfo_t ProcInfo;
400// gSystem->GetProcInfo(&ProcInfo);
401// HLTInfo("task %s initialized (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
9ce4bf4a 402 } else {
403 }
404 lnk = lnk->Next();
405 }
406 if (iResult<0) {
dee38f1b 407 HLTError("can not initialize task list, error %d", iResult);
53feaef5 408 }
dee38f1b 409
53feaef5 410 return iResult;
411}
412
90ebac25 413int AliHLTSystem::InitBenchmarking(TObjArray* pStopwatches)
414{
415 // see header file for class documentation
90ebac25 416 int iResult=0;
dee38f1b 417 if (pStopwatches==NULL) return 0;
418
419 for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
420 TStopwatch* pStopwatch= new TStopwatch;
421 if (pStopwatch) {
422 pStopwatch->Reset();
423 pStopwatches->AddAt(pStopwatch, i);
424 } else {
425 iResult=-ENOMEM;
426 break;
427 }
428 }
429
90ebac25 430 TObjLink *lnk=fTaskList.FirstLink();
431 while (lnk && iResult>=0) {
432 TObject* obj=lnk->GetObject();
433 if (obj) {
434 AliHLTTask* pTask=(AliHLTTask*)obj;
435 AliHLTComponent* pComp=NULL;
436 if (iResult>=0 && (pComp=pTask->GetComponent())!=NULL) {
437 switch (pComp->GetComponentType()) {
438 case AliHLTComponent::kProcessor:
439 pComp->SetStopwatches(pStopwatches);
440 break;
441 case AliHLTComponent::kSource:
442 {
443 // this switch determines whether the time consumption of the
444 // AliHLTComponent base methods should be counted to the input
445 // stopwatch or base stopwatch.
446 //int inputBase=(int)AliHLTComponent::kSWBase;
447 int inputBase=(int)AliHLTComponent::kSWInput;
448 pComp->SetStopwatch(pStopwatches->At(inputBase), AliHLTComponent::kSWBase);
449 pComp->SetStopwatch(pStopwatches->At((int)AliHLTComponent::kSWInput), AliHLTComponent::kSWDA);
450 }
451 break;
452 case AliHLTComponent::kSink:
453 {
454 // this switch determines whether the time consumption of the
455 // AliHLTComponent base methods should be counted to the output
456 // stopwatch or base stopwatch.
457 //int outputBase=(int)AliHLTComponent::kSWBase;
458 int outputBase=(int)AliHLTComponent::kSWOutput;
459 pComp->SetStopwatch(pStopwatches->At(outputBase), AliHLTComponent::kSWBase);
460 pComp->SetStopwatch(pStopwatches->At((int)AliHLTComponent::kSWOutput), AliHLTComponent::kSWDA);
461 }
462 break;
85f0cede 463 default:
464 HLTWarning("unknown component type %d", (int)pComp->GetComponentType());
90ebac25 465 }
466 }
467 } else {
468 }
469 lnk = lnk->Next();
470 }
471 return iResult;
472}
473
dee38f1b 474int AliHLTSystem::PrintBenchmarking(TObjArray* pStopwatches, int bClean)
475{
476 // see header file for class documentation
477 int iInitialized=1;
478 if (pStopwatches==NULL) return 0;
479
480 for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
481 if (!dynamic_cast<TStopwatch*>(pStopwatches->At(i))) {
482 iInitialized=0;
483 break;
484 }
485 }
486
487 if (iInitialized!=0) {
488 HLTInfo("HLT statistics:\n"
489 " base: R:%.3fs C:%.3fs\n"
490 " input: R:%.3fs C:%.3fs\n"
491 " output: R:%.3fs C:%.3fs\n"
492 " event processing : R:%.3fs C:%.3fs"
493 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWBase))->RealTime()
494 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWBase))->CpuTime()
495 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWInput))->RealTime()
496 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWInput))->CpuTime()
497 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWOutput))->RealTime()
498 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWOutput))->CpuTime()
499 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWDA))->RealTime()
500 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWDA))->CpuTime()
501 );
502 }
503
504 if (bClean) {
505 for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
506 TObject* pObj=pStopwatches->RemoveAt(i);
507 if (pObj) delete pObj;
508 }
509 }
510 return 0;
511}
512
53feaef5 513int AliHLTSystem::StartTasks()
514{
70ed7d01 515 // see header file for class documentation
53feaef5 516 int iResult=0;
517 TObjLink *lnk=fTaskList.FirstLink();
518 while (lnk && iResult>=0) {
519 TObject* obj=lnk->GetObject();
520 if (obj) {
521 AliHLTTask* pTask=(AliHLTTask*)obj;
522 iResult=pTask->StartRun();
dba03d72 523// ProcInfo_t ProcInfo;
524// gSystem->GetProcInfo(&ProcInfo);
525// HLTInfo("task %s started (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
53feaef5 526 } else {
527 }
528 lnk = lnk->Next();
529 }
530 if (iResult<0) {
dee38f1b 531 HLTError("can not start task list, error %d", iResult);
532 } else {
533 fEventCount=0;
534 fGoodEvents=0;
457ec821 535 if ((iResult=SendControlEvent(kAliHLTDataTypeSOR))<0) {
536 HLTError("can not send SOR event");
537 }
53feaef5 538 }
539 return iResult;
540}
541
542int AliHLTSystem::ProcessTasks(Int_t eventNo)
543{
70ed7d01 544 // see header file for class documentation
53feaef5 545 int iResult=0;
546 HLTDebug("processing event no %d", eventNo);
547 TObjLink *lnk=fTaskList.FirstLink();
548 while (lnk && iResult>=0) {
549 TObject* obj=lnk->GetObject();
550 if (obj) {
551 AliHLTTask* pTask=(AliHLTTask*)obj;
9ce4bf4a 552 iResult=pTask->ProcessTask(eventNo);
dba03d72 553// ProcInfo_t ProcInfo;
554// gSystem->GetProcInfo(&ProcInfo);
555// HLTInfo("task %s processed (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
53feaef5 556 } else {
557 }
558 lnk = lnk->Next();
559 }
dee38f1b 560
561 if (iResult>=0) {
8f471af0 562 HLTImportant("Event %d successfully finished (%d)", eventNo, iResult);
dee38f1b 563 iResult=0;
564 } else {
565 HLTError("Processing of event %d failed (%d)", eventNo, iResult);
566 }
567
53feaef5 568 return iResult;
569}
570
571int AliHLTSystem::StopTasks()
572{
70ed7d01 573 // see header file for class documentation
53feaef5 574 int iResult=0;
457ec821 575 if ((iResult=SendControlEvent(kAliHLTDataTypeEOR))<0) {
576 HLTError("can not send EOR event");
577 }
53feaef5 578 TObjLink *lnk=fTaskList.FirstLink();
457ec821 579 while (lnk) {
53feaef5 580 TObject* obj=lnk->GetObject();
581 if (obj) {
582 AliHLTTask* pTask=(AliHLTTask*)obj;
457ec821 583 int locResult=pTask->EndRun();
584 if (iResult>=0 && locResult<0) iResult=locResult;
dba03d72 585// ProcInfo_t ProcInfo;
586// gSystem->GetProcInfo(&ProcInfo);
587// HLTInfo("task %s stopped (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
53feaef5 588 } else {
589 }
590 lnk = lnk->Next();
591 }
dee38f1b 592 PrintBenchmarking(fStopwatches, 1 /*clean*/);
014d39ce 593 return iResult;
594}
9ce4bf4a 595
457ec821 596int AliHLTSystem::SendControlEvent(AliHLTComponentDataType dt)
597{
598 // see header file for class documentation
599
600 // disabled for the moment
601 return 0;
602
603 int iResult=0;
604 AliHLTRunDesc runDesc;
605 memset(&runDesc, 0, sizeof(AliHLTRunDesc));
606 runDesc.fStructSize=sizeof(AliHLTRunDesc);
607 AliHLTDataSource::AliSpecialEventGuard g(&runDesc, dt, kAliHLTVoidDataSpec);
608 HLTDebug("sending event %s, run descriptor %p", AliHLTComponent::DataType2Text(dt).c_str(), &runDesc);
609 TObjLink *lnk=fTaskList.FirstLink();
610 while (lnk && iResult>=0) {
611 TObject* obj=lnk->GetObject();
612 if (obj) {
613 AliHLTTask* pTask=(AliHLTTask*)obj;
614 iResult=pTask->ProcessTask(-1);
615 } else {
616 }
617 lnk = lnk->Next();
618 }
619 HLTDebug("event %s done (%d)", AliHLTComponent::DataType2Text(dt).c_str(), iResult);
620 return iResult;
621}
622
9ce4bf4a 623int AliHLTSystem::DeinitTasks()
624{
70ed7d01 625 // see header file for class documentation
9ce4bf4a 626 int iResult=0;
627 TObjLink *lnk=fTaskList.FirstLink();
628 while (lnk && iResult>=0) {
629 TObject* obj=lnk->GetObject();
630 if (obj) {
631 AliHLTTask* pTask=(AliHLTTask*)obj;
632 iResult=pTask->Deinit();
dba03d72 633// ProcInfo_t ProcInfo;
634// gSystem->GetProcInfo(&ProcInfo);
635// HLTInfo("task %s cleaned (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
9ce4bf4a 636 } else {
637 }
638 lnk = lnk->Next();
639 }
dee38f1b 640 fEventCount=-1;
641 fGoodEvents=-1;
642
9ce4bf4a 643 return iResult;
644}
645
c043fa2c 646void* AliHLTSystem::AllocMemory( void* /*param*/, unsigned long size )
9ce4bf4a 647{
70ed7d01 648 // see header file for class documentation
c043fa2c 649 void* p=NULL;
650 try {
651 p=(void*)new char[size];
242bb794 652 }
c043fa2c 653 catch (...) {
654 AliHLTLogging log;
655 log.LoggingVarargs(kHLTLogError, "AliHLTSystem" , "AllocMemory" , __FILE__ , __LINE__ , "exeption during memory allocation" );
656 }
657 return p;
9ce4bf4a 658}
242bb794 659
660int AliHLTSystem::Reconstruct(int nofEvents, AliRunLoader* runLoader,
661 AliRawReader* rawReader)
662{
663 // see header file for class documentation
664 int iResult=0;
dee38f1b 665 if (runLoader || rawReader || nofEvents==0) {
666 if (nofEvents>0) {HLTInfo("Run Loader %p, Raw Reader %p , %d event(s)", runLoader, rawReader, nofEvents);}
242bb794 667 if (CheckStatus(kReady)) {
dee38f1b 668 if (nofEvents==0) {
669 // special case to close the reconstruction
f3506ea2 670 if (!CheckStatus(kError)) {
dee38f1b 671 StopTasks();
672 DeinitTasks();
f3506ea2 673 }
dee38f1b 674 } else {
8451168b 675 if ((iResult=AliHLTOfflineInterface::SetParamsToComponents(runLoader, rawReader))>=0) {
dee38f1b 676 // the system always remains started after event processing, a specific
f3506ea2 677 // call with nofEvents==0 is needed to execute the stop sequence
678 if ((iResult=Run(nofEvents, 0))<0) SetStatusFlags(kError);
dee38f1b 679 }
8451168b 680 }
242bb794 681 } else {
682 HLTError("wrong state %#x, required flags %#x", GetStatusFlags(), kReady);
683 }
684 } else {
0bf7536b 685 HLTError("missing RunLoader (%p)/RawReader (%p) instance", runLoader, rawReader);
242bb794 686 iResult=-EINVAL;
687 }
688 return iResult;
689}
690
af885e0f 691int AliHLTSystem::FillESD(int eventNo, AliRunLoader* runLoader, AliESDEvent* esd)
242bb794 692{
693 // see header file for class documentation
694 int iResult=0;
dee38f1b 695 if (runLoader || esd) {
242bb794 696 HLTInfo("Event %d: Run Loader %p, ESD %p", eventNo, runLoader, esd);
8451168b 697 iResult=AliHLTOfflineInterface::FillComponentESDs(eventNo, runLoader, esd);
242bb794 698 } else {
699 HLTError("missing run loader/ESD instance(s)");
700 iResult=-EINVAL;
701 }
702 return iResult;
703}
704
705int AliHLTSystem::LoadComponentLibraries(const char* libraries)
706{
707 // see header file for class documentation
708 int iResult=0;
709 if (libraries) {
710 if (fpComponentHandler) {
711 TString libs(libraries);
712 TObjArray* pTokens=libs.Tokenize(" ");
713 if (pTokens) {
714 int iEntries=pTokens->GetEntries();
715 for (int i=0; i<iEntries && iResult>=0; i++) {
716 iResult=fpComponentHandler->LoadLibrary((((TObjString*)pTokens->At(i))->GetString()).Data());
717 }
718 delete pTokens;
719 }
720 if (iResult>=0) {
721 SetStatusFlags(kLibrariesLoaded);
722 } else {
723 // lets see if we need this, probably not
724 //fpComponentHandler->UnloadLibraries();
725 ClearStatusFlags(kLibrariesLoaded);
726 }
727 } else {
728 iResult=-EFAULT;
729 HLTFatal("no component handler available");
730 }
731 } else {
732 iResult=-EINVAL;
733 }
734 return iResult;
735}
736
737int AliHLTSystem::Configure(AliRunLoader* runloader)
dee38f1b 738{
739 // see header file for class documentation
f3506ea2 740 return Configure(NULL, runloader);
dee38f1b 741}
742
743int AliHLTSystem::Configure(AliRawReader* rawReader, AliRunLoader* runloader)
242bb794 744{
745 // see header file for class documentation
746 int iResult=0;
747 if (CheckStatus(kRunning)) {
748 HLTError("HLT system in running state, can not configure");
749 return -EBUSY;
750 }
e642a402 751 ClearStatusFlags(kTaskListCreated);
c215072c 752 if (CheckFilter(kHLTLogDebug))
753 AliHLTModuleAgent::PrintStatus();
c043fa2c 754 if (CheckStatus(kConfigurationLoaded)==0) {
dee38f1b 755 iResult=LoadConfigurations(rawReader, runloader);
c043fa2c 756 } else {
dee38f1b 757 if (fChains.Length()==0) {
c043fa2c 758 HLTError("custom configuration(s) specified, but no configuration to run in local reconstruction, use \'localrec=<conf>\' option");
759 iResult=-ENOENT;
760 }
761 }
242bb794 762 if (iResult>=0) {
763 SetStatusFlags(kConfigurationLoaded);
c043fa2c 764 if (CheckFilter(kHLTLogDebug))
765 fpConfigurationHandler->PrintConfigurations();
f3506ea2 766 iResult=BuildTaskListsFromReconstructionChains(rawReader, runloader);
242bb794 767 if (iResult>=0) {
768 SetStatusFlags(kTaskListCreated);
769 }
770 }
771 if (iResult<0) SetStatusFlags(kError);
772
773 return iResult;
774}
775
c043fa2c 776int AliHLTSystem::ScanOptions(const char* options)
777{
778 // see header file for class documentation
779 int iResult=0;
780 if (options) {
d76bc02a 781 //AliHLTComponentHandler::TLibraryMode libMode=AliHLTComponentHandler::kDynamic;
83670b1d 782 TString libs("");
c043fa2c 783 TString alloptions(options);
784 TObjArray* pTokens=alloptions.Tokenize(" ");
785 if (pTokens) {
786 int iEntries=pTokens->GetEntries();
787 for (int i=0; i<iEntries; i++) {
788 TString token=(((TObjString*)pTokens->At(i))->GetString());
789 if (token.Contains("loglevel=")) {
790 TString param=token.ReplaceAll("loglevel=", "");
791 if (param.IsDigit()) {
792 SetGlobalLoggingLevel((AliHLTComponentLogSeverity)param.Atoi());
793 } else if (param.BeginsWith("0x") &&
794 param.Replace(0,2,"",0).IsHex()) {
795 int severity=0;
796 sscanf(param.Data(),"%x", &severity);
797 SetGlobalLoggingLevel((AliHLTComponentLogSeverity)severity);
798 } else {
799 HLTWarning("wrong parameter for option \'loglevel=\', (hex) number expected");
800 }
dba03d72 801 } else if (token.Contains("frameworklog=")) {
802 TString param=token.ReplaceAll("frameworklog=", "");
803 if (param.IsDigit()) {
804 SetFrameworkLog((AliHLTComponentLogSeverity)param.Atoi());
805 } else if (param.BeginsWith("0x") &&
806 param.Replace(0,2,"",0).IsHex()) {
807 int severity=0;
808 sscanf(param.Data(),"%x", &severity);
809 SetFrameworkLog((AliHLTComponentLogSeverity)severity);
810 } else {
811 HLTWarning("wrong parameter for option \'loglevel=\', (hex) number expected");
812 }
c043fa2c 813 } else if (token.Contains("alilog=off")) {
814 SwitchAliLog(0);
815 } else if (token.Contains("config=")) {
816 TString param=token.ReplaceAll("config=", "");
817 Int_t error=0;
818 gROOT->Macro(param.Data(), &error);
819 if (error==0) {
820 SetStatusFlags(kConfigurationLoaded);
821 } else {
822 HLTError("can not execute macro \'%s\'", param.Data());
823 iResult=-EBADF;
824 }
d85f150c 825 } else if (token.Contains("chains=")) {
dee38f1b 826 TString param=token.ReplaceAll("chains=", "");
827 fChains=param.ReplaceAll(",", " ");
dba03d72 828 } else if (token.Contains("libmode=")) {
829 TString param=token.ReplaceAll("libmode=", "");
830 param.ReplaceAll(",", " ");
831 if (fpComponentHandler) {
832 if (param.CompareTo("static")==0) {
833 fpComponentHandler->SetLibraryMode(AliHLTComponentHandler::kStatic);
834 } else if (param.CompareTo("dynamic")==0) {
835 fpComponentHandler->SetLibraryMode(AliHLTComponentHandler::kDynamic);
836 } else {
837 HLTWarning("wrong argument for option \'libmode=\', use \'static\' or \'dynamic\'");
838 }
839 }
83670b1d 840 } else if (token.BeginsWith("lib") && token.EndsWith(".so")) {
841 libs+=token;
842 libs+=" ";
c043fa2c 843 } else {
844 HLTWarning("unknown option \'%s\'", token.Data());
845 }
846 }
847 delete pTokens;
848 }
83670b1d 849
850 if (iResult>=0) {
851 if (libs.IsNull()) {
6a8e0bb4 852 const char** deflib=fgkHLTDefaultLibs;
83670b1d 853 while (*deflib) {
854 libs+=*deflib++;
855 libs+=" ";
856 }
857 }
858 if ((!CheckStatus(AliHLTSystem::kLibrariesLoaded)) &&
859 (LoadComponentLibraries(libs.Data())<0)) {
860 HLTError("error while loading HLT libraries");
861 iResult=-EFAULT;
862 }
863 }
c043fa2c 864 }
865 return iResult;
866}
867
242bb794 868int AliHLTSystem::Reset(int bForce)
869{
870 // see header file for class documentation
871 int iResult=0;
872 if (!bForce && CheckStatus(kRunning)) {
873 HLTError("HLT system in running state, can not configure");
874 return -EBUSY;
875 }
876 CleanTaskList();
877 ClearStatusFlags(~kUninitialized);
878 return iResult;
879}
880
dee38f1b 881int AliHLTSystem::LoadConfigurations(AliRawReader* rawReader, AliRunLoader* runloader)
242bb794 882{
883 // see header file for class documentation
884 if (CheckStatus(kRunning)) {
885 HLTError("HLT system in running state, can not configure");
886 return -EBUSY;
887 }
888 int iResult=0;
889 AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent();
60b26a17 890 while (pAgent && iResult>=0) {
891 const char* deplibs=pAgent->GetRequiredComponentLibraries();
892 if (deplibs) {
c215072c 893 HLTDebug("load libraries \'%s\' for agent %s (%p)", deplibs, pAgent->GetName(), pAgent);
60b26a17 894 iResult=LoadComponentLibraries(deplibs);
895 }
896 if (iResult>=0) {
897 HLTDebug("load configurations for agent %s (%p)", pAgent->GetName(), pAgent);
dee38f1b 898 pAgent->CreateConfigurations(fpConfigurationHandler, rawReader, runloader);
60b26a17 899 pAgent=AliHLTModuleAgent::GetNextAgent();
900 }
242bb794 901 }
902 return iResult;
903}
904
f3506ea2 905int AliHLTSystem::BuildTaskListsFromReconstructionChains(AliRawReader* rawReader, AliRunLoader* runloader)
242bb794 906{
907 // see header file for class documentation
908 if (CheckStatus(kRunning)) {
909 HLTError("HLT system in running state, can not configure");
910 return -EBUSY;
911 }
912 if (!CheckStatus(kConfigurationLoaded)) {
913 HLTWarning("configurations not yet loaded");
914 return 0;
915 }
916
917 int iResult=0;
f3506ea2 918 int bHaveOutput=0;
919
920 // query chains
921 TString chains;
922 if (fChains.Length()>0) {
923 chains=fChains;
8f471af0 924 HLTImportant("custom reconstruction chain: %s", chains.Data());
f3506ea2 925 } else {
926 AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent();
927 while ((pAgent || fChains.Length()>0) && iResult>=0) {
928 const char* agentchains=pAgent->GetReconstructionChains(rawReader, runloader);
929 if (agentchains) {
930 if (!chains.IsNull()) chains+="";
931 chains+=agentchains;
932 HLTInfo("reconstruction chains for agent %s (%p): %s", pAgent->GetName(), pAgent, agentchains);
933 }
934 pAgent=AliHLTModuleAgent::GetNextAgent();
c043fa2c 935 }
f3506ea2 936 }
937
938 // build task list for chains
939 TObjArray* pTokens=chains.Tokenize(" ");
940 if (pTokens) {
941 int iEntries=pTokens->GetEntries();
942 for (int i=0; i<iEntries && iResult>=0; i++) {
943 const char* pCID=((TObjString*)pTokens->At(i))->GetString().Data();
944 AliHLTConfiguration* pConf=fpConfigurationHandler->FindConfiguration(pCID);
945 if (pConf) {
946 iResult=BuildTaskList(pConf);
947 if (runloader) {
948 assert(fpComponentHandler!=NULL);
949 TString cid=pConf->GetComponentID();
950 if (cid.CompareTo("HLTOUT")==0) {
951 // remove from the input of a global HLTOUT configuration
952 chains.ReplaceAll(pCID, "");
953 } else if (bHaveOutput==0) {
954 // check whether this configuration produces data output
955 if ((bHaveOutput=fpComponentHandler->HasOutputData(cid.Data()))<0) {
956 bHaveOutput=0;
957 chains.ReplaceAll(pCID, "");
958 }
959 }
242bb794 960 }
f3506ea2 961 } else {
962 HLTWarning("can not find configuration %s", pCID);
242bb794 963 }
242bb794 964 }
f3506ea2 965 delete pTokens;
966 }
967
968 // build HLTOUT for simulation
969 if (iResult>=0 && runloader) {
970 if (bHaveOutput) {
971 // there are components in the chain which produce data which need to be
972 // piped to an HLTOUT
973 if (fpComponentHandler->FindComponentIndex("HLTOUT")>=0 ||
974 LoadComponentLibraries("libHLTsim.so")>=0) {
975 AliHLTConfiguration globalout("_globalout_", "HLTOUT", chains.Data(), NULL);
976 iResult=BuildTaskList("_globalout_");
977 } else {
978 HLTError("can not load libHLTsim.so and HLTOUT component");
979 iResult=-EFAULT;
980 }
c043fa2c 981 }
242bb794 982 }
f3506ea2 983
242bb794 984 if (iResult>=0) SetStatusFlags(kTaskListCreated);
985
986 return iResult;
987}
988
989int AliHLTSystem::CheckStatus(int flag)
990{
991 // see header file for class documentation
992 if (flag==kUninitialized && flag==fState) return 1;
993 if ((fState&flag)==flag) return 1;
994 return 0;
995}
996
997int AliHLTSystem::GetStatusFlags()
998{
999 // see header file for class documentation
1000 return fState;
1001}
1002
1003int AliHLTSystem::SetStatusFlags(int flags)
1004{
1005 // see header file for class documentation
1006 fState|=flags;
1007 return fState;
1008}
1009
1010int AliHLTSystem::ClearStatusFlags(int flags)
1011{
1012 // see header file for class documentation
1013 fState&=~flags;
1014 return fState;
1015}
85f0cede 1016
1017void* AliHLTSystem::FindDynamicSymbol(const char* library, const char* symbol)
1018{
c043fa2c 1019 // see header file for class documentation
85f0cede 1020 if (fpComponentHandler==NULL) return NULL;
1021 return fpComponentHandler->FindSymbol(library, symbol);
1022}
dba03d72 1023
1024void AliHLTSystem::SetFrameworkLog(AliHLTComponentLogSeverity level)
1025{
6a8e0bb4 1026 // see header file for class documentation
dba03d72 1027 SetLocalLoggingLevel(level);
1028 if (fpComponentHandler) fpComponentHandler->SetLocalLoggingLevel(level);
1029 if (fpConfigurationHandler) fpConfigurationHandler->SetLocalLoggingLevel(level);
1030}