Repository navigation
Expand file tree
/
Copy pathsurface_triage.rs
More file actions
461 lines (434 loc) · 16 KB
/
Copy pathsurface_triage.rs
File metadata and controls
461 lines (434 loc) · 16 KB
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
//! Local, offline deterministic surface triage engine for pinpoint repair.
//!
//! Pure deterministic statistics + expert rules (zero AI/ML models and zero network calls):
//! * a streaming latency baseline gives every disk its own adaptive "slow" threshold,
//! * scan findings become typed targets (unreadable sector / weak sector / slow area),
//! * dense clusters of bad sectors are widened into dead zones with a safety margin,
//! * every target gets an action (heal, isolate, relocate) and the disk gets a verdict.
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
/// Most targets kept from one full scan (enough for heavy damage, small enough for SQLite).
pub const MAX_TARGETS: usize = 8192;
/// Hits closer than this are treated as one damaged area.
pub const DEAD_ZONE_GAP: u64 = 64 * 1024 * 1024;
/// Hits needed inside one area before it is called a dead zone.
pub const DEAD_ZONE_MIN_HITS: usize = 3;
/// Extra space quarantined around a dead zone (damage on platters tends to spread).
pub const DEAD_ZONE_MARGIN: u64 = 1024 * 1024;
/// Largest single dead zone; anything bigger means "replace the disk", not "fence it off".
pub const DEAD_ZONE_MAX: u64 = 512 * 1024 * 1024;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TargetKind {
/// Read-only slow region (stale SSD data or a weak HDD area without a single bad sector).
SlowArea,
/// Sector that reads, but only after long retries.
WeakSector,
/// Sector that cannot be read at all (pending / uncorrectable).
Unreadable,
}
impl TargetKind {
pub fn label(self) -> &'static str {
self.label_for(false)
}
/// Label in the requested UI language (`true` = English).
pub fn label_for(self, en: bool) -> &'static str {
match (self, en) {
(TargetKind::SlowArea, false) => "Yavaş bölge",
(TargetKind::WeakSector, false) => "Zayıf sektör",
(TargetKind::Unreadable, false) => "Okunamayan sektör",
(TargetKind::SlowArea, true) => "Slow region",
(TargetKind::WeakSector, true) => "Weak sector",
(TargetKind::Unreadable, true) => "Unreadable sector",
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Target {
/// Byte offset on the physical disk.
pub offset: u64,
pub len: u64,
pub kind: TargetKind,
pub latency_ms: Option<f64>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Action {
/// Move user data away, rewrite the sector so firmware refreshes or remaps it, re-test.
Heal,
/// Move user data away and permanently fence the area off.
Isolate,
/// Rewrite user data to a fresh location (SSD stale-data refresh).
Relocate,
}
/// Streaming latency histogram (log2 buckets from 1/16 ms) with an O(1) median.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct LatencyBaseline {
buckets: [u64; 32],
count: u64,
}
impl LatencyBaseline {
pub fn new() -> Self {
Self::default()
}
fn bucket(ms: f64) -> usize {
let v = (ms / 0.0625).max(1.0);
(v.log2().floor() as usize).min(31)
}
pub fn push(&mut self, ms: f64) {
if ms.is_finite() && ms >= 0.0 {
self.buckets[Self::bucket(ms)] += 1;
self.count += 1;
}
}
pub fn count(&self) -> u64 {
self.count
}
/// Geometric centre of the bucket holding the median sample.
pub fn median(&self) -> Option<f64> {
if self.count == 0 {
return None;
}
let half = self.count.div_ceil(2);
let mut seen = 0;
for (i, n) in self.buckets.iter().enumerate() {
seen += n;
if seen >= half {
return Some(0.0625 * 2f64.powi(i as i32) * std::f64::consts::SQRT_2);
}
}
None
}
}
/// Per-disk "slow chunk" threshold for 1 MiB reads. Fixed until 256 samples exist, then
/// a multiple of this disk's own median, so a USB HDD and an NVMe SSD are judged fairly.
pub fn slow_threshold_ms(base: &LatencyBaseline, is_hdd: bool) -> f64 {
if base.count() < 256 {
return if is_hdd { 80.0 } else { 30.0 };
}
let m = base.median().unwrap_or(1.0);
if is_hdd {
(m * 8.0).clamp(50.0, 150.0)
} else {
(m * 10.0).clamp(8.0, 80.0)
}
}
/// Groups unreadable / weak hits that sit close together. Returns physical `(start, end)`
/// spans (margin included) for every group with at least `DEAD_ZONE_MIN_HITS` hits.
pub fn dead_zones(targets: &[Target]) -> Vec<(u64, u64)> {
let mut hits: Vec<&Target> = targets
.iter()
.filter(|t| t.kind != TargetKind::SlowArea)
.collect();
hits.sort_by_key(|t| t.offset);
let mut zones = Vec::new();
let mut i = 0;
while i < hits.len() {
let mut j = i;
while j + 1 < hits.len()
&& hits[j + 1]
.offset
.saturating_sub(hits[j].offset + hits[j].len)
<= DEAD_ZONE_GAP
&& hits[j + 1].offset + hits[j + 1].len - hits[i].offset <= DEAD_ZONE_MAX
{
j += 1;
}
if j + 1 - i >= DEAD_ZONE_MIN_HITS {
let start = hits[i].offset.saturating_sub(DEAD_ZONE_MARGIN);
let end = hits[j].offset + hits[j].len + DEAD_ZONE_MARGIN;
zones.push((start, end));
}
i = j + 1;
}
zones
}
/// One planned repair unit in volume cluster space.
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct Region {
pub lcn: u64,
pub clusters: u64,
pub phys_offset: u64,
pub kind: TargetKind,
pub dead_zone: bool,
pub latency_ms: Option<f64>,
}
/// Maps physical targets onto the selected volume, widens dead zones, merges overlaps.
/// Returns the regions and how many targets fell outside this volume.
pub fn plan_regions(
targets: &[Target],
vol_start: u64,
vol_len: u64,
cluster: u64,
) -> (Vec<Region>, usize) {
let cluster = cluster.max(512);
let vol_end = vol_start.saturating_add(vol_len);
let zones = dead_zones(targets);
let mut spans: Vec<(u64, u64, TargetKind, bool, Option<f64>)> = Vec::new();
let mut outside = 0usize;
for t in targets {
let end = t.offset + t.len.max(1);
if end <= vol_start || t.offset >= vol_end {
outside += 1;
continue;
}
let in_zone = zones.iter().any(|(s, e)| t.offset < *e && end > *s);
if !in_zone {
spans.push((
t.offset.max(vol_start),
end.min(vol_end),
t.kind,
false,
t.latency_ms,
));
}
}
for (s, e) in &zones {
if *e <= vol_start || *s >= vol_end {
continue;
}
let worst = targets
.iter()
.filter(|t| t.offset < *e && t.offset + t.len > *s)
.map(|t| t.kind)
.max()
.unwrap_or(TargetKind::WeakSector);
let lat = targets
.iter()
.filter(|t| t.offset < *e && t.offset + t.len > *s)
.filter_map(|t| t.latency_ms)
.fold(None, |acc: Option<f64>, v| {
Some(acc.map_or(v, |a| a.max(v)))
});
spans.push(((*s).max(vol_start), (*e).min(vol_end), worst, true, lat));
}
let mut regions: Vec<Region> = spans
.into_iter()
.map(|(s, e, kind, dz, lat)| {
let lcn = (s - vol_start) / cluster;
let end_lcn = (e - vol_start).div_ceil(cluster);
Region {
lcn,
clusters: end_lcn.saturating_sub(lcn).max(1),
phys_offset: vol_start + lcn * cluster,
kind,
dead_zone: dz,
latency_ms: lat,
}
})
.collect();
regions.sort_by_key(|r| r.lcn);
let mut merged: Vec<Region> = Vec::with_capacity(regions.len());
for r in regions {
if let Some(last) = merged.last_mut() {
if r.lcn <= last.lcn + last.clusters {
let end = (r.lcn + r.clusters).max(last.lcn + last.clusters);
last.clusters = end - last.lcn;
last.kind = last.kind.max(r.kind);
last.dead_zone |= r.dead_zone;
last.latency_ms = match (last.latency_ms, r.latency_ms) {
(Some(a), Some(b)) => Some(a.max(b)),
(a, b) => a.or(b),
};
continue;
}
}
merged.push(r);
}
(merged, outside)
}
/// What the engine should do with one region.
pub fn decide(region: &Region, is_hdd: bool, isolate_mode: bool) -> Action {
if !is_hdd {
return Action::Relocate;
}
if isolate_mode || region.dead_zone {
Action::Isolate
} else {
Action::Heal
}
}
/// Disk-level verdict after a full scan, shown as the "Akıllı teşhis" card.
pub fn diagnose(targets: &[Target], is_hdd: bool, pending_197: u64, tail_degraded: bool) -> Value {
let count = |k: TargetKind| targets.iter().filter(|t| t.kind == k).count();
let unreadable = count(TargetKind::Unreadable);
let weak = count(TargetKind::WeakSector);
let slow = count(TargetKind::SlowArea);
let zones = if is_hdd { dead_zones(targets).len() } else { 0 };
let penalty = (unreadable * 2).min(60)
+ weak.min(25)
+ (slow / 4).min(15)
+ zones * 5
+ if pending_197 > 0 { 10 } else { 0 }
+ if tail_degraded { 5 } else { 0 };
let score = 100usize.saturating_sub(penalty);
let (verdict, title, advice, recommended): (&str, &str, String, Option<&str>) = if unreadable
+ weak
+ slow
== 0
{
if pending_197 > 0 {
(
"attention",
"Yüzey temiz, ama diskte bekleyen sektör kaydı var",
format!("Tarama hiçbir bozuk okuma bulmadı; SMART {pending_197} bekleyen sektör bildiriyor. Bunlar büyük olasılıkla boş alanda; bir sonraki yazmada disk kendisi düzeltir."),
None,
)
} else {
(
"healthy",
"Yüzey temiz",
"Bozuk, zayıf veya yavaş bölge bulunmadı. Onarım gerekmez.".into(),
None,
)
}
} else if is_hdd && (unreadable > 500 || zones > 10) {
(
"failing",
"Disk ciddi hasarlı",
format!("{unreadable} okunamayan sektör ve {zones} ölü bölge var. Önce verilerinizi başka diske yedekleyin, sonra ölü bölgeleri kapatın ve diski değiştirmeyi planlayın."),
Some("bad_zone_isolate"),
)
} else if !is_hdd {
(
"repairable",
"Yavaş okunan bölgeler bulundu",
format!("{} yavaş bölge var (genelde uzun süredir yazılmamış eski veri). Tazeleme bu verileri yeni hücrelere yazarak okuma hızını geri getirir.", slow + weak + unreadable),
Some("magnetic_refresh"),
)
} else if zones > 0 {
(
"repairable",
"Yoğun hasarlı bölge bulundu",
format!("Bozuk sektörler {zones} bölgede kümelenmiş. Bu alanlardaki dosyalar taşınıp bölge kalıcı olarak kapatılmalı; dağınık sektörler ayrıca onarılabilir."),
Some("bad_zone_isolate"),
)
} else {
(
"repairable",
"Onarılabilir sektörler bulundu",
format!("{unreadable} okunamayan ve {weak} zayıf sektör dağınık halde. Nokta atışı onarım bunları yeniden yazıp diskin yedek sektörlere aktarmasını sağlar."),
Some("magnetic_refresh"),
)
};
json!({
"score": score,
"verdict": verdict,
"title": title,
"advice": advice,
"recommended_mode": recommended,
"unreadable": unreadable,
"weak": weak,
"slow": slow,
"dead_zones": zones,
"engine": "Deterministik yüzey teşhisi · istatistik + kural motoru · çevrimdışı"
})
}
#[cfg(test)]
mod tests {
use super::*;
const MIB: u64 = 1024 * 1024;
fn t(offset: u64, kind: TargetKind) -> Target {
Target {
offset,
len: 4096,
kind,
latency_ms: None,
}
}
#[test]
fn baseline_median_and_adaptive_threshold_follow_the_disk() {
let mut nvme = LatencyBaseline::new();
for _ in 0..500 {
nvme.push(0.6);
}
let ssd = slow_threshold_ms(&nvme, false);
assert!((8.0..=10.0).contains(&ssd), "{ssd}");
let mut hdd = LatencyBaseline::new();
for _ in 0..500 {
hdd.push(9.0);
}
let h = slow_threshold_ms(&hdd, true);
assert!((50.0..=150.0).contains(&h), "{h}");
assert_eq!(slow_threshold_ms(&LatencyBaseline::new(), true), 80.0);
}
#[test]
fn dense_bad_sectors_become_one_dead_zone_with_margin() {
let targets = vec![
t(100 * MIB, TargetKind::Unreadable),
t(110 * MIB, TargetKind::WeakSector),
t(130 * MIB, TargetKind::Unreadable),
t(900 * MIB, TargetKind::Unreadable),
];
let zones = dead_zones(&targets);
assert_eq!(zones.len(), 1);
assert_eq!(zones[0].0, 99 * MIB);
assert_eq!(zones[0].1, 130 * MIB + 4096 + MIB);
}
#[test]
fn plan_maps_to_clusters_merges_and_skips_other_volumes() {
let vol_start = 200 * MIB;
let targets = vec![
t(vol_start + 8192, TargetKind::WeakSector),
t(vol_start + 8192 + 4096, TargetKind::Unreadable),
t(MIB, TargetKind::Unreadable),
];
let (regions, outside) = plan_regions(&targets, vol_start, 100 * MIB, 4096);
assert_eq!(outside, 1);
assert_eq!(regions.len(), 1);
assert_eq!(regions[0].lcn, 2);
assert_eq!(regions[0].clusters, 2);
assert_eq!(regions[0].kind, TargetKind::Unreadable);
assert_eq!(regions[0].phys_offset, vol_start + 8192);
assert_eq!(decide(®ions[0], true, false), Action::Heal);
assert_eq!(decide(®ions[0], true, true), Action::Isolate);
assert_eq!(decide(®ions[0], false, false), Action::Relocate);
}
#[test]
fn diagnosis_recommends_the_right_repair() {
let clean = diagnose(&[], true, 0, false);
assert_eq!(clean["verdict"], "healthy");
assert!(clean["recommended_mode"].is_null());
let scattered = diagnose(
&[
t(0, TargetKind::Unreadable),
t(10_000 * MIB, TargetKind::WeakSector),
],
true,
1,
false,
);
assert_eq!(scattered["recommended_mode"], "magnetic_refresh");
let dense: Vec<_> = (0..4).map(|i| t(i * MIB, TargetKind::Unreadable)).collect();
assert_eq!(
diagnose(&dense, true, 0, false)["recommended_mode"],
"bad_zone_isolate"
);
let stale = vec![Target {
offset: 0,
len: MIB,
kind: TargetKind::SlowArea,
latency_ms: Some(40.0),
}];
assert_eq!(
diagnose(&stale, false, 0, false)["recommended_mode"],
"magnetic_refresh"
);
}
#[test]
fn latency_baseline_serde_roundtrip_preserves_buckets_and_threshold() {
let mut base = LatencyBaseline::new();
for i in 0..300 {
base.push(if i % 5 == 0 { 12.0 } else { 8.5 });
}
let encoded = serde_json::to_value(&base).unwrap();
let decoded: LatencyBaseline = serde_json::from_value(encoded).unwrap();
assert_eq!(decoded, base);
assert_eq!(decoded.count(), 300);
assert_eq!(decoded.median(), base.median());
assert_eq!(
slow_threshold_ms(&decoded, true),
slow_threshold_ms(&base, true)
);
}
}