GCC Code Coverage Report


Directory: ./
File: include/sot/core/timer.hh
Date: 2024-08-13 12:13:25
Exec Total Coverage
Lines: 0 28 0.0%
Branches: 0 50 0.0%

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