Skip to content

Commit 2062ec0

Browse files
feat: improve power limit error handling and display constraints
1 parent abed657 commit 2062ec0

1 file changed

Lines changed: 32 additions & 4 deletions

File tree

src/main.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clap::{Args, CommandFactory, Parser, Subcommand};
22
use clap_complete::{generate, Generator, Shell};
3-
use nvml_wrapper::{Device, Nvml};
3+
use nvml_wrapper::{error::NvmlError, Device, Nvml};
44
use serde::Deserialize;
55
use std::{collections::HashMap, io};
66

@@ -81,9 +81,27 @@ impl Sets {
8181
}
8282

8383
if let Some(limit) = self.power_limit {
84-
device
85-
.set_power_management_limit(limit)
86-
.expect("Failed to set GPU power limit");
84+
if let Err(e) = device.set_power_management_limit(limit) {
85+
match e {
86+
NvmlError::InvalidArg => {
87+
let mut error_msg = format!(
88+
"Failed to set GPU power limit: {} mW is out of range.",
89+
limit
90+
);
91+
if let Ok(constraints) = device.power_management_limit_constraints() {
92+
error_msg.push_str(&format!(
93+
" Valid range: {}-{} mW ({}-{} W)",
94+
constraints.min_limit,
95+
constraints.max_limit,
96+
constraints.min_limit / 1000,
97+
constraints.max_limit / 1000
98+
));
99+
}
100+
panic!("{}", error_msg);
101+
}
102+
_ => panic!("Failed to set GPU power limit: {:?}", e),
103+
}
104+
}
87105
}
88106

89107
if let (Some(min_clock), Some(max_clock)) = (self.min_clock, self.max_clock) {
@@ -151,6 +169,16 @@ fn main() {
151169
Ok(power_limit) => println!("GPU power limit: {} W", power_limit / 1000),
152170
Err(e) => eprintln!("Failed to get GPU power limit: {:?}", e),
153171
}
172+
173+
let power_constraints = device.power_management_limit_constraints();
174+
match power_constraints {
175+
Ok(constraints) => println!(
176+
"GPU power limit range: {}-{} W",
177+
constraints.min_limit / 1000,
178+
constraints.max_limit / 1000
179+
),
180+
Err(e) => eprintln!("Failed to get GPU power limit constraints: {:?}", e),
181+
}
154182
}
155183
None => {
156184
let Ok(config_file) = std::fs::read_to_string(cli.file) else {

0 commit comments

Comments
 (0)