1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#[link(name = "nvidia-ml")]
use nvml_binding::bindings::nvmlBAR1Memory_t;
use nvml_binding::bindings::nvmlClockType_enum_NVML_CLOCK_MEM;
use nvml_binding::bindings::nvmlClockType_enum_NVML_CLOCK_SM;
use nvml_binding::bindings::nvmlDeviceGetBAR1MemoryInfo;
use nvml_binding::bindings::nvmlDeviceGetClockInfo;
use nvml_binding::bindings::nvmlDeviceGetCudaComputeCapability;
use nvml_binding::bindings::nvmlDeviceGetHandleByIndex_v2;
use nvml_binding::bindings::nvmlDeviceGetMaxPcieLinkGeneration;
use nvml_binding::bindings::nvmlDeviceGetMaxPcieLinkWidth;
use nvml_binding::bindings::nvmlDeviceGetMemoryInfo;
use nvml_binding::bindings::nvmlDeviceGetMinorNumber;
use nvml_binding::bindings::nvmlDeviceGetName;
use nvml_binding::bindings::nvmlDeviceGetPciInfo_v3;
use nvml_binding::bindings::nvmlDeviceGetPowerManagementLimit;
use nvml_binding::bindings::nvmlDeviceGetUUID;
use nvml_binding::bindings::nvmlDevice_t;
use nvml_binding::bindings::nvmlMemory_t;
use nvml_binding::bindings::nvmlPciInfo_t;
use nvml_binding::bindings::nvmlReturn_enum_NVML_ERROR_NOT_SUPPORTED;
use nvml_binding::bindings::nvmlReturn_enum_NVML_SUCCESS;
use nvml_binding::bindings::nvmlSystemGetDriverVersion;
use nvml_binding::bindings::NVML_DEVICE_NAME_BUFFER_SIZE;
use nvml_binding::bindings::NVML_DEVICE_UUID_BUFFER_SIZE;
use nvml_binding::bindings::NVML_SYSTEM_DRIVER_VERSION_BUFFER_SIZE;

// If you want to use the function of c as the callback function of rust,
// the function type must be marked as unsafe extern "C"
type ProcessOneInterger =
    unsafe extern "C" fn(*mut nvml_binding::bindings::nvmlDevice_st, *mut u32) -> u32;

pub struct NVML;

impl NVML {
    pub fn new() -> Result<NVML, String> {
        unsafe {
            let result = nvml_binding::bindings::nvmlInit_v2();
            if result == nvml_binding::bindings::nvmlReturn_enum_NVML_ERROR_LIBRARY_NOT_FOUND {
                return Err(format!("could not local NVML library"));
            }
            match result_error(result) {
                None => Ok(NVML),
                Some(message) => Err(message),
            }
        }
    }

    pub fn device_get_count(&self) -> Result<u32, String> {
        unsafe {
            let mut n: ::std::os::raw::c_uint = 0;
            let result = nvml_binding::bindings::nvmlDeviceGetCount_v2(&mut n as *mut u32);
            if result == nvml_binding::bindings::nvmlReturn_enum_NVML_SUCCESS {
                return Ok(n as u32);
            }
            Err(result_error(result).unwrap())
        }
    }

    pub fn nvml_system_get_driver_version(&self) -> Result<String, String> {
        unsafe {
            let mut driver: [::std::os::raw::c_char;
                NVML_SYSTEM_DRIVER_VERSION_BUFFER_SIZE as usize] =
                [0; NVML_SYSTEM_DRIVER_VERSION_BUFFER_SIZE as usize];
            let result =
                nvmlSystemGetDriverVersion(&mut driver[0], NVML_SYSTEM_DRIVER_VERSION_BUFFER_SIZE);
            if result == nvmlReturn_enum_NVML_SUCCESS {
                return Ok(std::ffi::CStr::from_ptr(driver.as_ptr() as *const _)
                    .to_str()
                    .unwrap()
                    .to_owned());
            }
            Err(result_error(result).unwrap())
        }
    }
}

