1 //! The probe, on fixture text and on this machine.2 3 use inf3r_host::Host;4 5 const SONAMES: &str = "\tlibcudnn.so.9 (libc6,x86-64) => /usr/lib/libcudnn.so.9\n\tlibcudart.so.12 (libc6,x86-64) => /usr/lib/libcudart.so.12\n\tlibcuda.so (libc6,x86-64) => /usr/lib/libcuda.so\n\tlibc.so.6 (libc6,x86-64) => /usr/lib/libc.so.6\n";6 7 fn host() -> Host {8 Host {9 arch: "x86_64".into(),10 driver: Some("0.0.0".into()),11 cuda: Some("13.0".into()),12 gpus: vec![inf3r_host::Gpu {13 id: "cuda:0".into(),14 name: "Synthetic GPU".into(),15 bus: "0000:05:00.0".into(),16 bytes: Some(12 * 1024 * 1024 * 1024),17 free_bytes: Some(11 * 1024 * 1024 * 1024),18 compute: Some("sm00".into()),19 }],20 sonames: SONAMES21 .lines()22 .filter_map(|line| line.split_whitespace().next())23 .map(str::to_string)24 .collect(),25 }26 }27 28 #[test]29 fn a_requires_entry_matches_the_major_version_of_a_soname() {30 let host = host();31 assert!(host.has_library("cudnn>=9"));32 assert!(host.has_library("cudnn"));33 assert!(host.has_library("cudart>=12"));34 assert!(!host.has_library("cudnn>=10"));35 assert!(!host.has_library("cublas"));36 // `libcuda.so` has no major component, so it satisfies the bare name and37 // cannot satisfy a floor.38 assert!(host.has_library("cuda"));39 assert!(!host.has_library("cuda>=1"));40 }41 42 #[test]43 fn a_device_is_found_by_its_catalogue_identifier() {44 let host = host();45 assert_eq!(host.gpu("cuda:0").unwrap().bus, "0000:05:00.0");46 assert_eq!(host.gpu("cuda:0").unwrap().backend(), "cuda");47 assert_eq!(host.gpu("cuda:1"), None);48 assert_eq!(host.total_bytes(), 12 * 1024 * 1024 * 1024);49 }50 51 #[test]52 fn a_device_without_a_measured_size_contributes_nothing() {53 let mut host = host();54 host.gpus[0].bytes = None;55 assert_eq!(host.total_bytes(), 0);56 }57 58 #[test]59 fn the_probe_reads_this_machine() {60 let host = Host::probe();61 62 assert_eq!(host.arch, std::env::consts::ARCH);63 // The probe must not claim a device that it cannot open, and it must not64 // invent a size for one that it can.65 for gpu in &host.gpus {66 assert!(gpu.id.contains(':'), "{} has no backend", gpu.id);67 assert!(68 matches!(gpu.backend(), "cuda" | "rocm" | "vulkan" | "metal" | "cpu"),69 "{} has an unknown backend",70 gpu.id71 );72 assert!(!gpu.bus.is_empty());73 if let Some(bytes) = gpu.bytes {74 assert!(bytes > 0, "{} has 0 bytes", gpu.id);75 }76 if let (Some(free), Some(total)) = (gpu.free_bytes, gpu.bytes) {77 assert!(free <= total, "{} reports {free} free of {total}", gpu.id);78 }79 }80 // A machine with a driver but no readable version is a bug in the parser.81 if std::path::Path::new("/proc/driver/nvidia/version").exists() {82 assert!(host.driver.is_some(), "the driver version did not parse");83 }84 let cpu = host.gpu("cpu:0").expect("the CPU is a device");85 assert_eq!(cpu.backend(), "cpu");86 assert!(cpu.bytes.is_some_and(|bytes| bytes > 0));87 }88 89 #[test]90 fn free_bytes_is_measured_or_absent_but_never_above_the_device() {91 for gpu in &Host::probe().gpus {92 if let (Some(free), Some(total)) = (gpu.free_bytes, gpu.bytes) {93 assert!(free <= total, "{} reports {free} free of {total}", gpu.id);94 }95 }96 }97 98 #[test]99 fn the_soname_parser_keeps_libraries_and_drops_paths() {100 let names = inf3r_host::Host::probe().sonames;101 if names.is_empty() {102 return;103 }104 for name in &names {105 assert!(name.starts_with("lib"), "{name} is not a soname");106 assert!(!name.contains('/'), "{name} is a path");107 }108 }109 110 #[test]111 fn a_process_tree_names_the_process_and_its_child() {112 let mut child = std::process::Command::new("sh")113 .args(["-c", "sleep 5"])114 .spawn()115 .expect("spawn a child");116 let tree = inf3r_host::process_tree(std::process::id());117 assert!(tree.contains(&std::process::id()));118 assert!(tree.contains(&child.id()), "{tree:?}");119 let measured = inf3r_host::process_tree_bytes(std::process::id()).expect("rss");120 assert!(measured > 0);121 let _ = child.kill();122 let _ = child.wait();123 }124 125 #[test]126 fn a_gpu_free_query_never_claims_more_than_the_device() {127 let host = Host::probe();128 for gpu in &host.gpus {129 if let (Some(free), Some(total)) = (inf3r_host::device_free_bytes(&gpu.id), gpu.bytes) {130 assert!(free <= total, "{} reports {free} free of {total}", gpu.id);131 }132 }133 assert_eq!(inf3r_host::device_free_bytes("gpu0:0"), None);134 }135 136 #[test]137 fn a_host_record_round_trips_through_the_probe_boundary() {138 let host = inf3r_host::Host {139 arch: "x86_64".into(),140 driver: Some("615.1".into()),141 cuda: Some("13.4".into()),142 gpus: vec![143 inf3r_host::Gpu {144 id: "cuda:0".into(),145 name: "NVIDIA test".into(),146 bus: "0000:05:00.0".into(),147 bytes: Some(12_884_901_888),148 free_bytes: None,149 compute: Some("sm00".into()),150 },151 inf3r_host::Gpu {152 id: "cpu:0".into(),153 name: "CPU".into(),154 bus: "cpu".into(),155 bytes: Some(1 << 40),156 free_bytes: Some(1 << 39),157 compute: None,158 },159 ],160 sonames: ["libcudart.so.13".to_string()].into_iter().collect(),161 };162 let decoded = inf3r_host::Host::decode(&host.encode()).expect("the record decodes");163 assert_eq!(decoded, host);164 165 assert!(inf3r_host::Host::decode("host x86_64\n").is_none());166 assert!(inf3r_host::Host::decode("host\tx86_64\ngpu\tonly\n").is_none());167 }