lambdust 0.1.1

A Scheme dialect with gradual typing and effect systems
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//! Parallel generational garbage collector coordinator
//!
//! This module implements the main coordinator for the parallel generational 
//! garbage collector, managing collection phases, thread synchronization, and
//! generation-specific collection algorithms.

use crate::eval::value::Value;
use crate::jit::metrics::JitMetrics;
use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, RwLock, Mutex, Condvar, atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering}};
use std::time::{Duration, Instant};
use std::thread::{self, ThreadId};

/// Configuration for the parallel garbage collector
#[derive(Debug, Clone)]
pub struct ParallelGcConfig {
    /// Maximum number of collector threads
    pub max_collector_threads: usize,
    /// Young generation heap size limit (bytes)
    pub young_generation_size: usize,
    /// Old generation heap size limit (bytes)
    pub old_generation_size: usize,
    /// Large object size threshold (bytes)
    pub large_object_threshold: usize,
    /// Target pause time for minor collections (milliseconds)
    pub target_minor_pause_ms: u64,
    /// Target pause time for major collections (milliseconds)
    pub target_major_pause_ms: u64,
    /// Enable NUMA-aware allocation
    pub numa_aware: bool,
    /// Enable adaptive tuning
    pub adaptive_tuning: bool,
}

impl Default for ParallelGcConfig {
    fn default() -> Self {
        ParallelGcConfig {
            max_collector_threads: num_cpus::get(),
            young_generation_size: 64 * 1024 * 1024, // 64MB
            old_generation_size: 256 * 1024 * 1024,  // 256MB
            large_object_threshold: 32 * 1024,       // 32KB
            target_minor_pause_ms: 10,               // 10ms
            target_major_pause_ms: 50,               // 50ms
            numa_aware: true,
            adaptive_tuning: true,
        }
    }
}

/// Collection phase indicator
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CollectionPhase {
    /// No collection in progress
    Idle,
    /// Minor collection (young generation)
    MinorCollection,
    /// Major collection (full heap)
    MajorCollection,
    /// Incremental collection in progress
    IncrementalCollection,
    /// Concurrent marking phase
    ConcurrentMarking,
}

/// GC statistics and metrics
#[derive(Debug, Default)]
pub struct GcStatistics {
    /// Total number of minor collections
    pub minor_collections: AtomicU64,
    /// Total number of major collections  
    pub major_collections: AtomicU64,
    /// Total time spent in minor collections
    pub minor_collection_time: AtomicU64,
    /// Total time spent in major collections
    pub major_collection_time: AtomicU64,
    /// Total bytes allocated
    pub total_allocated: AtomicU64,
    /// Total bytes reclaimed
    pub total_reclaimed: AtomicU64,
    /// Average minor collection pause times (nanoseconds)
    pub avg_minor_pause_ns: AtomicU64,
    /// Average major collection pause times (nanoseconds)
    pub avg_major_pause_ns: AtomicU64,
    /// Current heap utilization percentage
    pub heap_utilization: AtomicUsize,
}

impl GcStatistics {
    /// Create new statistics instance
    pub fn new() -> Self {
        Self::default()
    }

    /// Record a minor collection
    pub fn record_minor_collection(&self, pause_time: Duration) {
        self.minor_collections.fetch_add(1, Ordering::Relaxed);
        let pause_ns = pause_time.as_nanos() as u64;
        self.minor_collection_time.fetch_add(pause_ns, Ordering::Relaxed);
        
        // Update running average
        let count = self.minor_collections.load(Ordering::Relaxed);
        let total_time = self.minor_collection_time.load(Ordering::Relaxed);
        self.avg_minor_pause_ns.store(total_time / count, Ordering::Relaxed);
    }

    /// Record a major collection
    pub fn record_major_collection(&self, pause_time: Duration) {
        self.major_collections.fetch_add(1, Ordering::Relaxed);
        let pause_ns = pause_time.as_nanos() as u64;
        self.major_collection_time.fetch_add(pause_ns, Ordering::Relaxed);
        
        // Update running average
        let count = self.major_collections.load(Ordering::Relaxed);
        let total_time = self.major_collection_time.load(Ordering::Relaxed);
        self.avg_major_pause_ns.store(total_time / count, Ordering::Relaxed);
    }

