common_mem_prof/jemalloc/
error.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
15use std::any::Any;
16use std::path::PathBuf;
17
18use common_error::ext::{BoxedError, ErrorExt};
19use common_error::status_code::StatusCode;
20use common_macro::stack_trace_debug;
21use snafu::{Location, Snafu};
22
23#[derive(Snafu)]
24#[snafu(visibility(pub))]
25#[stack_trace_debug]
26pub enum Error {
27    #[snafu(display("Failed to read OPT_PROF"))]
28    ReadOptProf {
29        #[snafu(source)]
30        error: tikv_jemalloc_ctl::Error,
31    },
32
33    #[snafu(display("Memory profiling is not enabled"))]
34    ProfilingNotEnabled,
35
36    #[snafu(display("Failed to build temp file from given path: {:?}", path))]
37    BuildTempPath {
38        path: PathBuf,
39        #[snafu(implicit)]
40        location: Location,
41    },
42
43    #[snafu(display("Failed to open temp file: {}", path))]
44    OpenTempFile {
45        path: String,
46        #[snafu(source)]
47        error: std::io::Error,
48    },
49
50    #[snafu(display("Failed to dump profiling data to temp file: {:?}", path))]
51    DumpProfileData {
52        path: PathBuf,
53        #[snafu(source)]
54        error: tikv_jemalloc_ctl::Error,
55    },
56
57    #[snafu(display("Failed to activate heap profiling"))]
58    ActivateProf {
59        #[snafu(source)]
60        error: tikv_jemalloc_ctl::Error,
61    },
62
63    #[snafu(display("Failed to deactivate heap profiling"))]
64    DeactivateProf {
65        #[snafu(source)]
66        error: tikv_jemalloc_ctl::Error,
67    },
68
69    #[snafu(display("Failed to read heap profiling status"))]
70    ReadProfActive {
71        #[snafu(source)]
72        error: tikv_jemalloc_ctl::Error,
73    },
74
75    #[snafu(display("Failed to read jemalloc gdump flag"))]
76    ReadGdump {
77        #[snafu(source)]
78        error: tikv_jemalloc_ctl::Error,
79    },
80
81    #[snafu(display("Failed to update jemalloc gdump flag"))]
82    UpdateGdump {
83        #[snafu(source)]
84        error: tikv_jemalloc_ctl::Error,
85    },
86}
87
88impl ErrorExt for Error {
89    fn status_code(&self) -> StatusCode {
90        match self {
91            Error::ReadOptProf { .. } => StatusCode::Internal,
92            Error::ProfilingNotEnabled => StatusCode::InvalidArguments,
93            Error::BuildTempPath { .. } => StatusCode::Internal,
94            Error::OpenTempFile { .. } => StatusCode::StorageUnavailable,
95            Error::DumpProfileData { .. } => StatusCode::StorageUnavailable,
96            Error::ActivateProf { .. } => StatusCode::Internal,
97            Error::DeactivateProf { .. } => StatusCode::Internal,
98            Error::ReadProfActive { .. } => StatusCode::Internal,
99            Error::ReadGdump { .. } => StatusCode::Internal,
100            Error::UpdateGdump { .. } => StatusCode::Internal,
101        }
102    }
103
104    fn as_any(&self) -> &dyn Any {
105        self
106    }
107}
108
109impl From<Error> for crate::error::Error {
110    fn from(e: Error) -> Self {
111        Self::Internal {
112            source: BoxedError::new(e),
113        }
114    }
115}