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 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
/*! A complete, safe, and ergonomic Rust wrapper for the [NVIDIA Management Library] (https://developer.nvidia.com/nvidia-management-library-nvml) (NVML), a C-based programmatic interface for monitoring and managing various states within NVIDIA (primarily Tesla) GPUs. ``` # use nvml_wrapper::NVML; # use nvml_wrapper::error::*; # fn main() { # test().unwrap(); # } # fn test() -> Result<()> { let nvml = NVML::init()?; // Get the first `Device` (GPU) in the system let device = nvml.device_by_index(0)?; let brand = device.brand()?; // GeForce on my system let fan_speed = device.fan_speed()?; // Currently 17% on my system let power_limit = device.enforced_power_limit()?; // 275k milliwatts on my system let encoder_util = device.encoder_utilization()?; // Currently 0 on my system; Not encoding anything let memory_info = device.memory_info()?; // Currently 1.63/6.37 GB used on my system // ... and there's a whole lot more you can do. Everything in NVML is wrapped and ready to go // (except for a few (~9) NvLink-related items that I will get to soon) # Ok(()) # } ``` NVML is intended to be a platform for building 3rd-party applications, and is also the underlying library for NVIDIA's nvidia-smi tool. It supports the following platforms: * Windows * Windows Server 2008 R2 64-bit * Windows Server 2012 R2 64-bit * Windows 7 64-bit * Windows 8 64-bit * Windows 10 64-bit * Linux * 64-bit * 32-bit * Hypervisors * Windows Server 2008R2/2012 Hyper-V 64-bit * Citrix XenServer 6.2 SP1+ * VMware ESX 5.1/5.5 And the following products: * Full Support * Tesla products Fermi architecture and up * Quadro products Fermi architecture and up * GRID products Kepler architecture and up * Select GeForce Titan products * Limited Support * All GeForce products Fermi architecture and up Although NVIDIA does not explicitly support it, most of the functionality offered by NVML works on my dev machine (980 Ti). Even if your device is not on the list, try it out and see what works: ```bash cargo test ``` ## Compilation The NVML library comes with the NVIDIA drivers and is essentially present on any system with a functioning NVIDIA graphics card. The compilation steps vary between Windows and Linux, however. ### Windows The NVML library dll can be found at `%ProgramW6432%\NVIDIA Corporation\NVSMI\` (which is `C:\Program Files\NVIDIA Corporation\NVSMI\` on my machine). You will need to add this folder to your `PATH` in order to have everything work properly at runtime; alternatively, place a copy of the dll in the same folder as your executable. ### Linux The NVML library can be found at `/usr/lib/nvidia-<driver-version>/libnvidia-ml.so`; on my system with driver version 375.51 installed, this puts the library at `/usr/lib/nvidia-375/libnvidia-ml.so`. You will need to create a symbolic link: ```bash sudo ln -s /usr/lib/nvidia-<driver-version>/libnvidia-ml.so /usr/lib ``` ## Rustc Support Currently supports rustc 1.17.0 or greater. The target version is the **latest** stable version; I do not intend to pin to an older one at any time. A small amount of NVML features involve dealing with untagged unions over FFI; a rustc nightly-only type is used in order to facilitate this. If you require use of the nightly-only functionality, compile with the `nightly` feature toggled on (and of course, with a nightly compiler): ```bash cargo build --features "nightly" ``` ## Cargo Features The `nightly` feature can be toggled on to enable nightly-only features; read above. The `serde` feature can be toggled on in order to `#[derive(Serialize, Deserialize)]` for every NVML data structure. */ #![recursion_limit = "1024"] #![cfg_attr(feature = "cargo-clippy", allow(doc_markdown))] #[macro_use] extern crate error_chain; #[macro_use] extern crate bitflags; #[macro_use] extern crate wrapcenum_derive; #[cfg(feature = "serde")] #[macro_use] extern crate serde; extern crate nvml_wrapper_sys as ffi; pub mod device; pub mod error; pub mod unit; pub mod structs; pub mod struct_wrappers; pub mod enums; pub mod enum_wrappers; pub mod event; pub mod bitmasks; #[cfg(test)] mod test_utils; // Re-exports for convenience pub use device::Device; pub use unit::Unit; pub use event::EventSet; use error::*; use ffi::bindings::*; use std::os::raw::{c_uint, c_int}; use std::ffi::{CStr, CString}; #[cfg(target_os = "linux")] use std::ptr; use std::mem; #[cfg(target_os = "linux")] use enum_wrappers::device::*; #[cfg(target_os = "linux")] use struct_wrappers::device::PciInfo; use struct_wrappers::unit::HwbcEntry; use std::io; use std::io::Write; /** The main struct that this library revolves around. According to NVIDIA's documentation, "It is the user's responsibility to call `nvmlInit()` before calling any other methods, and `nvmlShutdown()` once NVML is no longer being used." This struct is used to enforce those rules. Also according to NVIDIA's documentation, "NVML is thread-safe so it is safe to make simultaneous NVML calls from multiple threads." In the Rust world, this translates to `NVML` being `Send` + `Sync`. You can `.clone()` an `Arc` wrapped `NVML` and enjoy using it on any thread. NOTE: If you care about possible errors returned from `nvmlShutdown()`, use the `.shutdown()` method on this struct. **The `Drop` implementation ignores errors.** When reading documentation on this struct and its members, remember that a lot of it, especially in regards to errors returned, is copied from NVIDIA's docs. While they can be found online [here](http://docs.nvidia.com/deploy/nvml-api/index.html), the hosted docs are outdated and do not accurately reflect the version of NVML that this library is written for; beware. You should ideally read the doc comments on an up-to-date NVML API header. Such a header can be downloaded as part of the [CUDA toolkit](https://developer.nvidia.com/cuda-downloads). */ #[derive(Debug)] pub struct NVML; // Here to clarify that NVML does have these traits. I know they are implemented without this. unsafe impl Send for NVML {} unsafe impl Sync for NVML {} impl NVML { /** Handles NVML initilization and must be called before doing anything else. This static function can be called multiple times and multiple NVML structs can be used at the same time. NVIDIA's docs state that "A reference count of the number of initializations is maintained. Shutdown only occurs when the reference count reaches zero." In practice, there should be no need to create multiple `NVML` structs; wrap this struct in an `Arc` and go that route. Note that this will initialize NVML but not any GPUs. This means that NVML can communicate with a GPU even when other GPUs in a system are bad or unstable. # Errors * `DriverNotLoaded`, if the NVIDIA driver is not running * `NoPermission`, if NVML does not have permission to talk to the driver * `Unknown`, on any unexpected error */ // Checked against local #[inline] pub fn init() -> Result<Self> { unsafe { nvml_try(nvmlInit_v2())?; } Ok(NVML) } /** Use this to shutdown NVML and release allocated resources if you care about handling potential errors (*the `Drop` implementation ignores errors!*). # Errors * `Uninitialized`, if the library has not been successfully initialized * `Unknown`, on any unexpected error */ // Thanks to `sorear` on IRC for suggesting this approach // Checked against local #[inline] pub fn shutdown(self) -> Result<()> { unsafe { nvml_try(nvmlShutdown())?; } Ok(mem::forget(self)) } /** Get the number of compute devices in the system (compute device == one GPU). Note that this may return devices you do not have permission to access. # Errors * `Uninitialized`, if the library has not been successfully initialized * `Unknown`, on any unexpected error */ // Checked against local // Tested #[inline] pub fn device_count(&self) -> Result<u32> { unsafe { let mut count: c_uint = mem::zeroed(); nvml_try(nvmlDeviceGetCount_v2(&mut count))?; Ok(count as u32) } } /** Gets the version of the system's graphics driver and returns it as an alphanumeric string. # Errors * `Uninitialized`, if the library has not been successfully initialized * `Utf8Error`, if the string obtained from the C function is not valid Utf8 */ // Checked against local // Tested #[inline] pub fn sys_driver_version(&self) -> Result<String> { unsafe { let mut version_vec = Vec::with_capacity(NVML_SYSTEM_DRIVER_VERSION_BUFFER_SIZE as usize); nvml_try(nvmlSystemGetDriverVersion(version_vec.as_mut_ptr(), NVML_SYSTEM_DRIVER_VERSION_BUFFER_SIZE))?; let version_raw = CStr::from_ptr(version_vec.as_ptr()); Ok(version_raw.to_str()?.into()) } } /** Gets the version of the system's NVML library and returns it as an alphanumeric string. # Errors * `Uninitialized`, if the library has not been successfully initialized * `Utf8Error`, if the string obtained from the C function is not valid Utf8 */ // Checked against local // Tested #[inline] pub fn sys_nvml_version(&self) -> Result<String> { unsafe { let mut version_vec = Vec::with_capacity(NVML_SYSTEM_NVML_VERSION_BUFFER_SIZE as usize); nvml_try(nvmlSystemGetNVMLVersion(version_vec.as_mut_ptr(), NVML_SYSTEM_NVML_VERSION_BUFFER_SIZE))?; // Thanks to `Amaranth` on IRC for help with this let version_raw = CStr::from_ptr(version_vec.as_ptr()); Ok(version_raw.to_str()?.into()) } } /** Gets the name of the process for the given process ID, cropped to the provided length. # Errors * `Uninitialized`, if the library has not been successfully initialized * `InvalidArg`, if the length is 0 (if this is returned without length being 0, file an issue) * `NotFound`, if the process does not exist * `NoPermission`, if the user doesn't have permission to perform the operation * `Utf8Error`, if the string obtained from the C function is not valid UTF-8. NVIDIA's docs say that the string encoding is ANSI, so this may very well happen. * `Unknown`, on any unexpected error */ // TODO: The docs say the string is ANSI-encoded. Not sure if I should try to do anything about that // Checked against local // Tested #[inline] pub fn sys_process_name(&self, pid: u32, length: usize) -> Result<String> { unsafe { let mut name_vec = Vec::with_capacity(length); nvml_try(nvmlSystemGetProcessName(pid, name_vec.as_mut_ptr(), length as c_uint))?; let name_raw = CStr::from_ptr(name_vec.as_ptr()); Ok(name_raw.to_str()?.into()) } } /** Acquire the handle for a particular device based on its index (starts at 0). Usage of this function causes NVML to initialize the target GPU. Additional GPUs may be initialized if the target GPU is an SLI slave. You can determine valid indices by using `.get_device_count()`. This function doesn't call that for you, but the actual C function to get the device handle will return an error in the case of an invalid index. This means that the `InvalidArg` error will be returned if you pass in an invalid index. NVIDIA's docs state that "The order in which NVML enumerates devices has no guarantees of consistency between reboots. For that reason it is recommended that devices be looked up by their PCI ids or UUID." In this library, that translates into usage of `.device_by_uuid()` and `.device_by_pci_bus_id()`. The NVML index may not correlate with other APIs such as the CUDA device index. # Errors * `Uninitialized`, if the library has not been successfully initialized * `InvalidArg`, if index is invalid * `InsufficientPower`, if any attached devices have improperly attached external power cables * `NoPermission`, if the user doesn't have permission to talk to this device * `IrqIssue`, if the NVIDIA kernel detected an interrupt issue with the attached GPUs * `GpuLost`, if the target GPU has fallen off the bus or is otherwise inaccessible * `Unknown`, on any unexpected error */ // Checked against local // Tested #[inline] pub fn device_by_index(&self, index: u32) -> Result<Device> { unsafe { let mut device: nvmlDevice_t = mem::zeroed(); nvml_try(nvmlDeviceGetHandleByIndex_v2(index, &mut device))?; Ok(device.into()) } } /** Acquire the handle for a particular device based on its PCI bus ID. Usage of this function causes NVML to initialize the target GPU. Additional GPUs may be initialized if the target GPU is an SLI slave. The bus ID corresponds to the `bus_id` returned by `Device.pci_info()`. # Errors * `Uninitialized`, if the library has not been successfully initialized * `InvalidArg`, if `pci_bus_id` is invalid * `NotFound`, if `pci_bus_id` does not match a valid device on the system * `InsufficientPower`, if any attached devices have improperly attached external power cables * `NoPermission`, if the user doesn't have permission to talk to this device * `IrqIssue`, if the NVIDIA kernel detected an interrupt issue with the attached GPUs * `GpuLost`, if the target GPU has fallen off the bus or is otherwise inaccessible * `NulError`, for which you can read the docs on `std::ffi::NulError` * `Unknown`, on any unexpected error */ // Checked against local // Tested #[inline] pub fn device_by_pci_bus_id<S: AsRef<str>>(&self, pci_bus_id: S) -> Result<Device> where Vec<u8>: From<S> { unsafe { let c_string = CString::new(pci_bus_id)?; let mut device: nvmlDevice_t = mem::zeroed(); nvml_try(nvmlDeviceGetHandleByPciBusId_v2(c_string.as_ptr(), &mut device))?; Ok(device.into()) } } /// Not documenting this because it's deprecated and does not seem to work anymore. // Tested (for an error) #[deprecated(note = "use `.device_by_uuid()`, this errors on dual GPU boards")] #[inline] pub fn device_by_serial<S: AsRef<str>>(&self, board_serial: S) -> Result<Device> where Vec<u8>: From<S> { unsafe { let c_string = CString::new(board_serial)?; let mut device: nvmlDevice_t = mem::zeroed(); nvml_try(nvmlDeviceGetHandleBySerial(c_string.as_ptr(), &mut device))?; Ok(device.into()) } } /** Acquire the handle for a particular device based on its globally unique immutable UUID. Usage of this function causes NVML to initialize the target GPU. Additional GPUs may be initialized as the function called within searches for the target GPU. # Errors * `Uninitialized`, if the library has not been successfully initialized * `InvalidArg`, if `uuid` is invalid * `NotFound`, if `uuid` does not match a valid device on the system * `InsufficientPower`, if any attached devices have improperly attached external power cables * `IrqIssue`, if the NVIDIA kernel detected an interrupt issue with the attached GPUs * `GpuLost`, if the target GPU has fallen off the bus or is otherwise inaccessible * `NulError`, for which you can read the docs on `std::ffi::NulError` * `Unknown`, on any unexpected error NVIDIA doesn't mention `NoPermission` for this one. Strange! */ // Checked against local // Tested #[inline] pub fn device_by_uuid<S: AsRef<str>>(&self, uuid: S) -> Result<Device> where Vec<u8>: From<S> { unsafe { let c_string = CString::new(uuid)?; let mut device: nvmlDevice_t = mem::zeroed(); nvml_try(nvmlDeviceGetHandleByUUID(c_string.as_ptr(), &mut device))?; Ok(device.into()) } } /** Gets the common ancestor for two devices. Note: this is the same as `Device.topology_common_ancestor()`. # Errors * `InvalidArg`, if the device is invalid * `NotSupported`, if this `Device` or the OS does not support this feature * `Unknown`, on any unexpected error # Platform Support Only supports Linux. */ // Checked against local #[cfg(target_os = "linux")] #[inline] pub fn topology_common_ancestor(&self, device1: &Device, device2: &Device) -> Result<TopologyLevel> { unsafe { let mut level: nvmlGpuTopologyLevel_t = mem::zeroed(); nvml_try(nvmlDeviceGetTopologyCommonAncestor(device1.unsafe_raw(), device2.unsafe_raw(), &mut level))?; Ok(level.into()) } } /** Acquire the handle for a particular `Unit` based on its index. Valid indices are derived from the count returned by `.unit_count()`. For example, if `unit_count` is 2 the valid indices are 0 and 1, corresponding to UNIT 0 and UNIT 1. Note that the order in which NVML enumerates units has no guarantees of consistency between reboots. # Errors * `Uninitialized`, if the library has not been successfully initialized * `InvalidArg`, if `index` is invalid * `Unknown`, on any unexpected error # Device Support For S-class products. */ // Checked against local // Tested (for an error) #[inline] pub fn unit_by_index(&self, index: u32) -> Result<Unit> { unsafe { let mut unit: nvmlUnit_t = mem::zeroed(); nvml_try(nvmlUnitGetHandleByIndex(index as c_uint, &mut unit))?; Ok(unit.into()) } } /** Checks if the passed-in `Device`s are on the same physical board. Note: this is the same as `Device.is_on_same_board_as()`. # Errors * `Uninitialized`, if the library has not been successfully initialized * `InvalidArg`, if either `Device` is invalid * `NotSupported`, if this check is not supported by this `Device` * `GpuLost`, if this `Device` has fallen off the bus or is otherwise inaccessible * `Unknown`, on any unexpected error */ // Checked against local #[inline] pub fn are_devices_on_same_board(device1: &Device, device2: &Device) -> Result<bool> { unsafe { let mut bool_int: c_int = mem::zeroed(); nvml_try(nvmlDeviceOnSameBoard(device1.unsafe_raw(), device2.unsafe_raw(), &mut bool_int))?; match bool_int { 0 => Ok(false), _ => Ok(true), } } } /** Gets the set of GPUs that have a CPU affinity with the given CPU number. # Errors * `InvalidArg`, if `cpu_number` is invalid * `NotSupported`, if this `Device` or the OS does not support this feature * `Unknown`, an error has occurred in the underlying topology discovery # Platform Support Only supports Linux. */ // Tested #[cfg(target_os = "linux")] #[inline] pub fn topology_gpu_set(&self, cpu_number: u32) -> Result<Vec<Device>> { unsafe { let mut count = match self.topology_gpu_set_count(cpu_number)? { 0 => return Ok(vec![]), value => value, }; let mut devices: Vec<nvmlDevice_t> = vec![mem::zeroed(); count as usize]; // Indexing 0 here is safe because we make sure `count` is not 0 above nvml_try(nvmlSystemGetTopologyGpuSet(cpu_number, &mut count, devices.as_mut_ptr()))?; Ok(devices.iter() .map(|d| Device::from(*d)) .collect()) } } // Helper function for the above. #[cfg(target_os = "linux")] #[inline] fn topology_gpu_set_count(&self, cpu_number: u32) -> Result<c_uint> { unsafe { // Indicates that we want the count let mut count: c_uint = 0; // Passing null doesn't indicate that we want the count, just allowed nvml_try(nvmlSystemGetTopologyGpuSet(cpu_number, &mut count, ptr::null_mut()))?; Ok(count) } } /** Gets the IDs and firmware versions for any Host Interface Cards in the system. # Errors * `Uninitialized`, if the library has not been successfully initialized # Device Support Supports S-class products. */ // Checked against local // Tested #[inline] pub fn hic_versions(&self) -> Result<Vec<HwbcEntry>> { unsafe { let mut count: c_uint = match self.hic_count()? { 0 => return Ok(vec![]), value => value, }; let mut hics: Vec<nvmlHwbcEntry_t> = vec![mem::zeroed(); count as usize]; // Indexing 0 here is safe because we make sure `count` is not 0 above nvml_try(nvmlSystemGetHicVersion(&mut count, hics.as_mut_ptr()))?; hics.iter() .map(|h| HwbcEntry::try_from(*h)) .collect() } } /** Gets the count of Host Interface Cards in the system. # Errors * `Uninitialized`, if the library has not been successfully initialized # Device Support Supports S-class products. */ // Tested as part of the above method #[inline] pub fn hic_count(&self) -> Result<u32> { unsafe { /* NVIDIA doesn't even say that `count` will be set to the count if `InsufficientSize` is returned. But we can assume sanity, right? The idea here is: If there are 0 HICs, NVML_SUCCESS is returned, `count` is set to 0. We return count, all good. If there is 1 HIC, NVML_SUCCESS is returned, `count` is set to 1. We return count, all good. If there are >= 2 HICs, NVML_INSUFFICIENT_SIZE is returned. `count` is theoretically set to the actual count, and we return it. */ let mut count: c_uint = 1; let mut hics: [nvmlHwbcEntry_t; 1] = [mem::zeroed()]; match nvmlSystemGetHicVersion(&mut count, hics.as_mut_ptr()) { nvmlReturn_t::NVML_SUCCESS | nvmlReturn_t::NVML_ERROR_INSUFFICIENT_SIZE => Ok(count), // We know that this will be an error other => nvml_try(other).map(|_| 0), } } } /** Gets the number of units in the system. # Errors * `Uninitialized`, if the library has not been successfully initialized * `Unknown`, on any unexpected error # Device Support Supports S-class products. */ // Checked against local // Tested #[inline] pub fn unit_count(&self) -> Result<u32> { unsafe { let mut count: c_uint = mem::zeroed(); nvml_try(nvmlUnitGetCount(&mut count))?; Ok(count) } } /** Create an empty set of events. # Errors * `Uninitialized`, if the library has not been successfully initialized * `Unknown`, on any unexpected error # Device Support Supports Fermi and newer fully supported devices. */ // Checked against local // Tested #[inline] pub fn create_event_set(&self) -> Result<EventSet> { unsafe { let mut set: nvmlEventSet_t = mem::zeroed(); nvml_try(nvmlEventSetCreate(&mut set))?; Ok(set.into()) } } /** Request the OS and the NVIDIA kernel driver to rediscover a portion of the PCI subsystem in search of GPUs that were previously removed. The portion of the PCI tree can be narrowed by specifying a domain, bus, and device in the passed-in `pci_info`. **If all of these fields are zeroes, the entire PCI tree will be searched.** Note that for long-running NVML processes, the enumeration of devices will change based on how many GPUs are discovered and where they are inserted in bus order. All newly discovered GPUs will be initialized and have their ECC scrubbed which may take several seconds per GPU. **All device handles are no longer guaranteed to be valid post discovery**. I am not sure if this means **all** device handles, literally, or if NVIDIA is referring to handles that had previously been obtained to devices that were then removed and have now been re-discovered. Must be run as administrator. # Errors * `Uninitialized`, if the library has not been successfully initialized * `OperatingSystem`, if the operating system is denying this feature * `NoPermission`, if the calling process has insufficient permissions to perform this operation * `NulError`, if an issue is encountered when trying to convert a Rust `String` into a `CString`. * `Unknown`, on any unexpected error # Device Support Supports Maxwell and newer fully supported devices. Some Kepler devices are also supported (that's all NVIDIA says, no specifics). # Platform Support Only supports Linux. */ // TODO: constructor for default pci_infos ^ // Checked against local #[cfg(target_os = "linux")] #[inline] pub fn discover_gpus(&self, pci_info: PciInfo) -> Result<()> { unsafe { nvml_try(nvmlDeviceDiscoverGpus(&mut pci_info.try_into_c()?)) } } } /// This `Drop` implementation ignores errors! Use the `.shutdown()` method on the `NVML` struct /// if you care about handling them. impl Drop for NVML { fn drop(&mut self) { #[allow(unused_must_use)] unsafe { match nvml_try(nvmlShutdown()) { Ok(()) => (), Err(e) => { io::stderr().write(format!("WARNING: Error returned by \ `nmvlShutdown()` in Drop implementation: {:?}", e).as_bytes()); } } } } } #[cfg(test)] mod test { use super::*; use test_utils::*; #[test] fn nvml_is_send() { assert_send::<NVML>() } #[test] fn nvml_is_sync() { assert_sync::<NVML>() } #[test] fn device_count() { test(3, || { nvml().device_count() }) } #[test] fn sys_driver_version() { test(3, || { nvml().sys_driver_version() }) } #[test] fn sys_nvml_version() { test(3, || { nvml().sys_nvml_version() }) } #[test] fn sys_process_name() { let nvml = nvml(); test_with_device(3, &nvml, |device| { let processes = device.running_graphics_processes()?; nvml.sys_process_name(processes[0].pid, 64) }) } #[test] fn device_by_index() { let nvml = nvml(); test(3, || { nvml.device_by_index(0) }) } #[test] fn device_by_pci_bus_id() { let nvml = nvml(); test_with_device(3, &nvml, |device| { let id = device.pci_info()?.bus_id; nvml.device_by_pci_bus_id(id) }) } // Can't get serial on my machine #[cfg(not(feature = "test-local"))] #[test] fn device_by_serial() { let nvml = nvml(); #[allow(deprecated)] test_with_device(3, &nvml, |device| { let serial = device.serial()?; nvml.device_by_serial(serial) }) } #[test] fn device_by_uuid() { let nvml = nvml(); test_with_device(3, &nvml, |device| { let uuid = device.uuid()?; nvml.device_by_uuid(uuid) }) } // Errors on my machine #[cfg_attr(feature = "test-local", should_panic(expected = "InvalidArg"))] #[test] fn unit_by_index() { let nvml = nvml(); test(3, || { match nvml.unit_by_index(0) { // I have no unit to test with Err(Error(ErrorKind::InvalidArg, _)) => panic!("InvalidArg"), other => other, } }) } #[cfg(target_os = "linux")] #[test] fn topology_gpu_set() { let nvml = nvml(); test(3, || { nvml.topology_gpu_set(0) }) } #[test] fn hic_version() { let nvml = nvml(); test(3, || { nvml.hic_versions() }) } #[test] fn unit_count() { test(3, || { nvml().unit_count() }) } #[test] fn create_event_set() { let nvml = nvml(); test(3, || { nvml.create_event_set() }) } #[cfg(target_os = "linux")] #[should_panic(expected = "NoPermission")] #[test] fn discover_gpus() { let nvml = nvml(); test_with_device(3, &nvml, |device| { let pci_info = device.pci_info()?; // We don't test with admin perms and therefore expect an error match nvml.discover_gpus(pci_info) { Err(Error(ErrorKind::NoPermission, _)) => panic!("NoPermission"), other => other, } }) } }