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