s0_udp 0.1.1

sync0 基于 compio 的高速 UDP 传输、线协议、自适应 Zstd 与 Dragonfly P2P 引擎
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
use std::{
  fs::File,
  mem::swap,
  path::PathBuf,
  sync::{
    Arc,
    atomic::{AtomicBool, AtomicI32, AtomicU32, Ordering},
  },
  thread::{self, JoinHandle},
  time::Duration,
};

use parking_lot::{Condvar, Mutex, RwLock};
use s0_cdc::{FileAt, FileSpan, locate_span};
use s0_codec::{Codec, Detector, skip_name};
use s0_const::{MAX_BLOCK, ZSTD_DEFAULT_LEVEL};
use s0_fs::{RootBuf, SmallCache};

use crate::{
  ArgError, UdpError,
  bench::{EMPTY, manifest::Entry},
  now_us,
};

/// 后台预取与并行 Zstd 压缩工作线程数
const PREFETCH_WORKERS: usize = 4;
/// 表示关闭 Zstd 压缩的哨兵级别(区别于负数快速压缩级别 `-7`、`-3`)
pub(crate) const LVL_OFF: i32 = i32::MIN;
/// 前瞻预取窗口(128 块 = 16 MiB)
const PREFETCH_AHEAD: u32 = 128;
/// 预取环容量(必须为 2 的幂且 `>= 2 * PREFETCH_AHEAD`)
const PREFETCH_RING: usize = 256;

pub(crate) enum SrcKind {
  File(File),
  Dir(PathBuf, Arc<SmallCache>),
}

impl SrcKind {
  pub(crate) fn root_buf(&self) -> RootBuf {
    match self {
      Self::File(_) => RootBuf::new(PathBuf::new().as_path()),
      Self::Dir(root, _) => RootBuf::new(root),
    }
  }
}

/// 从虚拟流偏移 `off` 读取 `raw.len()` 字节;对连续的 `SmallCache` 小文件自动合并为单次读取
pub(crate) fn read_into(
  kind: &SrcKind,
  entries: &[Entry],
  spans: &[FileSpan],
  cursor: (&mut usize, &mut RootBuf, &mut Option<(usize, File)>),
  dst: (&mut [u8], u64),
) -> Result<(bool, bool), UdpError> {
  let (span_hint, root_buf, open) = cursor;
  let (raw, off) = dst;
  let len = raw.len();
  let end = off + len as u64;
  let mut done = 0usize;
  let mut all_skip_ext = matches!(kind, SrcKind::Dir(..));
  let mut entered_compressible = false;
  let start = locate_span(spans, span_hint, off);

  let mut pending_cache: Option<(u64, usize, usize)> = None;
  let flush_cache = |cache: &SmallCache,
                     raw: &mut [u8],
                     pending: &mut Option<(u64, usize, usize)>|
   -> Result<(), UdpError> {
    if let Some((c_off, r_start, r_end)) = pending.take() {
      cache.read_at(c_off, &mut raw[r_start..r_end])?;
    }
    Ok(())
  };

  for (i, sp) in spans[start..].iter().enumerate() {
    if sp.delta_off >= end || done >= len {
      break;
    }
    if sp.delta_off + sp.len <= end {
      *span_hint = start + i + 1;
    } else {
      *span_hint = start + i;
    }
    let s = off.max(sp.delta_off);
    let t = end.min(sp.delta_off + sp.len);
    if t > s {
      let chunk_len = (t - s) as usize;
      let file_off = sp.file_off + (s - sp.delta_off);
      match kind {
        SrcKind::File(f) => {
          all_skip_ext = false;
          f.read_exact_at_pos(&mut raw[done..done + chunk_len], file_off)?;
        }
        SrcKind::Dir(_, cache) => {
          let idx = sp.idx;
          let e = &entries[idx];
          if !skip_name(&e.path) {
            all_skip_ext = false;
            if file_off == 0 {
              entered_compressible = true;
            }
          }
          if let Some(base) = e.cached {
            let c_off = base + file_off;
            match &mut pending_cache {
              Some((base_c, r_start, r_end))
                if *r_end == done && *base_c + ((*r_end - *r_start) as u64) == c_off =>
              {
                *r_end = done + chunk_len;
              }
              _ => {
                flush_cache(cache, raw, &mut pending_cache)?;
                pending_cache = Some((c_off, done, done + chunk_len));
              }
            }
          } else {
            flush_cache(cache, raw, &mut pending_cache)?;
            let chunk = &mut raw[done..done + chunk_len];
            if let Some((cached_idx, f)) = open
              && *cached_idx == idx
            {
              f.read_exact_at_pos(chunk, file_off)?;
            } else {
              let f = File::open(root_buf.join(&e.path))?;
              f.read_exact_at_pos(chunk, file_off)?;
              if file_off + (t - s) < e.size {
                *open = Some((idx, f));
              }
            }
          }
        }
      }
      done += chunk_len;
    }
  }
  if let SrcKind::Dir(_, cache) = kind {
    flush_cache(cache, raw, &mut pending_cache)?;
  }
  if done != len {
    return Err(ArgError::StreamBounds.into());
  }
  Ok((all_skip_ext, entered_compressible))
}

