cpu_utils/
errors.rs

1/*
2 * Copyright 2024 Fluence DAO
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use thiserror::Error as ThisError;
18
19use crate::LogicalCoreId;
20use crate::PhysicalCoreId;
21
22#[derive(Debug, ThisError)]
23pub enum CPUTopologyError {
24    #[error(transparent)]
25    RawHwlocError(#[from] hwlocality::errors::RawHwlocError),
26
27    #[error(transparent)]
28    TypeToDepthError(#[from] hwlocality::object::depth::TypeToDepthError),
29
30    #[error(transparent)]
31    CPUBindingError(#[from] hwlocality::cpu::binding::CpuBindingError),
32
33    #[error("topology allocation failed, probably not enough free memory")]
34    TopologyAllocationFailed,
35
36    #[error("physical core id {core_id} is too big to be represented as a signed int")]
37    LogicalCoreIdTooBig { core_id: LogicalCoreId },
38
39    #[error("physical cores not found")]
40    PhysicalCoresNotFound,
41
42    #[error("physical core with {core_id} id not found")]
43    PhysicalCoreNotFound { core_id: PhysicalCoreId },
44
45    #[error("logical cores for physical core with {core_id} id not found")]
46    LogicalCoresNotFound { core_id: PhysicalCoreId },
47
48    #[error("cpuset for a physical core with {core_id} not found")]
49    CPUSetNotFound { core_id: PhysicalCoreId },
50}
51
52impl CPUTopologyError {
53    pub fn physical_core_not_found(core_id: PhysicalCoreId) -> Self {
54        Self::PhysicalCoreNotFound { core_id }
55    }
56
57    pub fn logical_cores_not_found(core_id: PhysicalCoreId) -> Self {
58        Self::PhysicalCoreNotFound { core_id }
59    }
60
61    pub fn cpuset_not_found(core_id: PhysicalCoreId) -> Self {
62        Self::CPUSetNotFound { core_id }
63    }
64
65    pub fn logical_core_too_big(core_id: LogicalCoreId) -> Self {
66        Self::LogicalCoreIdTooBig { core_id }
67    }
68}