    /// Update allocation statistics
    pub fn record_allocation(&self, size: u64) {
        self.total_allocated.fetch_add(size, Ordering::Relaxed);
    }

    /// Update reclamation statistics
    pub fn record_reclamation(&self, size: u64) {
        self.total_reclaimed.fetch_add(size, Ordering::Relaxed);
    }

    /// Update heap utilization
    pub fn update_heap_utilization(&self, percentage: usize) {
        self.heap_utilization.store(percentage, Ordering::Relaxed);
    }
}

/// Thread-safe safepoint coordination
#[derive(Debug)]
pub struct SafepointCoordinator {
    /// Whether a safepoint is requested
    safepoint_requested: AtomicBool,
    /// Number of threads that have reached the safepoint
    threads_at_safepoint: AtomicUsize,
    /// Total number of mutator threads
    total_threads: AtomicUsize,
    /// Synchronization primitives
    safepoint_lock: Mutex<()>,
    safepoint_reached: Condvar,
    safepoint_released: Condvar,
}

impl SafepointCoordinator {
    /// Create new safepoint coordinator
    pub fn new() -> Self {
        SafepointCoordinator {
            safepoint_requested: AtomicBool::new(false),
            threads_at_safepoint: AtomicUsize::new(0),
            total_threads: AtomicUsize::new(0),
            safepoint_lock: Mutex::new(()),
            safepoint_reached: Condvar::new(),
            safepoint_released: Condvar::new(),
        }
    }

    /// Register a mutator thread
    pub fn register_thread(&self) {
        self.total_threads.fetch_add(1, Ordering::Relaxed);
    }

    /// Unregister a mutator thread
    pub fn unregister_thread(&self) {
        self.total_threads.fetch_sub(1, Ordering::Relaxed);
    }

    /// Request all threads to reach safepoint
    pub fn request_safepoint(&self) -> Result<(), String> {
        self.safepoint_requested.store(true, Ordering::Relaxed);
        
        let mut lock = self.safepoint_lock.lock().map_err(|_| "Failed to acquire safepoint lock")?;
        
        // Wait for all threads to reach safepoint
        let total = self.total_threads.load(Ordering::Relaxed);
        
        loop {
            let threads_reached = self.threads_at_safepoint.load(Ordering::Relaxed);
            if threads_reached >= total {
                break;
            }
            
            match self.safepoint_reached.wait_timeout(lock, Duration::from_millis(100)) {
                Ok((new_lock, timeout_result)) => {
                    lock = new_lock;
                    if timeout_result.timed_out() {
                        // Check if threads are making progress
                        let current_reached = self.threads_at_safepoint.load(Ordering::Relaxed);
                        if current_reached < threads_reached {
                            return Err("Timeout waiting for threads to reach safepoint".to_string());
                        }
                    }
                }
                Err(_) => return Err("Error waiting for safepoint".to_string()),
            }
        }

        Ok(())
    }

    /// Release threads from safepoint
    pub fn release_safepoint(&self) {
        self.safepoint_requested.store(false, Ordering::Relaxed);
        self.threads_at_safepoint.store(0, Ordering::Relaxed);
        self.safepoint_released.notify_all();
    }

    /// Check if safepoint is requested (called by mutator threads)
    pub fn check_safepoint(&self) -> bool {
        if self.safepoint_requested.load(Ordering::Relaxed) {
            self.reach_safepoint();
            true
        } else {
            false
        }
    }

    /// Reach safepoint (called by mutator threads)
    fn reach_safepoint(&self) {
        self.threads_at_safepoint.fetch_add(1, Ordering::Relaxed);
        self.safepoint_reached.notify_all();

        // Wait for release
        let lock = self.safepoint_lock.lock().unwrap();
        let _result = self.safepoint_released.wait_while(lock, |_| {
            self.safepoint_requested.load(Ordering::Relaxed)
        });
    }
}

impl Default for SafepointCoordinator {
    fn default() -> Self {
        Self::new()
    }
}

