Skip to content

Commit d7f6688

Browse files
committed
Move timing code into logging system
Also implement a WithPerfLogging class based on timing code to remove duplicate timing code in several processors.
1 parent ef7fd4e commit d7f6688

File tree

10 files changed

+122
-262
lines changed

10 files changed

+122
-262
lines changed

examples/protonect/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ SET(SOURCES
101101
src/resource.cpp
102102
src/command_transaction.cpp
103103
src/registration.cpp
104-
src/timer.cpp
105104
src/logging.cpp
106105
src/libfreenect2.cpp
107106

examples/protonect/include/libfreenect2/logging.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@
3636
namespace libfreenect2
3737
{
3838

39+
class WithPerfLoggingImpl;
40+
41+
class WithPerfLogging
42+
{
43+
public:
44+
WithPerfLogging();
45+
virtual ~WithPerfLogging();
46+
void startTiming();
47+
std::ostream &stopTiming(std::ostream &stream);
48+
private:
49+
WithPerfLoggingImpl *impl_;
50+
};
51+
3952
class LogMessage
4053
{
4154
private:

examples/protonect/include/libfreenect2/packet_processor.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
#ifndef PACKET_PROCESSOR_H_
2828
#define PACKET_PROCESSOR_H_
2929

30-
#include <libfreenect2/timer.h>
31-
3230
namespace libfreenect2
3331
{
3432

examples/protonect/include/libfreenect2/timer.h

Lines changed: 0 additions & 51 deletions
This file was deleted.

examples/protonect/src/cpu_depth_packet_processor.cpp

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ inline int bfi(int width, int offset, int src2, int src3)
202202
return ((src2 << offset) & bitmask) | (src3 & ~bitmask);
203203
}
204204

205-
class CpuDepthPacketProcessorImpl
205+
class CpuDepthPacketProcessorImpl: public WithPerfLogging
206206
{
207207
public:
208208
Mat<uint16_t> p0_table0, p0_table1, p0_table2;
@@ -214,11 +214,6 @@ class CpuDepthPacketProcessorImpl
214214
float trig_table1[512*424][6];
215215
float trig_table2[512*424][6];
216216

217-
double timing_acc;
218-
double timing_acc_n;
219-
220-
Timer timer;
221-
222217
bool enable_bilateral_filter, enable_edge_filter;
223218
DepthPacketProcessor::Parameters params;
224219

@@ -231,34 +226,12 @@ class CpuDepthPacketProcessorImpl
231226
newIrFrame();
232227
newDepthFrame();
233228

234-
timing_acc = 0.0;
235-
timing_acc_n = 0.0;
236-
237229
enable_bilateral_filter = true;
238230
enable_edge_filter = true;
239231

240232
flip_ptables = true;
241233
}
242234

243-
void startTiming()
244-
{
245-
timer.start();
246-
}
247-
248-
void stopTiming()
249-
{
250-
timing_acc += timer.stop();
251-
timing_acc_n += 1.0;
252-
253-
if(timing_acc_n >= 100.0)
254-
{
255-
double avg = (timing_acc / timing_acc_n);
256-
LOG_INFO << "[CpuDepthPacketProcessor] avg. time: " << (avg * 1000) << "ms -> ~" << (1.0/avg) << "Hz";
257-
timing_acc = 0.0;
258-
timing_acc_n = 0.0;
259-
}
260-
}
261-
262235
void newIrFrame()
263236
{
264237
ir_frame = new Frame(512, 424, 4);
@@ -975,7 +948,7 @@ void CpuDepthPacketProcessor::process(const DepthPacket &packet)
975948
impl_->newDepthFrame();
976949
}
977950

978-
impl_->stopTiming();
951+
impl_->stopTiming(LOG_INFO);
979952
}
980953

981954
} /* namespace libfreenect2 */

examples/protonect/src/logging.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@
3030
#include <string>
3131
#include <algorithm>
3232

33+
#ifdef LIBFREENECT2_WITH_CXX11_SUPPORT
34+
#include <chrono>
35+
#endif
36+
37+
#ifdef LIBFREENECT2_WITH_OPENGL_SUPPORT
38+
#include <GLFW/glfw3.h>
39+
#endif
40+
3341
namespace libfreenect2
3442
{
3543
Logger::~Logger() {}
@@ -144,6 +152,98 @@ void setGlobalLogger(Logger *logger)
144152
userLogger_ = logger;
145153
}
146154

155+
class Timer
156+
{
157+
public:
158+
double duration;
159+
size_t count;
160+
161+
Timer()
162+
{
163+
reset();
164+
}
165+
166+
void reset()
167+
{
168+
duration = 0;
169+
count = 0;
170+
}
171+
172+
#ifdef LIBFREENECT2_WITH_CXX11_SUPPORT
173+
std::chrono::time_point<std::chrono::high_resolution_clock> time_start;
174+
175+
void start()
176+
{
177+
time_start = std::chrono::high_resolution_clock::now();
178+
}
179+
180+
void stop()
181+
{
182+
duration += std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::high_resolution_clock::now() - time_start).count();
183+
count++;
184+
}
185+
#elif defined(LIBFREENECT2_WITH_OPENGL_SUPPORT)
186+
double time_start;
187+
188+
void start()
189+
{
190+
time_start = glfwGetTime();
191+
}
192+
193+
void stop()
194+
{
195+
duration += glfwGetTime() - time_start;
196+
count++;
197+
}
198+
#else
199+
void start()
200+
{
201+
}
202+
203+
void stop()
204+
{
205+
}
206+
#endif
207+
};
208+
209+
class WithPerfLoggingImpl: public Timer
210+
{
211+
public:
212+
std::ostream &stop(std::ostream &stream)
213+
{
214+
Timer::stop();
215+
if (count < 100)
216+
{
217+
stream.setstate(std::ios::eofbit);
218+
return stream;
219+
}
220+
double avg = duration / count;
221+
reset();
222+
stream << "avg. time: " << (avg * 1000) << "ms -> ~" << (1.0/avg) << "Hz";
223+
return stream;
224+
}
225+
};
226+
227+
WithPerfLogging::WithPerfLogging()
228+
:impl_(new WithPerfLoggingImpl)
229+
{
230+
}
231+
232+
WithPerfLogging::~WithPerfLogging()
233+
{
234+
delete impl_;
235+
}
236+
237+
void WithPerfLogging::startTiming()
238+
{
239+
impl_->start();
240+
}
241+
242+
std::ostream &WithPerfLogging::stopTiming(std::ostream &stream)
243+
{
244+
impl_->stop(stream);
245+
}
246+
147247
std::string getShortName(const char *func)
148248
{
149249
std::string src(func);

examples/protonect/src/opencl_depth_packet_processor.cpp

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ std::string loadCLSource(const std::string &filename)
6565
return std::string(reinterpret_cast<const char *>(data), length);
6666
}
6767

