Debian Codegraph @

rust-interface/examples/amdsmi_exporter/amdsmi_metric.rs

raw
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 use amdsmi::{AmdsmiProcessorHandle, AmdsmiResult, AmdsmiStatusT};
22 use prometheus_client::encoding::EncodeLabelSet;
23 use prometheus_client::registry::Unit;
24 use std::fmt::Debug;
25 use std::sync::Arc;
26
27 #[derive(Clone, Hash, PartialEq, Eq, EncodeLabelSet, Debug)]
28 pub struct AmdsmiLabels {
29 pub socket_id: String,
30 pub processor_id: String,
31 pub bdf: String,
32 }
33 #[derive(Clone)]
34 pub struct AmdsmiInfo {
35 pub name: &'static str,
36 pub description: &'static str,
37 pub unit: Unit,
38 }
39 #[derive(Clone)]
40 pub struct AmdsmiMetric {
41 pub info: AmdsmiInfo,
42 pub value: i64,
43 pub status: AmdsmiStatusT,
44 }
45
46 pub type AmdsmiCollector = Box<dyn Fn(AmdsmiProcessorHandle) -> Vec<AmdsmiMetric> + Send + Sync>;
47
48 pub struct AmdsmiGauge<T, F>
49 where
50 F: Fn(AmdsmiProcessorHandle) -> AmdsmiResult<T> + Send + Sync,
51 {
52 pub info: AmdsmiInfo,
53 pub func: F,
54 }
55
56 impl<T, F> AmdsmiGauge<T, F>
57 where
58 F: Fn(AmdsmiProcessorHandle) -> AmdsmiResult<T> + Send + Sync,
59 {
60 pub fn new(name: &'static str, description: &'static str, unit: Unit, func: F) -> Self {
61 Self {
62 info: AmdsmiInfo {
63 name,
64 description,
65 unit,
66 },
67 func,
68 }
69 }
70 }
71
72 pub struct AmdsmiGaugeGroup<T, F>
73 where
74 F: Fn(AmdsmiProcessorHandle) -> AmdsmiResult<T> + Send + Sync + Clone,
75 {
76 pub name: &'static str, /* name of the group */
77 pub info_list: Vec<(
78 AmdsmiInfo,
79 Arc<dyn Fn(&T) -> i64 + Send + Sync>, /* field accessor */
80 )>,
81 pub func: F,
82 }
83
84 impl<T, F> AmdsmiGaugeGroup<T, F>
85 where
86 F: Fn(AmdsmiProcessorHandle) -> AmdsmiResult<T> + Send + Sync + Clone,
87 {
88 pub fn new(
89 name: &'static str, /* name of the group */
90 info_list: Vec<(
91 &'static str, /* name */
92 &'static str, /* description */
93 Unit, /* unit */
94 Arc<dyn Fn(&T) -> i64 + Send + Sync>, /* field accessor */
95 )>,
96 func: F,
97 ) -> Self {
98 Self {
99 name,
100 info_list: info_list
101 .into_iter()
102 .map(|(name, description, unit, accessor)| {
103 (
104 AmdsmiInfo {
105 name,
106 description,
107 unit,
108 },
109 accessor,
110 )
111 })
112 .collect(),
113 func,
114 }
115 }
116 }
117
118 /// A macro to define a unit as `Unit::Other`.
119 ///
120 /// This macro takes a unit string as an argument and generates the appropriate `Unit` value.
121 ///
122 /// # Parameters
123 /// - `$unit_str`: The unit string to define the unit.
124 ///
125 /// # Example
126 ///
127 /// ```rust
128 /// # use amdsmi::*;
129 /// # use amdsmi_metric::*;
130 ///
131 /// let unit = metric_unit_other!("W");
132 /// assert_eq!(unit, Unit::Other("W".to_string()));
133 /// ```
134 macro_rules! metric_unit_other {
135 ($unit_str:expr) => {
136 Unit::Other($unit_str.to_string())
137 };
138 }