Skip to content
API Documentation
BastionGuard™
CpuThrottle Class Reference

Public Member Functions

 CpuThrottle (double target_pct=2.0, int check_interval_ms=1000)
void enforce ()

Static Private Member Functions

static long hertz_total ()

Private Attributes

CpuSnapshot prev
unsigned long long prev_total = 0
double target
int interval_ms

Detailed Description

Definition at line 200 of file BastionGuard-ransomware-scanner.cpp.

Constructor & Destructor Documentation

◆ CpuThrottle()

CpuThrottle::CpuThrottle ( double target_pct = 2.0,
int check_interval_ms = 1000 )
inline

Definition at line 202 of file BastionGuard-ransomware-scanner.cpp.

203 : target(target_pct), interval_ms(check_interval_ms)
204 {
205 prev.ts_ms = now_ms();
207 prev.proc_time = 0;
209 }
static bool read_proc_self(unsigned long long &proc_out)
static bool read_proc_stat(unsigned long long &total_out)
static long long now_ms()

References interval_ms, now_ms(), prev, prev_total, CpuSnapshot::proc_time, read_proc_self(), read_proc_stat(), target, and CpuSnapshot::ts_ms.

Member Function Documentation

◆ enforce()

void CpuThrottle::enforce ( )
inline

Definition at line 211 of file BastionGuard-ransomware-scanner.cpp.

211 {
212 long long now = now_ms();
213 if (now - prev.ts_ms < interval_ms) return;
214 unsigned long long cur_total = 0, cur_proc = 0;
215 if (!read_proc_stat(cur_total) || !read_proc_self(cur_proc)) {
216 prev.ts_ms = now;
217 return;
218 }
219
220 unsigned long long total_diff = (cur_total > prev_total) ? cur_total - prev_total : 0;
221 unsigned long long proc_diff = (cur_proc > prev.proc_time) ? cur_proc - prev.proc_time : 0;
222
223 double seconds = (double)total_diff / (double)hertz_total();
224 double proc_secs = (double)proc_diff / (double)sysconf(_SC_CLK_TCK);
225
226 double pct = 0.0;
227 if (seconds > 0.0) pct = 100.0 * (proc_secs / seconds);
228
229 if (pct > target) {
230 double excess = pct - target;
231 int base_ms = 50;
232 int sleep_ms = (int) std::min(2000.0, base_ms * (excess / std::max(0.1, target)));
233 std::this_thread::sleep_for(std::chrono::milliseconds(sleep_ms));
234 }
235
236 prev_total = cur_total;
237 prev.proc_time = cur_proc;
238 prev.ts_ms = now;
239 }

References hertz_total(), interval_ms, now_ms(), prev, prev_total, CpuSnapshot::proc_time, read_proc_self(), read_proc_stat(), target, and CpuSnapshot::ts_ms.

Referenced by main().

◆ hertz_total()

long CpuThrottle::hertz_total ( )
inlinestaticprivate

Definition at line 247 of file BastionGuard-ransomware-scanner.cpp.

247 {
248 static long hz = sysconf(_SC_CLK_TCK);
249 return hz > 0 ? hz : 100;
250 }

Referenced by enforce().

Member Data Documentation

◆ interval_ms

int CpuThrottle::interval_ms
private

Definition at line 245 of file BastionGuard-ransomware-scanner.cpp.

Referenced by CpuThrottle(), and enforce().

◆ prev

CpuSnapshot CpuThrottle::prev
private

Definition at line 242 of file BastionGuard-ransomware-scanner.cpp.

Referenced by CpuThrottle(), and enforce().

◆ prev_total

unsigned long long CpuThrottle::prev_total = 0
private

Definition at line 243 of file BastionGuard-ransomware-scanner.cpp.

Referenced by CpuThrottle(), and enforce().

◆ target

double CpuThrottle::target
private

Definition at line 244 of file BastionGuard-ransomware-scanner.cpp.

Referenced by CpuThrottle(), and enforce().


The documentation for this class was generated from the following file: