44 #ifndef GU_PROFILE_HPP
45 #define GU_PROFILE_HPP
48 #include "gu_datetime.hpp"
49 #include "gu_lock.hpp"
51 #if defined(HAVE_BOOST_UNORDERED_MAP_HPP)
52 #include <boost/unordered_map.hpp>
53 #elif defined(HAVE_UNORDERED_MAP)
54 #include <unordered_map>
55 #elif defined(HAVE_TR1_UNORDERED_MAP)
56 #include <tr1/unordered_map>
59 #endif // HAVE_BOOST_UNORDERED_MAP_HPP
71 std::ostream& operator<<(std::ostream&,
const Key&);
72 std::ostream& operator<<(std::ostream&,
const Profile&);
83 Key(
const char*
const file,
84 const char*
const func,
91 bool operator==(
const Key& cmp)
const
93 return (line_ == cmp.line_ &&
98 bool operator<(
const Key& cmp)
const
100 return (line_ < cmp.line_ ||
101 (line_ == cmp.line_ && (func_ < cmp.func_ ||
102 (func_ == cmp.func_ && file_ < cmp.file_))));
104 std::string to_string()
const
106 std::ostringstream os;
111 friend class KeyHash;
114 friend std::ostream& operator<<(std::ostream& os,
const Key&);
115 const char*
const file_;
116 const char*
const func_;
120 #ifdef HAVE_BOOST_UNORDERED_MAP_HPP
121 class gu::prof::KeyHash
124 size_t operator()(
const Key& key)
const
126 return boost::hash_value(key.file_)
127 ^ boost::hash_value(key.func_)
128 ^ boost::hash_value(key.line_);
132 #endif // HAVE_BOOST_UNORDERED_MAP_HPP
134 inline std::ostream& gu::prof::operator<<(std::ostream& os,
137 return os << key.file_ <<
":" << key.func_ <<
":" << key.line_;
153 mutable long long int enter_time_calendar_;
154 mutable long long int enter_time_thread_cputime_;
165 PointStats(
long long int count = 0,
166 long long int time_calendar = 0,
167 long long int time_thread_cputime = 0) :
169 time_calendar_ (time_calendar ),
170 time_thread_cputime_(time_thread_cputime)
173 PointStats operator+(
const PointStats& add)
const
175 return PointStats(count_ + add.count_,
176 time_calendar_ + add.time_calendar_,
177 time_thread_cputime_+ add.time_thread_cputime_);
180 long long int count_;
181 long long int time_calendar_;
182 long long int time_thread_cputime_;
184 #if defined(HAVE_BOOST_UNORDERED_MAP_HPP)
185 typedef boost::unordered_map<Key, PointStats, KeyHash> Map;
186 #elif defined(HAVE_UNORDERED_MAP)
187 typedef std::unordered_map<Key, PointStats, KeyHash> Map;
188 #elif defined(HAVE_TR1_UNORDERED_MAP)
189 typedef std::tr1::unordered_map<Key, PointStats, KeyHash> Map;
191 typedef std::map<Key, PointStats> Map;
199 Profile(
const std::string& name =
"profile") :
201 start_time_calendar_(gu_time_calendar()),
202 start_time_thread_cputime_(gu_time_thread_cputime()),
207 void enter(
const Point& point)
const
209 point.enter_time_calendar_ = gu_time_calendar();
210 point.enter_time_thread_cputime_ = gu_time_thread_cputime();
212 points_[point.key_].count_++;
215 void leave(
const Point& point)
const
217 long long int t_cal(gu_time_calendar());
218 long long int t_thdcpu(gu_time_thread_cputime());
221 PointStats& pointst(points_[point.key_]);
222 pointst.time_calendar_ +=
223 (t_cal - point.enter_time_calendar_);
224 pointst.time_thread_cputime_ +=
225 (t_thdcpu - point.enter_time_thread_cputime_);
234 friend std::ostream& operator<<(std::ostream&,
const Profile&);
236 std::string
const name_;
237 long long int const start_time_calendar_;
238 long long int const start_time_thread_cputime_;
243 inline gu::prof::Point::Point(
const Profile& prof,
248 key_(file, func, line),
249 enter_time_calendar_(),
250 enter_time_thread_cputime_()
255 inline gu::prof::Point::~Point()
264 inline std::ostream& gu::prof::operator<<(std::ostream& os,
const Profile& prof)
267 Profile::PointStats cumul;
269 char prev_fill(os.fill());
271 os <<
"\nprofile name: " << prof.name_;
273 os << std::left << std::fixed << std::setprecision(3);
275 os << std::setw(40) <<
"point";
276 os << std::setw(10) <<
"count";
277 os << std::setw(10) <<
"calendar";
278 os << std::setw(10) <<
"cpu";
280 << std::setfill(
'-') << std::setw(70) <<
""
281 << std::setfill(
' ') <<
"\n";
282 for (Profile::Map::const_iterator i = prof.points_.begin();
283 i != prof.points_.end(); ++i)
285 os << std::setw(40) << std::left << i->first.to_string();
287 os << std::setw(10) << i->second.count_;
288 os << std::setw(10) << double(i->second.time_calendar_)*1.e-9;
289 os << std::setw(10) << double(i->second.time_thread_cputime_)*1.e-9;
292 cumul = cumul + i->second;
295 os <<
"\ntot count : " << cumul.count_;
296 os <<
"\ntot calendar time : " << double(cumul.time_calendar_)*1.e-9;
297 os <<
"\ntot thread cputime: " << double(cumul.time_thread_cputime_)*1.e-9;
298 os <<
"\ntot ct since ctor : "
311 #define profile_enter(__p) \
313 const gu::prof::Point __point((__p), __FILE__, \
314 __FUNCTION__, __LINE__); \
316 #define profile_leave(__p) \
319 #define profile_enter(__p)
320 #define profile_leave(__p)
323 #endif // GU_PROFILE_HPP
Definition: gu_lock.hpp:20
Definition: gu_profile.hpp:161
Definition: gu_mutex.hpp:19
Profile(const std::string &name="profile")
Definition: gu_profile.hpp:199
Definition: gu_profile.hpp:80
Definition: gu_profile.hpp:141
static Date now()
Get system time.
Definition: gu_datetime.hpp:124