profiler.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef _WIN32
4 #include "windows.h"
5 #include "psapi.h"
6 #else
7 #endif
8 
9 
10 float mb_in_use()
11 {
12  float mem = 0;
13 #ifdef _WIN32
14  PROCESS_MEMORY_COUNTERS_EX pmc;
15  GetProcessMemoryInfo( GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS *)&pmc, sizeof( pmc ) );
16 
17  mem = float( pmc.WorkingSetSize / (1024. * 1024.) );
18 #else
19 #endif
20  return mem;
21 }
23 {
24  float _baseline;
25  std::atomic< float > _peak{ 0 };
26  std::atomic_bool _alive{ true };
27 
28  std::thread _th;
29 
30  // section
31  float _s_start = 0.f;
32  std::atomic< float > _s_peak;
33 
34 public:
36  : _baseline( mb_in_use() )
37  {
38  _th = std::thread( [&]() {
39  while( _alive )
40  {
41  auto const mb = mb_in_use();
42  _peak = std::max( mb, (float)_peak );
43  _s_peak = std::max( mb, (float)_s_peak );
44  std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
45  }
46  } );
47  }
48 
49  void section( char const * heading )
50  {
51  _s_start = mb_in_use();
52  _s_peak = _s_start;
53  TRACE( "\n\n-P------------ " << heading << ":" );
54  }
55 
56  void section_end()
57  {
58  auto const mb = mb_in_use();
59  _s_peak = std::max( mb, (float)_s_peak );
60  TRACE( "-P------------ " << _s_start << " --> " << mb << " / " << _s_peak
61  << " MB delta " << (mb - _s_start) << " / "
62  << (_s_peak - _s_start) << " peak\n" );
63  }
64 
66  {
67  stop();
68 
69  auto const mb = mb_in_use();
70  TRACE( "\n-P------------ FINAL: " << mb << " / " << _peak << " peak (" << _baseline
71  << " baseline)" );
72  }
73 
74  void snapshot( char const * desc )
75  {
76  TRACE( "-P------------ " << desc << ": " << mb_in_use() << " / " << (float)_peak << " peak" );
77  }
78 
79  void stop()
80  {
81  if( _alive )
82  {
83  _alive = false;
84  _th.join();
85  }
86  }
87 
88  float get_peak() const { return _peak; }
89  float get_baseline() const { return _baseline; }
90 };
std::atomic< float > _s_peak
Definition: profiler.h:32
void section_end()
Definition: profiler.h:56
std::atomic_bool _alive
Definition: profiler.h:26
std::atomic< float > _peak
Definition: profiler.h:25
float get_peak() const
Definition: profiler.h:88
float _s_start
Definition: profiler.h:31
float _baseline
Definition: profiler.h:24
float get_baseline() const
Definition: profiler.h:89
float mb_in_use()
Definition: profiler.h:10
void section(char const *heading)
Definition: profiler.h:49
std::thread _th
Definition: profiler.h:28
void stop()
Definition: profiler.h:79
void snapshot(char const *desc)
Definition: profiler.h:74


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:39