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::processor::Processor as ProcessorSchema;
19use crate::schema::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 = "controls")]
29use crate::control::extract_environment_power_limit_control;
30#[cfg(feature = "controls")]
31use crate::control::Control;
32#[cfg(feature = "sensors")]
33use crate::extract_sensor_uris;
34#[cfg(feature = "sensors")]
35use crate::sensor::extract_environment_sensors;
36#[cfg(feature = "sensors")]
37use crate::sensor::SensorLink;
38
39/// Represents a processor in a computer system.
40///
41/// Provides access to processor information and associated metrics/sensors.
42pub struct Processor<B: Bmc> {
43 bmc: NvBmc<B>,
44 data: Arc<ProcessorSchema>,
45}
46
47impl<B: Bmc> Processor<B> {
48 /// Create a new processor handle.
49 pub(crate) async fn new(
50 bmc: &NvBmc<B>,
51 nav: &NavProperty<ProcessorSchema>,
52 ) -> Result<Self, Error<B>> {
53 nav.get(bmc.as_ref())
54 .await
55 .map_err(crate::Error::Bmc)
56 .map(|data| Self {
57 bmc: bmc.clone(),
58 data,
59 })
60 }
61
62 /// Get the raw schema data for this processor.
63 ///
64 /// Returns an `Arc` to the underlying schema, allowing cheap cloning
65 /// and sharing of the data.
66 #[must_use]
67 pub fn raw(&self) -> Arc<ProcessorSchema> {
68 self.data.clone()
69 }
70
71 /// Get processor metrics.
72 ///
73 /// Returns the processor's performance and state metrics if available.
74 ///
75 /// # Errors
76 ///
77 /// Returns an error if:
78 /// - The processor does not have metrics
79 /// - Fetching metrics data fails
80 pub async fn metrics(&self) -> Result<Option<Arc<ProcessorMetrics>>, Error<B>> {
81 if let Some(metrics_ref) = &self.data.metrics {
82 metrics_ref
83 .get(self.bmc.as_ref())
84 .await
85 .map_err(Error::Bmc)
86 .map(Some)
87 } else {
88 Ok(None)
89 }
90 }
91
92 /// Get the environment sensors for this processor.
93 ///
94 /// Returns a vector of `Sensor<B>` obtained from environment metrics, if available.
95 ///
96 /// # Errors
97 ///
98 /// Returns an error if get of environment metrics failed.
99 #[cfg(feature = "sensors")]
100 pub async fn environment_sensor_links(&self) -> Result<Vec<SensorLink<B>>, Error<B>> {
101 let sensor_refs = if let Some(env_ref) = &self.data.environment_metrics {
102 extract_environment_sensors(env_ref, self.bmc.as_ref()).await?
103 } else {
104 Vec::new()
105 };
106
107 Ok(sensor_refs
108 .into_iter()
109 .map(|r| SensorLink::new(&self.bmc, r))
110 .collect())
111 }
112
113 /// Get the environment power limit control for this processor.
114 ///
115 /// Returns `Ok(None)` when environment metrics or `PowerLimitWatts` is absent.
116 ///
117 /// # Errors
118 ///
119 /// Returns an error if fetching environment metrics or the control fails.
120 #[cfg(feature = "controls")]
121 pub async fn environment_power_limit_control(&self) -> Result<Option<Control<B>>, Error<B>> {
122 let Some(env_ref) = &self.data.environment_metrics else {
123 return Ok(None);
124 };
125
126 extract_environment_power_limit_control(&self.bmc, env_ref).await
127 }
128
129 /// Get the metrics sensors for this processor.
130 ///
131 /// Returns a vector of `Sensor<B>` obtained from metrics metrics, if available.
132 ///
133 /// # Errors
134 ///
135 /// Returns an error if get of metrics failed.
136 #[cfg(feature = "sensors")]
137 pub async fn metrics_sensor_links(&self) -> Result<Vec<SensorLink<B>>, Error<B>> {
138 let sensor_refs = if let Some(metrics_ref) = &self.data.metrics {
139 metrics_ref
140 .get(self.bmc.as_ref())
141 .await
142 .map_err(Error::Bmc)
143 .map(|m| {
144 extract_sensor_uris!(m,
145 single: core_voltage,
146 )
147 })?
148 } else {
149 Vec::new()
150 };
151
152 Ok(sensor_refs
153 .into_iter()
154 .map(|r| SensorLink::new(&self.bmc, r))
155 .collect())
156 }
157}
158
159impl<B: Bmc> Resource for Processor<B> {
160 fn resource_ref(&self) -> &ResourceSchema {
161 &self.data.as_ref().base
162 }
163}