Skip to content

Commit

Permalink
feat: 提供默认限制策略
Browse files Browse the repository at this point in the history
  • Loading branch information
shadow3aaa committed Jan 8, 2025
1 parent 406d25d commit fb64a8e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/cpu_common/extra_policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@
// You should have received a copy of the GNU General Public License along
// with fas-rs. If not, see <https://www.gnu.org/licenses/>.

#[derive(Debug)]
#[derive(Debug, PartialEq, Eq)]
pub enum ExtraPolicy {
AbsRangeBound(AbsRangeBound),
RelRangeBound(RelRangeBound),
None,
}

#[derive(Debug)]
#[derive(Debug, PartialEq, Eq)]
pub struct AbsRangeBound {
pub min: Option<isize>,
pub max: Option<isize>,
}

#[derive(Debug)]
#[derive(Debug, PartialEq, Eq)]
pub struct RelRangeBound {
pub rel_to: i32,
pub min: Option<isize>,
Expand Down
88 changes: 70 additions & 18 deletions src/cpu_common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ use crate::{
Extension,
};
use cpu_info::Info;
use extra_policy::ExtraPolicy;
use extra_policy::{ExtraPolicy, RelRangeBound};

pub static EXTRA_POLICY_MAP: OnceLock<HashMap<i32, Mutex<ExtraPolicy>>> = OnceLock::new();
pub static IGNORE_MAP: OnceLock<HashMap<i32, AtomicBool>> = OnceLock::new();
static EXTRA_POLICY_MAP_DEFAULT: OnceLock<HashMap<i32, ExtraPolicy>> = OnceLock::new();

#[derive(Debug)]
pub struct Controller {
Expand All @@ -63,6 +64,26 @@ impl Controller {
.map(|cpu| (cpu.policy, Mutex::new(ExtraPolicy::None)))
.collect()
});
EXTRA_POLICY_MAP_DEFAULT.get_or_init(|| {
let prime_policy = cpu_infos.last().map(|info| info.policy).unwrap();
cpu_infos
.iter()
.map(|info| {
if info.policy == prime_policy {
(info.policy, ExtraPolicy::None)
} else {
(
info.policy,
ExtraPolicy::RelRangeBound(RelRangeBound {
rel_to: prime_policy,
min: Some(-100_000),
max: Some(100_000),
}),
)
}
})
.collect()
});
IGNORE_MAP.get_or_init(|| {
cpu_infos
.iter()
Expand Down Expand Up @@ -268,27 +289,58 @@ impl Controller {
) -> HashMap<i32, isize> {
for policy in sorted_policies {
if let Some(freq) = fas_freqs.get(policy).copied() {
let adjusted_freq = match *EXTRA_POLICY_MAP
let extra_policy_map = EXTRA_POLICY_MAP
.get()
.context("EXTRA_POLICY_MAP not initialized")
.unwrap()
.get(policy)
.context("CPU Policy not found")
.unwrap()
.lock()
.unwrap();
let adjusted_freq = if extra_policy_map
.values()
.all(|policy| *policy.lock() == ExtraPolicy::None)
{
ExtraPolicy::RelRangeBound(ref rel_bound) => {
let rel_to_freq = fas_freqs.get(&rel_bound.rel_to).copied().unwrap_or(0);

#[cfg(debug_assertions)]
debug!("policy{} rel_to {}", policy, rel_to_freq);

freq.clamp(
rel_to_freq + rel_bound.min.unwrap_or(isize::MIN),
rel_to_freq + rel_bound.max.unwrap_or(isize::MAX),
)
let extra_policy_map = EXTRA_POLICY_MAP_DEFAULT
.get()
.context("EXTRA_POLICY_MAP_DEFAULT not initialized")
.unwrap();
match *extra_policy_map
.get(policy)
.context("CPU Policy not found")
.unwrap()
{
ExtraPolicy::RelRangeBound(ref rel_bound) => {
let rel_to_freq =
fas_freqs.get(&rel_bound.rel_to).copied().unwrap_or(0);

#[cfg(debug_assertions)]
debug!("policy{} rel_to {}", policy, rel_to_freq);

freq.clamp(
rel_to_freq + rel_bound.min.unwrap_or(isize::MIN),
rel_to_freq + rel_bound.max.unwrap_or(isize::MAX),
)
}
_ => freq,
}
} else {
match *extra_policy_map
.get(policy)
.context("CPU Policy not found")
.unwrap()
.lock()
{
ExtraPolicy::RelRangeBound(ref rel_bound) => {
let rel_to_freq =
fas_freqs.get(&rel_bound.rel_to).copied().unwrap_or(0);

#[cfg(debug_assertions)]
debug!("policy{} rel_to {}", policy, rel_to_freq);

freq.clamp(
rel_to_freq + rel_bound.min.unwrap_or(isize::MIN),
rel_to_freq + rel_bound.max.unwrap_or(isize::MAX),
)
}
_ => freq,
}
_ => freq,
};

#[cfg(debug_assertions)]
Expand Down

0 comments on commit fb64a8e

Please sign in to comment.