struct Plan {
  epoch: u64,
  spans: Arc<Vec<FileSpan>>,
  total: u64,
  nblk: u32,
}

pub(crate) struct ReadyBlk {
  pub(crate) epoch: u64,
  pub(crate) blk: u32,
  pub(crate) zstd: bool,
  pub(crate) data: Vec<u8>,
  pub(crate) raw_len: usize,
  pub(crate) comp_us: u64,
  pub(crate) lvl: i32,
  pub(crate) attempted_zstd: bool,
}

impl Default for ReadyBlk {
  fn default() -> Self {
    Self {
      epoch: 0,
      blk: EMPTY,
      zstd: false,
      data: Vec::new(),
      raw_len: 0,
      comp_us: 0,
      lvl: 1,
      attempted_zstd: false,
    }
  }
}

impl ReadyBlk {
  #[inline]
  fn take_if_match(&mut self, epoch: u64, blk: u32, dst: &mut Vec<u8>) -> Option<Self> {
    if self.epoch != epoch || self.blk != blk {
      return None;
    }
    self.blk = EMPTY;
    let mut out = Self {
      epoch: self.epoch,
      blk,
      zstd: self.zstd,
      data: Vec::new(),
      raw_len: self.raw_len,
      comp_us: self.comp_us,
      lvl: self.lvl,
      attempted_zstd: self.attempted_zstd,
    };
    swap(dst, &mut self.data);
    swap(&mut out.data, dst);
    Some(out)
  }
}

struct SharedState {
  kind: Arc<SrcKind>,
  entries: Arc<Vec<Entry>>,
  skip_all: bool,
  check_magic: bool,
  plan: RwLock<Plan>,
  next_fetch_blk: AtomicU32,
  max_allowed_blk: AtomicU32,
  lvl: AtomicI32,
  stop: AtomicBool,
  work_mu: Mutex<()>,
  work_cv: Condvar,
  ring: Box<[Mutex<ReadyBlk>]>,
  ready_cv: Condvar,
}

pub(crate) struct Prefetcher {
  shared: Arc<SharedState>,
  workers: Vec<JoinHandle<()>>,
}

impl Prefetcher {
  pub(crate) fn new(
    kind: Arc<SrcKind>,
    entries: Arc<Vec<Entry>>,
    spans: Arc<Vec<FileSpan>>,
    total: u64,
    skip_all: bool,
    check_magic: bool,
  ) -> Self {
    let nblk = total.div_ceil(MAX_BLOCK as u64) as u32;
    let init_ahead = nblk.min(PREFETCH_AHEAD);
    let ring = (0..PREFETCH_RING)
      .map(|_| Mutex::new(ReadyBlk::default()))
      .collect::<Vec<_>>()
      .into_boxed_slice();
    let shared = Arc::new(SharedState {
      kind,
      entries,
      skip_all,
      check_magic,
      plan: RwLock::new(Plan {
        epoch: 1,
        spans,
        total,
        nblk,
      }),
      next_fetch_blk: AtomicU32::new(0),
      max_allowed_blk: AtomicU32::new(init_ahead),
      lvl: AtomicI32::new(if skip_all {
        LVL_OFF
      } else {
        ZSTD_DEFAULT_LEVEL
      }),
      stop: AtomicBool::new(false),
      work_mu: Mutex::new(()),
      work_cv: Condvar::new(),
      ring,
      ready_cv: Condvar::new(),
    });
    let n_workers = if nblk > 4 {
      (nblk as usize).div_ceil(8).clamp(1, PREFETCH_WORKERS)
    } else {
      0
    };
    let mut workers = Vec::with_capacity(n_workers);
    for _ in 0..n_workers {
      let st = Arc::clone(&shared);
      workers.push(thread::spawn(move || worker_loop(st)));
    }
    Self { shared, workers }
  }

  #[inline]
  pub(crate) fn worker_count(&self) -> u32 {
    self.workers.len().max(1) as u32
  }

  #[inline]
  pub(crate) fn set_level(&self, lvl: i32) {
    self.shared.lvl.store(lvl, Ordering::Relaxed);
  }

  pub(crate) fn reset_plan(&self, epoch: u64, spans: Arc<Vec<FileSpan>>, total: u64) {
    let nblk = total.div_ceil(MAX_BLOCK as u64) as u32;
    {
      let mut p = self.shared.plan.write();
      p.epoch = epoch;
      p.spans = spans;
      p.total = total;
      p.nblk = nblk;
    }
    self.shared.next_fetch_blk.store(0, Ordering::Release);
    self
      .shared
      .max_allowed_blk
      .store(nblk.min(PREFETCH_AHEAD), Ordering::Release);
    self.shared.work_cv.notify_all();
  }

  pub(crate) fn request_ahead(&self, blk: u32, nblk: u32) {
    if self.workers.is_empty() {
      return;
    }
    let want = blk.saturating_add(PREFETCH_AHEAD).min(nblk);
    let prev = self
      .shared
      .max_allowed_blk
      .fetch_max(want, Ordering::AcqRel);
    if want > prev {
      self.shared.work_cv.notify_all();
    }
  }

