Skip to content

adding device::active_vgpus() #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions nvml-wrapper/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1180,7 +1180,7 @@

* `UnexpectedVariant`, for which you can read the docs for
* `IncorrectBits`, if bits are found in a session's info flags that don't
match the flags in this wrapper

Check warning on line 1183 in nvml-wrapper/src/device.rs

View workflow job for this annotation

GitHub Actions / Clippy

doc list item overindented
* `Uninitialized`, if the library has not been successfully initialized
* `NotSupported`, if this `Device` does not support this feature
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
Expand Down Expand Up @@ -3560,7 +3560,7 @@

* `Uninitialized`, if the library has not been successfully initialized
* `IncorrectBits`, if NVML returns any bits that do not correspond to flags in
`ThrottleReasons`

Check warning on line 3563 in nvml-wrapper/src/device.rs

View workflow job for this annotation

GitHub Actions / Clippy

doc list item without indentation
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
* `Unknown`, on any unexpected error

Expand Down Expand Up @@ -4607,7 +4607,7 @@
* `Uninitialized`, if the library has not been successfully initialized
* `InvalidArg`, if the `Device` is invalid
* `NotSupported`, if this `Device` does not support this feature or accounting mode
is disabled

Check warning on line 4610 in nvml-wrapper/src/device.rs

View workflow job for this annotation

GitHub Actions / Clippy

doc list item without indentation
* `Unknown`, on any unexpected error

# Device Support
Expand Down Expand Up @@ -4669,7 +4669,7 @@
* `Uninitialized`, if the library has not been successfully initialized
* `InvalidArg`, if the `Device` is invalid
* `NotSupported`, if this `Device` does not support this feature or accounting
mode is disabled

Check warning on line 4672 in nvml-wrapper/src/device.rs

View workflow job for this annotation

GitHub Actions / Clippy

doc list item without indentation
* `Unknown`, on any unexpected error
*/
// Checked against local
Expand Down Expand Up @@ -4726,9 +4726,9 @@
Note:
* Accounting mode needs to be on. See `.is_accounting_enabled()`.
* Only compute and graphics applications stats can be queried. Monitoring
applications can't be queried since they don't contribute to GPU utilization.

Check warning on line 4729 in nvml-wrapper/src/device.rs

View workflow job for this annotation

GitHub Actions / Clippy

doc list item without indentation
* If a PID collision occurs, the stats of the latest process (the one that
terminated last) will be reported.

Check warning on line 4731 in nvml-wrapper/src/device.rs

View workflow job for this annotation

GitHub Actions / Clippy

doc list item without indentation

# Errors

Expand All @@ -4736,7 +4736,7 @@
* `InvalidArg`, if the `Device` is invalid
* `NotFound`, if the process stats were not found
* `NotSupported`, if this `Device` does not support this feature or accounting
mode is disabled

Check warning on line 4739 in nvml-wrapper/src/device.rs

View workflow job for this annotation

GitHub Actions / Clippy

doc list item without indentation
* `Unknown`, on any unexpected error

# Device Support
Expand Down Expand Up @@ -4770,8 +4770,8 @@

Note:
* This setting is not persistent and will default to disabled after the driver
unloads. Enable persistence mode to be sure the setting doesn't switch off

Check warning on line 4773 in nvml-wrapper/src/device.rs

View workflow job for this annotation

GitHub Actions / Clippy

doc list item without indentation
to disabled.

Check warning on line 4774 in nvml-wrapper/src/device.rs

View workflow job for this annotation

GitHub Actions / Clippy

doc list item without indentation
* Enabling accounting mode has no negative impact on GPU performance.
* Disabling accounting clears accounting information for all PIDs

Expand Down Expand Up @@ -4844,7 +4844,7 @@
* `Uninitialized`, if the library has not been successfully initialized
* `InvalidArg`, if the `Device` is invalid or `api_type` is invalid (shouldn't occur?)
* `NotSupported`, if this `Device` does not support changing API restrictions or
this `Device` does not support the feature that API restrictions are being set for

Check warning on line 4847 in nvml-wrapper/src/device.rs

View workflow job for this annotation

GitHub Actions / Clippy

doc list item without indentation
(e.g. enabling/disabling auto boosted clocks is not supported by this `Device`).
* `NoPermission`, if the user doesn't have permission to perform this operation
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
Expand Down Expand Up @@ -5793,12 +5793,47 @@
}
}

/**
Gets the active vGPU instances for `Device`

A list as Vec of vGPU handles is returned to be used with nvmlVgpuInstance* calls.

# Errors

* `Uninitialized`, if the library has not been successfully initialized
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
* `Unknown`, on any unexpected error
* `NotSupported`, if the platform does not support this feature

# Platform Support

Only supports Linux.
*/
// Checked against local
// Tested
#[cfg(target_os = "linux")]
#[doc(alias = "nvmlDeviceGetActiveVgpus")]
pub fn active_vgpus(&self) -> Result<Vec<nvmlVgpuInstance_t>, NvmlError> {
let sym = nvml_sym(self.nvml.lib.nvmlDeviceGetActiveVgpus.as_ref())?;

unsafe {
let mut count: u32 = mem::zeroed();

nvml_try(sym(self.device, std::ptr::null_mut(), &mut count))?;
let mut arr: Vec<nvmlVgpuInstance_t> = vec![0; count as usize];
nvml_try(sym(self.device, arr.as_mut_ptr(), &mut count))?;

Ok(arr)
}
}

/**
Gets the virtualization mode of `Device`

# Errors

* `Uninitialized`, if the library has not been successfully initialized

* `InvalidArg`, if this `Device` is invalid or `clock_type` is invalid (shouldn't occur?)
* `NotSupported`, if this `Device` does not support this feature
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
Expand All @@ -5807,6 +5842,7 @@
# Device support

Supports Kepler and newer fully supported devices.

*/
// Checked against local
// Tested
Expand Down Expand Up @@ -7076,6 +7112,13 @@
test_with_device(3, &nvml, |device| device.is_drain_enabled(None))
}

#[cfg(target_os = "linux")]
#[test]
fn active_vgpus() {
let nvml = nvml();
test_with_device(3, &nvml, |device| device.active_vgpus())
}

#[cfg(target_os = "linux")]
#[test]
fn virtualization_mode() {
Expand Down
Loading