3 /**************************************************************************
4 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
6 * Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
7 * for The ALICE Off-line Project. *
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 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////////////////
20 // HLT configuration handling //
22 ///////////////////////////////////////////////////////////////////////////////
29 #include "AliHLTConfiguration.h"
30 #include "AliHLTComponent.h"
31 #include "AliHLTComponentHandler.h"
35 ClassImp(AliHLTConfiguration)
37 /* the global configuration handler which is used to automatically register the configuration
39 AliHLTConfigurationHandler* AliHLTConfiguration::fConfigurationHandler=NULL;
41 AliHLTConfiguration::AliHLTConfiguration()
50 fListSrcElement=fListSources.begin();
53 AliHLTConfiguration::AliHLTConfiguration(const char* id, const char* component, const char* sources, const char* arguments)
58 if (id && component) {
61 fStringSources=sources;
64 fListSrcElement=fListSources.begin();
65 if (fConfigurationHandler) {
66 fConfigurationHandler->RegisterConfiguration(this);
68 HLTError("no configuration handler set, abort registration");
73 AliHLTConfiguration::~AliHLTConfiguration()
75 if (fConfigurationHandler) {
76 if (fConfigurationHandler->FindConfiguration(fID)!=NULL) {
77 fConfigurationHandler->RemoveConfiguration(this);
82 for (int i=0; i<fArgc; i++) {
91 int AliHLTConfiguration::GlobalInit(AliHLTConfigurationHandler* pHandler)
94 if (fConfigurationHandler!=NULL) {
95 fConfigurationHandler->Logging(kHLTLogWarning, "AliHLTConfiguration::GlobalInit", HLT_DEFAULT_LOG_KEYWORD, "configuration handler already initialized, overriding object %p", fConfigurationHandler);
97 fConfigurationHandler=pHandler;
101 int AliHLTConfiguration::GlobalDeinit()
104 fConfigurationHandler=NULL;
108 const char* AliHLTConfiguration::GetName() const {
111 return TObject::GetName();
114 AliHLTConfiguration* AliHLTConfiguration::GetSource(const char* id)
116 AliHLTConfiguration* pSrc=NULL;
118 // first check the current element
119 if (fListSrcElement!=fListSources.end() && strcmp(id, (*fListSrcElement)->GetName())==0) {
120 pSrc=*fListSrcElement;
124 pSrc=GetFirstSource();
126 if (strcmp(id, pSrc->GetName())==0)
128 pSrc=GetNextSource();
135 AliHLTConfiguration* AliHLTConfiguration::GetFirstSource()
137 AliHLTConfiguration* pSrc=NULL;
138 if (fNofSources>=0 || ExtractSources()) {
139 fListSrcElement=fListSources.begin();
140 if (fListSrcElement!=fListSources.end()) pSrc=*fListSrcElement;
145 AliHLTConfiguration* AliHLTConfiguration::GetNextSource()
147 AliHLTConfiguration* pSrc=NULL;
149 if (fListSrcElement!=fListSources.end() && (++fListSrcElement)!=fListSources.end())
150 pSrc=*fListSrcElement;
155 int AliHLTConfiguration::SourcesResolved(int bAuto)
158 if (fNofSources>=0 || bAuto && (iResult=ExtractSources())>=0) {
159 //HLTDebug("fNofSources=%d", fNofSources);
160 //HLTDebug("list size = %d", fListSources.size());
161 iResult=fNofSources==(int)fListSources.size();
166 int AliHLTConfiguration::InvalidateSource(AliHLTConfiguration* pConf)
170 vector<AliHLTConfiguration*>::iterator element=fListSources.begin();
171 while (element!=fListSources.end()) {
172 if (*element==pConf) {
173 fListSources.erase(element);
174 fListSrcElement=fListSources.end();
175 // there is no need to re-evaluate until there was a new configuration registered
176 // -> postpone the invalidation, its done in AliHLTConfigurationHandler::RegisterConfiguration
177 //InvalidateSources();
188 void AliHLTConfiguration::PrintStatus()
190 HLTLogKeyword("configuration status");
191 HLTMessage("status of configuration \"%s\" (%p)", GetName(), this);
192 if (fComponent) HLTMessage(" - component: \"%s\"", fComponent);
193 else HLTMessage(" - component string invalid");
194 if (fStringSources) HLTMessage(" - sources: \"%s\"", fStringSources);
195 else HLTMessage(" - no sources");
196 if (SourcesResolved(1)<=0)
197 HLTMessage(" there are unresolved sources");
198 AliHLTConfiguration* pSrc=GetFirstSource();
200 HLTMessage(" source \"%s\" (%p) resolved", pSrc->GetName(), pSrc);
201 pSrc=GetNextSource();
205 int AliHLTConfiguration::GetArguments(int* pArgc, const char*** pArgv)
208 if (pArgc && pArgv) {
210 *pArgv=(const char**)fArgv;
218 int AliHLTConfiguration::ExtractSources()
222 if (fStringSources!=NULL) {
223 vector<char*> tgtList;
224 fListSources.clear();
225 if ((iResult=InterpreteString(fStringSources, tgtList))>=0) {
226 fNofSources=tgtList.size();
227 vector<char*>::iterator element=tgtList.begin();
228 while ((element=tgtList.begin())!=tgtList.end()) {
229 if (fConfigurationHandler) {
230 AliHLTConfiguration* pConf=fConfigurationHandler->FindConfiguration(*element);
232 HLTDebug("source \"%s\" inserted", pConf->GetName());
233 fListSources.push_back(pConf);
235 HLTError("can not find source \"%s\"", (*element));
238 } else if (iResult>=0) {
240 HLTFatal("global configuration handler not initialized, can not resolve sources");
243 tgtList.erase(element);
245 fListSrcElement=fListSources.begin();
251 int AliHLTConfiguration::ExtractArguments()
254 if (fArguments!=NULL) {
255 vector<char*> tgtList;
256 if ((iResult=InterpreteString(fArguments, tgtList))>=0) {
257 fArgc=tgtList.size();
258 //HLTDebug("found %d arguments", fArgc);
260 fArgv = new char*[fArgc];
262 vector<char*>::iterator element=tgtList.begin();
264 while (element!=tgtList.end()) {
265 //HLTDebug("assign arguments %d (%s)", i, *element);
266 fArgv[i++]=(*element);
278 int AliHLTConfiguration::InterpreteString(const char* arg, vector<char*>& argList)
282 //HLTDebug("interprete \"%s\"", arg);
286 if (arg[i]==0 || arg[i]==' ') {
288 char* pEntry= new char[i-prec+1];
290 strncpy(pEntry, &arg[prec], i-prec);
291 pEntry[i-prec]=0; // terminate string
292 //HLTDebug("create string \"%s\", insert at %d", pEntry, argList.size());
293 argList.push_back(pEntry);
298 } else if (prec==-1) prec=i;
299 } while (arg[i++]!=0 && iResult>=0);
306 int AliHLTConfiguration::FollowDependency(const char* id, TList* pTgtList)
310 AliHLTConfiguration* pDep=NULL;
311 if ((pDep=GetSource(id))!=NULL) {
312 if (pTgtList) pTgtList->Add(pDep);
315 pDep=GetFirstSource();
316 while (pDep && iResult==0) {
317 if ((iResult=pDep->FollowDependency(id, pTgtList))>0) {
318 if (pTgtList) pTgtList->AddFirst(pDep);
321 pDep=GetNextSource();
330 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
334 AliHLTTask::AliHLTTask()
336 fpConfiguration=NULL;
338 fpBlockDataArray=NULL;
341 AliHLTTask::AliHLTTask(AliHLTConfiguration* fConf, AliHLTComponentHandler* pCH)
343 fpConfiguration=NULL;
345 fpBlockDataArray=NULL;
349 AliHLTTask::~AliHLTTask()
351 if (fpComponent) delete fpComponent;
353 if (fpBlockDataArray) delete[] fpBlockDataArray;
354 fpBlockDataArray=NULL;
357 int AliHLTTask::Init(AliHLTConfiguration* fConf, AliHLTComponentHandler* pCH)
361 fpConfiguration=fConf;
364 const char** argv=NULL;
365 if ((iResult=fConf->GetArguments(&argc, &argv))>=0) {
366 iResult=pCH->CreateComponent(fConf->GetComponentID(), NULL, argc, argv, fpComponent);
369 HLTError("can not find component \"%s\"", fConf->GetComponentID());
379 const char *AliHLTTask::GetName() const
382 return fpConfiguration->GetName();
383 return TObject::GetName();
386 AliHLTConfiguration* AliHLTTask::GetConf()
388 return fpConfiguration;
391 AliHLTComponent* AliHLTTask::GetComponent()
396 AliHLTTask* AliHLTTask::FindDependency(const char* id)
398 AliHLTTask* pTask=NULL;
400 pTask=(AliHLTTask*)fListDependencies.FindObject(id);
405 int AliHLTTask::FollowDependency(const char* id, TList* pTgtList)
409 AliHLTTask* pDep=NULL;
410 if ((pDep=(AliHLTTask*)fListDependencies.FindObject(id))!=NULL) {
411 if (pTgtList) pTgtList->Add(pDep);
414 TObjLink* lnk=fListDependencies.FirstLink();
415 while (lnk && iResult==0) {
416 pDep=(AliHLTTask*)lnk->GetObject();
418 if ((iResult=pDep->FollowDependency(id, pTgtList))>0) {
419 if (pTgtList) pTgtList->AddFirst(pDep);
434 void AliHLTTask::PrintDependencyTree(const char* id, int bFromConfiguration)
436 HLTLogKeyword("task dependencies");
439 if (bFromConfiguration) {
441 iResult=fpConfiguration->FollowDependency(id, &tgtList);
445 iResult=FollowDependency(id, &tgtList);
447 HLTMessage(" task \"%s\": dependency level %d ", GetName(), iResult);
448 TObjLink* lnk=tgtList.FirstLink();
450 char* pSpace = new char[iResult+1];
452 memset(pSpace, 32, iResult);
455 TObject* obj=lnk->GetObject();
456 HLTMessage(" %s^-- %s ", &pSpace[i--], obj->GetName());
466 int AliHLTTask::InsertBlockData(AliHLTComponent_BlockData* pBlock, AliHLTTask* pSource)
472 int AliHLTTask::SetDependency(AliHLTTask* pDep)
476 if (FindDependency(pDep->GetName())==NULL) {
477 fListDependencies.Add(pDep);
487 int AliHLTTask::CheckDependencies()
490 AliHLTConfiguration* pSrc=fpConfiguration->GetFirstSource();
492 if (FindDependency(pSrc->GetName())==NULL) {
493 //HLTDebug("dependency \"%s\" unresolved", pSrc->GetName());
496 pSrc=fpConfiguration->GetNextSource();
502 int AliHLTTask::Depends(AliHLTTask* pTask)
506 if (fpConfiguration) {
507 iResult=fpConfiguration->GetSource(pTask->GetName())!=NULL;
509 //HLTDebug("task \"%s\" depends on \"%s\"", GetName(), pTask->GetName());
511 //HLTDebug("task \"%s\" independend of \"%s\"", GetName(), pTask->GetName());
522 AliHLTTask* AliHLTTask::FindTarget(const char* id)
524 AliHLTTask* pTask=NULL;
526 pTask=(AliHLTTask*)fListTargets.FindObject(id);
531 int AliHLTTask::SetTarget(AliHLTTask* pTgt)
535 if (FindTarget(pTgt->GetName())==NULL) {
536 fListTargets.Add(pTgt);
546 int AliHLTTask::BuildBlockDataArray(AliHLTComponent_BlockData*& pTgt)
553 // int AliHLTTask::ProcessTask(...)
560 int AliHLTTask::ClearSourceBlocks()
566 void AliHLTTask::PrintStatus()
568 HLTLogKeyword("task properties");
570 HLTMessage(" component: %s (%p)", fpComponent->GetComponentID(), fpComponent);
572 HLTMessage(" no component set!");
574 if (fpConfiguration) {
575 AliHLTConfiguration* pSrc=fpConfiguration->GetFirstSource();
577 const char* pQualifier="unresolved";
578 if (FindDependency(pSrc->GetName()))
579 pQualifier="resolved";
580 HLTMessage(" source: %s (%s)", pSrc->GetName(), pQualifier);
581 pSrc=fpConfiguration->GetNextSource();
583 TObjLink* lnk = fListTargets.FirstLink();
585 TObject *obj = lnk->GetObject();
586 HLTMessage(" target: %s", obj->GetName());
590 HLTMessage(" task \"%s\" not initialized", GetName());
594 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
596 TList AliHLTConfigurationHandler::fListConfigurations;
597 TList AliHLTConfigurationHandler::fListDynamicConfigurations;
599 ClassImp(AliHLTConfigurationHandler)
601 AliHLTConfigurationHandler::AliHLTConfigurationHandler()
605 AliHLTConfigurationHandler::~AliHLTConfigurationHandler()
607 TObjLink* lnk=fListDynamicConfigurations.FirstLink();
609 TObject* obj=lnk->GetObject();
610 if (fListConfigurations.FindObject(obj->GetName())==NULL) {
611 HLTDebug("delete dynamic configuration \"%s\"", obj->GetName());
618 int AliHLTConfigurationHandler::RegisterConfiguration(AliHLTConfiguration* pConf)
622 if (FindConfiguration(pConf->GetName()) == NULL) {
623 fListConfigurations.Add(pConf);
624 //HLTDebug("configuration \"%s\" registered", pConf->GetName());
626 // mark all configurations with unresolved dependencies for re-evaluation
627 TObjLink* lnk=fListConfigurations.FirstLink();
629 AliHLTConfiguration* pSrc=(AliHLTConfiguration*)lnk->GetObject();
630 if (pSrc && pSrc!=pConf && pSrc->SourcesResolved()!=1) {
631 pSrc->InvalidateSources();
637 HLTWarning("configuration \"%s\" already registered", pConf->GetName());
645 int AliHLTConfigurationHandler::CreateConfiguration(const char* id, const char* component, const char* sources, const char* arguments)
648 AliHLTConfiguration* pConf= new AliHLTConfiguration(id, component, sources, arguments);
650 // the configuration will be registered automatically, if this failes the configuration
651 // is missing -> delete it
652 if (FindConfiguration(id)==NULL) {
657 fListDynamicConfigurations.Add(pConf);
660 HLTError("system error: object allocation failed");
666 void AliHLTConfigurationHandler::PrintConfigurations()
668 HLTLogKeyword("configuration listing");
669 HLTMessage("registered configurations:");
670 TObjLink *lnk = fListConfigurations.FirstLink();
672 TObject *obj = lnk->GetObject();
673 HLTMessage(" %s", obj->GetName());
678 int AliHLTConfigurationHandler::RemoveConfiguration(const char* id)
682 AliHLTConfiguration* pConf=NULL;
683 if ((pConf=FindConfiguration(id))!=NULL) {
684 iResult=RemoveConfiguration(pConf);
686 HLTWarning("can not find configuration \"%s\"", id);
695 int AliHLTConfigurationHandler::RemoveConfiguration(AliHLTConfiguration* pConf)
699 // remove the configuration from the list
700 fListConfigurations.Remove(pConf);
701 // remove cross links in the remaining configurations
702 TObjLink* lnk=fListConfigurations.FirstLink();
703 while (lnk && iResult>=0) {
704 AliHLTConfiguration* pRem=(AliHLTConfiguration*)lnk->GetObject();
706 pRem->InvalidateSource(pConf);
716 AliHLTConfiguration* AliHLTConfigurationHandler::FindConfiguration(const char* id)
718 AliHLTConfiguration* pConf=NULL;
720 pConf=(AliHLTConfiguration*)fListConfigurations.FindObject(id);