/// Main parallel garbage collector coordinator
#[derive(Debug)]
pub struct ParallelGc {
    /// GC configuration
    config: Arc<ParallelGcConfig>,
    /// Current collection phase
    current_phase: Arc<RwLock<CollectionPhase>>,
    /// GC statistics
    statistics: Arc<GcStatistics>,
    /// Safepoint coordinator
    safepoint: Arc<SafepointCoordinator>,
    /// JIT metrics integration
    jit_metrics: Option<Arc<RwLock<JitMetrics>>>,
    /// Adaptive tuning parameters
    adaptive_params: Arc<RwLock<AdaptiveTuningParams>>,
    /// Collection request queue
    collection_requests: Arc<Mutex<VecDeque<CollectionRequest>>>,
    /// Worker thread pool
    worker_threads: Arc<RwLock<Vec<thread::JoinHandle<()>>>>,
    /// Shutdown flag
    shutdown_requested: Arc<AtomicBool>,
}

/// Adaptive tuning parameters
#[derive(Debug, Clone)]
pub struct AdaptiveTuningParams {
    /// Current allocation rate (bytes per second)
    allocation_rate: f64,
    /// Recent pause time samples
    pause_time_samples: VecDeque<Duration>,
    /// Current heap growth rate
    heap_growth_rate: f64,
    /// Recommended collection frequency adjustment
    collection_frequency_multiplier: f64,
}

impl Default for AdaptiveTuningParams {
    fn default() -> Self {
        AdaptiveTuningParams {
            allocation_rate: 0.0,
            pause_time_samples: VecDeque::with_capacity(100),
            heap_growth_rate: 1.0,
            collection_frequency_multiplier: 1.0,
        }
    }
}

/// Collection request types
#[derive(Debug, Clone)]
pub enum CollectionRequest {
    /// Minor collection requested
    Minor,
    /// Major collection requested  
    Major,
    /// Incremental collection step
    IncrementalStep,
    /// Emergency collection (low memory)
    Emergency,
}

impl ParallelGc {
    /// Create a new parallel garbage collector
    pub fn new(config: ParallelGcConfig) -> Self {
        ParallelGc {
            config: Arc::new(config),
            current_phase: Arc::new(RwLock::new(CollectionPhase::Idle)),
            statistics: Arc::new(GcStatistics::new()),
            safepoint: Arc::new(SafepointCoordinator::new()),
            jit_metrics: None,
            adaptive_params: Arc::new(RwLock::new(AdaptiveTuningParams::default())),
            collection_requests: Arc::new(Mutex::new(VecDeque::new())),
            worker_threads: Arc::new(RwLock::new(Vec::new())),
            shutdown_requested: Arc::new(AtomicBool::new(false)),
        }
    }

    /// Initialize the garbage collector with optional JIT metrics integration
    pub fn initialize(&mut self, jit_metrics: Option<Arc<RwLock<JitMetrics>>>) -> Result<(), String> {
        self.jit_metrics = jit_metrics;
        self.start_worker_threads()?;
        Ok(())
    }

    /// Start worker threads for concurrent collection
    fn start_worker_threads(&self) -> Result<(), String> {
        let num_threads = self.config.max_collector_threads;
        let mut worker_handles = self.worker_threads.write().map_err(|_| "Failed to acquire worker threads lock")?;
        
        for thread_id in 0..num_threads {
            let config = Arc::clone(&self.config);
            let statistics = Arc::clone(&self.statistics);
            let safepoint = Arc::clone(&self.safepoint);
            let collection_requests = Arc::clone(&self.collection_requests);
            let shutdown_requested = Arc::clone(&self.shutdown_requested);
            let current_phase = Arc::clone(&self.current_phase);

            let handle = thread::Builder::new()
                .name(format!("gc-worker-{thread_id}"))
                .spawn(move || {
                    Self::worker_thread_main(
                        thread_id,
                        config,
                        statistics,
                        safepoint,
                        collection_requests,
                        current_phase,
                        shutdown_requested,
                    );
                })
                .map_err(|e| format!("Failed to spawn GC worker thread: {e}"))?;

            worker_handles.push(handle);
        }

        Ok(())
    }

