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(const char*** 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;
339 fBlockDataArraySize=0;
343 AliHLTTask::AliHLTTask(AliHLTConfiguration* fConf, AliHLTComponentHandler* pCH)
345 fpConfiguration=NULL;
347 fpBlockDataArray=NULL;
348 fBlockDataArraySize=0;
353 AliHLTTask::~AliHLTTask()
355 if (fpComponent) delete fpComponent;
357 if (fpBlockDataArray) delete[] fpBlockDataArray;
358 fpBlockDataArray=NULL;
361 int AliHLTTask::Init(AliHLTConfiguration* fConf, AliHLTComponentHandler* pCH)
365 fpConfiguration=fConf;
368 const char** argv=NULL;
369 if ((iResult=fConf->GetArguments(&argv))>=0) {
370 argc=iResult; // just to make it clear
371 iResult=pCH->CreateComponent(fConf->GetComponentID(), NULL, argc, argv, fpComponent);
374 HLTError("can not find component \"%s\"", fConf->GetComponentID());
384 const char *AliHLTTask::GetName() const
387 return fpConfiguration->GetName();
388 return TObject::GetName();
391 AliHLTConfiguration* AliHLTTask::GetConf() const
393 return fpConfiguration;
396 AliHLTComponent* AliHLTTask::GetComponent() const
401 AliHLTTask* AliHLTTask::FindDependency(const char* id)
403 AliHLTTask* pTask=NULL;
405 pTask=(AliHLTTask*)fListDependencies.FindObject(id);
410 int AliHLTTask::FollowDependency(const char* id, TList* pTgtList)
414 AliHLTTask* pDep=NULL;
415 if ((pDep=(AliHLTTask*)fListDependencies.FindObject(id))!=NULL) {
416 if (pTgtList) pTgtList->Add(pDep);
419 TObjLink* lnk=fListDependencies.FirstLink();
420 while (lnk && iResult==0) {
421 pDep=(AliHLTTask*)lnk->GetObject();
423 if ((iResult=pDep->FollowDependency(id, pTgtList))>0) {
424 if (pTgtList) pTgtList->AddFirst(pDep);
439 void AliHLTTask::PrintDependencyTree(const char* id, int bFromConfiguration)
441 HLTLogKeyword("task dependencies");
444 if (bFromConfiguration) {
446 iResult=fpConfiguration->FollowDependency(id, &tgtList);
450 iResult=FollowDependency(id, &tgtList);
452 HLTMessage(" task \"%s\": dependency level %d ", GetName(), iResult);
453 TObjLink* lnk=tgtList.FirstLink();
455 char* pSpace = new char[iResult+1];
457 memset(pSpace, 32, iResult);
460 TObject* obj=lnk->GetObject();
461 HLTMessage(" %s^-- %s ", &pSpace[i--], obj->GetName());
471 /* this function is most likely depricated
472 int AliHLTTask::InsertBlockData(AliHLTComponent_BlockData* pBlock, AliHLTTask* pSource)
479 int AliHLTTask::SetDependency(AliHLTTask* pDep)
483 if (FindDependency(pDep->GetName())==NULL) {
484 fListDependencies.Add(pDep);
494 int AliHLTTask::CheckDependencies()
497 AliHLTConfiguration* pSrc=fpConfiguration->GetFirstSource();
499 if (FindDependency(pSrc->GetName())==NULL) {
500 //HLTDebug("dependency \"%s\" unresolved", pSrc->GetName());
503 pSrc=fpConfiguration->GetNextSource();
509 int AliHLTTask::Depends(AliHLTTask* pTask)
513 if (fpConfiguration) {
514 iResult=fpConfiguration->GetSource(pTask->GetName())!=NULL;
516 //HLTDebug("task \"%s\" depends on \"%s\"", GetName(), pTask->GetName());
518 //HLTDebug("task \"%s\" independend of \"%s\"", GetName(), pTask->GetName());
529 AliHLTTask* AliHLTTask::FindTarget(const char* id)
531 AliHLTTask* pTask=NULL;
533 pTask=(AliHLTTask*)fListTargets.FindObject(id);
538 int AliHLTTask::SetTarget(AliHLTTask* pTgt)
542 if (FindTarget(pTgt->GetName())==NULL) {
543 fListTargets.Add(pTgt);
553 /* this function is most likely depricated
554 int AliHLTTask::BuildBlockDataArray(AliHLTComponent_BlockData*& pBlockData)
561 int AliHLTTask::StartRun()
564 int iNofInputDataBlocks=0;
565 AliHLTComponent* pComponent=GetComponent();
567 // determine the number of input data blocks provided from the source tasks
568 TObjLink* lnk=fListDependencies.FirstLink();
569 while (lnk && iResult>=0) {
570 AliHLTTask* pSrcTask=(AliHLTTask*)lnk->GetObject();
572 if ((iResult=pSrcTask->GetNofMatchingDataTypes(this))>0) {
573 iNofInputDataBlocks+=iResult;
574 } else if (iResult==0) {
575 HLTWarning("source task %s (%p) does not provide any matching data type for task %s (%p)", pSrcTask->GetName(), pSrcTask, GetName(), this);
577 HLTError("task %s (%p): error getting matching data types for source task %s (%p)", GetName(), this, pSrcTask->GetName(), pSrcTask);
584 if (fpBlockDataArray) {
585 HLTWarning("block data array for task %s (%p) was not cleaned", GetName(), this);
586 delete [] fpBlockDataArray;
587 fpBlockDataArray=NULL;
588 fBlockDataArraySize=0;
592 //iResult=Init( AliHLTComponentEnvironment* environ, void* environ_param, int argc, const char** argv );
594 // allocate internal task varables for bookkeeping aso.
595 fpBlockDataArray=new AliHLTComponent_BlockData[iNofInputDataBlocks];
596 if (fpBlockDataArray) {
597 fBlockDataArraySize=iNofInputDataBlocks;
599 HLTError("memory allocation failed");
604 HLTError("task %s (%p) does not have a component", GetName(), this);
610 int AliHLTTask::EndRun()
616 int AliHLTTask::ProcessTask()
619 if (fpComponent && fpBlockDataArray) {
620 int iSourceDataBlock=0;
621 int iInputDataVolume=0;
623 int iNofInputDataBlocks=0;
624 /* TODO: the assumption of only one output data type per component is the current constraint
625 * later it should be checked how many output blocks of the source component match the input
626 * data types of the consumer component (GetNofMatchingDataBlocks). If one assumes that a
627 * certain output block is always been produced, the initialization could be done in the
628 * StartRun. Otherwise the fpBlockDataArray has to be adapted each time.
630 iNofInputDataBlocks=fListDependencies.GetSize(); // one block per source
631 // is not been used since the allocation was done in StartRun, but check the size
632 if (iNofInputDataBlocks>fBlockDataArraySize) {
633 HLTError("block data array too small");
636 AliHLTTask* pSrcTask=NULL;
637 TList subscribedTaskList;
638 TObjLink* lnk=fListDependencies.FirstLink();
640 // subscribe to all source tasks
641 while (lnk && iResult>=0) {
642 pSrcTask=(AliHLTTask*)lnk->GetObject();
644 if (pSrcTask->GetNofMatchingDataBlocks(this)<fBlockDataArraySize-iSourceDataBlock) {
645 if ((iResult=pSrcTask->Subscribe(this, &fpBlockDataArray[iSourceDataBlock],fBlockDataArraySize-iSourceDataBlock))>0) {
646 for (int i=0; i<iResult; i++) {
647 iInputDataVolume+=fpBlockDataArray[i+iSourceDataBlock].fSize;
648 // put the source task as many times into the list as it provides data blocks
649 // makes the bookkeeping for the data release easier
650 subscribedTaskList.Add(pSrcTask);
652 iSourceDataBlock+=iResult;
653 HLTDebug("Task %s (%p) successfully subscribed to %d data blocks of task %s (%p)", GetName(), this, iResult, pSrcTask->GetName(), pSrcTask);
656 HLTError("Task %s (%p): subscription to task %s (%p) failed with error %d", GetName(), this, pSrcTask->GetName(), pSrcTask, iResult);
660 HLTFatal("Task %s (%p): too little space in data block array for subscription to task %s (%p)", GetName(), this, pSrcTask->GetName(), pSrcTask);
664 HLTFatal("fatal internal error in ROOT list handling");
672 long unsigned int iConstBase=0;
673 double fInputMultiplier=0;
674 fpComponent->GetOutputDataSize(iConstBase, fInputMultiplier);
675 int iOutputDataSize=int(fInputMultiplier*iInputDataVolume) + iConstBase;
676 AliHLTUInt8_t* pTgtBuffer=fpDataBuffer->GetTargetBuffer(iOutputDataSize);
677 AliHLTComponent_EventData evtData;
678 AliHLTComponent_TriggerData trigData;
679 AliHLTUInt32_t size=iOutputDataSize;
680 AliHLTUInt32_t outputBlockCnt=0;
681 AliHLTComponent_BlockData* outputBlocks=NULL;
682 AliHLTComponent_EventDoneData* edd;
683 if (pTgtBuffer!=NULL || iOutputDataSize==0) {
684 iResult=fpComponent->ProcessEvent(evtData, fpBlockDataArray, trigData, pTgtBuffer, size, outputBlockCnt, outputBlocks, edd);
689 // now release all buffers which we have subscribed to
691 lnk=subscribedTaskList.FirstLink();
693 pSrcTask=(AliHLTTask*)lnk->GetObject();
696 if ((iTempRes=pSrcTask->Release(&fpBlockDataArray[iSourceDataBlock], this))>=0) {
697 HLTDebug("Task %s (%p) successfully released task %s (%p)", GetName(), this, pSrcTask->GetName(), pSrcTask);
699 HLTError("Task %s (%p): realease of task %s (%p) failed with error %d", GetName(), this, pSrcTask->GetName(), pSrcTask, iTempRes);
702 HLTFatal("fatal internal error in ROOT list handling");
705 subscribedTaskList.Remove(lnk);
706 lnk=subscribedTaskList.FirstLink();
709 if (subscribedTaskList.GetSize()>0) {
710 HLTError("task %s (%p): could not release all data buffers", GetName(), this);
713 HLTError("internal failure: task not initialized");
719 int AliHLTTask::GetNofMatchingDataBlocks(const AliHLTTask* pConsumerTask)
724 iResult=fpDataBuffer->FindMatchingDataBlocks(pConsumerTask->GetComponent(), NULL);
726 HLTFatal("internal data buffer missing");
735 int AliHLTTask::GetNofMatchingDataTypes(const AliHLTTask* pConsumerTask)
739 AliHLTComponent* pComponent=GetComponent();
744 iResult=pComponent->FindMatchingDataTypes(pConsumerTask->GetComponent(), NULL);
746 HLTFatal("task initialization failed");
755 int AliHLTTask::Subscribe(const AliHLTTask* pConsumerTask, AliHLTComponent_BlockData* pBlockDesc, int iArraySize)
760 iResult=fpDataBuffer->Subscribe(pConsumerTask->GetComponent(), pBlockDesc, iArraySize);
762 HLTFatal("internal data buffer missing");
771 int AliHLTTask::Release(AliHLTComponent_BlockData* pBlockDesc, const AliHLTTask* pConsumerTask)
774 if (pConsumerTask && pBlockDesc) {
776 iResult=fpDataBuffer->Release(pBlockDesc, pConsumerTask->GetComponent());
778 HLTFatal("internal data buffer missing");
787 /* this function is most likely depricated
788 int AliHLTTask::ClearSourceBlocks()
795 void AliHLTTask::PrintStatus()
797 HLTLogKeyword("task properties");
799 HLTMessage(" component: %s (%p)", fpComponent->GetComponentID(), fpComponent);
801 HLTMessage(" no component set!");
803 if (fpConfiguration) {
804 AliHLTConfiguration* pSrc=fpConfiguration->GetFirstSource();
806 const char* pQualifier="unresolved";
807 if (FindDependency(pSrc->GetName()))
808 pQualifier="resolved";
809 HLTMessage(" source: %s (%s)", pSrc->GetName(), pQualifier);
810 pSrc=fpConfiguration->GetNextSource();
812 TObjLink* lnk = fListTargets.FirstLink();
814 TObject *obj = lnk->GetObject();
815 HLTMessage(" target: %s", obj->GetName());
819 HLTMessage(" task \"%s\" not initialized", GetName());
823 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
825 TList AliHLTConfigurationHandler::fListConfigurations;
826 TList AliHLTConfigurationHandler::fListDynamicConfigurations;
828 ClassImp(AliHLTConfigurationHandler)
830 AliHLTConfigurationHandler::AliHLTConfigurationHandler()
834 AliHLTConfigurationHandler::~AliHLTConfigurationHandler()
836 TObjLink* lnk=fListDynamicConfigurations.FirstLink();
838 TObject* obj=lnk->GetObject();
839 if (fListConfigurations.FindObject(obj->GetName())==NULL) {
840 HLTDebug("delete dynamic configuration \"%s\"", obj->GetName());
847 int AliHLTConfigurationHandler::RegisterConfiguration(AliHLTConfiguration* pConf)
851 if (FindConfiguration(pConf->GetName()) == NULL) {
852 fListConfigurations.Add(pConf);
853 //HLTDebug("configuration \"%s\" registered", pConf->GetName());
855 // mark all configurations with unresolved dependencies for re-evaluation
856 TObjLink* lnk=fListConfigurations.FirstLink();
858 AliHLTConfiguration* pSrc=(AliHLTConfiguration*)lnk->GetObject();
859 if (pSrc && pSrc!=pConf && pSrc->SourcesResolved()!=1) {
860 pSrc->InvalidateSources();
866 HLTWarning("configuration \"%s\" already registered", pConf->GetName());
874 int AliHLTConfigurationHandler::CreateConfiguration(const char* id, const char* component, const char* sources, const char* arguments)
877 AliHLTConfiguration* pConf= new AliHLTConfiguration(id, component, sources, arguments);
879 // the configuration will be registered automatically, if this failes the configuration
880 // is missing -> delete it
881 if (FindConfiguration(id)==NULL) {
886 fListDynamicConfigurations.Add(pConf);
889 HLTError("system error: object allocation failed");
895 void AliHLTConfigurationHandler::PrintConfigurations()
897 HLTLogKeyword("configuration listing");
898 HLTMessage("registered configurations:");
899 TObjLink *lnk = fListConfigurations.FirstLink();
901 TObject *obj = lnk->GetObject();
902 HLTMessage(" %s", obj->GetName());
907 int AliHLTConfigurationHandler::RemoveConfiguration(const char* id)
911 AliHLTConfiguration* pConf=NULL;
912 if ((pConf=FindConfiguration(id))!=NULL) {
913 iResult=RemoveConfiguration(pConf);
915 HLTWarning("can not find configuration \"%s\"", id);
924 int AliHLTConfigurationHandler::RemoveConfiguration(AliHLTConfiguration* pConf)
928 // remove the configuration from the list
929 fListConfigurations.Remove(pConf);
930 // remove cross links in the remaining configurations
931 TObjLink* lnk=fListConfigurations.FirstLink();
932 while (lnk && iResult>=0) {
933 AliHLTConfiguration* pRem=(AliHLTConfiguration*)lnk->GetObject();
935 pRem->InvalidateSource(pConf);
945 AliHLTConfiguration* AliHLTConfigurationHandler::FindConfiguration(const char* id)
947 AliHLTConfiguration* pConf=NULL;
949 pConf=(AliHLTConfiguration*)fListConfigurations.FindObject(id);