Skip to content

Commit 8efb59b

Browse files
committed
logging: Add an option to collect profiling
Use cmake -DENABLE_PROFILING=ON (OFF by default).
1 parent a7a420c commit 8efb59b

4 files changed

Lines changed: 67 additions & 6 deletions

File tree

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ OPTION(ENABLE_CUDA "Enable CUDA support" ON)
3636
OPTION(ENABLE_OPENGL "Enable OpenGL support" ON)
3737
OPTION(ENABLE_VAAPI "Enable VA-API support" ON)
3838
OPTION(ENABLE_TEGRAJPEG "Enable Tegra HW JPEG support" ON)
39+
OPTION(ENABLE_PROFILING "Collect profiling stats (memory consuming)" OFF)
40+
41+
IF(ENABLE_PROFILING)
42+
SET(LIBFREENECT2_WITH_PROFILING 1)
43+
ENDIF()
3944

4045
IF(MSVC)
4146
# suppress several "possible loss of data" warnings, and

include/libfreenect2/config.h.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,6 @@
6161

6262
#cmakedefine LIBFREENECT2_WITH_CXX11_SUPPORT
6363

64+
#cmakedefine LIBFREENECT2_WITH_PROFILING
65+
6466
#endif // LIBFREENECT2_CONFIG_H

src/logging.cpp

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@
3232
#include <string>
3333
#include <algorithm>
3434

35+
#ifdef LIBFREENECT2_WITH_PROFILING
36+
#include <vector>
37+
#include <numeric>
38+
#include <functional>
39+
#include <cmath>
40+
#endif
41+
3542
#ifdef LIBFREENECT2_WITH_CXX11_SUPPORT
3643
#include <chrono>
3744
#endif
@@ -192,6 +199,9 @@ class Timer
192199

193200
Timer()
194201
{
202+
#if defined(LIBFREENECT2_WITH_OPENGL_SUPPORT)
203+
glfwInit();
204+
#endif
195205
reset();
196206
}
197207

@@ -209,10 +219,13 @@ class Timer
209219
time_start = std::chrono::high_resolution_clock::now();
210220
}
211221

212-
void stop()
222+
double stop()
213223
{
214-
duration += std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::high_resolution_clock::now() - time_start).count();
224+
auto time_diff = std::chrono::high_resolution_clock::now() - time_start;
225+
double this_duration = std::chrono::duration_cast<std::chrono::duration<double>>(time_diff).count();
226+
duration += this_duration;
215227
count++;
228+
return this_duration;
216229
}
217230
#elif defined(LIBFREENECT2_WITH_OPENGL_SUPPORT)
218231
double time_start;
@@ -222,28 +235,68 @@ class Timer
222235
time_start = glfwGetTime();
223236
}
224237

225-
void stop()
238+
double stop()
226239
{
227-
duration += glfwGetTime() - time_start;
240+
double this_duration = glfwGetTime() - time_start;
241+
duration += this_duration;
228242
count++;
243+
return this_duration;
229244
}
230245
#else
231246
void start()
232247
{
233248
}
234249

235-
void stop()
250+
double stop()
236251
{
252+
return 0;
237253
}
238254
#endif
239255
};
240256

241257
class WithPerfLoggingImpl: public Timer
242258
{
243259
public:
260+
#ifdef LIBFREENECT2_WITH_PROFILING
261+
std::vector<double> stats;
262+
std::string name;
263+
264+
WithPerfLoggingImpl()
265+
{
266+
stats.reserve(30*100);
267+
}
268+
269+
~WithPerfLoggingImpl()
270+
{
271+
if (stats.size() < 2)
272+
return;
273+
std::vector<double> &v = stats;
274+
std::sort(v.begin(), v.end());
275+
double sum = std::accumulate(v.begin(), v.end(), 0.0);
276+
size_t n = v.size();
277+
double mean = sum / n;
278+
std::vector<double> diff(n);
279+
std::transform(v.begin(), v.end(), diff.begin(), std::bind2nd(std::minus<double>(), mean));
280+
double sqsum = std::inner_product(diff.begin(), diff.end(), diff.begin(), 0.0);
281+
double std = std::sqrt(sqsum / (n-1));
282+
283+
std::cout << name << v[0] << " " << v[n/20] << " " << v[n/2] << " " << v[n - (n+19)/20] << " " << v[n-1] << " mean=" << mean << " std=" << std << " n=" << n << std::endl;
284+
}
285+
#endif
286+
244287
std::ostream &stop(std::ostream &stream)
245288
{
289+
#ifndef LIBFREENECT2_WITH_PROFILING
246290
Timer::stop();
291+
#else
292+
double this_duration = Timer::stop();
293+
if (name.empty())
294+
{
295+
std::stringstream &ss = static_cast<std::stringstream &>(stream);
296+
name = ss.str();
297+
}
298+
stats.push_back(this_duration*1e3);
299+
#endif
247300
if (count < 100)
248301
{
249302
stream.setstate(std::ios::eofbit);

src/turbo_jpeg_rgb_packet_processor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ void TurboJpegRgbPacketProcessor::process(const RgbPacket &packet)
9696

9797
int r = tjDecompress2(impl_->decompressor, packet.jpeg_buffer, packet.jpeg_buffer_length, impl_->frame->data, 1920, 1920 * tjPixelSize[TJPF_BGRX], 1080, TJPF_BGRX, 0);
9898

99+
impl_->stopTiming(LOG_INFO);
100+
99101
if(r == 0)
100102
{
101103
if(listener_->onNewFrame(Frame::Color, impl_->frame))
@@ -108,7 +110,6 @@ void TurboJpegRgbPacketProcessor::process(const RgbPacket &packet)
108110
LOG_ERROR << "Failed to decompress rgb image! TurboJPEG error: '" << tjGetErrorStr() << "'";
109111
}
110112

111-
impl_->stopTiming(LOG_INFO);
112113
}
113114
}
114115

0 commit comments

Comments
 (0)