]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTSystem.cxx
- changes according to coding conventions and documentation
[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 /** ROOT macro for the implementation of ROOT specific class methods */
38 ClassImp(AliHLTSystem)
39
40 AliHLTSystem::AliHLTSystem()
41   :
42   fpComponentHandler(new AliHLTComponentHandler()),
43   fpConfigurationHandler(new AliHLTConfigurationHandler()),
44   fTaskList()
45 {
46   if (fpComponentHandler) {
47     AliHLTComponentEnvironment env;
48     memset(&env, 0, sizeof(AliHLTComponentEnvironment));
49     env.fLoggingFunc=AliHLTLogging::Message;
50     fpComponentHandler->SetEnvironment(&env);
51
52     // init logging function in AliHLTLogging
53     Init(AliHLTLogging::Message);
54   } else {
55     HLTFatal("can not create Component Handler");
56   }
57   if (fpConfigurationHandler) {
58     AliHLTConfiguration::GlobalInit(fpConfigurationHandler);
59   } else {
60     HLTFatal("can not create Configuration Handler");
61   }
62 }
63
64 AliHLTSystem::AliHLTSystem(const AliHLTSystem&)
65   :
66   AliHLTLogging(),
67   fpComponentHandler(NULL),
68   fpConfigurationHandler(NULL),
69   fTaskList()
70 {
71   HLTFatal("copy constructor untested");
72 }
73
74 AliHLTSystem& AliHLTSystem::operator=(const AliHLTSystem&)
75
76   HLTFatal("assignment operator untested");
77   return *this;
78 }
79
80 AliHLTSystem::~AliHLTSystem()
81 {
82   CleanTaskList();
83   AliHLTConfiguration::GlobalDeinit();
84   if (fpConfigurationHandler) {
85     delete fpConfigurationHandler;
86   }
87   fpConfigurationHandler=NULL;
88   
89   if (fpComponentHandler) {
90     delete fpComponentHandler;
91   }
92   fpComponentHandler=NULL;
93 }
94
95 int AliHLTSystem::AddConfiguration(AliHLTConfiguration* pConf)
96 {
97   int iResult=0;
98   if (pConf) {
99   } else {
100     iResult=-EINVAL;
101   }
102   return iResult;
103 }
104
105 int AliHLTSystem::InsertConfiguration(AliHLTConfiguration* pConf, AliHLTConfiguration* pPrec)
106 {
107   int iResult=0;
108   if (pConf) {
109     if (pPrec) {
110       // find the position
111     }
112   } else {
113     iResult=-EINVAL;
114   }
115   return iResult;
116 }
117
118 int AliHLTSystem::DeleteConfiguration(AliHLTConfiguration* pConf)
119 {
120   int iResult=0;
121   if (pConf) {
122   } else {
123     iResult=-EINVAL;
124   }
125   return iResult;
126 }
127
128 int AliHLTSystem::BuildTaskList(AliHLTConfiguration* pConf)
129 {
130   int iResult=0;
131   if (pConf) {
132     AliHLTTask* pTask=NULL;
133     if ((pTask=FindTask(pConf->GetName()))!=NULL) {
134       if (pTask->GetConf()!=pConf) {
135         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);
136         iResult=-EEXIST;
137         pTask=NULL;
138       }
139     } else if (pConf->SourcesResolved(1)!=1) {
140         HLTError("configuration \"%s\" has unresolved sources, aborting ...", pConf->GetName());
141         iResult=-ENOLINK;
142     } else {
143       pTask=new AliHLTTask(pConf);
144       if (pTask==NULL) {
145         iResult=-ENOMEM;
146       }
147     }
148     if (pTask) {
149       // check for circular dependencies
150       if ((iResult=pConf->FollowDependency(pConf->GetName()))>0) {
151         HLTError("detected circular dependency for configuration \"%s\"", pTask->GetName());
152         pTask->PrintDependencyTree(pTask->GetName(), 1/*use the configuration list*/);
153         HLTError("aborted ...");
154         iResult=-ELOOP;
155       }
156       if (iResult>=0) {
157         // check whether all dependencies are already in the task list
158         // create the missing ones
159         // this step is an iterative process which calls this function again for the missing
160         // configurations, in order to avoid the currently processed task to be created
161         // again it is added to the list temporarily and removed afterwards
162         // This is of high importance to preserve the order of the tasks. Furthermore, the
163         // InsertTask method has to be used in order to set all the cross links right 
164         fTaskList.Add(pTask);
165         AliHLTConfiguration* pDep=pConf->GetFirstSource();
166         while (pDep!=NULL && iResult>=0) {
167           if (FindTask(pDep->GetName())==NULL) {
168             iResult=BuildTaskList(pDep);
169           }
170           pDep=pConf->GetNextSource();
171         }
172         // remove the temporarily added task
173         fTaskList.Remove(pTask);
174
175         // insert the task and set the cross-links
176         if (iResult>=0) {
177           iResult=InsertTask(pTask);
178         }
179       } else {
180         delete pTask;
181         pTask=NULL;
182       }
183     }
184   } else {
185     iResult=-EINVAL;
186   }
187   return iResult;
188 }
189
190 int AliHLTSystem::CleanTaskList()
191 {
192   int iResult=0;
193   TObjLink* lnk=NULL;
194   while ((lnk=fTaskList.FirstLink())!=NULL) {
195     fTaskList.Remove(lnk);
196     delete (lnk->GetObject());
197   }
198   return iResult;
199 }
200
201 int AliHLTSystem::InsertTask(AliHLTTask* pTask)
202 {
203   int iResult=0;
204   TObjLink *lnk = NULL;
205   if ((iResult=pTask->CheckDependencies())>0)
206     lnk=fTaskList.FirstLink();
207   while (lnk && iResult>0) {
208     AliHLTTask* pCurr = (AliHLTTask*)lnk->GetObject();
209     //HLTDebug("checking  \"%s\"", pCurr->GetName());
210     iResult=pTask->Depends(pCurr);
211     if (iResult>0) {
212       iResult=pTask->SetDependency(pCurr);
213       pCurr->SetTarget(pTask);
214       HLTDebug("set dependency  \"%s\" for configuration \"%s\"", pCurr->GetName(), pTask->GetName());
215     }
216     if (pCurr->Depends(pTask)) {
217       // circular dependency
218       HLTError("circular dependency: can not resolve dependencies for configuration \"%s\"", pTask->GetName());
219       iResult=-ELOOP;
220     } else if ((iResult=pTask->CheckDependencies())>0) {
221       lnk = lnk->Next();
222     }
223   }
224   if (iResult==0) {
225       if (lnk) {
226         fTaskList.AddAfter(lnk, pTask);
227       } else {
228         fTaskList.AddFirst(pTask);
229       }
230       HLTDebug("task \"%s\" inserted", pTask->GetName());
231   } else if (iResult>0) {
232     HLTError("can not resolve dependencies for configuration \"%s\" (%d unresolved)", pTask->GetName(), iResult);
233     iResult=-ENOLINK;
234   }
235   return iResult;
236 }
237
238 AliHLTTask* AliHLTSystem::FindTask(const char* id)
239 {
240   AliHLTTask* pTask=NULL;
241   if (id) {
242     pTask=(AliHLTTask*)fTaskList.FindObject(id); 
243   }
244   return pTask;
245 }
246
247 void AliHLTSystem::PrintTaskList()
248 {
249   HLTLogKeyword("task list");
250   TObjLink *lnk = NULL;
251   HLTMessage("Task List");
252   lnk=fTaskList.FirstLink();
253   while (lnk) {
254     TObject* obj=lnk->GetObject();
255     if (obj) {
256       HLTMessage("  %s - status:", obj->GetName());
257       AliHLTTask* pTask=(AliHLTTask*)obj;
258       pTask->PrintStatus();
259     } else {
260     }
261     lnk = lnk->Next();
262   }
263 }
264
265 void AliHLTSystem::PrintComponentDataTypeInfo(const AliHLTComponentDataType& dt) {
266   TString msg;
267   msg.Form("AliHLTComponentDataType(%d): ID=\"", dt.fStructSize);
268   for ( unsigned i = 0; i < kAliHLTComponentDataTypefIDsize; i++ ) {
269    if (dt.fID[i]!=0) msg+=dt.fID[i];
270    else msg+="\\0";
271   }
272   msg+="\" Origin=\"";
273   for ( unsigned i = 0; i < kAliHLTComponentDataTypefOriginSize; i++ ) {
274    if (dt.fOrigin[i]!=0) msg+=dt.fOrigin[i];
275    else msg+="\\0";
276   }
277   msg+="\"";
278   HLTMessage(msg.Data());
279 }
280
281 int AliHLTSystem::Run(Int_t iNofEvents) 
282 {
283   int iResult=0;
284   if ((iResult=StartTasks())>=0) {
285     for (int i=0; i<iNofEvents && iResult>=0; i++) {
286       iResult=ProcessTasks(i);
287       if (iResult>=0) {
288         HLTInfo("Event %d successfully finished (%d)", i, iResult);
289         iResult=0;
290       } else {
291         HLTError("Processing of event %d failed (%d)", i, iResult);
292         // TODO: define different running modes to either ignore errors in
293         // event processing or not
294         // currently ignored 
295         iResult=0;
296       }
297     }
298     StopTasks();
299   } else {
300     HLTError("can not start task list");
301   }
302   return iResult;
303 }
304
305 int AliHLTSystem::StartTasks()
306 {
307   int iResult=0;
308   TObjLink *lnk=fTaskList.FirstLink();
309   while (lnk && iResult>=0) {
310     TObject* obj=lnk->GetObject();
311     if (obj) {
312       AliHLTTask* pTask=(AliHLTTask*)obj;
313       iResult=pTask->StartRun();
314     } else {
315     }
316     lnk = lnk->Next();
317   }
318   if (iResult<0) {
319   }
320   return iResult;
321 }
322
323 int AliHLTSystem::ProcessTasks(Int_t eventNo)
324 {
325   int iResult=0;
326   HLTDebug("processing event no %d", eventNo);
327   TObjLink *lnk=fTaskList.FirstLink();
328   while (lnk && iResult>=0) {
329     TObject* obj=lnk->GetObject();
330     if (obj) {
331       AliHLTTask* pTask=(AliHLTTask*)obj;
332       iResult=pTask->ProcessTask();
333     } else {
334     }
335     lnk = lnk->Next();
336   }
337   return iResult;
338 }
339
340 int AliHLTSystem::StopTasks()
341 {
342   int iResult=0;
343   TObjLink *lnk=fTaskList.FirstLink();
344   while (lnk && iResult>=0) {
345     TObject* obj=lnk->GetObject();
346     if (obj) {
347       AliHLTTask* pTask=(AliHLTTask*)obj;
348       iResult=pTask->EndRun();
349     } else {
350     }
351     lnk = lnk->Next();
352   }
353   return iResult;
354 }