impl Drop for NVML {
    fn drop(&mut self) {
        unsafe {
            nvml_binding::bindings::nvmlShutdown();
        }
    }
}

pub fn result_error(result: nvml_binding::bindings::nvmlReturn_t) -> Option<String> {
    unsafe {
        if result == nvml_binding::bindings::nvmlReturn_enum_NVML_SUCCESS {
            return None;
        }
        let ptr = nvml_binding::bindings::nvmlErrorString(result);
        let message = std::ffi::CStr::from_ptr(ptr).to_str().unwrap().to_owned();
        Some(message)
    }
}

pub struct PCIInfo {
    pub bus_id: String,
    pub bar1: u64,
    pub bandwidth: u64,
}

pub struct ClockInfo {
    pub cores: u64,
    pub memory: u64,
}

pub struct P2PLink {
    pub bus_id: String,
    pub link: P2PLinkType,
}

pub enum P2PLinkType {
    P2PLinkUnknown = 0,
    P2PLinkCrossCPU = 1,
    P2PLinkSameCPU = 2,
    P2PLinkHostBridge = 3,
    P2PLinkMultiSwitch = 4,
    P2PLinkSingleSwitch = 5,
    P2PLinkSameBoard = 6,
    SingleNVLINKLink = 7,
    TwoNVLINKLinks = 8,
    ThreeNVLINKLinks = 9,
    FourNVLINKLinks = 10,
    FiveNVLINKLinks = 11,
    SixNVLINKLinks = 12,
}
#[derive(Debug)]
pub struct CudaComputeCapabilityInfo {
    pub major: u64,
    pub minor: u64,
}

pub struct Device {
    pub handler: Handler,
    pub uuid: String,
    pub path: String,
    pub model: String,
    pub power: u64,
    pub memory: u64,
    pub cpu_affinity: u64,
    pub pci: PCIInfo,
    pub clocks: ClockInfo,
    pub topology: Vec<i8>,
    pub cuda_compute_capability: CudaComputeCapabilityInfo,
}

impl Device {
    pub fn new(index: u32) -> Result<Self, String> {
        let handler = Handler::new(index)?;
        let model = handler.get_name()?;
        let uuid = handler.get_uuid()?;
        let minor_count = handler.get_minor_number()?;
        let power = handler.get_power_management_limit()?;
        let memory_info = handler.get_memory_info()?;
        let bus_id = handler.get_pci_info()?;
        let (bar1, _) = handler.get_bar1_memory_info()?;
        let pcig = handler.get_max_pcie_link_generation()?;
        let pciw = handler.get_max_pcie_link_width()?;
        let (cores, mem) = handler.get_clock_info()?;
        let (major, minor) = handler.get_cuda_compute_capability()?;
        let node = Self::numa_node(&bus_id)?;
        Ok(Device {
            handler,
            uuid,
            path: format!("/dev/nvidia{}", minor_count),
            model,
            power: power,
            memory: memory_info.total,
            cpu_affinity: node,
            pci: PCIInfo {
                bus_id,
                bar1,
                bandwidth: Self::pci_bandwidth(pcig, pciw),
            },
            clocks: ClockInfo { cores, memory: mem },
            topology: vec![],
            cuda_compute_capability: CudaComputeCapabilityInfo { minor, major },
        })
    }

    fn numa_node(bus_id: &str) -> Result<u64, String> {
        let filepath = format!("/sys/bus/pci/devices/{}/numa_node", bus_id.to_lowercase());
        match std::fs::read_to_string(&filepath) {
            Ok(content) => match content.parse() {
                Ok(node) => Ok(node),
                Err(e) => Err(format!("{}", e)),
            },
            Err(_) => Ok(0),
        }
    }
    fn pci_bandwidth(gen: u64, width: u64) -> u64 {
        width
            * match gen {
                1 => 250,
                2 => 500,
                3 => 985,
                4 => 1969,
                _ => 0,
            }
    }
}

