2 // splitted from AliHLTConfiguration.cxx,v 1.25 2007/10/12 13:24:47
3 /**************************************************************************
4 * This file is property of and copyright by the ALICE HLT Project *
5 * ALICE Experiment at CERN, All rights reserved. *
7 * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 * for The ALICE HLT Project. *
10 * Permission to use, copy, modify and distribute this software and its *
11 * documentation strictly for non-commercial purposes is hereby granted *
12 * without fee, provided that the above copyright notice appears in all *
13 * copies and that both the copyright notice and this permission notice *
14 * appear in the supporting documentation. The authors make no claims *
15 * about the suitability of this software for any purpose. It is *
16 * provided "as is" without express or implied warranty. *
17 **************************************************************************/
19 /** @file AliHLTTask.cxx
20 @author Matthias Richter
22 @brief Implementation of HLT tasks.
25 // see header file for class documentation
27 // refer to README to build package
29 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
39 #include "AliHLTTask.h"
40 #include "AliHLTConfiguration.h"
41 #include "AliHLTComponent.h"
42 #include "AliHLTComponentHandler.h"
45 /** ROOT macro for the implementation of ROOT specific class methods */
48 AliHLTTask::AliHLTTask()
50 fpConfiguration(NULL),
57 // see header file for class documentation
59 // refer to README to build package
61 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
64 AliHLTTask::AliHLTTask(AliHLTConfiguration* pConf)
66 fpConfiguration(pConf),
73 // see header file for function documentation
76 AliHLTTask::~AliHLTTask()
78 // see header file for function documentation
79 TObjLink* lnk=fListDependencies.FirstLink();
82 AliHLTTask* pTask=(AliHLTTask*)lnk->GetObject();
83 pTask->UnsetTarget(this);
86 lnk=fListTargets.FirstLink();
89 AliHLTTask* pTask=(AliHLTTask*)lnk->GetObject();
90 pTask->UnsetDependency(this);
94 if (fpComponent) delete fpComponent;
98 int AliHLTTask::Init(AliHLTConfiguration* pConf, AliHLTComponentHandler* pCH)
100 // see header file for function documentation
102 if (fpConfiguration!=NULL && pConf!=NULL && fpConfiguration!=pConf) {
103 HLTWarning("overriding existing reference to configuration object %p (%s) by %p",
104 fpConfiguration, GetName(), pConf);
106 if (pConf!=NULL) fpConfiguration=pConf;
107 if (fpConfiguration) {
110 const char** argv=NULL;
111 if ((iResult=fpConfiguration->GetArguments(&argv))>=0) {
112 argc=iResult; // just to make it clear
113 // TODO: we have to think about the optional environment parameter,
114 // currently just set to NULL.
115 iResult=pCH->CreateComponent(fpConfiguration->GetComponentID(), NULL, argc, argv, fpComponent);
116 if (fpComponent && iResult>=0) {
117 //HLTDebug("component %s (%p) created", fpComponent->GetComponentID(), fpComponent);
119 //HLTError("can not find component \"%s\" (%d)", fpConfiguration->GetComponentID(), iResult);
122 HLTError("can not get argument list for configuration %s (%s)", fpConfiguration->GetName(), fpConfiguration->GetComponentID());
126 HLTError("component handler instance needed for task initialization");
130 HLTError("configuration object instance needed for task initialization");
134 iResult=CustomInit(pCH);
139 int AliHLTTask::Deinit()
141 // see header file for function documentation
144 AliHLTComponent* pComponent=GetComponent();
147 //HLTDebug("delete component %s (%p)", pComponent->GetComponentID(), pComponent);
148 pComponent->Deinit();
151 HLTWarning("task %s (%p) doesn't seem to be in initialized", GetName(), this);
156 const char *AliHLTTask::GetName() const
158 // see header file for function documentation
160 return fpConfiguration->GetName();
161 return TObject::GetName();
164 AliHLTConfiguration* AliHLTTask::GetConf() const
166 // see header file for function documentation
167 return fpConfiguration;
170 AliHLTComponent* AliHLTTask::GetComponent() const
172 // see header file for function documentation
176 AliHLTTask* AliHLTTask::FindDependency(const char* id)
178 // see header file for function documentation
179 AliHLTTask* pTask=NULL;
181 pTask=(AliHLTTask*)fListDependencies.FindObject(id);
186 int AliHLTTask::FollowDependency(const char* id, TList* pTgtList)
188 // see header file for function documentation
191 AliHLTTask* pDep=NULL;
192 if ((pDep=(AliHLTTask*)fListDependencies.FindObject(id))!=NULL) {
193 if (pTgtList) pTgtList->Add(pDep);
196 TObjLink* lnk=fListDependencies.FirstLink();
197 while (lnk && iResult==0) {
198 pDep=(AliHLTTask*)lnk->GetObject();
200 if ((iResult=pDep->FollowDependency(id, pTgtList))>0) {
201 if (pTgtList) pTgtList->AddFirst(pDep);
216 void AliHLTTask::PrintDependencyTree(const char* id, int bFromConfiguration)
218 // see header file for function documentation
219 HLTLogKeyword("task dependencies");
222 if (bFromConfiguration) {
224 iResult=fpConfiguration->FollowDependency(id, &tgtList);
228 iResult=FollowDependency(id, &tgtList);
230 HLTMessage(" task \"%s\": dependency level %d ", GetName(), iResult);
231 TObjLink* lnk=tgtList.FirstLink();
233 char* pSpace = new char[iResult+1];
235 memset(pSpace, 32, iResult);
238 TObject* obj=lnk->GetObject();
239 HLTMessage(" %s^-- %s ", &pSpace[i--], obj->GetName());
249 int AliHLTTask::SetDependency(AliHLTTask* pDep)
251 // see header file for function documentation
254 if (FindDependency(pDep->GetName())==NULL) {
255 fListDependencies.Add(pDep);
265 int AliHLTTask::UnsetDependency(AliHLTTask* pDep)
267 // see header file for function documentation
268 fListDependencies.Remove(pDep);
269 if (fpConfiguration) {
270 fpConfiguration->InvalidateSources();
275 int AliHLTTask::CheckDependencies()
277 // see header file for function documentation
279 AliHLTConfiguration* pSrc=fpConfiguration->GetFirstSource();
281 if (FindDependency(pSrc->GetName())==NULL) {
282 //HLTDebug("dependency \"%s\" unresolved", pSrc->GetName());
285 pSrc=fpConfiguration->GetNextSource();
291 int AliHLTTask::Depends(AliHLTTask* pTask)
293 // see header file for function documentation
296 if (fpConfiguration) {
297 iResult=fpConfiguration->GetSource(pTask->GetName())!=NULL;
299 //HLTDebug("task \"%s\" depends on \"%s\"", GetName(), pTask->GetName());
301 //HLTDebug("task \"%s\" independend of \"%s\"", GetName(), pTask->GetName());
312 AliHLTTask* AliHLTTask::FindTarget(const char* id)
314 // see header file for function documentation
315 AliHLTTask* pTask=NULL;
317 pTask=(AliHLTTask*)fListTargets.FindObject(id);
322 int AliHLTTask::SetTarget(AliHLTTask* pTgt)
324 // see header file for function documentation
327 if (FindTarget(pTgt->GetName())==NULL) {
328 fListTargets.Add(pTgt);
338 int AliHLTTask::UnsetTarget(AliHLTTask* pTarget)
340 // see header file for function documentation
341 fListTargets.Remove(pTarget);
345 int AliHLTTask::StartRun()
347 // see header file for function documentation
349 int iNofInputDataBlocks=0;
350 AliHLTComponent* pComponent=GetComponent();
352 // determine the number of input data blocks provided from the source tasks
353 { // set scope for lnk as a local variable
354 TObjLink* lnk=fListDependencies.FirstLink();
355 while (lnk && iResult>=0) {
356 AliHLTTask* pSrcTask=(AliHLTTask*)lnk->GetObject();
358 if ((iResult=pSrcTask->GetNofMatchingDataTypes(this))>0) {
359 iNofInputDataBlocks+=iResult;
360 } else if (iResult==0) {
361 HLTWarning("source task %s (%p) does not provide any matching data type for task %s (%p)", pSrcTask->GetName(), pSrcTask, GetName(), this);
363 HLTError("task %s (%p): error getting matching data types for source task %s (%p)", GetName(), this, pSrcTask->GetName(), pSrcTask);
371 if (fBlockDataArray.size()>0) {
372 HLTWarning("block data array for task %s (%p) was not cleaned", GetName(), this);
373 fBlockDataArray.clear();
377 // the initialization of the component is done by the ComponentHandler after creation
379 //iResult=Init( AliHLTComponentEnvironment* environ, void* environ_param, int argc, const char** argv );
381 // allocate the data buffer, which controls the output buffer and subscriptions
383 fpDataBuffer=new AliHLTDataBuffer;
384 if (fpDataBuffer!=NULL) {
385 fpDataBuffer->SetLocalLoggingLevel(GetLocalLoggingLevel());
386 HLTDebug("created data buffer %p for task %s (%p)", fpDataBuffer, GetName(), this);
387 TObjLink* lnk=fListTargets.FirstLink();
388 while (lnk && iResult>=0) {
389 AliHLTTask* pTgtTask=(AliHLTTask*)lnk->GetObject();
391 if ((iResult=fpDataBuffer->SetConsumer(pTgtTask->GetComponent()))>=0) {
400 HLTFatal("can not create data buffer object, memory allocation failed");
406 // send the SOR event
410 HLTError("task %s (%p) does not have a component", GetName(), this);
416 int AliHLTTask::EndRun()
418 // see header file for function documentation
420 if (fBlockDataArray.size()>0) {
421 fBlockDataArray.clear();
424 AliHLTDataBuffer* pBuffer=fpDataBuffer;
431 int AliHLTTask::ProcessTask(Int_t eventNo)
433 // see header file for function documentation
435 AliHLTComponent* pComponent=GetComponent();
436 if (pComponent && fpDataBuffer) {
437 HLTDebug("Processing task %s (%p) fpDataBuffer %p", GetName(), this, fpDataBuffer);
438 fpDataBuffer->Reset();
439 int iSourceDataBlock=0;
440 int iInputDataVolume=0;
442 AliHLTTask* pSrcTask=NULL;
443 AliHLTTaskPList subscribedTaskList;
444 TObjLink* lnk=fListDependencies.FirstLink();
446 // instances of SOR and EOR events to be kept
450 // subscribe to all source tasks
451 fBlockDataArray.clear();
452 while (lnk && iResult>=0) {
453 pSrcTask=(AliHLTTask*)lnk->GetObject();
455 int iMatchingDB=pSrcTask->GetNofMatchingDataBlocks(this);
457 HLTError("task %s (%p): error getting no of matching data blocks from task %s (%p), error %d", GetName(), this, pSrcTask->GetName(), pSrcTask, iMatchingDB);
460 } else if (iMatchingDB==0) {
461 HLTDebug("source task %s (%p) does not provide any matching data type for task %s (%p)", pSrcTask->GetName(), pSrcTask, GetName(), this);
463 if ((iResult=pSrcTask->Subscribe(this, fBlockDataArray))>=0) {
465 AliHLTComponentBlockDataList::iterator block=fBlockDataArray.begin();
466 for (int i=0; block!=fBlockDataArray.end(); i++) {
468 bRemove|=(*block).fDataType==kAliHLTDataTypeSOR && !(iSOR<0 && (iSOR=i)>=0);
469 bRemove|=(*block).fDataType==kAliHLTDataTypeEOR && !(iEOR<0 && (iEOR=i)>=0);
470 //HLTInfo("block %d, iSOR=%d iEOR=%d remove=%d", i, iSOR, iEOR, bRemove);
471 if (i<iSourceDataBlock) {
473 } else if (bRemove) {
474 HLTDebug("remove duplicated event %s (%d)", AliHLTComponent::DataType2Text((*block).fDataType).c_str(), i);
475 pSrcTask->Release(&(*block), this);
476 block=fBlockDataArray.erase(block);
479 iInputDataVolume+=(*block).fSize;
480 // put the source task as many times into the list as it provides data blocks
481 // makes the bookkeeping for the data release easier
482 subscribedTaskList.push_back(pSrcTask);
486 HLTDebug("Task %s (%p) successfully subscribed to %d data block(s) of task %s (%p)", GetName(), this, iResult, pSrcTask->GetName(), pSrcTask);
487 iSourceDataBlock=fBlockDataArray.size();
490 HLTError("Task %s (%p): subscription to task %s (%p) failed with error %d", GetName(), this, pSrcTask->GetName(), pSrcTask, iResult);
494 HLTFatal("fatal internal error in ROOT list handling");
501 int iNofTrial=0; // repeat processing if component returns -ENOSPC
502 AliHLTUInt32_t size=0;
505 long unsigned int iOutputDataSize=0;
506 AliHLTConfiguration* pConf=GetConf();
508 // check if there was a buffer size specified, query output size
509 // estimator from component otherwize
510 if (pConf && pConf->GetOutputBufferSize()>=0) {
511 iOutputDataSize=pConf->GetOutputBufferSize();
513 long unsigned int iConstBase=0;
514 double fInputMultiplier=0;
515 if (pComponent->GetComponentType()!=AliHLTComponent::kSink)
516 pComponent->GetOutputDataSize(iConstBase, fInputMultiplier);
517 if (fInputMultiplier<0) {
518 HLTWarning("ignoring negative input multiplier");
521 iOutputDataSize=int(fInputMultiplier*iInputDataVolume) + iConstBase;
522 //HLTDebug("task %s: reqired output size %d", GetName(), iOutputDataSize);
525 // dont process again if the buffer size is the same
526 if (size==iOutputDataSize) break;
527 HLTInfo("processing task %s again with buffer size %d", GetName(), iOutputDataSize);
529 AliHLTUInt8_t* pTgtBuffer=NULL;
530 if (iOutputDataSize>0) pTgtBuffer=fpDataBuffer->GetTargetBuffer(iOutputDataSize);
531 //HLTDebug("provided raw buffer %p", pTgtBuffer);
532 AliHLTComponentEventData evtData;
533 AliHLTComponent::FillEventData(evtData);
534 evtData.fEventID=(AliHLTEventID_t)eventNo;
535 evtData.fBlockCnt=iSourceDataBlock;
536 AliHLTComponentTriggerData trigData;
537 trigData.fStructSize=sizeof(trigData);
538 trigData.fDataSize=0;
540 size=iOutputDataSize;
541 AliHLTUInt32_t outputBlockCnt=0;
542 AliHLTComponentBlockData* outputBlocks=NULL;
543 AliHLTComponentEventDoneData* edd=NULL;
544 if (pTgtBuffer!=NULL || iOutputDataSize==0) {
545 iResult=pComponent->ProcessEvent(evtData, &fBlockDataArray[0], trigData, pTgtBuffer, size, outputBlockCnt, outputBlocks, edd);
546 HLTDebug("task %s: component %s ProcessEvent finnished (%d): size=%d blocks=%d", GetName(), pComponent->GetComponentID(), iResult, size, outputBlockCnt);
547 if (iResult>=0 && outputBlocks) {
548 if (fListTargets.First()!=NULL) {
549 AliHLTComponentBlockDataList segments;
550 for (AliHLTUInt32_t oblock=0; oblock<outputBlockCnt; oblock++) {
551 AliHLTUInt32_t iblock=0;
552 for (; iblock<evtData.fBlockCnt; iblock++) {
553 if (fBlockDataArray[iblock].fPtr==outputBlocks[oblock].fPtr) {
554 assert(subscribedTaskList[iblock]!=NULL);
555 if (subscribedTaskList[iblock]==NULL) continue;
556 HLTDebug("forward segment %d (source task %s %p) to data buffer %p", iblock, pSrcTask->GetName(), pSrcTask, fpDataBuffer);
557 fpDataBuffer->Forward(subscribedTaskList[iblock], &fBlockDataArray[iblock]);
558 subscribedTaskList[iblock]=NULL; // not to be released in the loop further down
562 if (iblock==evtData.fBlockCnt) segments.push_back(outputBlocks[oblock]);
564 if (pTgtBuffer && segments.size()>0) {
565 iResult=fpDataBuffer->SetSegments(pTgtBuffer, &segments[0], segments.size());
568 // no forwarding, actually we dont even need to keep the data, this is a
569 // dead end (fListTargets empty)
570 //iResult=fpDataBuffer->SetSegments(pTgtBuffer, outputBlocks, outputBlockCnt);
572 delete [] outputBlocks; outputBlocks=NULL; outputBlockCnt=0;
574 fpDataBuffer->Reset();
576 if (fListTargets.First()!=NULL) {
577 if (iSOR>=0 && subscribedTaskList[iSOR]!=NULL) {
578 HLTDebug("forward SOR event segment %d (source task %s %p) to data buffer %p", iSOR, pSrcTask->GetName(), pSrcTask, fpDataBuffer);
579 fpDataBuffer->Forward(subscribedTaskList[iSOR], &fBlockDataArray[iSOR]);
580 subscribedTaskList[iSOR]=NULL; // not to be released in the loop further down
582 if (iEOR>=0 && subscribedTaskList[iEOR]!=NULL) {
583 HLTDebug("forward EOR event (%s) segment %d (source task %s %p) to data buffer %p", AliHLTComponent::DataType2Text(fBlockDataArray[iEOR].fDataType).c_str(), iEOR, pSrcTask->GetName(), pSrcTask, fpDataBuffer);
584 fpDataBuffer->Forward(subscribedTaskList[iEOR], &fBlockDataArray[iEOR]);
585 subscribedTaskList[iEOR]=NULL; // not to be released in the loop further down
589 HLTError("task %s: no target buffer available", GetName());
592 } while (iResult==-ENOSPC && iNofTrial++<1);
595 // now release all buffers which we have subscribed to
597 AliHLTTaskPList::iterator element;
598 while ((element=subscribedTaskList.begin())!=subscribedTaskList.end()) {
602 if ((iTempRes=pSrcTask->Release(&fBlockDataArray[iSourceDataBlock], this))>=0) {
603 HLTDebug("Task %s (%p) successfully released segment of task %s (%p)", GetName(), this, pSrcTask->GetName(), pSrcTask);
605 HLTError("Task %s (%p): realease of task %s (%p) failed with error %d", GetName(), this, pSrcTask->GetName(), pSrcTask, iTempRes);
608 subscribedTaskList.erase(element);
611 if (subscribedTaskList.size()>0) {
612 HLTError("task %s (%p): could not release all data buffers", GetName(), this);
615 HLTError("task %s (%p): internal failure (not initialized component %p, data buffer %p)", GetName(), this, fpComponent, fpDataBuffer);
621 int AliHLTTask::GetNofMatchingDataBlocks(const AliHLTTask* pConsumerTask) const
623 // see header file for function documentation
627 iResult=fpDataBuffer->FindMatchingDataBlocks(pConsumerTask->GetComponent(), NULL);
629 HLTFatal("internal data buffer missing");
638 int AliHLTTask::GetNofMatchingDataTypes(const AliHLTTask* pConsumerTask) const
640 // see header file for function documentation
643 AliHLTComponent* pComponent=GetComponent();
646 HLTError("component not initialized");
650 iResult=pComponent->FindMatchingDataTypes(pConsumerTask->GetComponent(), NULL);
652 HLTFatal("task initialization failed");
661 int AliHLTTask::Subscribe(const AliHLTTask* pConsumerTask, AliHLTComponentBlockDataList& blockDescList)
663 // see header file for function documentation
667 iResult=fpDataBuffer->Subscribe(pConsumerTask->GetComponent(), blockDescList);
669 HLTFatal("internal data buffer missing");
678 int AliHLTTask::Release(AliHLTComponentBlockData* pBlockDesc, const AliHLTTask* pConsumerTask)
680 // see header file for function documentation
682 if (pConsumerTask && pBlockDesc) {
684 iResult=fpDataBuffer->Release(pBlockDesc, pConsumerTask->GetComponent(), this);
686 HLTFatal("internal data buffer missing");
695 void AliHLTTask::PrintStatus()
697 // see header file for function documentation
698 HLTLogKeyword("task properties");
699 AliHLTComponent* pComponent=GetComponent();
701 HLTMessage(" component: %s (%p)", pComponent->GetComponentID(), pComponent);
703 HLTMessage(" no component set!");
705 if (fpConfiguration) {
706 AliHLTConfiguration* pSrc=fpConfiguration->GetFirstSource();
708 const char* pQualifier="unresolved";
709 if (FindDependency(pSrc->GetName()))
710 pQualifier="resolved";
711 HLTMessage(" source: %s (%s)", pSrc->GetName(), pQualifier);
712 pSrc=fpConfiguration->GetNextSource();
714 TObjLink* lnk = fListTargets.FirstLink();
716 TObject *obj = lnk->GetObject();
717 HLTMessage(" target: %s", obj->GetName());
721 HLTMessage(" task \"%s\" not initialized", GetName());
725 int AliHLTTask::CustomInit(AliHLTComponentHandler* /*pCH*/)
727 // default implementation nothing to do
731 int AliHLTTask::CustomCleanup()
733 // default implementation nothing to do