00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef VCSN_MISC_TIMER_INTERNAL_GATHERING_HH
00019 # define VCSN_MISC_TIMER_INTERNAL_GATHERING_HH
00020
00030 # include <stack>
00031 # include <vector>
00032 # include <map>
00033 # include <list>
00034 # include <string>
00035 # include <iosfwd>
00036
00037 # include <sys/time.h>
00038 # include <sys/resource.h>
00039
00040 # ifdef VAUCANSON
00041 # define NAMESPACE_VCSN_BEGIN namespace vcsn {
00042 # define NAMESPACE_VCSN_END } // namespace vcsn
00043 # include <vaucanson/misc/contract.hh>
00044 # else
00045 # define NAMESPACE_VCSN_BEGIN
00046 # define NAMESPACE_VCSN_END
00047 # include <cassert>
00048 # define precondition(C) assert(C)
00049 # endif
00050
00051 NAMESPACE_VCSN_BEGIN
00052
00053 namespace misc
00054 {
00055 class Timer;
00056
00057 namespace timer
00058 {
00059
00060 class TimeStamp;
00061
00062
00063 class Task;
00064 class Call;
00065 class StackedCall;
00066
00067 typedef std::map<const std::string, int> task_names_map;
00068 typedef std::vector<Task> task_vector;
00069 typedef std::map<int, Call> call_map;
00070 typedef std::stack<StackedCall> call_stack;
00071
00072
00073
00074 class GraphCall;
00075 class GraphComponent;
00076 class GraphTask;
00077
00078
00079
00080 enum time_unit
00081 {
00082 TIME_DEFAULT,
00083 TIME_H,
00084 TIME_M,
00085 TIME_S,
00086 TIME_MS
00087 };
00088
00089 struct TimeVal
00090 {
00091 TimeVal ();
00092
00093
00094 TimeVal (double d);
00095
00096 TimeVal (int i);
00097 TimeVal (const TimeVal& tv);
00098 TimeVal (const timeval& tv);
00099
00100 TimeVal operator+ (const TimeVal& tv) const;
00101 TimeVal operator- (const TimeVal& tv) const;
00102 TimeVal& operator+= (const TimeVal& tv);
00103 TimeVal& operator-= (const TimeVal& tv);
00104 TimeVal& operator= (const TimeVal& tv);
00105
00106 TimeVal& operator/= (double d);
00107 TimeVal operator/ (double d) const;
00108
00109 bool operator== (const TimeVal& tv) const;
00110 bool operator< (const TimeVal& tv) const;
00111 bool operator> (const TimeVal& tv) const;
00112
00113 double us () const;
00114 double ms () const;
00115 double s () const;
00116 double m () const;
00117 double h () const;
00118
00119 std::ostream& print (std::ostream& o,
00120 time_unit u = TIME_DEFAULT) const;
00121
00122 void clear ();
00123 void set (const timeval&);
00124
00125 long tv_sec;
00126 long tv_usec;
00127 };
00128
00129
00130 std::ostream& print_time (std::ostream& o,
00131 timer::TimeVal& time,
00132 time_unit u = TIME_DEFAULT);
00133
00134 class TimeStamp
00135 {
00136 friend class misc::Timer;
00137 friend class Call;
00138 friend class StackedCall;
00139 friend class GraphCall;
00140 friend class GraphComponent;
00141 friend class GraphTask;
00142
00143 TimeStamp ();
00144
00145
00146 void set_to_now ();
00147
00148
00149
00150 void set_to_lap ();
00151
00152
00153 void clear ();
00154
00155 std::ostream& print (std::ostream& o) const;
00156
00157 TimeStamp& operator+= (const TimeStamp& rhs);
00158 TimeStamp& operator-= (const TimeStamp& rhs);
00159
00160
00161 bool operator< (const TimeStamp& rhs) const;
00162
00163 private:
00164 TimeVal user_;
00165 TimeVal sys_;
00166 };
00167
00168
00169 class Task
00170 {
00171 public:
00172 friend class misc::Timer;
00173
00174 Task ();
00175
00176 Task (const Task& task);
00177
00178
00179 Task (const std::string& name,
00180 unsigned int id);
00181
00182
00183 ~Task ();
00184
00185
00186
00187 Call& call (unsigned int called);
00188
00189 private:
00190 std::string name_;
00191 unsigned int id_;
00192 call_map calls_;
00193 };
00194
00195 class Call
00196 {
00197 public:
00198 friend class misc::Timer;
00199 friend class Task;
00200
00201
00202 explicit Call (unsigned int called = 0);
00203
00204
00205 Call& operator+= (const Call& call);
00206
00207 private:
00208
00209
00210 void add (const StackedCall& call);
00211
00212
00213
00214 TimeStamp total_;
00215
00216
00217 TimeStamp self_;
00218
00219
00220 unsigned int count_;
00221
00222 unsigned int called_;
00223 };
00224
00225 class StackedCall
00226 {
00227 public:
00228 friend class misc::Timer;
00229 friend class Task;
00230 friend class Call;
00231
00232 explicit StackedCall (unsigned int called = 0);
00233 StackedCall ();
00234
00235 private:
00236 TimeStamp total_;
00237 TimeStamp children_;
00238
00239 unsigned int called_;
00240 };
00241 }
00242 }
00243
00244 NAMESPACE_VCSN_END
00245
00246 #endif // !VCSN_MISC_TIMER_INTERNAL_GATHERING_HH