interplane.Get Omega
Read in your language

spark-rsi-mv

Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger

Brought by drakestapleton

branch main Branches Tags History

spark-rsi-mv / src / evaluator / layers / resource_efficiency.rs

204 lines · 6688 bytes Raw Blame History
1use super::LayerResult;
2use crate::evaluator::metrics::{RusageMetrics, StatmMetrics};
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
6pub struct ResourceEfficiencyEvaluation {
7 pub parent_rss_kb: i64,
8 pub candidate_rss_kb: i64,
9 pub rss_growth_pct: f64,
10 pub rss_growth_within_budget: bool,
11 pub host_memory_within_budget: bool,
12 pub context_switches_delta: i64,
13 pub passed: bool,
14 pub violations: Vec<String>,
15}
16
17impl ResourceEfficiencyEvaluation {
18 pub fn to_layer_result(&self) -> LayerResult {
19 let score = if self.passed {
20 1.0
21 } else if self.host_memory_within_budget {
22 0.5
23 } else {
24 0.0
25 };
26
27 let summary = format!(
28 "ResourceEfficiency: rss_growth={:.2}%, limit_mb_ok={}, ctxt_delta={}",
29 self.rss_growth_pct, self.host_memory_within_budget, self.context_switches_delta
30 );
31
32 LayerResult {
33 layer_name: "ResourceEfficiency".to_string(),
34 is_hard_invariant: false,
35 passed: self.passed,
36 score,
37 summary,
38 violations: self.violations.clone(),
39 }
40 }
41}
42
43pub struct ResourceEfficiencyLayer;
44
45impl ResourceEfficiencyLayer {
46 pub const MAX_RSS_GROWTH_PCT: f64 = 2.0;
47 pub const DEFAULT_UNIFIED_MEMORY_LIMIT_MB: u64 = 49_152; // 48 GB Grace Blackwell ceiling
48
49 pub fn evaluate(
50 parent_rusage: &RusageMetrics,
51 candidate_rusage: &RusageMetrics,
52 candidate_statm: Option<&StatmMetrics>,
53 memory_limit_mb: Option<u64>,
54 ) -> ResourceEfficiencyEvaluation {
55 let mut violations = Vec::new();
56
57 let parent_rss_kb = parent_rusage.max_rss_kb;
58 let candidate_rss_kb = candidate_rusage.max_rss_kb;
59
60 let rss_growth_pct = if parent_rss_kb > 0 {
61 ((candidate_rss_kb - parent_rss_kb) as f64 / (parent_rss_kb as f64)) * 100.0
62 } else {
63 0.0
64 };
65
66 let rss_growth_within_budget = rss_growth_pct <= Self::MAX_RSS_GROWTH_PCT;
67 if !rss_growth_within_budget {
68 violations.push(format!(
69 "Candidate peak RSS growth exceeded 2.0% threshold: {:.2}% (parent={} KB, cand={} KB)",
70 rss_growth_pct, parent_rss_kb, candidate_rss_kb
71 ));
72 }
73
74 let limit_mb = memory_limit_mb.unwrap_or(Self::DEFAULT_UNIFIED_MEMORY_LIMIT_MB);
75 let mut host_memory_within_budget = true;
76
77 if let Some(statm) = candidate_statm {
78 let resident_mb = statm.resident_kb / 1024;
79 if resident_mb > limit_mb {
80 violations.push(format!(
81 "Candidate resident memory exceeded unified memory limit: {} MB > {} MB",
82 resident_mb, limit_mb
83 ));
84 host_memory_within_budget = false;
85 }
86 }
87
88 let ctxt_parent =
89 parent_rusage.voluntary_context_switches + parent_rusage.involuntary_context_switches;
90 let ctxt_cand = candidate_rusage.voluntary_context_switches
91 + candidate_rusage.involuntary_context_switches;
92 let context_switches_delta = ctxt_cand - ctxt_parent;
93
94 let passed = rss_growth_within_budget && host_memory_within_budget;
95
96 ResourceEfficiencyEvaluation {
97 parent_rss_kb,
98 candidate_rss_kb,
99 rss_growth_pct,
100 rss_growth_within_budget,
101 host_memory_within_budget,
102 context_switches_delta,
103 passed,
104 violations,
105 }
106 }
107}
108
109#[cfg(test)]
110mod tests {
111 use super::*;
112
113 #[test]
114 fn test_resource_efficiency_clean() {
115 let parent = RusageMetrics {
116 user_time_us: 1000,
117 system_time_us: 500,
118 max_rss_kb: 50_000,
119 voluntary_context_switches: 10,
120 involuntary_context_switches: 5,
121 };
122 let candidate = RusageMetrics {
123 user_time_us: 950,
124 system_time_us: 480,
125 max_rss_kb: 50_500, // 1% growth <= 2%
126 voluntary_context_switches: 8,
127 involuntary_context_switches: 4,
128 };
129
130 let statm = StatmMetrics {
131 total_pages: 20_000,
132 resident_pages: 12_500, // 50 MB
133 shared_pages: 2000,
134 text_pages: 500,
135 data_pages: 9000,
136 resident_kb: 50_000,
137 };
138
139 let eval = ResourceEfficiencyLayer::evaluate(&parent, &candidate, Some(&statm), None);
140 assert!(eval.passed);
141 assert!(eval.rss_growth_within_budget);
142 assert!(eval.host_memory_within_budget);
143
144 let lr = eval.to_layer_result();
145 assert!(lr.passed);
146 assert!((lr.score - 1.0).abs() < 1e-6);
147 }
148
149 #[test]
150 fn test_resource_efficiency_rss_growth_violation() {
151 let parent = RusageMetrics {
152 user_time_us: 1000,
153 system_time_us: 500,
154 max_rss_kb: 50_000,
155 voluntary_context_switches: 10,
156 involuntary_context_switches: 5,
157 };
158 let candidate = RusageMetrics {
159 user_time_us: 950,
160 system_time_us: 480,
161 max_rss_kb: 60_000, // 20% growth > 2%
162 voluntary_context_switches: 8,
163 involuntary_context_switches: 4,
164 };
165
166 let eval = ResourceEfficiencyLayer::evaluate(&parent, &candidate, None, None);
167 assert!(!eval.passed);
168 assert!(!eval.rss_growth_within_budget);
169 assert!(eval.violations[0].contains("exceeded 2.0% threshold"));
170 }
171
172 #[test]
173 fn test_resource_efficiency_host_memory_limit_exceeded() {
174 let parent = RusageMetrics {
175 user_time_us: 1000,
176 system_time_us: 500,
177 max_rss_kb: 50_000,
178 voluntary_context_switches: 10,
179 involuntary_context_switches: 5,
180 };
181 let candidate = RusageMetrics {
182 user_time_us: 950,
183 system_time_us: 480,
184 max_rss_kb: 50_000,
185 voluntary_context_switches: 8,
186 involuntary_context_switches: 4,
187 };
188
189 let statm = StatmMetrics {
190 total_pages: 20_000_000,
191 resident_pages: 13_000_000, // 52 GB > 48 GB
192 shared_pages: 2000,
193 text_pages: 500,
194 data_pages: 9000,
195 resident_kb: 52_000_000,
196 };
197
198 let eval =
199 ResourceEfficiencyLayer::evaluate(&parent, &candidate, Some(&statm), Some(49_152));
200 assert!(!eval.passed);
201 assert!(!eval.host_memory_within_budget);
202 assert!(eval.violations[0].contains("exceeded unified memory limit"));
203 }
204}