    /// Main worker thread function
    fn worker_thread_main(
        _thread_id: usize,
        _config: Arc<ParallelGcConfig>,
        _statistics: Arc<GcStatistics>,
        _safepoint: Arc<SafepointCoordinator>,
        collection_requests: Arc<Mutex<VecDeque<CollectionRequest>>>,
        _current_phase: Arc<RwLock<CollectionPhase>>,
        shutdown_requested: Arc<AtomicBool>,
    ) {
        while !shutdown_requested.load(Ordering::Relaxed) {
            // Check for collection requests
            let request = {
                let mut requests = collection_requests.lock().unwrap();
                requests.pop_front()
            };

            match request {
                Some(CollectionRequest::Minor) => {
                    // TODO: Implement minor collection
                }
                Some(CollectionRequest::Major) => {
                    // TODO: Implement major collection
                }
                Some(CollectionRequest::IncrementalStep) => {
                    // TODO: Implement incremental step
                }
                Some(CollectionRequest::Emergency) => {
                    // TODO: Implement emergency collection
                }
                None => {
                    // No work available, sleep briefly
                    thread::sleep(Duration::from_millis(1));
                }
            }
        }
    }

    /// Request a minor collection
    pub fn request_minor_collection(&self) -> Result<(), String> {
        let mut requests = self.collection_requests.lock().map_err(|_| "Failed to acquire collection requests lock")?;
        requests.push_back(CollectionRequest::Minor);
        Ok(())
    }

    /// Request a major collection
    pub fn request_major_collection(&self) -> Result<(), String> {
        let mut requests = self.collection_requests.lock().map_err(|_| "Failed to acquire collection requests lock")?;
        requests.push_back(CollectionRequest::Major);
        Ok(())
    }

    /// Get current GC statistics
    pub fn get_statistics(&self) -> &GcStatistics {
        &self.statistics
    }

    /// Get current collection phase
    pub fn get_current_phase(&self) -> CollectionPhase {
        *self.current_phase.read().unwrap()
    }

    /// Update adaptive tuning parameters based on recent performance
    pub fn update_adaptive_tuning(&self, allocation_rate: f64, recent_pause: Duration) {
        if !self.config.adaptive_tuning {
            return;
        }

        if let Ok(mut params) = self.adaptive_params.write() {
            params.allocation_rate = allocation_rate;
            
            // Keep a rolling window of pause time samples
            params.pause_time_samples.push_back(recent_pause);
            if params.pause_time_samples.len() > 100 {
                params.pause_time_samples.pop_front();
            }

            // Calculate average pause time
            let avg_pause = if !params.pause_time_samples.is_empty() {
                let total: Duration = params.pause_time_samples.iter().sum();
                total / params.pause_time_samples.len() as u32
            } else {
                Duration::from_millis(0)
            };

            // Adjust collection frequency based on pause times
            let target_pause = Duration::from_millis(self.config.target_minor_pause_ms);
            if avg_pause > target_pause {
                // Pause times too high, collect more frequently
                params.collection_frequency_multiplier = (params.collection_frequency_multiplier * 1.1).min(3.0);
            } else if avg_pause < target_pause / 2 {
                // Pause times very low, can collect less frequently
                params.collection_frequency_multiplier = (params.collection_frequency_multiplier * 0.9).max(0.5);
            }
        }
    }

    /// Register a mutator thread with the safepoint coordinator
    pub fn register_mutator_thread(&self) {
        self.safepoint.register_thread();
    }

    /// Unregister a mutator thread from the safepoint coordinator
    pub fn unregister_mutator_thread(&self) {
        self.safepoint.unregister_thread();
    }

    /// Check if a safepoint is requested (called by mutator threads)
    pub fn check_safepoint(&self) -> bool {
        self.safepoint.check_safepoint()
    }

    /// Shutdown the garbage collector
    pub fn shutdown(&self) -> Result<(), String> {
        // Signal shutdown
        self.shutdown_requested.store(true, Ordering::Relaxed);

        // Wait for worker threads to finish
        let mut handles = self.worker_threads.write().map_err(|_| "Failed to acquire worker threads lock")?;
        while let Some(handle) = handles.pop() {
            handle.join().map_err(|_| "Failed to join worker thread")?;
        }

        Ok(())
    }
}

impl Drop for ParallelGc {
    fn drop(&mut self) {
        let _ = self.shutdown();
    }
}