timer.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2010,
3  * François Bleibel,
4  * Olivier Stasse,
5  *
6  * CNRS/AIST
7  *
8  */
9 
10 #ifndef __SOT_TIMER_HH
11 #define __SOT_TIMER_HH
12 
13 /* --------------------------------------------------------------------- */
14 /* --- INCLUDE --------------------------------------------------------- */
15 /* --------------------------------------------------------------------- */
16 
17 /* Classes standards. */
18 #include <list> /* Classe std::list */
19 #ifndef WIN32
20 #include <sys/time.h>
21 #else /*WIN32*/
22 // When including Winsock2.h, the MAL must be included first
23 #include <Winsock2.h>
24 #include <dynamic-graph/linear-algebra.h>
26 #endif /*WIN32*/
27 
28 /* SOT */
29 #include <dynamic-graph/all-signals.h>
30 #include <dynamic-graph/entity.h>
31 #include <sot/core/debug.hh>
32 
33 /* --------------------------------------------------------------------- */
34 /* --- API ------------------------------------------------------------- */
35 /* --------------------------------------------------------------------- */
36 
37 #if defined(WIN32)
38 #if defined(timer_EXPORTS)
39 #define Timer_EXPORT __declspec(dllexport)
40 #else
41 #define Timer_EXPORT __declspec(dllimport)
42 #endif
43 #else
44 #define Timer_EXPORT
45 #endif
46 
47 /* --------------------------------------------------------------------- */
48 /* --- CLASS ----------------------------------------------------------- */
49 /* --------------------------------------------------------------------- */
50 namespace dg = dynamicgraph;
51 
52 template <class T> class Timer_EXPORT Timer : public dg::Entity {
53 public:
54  static const std::string CLASS_NAME;
55  virtual const std::string &getClassName(void) const { return CLASS_NAME; }
56 
57 protected:
58  struct timeval t0, t1;
59  clock_t c0, c1;
60  double dt;
61 
62 public:
63  /* --- CONSTRUCTION --- */
64  Timer(const std::string &name);
65 
66 public: /* --- DISPLAY --- */
67  virtual void display(std::ostream &os) const;
68  Timer_EXPORT friend std::ostream &operator<<(std::ostream &os,
69  const Timer<T> &timer) {
70  timer.display(os);
71  return os;
72  }
73 
74 public: /* --- SIGNALS --- */
75  dg::SignalPtr<T, int> sigSIN;
76  dg::SignalTimeDependent<T, int> sigSOUT;
77  dg::SignalTimeDependent<T, int> sigClockSOUT;
78  dg::Signal<double, int> timerSOUT;
79 
80 protected: /* --- SIGNAL FUNCTIONS --- */
81  void plug(dg::Signal<T, int> &sig) {
82  sigSIN = &sig;
83  dt = 0.;
84  }
85 
86  template <bool UseClock> T &compute(T &t, const int &time) {
87  sotDEBUGIN(15);
88  if (UseClock) {
89  c0 = clock();
90  sotDEBUG(15) << "t0: " << c0 << std::endl;
91  } else {
92  gettimeofday(&t0, NULL);
93  sotDEBUG(15) << "t0: " << t0.tv_sec << " - " << t0.tv_usec << std::endl;
94  }
95 
96  t = sigSIN(time);
97 
98  if (UseClock) {
99  c1 = clock();
100  sotDEBUG(15) << "t1: " << c0 << std::endl;
101  dt = ((double)(c1 - c0) * 1000) / CLOCKS_PER_SEC;
102  } else {
103  gettimeofday(&t1, NULL);
104  dt = ((static_cast<double>(t1.tv_sec) - static_cast<double>(t0.tv_sec)) *
105  1000. +
106  (static_cast<double>(t1.tv_usec) - static_cast<double>(t0.tv_usec) +
107  0.) /
108  1000.);
109  sotDEBUG(15) << "t1: " << t1.tv_sec << " - " << t1.tv_usec << std::endl;
110  }
111 
112  timerSOUT = dt;
113  timerSOUT.setTime(time);
114 
115  sotDEBUGOUT(15);
116  return t;
117  }
118 
119  double &getDt(double &res, const int & /*time*/) {
120  res = dt;
121  return res;
122  }
123 };
124 
125 void cmdChrono(const std::string &cmd, std::istringstream &args,
126  std::ostream &os);
127 
128 /* --------------------------------------------------------------------- */
129 /* --------------------------------------------------------------------- */
130 /* --------------------------------------------------------------------- */
131 
132 /* --- CONSTRUCTION ---------------------------------------------------- */
133 template <class T>
134 Timer<T>::Timer(const std::string &name)
135  : Entity(name), dt(0.), sigSIN(NULL, "Timer(" + name + ")::input(T)::sin"),
136  sigSOUT(boost::bind(&Timer::compute<false>, this, _1, _2), sigSIN,
137  "Timer(" + name + ")::output(T)::sout"),
138  sigClockSOUT(boost::bind(&Timer::compute<true>, this, _1, _2), sigSIN,
139  "Timer(" + name + ")::output(T)::clockSout"),
140  timerSOUT("Timer(" + name + ")::output(double)::timer") {
141  sotDEBUGIN(15);
142  timerSOUT.setFunction(boost::bind(&Timer::getDt, this, _1, _2));
143 
144  signalRegistration(sigSIN << sigSOUT << sigClockSOUT << timerSOUT);
145  sotDEBUGOUT(15);
146 }
147 
148 /* --- DISPLAY --------------------------------------------------------- */
149 template <class T> void Timer<T>::display(std::ostream &os) const {
150  os << "Timer <" << sigSIN << "> : " << dt << "ms." << std::endl;
151 }
152 
153 #endif /* #ifndef __SOT_SOT_HH */
utils-windows.hh
Timer::sigSIN
dg::SignalPtr< T, int > sigSIN
Definition: timer.hh:75
Timer::getDt
double & getDt(double &res, const int &)
Definition: timer.hh:119
dynamicgraph
Definition: abstract-sot-external-interface.hh:17
Timer_EXPORT
#define Timer_EXPORT
Definition: timer.hh:44
Timer::CLASS_NAME
static const std::string CLASS_NAME
Definition: timer.hh:54
Timer
Definition: timer.hh:52
debug.hh
sotDEBUGOUT
#define sotDEBUGOUT(level)
Definition: debug.hh:214
Timer::plug
void plug(dg::Signal< T, int > &sig)
Definition: timer.hh:81
Timer::c1
clock_t c1
Definition: timer.hh:59
Timer::operator<<
Timer_EXPORT friend std::ostream & operator<<(std::ostream &os, const Timer< T > &timer)
Definition: timer.hh:68
Timer::Timer
Timer(const std::string &name)
Definition: timer.hh:134
cmdChrono
void cmdChrono(const std::string &cmd, std::istringstream &args, std::ostream &os)
sotDEBUGIN
#define sotDEBUGIN(level)
Definition: debug.hh:213
Timer::dt
double dt
Definition: timer.hh:60
Timer::timerSOUT
dg::Signal< double, int > timerSOUT
Definition: timer.hh:78
Timer::compute
T & compute(T &t, const int &time)
Definition: timer.hh:86
Timer::sigClockSOUT
dg::SignalTimeDependent< T, int > sigClockSOUT
Definition: timer.hh:77
Timer::sigSOUT
dg::SignalTimeDependent< T, int > sigSOUT
Definition: timer.hh:76
Timer::display
virtual void display(std::ostream &os) const
Definition: timer.hh:149
Timer::getClassName
virtual const std::string & getClassName(void) const
Definition: timer.hh:55
sotDEBUG
#define sotDEBUG(level)
Definition: debug.hh:167