68-
class OpenCLDepthPacketProcessorImpl
68+
class OpenCLDepthPacketProcessorImpl: public WithPerfLogging
6969
{
7070
public:
7171
cl_short lut11to16[2048];
@@ -75,11 +75,6 @@ class OpenCLDepthPacketProcessorImpl
7575
libfreenect2::DepthPacketProcessor::Config config;
7676
DepthPacketProcessor::Parameters params;
7777

78-
double timing_acc;
79-
double timing_acc_n;
80-
81-
Timer timer;
82-
8378
Frame *ir_frame, *depth_frame;
8479

8580
cl::Context context;
@@ -144,8 +139,6 @@ class OpenCLDepthPacketProcessorImpl
144139
newIrFrame();
145140
newDepthFrame();
146141

147-
timing_acc = 0.0;
148-
timing_acc_n = 0.0;
149142
image_size = 512 * 424;
150143

151144
deviceInitialized = initDevice(deviceId);
@@ -536,25 +529,6 @@ class OpenCLDepthPacketProcessorImpl
536529
return true;
537530
}
538531

539-
void startTiming()
540-
{
541-
timer.start();
542-
}
543-
544-
void stopTiming()
545-
{
546-
timing_acc += timer.stop();
547-
timing_acc_n += 1.0;
548-
549-
if(timing_acc_n >= 100.0)
550-
{
551-
double avg = (timing_acc / timing_acc_n);
552-
LOG_INFO << "[OpenCLDepthPacketProcessor] avg. time: " << (avg * 1000) << "ms -> ~" << (1.0 / avg) << "Hz";
553-
timing_acc = 0.0;
554-
timing_acc_n = 0.0;
555-
}
556-
}
557-
558532
void newIrFrame()
559533
{
560534
ir_frame = new Frame(512, 424, 4);
@@ -673,7 +647,7 @@ void OpenCLDepthPacketProcessor::process(const DepthPacket &packet)
673647

674648
impl_->run(packet);
675649

676-
impl_->stopTiming();
650+
impl_->stopTiming(LOG_INFO);
677651

678652
if(has_listener)
679653
{

examples/protonect/src/opengl_depth_packet_processor.cpp

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ struct Texture : public WithOpenGLBindings
330330
}
331331
};
332332

333-
struct OpenGLDepthPacketProcessorImpl : public WithOpenGLBindings
333+
struct OpenGLDepthPacketProcessorImpl : public WithOpenGLBindings, public WithPerfLogging
334334
{
335335
GLFWwindow *opengl_context_ptr;
336336
std::string shader_folder;
@@ -364,11 +364,6 @@ struct OpenGLDepthPacketProcessorImpl : public WithOpenGLBindings
364364
DepthPacketProcessor::Parameters params;
365365
bool params_need_update;
366366

367-
double timing_acc;
368-
double timing_acc_n;
369-
370-
Timer timer;
371-
372367
bool do_debug;
373368

374369
struct Vertex
@@ -387,8 +382,6 @@ struct OpenGLDepthPacketProcessorImpl : public WithOpenGLBindings
387382
stage2_framebuffer(0),
388383
filter2_framebuffer(0),
389384
params_need_update(true),
390-
timing_acc(0),
391-
timing_acc_n(0),
392385
do_debug(debug)
393386
{
394387
}
@@ -441,25 +434,6 @@ struct OpenGLDepthPacketProcessorImpl : public WithOpenGLBindings
441434
debug.gl(b);
442435
}
443436

444-
void startTiming()
445-
{
446-
timer.start();
447-
}
448-
449-
void stopTiming()
450-
{
451-
timing_acc += timer.stop();
452-
timing_acc_n += 1.0;
453-
454-
if(timing_acc_n >= 100.0)
455-
{
456-
double avg = (timing_acc / timing_acc_n);
457-
LOG_INFO << "[OpenGLDepthPacketProcessor] avg. time: " << (avg * 1000) << "ms -> ~" << (1.0/avg) << "Hz";
458-
timing_acc = 0.0;
459-
timing_acc_n = 0.0;
460-
}
461-
}
462-
463437
void initialize()
464438
{
465439
ChangeCurrentOpenGLContext ctx(opengl_context_ptr);
@@ -946,7 +920,7 @@ void OpenGLDepthPacketProcessor::process(const DepthPacket &packet)
946920

947921
if(impl_->do_debug) glfwSwapBuffers(impl_->opengl_context_ptr);
948922

949-
impl_->stopTiming();
923+
impl_->stopTiming(LOG_INFO);
950924

951925
if(has_listener)
952926
{

0 commit comments

Comments
 (0)