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