pub struct Handler {
    pub dev: nvmlDevice_t,
}

impl Handler {
    pub fn new(index: u32) -> Result<Handler, String> {
        unsafe {
            let mut dev: nvmlDevice_t = std::ptr::null_mut();
            let result = nvmlDeviceGetHandleByIndex_v2(
                index as ::std::os::raw::c_uint,
                &mut dev as *mut nvmlDevice_t,
            );
            if result != nvmlReturn_enum_NVML_SUCCESS {
                return Err(result_error(result).unwrap());
            }
            Ok(Handler { dev })
        }
    }
    pub fn get_name(&self) -> Result<String, String> {
        unsafe {
            let mut name: [::std::os::raw::c_char; NVML_DEVICE_NAME_BUFFER_SIZE as usize] =
                [0; NVML_DEVICE_NAME_BUFFER_SIZE as usize];
            let result = nvmlDeviceGetName(self.dev, &mut name[0], NVML_DEVICE_NAME_BUFFER_SIZE);
            if result == nvmlReturn_enum_NVML_ERROR_NOT_SUPPORTED {
                return Err(String::from("not supported"));
            }
            if result == nvmlReturn_enum_NVML_SUCCESS {
                return Ok(std::ffi::CStr::from_ptr(name.as_ptr() as *const _)
                    .to_str()
                    .unwrap()
                    .to_owned());
            }
            Err(result_error(result).unwrap())
        }
    }
    pub fn get_uuid(&self) -> Result<String, String> {
        unsafe {
            let mut name: [::std::os::raw::c_char; NVML_DEVICE_UUID_BUFFER_SIZE as usize] =
                [0; NVML_DEVICE_UUID_BUFFER_SIZE as usize];
            let result = nvmlDeviceGetUUID(self.dev, &mut name[0], NVML_DEVICE_UUID_BUFFER_SIZE);
            if result == nvmlReturn_enum_NVML_ERROR_NOT_SUPPORTED {
                return Err(String::from("not supported"));
            }
            if result == nvmlReturn_enum_NVML_SUCCESS {
                return Ok(std::ffi::CStr::from_ptr(name.as_ptr() as *const _)
                    .to_str()
                    .unwrap()
                    .to_owned());
            }
            Err(result_error(result).unwrap())
        }
    }

    pub fn get_pci_info(&self) -> Result<String, String> {
        unsafe {
            let mut pci_info: nvmlPciInfo_t = std::mem::uninitialized();
            let result = nvmlDeviceGetPciInfo_v3(self.dev, &mut pci_info as *mut nvmlPciInfo_t);
            if result == nvmlReturn_enum_NVML_ERROR_NOT_SUPPORTED {
                return Err(String::from("not supported"));
            }
            if result == nvmlReturn_enum_NVML_SUCCESS {
                return Ok(
                    std::ffi::CStr::from_ptr(pci_info.busId.as_ptr() as *const _)
                        .to_str()
                        .unwrap()
                        .to_owned(),
                );
            }
            Err(result_error(result).unwrap())
        }
    }

    pub fn get_bar1_memory_info(&self) -> Result<(u64, u64), String> {
        unsafe {
            let mut bar1_memory_info: nvmlBAR1Memory_t = std::mem::uninitialized();
            let result = nvmlDeviceGetBAR1MemoryInfo(
                self.dev,
                &mut bar1_memory_info as *mut nvmlBAR1Memory_t,
            );
            if result == nvmlReturn_enum_NVML_ERROR_NOT_SUPPORTED {
                return Err(String::from("not supported"));
            }
            if result == nvmlReturn_enum_NVML_SUCCESS {
                return Ok((
                    bar1_memory_info.bar1Total as u64,
                    bar1_memory_info.bar1Used as u64,
                ));
            }
            Err(result_error(result).unwrap())
        }
    }

