rustfs-erasure-codec 7.0.1

Rust implementation of Reed-Solomon erasure coding
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
#[cfg(all(feature = "std", feature = "benchmark-metrics"))]
use std::sync::atomic::AtomicUsize;
#[cfg(feature = "std")]
use std::sync::atomic::Ordering;

#[cfg(feature = "std")]
#[derive(Debug, Default)]
pub(crate) struct MetricCounter {
    #[cfg(feature = "benchmark-metrics")]
    value: AtomicUsize,
}

#[cfg(feature = "std")]
impl MetricCounter {
    #[inline]
    fn load(&self, ordering: Ordering) -> usize {
        #[cfg(feature = "benchmark-metrics")]
        {
            self.value.load(ordering)
        }
        #[cfg(not(feature = "benchmark-metrics"))]
        {
            let _ = ordering;
            0
        }
    }

    #[inline]
    fn store(&self, value: usize, ordering: Ordering) {
        #[cfg(feature = "benchmark-metrics")]
        {
            self.value.store(value, ordering);
        }
        #[cfg(not(feature = "benchmark-metrics"))]
        {
            let _ = (value, ordering);
        }
    }

    #[inline]
    pub(crate) fn fetch_add(&self, value: usize, ordering: Ordering) -> usize {
        #[cfg(feature = "benchmark-metrics")]
        {
            self.value.fetch_add(value, ordering)
        }
        #[cfg(not(feature = "benchmark-metrics"))]
        {
            let _ = (value, ordering);
            0
        }
    }
}

#[cfg(feature = "std")]
#[derive(Debug, Default)]
pub(crate) struct ReconstructionCacheMetrics {
    pub(crate) requests: MetricCounter,
    pub(crate) hits: MetricCounter,
    pub(crate) misses: MetricCounter,
    pub(crate) inserts: MetricCounter,
    pub(crate) evictions: MetricCounter,
}

#[cfg(feature = "std")]
#[derive(Debug, Default)]
pub(crate) struct RuntimeProfileMetrics {
    code_some_serial_calls: MetricCounter,
    code_some_parallel_calls: MetricCounter,
    code_some_total_bytes: MetricCounter,
    code_some_total_chunks: MetricCounter,
    code_some_small_output_chunk_parallel_calls: MetricCounter,
    code_some_small_output_chunk_parallel_outputs: MetricCounter,
    code_some_small_output_chunk_parallel_chunks: MetricCounter,
    code_single_serial_calls: MetricCounter,
    code_single_parallel_calls: MetricCounter,
    code_single_total_bytes: MetricCounter,
    code_single_total_chunks: MetricCounter,
    parallel_policy_calls: MetricCounter,
    parallel_policy_parallel: MetricCounter,
    parallel_policy_serial: MetricCounter,
    parallel_policy_total_jobs: MetricCounter,
    parallel_policy_total_chunk_len: MetricCounter,
    reconstruct_calls: MetricCounter,
    reconstruct_data_only_calls: MetricCounter,
    reconstruct_entry_parallel_calls: MetricCounter,
    reconstruct_entry_serial_calls: MetricCounter,
    reconstruct_opt_fallback_serial_calls: MetricCounter,
    reconstruct_total_missing_data: MetricCounter,
    reconstruct_total_missing_parity: MetricCounter,
    reconstruct_all_present_fast_path: MetricCounter,
    reconstruct_data_stage_calls: MetricCounter,
    reconstruct_data_stage_bytes: MetricCounter,
    reconstruct_parity_stage_calls: MetricCounter,
    reconstruct_parity_stage_bytes: MetricCounter,
    reconstruct_data_small_output_specialized_calls: MetricCounter,
}

#[cfg(feature = "std")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ReconstructionCacheStats {
    pub requests: usize,
    pub hits: usize,
    pub misses: usize,
    pub inserts: usize,
    pub evictions: usize,
}

#[cfg(feature = "std")]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ReconstructionCacheAnalysis {
    pub hit_rate: f64,
    pub reuse_ratio: f64,
    pub miss_cost_per_request: f64,
}

