]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/util/AliHLTMonitoringRelay.cxx
NULL pointer protections
[u/mrichter/AliRoot.git] / HLT / BASE / util / AliHLTMonitoringRelay.cxx
1 // $Id$
2
3 //**************************************************************************
4 //* This file is property of and copyright by the ALICE HLT Project        * 
5 //* ALICE Experiment at CERN, All rights reserved.                         *
6 //*                                                                        *
7 //* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
8 //*                  for The ALICE HLT Project.                            *
9 //*                                                                        *
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 //**************************************************************************
18
19 /// @file   AliHLTMonitoringRelay.cxx
20 /// @author Matthias Richter
21 /// @date   2009-11-11
22 /// @brief  Relay components for monitoring objects.
23 ///
24
25 #include <cstdlib>
26 #include <cassert>
27 #include "AliHLTMonitoringRelay.h"
28 #include "AliHLTMessage.h"
29 #include "TArrayC.h"
30 #include "TObject.h"
31 #include "TDatime.h"
32
33 /** ROOT macro for the implementation of ROOT specific class methods */
34 ClassImp(AliHLTMonitoringRelay)
35
36 AliHLTMonitoringRelay::AliHLTMonitoringRelay()
37   : AliHLTProcessor()
38   , fItems()
39   , fOutputSize()
40   , fFlags(0)
41 {
42   // see header file for class documentation
43   // or
44   // refer to README to build package
45   // or
46   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
47 }
48
49 AliHLTMonitoringRelay::~AliHLTMonitoringRelay()
50 {
51   // see header file for class documentation
52 }
53
54 void AliHLTMonitoringRelay::GetInputDataTypes(AliHLTComponentDataTypeList& list)
55 {
56   // see header file for class documentation
57   list.clear();
58   list.push_back(kAliHLTAnyDataType);
59 }
60
61 AliHLTComponentDataType AliHLTMonitoringRelay::GetOutputDataType()
62 {
63   // see header file for class documentation
64   return kAliHLTAnyDataType;
65 }
66
67 void AliHLTMonitoringRelay::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
68 {
69   // see header file for class documentation
70   constBase=fOutputSize;
71   inputMultiplier=1.0;
72 }
73
74 int AliHLTMonitoringRelay::DoInit( int argc, const char** argv )
75 {
76   // see header file for class documentation
77   int iResult=0;
78   fOutputSize=0;
79
80   iResult=ConfigureFromArgumentString(argc, argv);
81
82   return iResult;
83 }
84
85 int AliHLTMonitoringRelay::ScanConfigurationArgument(int argc, const char** argv)
86 {
87   // see header file for class documentation
88   if (argc<=0) return 0;
89   int i=0;
90   TString argument=argv[i];
91
92   // -verbose
93   if (argument.CompareTo("-verbose")==0) {
94     // 
95     return 1;
96   } else if (argument.CompareTo("-check-object")==0) { // check the objects in the blocks
97     SetFlag(kCheckObject);
98     return 1;
99   }
100
101   return 0;
102 }
103
104 int AliHLTMonitoringRelay::DoDeinit()
105 {
106   // see header file for class documentation
107   int iResult=0;
108   AliHLTMonitoringItemPList::iterator element=fItems.begin();
109   while (element!=fItems.end()) {
110     AliHLTMonitoringItem* pItem=*element;
111     element=fItems.erase(element);
112     if (pItem) {
113       delete pItem;
114     }
115   }
116   return iResult;
117 }
118
119 int AliHLTMonitoringRelay::DoEvent(const AliHLTComponentEventData& /*evtData*/,
120                                    AliHLTComponentTriggerData& /*trigData*/)
121 {
122   // see header file for class documentation
123   int iResult=0;
124   for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock();
125        pBlock!=NULL; 
126        pBlock=GetNextInputBlock()) {
127     // ignore private blocks
128     if (pBlock->fDataType==(kAliHLTAnyDataType|kAliHLTDataOriginPrivate)) continue;
129     TObject* pObject=NULL;
130     if (CheckFlag(kCheckObject)) pObject=AliHLTMessage::Extract(pBlock->fPtr, pBlock->fSize);
131     
132     AliHLTMonitoringItem* pItem=FindItem(pBlock, pObject);
133     if (pItem) {
134       HLTInfo("found block %s 0x%0lx %s %s", DataType2Text(pItem->GetDataType()).c_str(), pItem->GetSpecification(), pObject?pObject->GetName():"", pObject?pObject->GetName():"");
135       if (pItem->GetSize()<pBlock->fSize) {
136         // update with the new maximum
137         assert(fOutputSize>=pItem->GetSize());
138         fOutputSize-=pItem->GetSize();
139         fOutputSize+=pBlock->fSize;
140       }
141       pItem->SetData(pBlock->fPtr, pBlock->fSize);
142       HLTInfo("setting item size %d, total size %d", pItem->GetSize(), fOutputSize);
143     } else {
144       pItem=new AliHLTMonitoringItem(pBlock, pObject);
145       fItems.push_back(pItem);
146       fOutputSize+=pBlock->fSize;
147       HLTInfo("new item size %d (%d),  %s 0x%0lx %s %s", pItem->GetSize(), fOutputSize, DataType2Text(pItem->GetDataType()).c_str(), pItem->GetSpecification(), pObject?pObject->GetName():"", pObject?pObject->GetName():"");
148     }
149     if (pObject) delete pObject;
150   }
151
152   int nofObjects=0;
153   for (AliHLTMonitoringItemPList::iterator element=fItems.begin();
154        element!=fItems.end(); element++) {
155     AliHLTMonitoringItem* pItem=*element;
156     if (pItem) {
157       HLTInfo("push back item size %d (%d),  %s 0x%0lx", pItem->GetSize(), fOutputSize, DataType2Text(pItem->GetDataType()).c_str(), pItem->GetSpecification());
158       PushBack(pItem->GetBuffer(), pItem->GetSize(), pItem->GetDataType(), pItem->GetSpecification());
159       if (!pItem->GetObjectName().IsNull()) nofObjects++;
160     }
161   }
162   // info output once every 5 seconds
163   const TDatime time;
164   static UInt_t lastTime=0;
165   if (time.Get()-lastTime>5) {
166       lastTime=time.Get();
167       HLTBenchmark("accumulated %d items containing %d TObjects", fItems.size(), nofObjects);
168   }
169
170   return iResult;
171 }
172
173 AliHLTMonitoringRelay::AliHLTMonitoringItem* AliHLTMonitoringRelay::FindItem(const AliHLTComponentBlockData* pBlock, const TObject* pObject) const
174 {
175   // find an item by data type, specification, name and title
176   for (unsigned i=0; i<fItems.size(); i++) {
177     AliHLTMonitoringItem* pItem=fItems[i];
178     if (pItem &&
179         (*pItem)==(*pBlock) &&
180         (pObject==NULL || (*pItem)==(*pObject))) {
181       return pItem;
182     }
183   }
184   return NULL;  
185 }
186
187 AliHLTMonitoringRelay::AliHLTMonitoringItem::AliHLTMonitoringItem()
188   : fDt(kAliHLTVoidDataType)
189   , fSpecification(kAliHLTVoidDataSpec)
190   , fName()
191   , fTitle()
192   , fData(new TArrayC)
193   , fDataSize(0)
194 {
195   // standard constructor
196 }
197
198 AliHLTMonitoringRelay::AliHLTMonitoringItem::AliHLTMonitoringItem(const AliHLTComponentBlockData* pBlock, const TObject* pObject)
199   : fDt(kAliHLTVoidDataType)
200   , fSpecification(kAliHLTVoidDataSpec)
201   , fName()
202   , fTitle()
203   , fData(new TArrayC)
204   , fDataSize(0)
205 {
206   // constructor
207   if (pBlock) {
208     fDt=pBlock->fDataType;
209     fSpecification=pBlock->fSpecification;
210     if (fData) {
211       fData->Set(pBlock->fSize, reinterpret_cast<const Char_t*>(pBlock->fPtr));
212       fDataSize=pBlock->fSize;
213     }
214   }
215   if (pObject) {
216     fName=pObject->GetName();
217     fTitle=pObject->GetTitle();
218   }
219 }
220
221 AliHLTMonitoringRelay::AliHLTMonitoringItem::~AliHLTMonitoringItem()
222 {
223   // desstructor
224   if (fData) delete fData;
225 }
226
227 int AliHLTMonitoringRelay::AliHLTMonitoringItem::SetData(void* pBuffer, int size)
228 {
229   // copy the data buffer
230   if (!fData) {
231     fData=new TArrayC(size, reinterpret_cast<const Char_t*>(pBuffer));
232   }
233   if (!fData) {
234     return -ENOMEM;
235   }
236
237   if (fData->GetSize()<size) {
238     fData->Set(size, reinterpret_cast<const Char_t*>(pBuffer));
239   } else {
240     memcpy(fData->GetArray(), pBuffer, size);
241   }
242
243   fDataSize=size;
244   return 0;
245 }
246
247
248 void* AliHLTMonitoringRelay::AliHLTMonitoringItem::GetBuffer() const
249 {
250   // get buffer pointer of the current data
251   return fData!=NULL?fData->GetArray():NULL;
252 }
253
254 unsigned AliHLTMonitoringRelay::AliHLTMonitoringItem::GetSize() const
255 {
256   // get size of the current data
257   return fDataSize;
258 }
259
260 const AliHLTComponentDataType& AliHLTMonitoringRelay::AliHLTMonitoringItem::GetDataType() const
261 {
262   // get data type
263   return fDt;
264 }
265
266 AliHLTUInt32_t AliHLTMonitoringRelay::AliHLTMonitoringItem::GetSpecification() const
267 {
268   // get specification
269   return fSpecification;
270 }
271
272 bool AliHLTMonitoringRelay::AliHLTMonitoringItem::operator==(const AliHLTComponentBlockData& bd) const
273 {
274   // equal to data type and specification
275   if (bd.fDataType!=fDt) return false;
276   if (bd.fSpecification!=fSpecification) return false;
277   return true;
278 }
279
280 bool AliHLTMonitoringRelay::AliHLTMonitoringItem::operator!=(const AliHLTComponentBlockData& bd) const
281 {
282   // not equal to data type and specification
283   return not operator==(bd);
284 }
285
286 bool AliHLTMonitoringRelay::AliHLTMonitoringItem::operator==(const TObject& object) const
287 {
288   // equal to name and title
289   if (fName.CompareTo(object.GetName())!=0) return false;
290   if (!fTitle.IsNull() && fTitle.CompareTo(object.GetTitle())!=0) return false;
291   return true;
292 }
293
294 bool AliHLTMonitoringRelay::AliHLTMonitoringItem::operator!=(const TObject& object) const
295 {
296   // not equal to name and title
297   return not operator==(object);
298 }