servers/http/
mem_prof.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#[cfg(feature = "mem-prof")]
16use axum::extract::Query;
17use axum::http::StatusCode;
18use axum::response::IntoResponse;
19use serde::{Deserialize, Serialize};
20
21/// Output format.
22#[derive(Debug, Default, Serialize, Deserialize)]
23#[serde(rename_all = "snake_case")]
24pub enum Output {
25    /// google’s pprof format report in protobuf.
26    Proto,
27    /// jeheap text format. Define by jemalloc.
28    #[default]
29    Text,
30    /// svg flamegraph.
31    Flamegraph,
32}
33
34#[derive(Default, Serialize, Deserialize, Debug)]
35#[serde(default)]
36pub struct MemPprofQuery {
37    output: Output,
38}
39
40#[cfg(feature = "mem-prof")]
41#[axum_macros::debug_handler]
42pub async fn mem_prof_handler(
43    Query(req): Query<MemPprofQuery>,
44) -> crate::error::Result<impl IntoResponse> {
45    use snafu::ResultExt;
46
47    use crate::error::DumpProfileDataSnafu;
48
49    let dump = match req.output {
50        Output::Proto => common_mem_prof::dump_pprof().await,
51        Output::Text => common_mem_prof::dump_profile().await,
52        Output::Flamegraph => common_mem_prof::dump_flamegraph().await,
53    }
54    .context(DumpProfileDataSnafu)?;
55
56    Ok((StatusCode::OK, dump))
57}
58
59#[cfg(feature = "mem-prof")]
60#[axum_macros::debug_handler]
61pub async fn activate_heap_prof_handler() -> crate::error::Result<impl IntoResponse> {
62    use snafu::ResultExt;
63
64    use crate::error::DumpProfileDataSnafu;
65
66    common_mem_prof::activate_heap_profile().context(DumpProfileDataSnafu)?;
67
68    Ok((StatusCode::OK, "Heap profiling activated"))
69}
70
71#[cfg(feature = "mem-prof")]
72#[axum_macros::debug_handler]
73pub async fn deactivate_heap_prof_handler() -> crate::error::Result<impl IntoResponse> {
74    use snafu::ResultExt;
75
76    use crate::error::DumpProfileDataSnafu;
77
78    common_mem_prof::deactivate_heap_profile().context(DumpProfileDataSnafu)?;
79
80    Ok((StatusCode::OK, "Heap profiling deactivated"))
81}
82
83#[cfg(not(feature = "mem-prof"))]
84#[axum_macros::debug_handler]
85pub async fn mem_prof_handler() -> crate::error::Result<impl IntoResponse> {
86    Ok((
87        StatusCode::NOT_IMPLEMENTED,
88        "The 'mem-prof' feature is disabled",
89    ))
90}
91
92#[cfg(not(feature = "mem-prof"))]
93#[axum_macros::debug_handler]
94pub async fn activate_heap_prof_handler() -> crate::error::Result<impl IntoResponse> {
95    Ok((
96        StatusCode::NOT_IMPLEMENTED,
97        "The 'mem-prof' feature is disabled",
98    ))
99}
100
101#[cfg(feature = "mem-prof")]
102#[axum_macros::debug_handler]
103pub async fn heap_prof_status_handler() -> crate::error::Result<impl IntoResponse> {
104    use snafu::ResultExt;
105
106    use crate::error::DumpProfileDataSnafu;
107
108    let is_active = common_mem_prof::is_heap_profile_active().context(DumpProfileDataSnafu)?;
109
110    Ok((StatusCode::OK, format!("{{\"active\": {}}}", is_active)))
111}
112
113#[cfg(not(feature = "mem-prof"))]
114#[axum_macros::debug_handler]
115pub async fn deactivate_heap_prof_handler() -> crate::error::Result<impl IntoResponse> {
116    Ok((
117        StatusCode::NOT_IMPLEMENTED,
118        "The 'mem-prof' feature is disabled",
119    ))
120}
121
122#[cfg(not(feature = "mem-prof"))]
123#[axum_macros::debug_handler]
124pub async fn heap_prof_status_handler() -> crate::error::Result<impl IntoResponse> {
125    Ok((
126        StatusCode::NOT_IMPLEMENTED,
127        "The 'mem-prof' feature is disabled",
128    ))
129}