| 1 |
// Copyright (C) 2024 Advanced Micro Devices. All rights reserved. |
| 2 |
// |
| 3 |
// Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 4 |
// this software and associated documentation files (the "Software"), to deal in |
| 5 |
// the Software without restriction, including without limitation the rights to |
| 6 |
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 7 |
// the Software, and to permit persons to whom the Software is furnished to do so, |
| 8 |
// subject to the following conditions: |
| 9 |
// |
| 10 |
// The above copyright notice and this permission notice shall be included in all |
| 11 |
// copies or substantial portions of the Software. |
| 12 |
// |
| 13 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 15 |
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 16 |
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 17 |
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 18 |
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 19 |
// |
| 20 |
|
| 21 |
#[macro_use] |
| 22 |
mod amdsmi_collectors; |
| 23 |
#[macro_use] |
| 24 |
mod amdsmi_metric; |
| 25 |
mod collectors; |
| 26 |
mod http_server; |
| 27 |
|
| 28 |
use crate::amdsmi_collectors::*; |
| 29 |
use clap::{App, Arg, SubCommand}; |
| 30 |
use http_server::run_http_server; |
| 31 |
use std::net::SocketAddr; |
| 32 |
use std::sync::Arc; |
| 33 |
use tokio::sync::Mutex; |
| 34 |
|
| 35 |
const APP_VERSION_STR: &str = "1.0"; |
| 36 |
|
| 37 |
#[tokio::main] |
| 38 |
async fn main() { |
| 39 |
// Parse command line arguments |
| 40 |
let cli_matches = App::new("AMDSMI Exporter") |
| 41 |
.version(APP_VERSION_STR) |
| 42 |
.author("AMD <[email protected]>") |
| 43 |
.about("Exports AMD SMI metrics") |
| 44 |
.subcommand( |
| 45 |
SubCommand::with_name("list") |
| 46 |
.about("Lists the supported GPUs and metrics' information") |
| 47 |
) |
| 48 |
.subcommand( |
| 49 |
SubCommand::with_name("metric") |
| 50 |
.about("Exports AMD SMI metrics and runs the HTTP server") |
| 51 |
.arg( |
| 52 |
Arg::new("port") |
| 53 |
.short('p') |
| 54 |
.long("port") |
| 55 |
.value_name("PORT") |
| 56 |
.help("Sets the port to listen on") |
| 57 |
.takes_value(true), |
| 58 |
) |
| 59 |
.arg( |
| 60 |
Arg::new("gpu") |
| 61 |
.short('g') |
| 62 |
.long("gpu") |
| 63 |
.value_name("GPU") |
| 64 |
.help("Sets the GPU BDF or the zero-based index of the socket ID and processor ID for export. Example: --gpu 0000:03:00.0 or --gpu 0.0. You can specify multiple GPUs separated by commas") |
| 65 |
.takes_value(true) |
| 66 |
) |
| 67 |
.arg( |
| 68 |
Arg::new("whitelist") |
| 69 |
.short('w') |
| 70 |
.long("whitelist") |
| 71 |
.value_name("METRIC") |
| 72 |
.help("Comma-separated list of metric field names to include") |
| 73 |
.takes_value(true), |
| 74 |
) |
| 75 |
.arg( |
| 76 |
Arg::new("blacklist") |
| 77 |
.short('b') |
| 78 |
.long("blacklist") |
| 79 |
.value_name("METRIC") |
| 80 |
.help("Comma-separated list of metric field names to exclude") |
| 81 |
.takes_value(true), |
| 82 |
) |
| 83 |
) |
| 84 |
.get_matches(); |
| 85 |
|
| 86 |
let collectors = Arc::new(Mutex::new(AmdsmiCollectors::default())); |
| 87 |
|
| 88 |
match cli_matches.subcommand() { |
| 89 |
Some(("list", _)) => { |
| 90 |
collectors.lock().await.print_supported(); |
| 91 |
} |
| 92 |
Some(("metric", arg_options)) => { |
| 93 |
export_metrics(arg_options, &collectors).await; |
| 94 |
} |
| 95 |
_ => { |
| 96 |
eprintln!("Invalid command. Please use 'list' to list supported GPUs and metrics, or 'metric' to export metrics and run the HTTP server. Use --help for more information."); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
async fn export_metrics(arg_options: &clap::ArgMatches, collectors: &Arc<Mutex<AmdsmiCollectors>>) { |
| 102 |
let port: u16 = arg_options |
| 103 |
.get_one::<String>("port") |
| 104 |
.unwrap_or(&"9898".to_string()) |
| 105 |
.parse::<u16>() |
| 106 |
.unwrap_or_else(|_| { |
| 107 |
eprintln!("Invalid port number provided. Using default port 9898."); |
| 108 |
9898 |
| 109 |
}); |
| 110 |
|
| 111 |
let gpu_filters: Option<Vec<String>> = arg_options |
| 112 |
.get_one::<String>("gpu") |
| 113 |
.map(|s| s.split(',').map(|s| s.trim().to_string()).collect()); |
| 114 |
|
| 115 |
let whitelist: Option<Vec<String>> = arg_options |
| 116 |
.get_one::<String>("whitelist") |
| 117 |
.map(|s| s.split(',').map(|s| s.trim().to_string()).collect()); |
| 118 |
|
| 119 |
let blacklist: Option<Vec<String>> = arg_options |
| 120 |
.get_one::<String>("blacklist") |
| 121 |
.map(|s| s.split(',').map(|s| s.trim().to_string()).collect()); |
| 122 |
|
| 123 |
collectors |
| 124 |
.lock() |
| 125 |
.await |
| 126 |
.set_gpu_filter(gpu_filters) |
| 127 |
.set_field_filter(whitelist, blacklist); |
| 128 |
|
| 129 |
let addr = SocketAddr::from(([0, 0, 0, 0], port)); |
| 130 |
|
| 131 |
// Run the HTTP server |
| 132 |
run_http_server(&collectors, addr).await; |
| 133 |
} |