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
76impl ErrorExt for Error {
77    fn status_code(&self) -> StatusCode {
78        match self {
79            Error::ReadOptProf { .. } => StatusCode::Internal,
80            Error::ProfilingNotEnabled => StatusCode::InvalidArguments,
81            Error::BuildTempPath { .. } => StatusCode::Internal,
82            Error::OpenTempFile { .. } => StatusCode::StorageUnavailable,
83            Error::DumpProfileData { .. } => StatusCode::StorageUnavailable,
84            Error::ActivateProf { .. } => StatusCode::Internal,
85            Error::DeactivateProf { .. } => StatusCode::Internal,
86            Error::ReadProfActive { .. } => StatusCode::Internal,
87        }
88    }
89
90    fn as_any(&self) -> &dyn Any {
91        self
92    }
93}
94
95impl From<Error> for crate::error::Error {
96    fn from(e: Error) -> Self {
97        Self::Internal {
98            source: BoxedError::new(e),
99        }
100    }
101}