sot-core  4.11.2
Hierarchical task solver plug-in for dynamic-graph.
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 
51 template <class T> class Timer_EXPORT Timer : public dynamicgraph::Entity {
52 public:
53  static const std::string CLASS_NAME;
54  virtual const std::string &getClassName(void) const { return CLASS_NAME; }
55 
56 protected:
57  struct timeval t0, t1;
58  clock_t c0, c1;
59  double dt;
60 
61 public:
62  /* --- CONSTRUCTION --- */
63  Timer(const std::string &name);
64 
65 public: /* --- DISPLAY --- */
66  virtual void display(std::ostream &os) const;
67  Timer_EXPORT friend std::ostream &operator<<(std::ostream &os,
68  const Timer<T> &timer) {
69  timer.display(os);
70  return os;
71  }
72 
73 public: /* --- SIGNALS --- */
74  dynamicgraph::SignalPtr<T, int> sigSIN;
75  dynamicgraph::SignalTimeDependent<T, int> sigSOUT;
76  dynamicgraph::SignalTimeDependent<T, int> sigClockSOUT;
77  dynamicgraph::Signal<double, int> timerSOUT;
78 
79 protected: /* --- SIGNAL FUNCTIONS --- */
80  void plug(dynamicgraph::Signal<T, int> &sig) {
81  sigSIN = &sig;
82  dt = 0.;
83  }
84 
85  template <bool UseClock> T &compute(T &t, const int &time) {
86  sotDEBUGIN(15);
87  if (UseClock) {
88  c0 = clock();
89  sotDEBUG(15) << "t0: " << c0 << std::endl;
90  } else {
91  gettimeofday(&t0, NULL);
92  sotDEBUG(15) << "t0: " << t0.tv_sec << " - " << t0.tv_usec << std::endl;
93  }
94 
95  t = sigSIN(time);
96 
97  if (UseClock) {
98  c1 = clock();
99  sotDEBUG(15) << "t1: " << c0 << std::endl;
100  dt = ((double)(c1 - c0) * 1000) / CLOCKS_PER_SEC;
101  } else {
102  gettimeofday(&t1, NULL);
103  dt = ((static_cast<double>(t1.tv_sec) - static_cast<double>(t0.tv_sec)) *
104  1000. +
105  (static_cast<double>(t1.tv_usec) - static_cast<double>(t0.tv_usec) +
106  0.) /
107  1000.);
108  sotDEBUG(15) << "t1: " << t1.tv_sec << " - " << t1.tv_usec << std::endl;
109  }
110 
111  timerSOUT = dt;
112  timerSOUT.setTime(time);
113 
114  sotDEBUGOUT(15);
115  return t;
116  }
117 
118  double &getDt(double &res, const int & /*time*/) {
119  res = dt;
120  return res;
121  }
122 };
123 
124 void cmdChrono(const std::string &cmd, std::istringstream &args,
125  std::ostream &os);
126 
127 /* --------------------------------------------------------------------- */
128 /* --------------------------------------------------------------------- */
129 /* --------------------------------------------------------------------- */
130 
131 /* --- CONSTRUCTION ---------------------------------------------------- */
132 template <class T>
133 Timer<T>::Timer(const std::string &name)
134  : Entity(name), dt(0.), sigSIN(NULL, "Timer(" + name + ")::input(T)::sin"),
135  sigSOUT(boost::bind(&Timer::compute<false>, this, _1, _2), sigSIN,
136  "Timer(" + name + ")::output(T)::sout"),
137  sigClockSOUT(boost::bind(&Timer::compute<true>, this, _1, _2), sigSIN,
138  "Timer(" + name + ")::output(T)::clockSout"),
139  timerSOUT("Timer(" + name + ")::output(double)::timer") {
140  sotDEBUGIN(15);
141  timerSOUT.setFunction(boost::bind(&Timer::getDt, this, _1, _2));
142 
143  signalRegistration(sigSIN << sigSOUT << sigClockSOUT << timerSOUT);
144  sotDEBUGOUT(15);
145 }
146 
147 /* --- DISPLAY --------------------------------------------------------- */
148 template <class T> void Timer<T>::display(std::ostream &os) const {
149  os << "Timer <" << sigSIN << "> : " << dt << "ms." << std::endl;
150 }
151 
152 #endif /* #ifndef __SOT_SOT_HH */
dynamicgraph::SignalTimeDependent< T, int > sigClockSOUT
Definition: timer.hh:76
dynamicgraph::SignalPtr< T, int > sigSIN
Definition: timer.hh:74
double & getDt(double &res, const int &)
Definition: timer.hh:118
#define sotDEBUGOUT(level)
Definition: debug.hh:214
static const std::string CLASS_NAME
Definition: timer.hh:53
#define sotDEBUGIN(level)
Definition: debug.hh:213
Timer_EXPORT friend std::ostream & operator<<(std::ostream &os, const Timer< T > &timer)
Definition: timer.hh:67
Timer(const std::string &name)
Definition: timer.hh:133
double dt
Definition: timer.hh:59
clock_t c1
Definition: timer.hh:58
#define sotDEBUG(level)
Definition: debug.hh:167
virtual void display(std::ostream &os) const
Definition: timer.hh:148
dynamicgraph::SignalTimeDependent< T, int > sigSOUT
Definition: timer.hh:75
dynamicgraph::Signal< double, int > timerSOUT
Definition: timer.hh:77
virtual const std::string & getClassName(void) const
Definition: timer.hh:54
#define Timer_EXPORT
Definition: timer.hh:44
Definition: timer.hh:51
void plug(dynamicgraph::Signal< T, int > &sig)
Definition: timer.hh:80
T & compute(T &t, const int &time)
Definition: timer.hh:85
void cmdChrono(const std::string &cmd, std::istringstream &args, std::ostream &os)