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