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