Skip to content
Merged
Show file tree
Hide file tree
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
92 changes: 87 additions & 5 deletions nvml-wrapper/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@

use crate::enum_wrappers::{bool_from_state, device::*, state_from_bool};

use crate::enums::device::BusType;
use crate::enums::device::DeviceArchitecture;
use crate::enums::device::GpuLockedClocksSetting;
use crate::enums::device::PcieLinkMaxSpeed;
use crate::enums::device::PowerSource;
use crate::enums::device::{
BusType, DeviceArchitecture, FanControlPolicy, GpuLockedClocksSetting, PcieLinkMaxSpeed,
PowerSource,
};
#[cfg(target_os = "linux")]
use crate::error::NvmlErrorWithSource;
use crate::error::{nvml_sym, nvml_try, Bits, NvmlError};
Expand Down Expand Up @@ -73,8 +72,8 @@
nvml: &'nvml Nvml,
}

unsafe impl<'nvml> Send for Device<'nvml> {}

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

View workflow job for this annotation

GitHub Actions / Clippy

the following explicit lifetimes could be elided: 'nvml
unsafe impl<'nvml> Sync for Device<'nvml> {}

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

View workflow job for this annotation

GitHub Actions / Clippy

the following explicit lifetimes could be elided: 'nvml

assert_impl_all!(Device: Send, Sync);

Expand Down Expand Up @@ -155,10 +154,10 @@

* `Uninitialized`, if the library has not been successfully initialized
* `InvalidArg`, if this `Device` is invalid or the apiType is invalid (may occur if
the C lib changes dramatically?)

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

View workflow job for this annotation

GitHub Actions / Clippy

doc list item without indentation
* `NotSupported`, if this query is not supported by this `Device` or this `Device`
does not support the feature that is being queried (e.g. enabling/disabling auto

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

View workflow job for this annotation

GitHub Actions / Clippy

doc list item without indentation
boosted clocks is not supported by this `Device`).

Check warning on line 160 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
* `UnexpectedVariant`, for which you can read the docs for
* `Unknown`, on any unexpected error
Expand Down Expand Up @@ -192,7 +191,7 @@

* `Uninitialized`, if the library has not been successfully initialized
* `InvalidArg`, if this `Device` is invalid or the clockType is invalid (may occur
if the C lib changes dramatically?)

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

View workflow job for this annotation

GitHub Actions / Clippy

doc list item without indentation
* `NotSupported`, if this `Device` does not support this feature
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
* `Unknown`, on any unexpected error
Expand Down Expand Up @@ -432,7 +431,7 @@
* `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` or the `clock_type` on this `Device`
does not support this feature

Check warning on line 434 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 @@ -1236,7 +1235,7 @@
* `Uninitialized`, if the library has not been successfully initialized
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
* `UnexpectedVariant`, if an enum variant not defined in this wrapper gets
returned in a field of an `EncoderSessionInfo` struct

Check warning on line 1238 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 @@ -1357,6 +1356,89 @@
}
}

/**
Gets current fan control policy.

You can determine valid fan indices using [`Self::num_fans()`].

# Errors

* `Uninitialized`, if the library has not been successfully initialized
* `InvalidArg`, if this `Device` is invalid or `fan_idx` is invalid
* `NotSupported`, if this `Device` does not have a fan
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
* `UnexpectedVariant`, for which you can read the docs for
* `Unknown`, on any unexpected error

# Device Support

Supports Maxwell or newer fully supported discrete devices with fans.
*/
#[doc(alias = "nvmlGetFanControlPolicy_v2")]
pub fn fan_control_policy(&self, fan_idx: u32) -> Result<FanControlPolicy, NvmlError> {
let sym = nvml_sym(self.nvml.lib.nvmlDeviceGetFanControlPolicy_v2.as_ref())?;

unsafe {
let mut policy: nvmlFanControlPolicy_t = mem::zeroed();
nvml_try(sym(self.device, fan_idx, &mut policy))?;

FanControlPolicy::try_from(policy)
}
}

/**
Sets the speed of a specified fan.

WARNING: This function changes the fan control policy to manual. It means that YOU have to monitor the temperature and adjust the fan speed accordingly.
If you set the fan speed too low you can burn your GPU! Use [`Device::set_default_fan_speed`] to restore default control policy.

You can determine valid fan indices using [`Self::num_fans()`].

# Errors

* `Uninitialized`, if the library has not been successfully initialized
* `InvalidArg`, if this `Device` is invalid or `fan_idx` is invalid
* `NotSupported`, if this `Device` does not have a fan
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
* `UnexpectedVariant`, for which you can read the docs for
* `Unknown`, on any unexpected error

# Device Support

Supports Maxwell or newer fully supported discrete devices with fans.
*/
#[doc(alias = "nvmlDeviceSetFanSpeed_v2")]
pub fn set_fan_speed(&mut self, fan_idx: u32, speed: u32) -> Result<(), NvmlError> {
let sym = nvml_sym(self.nvml.lib.nvmlDeviceSetFanSpeed_v2.as_ref())?;

unsafe { nvml_try(sym(self.device, fan_idx, speed)) }
}

