-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e489cef
commit 3bf8eac
Showing
5 changed files
with
156 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* Copyright 2023 [email protected] | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. */ | ||
use std::{ | ||
fs, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
use anyhow::Result; | ||
|
||
use super::Freq; | ||
|
||
const CPUFREQ_DEBUG: &str = "/proc/cpudvfs/cpufreq_debug"; | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
pub struct Bounder { | ||
freq_debug: PathBuf, | ||
} | ||
|
||
impl Bounder { | ||
pub fn new() -> Option<Self> { | ||
let path = Path::new(CPUFREQ_DEBUG); | ||
|
||
if path.exists() { | ||
Some(Self { | ||
freq_debug: path.to_path_buf(), | ||
}) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
pub fn force_freq(&self, num: u8, l: Freq, r: Freq) -> Result<()> { | ||
let message = format!("{} {l} {r}", num); | ||
fs::write(&self.freq_debug, message)?; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* Copyright 2023 [email protected] | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. */ | ||
use std::{fs, os::unix::fs::PermissionsExt, path::Path}; | ||
|
||
use anyhow::Result; | ||
|
||
use super::{Freq, Policy}; | ||
|
||
impl Policy { | ||
pub fn lock_max_freq(&self, f: Freq) -> Result<()> { | ||
let path = self.path.join("scaling_max_freq"); | ||
lock_write(path, f.to_string()) | ||
} | ||
|
||
pub fn lock_min_freq(&self, f: Freq) -> Result<()> { | ||
let path = self.path.join("scaling_min_freq"); | ||
lock_write(path, f.to_string()) | ||
} | ||
|
||
pub fn lock_governor<S: AsRef<str>>(&self, g: S) -> Result<()> { | ||
let path = self.path.join("scaling_governor"); | ||
let governor = g.as_ref(); | ||
lock_write(path, governor) | ||
} | ||
|
||
pub fn unlock_max_freq(&self, f: Freq) -> Result<()> { | ||
let path = self.path.join("scaling_max_freq"); | ||
unlock_write(path, f.to_string()) | ||
} | ||
|
||
pub fn unlock_min_freq(&self, f: Freq) -> Result<()> { | ||
let path = self.path.join("scaling_min_freq"); | ||
unlock_write(path, f.to_string()) | ||
} | ||
|
||
pub fn unlock_governor<S: AsRef<str>>(&self, g: S) -> Result<()> { | ||
let path = self.path.join("scaling_governor"); | ||
let governor = g.as_ref(); | ||
unlock_write(path, governor) | ||
} | ||
} | ||
|
||
fn lock_write<S: AsRef<str>, P: AsRef<Path>>(p: P, s: S) -> Result<()> { | ||
let s = s.as_ref(); | ||
let p = p.as_ref(); | ||
|
||
let _ = fs::set_permissions(p, PermissionsExt::from_mode(0o644)); | ||
fs::write(p, s)?; | ||
let _ = fs::set_permissions(p, PermissionsExt::from_mode(0o444)); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn unlock_write<S: AsRef<str>, P: AsRef<Path>>(p: P, s: S) -> Result<()> { | ||
let s = s.as_ref(); | ||
let p = p.as_ref(); | ||
|
||
let _ = fs::set_permissions(p, PermissionsExt::from_mode(0o644)); | ||
fs::write(p, s)?; | ||
|
||
Ok(()) | ||
} |