Skip to main content

nv_redfish/computer_system/
processor.rs

1// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
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//! Processor and its configuration.
17
18use crate::schema::redfish::processor::Processor as ProcessorSchema;
19use crate::schema::redfish::processor_metrics::ProcessorMetrics;
20use crate::Error;
21use crate::NvBmc;
22use crate::Resource;
23use crate::ResourceSchema;
24use nv_redfish_core::Bmc;
25use nv_redfish_core::NavProperty;
26use std::sync::Arc;
27
28#[cfg(feature = "sensors")]
29use crate::extract_sensor_uris;
30#[cfg(feature = "sensors")]
31use crate::sensor::extract_environment_sensors;
32#[cfg(feature = "sensors")]
33use crate::sensor::SensorRef;
34
35/// Represents a processor in a computer system.
36///
37/// Provides access to processor information and associated metrics/sensors.
38pub struct Processor<B: Bmc> {
39    bmc: NvBmc<B>,
40    data: Arc<ProcessorSchema>,
41}
42
43impl<B: Bmc> Processor<B> {
44    /// Create a new processor handle.
45    pub(crate) async fn new(
46        bmc: &NvBmc<B>,
47        nav: &NavProperty<ProcessorSchema>,
48    ) -> Result<Self, Error<B>> {
49        nav.get(bmc.as_ref())
50            .await
51            .map_err(crate::Error::Bmc)
52            .map(|data| Self {
53                bmc: bmc.clone(),
54                data,
55            })
56    }
57
58    /// Get the raw schema data for this processor.
59    ///
60    /// Returns an `Arc` to the underlying schema, allowing cheap cloning
61    /// and sharing of the data.
62    #[must_use]
63    pub fn raw(&self) -> Arc<ProcessorSchema> {
64        self.data.clone()
65    }
66
67    /// Get processor metrics.
68    ///
69    /// Returns the processor's performance and state metrics if available.
70    ///
71    /// # Errors
72    ///
73    /// Returns an error if:
74    /// - The processor does not have metrics
75    /// - Fetching metrics data fails
76    pub async fn metrics(&self) -> Result<Option<Arc<ProcessorMetrics>>, Error<B>> {
77        if let Some(metrics_ref) = &self.data.metrics {
78            metrics_ref
79                .get(self.bmc.as_ref())
80                .await
81                .map_err(Error::Bmc)
82                .map(Some)
83        } else {
84            Ok(None)
85        }
86    }
87
88    /// Get the environment sensors for this processor.
89    ///
90    /// Returns a vector of `Sensor<B>` obtained from environment metrics, if available.
91    ///
92    /// # Errors
93    ///
94    /// Returns an error if get of environment metrics failed.
95    #[cfg(feature = "sensors")]
96    pub async fn environment_sensors(&self) -> Result<Vec<SensorRef<B>>, Error<B>> {
97        let sensor_refs = if let Some(env_ref) = &self.data.environment_metrics {
98            extract_environment_sensors(env_ref, self.bmc.as_ref()).await?
99        } else {
100            Vec::new()
101        };
102
103        Ok(sensor_refs
104            .into_iter()
105            .map(|r| SensorRef::new(self.bmc.clone(), r))
106            .collect())
107    }
108
109    /// Get the metrics sensors for this processor.
110    ///
111    /// Returns a vector of `Sensor<B>` obtained from metrics metrics, if available.
112    ///
113    /// # Errors
114    ///
115    /// Returns an error if get of metrics failed.
116    #[cfg(feature = "sensors")]
117    pub async fn metrics_sensors(&self) -> Result<Vec<SensorRef<B>>, Error<B>> {
118        let sensor_refs = if let Some(metrics_ref) = &self.data.metrics {
119            metrics_ref
120                .get(self.bmc.as_ref())
121                .await
122                .map_err(Error::Bmc)
123                .map(|m| {
124                    extract_sensor_uris!(m,
125                        single: core_voltage,
126                    )
127                })?
128        } else {
129            Vec::new()
130        };
131
132        Ok(sensor_refs
133            .into_iter()
134            .map(|r| SensorRef::new(self.bmc.clone(), r))
135            .collect())
136    }
137}
138
139impl<B: Bmc> Resource for Processor<B> {
140    fn resource_ref(&self) -> &ResourceSchema {
141        &self.data.as_ref().base
142    }
143}