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