44 #ifndef GCOMM_PROFILE_HPP
45 #define GCOMM_PROFILE_HPP
62 std::ostream& operator<<(std::ostream&,
const Key&);
63 std::ostream& operator<<(std::ostream&,
const Profile&);
74 Key(
const char*
const file,
75 const char*
const func,
83 bool operator==(
const Key& cmp)
const
85 return (line_ == cmp.line_ &&
90 bool operator<(
const Key& cmp)
const
92 return (line_ < cmp.line_ ||
93 (line_ == cmp.line_ && (func_ < cmp.func_ ||
94 (func_ == cmp.func_ && file_ < cmp.file_))));
96 std::string to_string()
const
98 std::ostringstream os;
105 friend std::ostream& operator<<(std::ostream& os,
const Key&);
106 const char*
const file_;
107 const char*
const func_;
111 inline std::ostream& prof::operator<<(std::ostream& os,
const prof::Key& key)
113 return os << key.file_ <<
":" << key.func_ <<
":" << key.line_;
129 mutable long long int enter_time_calendar_;
130 mutable long long int enter_time_thread_cputime_;
140 PointStats(
long long int count = 0,
141 long long int time_calendar = 0,
142 long long int time_thread_cputime = 0) :
144 time_calendar_ (time_calendar ),
145 time_thread_cputime_(time_thread_cputime)
148 PointStats operator+(
const PointStats& add)
const
150 return PointStats(count_ + add.count_,
151 time_calendar_ + add.time_calendar_,
152 time_thread_cputime_ + add.time_thread_cputime_);
155 long long int count_;
156 long long int time_calendar_;
157 long long int time_thread_cputime_;
165 Profile(
const std::string& name =
"profile") :
167 start_time_calendar_(gu_time_calendar()),
168 start_time_thread_cputime_(gu_time_thread_cputime()),
172 void enter(
const Point& point)
const
174 points_[point.key_].count_++;
175 point.enter_time_calendar_ = gu_time_calendar();
176 point.enter_time_thread_cputime_ = gu_time_thread_cputime();
179 void leave(
const Point& point)
const
181 long long int t_cal(gu_time_calendar());
182 long long int t_thdcpu(gu_time_thread_cputime());
183 points_[point.key_].time_calendar_ +=
184 (t_cal - point.enter_time_calendar_);
185 points_[point.key_].time_thread_cputime_ +=
186 (t_thdcpu - point.enter_time_thread_cputime_);
189 void clear() { points_.clear(); }
191 friend std::ostream& operator<<(std::ostream&,
const Profile&);
193 typedef std::map<Key, PointStats> Map;
194 std::string
const name_;
195 long long int const start_time_calendar_;
196 long long int const start_time_thread_cputime_;
200 inline prof::Point::Point(
const Profile& prof,
205 key_(file, func, line),
206 enter_time_calendar_(),
207 enter_time_thread_cputime_()
212 inline prof::Point::~Point()
221 inline std::ostream& prof::operator<<(std::ostream& os,
const Profile& prof)
223 Profile::PointStats cumul;
225 char prev_fill(os.fill());
227 os <<
"\nprofile name: " << prof.name_;
230 os << std::left << std::fixed << std::setprecision(3);
232 os << std::setw(40) <<
"point";
233 os << std::setw(10) <<
"count";
234 os << std::setw(10) <<
"calendar";
235 os << std::setw(10) <<
"cpu";
237 << std::setfill(
'-') << std::setw(70) <<
""
238 << std::setfill(
' ') <<
"\n";
239 for (Profile::Map::const_iterator i = prof.points_.begin();
240 i != prof.points_.end(); ++i)
242 os << std::setw(40) << std::left << i->first.to_string();
244 os << std::setw(10) << i->second.count_;
245 os << std::setw(10) << double(i->second.time_calendar_)*1.e-9;
246 os << std::setw(10) << double(i->second.time_thread_cputime_)*1.e-9;
249 cumul = cumul + i->second;
252 os <<
"\ntot count : " << cumul.count_;
253 os <<
"\ntot calendar time : " << double(cumul.time_calendar_)*1.e-9;
254 os <<
"\ntot thread cputime: " << double(cumul.time_thread_cputime_)*1.e-9;
255 os <<
"\ntot ct since ctor : "
256 << double(gu::datetime::Date::now().get_utc()
257 - prof.start_time_calendar_)*1.e-9;
269 #define profile_enter(__p) do { \
270 const prof::Point __point((__p), __FILE__, __FUNCTION__, __LINE__); \
272 #define profile_leave(__p) \
275 #define profile_enter(__p)
276 #define profile_leave(__p)
277 #endif // GCOMM_PROFILE
279 #endif // GCOMM_PROFILE_HPP
Definition: profile.hpp:136
Profile(const std::string &name="profile")
Definition: profile.hpp:165
Definition: profile.hpp:71
Definition: profile.hpp:117