19 #ifndef INCLUDE_UTIL_PROFILE_HPP
20 #define INCLUDE_UTIL_PROFILE_HPP
22 #include <sys/types.h>
23 #include <sys/timeb.h>
31 #include "RCF/ThreadLibrary.hpp"
35 class ProfilingResults
42 static ProfilingResults *&getSingletonPtrRef()
44 static ProfilingResults *pProfilingResults = NULL;
45 if (pProfilingResults == NULL)
47 pProfilingResults =
new ProfilingResults();
49 return pProfilingResults;
52 static ProfilingResults &getSingleton()
54 return *getSingletonPtrRef();
56 static void deleteSingleton()
58 delete getSingletonPtrRef();
59 getSingletonPtrRef() = NULL;
63 RCF::Lock lock(mMutex);
64 for (
unsigned int i=0; i<mResults.size(); i++)
66 std::ostringstream ostr;
67 ostr <<
"*************************\n";
68 ostr <<
"Profiling results for thread #" << i <<
":\n";
70 for (DataT::iterator iter_i = mResults[i]->begin(); iter_i != mResults[i]->end(); iter_i++) {
71 if (!(*iter_i).first.empty()) {
72 ostr << (*iter_i).first <<
": " << (*iter_i).second.first / 1000.0 <<
" s.\n";
73 SubDataT &subData = (*iter_i).second.second;
74 for (SubDataT::iterator iter_j = subData.begin(); iter_j != subData.end(); iter_j++)
75 ostr <<
" " << (*iter_j).first <<
": " << (*iter_j).second / 1000.0 <<
"s.\n";
79 ostr <<
"*************************\n";
80 std::cout << ostr.str();
85 friend class ProfilingData;
86 typedef std::map<std::string, unsigned int> SubDataT;
87 typedef std::map<std::string, std::pair<unsigned int, SubDataT > > DataT;
90 std::vector< DataT * > mResults;
92 void add( DataT *data )
94 RCF::Lock scoped_lock( mMutex );
95 mResults.push_back( data );
103 ProfilingData() : stack_(100), data_(new DataT()) { ProfilingResults::getSingleton().add( data_ ); }
105 static ProfilingData &getThreadSpecificInstance()
107 static RCF::ThreadSpecificPtr<ProfilingData>::Val profilingData;
109 if (NULL == profilingData.get())
111 profilingData.reset(
new ProfilingData());
112 profilingData->push(
"");
114 return *profilingData;
117 void push(std::string sz)
119 stack_.push_back(sz);
127 void add(
unsigned int timespan)
129 std::string cur = stack_[stack_.size()-1];
130 std::string prev = stack_[stack_.size()-2];
131 (*data_)[cur].first += timespan;
132 (*data_)[prev].second[cur] += timespan;
136 typedef ProfilingResults::DataT DataT;
137 std::vector<std::string> stack_;
141 inline unsigned int getTickCount()
143 return RCF::getCurrentTimeMs();
149 Profile(
const std::string &name) : name(name), t0(getTickCount()), t1(0)
151 ProfilingData::getThreadSpecificInstance().push(name);
157 ProfilingData::getThreadSpecificInstance().add(t1 - t0);
158 ProfilingData::getThreadSpecificInstance().pop();
182 class ImmediateProfile
185 ImmediateProfile(
const std::string &name) :
187 t0Ms(getTickCount()),
193 t1Ms = getTickCount();
194 std::ostringstream ostr;
195 ostr <<
"Profile result: " << mName <<
": " << t1Ms-t0Ms <<
"ms" << std::endl;
196 std::cout << ostr.str();