timer_internal_gathering.hh

Go to the documentation of this file.
00001 // timer_internal_gathering.hh: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2006, 2007 The Vaucanson Group.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // The complete GNU General Public Licence Notice can be found as the
00013 // `COPYING' file in the root directory.
00014 //
00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
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     // Internal Time representation
00060     class TimeStamp;
00061 
00062     // Data collection classes
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     // Friends of TimeStamp
00074     class GraphCall;
00075     class GraphComponent;
00076     class GraphTask;
00077 
00078 
00079     // Time units for printing
00080     typedef 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       // \param d: Time in seconds.
00094       TimeVal (double d);
00095       // \param i: Time in seconds.
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     // Print time in given unit
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       // Set to the current time
00146       void set_to_now ();
00147 
00148       // Set to the difference between the current time and the time
00149       // previously set.time_.set_to_lap();
00150       void set_to_lap ();
00151 
00152       // Set internal values to 0;
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       // Ordering using CPU time.
00161       bool operator< (const TimeStamp& rhs) const;
00162 
00163     private:
00164       TimeVal   user_;
00165       TimeVal   sys_;
00166     };
00167 
00168     // Data collection classes
00169     class Task
00170     {
00171     public:
00172       friend class misc::Timer;
00173 
00174       Task ();
00175 
00176       Task (const Task& task);
00177 
00178       // Create a new task.
00179       Task (const std::string&  name,
00180             unsigned int        id);
00181 
00182       // Free call list upon destruction.
00183       ~Task ();
00184 
00185       // Add the call if it doesn't exist.  Return the call to the
00186       // called task.
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       // Initialize upon creation.
00202       explicit Call (unsigned int called = 0);
00203 
00204       // Sum up the call stats;
00205       Call& operator+= (const Call& call);
00206 
00207     private:
00208       // Adds the calculated time of a task instance to the associated
00209       // call on Timer::pop()
00210       void add (const StackedCall&      call);
00211 
00212 
00213       // Total time of the called task and its children.
00214       TimeStamp total_;
00215 
00216       // Time of the called task itself only.
00217       TimeStamp self_;
00218 
00219       // Number of calls with the same calling and called tasks.
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   } // namespace timer
00242 } // namespace misc
00243 
00244 NAMESPACE_VCSN_END
00245 
00246 #endif // !VCSN_MISC_TIMER_INTERNAL_GATHERING_HH

Generated on Thu Dec 13 16:03:01 2007 for Vaucanson by  doxygen 1.5.4