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<Option<Arc<ProcessorMetrics>>, Error<B>> {
75        if let Some(metrics_ref) = &self.data.metrics {
76            metrics_ref
77                .get(self.bmc.as_ref())
78                .await
79                .map_err(Error::Bmc)
80                .map(Some)
81        } else {
82            Ok(None)
83        }
84    }
85
86    /// Get the environment sensors for this processor.
87    ///
88    /// Returns a vector of `Sensor<B>` obtained from environment metrics, if available.
89    ///
90    /// # Errors
91    ///
92    /// Returns an error if get of environment metrics failed.
93    #[cfg(feature = "sensors")]
94    pub async fn environment_sensors(&self) -> Result<Vec<SensorRef<B>>, Error<B>> {
95        let sensor_refs = if let Some(env_ref) = &self.data.environment_metrics {
96            extract_environment_sensors(env_ref, self.bmc.as_ref()).await?
97        } else {
98            Vec::new()
99        };
100
101        Ok(sensor_refs
102            .into_iter()
103            .map(|r| SensorRef::new(self.bmc.clone(), r))
104            .collect())
105    }
106
107    /// Get the metrics sensors for this processor.
108    ///
109    /// Returns a vector of `Sensor<B>` obtained from metrics metrics, if available.
110    ///
111    /// # Errors
112    ///
113    /// Returns an error if get of metrics failed.
114    #[cfg(feature = "sensors")]
115    pub async fn metrics_sensors(&self) -> Result<Vec<SensorRef<B>>, Error<B>> {
116        let sensor_refs = if let Some(metrics_ref) = &self.data.metrics {
117            metrics_ref
118                .get(self.bmc.as_ref())
119                .await
120                .map_err(Error::Bmc)
121                .map(|m| {
122                    extract_sensor_uris!(m,
123                        single: core_voltage,
124                    )
125                })?
126        } else {
127            Vec::new()
128        };
129
130        Ok(sensor_refs
131            .into_iter()
132            .map(|r| SensorRef::new(self.bmc.clone(), r))
133            .collect())
134    }
135}
136
137impl<B: Bmc> Resource for Processor<B> {
138    fn resource_ref(&self) -> &ResourceSchema {
139        &self.data.as_ref().base
140    }
141}