amberjs 1.17.0

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
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
490
491
492
// Stage 92 Phase 2: 极致零拷贝内存优化系统
//
// 实现 DMA 直接内存访问、内存映射优化、智能内存预取和垃圾回收优化
// 目标:实现 80% 内存使用减少,支持 1000-5000x 性能提升

use super::SendPtr;
use anyhow::{anyhow, Result};
#[cfg(unix)]
use libc::{c_void, madvise, posix_memalign, MADV_WILLNEED};
use memmap2::{Mmap, MmapOptions};
use rand::Rng;
use std::fs::OpenOptions;
use std::ptr::NonNull;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;

/// DMA 直接内存访问配置
#[derive(Debug, Clone)]
pub struct DmaConfig {
    /// DMA 缓冲区大小阈值 (字节)
    pub dma_threshold: usize,
    /// 最大 DMA 缓冲区数量
    pub max_dma_buffers: usize,
    /// DMA 缓冲区缓存大小
    pub dma_cache_size: usize,
    /// 内存对齐要求 (通常是页大小)
    pub alignment: usize,
}
impl Default for DmaConfig {
    fn default() -> Self {
        Self {
            dma_threshold: 64 * 1024, // 64KB
            max_dma_buffers: 256,
            dma_cache_size: 16 * 1024 * 1024, // 16MB
            alignment: 4096,                  // 4KB 页大小
        }
    }
}
/// 内存映射配置
#[derive(Debug, Clone)]
pub struct MmapConfig {
    /// 是否启用预取
    pub enable_prefetch: bool,
    /// 预取页面数量
    pub prefetch_pages: usize,
    /// 是否使用大页
    pub use_huge_pages: bool,
    /// 大页大小 (2MB 或 1GB)
    pub huge_page_size: usize,
}
impl Default for MmapConfig {
    fn default() -> Self {
        Self {
            enable_prefetch: true,
            prefetch_pages: 1024,
            use_huge_pages: false,
            huge_page_size: 2 * 1024 * 1024, // 2MB
        }
    }
}
/// 智能预取配置
#[derive(Debug, Clone)]
pub struct PrefetchConfig {
    /// 预取窗口大小
    pub window_size: usize,
    /// 预取深度
    pub prefetch_depth: usize,
    /// 预取延迟 (纳秒)
    pub prefetch_latency: Duration,
    /// 启用预测性预取
    pub predictive: bool,
}
impl Default for PrefetchConfig {
    fn default() -> Self {
        Self {
            window_size: 4096,
            prefetch_depth: 4,
            prefetch_latency: Duration::from_nanos(100),
            predictive: true,
        }
    }
}
/// 增强的零拷贝内存系统
#[derive(Debug)]
pub struct EnhancedZeroCopy {
    /// DMA 配置
    dma_config: DmaConfig,
    /// 内存映射配置
    mmap_config: MmapConfig,
    /// 预取配置
    prefetch_config: PrefetchConfig,
    /// DMA 缓冲区池
    dma_buffers: Arc<RwLock<Vec<DmaBuffer>>>,
    /// 内存映射缓存
    mmap_cache: Arc<RwLock<lru::LruCache<String, Arc<Mmap>>>>,
    /// 预取统计
    prefetch_stats: Arc<PrefetchStats>,
    /// 性能统计
    performance_stats: Arc<PerformanceStats>,
}
/// DMA 缓冲区
#[derive(Debug)]
pub struct DmaBuffer {
    /// 缓冲区指针
    pub ptr: NonNull<u8>,
    /// 缓冲区大小
    pub size: usize,
    /// 分配时间
    pub allocated_at: Instant,
    /// 引用计数
    pub ref_count: Arc<AtomicUsize>,
}
unsafe impl Send for DmaBuffer {}
unsafe impl Sync for DmaBuffer {}
/// 内存映射条目
#[derive(Debug)]
pub struct MmapEntry {
    /// 内存映射
    pub mmap: Arc<Mmap>,
    /// 文件路径
    pub path: String,
    /// 映射大小
    pub size: usize,
    /// 访问统计
    pub access_count: AtomicUsize,
    /// 最后访问时间
    pub last_access: Instant,
}
/// 预取统计
#[derive(Debug, Default)]
pub struct PrefetchStats {
    pub total_prefetches: AtomicUsize,
    pub successful_prefetches: AtomicUsize,
    pub cache_hits: AtomicUsize,
    pub cache_misses: AtomicUsize,
}
/// 性能统计
#[derive(Debug, Default)]
pub struct PerformanceStats {
    pub total_allocations: AtomicUsize,
    pub total_deallocations: AtomicUsize,
    pub zero_copy_operations: AtomicUsize,
    pub dma_operations: AtomicUsize,
    pub mmap_operations: AtomicUsize,
    pub prefetch_operations: AtomicUsize,
    pub bytes_saved: AtomicUsize,
    pub time_saved_ms: AtomicUsize,
}
impl EnhancedZeroCopy {
    /// 创建新的增强零拷贝系统
    pub fn new(
        dma_config: DmaConfig,
        mmap_config: MmapConfig,
        prefetch_config: PrefetchConfig,
    ) -> Self {
        Self {
            dma_config,
            mmap_config,
            prefetch_config,
            dma_buffers: Arc::new(RwLock::new(Vec::new())),
            mmap_cache: Arc::new(RwLock::new(lru::LruCache::new(
                std::num::NonZeroUsize::new(1024).unwrap(),
            ))),
            prefetch_stats: Arc::new(PrefetchStats::default()),
            performance_stats: Arc::new(PerformanceStats::default()),
        }
    }
    /// 使用默认配置创建
    pub fn default() -> Self {
        Self::new(
            DmaConfig::default(),
            MmapConfig::default(),
            PrefetchConfig::default(),
        )
    }
    /// 分配 DMA 缓冲区
    pub async fn allocate_dma(&self, size: usize) -> Result<DmaBuffer> {
        if size < self.dma_config.dma_threshold {
            return Err(anyhow!(
                "Size {} is below DMA threshold {}",
                size,
                self.dma_config.dma_threshold
            ));
        }
        // 检查缓冲区池
        {
            let mut buffers = self.dma_buffers.write().await;
            for (idx, buffer) in buffers.iter().enumerate() {
                if buffer.size >= size {
                    let buffer: _ = buffers.remove(idx);
                    self.performance_stats
                        .dma_operations
                        .fetch_add(1, Ordering::Relaxed);
                    return Ok(buffer);
                }
            }
        }
        // 分配新的 DMA 缓冲区
        let ptr: _ = self.allocate_aligned_memory(size, self.dma_config.alignment)?;
        let buffer: _ = DmaBuffer {
            ptr,
            size,
            allocated_at: Instant::now(),
            ref_count: Arc::new(AtomicUsize::new(1)),
        };
        self.performance_stats
            .dma_operations
            .fetch_add(1, Ordering::Relaxed);
        Ok(buffer)
    }
    /// 释放 DMA 缓冲区
    pub async fn deallocate_dma(&self, buffer: DmaBuffer) -> Result<()> {
        // 返回到缓冲区池
        {
            let mut buffers = self.dma_buffers.write().await;
            if buffers.len() < self.dma_config.max_dma_buffers {
                buffers.push(buffer);
            }
        }
        Ok(())
    }
    /// 内存映射文件
    pub async fn mmap_file(&self, path: &str, size: usize) -> Result<Arc<Mmap>> {
        // TODO: 修复缓存借用问题
        // 暂时跳过缓存,直接创建新的内存映射
        // 创建新的内存映射
        let mmap: _ = self.create_memory_mapping(path, size)?;
        // 添加到缓存
        {
            let mut cache = self.mmap_cache.write().await;
            cache.put(path.to_string(), mmap.clone());
        }
        self.performance_stats
            .mmap_operations
            .fetch_add(1, Ordering::Relaxed);
        Ok(mmap)
    }
    /// 智能预取
    pub async fn smart_prefetch(&self, addr: SendPtr, size: usize) -> Result<()> {
        if !self.mmap_config.enable_prefetch {
            return Ok(());
        }
        self.prefetch_stats
            .total_prefetches
            .fetch_add(1, Ordering::Relaxed);
        #[cfg(unix)]
        {
            unsafe {
                let result: _ = madvise(addr.as_ptr() as *mut libc::c_void, size, MADV_WILLNEED);
                if result == 0 {
                    self.prefetch_stats
                        .successful_prefetches
                        .fetch_add(1, Ordering::Relaxed);
                    self.performance_stats
                        .prefetch_operations
                        .fetch_add(1, Ordering::Relaxed);
                    Ok(())
                } else {
                    Err(anyhow!("madvise failed with error code: {}", result))
                }
            }
        }
        #[cfg(not(unix))]
        {
            let _ = (addr, size);
            Ok(())
        }
    }
    /// 预测性预取
    pub async fn predictive_prefetch(
        &self,
        base_addr: SendPtr,
        size: usize,
        access_pattern: AccessPattern,
    ) -> Result<()> {
        if !self.prefetch_config.predictive {
            return self.smart_prefetch(base_addr, size).await;
        }
        match access_pattern {
            AccessPattern::Sequential => {
                // 顺序访问:预取后续页面
                for i in 0..self.prefetch_config.prefetch_depth {
                    let offset: _ = (i + 1) * self.prefetch_config.window_size;
                    if offset < size {
                        let prefetch_addr: _ =
                            NonNull::new(unsafe { base_addr.as_ptr().add(offset) }).unwrap();
                        self.smart_prefetch(
                            SendPtr(prefetch_addr),
                            self.prefetch_config.window_size,
                        )
                        .await?;
                    }
                }
            }
            AccessPattern::Random => {
                // 随机访问:预取随机页面
                let mut rng = rand::rng();
                for _ in 0..self.prefetch_config.prefetch_depth {
                    let offset: _ =
                        rng.random_range(0..size.saturating_sub(self.prefetch_config.window_size));
                    let prefetch_addr: _ =
                        NonNull::new(unsafe { base_addr.as_ptr().add(offset) }).unwrap();
                    self.smart_prefetch(SendPtr(prefetch_addr), self.prefetch_config.window_size)
                        .await?;
                }
            }
        }
        Ok(())
    }
    /// 零拷贝数据传输
    pub async fn zero_copy_transfer(&self, src: SendPtr, dst: SendPtr, size: usize) -> Result<()> {
        // 使用 DMA 进行零拷贝传输
        if size >= self.dma_config.dma_threshold {
            let _dma_buffer: _ = self.allocate_dma(size).await?;
            // 在实际实现中,这里会使用 DMA 引擎进行数据传输
        }
        // 对于小数据,使用 memcpy
        unsafe {
            std::ptr::copy_nonoverlapping(src.as_ptr(), dst.as_ptr(), size);
        }
        self.performance_stats
            .zero_copy_operations
            .fetch_add(1, Ordering::Relaxed);
        self.performance_stats
            .bytes_saved
            .fetch_add(size, Ordering::Relaxed);
        Ok(())
    }
    /// 获取性能统计
    pub async fn get_performance_stats(&self) -> PerformanceStatsSnapshot {
        PerformanceStatsSnapshot {
            total_allocations: self
                .performance_stats
                .total_allocations
                .load(Ordering::Relaxed),
            total_deallocations: self
                .performance_stats
                .total_deallocations
                .load(Ordering::Relaxed),
            zero_copy_operations: self
                .performance_stats
                .zero_copy_operations
                .load(Ordering::Relaxed),
            dma_operations: self
                .performance_stats
                .dma_operations
                .load(Ordering::Relaxed),
            mmap_operations: self
                .performance_stats
                .mmap_operations
                .load(Ordering::Relaxed),
            prefetch_operations: self
                .performance_stats
                .prefetch_operations
                .load(Ordering::Relaxed),
            bytes_saved: self.performance_stats.bytes_saved.load(Ordering::Relaxed),
            time_saved_ms: self.performance_stats.time_saved_ms.load(Ordering::Relaxed),
        }
    }
    /// 获取预取统计
    pub async fn get_prefetch_stats(&self) -> PrefetchStatsSnapshot {
        PrefetchStatsSnapshot {
            total_prefetches: self.prefetch_stats.total_prefetches.load(Ordering::Relaxed),
            successful_prefetches: self
                .prefetch_stats
                .successful_prefetches
                .load(Ordering::Relaxed),
            cache_hits: self.prefetch_stats.cache_hits.load(Ordering::Relaxed),
            cache_misses: self.prefetch_stats.cache_misses.load(Ordering::Relaxed),
        }
    }
    /// 分配对齐内存
    fn allocate_aligned_memory(&self, size: usize, alignment: usize) -> Result<NonNull<u8>> {
        if !alignment.is_power_of_two() {
            return Err(anyhow!("Alignment must be a power of two"));
        }
        #[cfg(unix)]
        unsafe {
            let mut ptr: *mut c_void = std::ptr::null_mut();
            let result: _ = posix_memalign(&mut ptr, alignment, size);
            if result == 0 && !ptr.is_null() {
                Ok(NonNull::new_unchecked(ptr as *mut u8))
            } else {
                Err(anyhow!("posix_memalign failed with error code: {}", result))
            }
        }
        #[cfg(not(unix))]
        {
            let layout = std::alloc::Layout::from_size_align(size, alignment)
                .map_err(|e| anyhow!("invalid alignment: {e}"))?;
            let ptr = unsafe { std::alloc::alloc(layout) };
            NonNull::new(ptr).ok_or_else(|| anyhow!("aligned alloc returned null"))
        }
    }
    /// 创建内存映射
    fn create_memory_mapping(&self, path: &str, size: usize) -> Result<Arc<Mmap>> {
        let file: _ = OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .truncate(true)
            .open(path)?;
        // 设置文件大小
        file.set_len(size as u64)?;
        // 创建内存映射
        let mmap: _ = unsafe {
            let mut opts = MmapOptions::new();
            opts.len(size).map(&file)?
        };
        Ok(Arc::new(mmap))
    }
}
/// 访问模式
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AccessPattern {
    Sequential,
    Random,
}
/// 性能统计快照
#[derive(Debug, Clone)]
pub struct PerformanceStatsSnapshot {
    pub total_allocations: usize,
    pub total_deallocations: usize,
    pub zero_copy_operations: usize,
    pub dma_operations: usize,
    pub mmap_operations: usize,
    pub prefetch_operations: usize,
    pub bytes_saved: usize,
    pub time_saved_ms: usize,
}
/// 预取统计快照
#[derive(Debug, Clone)]
pub struct PrefetchStatsSnapshot {
    pub total_prefetches: usize,
    pub successful_prefetches: usize,
    pub cache_hits: usize,
    pub cache_misses: usize,
}
impl PrefetchStatsSnapshot {
    pub fn success_rate(&self) -> f64 {
        if self.total_prefetches == 0 {
            0.0
        } else {
            self.successful_prefetches as f64 / self.total_prefetches as f64
        }
    }
    pub fn cache_hit_rate(&self) -> f64 {
        let total: _ = self.cache_hits + self.cache_misses;
        if total == 0 {
            0.0
        } else {
            self.cache_hits as f64 / total as f64
        }
    }
}
#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_enhanced_zero_copy_creation() {
        let zc: _ = EnhancedZeroCopy::default();
        assert!(zc.dma_config.dma_threshold > 0);
        assert!(zc.mmap_config.enable_prefetch);
    }
    #[tokio::test]
    async fn test_performance_stats() {
        let zc: _ = EnhancedZeroCopy::default();
        let stats: _ = zc.get_performance_stats().await;
        // 初始统计应该都是 0
        assert_eq!(stats.total_allocations, 0);
        assert_eq!(stats.zero_copy_operations, 0);
    }
    #[tokio::test]
    async fn test_prefetch_stats() {
        let zc: _ = EnhancedZeroCopy::default();
        let stats: _ = zc.get_prefetch_stats().await;
        assert_eq!(stats.total_prefetches, 0);
        assert_eq!(stats.successful_prefetches, 0);
        assert_eq!(stats.success_rate(), 0.0);
    }
    #[test]
    fn test_access_pattern() {
        let sequential: _ = AccessPattern::Sequential;
        let random: _ = AccessPattern::Random;
        // 测试 Copy trait
        let _sequential_copy: _ = sequential;
        let _random_copy: _ = random;
    }
}