    pub fn get_clock_info(&self) -> Result<(u64, u64), String> {
        unsafe {
            let mut sm: ::std::os::raw::c_uint = 0;
            let mut mem: ::std::os::raw::c_uint = 0;
            let mut result = nvmlDeviceGetClockInfo(
                self.dev,
                nvmlClockType_enum_NVML_CLOCK_SM,
                &mut sm as *mut ::std::os::raw::c_uint,
            );
            if result == nvmlReturn_enum_NVML_ERROR_NOT_SUPPORTED {
                return Err(String::from("not supported"));
            }
            if result == nvmlReturn_enum_NVML_SUCCESS {
                result = nvmlDeviceGetClockInfo(
                    self.dev,
                    nvmlClockType_enum_NVML_CLOCK_MEM,
                    &mut mem as *mut ::std::os::raw::c_uint,
                );
            }
            if result == nvmlReturn_enum_NVML_ERROR_NOT_SUPPORTED {
                return Err(String::from("not supported"));
            }
            if result == nvmlReturn_enum_NVML_SUCCESS {
                return Ok((sm as u64, mem as u64));
            }
            Err(result_error(result).unwrap())
        }
    }
    pub fn get_cuda_compute_capability(&self) -> Result<(u64, u64), String> {
        unsafe {
            let mut major: ::std::os::raw::c_int = 0;
            let mut minor: ::std::os::raw::c_int = 0;
            let result = nvmlDeviceGetCudaComputeCapability(
                self.dev,
                &mut major as *mut ::std::os::raw::c_int,
                &mut minor as *mut ::std::os::raw::c_int,
            );
            if result != nvmlReturn_enum_NVML_SUCCESS {
                return Err(result_error(result).unwrap());
            }
            return Ok((major as u64, minor as u64));
        }
    }

    pub fn get_memory_info(&self) -> Result<nvmlMemory_t, String> {
        unsafe {
            let mut mem: nvmlMemory_t = std::mem::uninitialized();
            let mut result = nvmlDeviceGetMemoryInfo(self.dev, &mut mem as *mut nvmlMemory_t);
            if result == nvmlReturn_enum_NVML_ERROR_NOT_SUPPORTED {
                return Err(String::from("not supported"));
            }
            if result != nvmlReturn_enum_NVML_SUCCESS {
                return Err(result_error(result).unwrap());
            }
            Ok(mem)
        }
    }

    pub fn get_minor_number(&self) -> Result<u64, String> {
        unsafe { self.get_one_interger(nvmlDeviceGetMinorNumber) }
    }

    pub fn get_power_management_limit(&self) -> Result<u64, String> {
        unsafe { self.get_one_interger(nvmlDeviceGetPowerManagementLimit) }
    }

    pub fn get_max_pcie_link_generation(&self) -> Result<u64, String> {
        unsafe { self.get_one_interger(nvmlDeviceGetMaxPcieLinkGeneration) }
    }

    pub fn get_max_pcie_link_width(&self) -> Result<u64, String> {
        unsafe { self.get_one_interger(nvmlDeviceGetMaxPcieLinkWidth) }
    }
}

impl Handler {
    extern "C" fn get_one_interger(&self, f: ProcessOneInterger) -> Result<u64, String> {
        unsafe {
            let mut n: ::std::os::raw::c_uint = 0;
            let result = f(self.dev, &mut n as *mut ::std::os::raw::c_uint);
            if result == nvmlReturn_enum_NVML_ERROR_NOT_SUPPORTED {
                return Err(String::from("not supported"));
            }
            if result == nvmlReturn_enum_NVML_SUCCESS {
                return Ok(n as u64);
            }
            Err(result_error(result).unwrap())
        }
    }
}

#[cfg(test)]
mod test {
    #[test]
    fn test() {
        let nvml = super::NVML::new().unwrap();
    }
}