#[cfg(feature = "std")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RuntimeProfileStats {
    pub code_some_serial_calls: usize,
    pub code_some_parallel_calls: usize,
    pub code_some_total_bytes: usize,
    pub code_some_total_chunks: usize,
    pub code_some_small_output_chunk_parallel_calls: usize,
    pub code_some_small_output_chunk_parallel_outputs: usize,
    pub code_some_small_output_chunk_parallel_chunks: usize,
    pub code_single_serial_calls: usize,
    pub code_single_parallel_calls: usize,
    pub code_single_total_bytes: usize,
    pub code_single_total_chunks: usize,
    pub parallel_policy_calls: usize,
    pub parallel_policy_parallel: usize,
    pub parallel_policy_serial: usize,
    pub parallel_policy_total_jobs: usize,
    pub parallel_policy_total_chunk_len: usize,
    pub reconstruct_calls: usize,
    pub reconstruct_data_only_calls: usize,
    pub reconstruct_entry_parallel_calls: usize,
    pub reconstruct_entry_serial_calls: usize,
    pub reconstruct_opt_fallback_serial_calls: usize,
    pub reconstruct_total_missing_data: usize,
    pub reconstruct_total_missing_parity: usize,
    pub reconstruct_all_present_fast_path: usize,
    pub reconstruct_data_stage_calls: usize,
    pub reconstruct_data_stage_bytes: usize,
    pub reconstruct_parity_stage_calls: usize,
    pub reconstruct_parity_stage_bytes: usize,
    pub reconstruct_data_small_output_specialized_calls: usize,
}

#[cfg(feature = "std")]
impl ReconstructionCacheMetrics {
    pub(crate) fn snapshot(&self) -> ReconstructionCacheStats {
        ReconstructionCacheStats {
            requests: self.requests.load(Ordering::Relaxed),
            hits: self.hits.load(Ordering::Relaxed),
            misses: self.misses.load(Ordering::Relaxed),
            inserts: self.inserts.load(Ordering::Relaxed),
            evictions: self.evictions.load(Ordering::Relaxed),
        }
    }
}

#[cfg(feature = "std")]
impl ReconstructionCacheStats {
    /// Returns the cache hit rate (hits / requests).
    #[inline]
    pub fn hit_rate(&self) -> f64 {
        if self.requests == 0 {
            0.0
        } else {
            self.hits as f64 / self.requests as f64
        }
    }

    /// Returns the cache reuse ratio (hits / inserts).
    #[inline]
    pub fn reuse_ratio(&self) -> f64 {
        if self.inserts == 0 {
            0.0
        } else {
            self.hits as f64 / self.inserts as f64
        }
    }

    /// Returns the miss cost per request (misses / requests).
    #[inline]
    pub fn miss_cost_per_request(&self) -> f64 {
        if self.requests == 0 {
            0.0
        } else {
            self.misses as f64 / self.requests as f64
        }
    }

    /// Returns a combined analysis of all cache metrics.
    #[inline]
    pub fn analysis(&self) -> ReconstructionCacheAnalysis {
        ReconstructionCacheAnalysis {
            hit_rate: self.hit_rate(),
            reuse_ratio: self.reuse_ratio(),
            miss_cost_per_request: self.miss_cost_per_request(),
        }
    }
}

#[cfg(feature = "std")]
impl RuntimeProfileMetrics {
    pub(crate) fn snapshot(&self) -> RuntimeProfileStats {
        RuntimeProfileStats {
            code_some_serial_calls: self.code_some_serial_calls.load(Ordering::Relaxed),
            code_some_parallel_calls: self.code_some_parallel_calls.load(Ordering::Relaxed),
            code_some_total_bytes: self.code_some_total_bytes.load(Ordering::Relaxed),
            code_some_total_chunks: self.code_some_total_chunks.load(Ordering::Relaxed),
            code_some_small_output_chunk_parallel_calls: self
                .code_some_small_output_chunk_parallel_calls
                .load(Ordering::Relaxed),
            code_some_small_output_chunk_parallel_outputs: self
                .code_some_small_output_chunk_parallel_outputs
                .load(Ordering::Relaxed),
            code_some_small_output_chunk_parallel_chunks: self
                .code_some_small_output_chunk_parallel_chunks
                .load(Ordering::Relaxed),
            code_single_serial_calls: self.code_single_serial_calls.load(Ordering::Relaxed),
            code_single_parallel_calls: self.code_single_parallel_calls.load(Ordering::Relaxed),
            code_single_total_bytes: self.code_single_total_bytes.load(Ordering::Relaxed),
            code_single_total_chunks: self.code_single_total_chunks.load(Ordering::Relaxed),
            parallel_policy_calls: self.parallel_policy_calls.load(Ordering::Relaxed),
            parallel_policy_parallel: self.parallel_policy_parallel.load(Ordering::Relaxed),
            parallel_policy_serial: self.parallel_policy_serial.load(Ordering::Relaxed),
            parallel_policy_total_jobs: self.parallel_policy_total_jobs.load(Ordering::Relaxed),
            parallel_policy_total_chunk_len: self
                .parallel_policy_total_chunk_len
                .load(Ordering::Relaxed),
            reconstruct_calls: self.reconstruct_calls.load(Ordering::Relaxed),
            reconstruct_data_only_calls: self.reconstruct_data_only_calls.load(Ordering::Relaxed),
            reconstruct_entry_parallel_calls: self
                .reconstruct_entry_parallel_calls
                .load(Ordering::Relaxed),
            reconstruct_entry_serial_calls: self
                .reconstruct_entry_serial_calls
                .load(Ordering::Relaxed),
            reconstruct_opt_fallback_serial_calls: self
                .reconstruct_opt_fallback_serial_calls
                .load(Ordering::Relaxed),
            reconstruct_total_missing_data: self
                .reconstruct_total_missing_data
                .load(Ordering::Relaxed),
            reconstruct_total_missing_parity: self
                .reconstruct_total_missing_parity
                .load(Ordering::Relaxed),
            reconstruct_all_present_fast_path: self
                .reconstruct_all_present_fast_path
                .load(Ordering::Relaxed),
            reconstruct_data_stage_calls: self.reconstruct_data_stage_calls.load(Ordering::Relaxed),
            reconstruct_data_stage_bytes: self.reconstruct_data_stage_bytes.load(Ordering::Relaxed),
            reconstruct_parity_stage_calls: self
                .reconstruct_parity_stage_calls
                .load(Ordering::Relaxed),
            reconstruct_parity_stage_bytes: self
                .reconstruct_parity_stage_bytes
                .load(Ordering::Relaxed),
            reconstruct_data_small_output_specialized_calls: self
                .reconstruct_data_small_output_specialized_calls
                .load(Ordering::Relaxed),
        }
    }

