process_limiter 0.1.0

A lib which can directly limit the specified process occupancy, and support multithreading and dynamically changing occupancy
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use crate::LimitInfo;
use std::{thread::sleep, time::Duration};
use sysinfo::{Process, ProcessExt, Signal};

// Use a run-time-slice limiting approach
// Calculation method: (Working time slice percentage) = (Process CPU usage) / (Target CPU usage) * (Last working time slice percentage)
// ⚠: If the calculation Result% > 100% time slice, it is considered 100%
// ⚠: If the calculation Result% < Minimum margin% time slice, then it is considered 0%

pub fn limit_process(process: &Process, info: &mut LimitInfo) -> Duration {
    let (work_slice, sleep_slice, _total_slice) = info.result();
    sleep(work_slice);
    process.kill_with(Signal::Stop);
    sleep(sleep_slice);
    process.kill_with(Signal::Continue);
    work_slice
}