nvidia_checker/
lib.rs

1use carrot_utils::command::{get_command_output, print_command_result};
2use colored::*;
3use log::info;
4use serde_derive::{Deserialize, Serialize};
5
6#[derive(Debug, Deserialize, Serialize)]
7pub struct NvidiaEnvironment {
8    pub checked_time: String,
9    pub kernel: String,
10    pub os: String,
11    pub nvidia_driver: String,
12    pub cuda: String,
13    pub cudnn: String,
14    pub tensorrt: String,
15}
16
17impl NvidiaEnvironment {
18    pub fn init() -> NvidiaEnvironment {
19        let now_time = chrono::Local::now().format("%Y.%m.%d %H:%M:%S").to_string();
20        let os_version = get_os_version(true);
21        let kernel_version = get_kernel_version(true);
22        let nvidia_driver_version = get_nvidia_driver_version(true);
23        let cuda_version = get_cuda_version(true);
24        let cudnn_version = get_cudnn_version(true);
25        let tensorrt_version = get_tensorrt_version(true);
26        NvidiaEnvironment {
27            checked_time: now_time,
28            os: os_version,
29            kernel: kernel_version,
30            nvidia_driver: nvidia_driver_version,
31            cuda: cuda_version,
32            cudnn: cudnn_version,
33            tensorrt: tensorrt_version,
34        }
35    }
36
37    /// Print check results.
38    /// # Arguments
39    /// - target: Desirable software environment.
40    pub fn print_check_results(&self, target: &NvidiaEnvironment) {
41        info!("===== Check environment =====");
42        print_check_result("os", &self.os, &target.os);
43        print_check_result("kernel", &self.kernel, &target.kernel);
44        print_check_result("nvidia driver", &self.nvidia_driver, &target.nvidia_driver);
45        print_check_result("cuda", &self.cuda, &target.cuda);
46        print_check_result("cudnn", &self.cudnn, &target.cudnn);
47        print_check_result("tensorrt", &self.tensorrt, &target.tensorrt);
48    }
49}
50
51/// Print check result.
52/// # Arguments
53/// - environment: The kind of environment.
54/// - now_version: Software version for now.
55/// - target_version: Desirable software version.
56pub fn print_check_result(environment: &str, now_version: &str, target_version: &str) {
57    let check_string: ColoredString;
58    if now_version == target_version {
59        check_string = "OK".green();
60    } else {
61        check_string = "NG".red();
62    }
63    info!("{}: {}", environment, check_string);
64}
65
66/// Get Kernel version.
67/// # Arguments
68/// - print_terminal: If this parameter is true, print command output to terminal.
69pub fn get_kernel_version(print_terminal: bool) -> String {
70    let command = "uname -r";
71    let command_output = get_command_output(command);
72    if print_terminal == true {
73        print_command_result(command, &command_output);
74    }
75
76    command_output.to_string().replace("\n", "")
77}
78
79/// Get OS version.
80/// # Arguments
81/// - print_terminal: If this parameter is true, print command output to terminal.
82pub fn get_os_version(print_terminal: bool) -> String {
83    let command = "cat /etc/os-release";
84    let command_output = get_command_output(command);
85    if print_terminal == true {
86        print_command_result(command, &command_output);
87    }
88
89    for string_line in command_output.lines() {
90        if string_line.contains("PRETTY_NAME") {
91            let string_array: Vec<&str> = string_line.split("=").collect();
92            let version = string_array[1].to_string().replace("\"", "");
93            return version;
94        }
95    }
96    return "".to_string();
97}
98
99/// Get NVIDIA driver version.
100/// # Arguments
101/// - print_terminal: If this parameter is true, print command output to terminal.
102pub fn get_nvidia_driver_version(print_terminal: bool) -> String {
103    let command = "cat /proc/driver/nvidia/version";
104    let command_output = get_command_output(command);
105    if print_terminal == true {
106        print_command_result(command, &command_output);
107    }
108
109    for string_line in command_output.lines() {
110        let mut string_array: Vec<&str> = string_line.split(" ").collect();
111        string_array.retain(|&x| x != "");
112        return string_array[7].to_string();
113    }
114    return "".to_string();
115}
116
117/// Get CUDA version.
118/// # Arguments
119/// - print_terminal: If this parameter is true, print command output to terminal.
120pub fn get_cuda_version(print_terminal: bool) -> String {
121    let command = "nvcc -V";
122    let command_output = get_command_output(command);
123    if print_terminal == true {
124        print_command_result(command, &command_output);
125    }
126
127    for string_line in command_output.lines() {
128        if string_line.contains("Build") {
129            // "Build cuda_12.3.r12.3/compiler.33567101_0" -> ["Build", "cuda_12.3.r12.3/compiler.33567101_0"]
130            let temp_string_array: Vec<&str> = string_line.split(" ").collect();
131            // "cuda_12.3.r12.3/compiler.33567101_0" -> ["cuda_12.3.r12.3", "compiler.33567101_0"]
132            let temp_string_array_2: Vec<&str> = temp_string_array[1].split("/").collect();
133            let version = temp_string_array_2[0].to_string();
134            return version;
135        }
136    }
137    return "".to_string();
138}
139
140/// Get cuDNN version.
141/// # Arguments
142/// - print_terminal: If this parameter is true, print command output to terminal.
143pub fn get_cudnn_version(print_terminal: bool) -> String {
144    let command = "dpkg -l | grep cudnn";
145    let command_output = get_command_output(command);
146    if print_terminal == true {
147        print_command_result(command, &command_output);
148    }
149    get_dpkg_version(&command_output, "libcudnn8")
150}
151
152/// Get TensorRT version.
153/// # Arguments
154/// - print_terminal: If this parameter is true, print command output to terminal.
155pub fn get_tensorrt_version(print_terminal: bool) -> String {
156    let command = "dpkg -l | grep TensorRT";
157    let command_output = get_command_output(command);
158    if print_terminal == true {
159        print_command_result(command, &command_output);
160    }
161    get_dpkg_version(&command_output, "libnv")
162}
163
164/// Get version string with dpkg command.
165/// # Arguments
166/// - command_output: The output of command
167/// - pattern: Target package name
168fn get_dpkg_version(command_output: &str, pattern: &str) -> String {
169    let mut version_list: Vec<&str> = vec![];
170    for string_line in command_output.lines() {
171        if string_line.contains(pattern) {
172            // hi, libcudnn8 8.9.5.29-1+cuda12.2, amd64, cuDNN runtime libraries
173            let mut temp_string_array: Vec<&str> = string_line.split(" ").collect();
174            temp_string_array.retain(|&x| x != "");
175            version_list.push(temp_string_array[2]);
176        }
177    }
178    version_list.sort();
179    version_list.dedup();
180    version_list.join(",")
181}