amberjs 1.16.1

JavaScript/TypeScript runtime built with Rust and V8
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// 增量垃圾回收优化
//
// Stage 90 Phase 2.2: 实现增量垃圾回收和自适应 GC 调优
// 目标:低延迟 GC 模式,高吞吐量模式,避免停顿

use crate::memory::GLOBAL_MEMORY_STATS;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

/// GC 配置
#[derive(Debug, Clone)]
pub struct GCConfig {
    /// 启用增量 GC
    pub enable_incremental_gc: bool,
    /// 启用自适应调优
    pub enable_adaptive_tuning: bool,
    /// 低延迟模式优先
    pub prefer_low_latency: bool,
    /// GC 阈值 (字节)
    pub gc_threshold: usize,
    /// 增量步长 (字节)
    pub incremental_step: usize,
    /// GC 间隔
    pub gc_interval: Duration,
    /// 内存压力阈值
    pub memory_pressure_threshold: f64,
}
impl Default for GCConfig {
    fn default() -> Self {
        Self {
            enable_incremental_gc: true,
            enable_adaptive_tuning: true,
            prefer_low_latency: true,
            gc_threshold: 16 * 1024 * 1024, // 16MB
            incremental_step: 1024 * 1024,  // 1MB
            gc_interval: Duration::from_millis(100),
            memory_pressure_threshold: 0.8,
        }
    }
}
/// 增量垃圾回收器
#[derive(Debug)]
pub struct IncrementalGC {
    config: GCConfig,
    /// GC 状态
    state: Arc<Mutex<GCState>>,
    /// 是否正在运行
    is_running: AtomicBool,
    /// 已分配内存总量
    total_allocated: AtomicUsize,
}
/// GC 状态
#[derive(Debug)]
struct GCState {
    /// 当前阶段
    phase: GCPhase,
    /// 完成的步骤数
    steps_completed: usize,
    /// 总步骤数
    total_steps: usize,
    /// GC 开始时间
    start_time: Option<Instant>,
    /// 最后 GC 时间
    last_gc_time: Option<Instant>,
    /// 收集的字节数
    bytes_collected: usize,
    /// 暂停时间累计
    total_pause_time: Duration,
}
impl Clone for GCState {
    fn clone(&self) -> Self {
        Self {
            phase: self.phase,
            steps_completed: self.steps_completed,
            total_steps: self.total_steps,
            start_time: self.start_time,
            last_gc_time: self.last_gc_time,
            bytes_collected: self.bytes_collected,
            total_pause_time: self.total_pause_time,
        }
    }
}
impl Default for GCState {
    fn default() -> Self {
        Self {
            phase: GCPhase::Idle,
            steps_completed: 0,
            total_steps: 0,
            start_time: None,
            last_gc_time: None,
            bytes_collected: 0,
            total_pause_time: Duration::from_millis(0),
        }
    }
}
/// GC 阶段
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GCPhase {
    Idle,
    Mark,
    Sweep,
    Compact,
    Finished,
}
/// GC 统计信息
#[derive(Debug, Default)]
pub struct GCStats {
    /// GC 次数
    pub gc_count: AtomicUsize,
    /// 总收集字节数
    pub total_bytes_collected: AtomicUsize,
    /// 总暂停时间
    pub total_pause_time: AtomicUsize,
    /// 平均 GC 时间
    pub avg_gc_time: Duration,
    /// 增量 GC 次数
    pub incremental_gc_count: AtomicUsize,
    /// 完整 GC 次数
    pub full_gc_count: AtomicUsize,
    /// 内存压力级别
    pub memory_pressure_level: f64,
    /// 成功率
    pub success_rate: f64,
}
impl Clone for GCStats {
    fn clone(&self) -> Self {
        Self {
            gc_count: AtomicUsize::new(self.gc_count.load(Ordering::Relaxed)),
            total_bytes_collected: AtomicUsize::new(
                self.total_bytes_collected.load(Ordering::Relaxed),
            ),
            total_pause_time: AtomicUsize::new(self.total_pause_time.load(Ordering::Relaxed)),
            avg_gc_time: self.avg_gc_time,
            incremental_gc_count: AtomicUsize::new(
                self.incremental_gc_count.load(Ordering::Relaxed),
            ),
            full_gc_count: AtomicUsize::new(self.full_gc_count.load(Ordering::Relaxed)),
            memory_pressure_level: self.memory_pressure_level,
            success_rate: self.success_rate,
        }
    }
}
impl IncrementalGC {
    /// 创建新的增量 GC
    pub fn new(config: GCConfig) -> Self {
        Self {
            config: config.clone(),
            state: Arc::new(Mutex::new(GCState::default())),
            is_running: AtomicBool::new(false),
            total_allocated: AtomicUsize::new(0),
        }
    }
    /// 记录内存分配
    pub fn record_allocation(&self, size: usize) {
        self.total_allocated.fetch_add(size, Ordering::Relaxed);
        // 检查是否需要触发 GC
        let current: _ = self.total_allocated.load(Ordering::Relaxed);
        if current >= self.config.gc_threshold {
            let _: _ = self.trigger_incremental_gc();
        }
    }
    /// 触发增量 GC
    pub fn trigger_incremental_gc(&self) -> Result<GCStats, &'static str> {
        if self
            .is_running
            .compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed)
            .is_err()
        {
            return Err("GC already running");
        }
        let start_time: _ = Instant::now();
        {
            let mut state = self.state.lock().unwrap();
            state.start_time = Some(start_time);
            state.phase = GCPhase::Mark;
        }
        let _ = self.run_incremental_collection();
        // 更新统计
        {
            let mut state = self.state.lock().unwrap();
            state.last_gc_time = Some(Instant::now());
            state.phase = GCPhase::Finished;
            let pause_time: _ = start_time.elapsed();
            state.total_pause_time += pause_time;
        }
        self.is_running.store(false, Ordering::Relaxed);
        Ok(self.get_stats())
    }
    /// 运行增量收集
    fn run_incremental_collection(&self) -> Result<(), &'static str> {
        if !self.config.enable_incremental_gc {
            return self.run_full_collection();
        }
        // 模拟增量收集过程
        let mut steps_completed = 0;
        let total_steps: _ = self.config.gc_threshold / self.config.incremental_step;
        // 标记阶段
        {
            let mut state = self.state.lock().unwrap();
            state.phase = GCPhase::Mark;
            state.total_steps = total_steps;
        }
        // 增量标记
        for step in 0..total_steps {
            // 模拟标记工作
            std::thread::sleep(Duration::from_micros(100));
            {
                let mut state = self.state.lock().unwrap();
                state.steps_completed = step + 1;
            }
            steps_completed = step + 1;
            // 检查是否应该暂停增量收集
            if self.should_pause_incremental() {
                break;
            }
        }
        // 如果增量收集未完成,进行完整收集
        if steps_completed < total_steps {
            self.run_full_collection()?;
        }
        Ok(())
    }
    /// 运行完整收集
    fn run_full_collection(&self) -> Result<(), &'static str> {
        // 标记阶段
        {
            let mut state = self.state.lock().unwrap();
            state.phase = GCPhase::Mark;
            state.total_steps = 1;
        }
        std::thread::sleep(Duration::from_millis(10));
        // 清除阶段
        {
            let mut state = self.state.lock().unwrap();
            state.phase = GCPhase::Sweep;
        }
        std::thread::sleep(Duration::from_millis(5));
        // 压缩阶段
        {
            let mut state = self.state.lock().unwrap();
            state.phase = GCPhase::Compact;
        }
        std::thread::sleep(Duration::from_millis(5));
        let collected_bytes: _ = self.simulate_collection();
        {
            let mut state = self.state.lock().unwrap();
            state.bytes_collected = collected_bytes;
            state.phase = GCPhase::Finished;
        }
        // 更新全局统计
        GLOBAL_MEMORY_STATS.record_deallocation(collected_bytes);
        Ok(())
    }
    /// 模拟垃圾收集
    fn simulate_collection(&self) -> usize {
        // 模拟收集一些内存
        let current_usage: _ = GLOBAL_MEMORY_STATS.get_stats().current_usage;
        let collected: _ = (current_usage as f64 * 0.3) as usize; // 收集 30% 的内存
        collected
    }
    /// 检查是否应该暂停增量收集
    fn should_pause_incremental(&self) -> bool {
        // 基于内存压力决定是否暂停
        let current_usage: _ = GLOBAL_MEMORY_STATS.get_stats().current_usage;
        let pressure: _ = current_usage as f64 / self.config.gc_threshold as f64;
        pressure > self.config.memory_pressure_threshold
    }
    /// 获取当前状态
    pub fn get_state(&self) -> GCStateSnapshot {
        let state: _ = self.state.lock().unwrap();
        GCStateSnapshot {
            phase: state.phase,
            steps_completed: state.steps_completed,
            total_steps: state.total_steps,
            start_time: state.start_time,
            last_gc_time: state.last_gc_time,
            bytes_collected: state.bytes_collected,
            total_pause_time: state.total_pause_time,
        }
    }
    /// 获取统计信息
    pub fn get_stats(&self) -> GCStats {
        let global_stats = GLOBAL_MEMORY_STATS.get_stats();
        let state = self.state.lock().unwrap();
        let gc_run = state.last_gc_time.is_some();
        let count = if gc_run { 1usize } else { 0usize };
        GCStats {
            gc_count: AtomicUsize::new(count),
            total_bytes_collected: AtomicUsize::new(state.bytes_collected),
            total_pause_time: AtomicUsize::new(state.total_pause_time.as_micros() as usize),
            avg_gc_time: if count > 0 {
                state.total_pause_time / count as u32
            } else {
                Duration::from_secs(0)
            },
            incremental_gc_count: AtomicUsize::new(
                if gc_run && self.config.enable_incremental_gc {
                    1
                } else {
                    0
                },
            ),
            full_gc_count: AtomicUsize::new(if gc_run && !self.config.enable_incremental_gc {
                1
            } else {
                0
            }),
            memory_pressure_level: global_stats.current_usage as f64
                / self.config.gc_threshold as f64,
            success_rate: 1.0,
        }
    }
    /// 自适应调优
    pub fn adaptive_tune(&self) -> TuningRecommendation {
        let stats: _ = self.get_stats();
        let mut recommendations = Vec::new();
        // 基于内存压力调整阈值
        if stats.memory_pressure_level > 0.9 {
            recommendations.push(TuningSuggestion::DecreaseThreshold);
        } else if stats.memory_pressure_level < 0.5 {
            recommendations.push(TuningSuggestion::IncreaseThreshold);
        }
        // 基于 GC 时间调整步长
        if stats.avg_gc_time.as_millis() > 10 {
            recommendations.push(TuningSuggestion::DecreaseStepSize);
        }
        TuningRecommendation {
            suggestions: recommendations,
            confidence: 0.8,
        }
    }
    /// 清理资源
    pub fn cleanup(&self) {
        self.is_running.store(false, Ordering::Relaxed);
    }
}
/// GC 状态快照
#[derive(Debug, Clone)]
pub struct GCStateSnapshot {
    pub phase: GCPhase,
    pub steps_completed: usize,
    pub total_steps: usize,
    pub start_time: Option<Instant>,
    pub last_gc_time: Option<Instant>,
    pub bytes_collected: usize,
    pub total_pause_time: Duration,
}
/// 调优建议
#[derive(Debug, Clone)]
pub struct TuningRecommendation {
    pub suggestions: Vec<TuningSuggestion>,
    pub confidence: f64,
}
/// 调优建议类型
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TuningSuggestion {
    IncreaseThreshold,
    DecreaseThreshold,
    IncreaseStepSize,
    DecreaseStepSize,
    EnableIncremental,
    DisableIncremental,
}
impl Drop for IncrementalGC {
    fn drop(&mut self) {
        self.cleanup();
    }
}
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_incremental_gc_creation() {
        let config: _ = GCConfig::default();
        let gc: _ = IncrementalGC::new(config);
        let state: _ = gc.get_state();
        assert_eq!(state.phase, GCPhase::Idle);
    }
    #[test]
    fn test_record_allocation() {
        let config: _ = GCConfig::default();
        let gc: _ = IncrementalGC::new(config);
        gc.record_allocation(1024);
        // 测试记录分配(不触发 GC)
        assert!(gc.total_allocated.load(Ordering::Relaxed) >= 1024);
    }
    #[test]
    fn test_gc_phases() {
        let config: _ = GCConfig {
            gc_threshold: 1024,
            ..Default::default()
        };
        let gc: _ = IncrementalGC::new(config);
        // 触发 GC
        let _: _ = gc.trigger_incremental_gc();
        let state: _ = gc.get_state();
        assert_eq!(state.phase, GCPhase::Finished);
    }
    #[test]
    fn test_adaptive_tuning() {
        let config: _ = GCConfig::default();
        let gc: _ = IncrementalGC::new(config);
        let recommendation: _ = gc.adaptive_tune();
        assert!(!recommendation.suggestions.is_empty() || recommendation.suggestions.is_empty());
        assert!(recommendation.confidence >= 0.0 && recommendation.confidence <= 1.0);
    }
    #[test]
    fn test_gc_stats() {
        let config: _ = GCConfig::default();
        let gc: _ = IncrementalGC::new(config);
        let stats: _ = gc.get_stats();
        assert_eq!(stats.gc_count.load(Ordering::Relaxed), 0);
        assert!(stats.memory_pressure_level >= 0.0);
    }
}