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>
25 
27 #endif /*WIN32*/
28 
29 /* SOT */
31 #include <dynamic-graph/entity.h>
32 
33 #include <sot/core/debug.hh>
34 
35 /* --------------------------------------------------------------------- */
36 /* --- API ------------------------------------------------------------- */
37 /* --------------------------------------------------------------------- */
38 
39 #if defined(WIN32)
40 #if defined(timer_EXPORTS)
41 #define Timer_EXPORT __declspec(dllexport)
42 #else
43 #define Timer_EXPORT __declspec(dllimport)
44 #endif
45 #else
46 #define Timer_EXPORT
47 #endif
48 
49 /* --------------------------------------------------------------------- */
50 /* --- CLASS ----------------------------------------------------------- */
51 /* --------------------------------------------------------------------- */
52 
53 namespace dg = dynamicgraph;
55 template <class T>
57  public:
58  static const std::string CLASS_NAME;
59  virtual const std::string &getClassName(void) const { return CLASS_NAME; }
60 
61  protected:
62  struct timeval t0, t1;
63  clock_t c0, c1;
64  double dt;
65 
66  public:
67  /* --- CONSTRUCTION --- */
68  Timer(const std::string &name);
69 
70  public: /* --- DISPLAY --- */
71  virtual void display(std::ostream &os) const;
72  Timer_EXPORT friend std::ostream &operator<<(std::ostream &os,
73  const Timer<T> &timer) {
74  timer.display(os);
75  return os;
76  }
77 
78  public: /* --- SIGNALS --- */
83 
84  protected: /* --- SIGNAL FUNCTIONS --- */
86  sigSIN = &sig;
87  dt = 0.;
88  }
89 
90  template <bool UseClock>
91  T &compute(T &t, const sigtime_t &time) {
92  sotDEBUGIN(15);
93  if (UseClock) {
94  c0 = clock();
95  sotDEBUG(15) << "t0: " << c0 << std::endl;
96  } else {
97  gettimeofday(&t0, NULL);
98  sotDEBUG(15) << "t0: " << t0.tv_sec << " - " << t0.tv_usec << std::endl;
99  }
100 
101  t = sigSIN(time);
102 
103  if (UseClock) {
104  c1 = clock();
105  sotDEBUG(15) << "t1: " << c0 << std::endl;
106  dt = ((double)(c1 - c0) * 1000) / CLOCKS_PER_SEC;
107  } else {
108  gettimeofday(&t1, NULL);
109  dt = ((static_cast<double>(t1.tv_sec) - static_cast<double>(t0.tv_sec)) *
110  1000. +
111  (static_cast<double>(t1.tv_usec) - static_cast<double>(t0.tv_usec) +
112  0.) /
113  1000.);
114  sotDEBUG(15) << "t1: " << t1.tv_sec << " - " << t1.tv_usec << std::endl;
115  }
116 
117  timerSOUT = dt;
118  timerSOUT.setTime(time);
119 
120  sotDEBUGOUT(15);
121  return t;
122  }
123 
124  double &getDt(double &res, const sigtime_t & /*time*/) {
125  res = dt;
126  return res;
127  }
128 };
129 
130 void cmdChrono(const std::string &cmd, std::istringstream &args,
131  std::ostream &os);
132 
133 /* --------------------------------------------------------------------- */
134 /* --------------------------------------------------------------------- */
135 /* --------------------------------------------------------------------- */
136 
137 /* --- CONSTRUCTION ---------------------------------------------------- */
138 template <class T>
139 Timer<T>::Timer(const std::string &name)
140  : Entity(name),
141  dt(0.),
142  sigSIN(NULL, "Timer(" + name + ")::input(T)::sin"),
143  sigSOUT(boost::bind(&Timer::compute<false>, this, _1, _2), sigSIN,
144  "Timer(" + name + ")::output(T)::sout"),
145  sigClockSOUT(boost::bind(&Timer::compute<true>, this, _1, _2), sigSIN,
146  "Timer(" + name + ")::output(T)::clockSout"),
147  timerSOUT("Timer(" + name + ")::output(double)::timer") {
148  sotDEBUGIN(15);
149  timerSOUT.setFunction(boost::bind(&Timer::getDt, this, _1, _2));
150 
152  sotDEBUGOUT(15);
153 }
154 
155 /* --- DISPLAY --------------------------------------------------------- */
156 template <class T>
157 void Timer<T>::display(std::ostream &os) const {
158  os << "Timer <" << sigSIN << "> : " << dt << "ms." << std::endl;
159 }
160 
161 #endif /* #ifndef __SOT_SOT_HH */
utils-windows.hh
dynamicgraph::Signal< double, dg::sigtime_t >
Timer::getDt
double & getDt(double &res, const sigtime_t &)
Definition: timer.hh:124
T
int T
Timer::sigSIN
dynamicgraph::SignalPtr< T, dg::sigtime_t > sigSIN
Definition: timer.hh:79
dynamicgraph::SignalPtr< T, dg::sigtime_t >
dynamicgraph
Timer::compute
T & compute(T &t, const sigtime_t &time)
Definition: timer.hh:91
Timer::sigSOUT
dynamicgraph::SignalTimeDependent< T, dg::sigtime_t > sigSOUT
Definition: timer.hh:80
dynamicgraph::Entity
boost
sigtime_t
dynamicgraph::sigtime_t sigtime_t
dynamicgraph::Signal::setFunction
virtual void setFunction(boost::function2< T &, T &, Time > t, Mutex *mutexref=NULL)
Timer_EXPORT
#define Timer_EXPORT
Definition: timer.hh:46
Timer::CLASS_NAME
static const std::string CLASS_NAME
Definition: timer.hh:58
Timer
Definition: timer.hh:56
debug.hh
res
res
sotDEBUGOUT
#define sotDEBUGOUT(level)
Definition: debug.hh:215
dynamicgraph::sigtime_t
int64_t sigtime_t
Timer::c1
clock_t c1
Definition: timer.hh:63
Timer::operator<<
Timer_EXPORT friend std::ostream & operator<<(std::ostream &os, const Timer< T > &timer)
Definition: timer.hh:72
Timer::Timer
Timer(const std::string &name)
Definition: timer.hh:139
cmdChrono
void cmdChrono(const std::string &cmd, std::istringstream &args, std::ostream &os)
Definition: timer.cpp:45
sotDEBUGIN
#define sotDEBUGIN(level)
Definition: debug.hh:214
Timer::dt
double dt
Definition: timer.hh:64
sigtime_t
dg::sigtime_t sigtime_t
Definition: timer.hh:54
dynamicgraph::Entity::display
virtual void display(std::ostream &os) const
all-signals.h
Timer::timerSOUT
dynamicgraph::Signal< double, dg::sigtime_t > timerSOUT
Definition: timer.hh:82
linear-algebra.h
dynamicgraph::sot::double
double
Definition: fir-filter.cpp:49
dynamicgraph::SignalTimeDependent< T, dg::sigtime_t >
test-parameter-server.dt
float dt
Definition: test-parameter-server.py:14
t
Transform3f t
Timer::display
virtual void display(std::ostream &os) const
Definition: timer.hh:157
sig
Signal< dynamicgraph::Matrix, sigtime_t > sig("matrix")
Timer::getClassName
virtual const std::string & getClassName(void) const
Definition: timer.hh:59
dynamicgraph::Entity::signalRegistration
void signalRegistration(const SignalArray< sigtime_t > &signals)
Timer::sigClockSOUT
dynamicgraph::SignalTimeDependent< T, dg::sigtime_t > sigClockSOUT
Definition: timer.hh:81
Timer::plug
void plug(dynamicgraph::Signal< T, dg::sigtime_t > &sig)
Definition: timer.hh:85
compile.name
name
Definition: compile.py:23
sotDEBUG
#define sotDEBUG(level)
Definition: debug.hh:168


sot-core
Author(s): Olivier Stasse, ostasse@laas.fr
autogenerated on Tue Oct 24 2023 02:26:32