Line data Source code
1 : #include "source/common/profiler/profiler.h" 2 : 3 : #include <string> 4 : 5 : #ifdef PROFILER_AVAILABLE 6 : 7 : #include "gperftools/heap-profiler.h" 8 : #include "gperftools/profiler.h" 9 : 10 : namespace Envoy { 11 : namespace Profiler { 12 : 13 0 : bool Cpu::profilerEnabled() { return ProfilingIsEnabledForAllThreads(); } 14 : 15 0 : bool Cpu::startProfiler(const std::string& output_path) { 16 0 : return ProfilerStart(output_path.c_str()); 17 0 : } 18 : 19 0 : void Cpu::stopProfiler() { ProfilerStop(); } 20 : 21 0 : bool Heap::profilerEnabled() { 22 : // determined by PROFILER_AVAILABLE 23 0 : return true; 24 0 : } 25 : 26 0 : bool Heap::isProfilerStarted() { return IsHeapProfilerRunning(); } 27 0 : bool Heap::startProfiler(const std::string& output_file_name_prefix) { 28 0 : HeapProfilerStart(output_file_name_prefix.c_str()); 29 0 : return true; 30 0 : } 31 : 32 0 : bool Heap::stopProfiler() { 33 0 : if (!IsHeapProfilerRunning()) { 34 0 : return false; 35 0 : } 36 0 : HeapProfilerDump("stop and dump"); 37 0 : HeapProfilerStop(); 38 0 : return true; 39 0 : } 40 : 41 : } // namespace Profiler 42 : } // namespace Envoy 43 : 44 : #else 45 : 46 : namespace Envoy { 47 : namespace Profiler { 48 : 49 : bool Cpu::profilerEnabled() { return false; } 50 : bool Cpu::startProfiler(const std::string&) { return false; } 51 : void Cpu::stopProfiler() {} 52 : 53 : bool Heap::profilerEnabled() { return false; } 54 : bool Heap::isProfilerStarted() { return false; } 55 : bool Heap::startProfiler(const std::string&) { return false; } 56 : bool Heap::stopProfiler() { return false; } 57 : 58 : } // namespace Profiler 59 : } // namespace Envoy 60 : 61 : #endif // #ifdef PROFILER_AVAILABLE 62 : 63 : #ifdef TCMALLOC 64 : 65 : #include "tcmalloc/malloc_extension.h" 66 : #include "tcmalloc/profile_marshaler.h" 67 : 68 : namespace Envoy { 69 : namespace Profiler { 70 : 71 : absl::StatusOr<std::string> TcmallocProfiler::tcmallocHeapProfile() { 72 : auto profile = tcmalloc::MallocExtension::SnapshotCurrent(tcmalloc::ProfileType::kHeap); 73 : return tcmalloc::Marshal(profile); 74 : } 75 : 76 : } // namespace Profiler 77 : } // namespace Envoy 78 : 79 : #else 80 : 81 : namespace Envoy { 82 : namespace Profiler { 83 : 84 0 : absl::StatusOr<std::string> TcmallocProfiler::tcmallocHeapProfile() { 85 0 : return absl::Status(absl::StatusCode::kUnimplemented, 86 0 : "Heap profile is not implemented in current build"); 87 0 : } 88 : 89 : } // namespace Profiler 90 : } // namespace Envoy 91 : 92 : #endif // #ifdef TCMALLOC