Skip to content

Commit 9fb9526

Browse files
committed
adding device::virtualization_mode() wrapper.
Returns whether the current GPU device is within a virtualization mode or bare metal.
1 parent 572095f commit 9fb9526

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

nvml-wrapper/src/device.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5314,6 +5314,37 @@ impl<'nvml> Device<'nvml> {
53145314
}
53155315
}
53165316

5317+
/**
5318+
Gets the virtualization mode of `Device`
5319+
5320+
# Errors
5321+
5322+
* `Uninitialized`, if the library has not been successfully initialized
5323+
* `InvalidArg`, if this `Device` is invalid or `clock_type` is invalid (shouldn't occur?)
5324+
* `NotSupported`, if this `Device` does not support this feature
5325+
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
5326+
* `Unknown`, on any unexpected error
5327+
5328+
# Device support
5329+
5330+
Supports Kepler and newer fully supported devices.
5331+
*/
5332+
// Checked against local
5333+
// Tested
5334+
#[cfg(target_os = "linux")]
5335+
#[doc(alias = "nvmlDeviceGetVirtualizationMode")]
5336+
pub fn virtualization_mode(&self) -> Result<GpuVirtualizationMode, NvmlError> {
5337+
let sym = nvml_sym(self.nvml.lib.nvmlDeviceGetVirtualizationMode.as_ref())?;
5338+
5339+
unsafe {
5340+
let mut mode: nvmlGpuVirtualizationMode_t = mem::zeroed();
5341+
5342+
nvml_try(sym(self.device, &mut mode))?;
5343+
5344+
GpuVirtualizationMode::try_from(mode)
5345+
}
5346+
}
5347+
53175348
/**
53185349
Removes this `Device` from the view of both NVML and the NVIDIA kernel driver.
53195350
@@ -6514,6 +6545,13 @@ mod test {
65146545
test_with_device(3, &nvml, |device| device.is_drain_enabled(None))
65156546
}
65166547

6548+
#[cfg(target_os = "linux")]
6549+
#[test]
6550+
fn virtualization_mode() {
6551+
let nvml = nvml();
6552+
test_with_device(3, &nvml, |device| device.virtualization_mode())
6553+
}
6554+
65176555
#[cfg(target_os = "linux")]
65186556
#[test]
65196557
fn device_attributes() {

nvml-wrapper/src/enum_wrappers/device.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,3 +629,24 @@ pub enum ClockLimitId {
629629
#[wrap(c_variant = "NVML_CLOCK_LIMIT_ID_UNLIMITED")]
630630
Unlimited,
631631
}
632+
633+
#[derive(EnumWrapper, Debug, Clone, Eq, PartialEq, Hash)]
634+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
635+
#[wrap(c_enum = "nvmlGpuVirtualizationMode")]
636+
pub enum GpuVirtualizationMode {
637+
/// Bare metal mode
638+
#[wrap(c_variant = "NVML_GPU_VIRTUALIZATION_MODE_NONE")]
639+
Bare,
640+
/// Passthrough mode
641+
#[wrap(c_variant = "NVML_GPU_VIRTUALIZATION_MODE_PASSTHROUGH")]
642+
PassThrough,
643+
/// vGPU inside virtual machine mode
644+
#[wrap(c_variant = "NVML_GPU_VIRTUALIZATION_MODE_VGPU")]
645+
Vgpu,
646+
/// VGX hypervisor in vGPU mode
647+
#[wrap(c_variant = "NVML_GPU_VIRTUALIZATION_MODE_HOST_VGPU")]
648+
HostVgpu,
649+
/// VGX hypervisor in vSGA mode
650+
#[wrap(c_variant = "NVML_GPU_VIRTUALIZATION_MODE_HOST_VSGA")]
651+
HostVsga,
652+
}

nvml-wrapper/src/test_utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ impl ShouldPrint for PowerSource {}
105105
impl ShouldPrint for DeviceArchitecture {}
106106
impl ShouldPrint for PcieLinkMaxSpeed {}
107107
impl ShouldPrint for DeviceAttributes {}
108+
impl ShouldPrint for GpuVirtualizationMode {}
108109

109110
#[cfg(target_os = "windows")]
110111
impl ShouldPrint for DriverModelState {}

nvml-wrapper/unwrapped_functions.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ nvmlDeviceGetCapabilities
1212
nvmlDeviceGetClkMonStatus
1313
nvmlDeviceGetClockOffsets
1414
nvmlDeviceGetComputeInstanceId
15-
nvmlDeviceGetConfComputeGpuCertificate
1615
nvmlDeviceGetConfComputeMemSizeInfo
1716
nvmlDeviceGetConfComputeProtectedMemoryUsage
1817
nvmlDeviceGetCoolerInfo
19-
nvmlDeviceGetCpuAffinityWithinScope
2018
nvmlDeviceGetCreatableVgpus
2119
nvmlDeviceGetCurrentClockFreqs
2220
nvmlDeviceGetCurrentClocksEventReasons

0 commit comments

Comments
 (0)