1
#pragma once
2

            
3
#include <string>
4

            
5
#include "absl/status/statusor.h"
6

            
7
// Profiling support is provided in the release tcmalloc of `gperftools`, but not in the library
8
// that supplies the debug tcmalloc. So all the profiling code must be ifdef'd
9
// on PROFILER_AVAILABLE which is dependent on those two settings.
10
#if defined(GPERFTOOLS_TCMALLOC) && !defined(ENVOY_MEMORY_DEBUG_ENABLED)
11
#define PROFILER_AVAILABLE
12
#endif
13

            
14
namespace Envoy {
15
namespace Profiler {
16

            
17
/**
18
 * Process wide CPU profiling.
19
 */
20
class Cpu {
21
public:
22
  /**
23
   * @return whether the profiler is enabled or not.
24
   */
25
  static bool profilerEnabled();
26

            
27
  /**
28
   * Start the profiler and write to the specified path.
29
   * @return bool whether the call to start the profiler succeeded.
30
   */
31
  static bool startProfiler(const std::string& output_path);
32

            
33
  /**
34
   * Stop the profiler.
35
   */
36
  static void stopProfiler();
37
};
38

            
39
/**
40
 * Process wide heap profiling
41
 */
42
class Heap {
43
public:
44
  /**
45
   * @return whether the profiler is enabled in this build or not.
46
   */
47
  static bool profilerEnabled();
48

            
49
  /**
50
   * @return whether the profiler is started or not
51
   */
52
  static bool isProfilerStarted();
53

            
54
  /**
55
   * Start the profiler and write to the specified path.
56
   * @return bool whether the call to start the profiler succeeded.
57
   */
58
  static bool startProfiler(const std::string& output_path);
59

            
60
  /**
61
   * Stop the profiler.
62
   * @return bool whether the file is dumped
63
   */
64
  static bool stopProfiler();
65
};
66

            
67
/**
68
 * Default profiler which will be enabled when tcmalloc (not `gperftools`) is used.
69
 * This class is not thread-safe.
70
 */
71
class TcmallocProfiler {
72
public:
73
  TcmallocProfiler() = default;
74

            
75
  static absl::StatusOr<std::string> tcmallocHeapProfile();
76
  static absl::Status startAllocationProfile();
77
  static absl::StatusOr<std::string> stopAllocationProfile();
78
};
79

            
80
} // namespace Profiler
81
} // namespace Envoy