11use std:: fs:: { self , OpenOptions } ;
22use std:: io:: Write ;
3+ use std:: path:: PathBuf ;
34
45use crate :: performance:: gpu:: {
56 platform:: hardware:: Hardware ,
@@ -10,6 +11,7 @@ use crate::performance::gpu::{
1011pub struct Tdp {
1112 //pub path: String,
1213 hardware : Option < Hardware > ,
14+ base_path : Option < PathBuf > ,
1315}
1416
1517impl HardwareAccess for Tdp {
@@ -28,13 +30,49 @@ impl Tdp {
2830 None => None ,
2931 } ;
3032
31- Tdp { hardware }
33+ // Discover the package domain path
34+ let mut base_path = None ;
35+ if let Ok ( mut rapl_dir) = fs:: read_dir ( "/sys/class/powercap/intel-rapl" ) {
36+ while let Some ( Ok ( entry) ) = rapl_dir. next ( ) {
37+ let Ok ( file_type) = entry. file_type ( ) else {
38+ continue ;
39+ } ;
40+ if !file_type. is_dir ( ) {
41+ continue ;
42+ }
43+ let file_name = entry. file_name ( ) ;
44+ let Some ( file_name) = file_name. to_str ( ) else {
45+ continue ;
46+ } ;
47+ if !file_name. starts_with ( "intel-rapl:" ) {
48+ continue ;
49+ }
50+ let domain_path = entry. path ( ) ;
51+ let name_path = domain_path. join ( "name" ) ;
52+ let Ok ( name) = fs:: read_to_string ( name_path) else {
53+ continue ;
54+ } ;
55+ if !name. as_str ( ) . trim ( ) . starts_with ( "package" ) {
56+ continue ;
57+ }
58+ base_path = Some ( domain_path) ;
59+ break ;
60+ }
61+ }
62+
63+ Tdp {
64+ hardware,
65+ base_path,
66+ }
3267 }
3368}
3469
3570impl TDPDevice for Tdp {
3671 async fn tdp ( & self ) -> TDPResult < f64 > {
37- let path = "/sys/class/powercap/intel-rapl/intel-rapl:0/constraint_0_power_limit_uw" ;
72+ let Some ( base_path) = self . base_path . as_ref ( ) else {
73+ return Err ( TDPError :: FeatureUnsupported ) ;
74+ } ;
75+ let path = base_path. join ( "constraint_0_power_limit_uw" ) ;
3876 let result = fs:: read_to_string ( path) ;
3977 let content = result. map_err ( |err| TDPError :: IOError ( err. to_string ( ) ) ) ?;
4078 let content = content. trim ( ) ;
@@ -52,6 +90,9 @@ impl TDPDevice for Tdp {
5290 }
5391
5492 async fn set_tdp ( & mut self , value : f64 ) -> TDPResult < ( ) > {
93+ let Some ( base_path) = self . base_path . as_ref ( ) else {
94+ return Err ( TDPError :: FeatureUnsupported ) ;
95+ } ;
5596 if value < 1.0 {
5697 let err = "Cowardly refusing to set TDP less than 1" ;
5798 log:: warn!( "{}" , err) ;
@@ -67,7 +108,7 @@ impl TDPDevice for Tdp {
67108 }
68109
69110 // Open the sysfs file to write to
70- let path = "/sys/class/powercap/intel-rapl/intel-rapl:0/ constraint_0_power_limit_uw";
111+ let path = base_path . join ( " constraint_0_power_limit_uw") ;
71112 let file = OpenOptions :: new ( ) . write ( true ) . open ( path) ;
72113
73114 // Convert the value to a writable string
@@ -83,7 +124,10 @@ impl TDPDevice for Tdp {
83124 }
84125
85126 async fn boost ( & self ) -> TDPResult < f64 > {
86- let path = "/sys/class/powercap/intel-rapl/intel-rapl:0/constraint_1_power_limit_uw" ;
127+ let Some ( base_path) = self . base_path . as_ref ( ) else {
128+ return Err ( TDPError :: FeatureUnsupported ) ;
129+ } ;
130+ let path = base_path. join ( "constraint_1_power_limit_uw" ) ;
87131 let result = fs:: read_to_string ( path) ;
88132 let content = result. map_err ( |err| TDPError :: IOError ( err. to_string ( ) ) ) ?;
89133 let content = content. trim ( ) ;
@@ -102,6 +146,9 @@ impl TDPDevice for Tdp {
102146 }
103147
104148 async fn set_boost ( & mut self , value : f64 ) -> TDPResult < ( ) > {
149+ let Some ( base_path) = self . base_path . as_ref ( ) else {
150+ return Err ( TDPError :: FeatureUnsupported ) ;
151+ } ;
105152 log:: debug!( "Setting Boost: {}" , value) ;
106153 if value < 0.0 {
107154 let err = "Cowardly refusing to set TDP Boost less than 0" ;
@@ -118,7 +165,7 @@ impl TDPDevice for Tdp {
118165 } ;
119166
120167 // Write the short tdp
121- let path = "/sys/class/powercap/intel-rapl/intel-rapl:0/ constraint_1_power_limit_uw";
168+ let path = base_path . join ( " constraint_1_power_limit_uw") ;
122169 let file = OpenOptions :: new ( ) . write ( true ) . open ( path) ;
123170 let value = format ! ( "{}" , short_tdp) ;
124171 file. map_err ( |err| TDPError :: FailedOperation ( err. to_string ( ) ) ) ?
0 commit comments