From ff96ff34efadf8e19f53fc8578b04290de4ec9e7 Mon Sep 17 00:00:00 2001 From: Jae-Won Chung Date: Wed, 29 May 2024 12:37:38 -0400 Subject: [PATCH] Fix NVML persistence mode API name --- zeusd/src/devices/gpu/linux.rs | 2 +- zeusd/src/devices/gpu/macos.rs | 4 ++-- zeusd/src/devices/gpu/mod.rs | 17 +++++++--------- zeusd/src/routes/gpu.rs | 8 ++++---- zeusd/tests/gpu.rs | 36 +++++++++++++++++----------------- zeusd/tests/helpers/mod.rs | 20 +++++++++---------- 6 files changed, 42 insertions(+), 45 deletions(-) diff --git a/zeusd/src/devices/gpu/linux.rs b/zeusd/src/devices/gpu/linux.rs index 69061ac4..2b372464 100644 --- a/zeusd/src/devices/gpu/linux.rs +++ b/zeusd/src/devices/gpu/linux.rs @@ -31,7 +31,7 @@ impl GpuManager for NvmlGpu<'static> { } #[inline] - fn set_persistent_mode(&mut self, enabled: bool) -> Result<(), ZeusdError> { + fn set_persistence_mode(&mut self, enabled: bool) -> Result<(), ZeusdError> { Ok(self.device.set_persistent(enabled)?) } diff --git a/zeusd/src/devices/gpu/macos.rs b/zeusd/src/devices/gpu/macos.rs index 63188044..5ff19f4e 100644 --- a/zeusd/src/devices/gpu/macos.rs +++ b/zeusd/src/devices/gpu/macos.rs @@ -13,10 +13,10 @@ impl NvmlGpu { impl GpuManager for NvmlGpu { fn device_count() -> Result { - Ok(0) + Ok(1) } - fn set_persistent_mode(&mut self, _enabled: bool) -> Result<(), ZeusdError> { + fn set_persistence_mode(&mut self, _enabled: bool) -> Result<(), ZeusdError> { Ok(()) } diff --git a/zeusd/src/devices/gpu/mod.rs b/zeusd/src/devices/gpu/mod.rs index 2af3dbaa..2d102f91 100644 --- a/zeusd/src/devices/gpu/mod.rs +++ b/zeusd/src/devices/gpu/mod.rs @@ -30,7 +30,7 @@ pub trait GpuManager { fn device_count() -> Result where Self: Sized; - fn set_persistent_mode(&mut self, enabled: bool) -> Result<(), ZeusdError>; + fn set_persistence_mode(&mut self, enabled: bool) -> Result<(), ZeusdError>; fn set_power_management_limit(&mut self, power_limit: u32) -> Result<(), ZeusdError>; fn set_gpu_locked_clocks( &mut self, @@ -98,9 +98,6 @@ impl GpuManagementTasks { command: GpuCommand, request_start_time: Instant, ) -> Result<(), ZeusdError> { - if gpu_id >= self.senders.len() { - return Err(ZeusdError::GpuNotFoundError(gpu_id)); - } if gpu_id >= self.senders.len() { return Err(ZeusdError::GpuNotFoundError(gpu_id)); } @@ -152,8 +149,8 @@ async fn gpu_management_task( /// A GPU command that can be executed on a GPU. #[derive(Debug)] pub enum GpuCommand { - /// Enable or disable persistent mode. - SetPersistentMode { enabled: bool }, + /// Enable or disable persistence mode. + SetPersistenceMode { enabled: bool }, /// Set the power management limit in milliwatts. SetPowerLimit { power_limit_mw: u32 }, /// Set the GPU's locked clock range in MHz. @@ -179,18 +176,18 @@ impl GpuCommand { T: GpuManager, { match *self { - Self::SetPersistentMode { enabled } => { - let result = device.set_persistent_mode(enabled); + Self::SetPersistenceMode { enabled } => { + let result = device.set_persistence_mode(enabled); if result.is_ok() { tracing::info!( elapsed = ?request_start_time.elapsed(), - "Persistent mode {}", + "Persistence mode {}", if enabled { "enabled" } else { "disabled" }, ); } else { tracing::warn!( elapsed = ?request_start_time.elapsed(), - "Cannot {} persistent mode", + "Cannot {} persistence mode", if enabled { "enable" } else { "disable" }, ); } diff --git a/zeusd/src/routes/gpu.rs b/zeusd/src/routes/gpu.rs index 440a25ec..b26ebfe1 100644 --- a/zeusd/src/routes/gpu.rs +++ b/zeusd/src/routes/gpu.rs @@ -11,7 +11,7 @@ use crate::error::ZeusdError; /// Macro to generate a handler for a GPU command. /// /// This macro takes -/// - the API name (set_power_limit, set_persistent_mode, etc.), +/// - the API name (set_power_limit, set_persistence_mode, etc.), /// - the method and path for the request handler, /// - and a list of `field name ` pairs of the corresponding `GpuCommand` variant. /// @@ -80,8 +80,8 @@ macro_rules! impl_handler_for_gpu_command { } impl_handler_for_gpu_command!( - set_persistent_mode, - post("/{gpu_id}/set_persistent_mode"), + set_persistence_mode, + post("/{gpu_id}/set_persistence_mode"), enabled, ); @@ -116,7 +116,7 @@ impl_handler_for_gpu_command!( ); pub fn gpu_routes(cfg: &mut web::ServiceConfig) { - cfg.service(set_persistent_mode_handler) + cfg.service(set_persistence_mode_handler) .service(set_power_limit_handler) .service(set_gpu_locked_clocks_handler) .service(reset_gpu_locked_clocks_handler) diff --git a/zeusd/tests/gpu.rs b/zeusd/tests/gpu.rs index e8b214ef..e1944af3 100644 --- a/zeusd/tests/gpu.rs +++ b/zeusd/tests/gpu.rs @@ -4,19 +4,19 @@ use std::collections::HashSet; use tokio::task::JoinSet; use zeusd::routes::gpu::{ ResetGpuLockedClocks, ResetMemLockedClocks, SetGpuLockedClocks, SetMemLockedClocks, - SetPersistentMode, SetPowerLimit, + SetPersistenceMode, SetPowerLimit, }; use crate::helpers::{TestApp, ZeusdRequest}; #[tokio::test] -async fn test_set_persistent_mode_single() { +async fn test_set_persistence_mode_single() { let mut app = TestApp::start().await; let resp = app .send( 0, - SetPersistentMode { + SetPersistenceMode { enabled: true, block: true, }, @@ -25,13 +25,13 @@ async fn test_set_persistent_mode_single() { .expect("Failed to send request"); assert_eq!(resp.status(), 200); - let history = app.persistent_mode_history_for_gpu(0); + let history = app.persistence_mode_history_for_gpu(0); assert_eq!(history.len(), 1); assert_eq!(history[0], true); } #[tokio::test] -async fn test_set_persistent_mode_multiple() { +async fn test_set_persistence_mode_multiple() { let mut app = TestApp::start().await; let num_requests = 10; @@ -39,7 +39,7 @@ async fn test_set_persistent_mode_multiple() { let resp = app .send( i % 4, - SetPersistentMode { + SetPersistenceMode { enabled: (i / 4) % 2 == 0, block: true, }, @@ -51,23 +51,23 @@ async fn test_set_persistent_mode_multiple() { } assert_eq!( - app.persistent_mode_history_for_gpu(0), + app.persistence_mode_history_for_gpu(0), vec![true, false, true] ); assert_eq!( - app.persistent_mode_history_for_gpu(1), + app.persistence_mode_history_for_gpu(1), vec![true, false, true] ); - assert_eq!(app.persistent_mode_history_for_gpu(2), vec![true, false]); - assert_eq!(app.persistent_mode_history_for_gpu(3), vec![true, false]); + assert_eq!(app.persistence_mode_history_for_gpu(2), vec![true, false]); + assert_eq!(app.persistence_mode_history_for_gpu(3), vec![true, false]); } #[tokio::test] -async fn test_set_persistent_mode_invalid() { +async fn test_set_persistence_mode_invalid() { let app = TestApp::start().await; let client = reqwest::Client::new(); - let url = SetPersistentMode::build_url(&app, 0); + let url = SetPersistenceMode::build_url(&app, 0); let resp = client .post(url) .json(&serde_json::json!( @@ -86,7 +86,7 @@ async fn test_set_persistent_mode_invalid() { .expect("Failed to read response") .contains("missing field")); - let url = SetPersistentMode::build_url(&app, 0); + let url = SetPersistenceMode::build_url(&app, 0); let resp = client .post(url) .json(&serde_json::json!( @@ -105,7 +105,7 @@ async fn test_set_persistent_mode_invalid() { .expect("Failed to read response") .contains("invalid type")); - let url = SetPersistentMode::build_url(&app, 5); // Invalid GPU ID + let url = SetPersistenceMode::build_url(&app, 5); // Invalid GPU ID let resp = client .post(url) .json(&serde_json::json!( @@ -121,14 +121,14 @@ async fn test_set_persistent_mode_invalid() { } #[tokio::test] -async fn test_set_persistent_mode_bulk() { +async fn test_set_persistence_mode_bulk() { let mut app = TestApp::start().await; let mut set = JoinSet::new(); for i in 0..10 { set.spawn(app.send( 0, - SetPersistentMode { + SetPersistenceMode { enabled: i % 3 == 0, block: false, }, @@ -152,7 +152,7 @@ async fn test_set_persistent_mode_bulk() { assert_eq!( app.send( 0, - SetPersistentMode { + SetPersistenceMode { enabled: false, block: true, }, @@ -163,7 +163,7 @@ async fn test_set_persistent_mode_bulk() { 200 ); - let history = app.persistent_mode_history_for_gpu(0); + let history = app.persistence_mode_history_for_gpu(0); assert_eq!(history.len(), 11); assert_eq!(history.iter().filter(|enabled| **enabled).count(), 4); assert_eq!(history.iter().filter(|enabled| !**enabled).count(), 6 + 1); diff --git a/zeusd/tests/helpers/mod.rs b/zeusd/tests/helpers/mod.rs index cf604bc8..8e6e9860 100644 --- a/zeusd/tests/helpers/mod.rs +++ b/zeusd/tests/helpers/mod.rs @@ -25,7 +25,7 @@ static TRACING: Lazy<()> = Lazy::new(|| { #[derive(Clone)] pub struct TestGpu { - persistent_mode_tx: UnboundedSender, + persistence_mode_tx: UnboundedSender, power_limit_tx: UnboundedSender, gpu_locked_clocks_tx: UnboundedSender<(u32, u32)>, mem_locked_clocks_tx: UnboundedSender<(u32, u32)>, @@ -33,7 +33,7 @@ pub struct TestGpu { } pub struct TestGpuObserver { - persistent_mode_rx: UnboundedReceiver, + persistence_mode_rx: UnboundedReceiver, power_limit_rx: UnboundedReceiver, gpu_locked_clocks_rx: UnboundedReceiver<(u32, u32)>, mem_locked_clocks_rx: UnboundedReceiver<(u32, u32)>, @@ -41,20 +41,20 @@ pub struct TestGpuObserver { impl TestGpu { fn init() -> Result<(Self, TestGpuObserver), ZeusdError> { - let (persistent_mode_tx, persistent_mode_rx) = tokio::sync::mpsc::unbounded_channel(); + let (persistence_mode_tx, persistence_mode_rx) = tokio::sync::mpsc::unbounded_channel(); let (power_limit_tx, power_limit_rx) = tokio::sync::mpsc::unbounded_channel(); let (gpu_locked_clocks_tx, gpu_locked_clocks_rx) = tokio::sync::mpsc::unbounded_channel(); let (mem_locked_clocks_tx, mem_locked_clocks_rx) = tokio::sync::mpsc::unbounded_channel(); let gpu = TestGpu { - persistent_mode_tx, + persistence_mode_tx, power_limit_tx, gpu_locked_clocks_tx, mem_locked_clocks_tx, valid_power_limit_range: (100_000, 300_000), }; let observer = TestGpuObserver { - persistent_mode_rx, + persistence_mode_rx, power_limit_rx, gpu_locked_clocks_rx, mem_locked_clocks_rx, @@ -69,8 +69,8 @@ impl GpuManager for TestGpu { Ok(NUM_GPUS) } - fn set_persistent_mode(&mut self, enabled: bool) -> Result<(), ZeusdError> { - self.persistent_mode_tx.send(enabled).unwrap(); + fn set_persistence_mode(&mut self, enabled: bool) -> Result<(), ZeusdError> { + self.persistence_mode_tx.send(enabled).unwrap(); Ok(()) } @@ -151,7 +151,7 @@ macro_rules! impl_zeusd_request { }; } -impl_zeusd_request!(SetPersistentMode); +impl_zeusd_request!(SetPersistenceMode); impl_zeusd_request!(SetPowerLimit); impl_zeusd_request!(SetGpuLockedClocks); impl_zeusd_request!(ResetGpuLockedClocks); @@ -194,8 +194,8 @@ impl TestApp { client.post(url).json(&payload).send() } - pub fn persistent_mode_history_for_gpu(&mut self, gpu_id: usize) -> Vec { - let rx = &mut self.observers[gpu_id].persistent_mode_rx; + pub fn persistence_mode_history_for_gpu(&mut self, gpu_id: usize) -> Vec { + let rx = &mut self.observers[gpu_id].persistence_mode_rx; std::iter::from_fn(|| rx.try_recv().ok()).collect() }