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
16use crate::schema::redfish::memory::Memory as MemorySchema;
17use crate::schema::redfish::memory_metrics::MemoryMetrics;
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::sensor::extract_environment_sensors;
28#[cfg(feature = "sensors")]
29use crate::sensor::SensorRef;
30
31/// Represents a memory module (DIMM) in a computer system.
32///
33/// Provides access to memory module information and associated metrics/sensors.
34pub struct Memory<B: Bmc> {
35 bmc: NvBmc<B>,
36 data: Arc<MemorySchema>,
37}
38
39impl<B: Bmc> Memory<B> {
40 /// Create a new memory handle.
41 pub(crate) async fn new(
42 bmc: &NvBmc<B>,
43 nav: &NavProperty<MemorySchema>,
44 ) -> Result<Self, Error<B>> {
45 nav.get(bmc.as_ref())
46 .await
47 .map_err(Error::Bmc)
48 .map(|data| Self {
49 bmc: bmc.clone(),
50 data,
51 })
52 }
53
54 /// Get the raw schema data for this memory module.
55 ///
56 /// Returns an `Arc` to the underlying schema, allowing cheap cloning
57 /// and sharing of the data.
58 #[must_use]
59 pub fn raw(&self) -> Arc<MemorySchema> {
60 self.data.clone()
61 }
62
63 /// Get memory metrics.
64 ///
65 /// Returns the memory module's performance and state metrics if available.
66 ///
67 /// # Errors
68 ///
69 /// Returns an error if:
70 /// - The memory module does not have metrics
71 /// - Fetching metrics data fails
72 pub async fn metrics(&self) -> Result<Arc<MemoryMetrics>, Error<B>> {
73 let metrics_ref = self
74 .data
75 .metrics
76 .as_ref()
77 .ok_or(Error::MetricsNotAvailable)?;
78
79 metrics_ref.get(self.bmc.as_ref()).await.map_err(Error::Bmc)
80 }
81
82 /// Get the environment sensors for this memory.
83 ///
84 /// Returns a vector of `Sensor<B>` obtained from environment metrics, if available. /// # Errors
85 ///
86 /// # Errors
87 ///
88 /// Returns an error if get of environment metrics failed.
89 #[cfg(feature = "sensors")]
90 pub async fn environment_sensors(&self) -> Result<Vec<SensorRef<B>>, Error<B>> {
91 let sensor_refs = if let Some(env_ref) = &self.data.environment_metrics {
92 extract_environment_sensors(env_ref, self.bmc.as_ref()).await?
93 } else {
94 Vec::new()
95 };
96
97 Ok(sensor_refs
98 .into_iter()
99 .map(|r| SensorRef::new(self.bmc.clone(), r))
100 .collect())
101 }
102}
103
104impl<B: Bmc> Resource for Memory<B> {
105 fn resource_ref(&self) -> &ResourceSchema {
106 &self.data.as_ref().base
107 }
108}