Skip to content

Commit

Permalink
other: handle systems with only libnvidia-ml.so.1
Browse files Browse the repository at this point in the history
Recently, NVIDIA CUDA repository packages started shipping only
`libnvidia-ml.so.1` file, without `libnvidia-ml.so`. The upstream
`nvml-wrapper` package has a fix proposed
(Cldfire/nvml-wrapper#63), yet the package is
in search of a maintainer at the moment.

To allow `bottom` to correctly detect NVIDIA GPUs on Ubuntu with
official NVIDIA packages, add a wrapper around `Nvml::init` to be more
persistent in its search for the NVML library.
  • Loading branch information
al42and committed Jan 4, 2025
1 parent dbda1ee commit c479974
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/data_collection/nvidia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,35 @@ pub struct GpusData {
pub procs: Option<(u64, Vec<HashMap<u32, (u64, u32)>>)>,
}

/// Wrapper around Nvml::init
///
/// On Linux, if `Nvml::init()` fails, this function attempts to explicitly load
/// the library from `libnvidia-ml.so.1`. On other platforms, it simply calls `Nvml::init`.
///
/// This is a workaround until https://github.com/Cldfire/nvml-wrapper/pull/63 is accepted.
/// Then, we can gp back to just using `Nvml::init` on all platforms.
fn init_nvml() -> Result<Nvml, NvmlError> {
#[cfg(not(target_os = "linux"))]
{
Nvml::init()
}
#[cfg(target_os = "linux")]
{
match Nvml::init() {
Ok(nvml) => Ok(nvml),
Err(_) => Nvml::builder()
.lib_path(std::ffi::OsStr::new("libnvidia-ml.so.1"))
.init(),
}
}
}

/// Returns the GPU data from NVIDIA cards.
#[inline]
pub fn get_nvidia_vecs(
temp_type: &TemperatureType, filter: &Option<Filter>, widgets_to_harvest: &UsedWidgets,
) -> Option<GpusData> {
if let Ok(nvml) = NVML_DATA.get_or_init(Nvml::init) {
if let Ok(nvml) = NVML_DATA.get_or_init(init_nvml) {
if let Ok(num_gpu) = nvml.device_count() {
let mut temp_vec = Vec::with_capacity(num_gpu as usize);
let mut mem_vec = Vec::with_capacity(num_gpu as usize);
Expand Down

0 comments on commit c479974

Please sign in to comment.