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