diff --git a/source/common/profiler/profiler.cc b/source/common/profiler/profiler.cc index 973b243a7040..64ad6d1e8798 100644 --- a/source/common/profiler/profiler.cc +++ b/source/common/profiler/profiler.cc @@ -68,20 +68,21 @@ bool Heap::stopProfiler() { return false; } namespace Envoy { namespace Profiler { -static absl::optional alloc_profiler = - absl::nullopt; +static tcmalloc::MallocExtension::AllocationProfilingToken* alloc_profiler = nullptr; absl::StatusOr TcmallocProfiler::tcmallocHeapProfile() { auto profile = tcmalloc::MallocExtension::SnapshotCurrent(tcmalloc::ProfileType::kHeap); return tcmalloc::Marshal(profile); } -bool TcmallocProfiler::startAllocationProfile() { - if (alloc_profiler) { - return false; +absl::Status TcmallocProfiler::startAllocationProfile() { + if (alloc_profiler != nullptr) { + return absl::Status(absl::StatusCode::kFailedPrecondition, + "Allocation profiler has already started"); } - alloc_profiler = tcmalloc::MallocExtension::StartAllocationProfiling(); - return true; + alloc_profiler = new tcmalloc::MallocExtension::AllocationProfilingToken( + tcmalloc::MallocExtension::StartAllocationProfiling()); + return absl::OkStatus(); } absl::StatusOr TcmallocProfiler::stopAllocationProfile() { @@ -89,9 +90,10 @@ absl::StatusOr TcmallocProfiler::stopAllocationProfile() { return absl::Status(absl::StatusCode::kFailedPrecondition, "Allocation profiler is not started"); } - const auto profile = std::move(alloc_profiler.value()).Stop(); + const auto profile = std::move(*alloc_profiler).Stop(); const auto result = tcmalloc::Marshal(profile); - alloc_profiler = absl::nullopt; + delete alloc_profiler; + alloc_profiler = nullptr; return result; } @@ -108,12 +110,14 @@ absl::StatusOr TcmallocProfiler::tcmallocHeapProfile() { "Heap profile is not implemented in current build"); } -bool TcmallocProfiler::startAllocationProfile() { return false; } - -absl::StatusOr TcmallocProfiler::stopAllocationProfile() { +absl::Status TcmallocProfiler::startAllocationProfile() { return absl::Status(absl::StatusCode::kUnimplemented, "Allocation profile is not implemented in current build"); -} + + absl::StatusOr TcmallocProfiler::stopAllocationProfile() { + return absl::Status(absl::StatusCode::kUnimplemented, + "Allocation profile is not implemented in current build"); + } } // namespace Profiler } // namespace Envoy diff --git a/source/common/profiler/profiler.h b/source/common/profiler/profiler.h index e2380d6e4579..11363789f00a 100644 --- a/source/common/profiler/profiler.h +++ b/source/common/profiler/profiler.h @@ -73,7 +73,7 @@ class TcmallocProfiler { TcmallocProfiler() = default; static absl::StatusOr tcmallocHeapProfile(); - static bool startAllocationProfile(); + static absl::Status startAllocationProfile(); static absl::StatusOr stopAllocationProfile(); }; diff --git a/source/server/admin/profiling_handler.cc b/source/server/admin/profiling_handler.cc index ad64d50ca268..82a5ef87bfb9 100644 --- a/source/server/admin/profiling_handler.cc +++ b/source/server/admin/profiling_handler.cc @@ -105,8 +105,9 @@ Http::Code TcmallocProfilingHandler::handlerAllocationProfiler(Http::ResponseHea } const bool enable = enableVal.value() == "y"; if (enable) { - const bool started = Profiler::TcmallocProfiler::startAllocationProfile(); - if (!started) { + const auto started = Profiler::TcmallocProfiler::startAllocationProfile(); + if (!started.ok()) { + response.add(started.message()); return Http::Code::BadRequest; } response.add("OK\n");