/**
Sets the the fan control policy to default.

You can determine valid fan indices using [`Self::num_fans()`].

# Errors

* `Uninitialized`, if the library has not been successfully initialized
* `InvalidArg`, if this `Device` is invalid or `fan_idx` is invalid
* `NotSupported`, if this `Device` does not have a fan
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
* `UnexpectedVariant`, for which you can read the docs for
* `Unknown`, on any unexpected error

# Device Support

Supports Maxwell or newer fully supported discrete devices with fans.
*/
#[doc(alias = "nvmlDeviceSetDefaultFanSpeed_v2")]
pub fn set_default_fan_speed(&mut self, fan_idx: u32) -> Result<(), NvmlError> {
let sym = nvml_sym(self.nvml.lib.nvmlDeviceSetDefaultFanSpeed_v2.as_ref())?;

unsafe { nvml_try(sym(self.device, fan_idx)) }
}

/**
Gets the number of fans on this [`Device`].

Expand Down Expand Up @@ -1899,7 +1981,7 @@
* `Uninitialized`, if the library has not been successfully initialized
* `InvalidArg`, if `error_type`, `counter_type`, or `location` is invalid (shouldn't occur?)
* `NotSupported`, if this `Device` does not support ECC error reporting for the specified
memory

Check warning on line 1984 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 @@ -2821,7 +2903,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 2906 in nvml-wrapper/src/device.rs

View workflow job for this annotation

GitHub Actions / Clippy

doc list item without indentation
* `NotSupported`, if this `Device` does not support this feature
* `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible
* `Unknown`, on any unexpected error
Expand Down
31 changes: 31 additions & 0 deletions nvml-wrapper/src/enums/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,34 @@ impl TryFrom<c_uint> for PcieLinkMaxSpeed {
}
}
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(u32)]
pub enum FanControlPolicy {
TemperatureContinousSw = NVML_FAN_POLICY_TEMPERATURE_CONTINOUS_SW,
Manual = NVML_FAN_POLICY_MANUAL,
}

/// Returned by [`crate::Device::get_fan_control_policy()`].
///
/// Policy used for fan control.
// TODO: technically this is an "enum wrapper" but the type on the C side isn't
// an enum
impl FanControlPolicy {
pub fn as_c(&self) -> nvmlFanControlPolicy_t {
*self as u32
}
}

impl TryFrom<nvmlFanControlPolicy_t> for FanControlPolicy {
type Error = NvmlError;

fn try_from(value: nvmlFanControlPolicy_t) -> Result<Self, Self::Error> {
match value {
NVML_FAN_POLICY_TEMPERATURE_CONTINOUS_SW => Ok(Self::TemperatureContinousSw),
NVML_FAN_POLICY_MANUAL => Ok(Self::TemperatureContinousSw),
_ => Err(NvmlError::UnexpectedVariant(value)),
}
}
}
37 changes: 7 additions & 30 deletions nvml-wrapper/unwrapped_functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,22 @@ nvmlComputeInstanceGetInfo_v2
nvmlDeviceClearFieldValues
nvmlDeviceCreateGpuInstance
nvmlDeviceCreateGpuInstanceWithPlacement
nvmlDeviceFreezeNvLinkUtilizationCounter
nvmlDeviceGetActiveVgpus
nvmlDeviceGetAdaptiveClockInfoStatus
nvmlDeviceGetAttributes
nvmlDeviceGetAttributes_v2
nvmlDeviceGetClkMonStatus
nvmlDeviceGetComputeInstanceId
nvmlDeviceGetComputeRunningProcesses
nvmlDeviceGetComputeRunningProcesses_v2
nvmlDeviceGetComputeRunningProcesses_v3
nvmlDeviceGetConfComputeGpuAttestationReport
nvmlDeviceGetConfComputeGpuCertificate
nvmlDeviceGetConfComputeMemSizeInfo
nvmlDeviceGetConfComputeProtectedMemoryUsage
nvmlDeviceGetCpuAffinityWithinScope
nvmlDeviceGetCreatableVgpus
nvmlDeviceGetCurrentClocksEventReasons
nvmlDeviceGetCurrentClocksThrottleReasons
nvmlDeviceGetDefaultEccMode
nvmlDeviceGetDeviceHandleFromMigDeviceHandle
nvmlDeviceGetDynamicPstatesInfo
nvmlDeviceGetFanControlPolicy_v2
nvmlDeviceGetGpcClkMinMaxVfOffset
nvmlDeviceGetGpcClkVfOffset
nvmlDeviceGetGpuFabricInfo
Expand All @@ -39,21 +33,14 @@ nvmlDeviceGetGpuInstanceProfileInfoV
nvmlDeviceGetGpuInstanceRemainingCapacity
nvmlDeviceGetGpuInstances
nvmlDeviceGetGpuMaxPcieLinkGeneration
nvmlDeviceGetGraphicsRunningProcesses
nvmlDeviceGetGraphicsRunningProcesses_v2
nvmlDeviceGetGraphicsRunningProcesses_v3
nvmlDeviceGetGridLicensableFeatures
nvmlDeviceGetGridLicensableFeatures_v2
nvmlDeviceGetGridLicensableFeatures_v3
nvmlDeviceGetGridLicensableFeatures_v4
nvmlDeviceGetGspFirmwareMode
nvmlDeviceGetGspFirmwareVersion
nvmlDeviceGetHostVgpuMode
nvmlDeviceGetInforomConfigurationChecksum
nvmlDeviceGetJpgUtilization
nvmlDeviceGetMPSComputeRunningProcesses
nvmlDeviceGetMPSComputeRunningProcesses_v2
nvmlDeviceGetMPSComputeRunningProcesses_v3
nvmlDeviceGetMaxMigDeviceCount
nvmlDeviceGetMemClkMinMaxVfOffset
nvmlDeviceGetMemClkVfOffset
Expand All @@ -63,24 +50,17 @@ nvmlDeviceGetMigMode
nvmlDeviceGetMinMaxClockOfPState
nvmlDeviceGetMinMaxFanSpeed
nvmlDeviceGetModuleId
nvmlDeviceGetNvLinkCapability
nvmlDeviceGetNvLinkErrorCounter
nvmlDeviceGetMPSComputeRunningProcesses
nvmlDeviceGetMPSComputeRunningProcesses_v2
nvmlDeviceGetMPSComputeRunningProcesses_v3
nvmlDeviceGetNvLinkRemoteDeviceType
nvmlDeviceGetNvLinkRemotePciInfo
nvmlDeviceGetNvLinkRemotePciInfo_v2
nvmlDeviceGetNvLinkUtilizationControl
nvmlDeviceGetNvLinkUtilizationCounter
nvmlDeviceGetOfaUtilization
nvmlDeviceGetP2PStatus
nvmlDeviceGetPgpuMetadataString
nvmlDeviceGetPowerManagementDefaultLimit
nvmlDeviceGetPowerManagementLimitConstraints
nvmlDeviceGetRemappedRows
nvmlDeviceGetRetiredPagesPendingStatus
nvmlDeviceGetRowRemapperHistogram
nvmlDeviceGetRunningProcessDetailList
nvmlDeviceGetSupportedClocksEventReasons
nvmlDeviceGetSupportedClocksThrottleReasons
nvmlDeviceGetSupportedPerformanceStates
nvmlDeviceGetSupportedVgpus
nvmlDeviceGetTargetFanSpeed
Expand All @@ -94,18 +74,12 @@ nvmlDeviceGetVgpuSchedulerState
nvmlDeviceGetVgpuUtilization
nvmlDeviceGetVirtualizationMode
nvmlDeviceIsMigDeviceHandle
nvmlDeviceResetNvLinkErrorCounters
nvmlDeviceResetNvLinkUtilizationCounter
nvmlDeviceSetConfComputeUnprotectedMemSize
nvmlDeviceSetDefaultAutoBoostedClocksEnabled
nvmlDeviceSetDefaultFanSpeed_v2
nvmlDeviceSetFanControlPolicy
nvmlDeviceSetFanSpeed_v2
nvmlDeviceSetGpcClkVfOffset
nvmlDeviceSetMemClkVfOffset
nvmlDeviceSetMigMode
nvmlDeviceSetNvLinkDeviceLowPowerThreshold
nvmlDeviceSetNvLinkUtilizationControl
nvmlDeviceSetTemperatureThreshold
nvmlDeviceSetVgpuSchedulerState
nvmlDeviceSetVirtualizationMode
Expand Down Expand Up @@ -165,8 +139,8 @@ nvmlVgpuInstanceSetEncoderCapacity
nvmlVgpuTypeGetCapabilities
nvmlVgpuTypeGetClass
nvmlVgpuTypeGetDeviceID
nvmlVgpuTypeGetFrameRateLimit
nvmlVgpuTypeGetFramebufferSize
nvmlVgpuTypeGetFrameRateLimit
nvmlVgpuTypeGetGpuInstanceProfileId
nvmlVgpuTypeGetLicense
nvmlVgpuTypeGetMaxInstances
Expand All @@ -182,11 +156,14 @@ this means some version is already wrapped and the listed names are either
newer versions to be wrapped or older versions that could be wrapped behind the
legacy-functions feature.

nvmlDeviceGetComputeRunningProcesses
nvmlDeviceGetCount
nvmlDeviceGetFanSpeed
nvmlDeviceGetGraphicsRunningProcesses
nvmlDeviceGetHandleByIndex
nvmlDeviceGetHandleByPciBusId
nvmlDeviceGetMemoryInfo_v2
nvmlDeviceGetNvLinkRemotePciInfo
nvmlDeviceGetPciInfo
nvmlDeviceGetPciInfo_v2
nvmlDeviceRemoveGpu
Expand Down
Loading