- adapted to AliRoot logging system, messages printed out via AliRoot
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTSystem.cxx
CommitLineData
f23a6e1a 1// $Id$
2
3/**************************************************************************
4 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
5 * *
6 * Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
f23a6e1a 7 * for The ALICE Off-line 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 **************************************************************************/
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
85869391 28#include "AliHLTStdIncludes.h"
f23a6e1a 29#include "AliHLTSystem.h"
30#include "AliHLTComponentHandler.h"
31#include "AliHLTComponent.h"
5ec8e281 32#include "AliHLTConfiguration.h"
c38ba6f9 33#include "AliHLTConfigurationHandler.h"
34#include "AliHLTTask.h"
2d7ff710 35#include "TString.h"
f23a6e1a 36
b22e91eb 37/** ROOT macro for the implementation of ROOT specific class methods */
f23a6e1a 38ClassImp(AliHLTSystem)
39
f23a6e1a 40AliHLTSystem::AliHLTSystem()
85869391 41 :
42 fpComponentHandler(new AliHLTComponentHandler()),
43 fpConfigurationHandler(new AliHLTConfigurationHandler()),
44 fTaskList()
f23a6e1a 45{
70ed7d01 46 // see header file for class documentation
47 // or
48 // refer to README to build package
49 // or
50 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
5f5b708b 51
fc455fba 52 if (fgNofInstances++>0)
53 HLTWarning("multiple instances of AliHLTSystem, you should not use more than one at a time");
54
5f5b708b 55 SetGlobalLoggingLevel(kHLTLogDefault);
f23a6e1a 56 if (fpComponentHandler) {
57 AliHLTComponentEnvironment env;
58 memset(&env, 0, sizeof(AliHLTComponentEnvironment));
9ce4bf4a 59 env.fAllocMemoryFunc=AliHLTSystem::AllocMemory;
fc455fba 60 env.fLoggingFunc=NULL;
f23a6e1a 61 fpComponentHandler->SetEnvironment(&env);
85465857 62 } else {
63 HLTFatal("can not create Component Handler");
64 }
85465857 65 if (fpConfigurationHandler) {
66 AliHLTConfiguration::GlobalInit(fpConfigurationHandler);
67 } else {
68 HLTFatal("can not create Configuration Handler");
f23a6e1a 69 }
70}
71
85869391 72AliHLTSystem::AliHLTSystem(const AliHLTSystem&)
73 :
53feaef5 74 AliHLTLogging(),
85869391 75 fpComponentHandler(NULL),
76 fpConfigurationHandler(NULL),
77 fTaskList()
78{
70ed7d01 79 // see header file for class documentation
fc455fba 80 if (fgNofInstances++>0)
81 HLTWarning("multiple instances of AliHLTSystem, you should not use more than one at a time");
82
85869391 83 HLTFatal("copy constructor untested");
84}
85
86AliHLTSystem& AliHLTSystem::operator=(const AliHLTSystem&)
87{
70ed7d01 88 // see header file for class documentation
85869391 89 HLTFatal("assignment operator untested");
90 return *this;
91}
f23a6e1a 92
93AliHLTSystem::~AliHLTSystem()
94{
70ed7d01 95 // see header file for class documentation
fc455fba 96 fgNofInstances--;
2d7ff710 97 CleanTaskList();
fc455fba 98 AliHLTConfiguration::GlobalDeinit(fpConfigurationHandler);
2d7ff710 99 if (fpConfigurationHandler) {
100 delete fpConfigurationHandler;
101 }
102 fpConfigurationHandler=NULL;
103
104 if (fpComponentHandler) {
105 delete fpComponentHandler;
106 }
107 fpComponentHandler=NULL;
f23a6e1a 108}
109
fc455fba 110int AliHLTSystem::fgNofInstances=0;
111
5ec8e281 112int AliHLTSystem::AddConfiguration(AliHLTConfiguration* pConf)
113{
70ed7d01 114 // see header file for class documentation
5ec8e281 115 int iResult=0;
53feaef5 116 if (pConf) {
117 } else {
118 iResult=-EINVAL;
119 }
5ec8e281 120 return iResult;
121}
122
014d39ce 123int AliHLTSystem::InsertConfiguration(AliHLTConfiguration* pConf, AliHLTConfiguration* pPrec)
124{
70ed7d01 125 // see header file for class documentation
014d39ce 126 int iResult=0;
53feaef5 127 if (pConf) {
128 if (pPrec) {
129 // find the position
130 }
131 } else {
132 iResult=-EINVAL;
133 }
014d39ce 134 return iResult;
135}
5ec8e281 136
137int AliHLTSystem::DeleteConfiguration(AliHLTConfiguration* pConf)
138{
70ed7d01 139 // see header file for class documentation
5ec8e281 140 int iResult=0;
53feaef5 141 if (pConf) {
142 } else {
143 iResult=-EINVAL;
144 }
5ec8e281 145 return iResult;
146}
147
148int AliHLTSystem::BuildTaskList(AliHLTConfiguration* pConf)
149{
70ed7d01 150 // see header file for class documentation
f23a6e1a 151 int iResult=0;
5ec8e281 152 if (pConf) {
153 AliHLTTask* pTask=NULL;
154 if ((pTask=FindTask(pConf->GetName()))!=NULL) {
155 if (pTask->GetConf()!=pConf) {
85465857 156 HLTError("configuration missmatch, there is already a task with configuration name \"%s\", but it is different. Most likely configuration %p is not registered properly", pConf->GetName(), pConf);
5ec8e281 157 iResult=-EEXIST;
158 pTask=NULL;
159 }
160 } else if (pConf->SourcesResolved(1)!=1) {
85465857 161 HLTError("configuration \"%s\" has unresolved sources, aborting ...", pConf->GetName());
5ec8e281 162 iResult=-ENOLINK;
163 } else {
53feaef5 164 pTask=new AliHLTTask(pConf);
5ec8e281 165 if (pTask==NULL) {
166 iResult=-ENOMEM;
167 }
168 }
169 if (pTask) {
3b35e87c 170 // check for circular dependencies
5ec8e281 171 if ((iResult=pConf->FollowDependency(pConf->GetName()))>0) {
3b35e87c 172 HLTError("detected circular dependency for configuration \"%s\"", pTask->GetName());
5ec8e281 173 pTask->PrintDependencyTree(pTask->GetName(), 1/*use the configuration list*/);
85465857 174 HLTError("aborted ...");
5ec8e281 175 iResult=-ELOOP;
176 }
177 if (iResult>=0) {
178 // check whether all dependencies are already in the task list
179 // create the missing ones
3b35e87c 180 // this step is an iterative process which calls this function again for the missing
181 // configurations, in order to avoid the currently processed task to be created
182 // again it is added to the list temporarily and removed afterwards
183 // This is of high importance to preserve the order of the tasks. Furthermore, the
184 // InsertTask method has to be used in order to set all the cross links right
5ec8e281 185 fTaskList.Add(pTask);
186 AliHLTConfiguration* pDep=pConf->GetFirstSource();
187 while (pDep!=NULL && iResult>=0) {
188 if (FindTask(pDep->GetName())==NULL) {
189 iResult=BuildTaskList(pDep);
190 }
191 pDep=pConf->GetNextSource();
192 }
3b35e87c 193 // remove the temporarily added task
5ec8e281 194 fTaskList.Remove(pTask);
195
196 // insert the task and set the cross-links
197 if (iResult>=0) {
198 iResult=InsertTask(pTask);
199 }
200 } else {
201 delete pTask;
202 pTask=NULL;
203 }
204 }
205 } else {
206 iResult=-EINVAL;
f23a6e1a 207 }
f23a6e1a 208 return iResult;
209}
210
5ec8e281 211int AliHLTSystem::CleanTaskList()
212{
70ed7d01 213 // see header file for class documentation
5ec8e281 214 int iResult=0;
215 TObjLink* lnk=NULL;
216 while ((lnk=fTaskList.FirstLink())!=NULL) {
217 fTaskList.Remove(lnk);
218 delete (lnk->GetObject());
219 }
220 return iResult;
221}
222
223int AliHLTSystem::InsertTask(AliHLTTask* pTask)
224{
70ed7d01 225 // see header file for class documentation
5ec8e281 226 int iResult=0;
227 TObjLink *lnk = NULL;
228 if ((iResult=pTask->CheckDependencies())>0)
229 lnk=fTaskList.FirstLink();
230 while (lnk && iResult>0) {
231 AliHLTTask* pCurr = (AliHLTTask*)lnk->GetObject();
85465857 232 //HLTDebug("checking \"%s\"", pCurr->GetName());
5ec8e281 233 iResult=pTask->Depends(pCurr);
234 if (iResult>0) {
235 iResult=pTask->SetDependency(pCurr);
236 pCurr->SetTarget(pTask);
85465857 237 HLTDebug("set dependency \"%s\" for configuration \"%s\"", pCurr->GetName(), pTask->GetName());
f23a6e1a 238 }
5ec8e281 239 if (pCurr->Depends(pTask)) {
3b35e87c 240 // circular dependency
241 HLTError("circular dependency: can not resolve dependencies for configuration \"%s\"", pTask->GetName());
5ec8e281 242 iResult=-ELOOP;
243 } else if ((iResult=pTask->CheckDependencies())>0) {
244 lnk = lnk->Next();
245 }
246 }
247 if (iResult==0) {
248 if (lnk) {
249 fTaskList.AddAfter(lnk, pTask);
250 } else {
251 fTaskList.AddFirst(pTask);
252 }
85465857 253 HLTDebug("task \"%s\" inserted", pTask->GetName());
5ec8e281 254 } else if (iResult>0) {
85465857 255 HLTError("can not resolve dependencies for configuration \"%s\" (%d unresolved)", pTask->GetName(), iResult);
5ec8e281 256 iResult=-ENOLINK;
f23a6e1a 257 }
5ec8e281 258 return iResult;
f23a6e1a 259}
260
5ec8e281 261AliHLTTask* AliHLTSystem::FindTask(const char* id)
262{
70ed7d01 263 // see header file for class documentation
5ec8e281 264 AliHLTTask* pTask=NULL;
265 if (id) {
266 pTask=(AliHLTTask*)fTaskList.FindObject(id);
267 }
268 return pTask;
269}
f23a6e1a 270
5ec8e281 271void AliHLTSystem::PrintTaskList()
272{
70ed7d01 273 // see header file for class documentation
85465857 274 HLTLogKeyword("task list");
5ec8e281 275 TObjLink *lnk = NULL;
85465857 276 HLTMessage("Task List");
5ec8e281 277 lnk=fTaskList.FirstLink();
278 while (lnk) {
279 TObject* obj=lnk->GetObject();
280 if (obj) {
85465857 281 HLTMessage(" %s - status:", obj->GetName());
5ec8e281 282 AliHLTTask* pTask=(AliHLTTask*)obj;
283 pTask->PrintStatus();
284 } else {
285 }
286 lnk = lnk->Next();
287 }
288}
014d39ce 289
53feaef5 290int AliHLTSystem::Run(Int_t iNofEvents)
014d39ce 291{
70ed7d01 292 // see header file for class documentation
014d39ce 293 int iResult=0;
9ce4bf4a 294 if ((iResult=InitTasks())>=0) {
295 if ((iResult=StartTasks())>=0) {
296 for (int i=0; i<iNofEvents && iResult>=0; i++) {
297 iResult=ProcessTasks(i);
298 if (iResult>=0) {
299 HLTInfo("Event %d successfully finished (%d)", i, iResult);
300 iResult=0;
301 } else {
302 HLTError("Processing of event %d failed (%d)", i, iResult);
303 // TODO: define different running modes to either ignore errors in
304 // event processing or not
305 // currently ignored
306 //iResult=0;
307 }
2d7ff710 308 }
9ce4bf4a 309 StopTasks();
310 } else {
311 HLTError("can not start task list");
53feaef5 312 }
9ce4bf4a 313 DeinitTasks();
fc455fba 314 } else if (iResult!=-ENOENT) {
9ce4bf4a 315 HLTError("can not initialize task list");
316 }
317 return iResult;
318}
319
320int AliHLTSystem::InitTasks()
321{
70ed7d01 322 // see header file for class documentation
9ce4bf4a 323 int iResult=0;
324 TObjLink *lnk=fTaskList.FirstLink();
fc455fba 325 if (lnk==NULL) {
326 HLTWarning("Task list is empty, aborting ...");
327 return -ENOENT;
328 }
9ce4bf4a 329 while (lnk && iResult>=0) {
330 TObject* obj=lnk->GetObject();
331 if (obj) {
332 AliHLTTask* pTask=(AliHLTTask*)obj;
333 iResult=pTask->Init(NULL, fpComponentHandler);
334 } else {
335 }
336 lnk = lnk->Next();
337 }
338 if (iResult<0) {
53feaef5 339 }
340 return iResult;
341}
342
343int AliHLTSystem::StartTasks()
344{
70ed7d01 345 // see header file for class documentation
53feaef5 346 int iResult=0;
347 TObjLink *lnk=fTaskList.FirstLink();
348 while (lnk && iResult>=0) {
349 TObject* obj=lnk->GetObject();
350 if (obj) {
351 AliHLTTask* pTask=(AliHLTTask*)obj;
352 iResult=pTask->StartRun();
353 } else {
354 }
355 lnk = lnk->Next();
356 }
357 if (iResult<0) {
358 }
359 return iResult;
360}
361
362int AliHLTSystem::ProcessTasks(Int_t eventNo)
363{
70ed7d01 364 // see header file for class documentation
53feaef5 365 int iResult=0;
366 HLTDebug("processing event no %d", eventNo);
367 TObjLink *lnk=fTaskList.FirstLink();
368 while (lnk && iResult>=0) {
369 TObject* obj=lnk->GetObject();
370 if (obj) {
371 AliHLTTask* pTask=(AliHLTTask*)obj;
9ce4bf4a 372 iResult=pTask->ProcessTask(eventNo);
373 HLTDebug("task %s finnished (%d)", pTask->GetName(), iResult);
53feaef5 374 } else {
375 }
376 lnk = lnk->Next();
377 }
378 return iResult;
379}
380
381int AliHLTSystem::StopTasks()
382{
70ed7d01 383 // see header file for class documentation
53feaef5 384 int iResult=0;
385 TObjLink *lnk=fTaskList.FirstLink();
386 while (lnk && iResult>=0) {
387 TObject* obj=lnk->GetObject();
388 if (obj) {
389 AliHLTTask* pTask=(AliHLTTask*)obj;
390 iResult=pTask->EndRun();
391 } else {
392 }
393 lnk = lnk->Next();
394 }
014d39ce 395 return iResult;
396}
9ce4bf4a 397
398int AliHLTSystem::DeinitTasks()
399{
70ed7d01 400 // see header file for class documentation
9ce4bf4a 401 int iResult=0;
402 TObjLink *lnk=fTaskList.FirstLink();
403 while (lnk && iResult>=0) {
404 TObject* obj=lnk->GetObject();
405 if (obj) {
406 AliHLTTask* pTask=(AliHLTTask*)obj;
407 iResult=pTask->Deinit();
408 } else {
409 }
410 lnk = lnk->Next();
411 }
412 return iResult;
413}
414
415void* AliHLTSystem::AllocMemory( void* param, unsigned long size )
416{
70ed7d01 417 // see header file for class documentation
9ce4bf4a 418 return (void*)new char[size];
419}