    pub(crate) fn reset(&self) {
        self.code_some_serial_calls.store(0, Ordering::Relaxed);
        self.code_some_parallel_calls.store(0, Ordering::Relaxed);
        self.code_some_total_bytes.store(0, Ordering::Relaxed);
        self.code_some_total_chunks.store(0, Ordering::Relaxed);
        self.code_some_small_output_chunk_parallel_calls
            .store(0, Ordering::Relaxed);
        self.code_some_small_output_chunk_parallel_outputs
            .store(0, Ordering::Relaxed);
        self.code_some_small_output_chunk_parallel_chunks
            .store(0, Ordering::Relaxed);
        self.code_single_serial_calls.store(0, Ordering::Relaxed);
        self.code_single_parallel_calls.store(0, Ordering::Relaxed);
        self.code_single_total_bytes.store(0, Ordering::Relaxed);
        self.code_single_total_chunks.store(0, Ordering::Relaxed);
        self.parallel_policy_calls.store(0, Ordering::Relaxed);
        self.parallel_policy_parallel.store(0, Ordering::Relaxed);
        self.parallel_policy_serial.store(0, Ordering::Relaxed);
        self.parallel_policy_total_jobs.store(0, Ordering::Relaxed);
        self.parallel_policy_total_chunk_len
            .store(0, Ordering::Relaxed);
        self.reconstruct_calls.store(0, Ordering::Relaxed);
        self.reconstruct_data_only_calls.store(0, Ordering::Relaxed);
        self.reconstruct_entry_parallel_calls
            .store(0, Ordering::Relaxed);
        self.reconstruct_entry_serial_calls
            .store(0, Ordering::Relaxed);
        self.reconstruct_opt_fallback_serial_calls
            .store(0, Ordering::Relaxed);
        self.reconstruct_total_missing_data
            .store(0, Ordering::Relaxed);
        self.reconstruct_total_missing_parity
            .store(0, Ordering::Relaxed);
        self.reconstruct_all_present_fast_path
            .store(0, Ordering::Relaxed);
        self.reconstruct_data_stage_calls
            .store(0, Ordering::Relaxed);
        self.reconstruct_data_stage_bytes
            .store(0, Ordering::Relaxed);
        self.reconstruct_parity_stage_calls
            .store(0, Ordering::Relaxed);
        self.reconstruct_parity_stage_bytes
            .store(0, Ordering::Relaxed);
        self.reconstruct_data_small_output_specialized_calls
            .store(0, Ordering::Relaxed);
    }

