]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/BASE/AliHLTSystem.cxx
fixing warnings
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTSystem.cxx
CommitLineData
f23a6e1a 1// $Id$
c5123824 2//**************************************************************************
3//* This file is property of and copyright by the ALICE HLT Project *
4//* ALICE Experiment at CERN, All rights reserved. *
5//* *
6//* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
7//* for The ALICE HLT Project. *
8//* *
9//* Permission to use, copy, modify and distribute this software and its *
10//* documentation strictly for non-commercial purposes is hereby granted *
11//* without fee, provided that the above copyright notice appears in all *
12//* copies and that both the copyright notice and this permission notice *
13//* appear in the supporting documentation. The authors make no claims *
14//* about the suitability of this software for any purpose. It is *
15//* provided "as is" without express or implied warranty. *
16//**************************************************************************
f23a6e1a 17
b22e91eb 18/** @file AliHLTSystem.cxx
19 @author Matthias Richter
20 @date
21 @brief Implementation of HLT module management.
22*/
f23a6e1a 23
0c0c9d99 24#if __GNUC__>= 3
f23a6e1a 25using namespace std;
26#endif
27
f3506ea2 28#include <cassert>
85869391 29#include "AliHLTStdIncludes.h"
f23a6e1a 30#include "AliHLTSystem.h"
31#include "AliHLTComponentHandler.h"
32#include "AliHLTComponent.h"
5ec8e281 33#include "AliHLTConfiguration.h"
c38ba6f9 34#include "AliHLTConfigurationHandler.h"
35#include "AliHLTTask.h"
242bb794 36#include "AliHLTModuleAgent.h"
8451168b 37#include "AliHLTOfflineInterface.h"
457ec821 38#include "AliHLTDataSource.h"
c5123824 39#include "AliHLTOUT.h"
40#include "AliHLTOUTHandler.h"
f3c1d403 41#include "AliHLTOUTTask.h"
19b5c321 42#include "AliHLTControlTask.h"
0a7afbf0 43#include "AliHLTDataBuffer.h"
c4ffab2c 44#include "AliHLTMisc.h"
242bb794 45#include <TObjArray.h>
46#include <TObjString.h>
90ebac25 47#include <TStopwatch.h>
d5ea63f7 48#include <TList.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",
33d33d06 59 // "libAliHLTPHOS.so",
887a669c 60 "libAliHLTMUON.so",
d5ea63f7 61 "libAliHLTTRD.so",
3262c4fc 62 "libAliHLTITS.so",
d5ea63f7 63 "libAliHLTGlobal.so",
c5123824 64 "libAliHLTTrigger.so",
83670b1d 65 NULL
66};
67
b22e91eb 68/** ROOT macro for the implementation of ROOT specific class methods */
f23a6e1a 69ClassImp(AliHLTSystem)
70
19b5c321 71AliHLTSystem::AliHLTSystem(AliHLTComponentLogSeverity loglevel, const char* name)
85869391 72 :
b005ef92 73 fpComponentHandler(AliHLTComponentHandler::CreateHandler()),
74 fpConfigurationHandler(AliHLTConfigurationHandler::CreateHandler()),
242bb794 75 fTaskList(),
c043fa2c 76 fState(0),
dee38f1b 77 fChains(),
78 fStopwatches(new TObjArray),
79 fEventCount(-1),
b005ef92 80 fGoodEvents(-1),
81 fpChainHandlers(NULL),
82 fpEsdHandlers(NULL),
f3c1d403 83 fpProprietaryHandlers(NULL),
19b5c321 84 fpHLTOUTTask(NULL),
85 fpControlTask(NULL),
86 fName(name)
c4ffab2c 87 , fECSParams()
f23a6e1a 88{
70ed7d01 89 // see header file for class documentation
90 // or
91 // refer to README to build package
92 // or
93 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
5f5b708b 94
b005ef92 95 if (fgNofInstances++>0) {
96 // July 2008: multiple instances are now allowed
97 // AliHLTSystem is used in multiple instances for the kChain HLTOUT handler
98 //HLTWarning("multiple instances of AliHLTSystem, you should not use more than one at a time");
99 }
fc455fba 100
83cb7e1d 101 SetGlobalLoggingLevel(loglevel);
102 SetFrameworkLog(loglevel);
f23a6e1a 103 if (fpComponentHandler) {
a3c9b745 104 AliHLTAnalysisEnvironment env;
105 memset(&env, 0, sizeof(AliHLTAnalysisEnvironment));
106 env.fStructSize=sizeof(AliHLTAnalysisEnvironment);
9ce4bf4a 107 env.fAllocMemoryFunc=AliHLTSystem::AllocMemory;
324ca3b4 108 env.fGetEventDoneDataFunc=AliHLTSystem::AllocEventDoneData;
fc455fba 109 env.fLoggingFunc=NULL;
f23a6e1a 110 fpComponentHandler->SetEnvironment(&env);
db95fec3 111 InitAliLogFunc(fpComponentHandler);
d4a18597 112 if (fgNofInstances==1) {
a742f6f8 113 fpComponentHandler->AnnounceVersion();
d4a18597 114 }
85465857 115 } else {
116 HLTFatal("can not create Component Handler");
117 }
85465857 118 if (fpConfigurationHandler) {
119 AliHLTConfiguration::GlobalInit(fpConfigurationHandler);
120 } else {
121 HLTFatal("can not create Configuration Handler");
f23a6e1a 122 }
123}
124
f23a6e1a 125AliHLTSystem::~AliHLTSystem()
126{
70ed7d01 127 // see header file for class documentation
fc455fba 128 fgNofInstances--;
a472be38 129 CleanHLTOUT();
2d7ff710 130 CleanTaskList();
fc455fba 131 AliHLTConfiguration::GlobalDeinit(fpConfigurationHandler);
2d7ff710 132 if (fpConfigurationHandler) {
b005ef92 133 fpConfigurationHandler->Destroy();
2d7ff710 134 }
135 fpConfigurationHandler=NULL;
136
137 if (fpComponentHandler) {
b005ef92 138 fpComponentHandler->Destroy();
2d7ff710 139 }
140 fpComponentHandler=NULL;
b09d01b5 141 delete fStopwatches;
19b5c321 142
143 // note: fpHLTOUTTask and fpControlTask are deleted by
144 // CleanTaskList
f23a6e1a 145}
146
fc455fba 147int AliHLTSystem::fgNofInstances=0;
148
a742f6f8 149int AliHLTSystem::BuildTaskList(const char* id)
150{
151 // see header file for class documentation
152 int iResult=0;
153 if (id) {
154 if (fpConfigurationHandler) {
155 AliHLTConfiguration* pConf=fpConfigurationHandler->FindConfiguration(id);
156 if (pConf) {
157 iResult=BuildTaskList(pConf);
158 } else {
159 HLTError("unknown configuration \"%s\"", id);
160 iResult=-EEXIST;
161 }
162 } else {
163 iResult=-EFAULT;
164 }
165 } else {
166 iResult=-EINVAL;
167 }
168 return iResult;
169}
170
5ec8e281 171int AliHLTSystem::BuildTaskList(AliHLTConfiguration* pConf)
172{
70ed7d01 173 // see header file for class documentation
f23a6e1a 174 int iResult=0;
5ec8e281 175 if (pConf) {
176 AliHLTTask* pTask=NULL;
177 if ((pTask=FindTask(pConf->GetName()))!=NULL) {
178 if (pTask->GetConf()!=pConf) {
c5123824 179 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);
5ec8e281 180 iResult=-EEXIST;
5ec8e281 181 }
c043fa2c 182 // task for this configuration exists, terminate
183 pTask=NULL;
5ec8e281 184 } else if (pConf->SourcesResolved(1)!=1) {
85465857 185 HLTError("configuration \"%s\" has unresolved sources, aborting ...", pConf->GetName());
5ec8e281 186 iResult=-ENOLINK;
187 } else {
53feaef5 188 pTask=new AliHLTTask(pConf);
5ec8e281 189 if (pTask==NULL) {
190 iResult=-ENOMEM;
dba03d72 191 } else {
192 pTask->SetLocalLoggingLevel(GetLocalLoggingLevel());
5ec8e281 193 }
194 }
c043fa2c 195 static int iterationLevel=0;
196 if (pTask && iResult>=0) {
3b35e87c 197 // check for circular dependencies
5ec8e281 198 if ((iResult=pConf->FollowDependency(pConf->GetName()))>0) {
3b35e87c 199 HLTError("detected circular dependency for configuration \"%s\"", pTask->GetName());
5ec8e281 200 pTask->PrintDependencyTree(pTask->GetName(), 1/*use the configuration list*/);
85465857 201 HLTError("aborted ...");
5ec8e281 202 iResult=-ELOOP;
203 }
204 if (iResult>=0) {
205 // check whether all dependencies are already in the task list
206 // create the missing ones
3b35e87c 207 // this step is an iterative process which calls this function again for the missing
208 // configurations, in order to avoid the currently processed task to be created
209 // again it is added to the list temporarily and removed afterwards
210 // This is of high importance to preserve the order of the tasks. Furthermore, the
211 // InsertTask method has to be used in order to set all the cross links right
5ec8e281 212 fTaskList.Add(pTask);
213 AliHLTConfiguration* pDep=pConf->GetFirstSource();
214 while (pDep!=NULL && iResult>=0) {
c043fa2c 215 HLTDebug("iteration %d: checking dependency %s (%p)", iterationLevel, pDep->GetName(), pDep);
5ec8e281 216 if (FindTask(pDep->GetName())==NULL) {
c043fa2c 217 HLTDebug("iteration %d: building task list for configuration %s (%p)", iterationLevel, pDep->GetName(), pDep);
218 iterationLevel++;
5ec8e281 219 iResult=BuildTaskList(pDep);
c043fa2c 220 iterationLevel--;
5ec8e281 221 }
222 pDep=pConf->GetNextSource();
223 }
3b35e87c 224 // remove the temporarily added task
5ec8e281 225 fTaskList.Remove(pTask);
226
227 // insert the task and set the cross-links
228 if (iResult>=0) {
c043fa2c 229 HLTDebug("iteration %d: inserting task %s (%p)", iterationLevel, pTask->GetName(), pTask);
5ec8e281 230 iResult=InsertTask(pTask);
231 }
232 } else {
233 delete pTask;
234 pTask=NULL;
235 }
236 }
237 } else {
238 iResult=-EINVAL;
f23a6e1a 239 }
f23a6e1a 240 return iResult;
241}
242
5ec8e281 243int AliHLTSystem::CleanTaskList()
244{
70ed7d01 245 // see header file for class documentation
5ec8e281 246 int iResult=0;
19b5c321 247 fpHLTOUTTask=NULL;
248 fpControlTask=NULL;
5ec8e281 249 TObjLink* lnk=NULL;
a742f6f8 250 while ((lnk=fTaskList.LastLink())!=NULL) {
5ec8e281 251 delete (lnk->GetObject());
a742f6f8 252 fTaskList.Remove(lnk);
5ec8e281 253 }
19b5c321 254
5ec8e281 255 return iResult;
256}
257
258int AliHLTSystem::InsertTask(AliHLTTask* pTask)
259{
70ed7d01 260 // see header file for class documentation
5ec8e281 261 int iResult=0;
a472be38 262 if (fpControlTask==NULL) {
263 fpControlTask=new AliHLTControlTask;
264 if (!fpControlTask) return -ENOMEM;
265 fTaskList.AddFirst(fpControlTask);
266 }
267 TObjLink *controlLnk=NULL;
268 TObjLink *lnk = fTaskList.FirstLink();
269 assert(!lnk || lnk->GetObject()==fpControlTask || fpControlTask==NULL);
270 if (lnk && lnk->GetObject()==fpControlTask) {
271 if (pTask->GetConf() && pTask->GetConf()->GetFirstSource()==NULL) {
272 pTask->SetDependency(fpControlTask);
273 fpControlTask->SetTarget(pTask);
274 }
275 controlLnk=lnk;
276 lnk=lnk->Next();
277 }
278 if ((iResult=pTask->CheckDependencies())<=0)
279 lnk=NULL;
5ec8e281 280 while (lnk && iResult>0) {
281 AliHLTTask* pCurr = (AliHLTTask*)lnk->GetObject();
85465857 282 //HLTDebug("checking \"%s\"", pCurr->GetName());
5ec8e281 283 iResult=pTask->Depends(pCurr);
284 if (iResult>0) {
285 iResult=pTask->SetDependency(pCurr);
286 pCurr->SetTarget(pTask);
85465857 287 HLTDebug("set dependency \"%s\" for configuration \"%s\"", pCurr->GetName(), pTask->GetName());
f23a6e1a 288 }
5ec8e281 289 if (pCurr->Depends(pTask)) {
3b35e87c 290 // circular dependency
291 HLTError("circular dependency: can not resolve dependencies for configuration \"%s\"", pTask->GetName());
5ec8e281 292 iResult=-ELOOP;
293 } else if ((iResult=pTask->CheckDependencies())>0) {
294 lnk = lnk->Next();
295 }
296 }
297 if (iResult==0) {
298 if (lnk) {
299 fTaskList.AddAfter(lnk, pTask);
a472be38 300 } else if (controlLnk) {
301 fTaskList.AddAfter(controlLnk, pTask);
5ec8e281 302 } else {
303 fTaskList.AddFirst(pTask);
304 }
a742f6f8 305 HLTDebug("task \"%s\" (%p) inserted (size %d)", pTask->GetName(), pTask, sizeof(AliHLTTask));
5ec8e281 306 } else if (iResult>0) {
85465857 307 HLTError("can not resolve dependencies for configuration \"%s\" (%d unresolved)", pTask->GetName(), iResult);
5ec8e281 308 iResult=-ENOLINK;
f23a6e1a 309 }
5ec8e281 310 return iResult;
f23a6e1a 311}
312
5ec8e281 313AliHLTTask* AliHLTSystem::FindTask(const char* id)
314{
70ed7d01 315 // see header file for class documentation
5ec8e281 316 AliHLTTask* pTask=NULL;
317 if (id) {
f3506ea2 318 pTask=dynamic_cast<AliHLTTask*>(fTaskList.FindObject(id));
5ec8e281 319 }
320 return pTask;
321}
f23a6e1a 322
5ec8e281 323void AliHLTSystem::PrintTaskList()
324{
70ed7d01 325 // see header file for class documentation
85465857 326 HLTLogKeyword("task list");
5ec8e281 327 TObjLink *lnk = NULL;
85465857 328 HLTMessage("Task List");
5ec8e281 329 lnk=fTaskList.FirstLink();
330 while (lnk) {
331 TObject* obj=lnk->GetObject();
332 if (obj) {
85465857 333 HLTMessage(" %s - status:", obj->GetName());
5ec8e281 334 AliHLTTask* pTask=(AliHLTTask*)obj;
335 pTask->PrintStatus();
336 } else {
337 }
338 lnk = lnk->Next();
339 }
340}
014d39ce 341
c4ffab2c 342int AliHLTSystem::Run(Int_t iNofEvents, int bStop, AliHLTUInt64_t trgMask)
014d39ce 343{
70ed7d01 344 // see header file for class documentation
014d39ce 345 int iResult=0;
242bb794 346 int iCount=0;
347 SetStatusFlags(kRunning);
dee38f1b 348 if (fEventCount>=0 || (iResult=InitTasks())>=0) {
349 if (fEventCount>=0 || (iResult=StartTasks())>=0) {
350 if (fEventCount==0) {
351 InitBenchmarking(fStopwatches);
352 } else {
9d8f5e01 353 // Matthias Oct 11 2008 this is a bug
354 // By resuming the stopwatches at this point, all continued counting, but the
355 // starting and stopping is controlled by the AliHLTStopwatchGuard
356 //ResumeBenchmarking(fStopwatches);
dee38f1b 357 }
358 for (int i=fEventCount; i<fEventCount+iNofEvents && iResult>=0; i++) {
f3c1d403 359 if (fpHLTOUTTask) {
360 if (iNofEvents>1 && i==fEventCount) {
361 HLTWarning("can not add more than one event to the HLTOUT, skipping all but last block");
362 }
363 // reset and prepare for new data
364 fpHLTOUTTask->Reset();
365 }
c4ffab2c 366 if ((iResult=ProcessTasks(i, trgMask))>=0) {
dee38f1b 367 fGoodEvents++;
242bb794 368 iCount++;
9ce4bf4a 369 } else {
9ce4bf4a 370 // TODO: define different running modes to either ignore errors in
371 // event processing or not
372 // currently ignored
dee38f1b 373 iResult=0;
9ce4bf4a 374 }
0a7afbf0 375 AliHLTDataBuffer::SetGlobalEventCount(iCount);
2d7ff710 376 }
dee38f1b 377 fEventCount+=iNofEvents;
378 if (bStop) StopTasks();
032c5e5e 379 else PauseBenchmarking(fStopwatches);
53feaef5 380 }
dee38f1b 381 if (bStop) DeinitTasks();
9ce4bf4a 382 }
90ebac25 383 if (iResult>=0) {
384 iResult=iCount;
ba7f962b 385 } else if (iResult==-126 /*ENOKEY*/) {
83670b1d 386 iResult=0; // do not propagate the error
90ebac25 387 }
242bb794 388 ClearStatusFlags(kRunning);
0a7afbf0 389 AliHLTDataBuffer::PrintStatistics();
9ce4bf4a 390 return iResult;
391}
392
393int AliHLTSystem::InitTasks()
394{
70ed7d01 395 // see header file for class documentation
9ce4bf4a 396 int iResult=0;
397 TObjLink *lnk=fTaskList.FirstLink();
dee38f1b 398
fc455fba 399 if (lnk==NULL) {
97d2b87a 400 HLTInfo("Task list is empty, skipping HLT");
ba7f962b 401 return -126 /*ENOKEY*/;
fc455fba 402 }
9ce4bf4a 403 while (lnk && iResult>=0) {
404 TObject* obj=lnk->GetObject();
405 if (obj) {
406 AliHLTTask* pTask=(AliHLTTask*)obj;
407 iResult=pTask->Init(NULL, fpComponentHandler);
dba03d72 408// ProcInfo_t ProcInfo;
409// gSystem->GetProcInfo(&ProcInfo);
410// HLTInfo("task %s initialized (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
9ce4bf4a 411 } else {
412 }
413 lnk = lnk->Next();
414 }
415 if (iResult<0) {
97d2b87a 416 HLTError("can not initialize task list, error %d", iResult);
53feaef5 417 }
dee38f1b 418
53feaef5 419 return iResult;
420}
421
90ebac25 422int AliHLTSystem::InitBenchmarking(TObjArray* pStopwatches)
423{
424 // see header file for class documentation
90ebac25 425 int iResult=0;
dee38f1b 426 if (pStopwatches==NULL) return 0;
427
428 for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
429 TStopwatch* pStopwatch= new TStopwatch;
430 if (pStopwatch) {
431 pStopwatch->Reset();
432 pStopwatches->AddAt(pStopwatch, i);
433 } else {
434 iResult=-ENOMEM;
435 break;
436 }
437 }
438
90ebac25 439 TObjLink *lnk=fTaskList.FirstLink();
440 while (lnk && iResult>=0) {
441 TObject* obj=lnk->GetObject();
442 if (obj) {
443 AliHLTTask* pTask=(AliHLTTask*)obj;
444 AliHLTComponent* pComp=NULL;
445 if (iResult>=0 && (pComp=pTask->GetComponent())!=NULL) {
446 switch (pComp->GetComponentType()) {
447 case AliHLTComponent::kProcessor:
448 pComp->SetStopwatches(pStopwatches);
449 break;
450 case AliHLTComponent::kSource:
451 {
452 // this switch determines whether the time consumption of the
453 // AliHLTComponent base methods should be counted to the input
454 // stopwatch or base stopwatch.
455 //int inputBase=(int)AliHLTComponent::kSWBase;
456 int inputBase=(int)AliHLTComponent::kSWInput;
457 pComp->SetStopwatch(pStopwatches->At(inputBase), AliHLTComponent::kSWBase);
458 pComp->SetStopwatch(pStopwatches->At((int)AliHLTComponent::kSWInput), AliHLTComponent::kSWDA);
459 }
460 break;
461 case AliHLTComponent::kSink:
462 {
463 // this switch determines whether the time consumption of the
464 // AliHLTComponent base methods should be counted to the output
465 // stopwatch or base stopwatch.
466 //int outputBase=(int)AliHLTComponent::kSWBase;
467 int outputBase=(int)AliHLTComponent::kSWOutput;
468 pComp->SetStopwatch(pStopwatches->At(outputBase), AliHLTComponent::kSWBase);
469 pComp->SetStopwatch(pStopwatches->At((int)AliHLTComponent::kSWOutput), AliHLTComponent::kSWDA);
470 }
471 break;
85f0cede 472 default:
473 HLTWarning("unknown component type %d", (int)pComp->GetComponentType());
90ebac25 474 }
475 }
476 } else {
477 }
478 lnk = lnk->Next();
479 }
480 return iResult;
481}
482
032c5e5e 483int AliHLTSystem::PauseBenchmarking(TObjArray* pStopwatches) const
484{
485 // see header file for class documentation
486 if (pStopwatches==NULL) return 0;
487
488 for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
489 if (!pStopwatches->At(i)) continue;
490 TStopwatch* pSw=dynamic_cast<TStopwatch*>(pStopwatches->At(i));
491 if (pSw) pSw->Stop();
492 }
493 return 0;
494}
495
496int AliHLTSystem::ResumeBenchmarking(TObjArray* pStopwatches) const
497{
498 // see header file for class documentation
499 if (pStopwatches==NULL) return 0;
500
501 for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
502 if (!pStopwatches->At(i)) continue;
503 TStopwatch* pSw=dynamic_cast<TStopwatch*>(pStopwatches->At(i));
504 if (pSw) pSw->Continue();
505 }
506 return 0;
507}
508
509int AliHLTSystem::PrintBenchmarking(TObjArray* pStopwatches, int bClean) const
dee38f1b 510{
511 // see header file for class documentation
512 int iInitialized=1;
513 if (pStopwatches==NULL) return 0;
514
515 for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
516 if (!dynamic_cast<TStopwatch*>(pStopwatches->At(i))) {
517 iInitialized=0;
518 break;
519 }
520 }
521
522 if (iInitialized!=0) {
032c5e5e 523 HLTImportant("HLT statistics:\n"
dee38f1b 524 " base: R:%.3fs C:%.3fs\n"
525 " input: R:%.3fs C:%.3fs\n"
526 " output: R:%.3fs C:%.3fs\n"
527 " event processing : R:%.3fs C:%.3fs"
528 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWBase))->RealTime()
529 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWBase))->CpuTime()
530 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWInput))->RealTime()
531 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWInput))->CpuTime()
532 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWOutput))->RealTime()
533 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWOutput))->CpuTime()
534 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWDA))->RealTime()
535 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWDA))->CpuTime()
536 );
537 }
538
539 if (bClean) {
540 for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
541 TObject* pObj=pStopwatches->RemoveAt(i);
542 if (pObj) delete pObj;
543 }
544 }
545 return 0;
546}
547
53feaef5 548int AliHLTSystem::StartTasks()
549{
70ed7d01 550 // see header file for class documentation
53feaef5 551 int iResult=0;
552 TObjLink *lnk=fTaskList.FirstLink();
553 while (lnk && iResult>=0) {
554 TObject* obj=lnk->GetObject();
555 if (obj) {
556 AliHLTTask* pTask=(AliHLTTask*)obj;
557 iResult=pTask->StartRun();
dba03d72 558// ProcInfo_t ProcInfo;
559// gSystem->GetProcInfo(&ProcInfo);
560// HLTInfo("task %s started (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
53feaef5 561 } else {
562 }
563 lnk = lnk->Next();
564 }
565 if (iResult<0) {
97d2b87a 566 HLTError("can not start task list, error %d", iResult);
dee38f1b 567 } else {
3dd8541e 568 SetStatusFlags(kStarted);
dee38f1b 569 fEventCount=0;
570 fGoodEvents=0;
457ec821 571 if ((iResult=SendControlEvent(kAliHLTDataTypeSOR))<0) {
97d2b87a 572 HLTError("can not send SOR event: error %d", iResult);
457ec821 573 }
53feaef5 574 }
575 return iResult;
576}
577
c4ffab2c 578int AliHLTSystem::ProcessTasks(Int_t eventNo, AliHLTUInt64_t trgMask)
53feaef5 579{
70ed7d01 580 // see header file for class documentation
53feaef5 581 int iResult=0;
582 HLTDebug("processing event no %d", eventNo);
583 TObjLink *lnk=fTaskList.FirstLink();
584 while (lnk && iResult>=0) {
585 TObject* obj=lnk->GetObject();
586 if (obj) {
587 AliHLTTask* pTask=(AliHLTTask*)obj;
c4ffab2c 588 iResult=pTask->ProcessTask(eventNo, gkAliEventTypeData, trgMask);
dba03d72 589// ProcInfo_t ProcInfo;
590// gSystem->GetProcInfo(&ProcInfo);
591// HLTInfo("task %s processed (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
53feaef5 592 } else {
593 }
594 lnk = lnk->Next();
595 }
dee38f1b 596
597 if (iResult>=0) {
97d2b87a 598 HLTImportant("Event %d successfully finished (%d)", eventNo, iResult);
dee38f1b 599 iResult=0;
600 } else {
97d2b87a 601 HLTError("Processing of event %d failed (%d)", eventNo, iResult);
dee38f1b 602 }
603
53feaef5 604 return iResult;
605}
606
607int AliHLTSystem::StopTasks()
608{
70ed7d01 609 // see header file for class documentation
53feaef5 610 int iResult=0;
457ec821 611 if ((iResult=SendControlEvent(kAliHLTDataTypeEOR))<0) {
97d2b87a 612 HLTError("can not send EOR event");
457ec821 613 }
19b5c321 614
615 // cleanup blocks from the last event. This is a bit awkward. All output
616 // blocks from the chains need to be stored in the HLTOUT task. Though,
617 // we do not know, whether HLTOUT is going to be processed or not.
618 if (fpHLTOUTTask)
619 fpHLTOUTTask->Reset();
620
53feaef5 621 TObjLink *lnk=fTaskList.FirstLink();
457ec821 622 while (lnk) {
53feaef5 623 TObject* obj=lnk->GetObject();
624 if (obj) {
625 AliHLTTask* pTask=(AliHLTTask*)obj;
457ec821 626 int locResult=pTask->EndRun();
627 if (iResult>=0 && locResult<0) iResult=locResult;
dba03d72 628// ProcInfo_t ProcInfo;
629// gSystem->GetProcInfo(&ProcInfo);
630// HLTInfo("task %s stopped (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
53feaef5 631 } else {
632 }
633 lnk = lnk->Next();
634 }
dee38f1b 635 PrintBenchmarking(fStopwatches, 1 /*clean*/);
3dd8541e 636 ClearStatusFlags(kStarted);
014d39ce 637 return iResult;
638}
9ce4bf4a 639
457ec821 640int AliHLTSystem::SendControlEvent(AliHLTComponentDataType dt)
641{
642 // see header file for class documentation
457ec821 643 int iResult=0;
a472be38 644
3baf1595 645 AliHLTComponentBlockDataList controlBlocks;
646 AliHLTComponentBlockData bd;
647
648 // run decriptor block of type kAliHLTDataTypeSOR/kAliHLTDataTypeEOR
649 AliHLTComponent::FillBlockData(bd);
457ec821 650 AliHLTRunDesc runDesc;
651 memset(&runDesc, 0, sizeof(AliHLTRunDesc));
652 runDesc.fStructSize=sizeof(AliHLTRunDesc);
5e9d7024 653 runDesc.fRunNo=AliHLTMisc::Instance().GetCDBRunNo();
3baf1595 654 bd.fPtr=&runDesc;
655 bd.fSize=sizeof(AliHLTRunDesc);
656 bd.fDataType=dt;
657 bd.fSpecification=kAliHLTVoidDataSpec;
658 controlBlocks.push_back(bd);
659
c4ffab2c 660 // ECS parameter of type kAliHLTDataTypeECSParam
661 if (fECSParams.IsNull())
662 fECSParams="CTP_TRIGGER_CLASS=00:DUMMY-TRIGGER-ALL:00-01-02-03-04-05-06-07-08-09-10-11-12-13-14-15-16-17";
3baf1595 663 AliHLTComponent::FillBlockData(bd);
c4ffab2c 664 bd.fPtr=(void*)fECSParams.Data();
665 bd.fSize=fECSParams.Length()+1;
3baf1595 666 bd.fDataType=kAliHLTDataTypeECSParam;
667 bd.fSpecification=kAliHLTVoidDataSpec;
668 controlBlocks.push_back(bd);
669
670 AliHLTControlTask::AliHLTControlEventGuard g(fpControlTask, controlBlocks);
457ec821 671 HLTDebug("sending event %s, run descriptor %p", AliHLTComponent::DataType2Text(dt).c_str(), &runDesc);
672 TObjLink *lnk=fTaskList.FirstLink();
673 while (lnk && iResult>=0) {
674 TObject* obj=lnk->GetObject();
675 if (obj) {
676 AliHLTTask* pTask=(AliHLTTask*)obj;
a472be38 677 AliHLTUInt32_t eventType=gkAliEventTypeUnknown;
678 if (dt==kAliHLTDataTypeSOR) eventType=gkAliEventTypeStartOfRun;
679 else if (dt==kAliHLTDataTypeEOR) eventType=gkAliEventTypeEndOfRun;
680 else HLTWarning("unknown control event %s", AliHLTComponent::DataType2Text(dt).c_str());
681 iResult=pTask->ProcessTask(-1, eventType);
457ec821 682 } else {
683 }
684 lnk = lnk->Next();
685 }
a472be38 686
687 // control events are not supposed to go into the HLTOUT
688 if (fpHLTOUTTask)
689 fpHLTOUTTask->Reset();
690
457ec821 691 HLTDebug("event %s done (%d)", AliHLTComponent::DataType2Text(dt).c_str(), iResult);
692 return iResult;
693}
694
9ce4bf4a 695int AliHLTSystem::DeinitTasks()
696{
70ed7d01 697 // see header file for class documentation
9ce4bf4a 698 int iResult=0;
1b820a65 699 TObjLink *lnk=fTaskList.LastLink();
618c89b9 700 while (lnk) {
9ce4bf4a 701 TObject* obj=lnk->GetObject();
702 if (obj) {
703 AliHLTTask* pTask=(AliHLTTask*)obj;
618c89b9 704 int localRes=pTask->Deinit();
705 if (iResult>=0) iResult=localRes;
dba03d72 706// ProcInfo_t ProcInfo;
707// gSystem->GetProcInfo(&ProcInfo);
708// HLTInfo("task %s cleaned (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
9ce4bf4a 709 } else {
710 }
1b820a65 711 lnk = lnk->Prev();
9ce4bf4a 712 }
dee38f1b 713 fEventCount=-1;
714 fGoodEvents=-1;
715
9ce4bf4a 716 return iResult;
717}
718
a472be38 719int AliHLTSystem::CleanHLTOUT()
720{
721 // see header file for class documentation
722 if (fpChainHandlers) {
723 AliHLTOUT::AliHLTOUTHandlerListEntryVector* pHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpChainHandlers);
724 fpChainHandlers=NULL;
725 if (pHandlers) {
726 AliHLTOUT::InvalidateBlocks(*pHandlers);
727 AliHLTOUT::RemoveEmptyDuplicateHandlers(*pHandlers);
728 }
729 assert(pHandlers->size()==0);
730 delete pHandlers;
731 }
732
733 if (fpEsdHandlers) {
734 AliHLTOUT::AliHLTOUTHandlerListEntryVector* pHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpEsdHandlers);
735 fpEsdHandlers=NULL;
736 if (pHandlers) {
737 AliHLTOUT::InvalidateBlocks(*pHandlers);
738 AliHLTOUT::RemoveEmptyDuplicateHandlers(*pHandlers);
739 }
740 assert(pHandlers->size()==0);
741 delete pHandlers;
742 }
743
744 if (fpProprietaryHandlers) {
745 AliHLTOUT::AliHLTOUTHandlerListEntryVector* pHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpProprietaryHandlers);
746 fpProprietaryHandlers=NULL;
747 if (pHandlers) {
748 AliHLTOUT::InvalidateBlocks(*pHandlers);
749 AliHLTOUT::RemoveEmptyDuplicateHandlers(*pHandlers);
750 }
751 assert(pHandlers->size()==0);
752 delete pHandlers;
753 }
754 return 0;
755}
756
c043fa2c 757void* AliHLTSystem::AllocMemory( void* /*param*/, unsigned long size )
9ce4bf4a 758{
70ed7d01 759 // see header file for class documentation
c043fa2c 760 void* p=NULL;
761 try {
762 p=(void*)new char[size];
242bb794 763 }
c043fa2c 764 catch (...) {
765 AliHLTLogging log;
766 log.LoggingVarargs(kHLTLogError, "AliHLTSystem" , "AllocMemory" , __FILE__ , __LINE__ , "exeption during memory allocation" );
767 }
768 return p;
9ce4bf4a 769}
242bb794 770
33d33d06 771int AliHLTSystem::AllocEventDoneData( void* /*param*/, AliHLTEventID_t /*eventID*/, unsigned long size, AliHLTComponentEventDoneData** edd )
324ca3b4 772{
773 // see header file for class documentation
774 unsigned long blocksize=sizeof(AliHLTComponentEventDoneData)+size;
775 void* block=AllocMemory(NULL, blocksize);
776 if (!block) return -ENOMEM;
777 memset(block, 0, blocksize);
778 *edd=reinterpret_cast<AliHLTComponentEventDoneData*>(block);
779 (*edd)->fStructSize=sizeof(AliHLTComponentEventDoneData);
780 (*edd)->fDataSize=size;
781 (*edd)->fData=reinterpret_cast<AliHLTUInt8_t*>(block)+sizeof(AliHLTComponentEventDoneData);
782
783 return 0;
784}
785
242bb794 786int AliHLTSystem::Reconstruct(int nofEvents, AliRunLoader* runLoader,
787 AliRawReader* rawReader)
788{
789 // see header file for class documentation
790 int iResult=0;
dee38f1b 791 if (runLoader || rawReader || nofEvents==0) {
792 if (nofEvents>0) {HLTInfo("Run Loader %p, Raw Reader %p , %d event(s)", runLoader, rawReader, nofEvents);}
242bb794 793 if (CheckStatus(kReady)) {
dee38f1b 794 if (nofEvents==0) {
795 // special case to close the reconstruction
f3506ea2 796 if (!CheckStatus(kError)) {
dee38f1b 797 StopTasks();
798 DeinitTasks();
a472be38 799 CleanHLTOUT();
f3506ea2 800 }
dee38f1b 801 } else {
8451168b 802 if ((iResult=AliHLTOfflineInterface::SetParamsToComponents(runLoader, rawReader))>=0) {
d5e397eb 803 AliHLTUInt64_t trgMask=0x1;
804 if (runLoader==NULL) {
805 // this is a quick workaround for the case of simulation
806 // the trigger framework is still under development, secondly, AliHLTSimulation
807 // does not yet add the emulated ECS parameters, so no CTP trigger is known in the HLT
808 // AliHLTTask will initialize one dummy CTP trigger class with bit 0, that's why the
809 // default trigger mask is 0x1
810 trgMask=AliHLTMisc::Instance().GetTriggerMask(rawReader);
811 }
dee38f1b 812 // the system always remains started after event processing, a specific
f3506ea2 813 // call with nofEvents==0 is needed to execute the stop sequence
c4ffab2c 814 if ((iResult=Run(nofEvents, 0, trgMask))<0) SetStatusFlags(kError);
dee38f1b 815 }
8451168b 816 }
242bb794 817 } else {
818 HLTError("wrong state %#x, required flags %#x", GetStatusFlags(), kReady);
819 }
820 } else {
0bf7536b 821 HLTError("missing RunLoader (%p)/RawReader (%p) instance", runLoader, rawReader);
242bb794 822 iResult=-EINVAL;
823 }
824 return iResult;
825}
826
af885e0f 827int AliHLTSystem::FillESD(int eventNo, AliRunLoader* runLoader, AliESDEvent* esd)
242bb794 828{
829 // see header file for class documentation
830 int iResult=0;
dee38f1b 831 if (runLoader || esd) {
242bb794 832 HLTInfo("Event %d: Run Loader %p, ESD %p", eventNo, runLoader, esd);
8451168b 833 iResult=AliHLTOfflineInterface::FillComponentESDs(eventNo, runLoader, esd);
242bb794 834 } else {
835 HLTError("missing run loader/ESD instance(s)");
836 iResult=-EINVAL;
837 }
838 return iResult;
839}
840
c5123824 841int AliHLTSystem::ProcessHLTOUT(AliHLTOUT* pHLTOUT, AliESDEvent* esd)
842{
843 // see header file for class documentation
844 int iResult=0;
845 if (!pHLTOUT) return -EINVAL;
c5123824 846 HLTDebug("processing %d HLT data blocks", pHLTOUT->GetNofDataBlocks());
0f1882a7 847
f3c1d403 848 // add the current HLTOUT task to the collection
849 if (fpHLTOUTTask) {
850 AliHLTOUT* pTask=dynamic_cast<AliHLTOUT*>(fpHLTOUTTask);
851 if (pTask && (iResult=pTask->Init())>=0) {
852 if (pTask->GetNofDataBlocks()>0) {
853 pHLTOUT->AddSubCollection(pTask);
854 }
855 } else {
856 HLTWarning("can not initialize HLTOUT sub collection %s for reconstruction chain (%d), data blocks are lost", pTask?fpHLTOUTTask->GetName():"nil", iResult);
857 iResult=0;
858 }
859 }
860
861
0f1882a7 862 //
863 // process all kChain handlers first
864 //
865 if ((iResult=ProcessHLTOUTkChain(pHLTOUT))<0) {
866 HLTWarning("Processing of kChain-type data blocks failed with error code %d", iResult);
867 iResult=0;
868 }
869
b005ef92 870 if (!fpEsdHandlers)
871 fpEsdHandlers=new AliHLTOUT::AliHLTOUTHandlerListEntryVector;
872 if (!fpProprietaryHandlers)
873 fpProprietaryHandlers=new AliHLTOUT::AliHLTOUTHandlerListEntryVector;
874
875 AliHLTOUT::AliHLTOUTHandlerListEntryVector* pEsdHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpEsdHandlers);
876 AliHLTOUT::AliHLTOUTHandlerListEntryVector* pProprietaryHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpProprietaryHandlers);
877 if (!pEsdHandlers || !pProprietaryHandlers) return -ENOMEM;
878
879 // invalidate all blocks
880 AliHLTOUT::InvalidateBlocks(*pEsdHandlers);
881 AliHLTOUT::InvalidateBlocks(*pProprietaryHandlers);
90c37647 882
d4a18597 883 AliHLTComponentDataTypeList esdBlocks;
884
c5123824 885 for (iResult=pHLTOUT->SelectFirstDataBlock();
886 iResult>=0;
887 iResult=pHLTOUT->SelectNextDataBlock()) {
888 AliHLTComponentDataType dt=kAliHLTVoidDataType;
889 AliHLTUInt32_t spec=kAliHLTVoidDataSpec;
890 pHLTOUT->GetDataBlockDescription(dt, spec);
891 AliHLTOUTHandler* pHandler=pHLTOUT->GetHandler();
892 AliHLTModuleAgent::AliHLTOUTHandlerType handlerType=pHLTOUT->GetDataBlockHandlerType();
0f1882a7 893
894 // default handling for ESD data blocks does not require an explicite handler
f94e8c27 895 if (!pHandler && (dt==kAliHLTDataTypeESDObject || dt==kAliHLTDataTypeESDTree)) {
c5123824 896 handlerType=AliHLTModuleAgent::kEsd;
897 }
898 const char* pMsg="invalid";
899 switch (handlerType) {
900 case AliHLTModuleAgent::kEsd:
901 {
902 if (pHandler) {
0f1882a7 903 // schedule for later processing
b005ef92 904 pHLTOUT->InsertHandler(*pEsdHandlers, pHLTOUT->GetDataBlockHandlerDesc());
c5123824 905 } else {
d4a18597 906 AliHLTComponentDataTypeList::iterator element=esdBlocks.begin();
907 for (; element!=esdBlocks.end(); element++) {
908 if (*element==dt) {
909 HLTWarning("multiple ESDs of identical data type %s, please add appropriate handler to merge ESDs", AliHLTComponent::DataType2Text(dt).c_str());
910 break;
911 }
912 }
913 if (element==esdBlocks.end()) esdBlocks.push_back(dt);
914
c5123824 915 // write directly
916 const AliHLTUInt8_t* pBuffer=NULL;
917 AliHLTUInt32_t size=0;
918 if (pHLTOUT->GetDataBuffer(pBuffer, size)>=0) {
57584811 919 pHLTOUT->WriteESD(pBuffer, size, dt, esd);
c5123824 920 pHLTOUT->ReleaseDataBuffer(pBuffer);
921 }
0f1882a7 922 pHLTOUT->MarkDataBlockProcessed();
c5123824 923 }
924 }
925 break;
926 case AliHLTModuleAgent::kRawReader:
927 // handled in the AliRawReaderHLT
928 break;
929 case AliHLTModuleAgent::kRawStream:
930 HLTWarning("HLTOUT handler type 'kRawStream' not yet implemented: agent %s, data type %s, specification %#x",
f75868b3 931 pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
c5123824 932 AliHLTComponent::DataType2Text(dt).c_str(), spec);
933 break;
934 case AliHLTModuleAgent::kChain:
0f1882a7 935 HLTWarning("HLTOUT handler type 'kChain' has already been processed: agent %s, data type %s, specification %#x\n"
936 "New block of this type added by the chain? Skipping data block ...",
f75868b3 937 pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
c5123824 938 AliHLTComponent::DataType2Text(dt).c_str(), spec);
939 break;
940 case AliHLTModuleAgent::kProprietary:
941 HLTDebug("processing proprietary data: agent %s, data type %s, specification %#x",
f75868b3 942 pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
c5123824 943 AliHLTComponent::DataType2Text(dt).c_str(), spec);
944 if (pHandler) {
945 AliHLTOUT::AliHLTOUTLockGuard g(pHLTOUT);
946 int res=pHandler->ProcessData(pHLTOUT);
947 if (res<0) {
948 HLTWarning("processing proprietary data failed (%d): agent %s, data type %s, specification %#x",
f75868b3 949 res, pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
c5123824 950 AliHLTComponent::DataType2Text(dt).c_str(), spec);
951 }
952 }
953 break;
954 case AliHLTModuleAgent::kUnknownOutput:
955 pMsg="unknown";
956 // fall trough intended
957 default:
958 HLTWarning("%s handler type: agent %s, data type %s, specification %#x, ... skipping data block",
f75868b3 959 pMsg, pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
c5123824 960 AliHLTComponent::DataType2Text(dt).c_str(), spec);
961 }
962 }
963 // TODO: the return value of SelectFirst/NextDataBlock must be
964 // changed in order to avoid this check
965 if (iResult==-ENOENT) iResult=0;
966
b005ef92 967 AliHLTOUT::AliHLTOUTHandlerListEntryVector::iterator handler;
968
969 // process and write all esd data blocks
970 for (handler=pEsdHandlers->begin(); handler!=pEsdHandlers->end() && iResult>=0; handler++) {
971 AliHLTOUT::AliHLTOUTSelectionGuard g(pHLTOUT, &(*handler));
972 AliHLTOUTHandler* pHandler=*handler;
c5123824 973 const AliHLTUInt8_t* pBuffer=NULL;
974 AliHLTUInt32_t size=0;
0f1882a7 975 pHandler->ProcessData(pHLTOUT);
c5123824 976 if ((size=pHandler->GetProcessedData(pBuffer))>0) {
b005ef92 977 AliHLTModuleAgent::AliHLTOUTHandlerDesc desc=*handler;
c5123824 978 AliHLTComponentDataType dt=desc;
57584811 979 pHLTOUT->WriteESD(pBuffer, size, dt, esd);
c5123824 980 pHandler->ReleaseProcessedData(pBuffer, size);
981 }
b005ef92 982 pHLTOUT->MarkDataBlocksProcessed(&(*handler));
983 }
984
985 // process all kProprietary data blocks
d4a18597 986 for (handler=pProprietaryHandlers->begin(); handler!=pProprietaryHandlers->end() && iResult>=0; handler++) {
b005ef92 987 AliHLTOUT::AliHLTOUTSelectionGuard g(pHLTOUT, &(*handler));
988 AliHLTOUTHandler* pHandler=*handler;
989 const AliHLTUInt8_t* pBuffer=NULL;
990 AliHLTUInt32_t size=0;
991 pHandler->ProcessData(pHLTOUT);
992 if ((size=pHandler->GetProcessedData(pBuffer))>0) {
993 HLTWarning("data produced by kProprietary handler ignored");
994 pHandler->ReleaseProcessedData(pBuffer, size);
995 }
996 pHLTOUT->MarkDataBlocksProcessed(&(*handler));
0f1882a7 997 }
998
b005ef92 999 // remove all empty handlers form the list (handlers which did not get a block this time)
1000 AliHLTOUT::RemoveEmptyDuplicateHandlers(*pEsdHandlers);
1001 AliHLTOUT::RemoveEmptyDuplicateHandlers(*pProprietaryHandlers);
1002
0f1882a7 1003 return iResult;
1004}
1005
1006int AliHLTSystem::ProcessHLTOUTkChain(AliHLTOUT* pHLTOUT)
1007{
1008 // see header file for class documentation
1009 int iResult=0;
1010 if (!pHLTOUT) return -EINVAL;
1011
b005ef92 1012 if (!fpChainHandlers)
1013 fpChainHandlers=new AliHLTOUT::AliHLTOUTHandlerListEntryVector;
1014
1015 AliHLTOUT::AliHLTOUTHandlerListEntryVector* pChainHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpChainHandlers);
1016 if (!pChainHandlers) return -ENOMEM;
1017
1018 // invalidate all blocks
1019 AliHLTOUT::InvalidateBlocks(*pChainHandlers);
1020
1021 // fill the list
1022 pHLTOUT->FillHandlerList(*pChainHandlers, AliHLTModuleAgent::kChain);
0f1882a7 1023
1024 // process all defined chain handlers
1025 AliHLTOUT::AliHLTOUTHandlerListEntryVector::iterator chainHandler;
b005ef92 1026 for (chainHandler=pChainHandlers->begin(); chainHandler!=pChainHandlers->end() && iResult>=0; chainHandler++) {
d4a18597 1027 if (chainHandler->IsEmpty()) continue;
0f1882a7 1028 AliHLTOUT::AliHLTOUTSelectionGuard g(pHLTOUT, &(*chainHandler));
1029 AliHLTOUTHandler* pHandler=*chainHandler;
1030 const AliHLTUInt8_t* pBuffer=NULL;
1031 AliHLTUInt32_t size=0;
1032 pHandler->ProcessData(pHLTOUT);
1033 if ((size=pHandler->GetProcessedData(pBuffer))>0) {
1034 AliHLTModuleAgent::AliHLTOUTHandlerDesc desc=*chainHandler;
3741b8c4 1035 //AliHLTComponentDataType dt=desc;
0f1882a7 1036
1037 pHandler->ReleaseProcessedData(pBuffer, size);
1038 }
1039 pHLTOUT->MarkDataBlocksProcessed(&(*chainHandler));
c5123824 1040 }
1041
b005ef92 1042 // remove all empty handlers form the list (handlers which did not get a block this time)
1043 AliHLTOUT::RemoveEmptyDuplicateHandlers(*pChainHandlers);
1044
c5123824 1045 return iResult;
1046}
1047
242bb794 1048int AliHLTSystem::LoadComponentLibraries(const char* libraries)
1049{
1050 // see header file for class documentation
1051 int iResult=0;
1052 if (libraries) {
1053 if (fpComponentHandler) {
1054 TString libs(libraries);
1055 TObjArray* pTokens=libs.Tokenize(" ");
1056 if (pTokens) {
77fd699f 1057 int iEntries=pTokens->GetEntriesFast();
242bb794 1058 for (int i=0; i<iEntries && iResult>=0; i++) {
1059 iResult=fpComponentHandler->LoadLibrary((((TObjString*)pTokens->At(i))->GetString()).Data());
1060 }
1061 delete pTokens;
1062 }
1063 if (iResult>=0) {
1064 SetStatusFlags(kLibrariesLoaded);
1065 } else {
1066 // lets see if we need this, probably not
1067 //fpComponentHandler->UnloadLibraries();
1068 ClearStatusFlags(kLibrariesLoaded);
1069 }
1070 } else {
1071 iResult=-EFAULT;
1072 HLTFatal("no component handler available");
1073 }
1074 } else {
1075 iResult=-EINVAL;
1076 }
1077 return iResult;
1078}
1079
1080int AliHLTSystem::Configure(AliRunLoader* runloader)
dee38f1b 1081{
1082 // see header file for class documentation
f3506ea2 1083 return Configure(NULL, runloader);
dee38f1b 1084}
1085
1086int AliHLTSystem::Configure(AliRawReader* rawReader, AliRunLoader* runloader)
242bb794 1087{
1088 // see header file for class documentation
1089 int iResult=0;
1090 if (CheckStatus(kRunning)) {
1091 HLTError("HLT system in running state, can not configure");
1092 return -EBUSY;
1093 }
e642a402 1094 ClearStatusFlags(kTaskListCreated);
c215072c 1095 if (CheckFilter(kHLTLogDebug))
1096 AliHLTModuleAgent::PrintStatus();
c043fa2c 1097 if (CheckStatus(kConfigurationLoaded)==0) {
dee38f1b 1098 iResult=LoadConfigurations(rawReader, runloader);
c043fa2c 1099 } else {
dee38f1b 1100 if (fChains.Length()==0) {
703bcca6 1101 HLTError("custom configuration(s) specified, but no configuration to run in local reconstruction, use \'chains=<chain,...>\' option");
c043fa2c 1102 iResult=-ENOENT;
1103 }
1104 }
242bb794 1105 if (iResult>=0) {
1106 SetStatusFlags(kConfigurationLoaded);
c043fa2c 1107 if (CheckFilter(kHLTLogDebug))
1108 fpConfigurationHandler->PrintConfigurations();
f3506ea2 1109 iResult=BuildTaskListsFromReconstructionChains(rawReader, runloader);
242bb794 1110 if (iResult>=0) {
1111 SetStatusFlags(kTaskListCreated);
1112 }
1113 }
1114 if (iResult<0) SetStatusFlags(kError);
1115
1116 return iResult;
1117}
1118
c043fa2c 1119int AliHLTSystem::ScanOptions(const char* options)
1120{
1121 // see header file for class documentation
1122 int iResult=0;
1123 if (options) {
d76bc02a 1124 //AliHLTComponentHandler::TLibraryMode libMode=AliHLTComponentHandler::kDynamic;
83670b1d 1125 TString libs("");
f59144fe 1126 TString excludelibs("");
c043fa2c 1127 TString alloptions(options);
1128 TObjArray* pTokens=alloptions.Tokenize(" ");
1129 if (pTokens) {
77fd699f 1130 int iEntries=pTokens->GetEntriesFast();
c043fa2c 1131 for (int i=0; i<iEntries; i++) {
1132 TString token=(((TObjString*)pTokens->At(i))->GetString());
1133 if (token.Contains("loglevel=")) {
1134 TString param=token.ReplaceAll("loglevel=", "");
1135 if (param.IsDigit()) {
1136 SetGlobalLoggingLevel((AliHLTComponentLogSeverity)param.Atoi());
1137 } else if (param.BeginsWith("0x") &&
1138 param.Replace(0,2,"",0).IsHex()) {
1139 int severity=0;
1140 sscanf(param.Data(),"%x", &severity);
1141 SetGlobalLoggingLevel((AliHLTComponentLogSeverity)severity);
1142 } else {
1143 HLTWarning("wrong parameter for option \'loglevel=\', (hex) number expected");
1144 }
dba03d72 1145 } else if (token.Contains("frameworklog=")) {
1146 TString param=token.ReplaceAll("frameworklog=", "");
1147 if (param.IsDigit()) {
1148 SetFrameworkLog((AliHLTComponentLogSeverity)param.Atoi());
1149 } else if (param.BeginsWith("0x") &&
1150 param.Replace(0,2,"",0).IsHex()) {
1151 int severity=0;
1152 sscanf(param.Data(),"%x", &severity);
1153 SetFrameworkLog((AliHLTComponentLogSeverity)severity);
1154 } else {
1155 HLTWarning("wrong parameter for option \'loglevel=\', (hex) number expected");
1156 }
c043fa2c 1157 } else if (token.Contains("alilog=off")) {
1158 SwitchAliLog(0);
1159 } else if (token.Contains("config=")) {
1160 TString param=token.ReplaceAll("config=", "");
1161 Int_t error=0;
1162 gROOT->Macro(param.Data(), &error);
1163 if (error==0) {
1164 SetStatusFlags(kConfigurationLoaded);
1165 } else {
1166 HLTError("can not execute macro \'%s\'", param.Data());
1167 iResult=-EBADF;
1168 }
d85f150c 1169 } else if (token.Contains("chains=")) {
dee38f1b 1170 TString param=token.ReplaceAll("chains=", "");
1171 fChains=param.ReplaceAll(",", " ");
703bcca6 1172 if (fChains.IsNull()) fChains=" "; // disable all chains
dba03d72 1173 } else if (token.Contains("libmode=")) {
1174 TString param=token.ReplaceAll("libmode=", "");
1175 param.ReplaceAll(",", " ");
1176 if (fpComponentHandler) {
1177 if (param.CompareTo("static")==0) {
1178 fpComponentHandler->SetLibraryMode(AliHLTComponentHandler::kStatic);
1179 } else if (param.CompareTo("dynamic")==0) {
1180 fpComponentHandler->SetLibraryMode(AliHLTComponentHandler::kDynamic);
1181 } else {
1182 HLTWarning("wrong argument for option \'libmode=\', use \'static\' or \'dynamic\'");
1183 }
1184 }
c4ffab2c 1185 } else if (token.BeginsWith("ECS=")) {
1186 fECSParams=token.ReplaceAll("ECS=", "");
83670b1d 1187 } else if (token.BeginsWith("lib") && token.EndsWith(".so")) {
1188 libs+=token;
1189 libs+=" ";
f59144fe 1190 } else if (token.BeginsWith("!lib") && token.EndsWith(".so")) {
1191 excludelibs+=token;
1192 excludelibs+=" ";
c043fa2c 1193 } else {
1194 HLTWarning("unknown option \'%s\'", token.Data());
1195 }
1196 }
1197 delete pTokens;
1198 }
83670b1d 1199
1200 if (iResult>=0) {
1201 if (libs.IsNull()) {
6a8e0bb4 1202 const char** deflib=fgkHLTDefaultLibs;
f59144fe 1203 for (;*deflib; deflib++) {
1204 if (excludelibs.Contains(*deflib)) continue;
1205 libs+=*deflib;
83670b1d 1206 libs+=" ";
1207 }
1208 }
1209 if ((!CheckStatus(AliHLTSystem::kLibrariesLoaded)) &&
1210 (LoadComponentLibraries(libs.Data())<0)) {
1211 HLTError("error while loading HLT libraries");
1212 iResult=-EFAULT;
1213 }
1214 }
c043fa2c 1215 }
1216 return iResult;
1217}
1218
242bb794 1219int AliHLTSystem::Reset(int bForce)
1220{
1221 // see header file for class documentation
1222 int iResult=0;
1223 if (!bForce && CheckStatus(kRunning)) {
1224 HLTError("HLT system in running state, can not configure");
1225 return -EBUSY;
1226 }
1227 CleanTaskList();
1228 ClearStatusFlags(~kUninitialized);
1229 return iResult;
1230}
1231
dee38f1b 1232int AliHLTSystem::LoadConfigurations(AliRawReader* rawReader, AliRunLoader* runloader)
242bb794 1233{
1234 // see header file for class documentation
1235 if (CheckStatus(kRunning)) {
1236 HLTError("HLT system in running state, can not configure");
1237 return -EBUSY;
1238 }
1239 int iResult=0;
d5ea63f7 1240 AliHLTModuleAgent* pAgent=NULL;
1241
1242 // first check for the required libraries and load those
1d398268 1243 TString extralibs;
d5ea63f7 1244 for (pAgent=AliHLTModuleAgent::GetFirstAgent();
1245 pAgent && iResult>=0;
1246 pAgent=AliHLTModuleAgent::GetNextAgent()) {
60b26a17 1247 const char* deplibs=pAgent->GetRequiredComponentLibraries();
1248 if (deplibs) {
1d398268 1249 HLTDebug("required libraries \'%s\' for agent %s (%p)", deplibs, pAgent->GetName(), pAgent);
f98abf68 1250 extralibs+=" ";
1d398268 1251 extralibs+=deplibs;
60b26a17 1252 }
242bb794 1253 }
1d398268 1254 if (iResult>=0) {
1255 iResult=LoadComponentLibraries(extralibs.Data());
1256 }
1257
d5ea63f7 1258 // in order to register the configurations in the correct sequence
1259 // all agents need to be ordered with respect to the required
1260 // libraries. Ordering relies on the naming convention
1261 // libAliHLT<Module>.so
1262 TList agents;
1263 for (pAgent=AliHLTModuleAgent::GetFirstAgent();
1264 pAgent && iResult>=0;
1265 pAgent=AliHLTModuleAgent::GetNextAgent()) {
1266 AliHLTModuleAgent* pPrevDep=NULL;
1267 TString dependencies=pAgent->GetRequiredComponentLibraries();
1268 TObjArray* pTokens=dependencies.Tokenize(" ");
1269 if (pTokens) {
1270 for (int n=0; n<pTokens->GetEntriesFast(); n++) {
1271 TString module=((TObjString*)pTokens->At(n))->GetString();
1272 HLTDebug(" checking %s", module.Data());
1273 module.ReplaceAll("libAliHLT", "");
1274 module.ReplaceAll(".so", "");
1275
1276 for (AliHLTModuleAgent* pCurrent=dynamic_cast<AliHLTModuleAgent*>(pPrevDep==NULL?agents.First():agents.After(pPrevDep));
1277 pCurrent!=NULL; pCurrent=dynamic_cast<AliHLTModuleAgent*>(agents.After(pCurrent))) {
1278 HLTDebug(" checking %s == %s", module.Data(), pCurrent->GetModuleId());
1279
1280 if (module.CompareTo(pCurrent->GetModuleId())==0) {
1281 pPrevDep=pCurrent;
1282 break;
1283 }
1284 }
1285 }
1286 delete pTokens;
1287 }
1288
1289 if (pPrevDep) {
1290 // insert right after the last dependency
1291 agents.AddAfter(pPrevDep, pAgent);
1292 HLTDebug("insert %s after %s", pAgent->GetModuleId(), pPrevDep->GetModuleId());
1293 } else {
1294 // insert at the beginning
1295 agents.AddFirst(pAgent);
1296 HLTDebug("insert %s at beginning", pAgent->GetModuleId());
1297 }
1298 }
1299
1300 // now we load the configurations
1301 if (agents.GetEntries()) {
1302 TIter next(&agents);
1303 while ((pAgent = dynamic_cast<AliHLTModuleAgent*>(next()))) {
1304 HLTDebug("load configurations for agent %s (%p)", pAgent->GetName(), pAgent);
1305 pAgent->CreateConfigurations(fpConfigurationHandler, rawReader, runloader);
1306 }
1307 }
1308
242bb794 1309 return iResult;
1310}
1311
f3506ea2 1312int AliHLTSystem::BuildTaskListsFromReconstructionChains(AliRawReader* rawReader, AliRunLoader* runloader)
242bb794 1313{
1314 // see header file for class documentation
1315 if (CheckStatus(kRunning)) {
1316 HLTError("HLT system in running state, can not configure");
1317 return -EBUSY;
1318 }
1319 if (!CheckStatus(kConfigurationLoaded)) {
1320 HLTWarning("configurations not yet loaded");
1321 return 0;
1322 }
1323
1324 int iResult=0;
f3506ea2 1325 int bHaveOutput=0;
1326
1327 // query chains
1328 TString chains;
1329 if (fChains.Length()>0) {
1330 chains=fChains;
8f471af0 1331 HLTImportant("custom reconstruction chain: %s", chains.Data());
f3506ea2 1332 } else {
1333 AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent();
1334 while ((pAgent || fChains.Length()>0) && iResult>=0) {
1335 const char* agentchains=pAgent->GetReconstructionChains(rawReader, runloader);
1336 if (agentchains) {
703bcca6 1337 if (!chains.IsNull()) chains+=" ";
f3506ea2 1338 chains+=agentchains;
1339 HLTInfo("reconstruction chains for agent %s (%p): %s", pAgent->GetName(), pAgent, agentchains);
1340 }
1341 pAgent=AliHLTModuleAgent::GetNextAgent();
c043fa2c 1342 }
f3506ea2 1343 }
1344
1345 // build task list for chains
1346 TObjArray* pTokens=chains.Tokenize(" ");
1347 if (pTokens) {
77fd699f 1348 int iEntries=pTokens->GetEntriesFast();
f3506ea2 1349 for (int i=0; i<iEntries && iResult>=0; i++) {
1350 const char* pCID=((TObjString*)pTokens->At(i))->GetString().Data();
1351 AliHLTConfiguration* pConf=fpConfigurationHandler->FindConfiguration(pCID);
1352 if (pConf) {
1353 iResult=BuildTaskList(pConf);
f3c1d403 1354 if (true) { // condition was deprecated but kept for sake of svn diff
1355 // bHaveOutput variable has to be set for both running modes
1356 // AliHLTSimulation and AliHLTReconstruction
f3506ea2 1357 assert(fpComponentHandler!=NULL);
1358 TString cid=pConf->GetComponentID();
f3c1d403 1359 if (runloader!=NULL && cid.CompareTo("HLTOUT")==0) {
f3506ea2 1360 // remove from the input of a global HLTOUT configuration
1361 chains.ReplaceAll(pCID, "");
1362 } else if (bHaveOutput==0) {
1363 // check whether this configuration produces data output
1364 if ((bHaveOutput=fpComponentHandler->HasOutputData(cid.Data()))<0) {
1365 bHaveOutput=0;
1366 chains.ReplaceAll(pCID, "");
1367 }
1368 }
242bb794 1369 }
f3506ea2 1370 } else {
1371 HLTWarning("can not find configuration %s", pCID);
242bb794 1372 }
242bb794 1373 }
f3506ea2 1374 delete pTokens;
1375 }
1376
1377 // build HLTOUT for simulation
1378 if (iResult>=0 && runloader) {
1379 if (bHaveOutput) {
1380 // there are components in the chain which produce data which need to be
1381 // piped to an HLTOUT
e0e92eab 1382
1383 // add the SchemaEvolutionComponent which analyzes the ROOT objects in
1384 // the output stream
1385 if (fpComponentHandler->FindComponentIndex("ROOTSchemaEvolutionComponent")>=0 ||
1386 fpComponentHandler->LoadLibrary("libAliHLTUtil.so")>=0) {
1387 AliHLTConfiguration schemaevo("_schemaevolution_", "ROOTSchemaEvolutionComponent",
1388 chains.Data(), "-file=HLT.StreamerInfo.root");
1389 iResult=BuildTaskList("_schemaevolution_");
1390 } else {
1391 HLTWarning("can not load libAliHLTUtil.so and ROOTSchemaEvolutionComponent");
1392 }
1393
1394 // add the HLTOUT component
f3506ea2 1395 if (fpComponentHandler->FindComponentIndex("HLTOUT")>=0 ||
703bcca6 1396 fpComponentHandler->LoadLibrary("libHLTsim.so")>=0) {
f3506ea2 1397 AliHLTConfiguration globalout("_globalout_", "HLTOUT", chains.Data(), NULL);
1398 iResult=BuildTaskList("_globalout_");
1399 } else {
1400 HLTError("can not load libHLTsim.so and HLTOUT component");
1401 iResult=-EFAULT;
1402 }
c043fa2c 1403 }
242bb794 1404 }
f3506ea2 1405
f3c1d403 1406 // build HLTOUT task for reconstruction
1407 // Matthias 08.07.2008 the rawReader is never set when running embedded into
1408 // AliReconstruction. The system is configured during AliHLTReconstructor::Init
1409 // where the RawReader is not available. It is available in the first invocation
1410 // of Reconstruct.
1411 //
1412 // That means that policy is slightly changed:
1413 // - if the run loader is available -> AliSimulation
1414 // - no run loader available -> AliReconstruction
1415 if (iResult>=0 && !runloader) {
1416 if (bHaveOutput) {
1417 // there are components in the chain which produce data which need to be
1418 // piped to an HLTOUT sub-collection
1419 if (!fpHLTOUTTask) {
19b5c321 1420 iResult=AddHLTOUTTask(chains.Data());
f3c1d403 1421 }
1422 }
1423 }
1424
242bb794 1425 if (iResult>=0) SetStatusFlags(kTaskListCreated);
1426
1427 return iResult;
1428}
1429
19b5c321 1430int AliHLTSystem::AddHLTOUTTask(const char* hltoutchains)
1431{
1432 // see header file for class documentation
1433 int iResult=0;
1434 if (!hltoutchains || hltoutchains[0]==0) return 0;
1435
1436 // check chains for output
1437 TString chains=hltoutchains;
1438 TObjArray* pTokens=chains.Tokenize(" ");
1439 if (pTokens) {
77fd699f 1440 int iEntries=pTokens->GetEntriesFast();
19b5c321 1441 for (int i=0; i<iEntries && iResult>=0; i++) {
1442 const char* token=((TObjString*)pTokens->At(i))->GetString().Data();
1443 AliHLTConfiguration* pConf=fpConfigurationHandler->FindConfiguration(token);
1444 if (pConf) {
1445 TString cid=pConf->GetComponentID();
1446 if (fpComponentHandler->HasOutputData(cid.Data())) {
1447 continue;
1448 }
1449 } else {
1450 HLTWarning("can not find configuration %s", token);
1451 }
1452 // remove from the list of hltout chains
1453 chains.ReplaceAll(token, "");
1454 }
1455 delete pTokens;
1456 }
1457
1458 // do not create the HLTOUT task if none of the chains have output
1459 if (chains.IsNull()) return 0;
1460
1461 // indicate the task to be available
1462 iResult=1;
1463
1464 if (fpHLTOUTTask) {
1465 if (strcmp(chains.Data(), fpHLTOUTTask->GetSourceChains())==0) {
1466 HLTWarning("HLTOUT task already added for chains \"%s\" %p", chains.Data(), fpHLTOUTTask);
1467 } else {
1468 HLTError("HLTOUT task already added for chains \"%s\" %p, ignoring new chains \"%s\"",
1469 fpHLTOUTTask->GetSourceChains(), fpHLTOUTTask, chains.Data());
1470 }
1471 return iResult;
1472 }
1473
1474 fpHLTOUTTask=new AliHLTOUTTask(chains);
1475 if (fpHLTOUTTask) {
1476 if (fpHLTOUTTask->GetConf() && fpHLTOUTTask->GetConf()->SourcesResolved()>=0) {
1477 iResult=InsertTask(fpHLTOUTTask);
1478 } else {
1479 HLTError("HLTOUT task (%s) sources not resolved", fpHLTOUTTask->GetName());
1480 iResult=-ENOENT;
1481 }
1482
1483 if (iResult<0) {
1484 delete fpHLTOUTTask;
1485 }
1486
1487 } else {
1488 iResult=-ENOMEM;
1489 }
1490 return iResult;
1491}
1492
242bb794 1493int AliHLTSystem::CheckStatus(int flag)
1494{
1495 // see header file for class documentation
1496 if (flag==kUninitialized && flag==fState) return 1;
1497 if ((fState&flag)==flag) return 1;
1498 return 0;
1499}
1500
1501int AliHLTSystem::GetStatusFlags()
1502{
1503 // see header file for class documentation
1504 return fState;
1505}
1506
1507int AliHLTSystem::SetStatusFlags(int flags)
1508{
1509 // see header file for class documentation
1510 fState|=flags;
1511 return fState;
1512}
1513
1514int AliHLTSystem::ClearStatusFlags(int flags)
1515{
1516 // see header file for class documentation
1517 fState&=~flags;
1518 return fState;
1519}
85f0cede 1520
f331e6c5 1521AliHLTfctVoid AliHLTSystem::FindDynamicSymbol(const char* library, const char* symbol)
85f0cede 1522{
c043fa2c 1523 // see header file for class documentation
85f0cede 1524 if (fpComponentHandler==NULL) return NULL;
1525 return fpComponentHandler->FindSymbol(library, symbol);
1526}
dba03d72 1527
1528void AliHLTSystem::SetFrameworkLog(AliHLTComponentLogSeverity level)
1529{
6a8e0bb4 1530 // see header file for class documentation
dba03d72 1531 SetLocalLoggingLevel(level);
1532 if (fpComponentHandler) fpComponentHandler->SetLocalLoggingLevel(level);
1533 if (fpConfigurationHandler) fpConfigurationHandler->SetLocalLoggingLevel(level);
1534}
97d2b87a 1535
1536int AliHLTSystem::LoggingVarargs(AliHLTComponentLogSeverity severity,
1537 const char* originClass, const char* originFunc,
1538 const char* file, int line, ... ) const
1539{
1540 // see header file for function documentation
1541 int iResult=0;
1542
1543 va_list args;
1544 va_start(args, line);
1545
1546 if (!fName.IsNull())
7efb6418 1547 AliHLTLogging::SetLogString(this, " (%p)", "%s_pfmt_: ", fName.Data());
97d2b87a 1548 iResult=SendMessage(severity, originClass, originFunc, file, line, AliHLTLogging::BuildLogString(NULL, args, !fName.IsNull() /*append if non empty*/));
1549 va_end(args);
1550
1551 return iResult;
1552}