  pub(crate) fn take_ready(&self, epoch: u64, blk: u32, dst: &mut Vec<u8>) -> Option<ReadyBlk> {
    if self.workers.is_empty() {
      return None;
    }
    let r_idx = (blk as usize) & (PREFETCH_RING - 1);
    if let Some(out) = self.shared.ring[r_idx]
      .lock()
      .take_if_match(epoch, blk, dst)
    {
      return Some(out);
    }
    // 若尚未被后台线程领取,则由主线程认领并返回 None 直接内联计算,绝不空等
    loop {
      let cur = self.shared.next_fetch_blk.load(Ordering::Acquire);
      if blk < cur {
        break;
      }
      if self
        .shared
        .next_fetch_blk
        .compare_exchange_weak(cur, blk + 1, Ordering::AcqRel, Ordering::Acquire)
        .is_ok()
      {
        return None;
      }
    }
    // 已被后台工作线程认领,等待其完成
    let mut g = self.shared.ring[r_idx].lock();
    for _ in 0..200 {
      if let Some(out) = g.take_if_match(epoch, blk, dst) {
        return Some(out);
      }
      self
        .shared
        .ready_cv
        .wait_for(&mut g, Duration::from_micros(50));
    }
    None
  }
}

impl Drop for Prefetcher {
  fn drop(&mut self) {
    self.shared.stop.store(true, Ordering::Release);
    self.shared.work_cv.notify_all();
    for h in self.workers.drain(..) {
      let _ = h.join();
    }
  }
}

fn worker_loop(st: Arc<SharedState>) {
  let mut codec = (!st.skip_all)
    .then(|| Codec::new(ZSTD_DEFAULT_LEVEL).ok())
    .flatten();
  let mut detector = Detector::with_flags(st.skip_all, st.check_magic);
  let mut root_buf = st.kind.root_buf();
  let mut open_fd: Option<(usize, File)> = None;
  let mut span_hint = 0usize;
  let mut last_epoch = 0u64;
  let mut raw_buf = Vec::with_capacity(MAX_BLOCK);

  while !st.stop.load(Ordering::Acquire) {
    let cur = st.next_fetch_blk.load(Ordering::Relaxed);
    let max_blk = st.max_allowed_blk.load(Ordering::Acquire);
    if cur >= max_blk {
      let mut g = st.work_mu.lock();
      if st.next_fetch_blk.load(Ordering::Relaxed) >= st.max_allowed_blk.load(Ordering::Acquire)
        && !st.stop.load(Ordering::Acquire)
      {
        st.work_cv.wait(&mut g);
      }
      continue;
    }
    let blk = st.next_fetch_blk.fetch_add(1, Ordering::AcqRel);
    let (epoch, spans, total, nblk) = {
      let p = st.plan.read();
      (p.epoch, Arc::clone(&p.spans), p.total, p.nblk)
    };
    if blk >= nblk {
      continue;
    }
    if epoch != last_epoch {
      last_epoch = epoch;
      span_hint = 0;
    }
    let off = u64::from(blk) * MAX_BLOCK as u64;
    let len = total.saturating_sub(off).min(MAX_BLOCK as u64) as usize;
    raw_buf.resize(len, 0);
    let Ok((all_skip_ext, entered_compressible)) = read_into(
      &st.kind,
      &st.entries,
      &spans,
      (&mut span_hint, &mut root_buf, &mut open_fd),
      (&mut raw_buf, off),
    ) else {
      continue;
    };
    if entered_compressible && detector.skip() {
      detector.reset_file();
    }
    let lvl = st.lvl.load(Ordering::Relaxed);
    let mut zstd = false;
    let mut comp_us = 0u64;
    let mut attempted_zstd = false;

    let r_idx = (blk as usize) & (PREFETCH_RING - 1);
    let mut slot = st.ring[r_idx].lock();
    slot.data.clear();
    if !all_skip_ext
      && lvl != LVL_OFF
      && let Some(c) = codec.as_mut()
      && c.set_level(lvl).is_ok()
    {
      let was_skip = detector.skip();
      let t0 = now_us();
      if let Ok(Some(comp)) = detector.compress(&raw_buf, c) {
        comp_us = now_us().saturating_sub(t0).max(1);
        attempted_zstd = true;
        zstd = true;
        slot.data.extend_from_slice(comp);
      } else {
        if !was_skip {
          comp_us = now_us().saturating_sub(t0).max(1);
          attempted_zstd = true;
        }
        slot.data.extend_from_slice(&raw_buf);
      }
    } else {
      slot.data.extend_from_slice(&raw_buf);
    }
    slot.epoch = epoch;
    slot.blk = blk;
    slot.zstd = zstd;
    slot.raw_len = len;
    slot.comp_us = comp_us;
    slot.lvl = lvl;
    slot.attempted_zstd = attempted_zstd;
    drop(slot);
    st.ready_cv.notify_all();
  }
}