    pub(crate) fn record_code_some(
        &self,
        parallel: bool,
        shard_len: usize,
        inputs: usize,
        outputs: usize,
        chunk_len: usize,
    ) {
        if parallel {
            self.code_some_parallel_calls
                .fetch_add(1, Ordering::Relaxed);
        } else {
            self.code_some_serial_calls.fetch_add(1, Ordering::Relaxed);
        }
        self.code_some_total_bytes.fetch_add(
            shard_len.saturating_mul(inputs).saturating_mul(outputs),
            Ordering::Relaxed,
        );
        let chunks = if chunk_len == 0 {
            0
        } else {
            shard_len.div_ceil(chunk_len)
        };
        self.code_some_total_chunks
            .fetch_add(chunks, Ordering::Relaxed);
    }

    pub(crate) fn record_code_some_small_output_chunk_parallel(
        &self,
        outputs: usize,
        chunks: usize,
    ) {
        self.code_some_small_output_chunk_parallel_calls
            .fetch_add(1, Ordering::Relaxed);
        self.code_some_small_output_chunk_parallel_outputs
            .fetch_add(outputs, Ordering::Relaxed);
        self.code_some_small_output_chunk_parallel_chunks
            .fetch_add(chunks, Ordering::Relaxed);
    }

    pub(crate) fn record_code_single(
        &self,
        parallel: bool,
        shard_len: usize,
        outputs: usize,
        chunk_len: usize,
    ) {
        if parallel {
            self.code_single_parallel_calls
                .fetch_add(1, Ordering::Relaxed);
        } else {
            self.code_single_serial_calls
                .fetch_add(1, Ordering::Relaxed);
        }
        self.code_single_total_bytes
            .fetch_add(shard_len.saturating_mul(outputs), Ordering::Relaxed);
        let chunks = if chunk_len == 0 {
            0
        } else {
            shard_len.div_ceil(chunk_len)
        };
        self.code_single_total_chunks
            .fetch_add(chunks, Ordering::Relaxed);
    }

    pub(crate) fn record_parallel_policy(&self, decision: crate::ParallelDecision) {
        self.parallel_policy_calls.fetch_add(1, Ordering::Relaxed);
        if decision.use_parallel {
            self.parallel_policy_parallel
                .fetch_add(1, Ordering::Relaxed);
        } else {
            self.parallel_policy_serial.fetch_add(1, Ordering::Relaxed);
        }
        self.parallel_policy_total_jobs
            .fetch_add(decision.jobs, Ordering::Relaxed);
        self.parallel_policy_total_chunk_len
            .fetch_add(decision.chunk_len, Ordering::Relaxed);
    }

    pub(crate) fn record_reconstruct(
        &self,
        data_only: bool,
        missing_data_count: usize,
        missing_parity_count: usize,
        all_present_fast_path: bool,
    ) {
        self.reconstruct_calls.fetch_add(1, Ordering::Relaxed);
        if data_only {
            self.reconstruct_data_only_calls
                .fetch_add(1, Ordering::Relaxed);
        }
        if all_present_fast_path {
            self.reconstruct_all_present_fast_path
                .fetch_add(1, Ordering::Relaxed);
        }
        self.reconstruct_total_missing_data
            .fetch_add(missing_data_count, Ordering::Relaxed);
        self.reconstruct_total_missing_parity
            .fetch_add(missing_parity_count, Ordering::Relaxed);
    }

    pub(crate) fn record_reconstruct_entry(&self, parallel: bool) {
        if parallel {
            self.reconstruct_entry_parallel_calls
                .fetch_add(1, Ordering::Relaxed);
        } else {
            self.reconstruct_entry_serial_calls
                .fetch_add(1, Ordering::Relaxed);
        }
    }

    pub(crate) fn record_reconstruct_opt_fallback_serial(&self) {
        self.reconstruct_opt_fallback_serial_calls
            .fetch_add(1, Ordering::Relaxed);
    }

    pub(crate) fn record_reconstruct_data_stage(&self, shard_len: usize, output_count: usize) {
        self.reconstruct_data_stage_calls
            .fetch_add(1, Ordering::Relaxed);
        self.reconstruct_data_stage_bytes
            .fetch_add(shard_len.saturating_mul(output_count), Ordering::Relaxed);
    }

    pub(crate) fn record_reconstruct_parity_stage(&self, shard_len: usize, output_count: usize) {
        self.reconstruct_parity_stage_calls
            .fetch_add(1, Ordering::Relaxed);
        self.reconstruct_parity_stage_bytes
            .fetch_add(shard_len.saturating_mul(output_count), Ordering::Relaxed);
    }

    pub(crate) fn record_reconstruct_data_small_output_specialized(&self) {
        self.reconstruct_data_small_output_specialized_calls
            .fetch_add(1, Ordering::Relaxed);
    }
}