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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
extern crate libc;
use self::libc::{int64_t, int32_t, c_double, c_int, c_void, c_char, size_t};
use std::str;
use std::ffi::CStr;
use std::marker::PhantomData;
use std::fmt;
use std::error::Error;
use std::mem;
use std::ptr;
use std::result;
pub use super::Result;
#[cfg(test)]
pub mod test;
#[repr(C)]
#[derive(Debug)]
struct hdr_histogram {
lowest_trackable_value: int64_t,
highest_trackable_value: int64_t,
unit_magnitude: int32_t,
significant_figures: int32_t,
sub_bucket_half_count_magnitude: int32_t,
sub_bucket_half_count: int32_t,
sub_bucket_mask: int64_t,
sub_bucket_count: int32_t,
bucket_count: int32_t,
min_value: int64_t,
max_value: int64_t,
normalizing_index_offset: int32_t,
conversion_ratio: c_double,
counts_len: int32_t,
total_count: int64_t,
counts: [int64_t; 0],
}
#[repr(C)]
#[derive(Default,Debug,Copy,Clone)]
struct hdr_histogram_bucket_config {
lowest_trackable_value: int64_t,
highest_trackable_value: int64_t,
unit_magnitude: int64_t,
significant_figures: int64_t,
sub_bucket_half_count_magnitude: int32_t,
sub_bucket_half_count: int32_t,
sub_bucket_mask: int64_t,
sub_bucket_count: int32_t,
bucket_count: int32_t,
counts_len: int32_t,
}
#[repr(C)]
struct hdr_iter_percentiles {
seen_last_value: bool,
ticks_per_half_histance: int32_t,
percentile_to_iterate_to: c_double,
percentile: c_double,
}
#[repr(C)]
struct hdr_iter_recorded {
count_added_in_this_iteration_step: int64_t,
}
#[repr(C)]
struct hdr_iter_linear {
value_units_per_bucket: int64_t,
count_added_in_this_iteration_step: int64_t,
next_value_reporting_level: int64_t,
next_value_reporting_level_lowest_equivalent: int64_t,
}
#[repr(C)]
struct hdr_iter_log {
log_base: c_double,
count_added_in_this_iteration_step: int64_t,
next_value_reporting_level: int64_t,
next_value_reporting_level_lowest_equivalent: int64_t,
}
#[repr(C)]
#[derive(Debug)]
struct hdr_iter {
h: *const hdr_histogram,
counts_index: int32_t,
count: int64_t,
cumulative_count: int64_t,
value: int64_t,
highest_equivalent_value: int64_t,
lowest_equivalent_value: int64_t,
median_equivalent_value: int64_t,
value_iterated_from: int64_t,
value_iterated_to: int64_t,
union: [int64_t; 4],
_next_fp: *const c_void,
}
impl Default for hdr_iter {
fn default() -> Self { unsafe { mem::zeroed() } }
}
#[allow(dead_code)]
extern {
fn hdr_init(lowest_trackable_value: int64_t, highest_trackable_value: int64_t,
significant_figures: c_int, res: *mut *mut hdr_histogram) -> c_int;
fn hdr_reset(h: *mut hdr_histogram);
fn hdr_get_memory_size(h: *const hdr_histogram) -> size_t;
fn hdr_record_value(h: *mut hdr_histogram, value: int64_t) -> bool;
fn hdr_record_values(h: *mut hdr_histogram, value: int64_t, count: int64_t) -> bool;
fn hdr_record_corrected_value(h: *mut hdr_histogram, value: int64_t, expected_interval: int64_t) -> bool;
fn hdr_record_corrected_values(h: *mut hdr_histogram, value: int64_t, count: int64_t,
expected_interval: int64_t) -> bool;
fn hdr_add(h: *mut hdr_histogram, from: *const hdr_histogram) -> int64_t;
fn hdr_add_while_correcting_for_coordinated_omission(h: *mut hdr_histogram, from: *const hdr_histogram,
expected_interval: int64_t) -> int64_t;
fn hdr_min(h: *const hdr_histogram) -> int64_t;
fn hdr_max(h: *const hdr_histogram) -> int64_t;
fn hdr_value_at_percentile(h: *const hdr_histogram, percentile: c_double) -> int64_t;
fn hdr_stddev(h: *const hdr_histogram) -> c_double;
fn hdr_mean(h: *const hdr_histogram) -> c_double;
fn hdr_values_are_equivalent(h: *const hdr_histogram, a: int64_t, b: int64_t) -> bool;
fn hdr_lowest_equivalent_value(h: *const hdr_histogram, value: int64_t) -> int64_t;
fn hdr_count_at_value(h: *const hdr_histogram, value: int64_t) -> int64_t;
fn hdr_count_at_index(h: *const hdr_histogram, index: int32_t) -> int64_t;
fn hdr_value_at_index(h: *const hdr_histogram, index: int32_t) -> int64_t;
fn hdr_calculate_bucket_config(lowest_trackable_value: int64_t, highest_trackable_value: int64_t,
significant_figures: c_int, cfg: *mut hdr_histogram_bucket_config) -> c_int;
fn hdr_init_preallocated(h: *mut hdr_histogram, cfg: *const hdr_histogram_bucket_config);
fn hdr_shift_values_left(h: *mut hdr_histogram, binary_orders_of_magnitude: int32_t) -> bool;
fn hdr_shift_values_right(h: *mut hdr_histogram, shift: int32_t) -> bool;
fn hdr_size_of_equivalent_value_range(h: *const hdr_histogram, value: int64_t) -> int64_t;
fn hdr_next_non_equivalent_value(h: *const hdr_histogram, value: int64_t) -> int64_t;
fn hdr_median_equivalent_value(h: *const hdr_histogram, value: int64_t) -> int64_t;
fn hdr_iter_init(iter: *mut hdr_iter, h: *const hdr_histogram);
fn hdr_iter_percentile_init(iter: *mut hdr_iter, h: *const hdr_histogram, ticks_per_half_distance: int32_t);
fn hdr_iter_recorded_init(iter: *mut hdr_iter, h: *const hdr_histogram);
fn hdr_iter_linear_init(iter: *mut hdr_iter, h: *const hdr_histogram, value_units_per_bucket: int64_t);
fn hdr_iter_log_init(iter: *mut hdr_iter, h: *const hdr_histogram, value_units_per_bucket: int64_t, log_base: c_double);
fn hdr_iter_next(iter: *mut hdr_iter) -> bool;
fn hdr_log_encode(histogram: *const hdr_histogram, encoded_histogram: *mut *mut c_char) -> c_int;
fn hdr_log_decode(histogram: *mut *mut hdr_histogram, base64_histogram: *const c_char, base64_len: size_t) -> c_int;
fn hdr_strerror(errnum: c_int) -> *const c_char;
}
#[derive(Copy,Clone,Debug)]
pub struct HistogramErr(&'static str);
impl fmt::Display for HistogramErr {
fn fmt(&self, fmt: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
let &HistogramErr(msg) = self;
msg.fmt(fmt)
}
}
impl Error for HistogramErr {
fn description(&self) -> &str { let &HistogramErr(msg) = self; msg }
}
pub struct Histogram {
histo: *mut hdr_histogram,
owned: bool,
}
unsafe impl Send for Histogram {}
impl Histogram {
pub fn init(lowest_trackable_value: u64, highest_trackable_value: u64, significant_figures: u32) -> Result<Histogram> {
let mut histo : *mut hdr_histogram = ptr::null_mut();
let r = unsafe {
hdr_init(lowest_trackable_value as int64_t, highest_trackable_value as int64_t,
significant_figures as int32_t, &mut histo)
};
if r != 0 || histo.is_null() {
Err(HistogramErr("Histogram init failed"))
} else {
Ok(Histogram { histo: histo, owned: true })
}
}
pub fn reset(&mut self) { unsafe { hdr_reset(self.histo) } }
#[inline]
pub fn record_value(&mut self, value: u64) -> bool {
unsafe { hdr_record_value(self.histo, value as int64_t) }
}
#[inline]
pub fn record_values(&mut self, value: u64, count: u64) -> bool {
unsafe { hdr_record_values(self.histo, value as int64_t, count as int64_t) }
}
#[inline]
pub fn record_corrected_value(&mut self, value: u64, expected_interval: u64) -> bool {
unsafe { hdr_record_corrected_value(self.histo, value as int64_t, expected_interval as int64_t) }
}
#[inline]
pub fn record_corrected_values(&mut self, value: u64, count: u64, expected_interval: u64) -> bool {
unsafe { hdr_record_corrected_values(self.histo, value as int64_t, count as int64_t, expected_interval as int64_t) }
}
pub fn add(&mut self, other: &Histogram) -> u64 {
unsafe { hdr_add(self.histo, other.histo) as u64 }
}
pub fn add_while_correcting_for_coordinated_omission(&mut self, other: &Histogram, expected_interval: u64) -> u64 {
unsafe {
hdr_add_while_correcting_for_coordinated_omission(self.histo, other.histo, expected_interval as int64_t) as u64
}
}
pub fn total_count(&self) -> u64 { unsafe { (*self.histo).total_count as u64 } }
pub fn min(&self) -> u64 {
unsafe { hdr_min(self.histo) as u64 }
}
pub fn max(&self) -> u64 {
unsafe { hdr_max(self.histo) as u64 }
}
pub fn value_at_percentile(&self, percentile: f64) -> u64 {
unsafe { hdr_value_at_percentile(self.histo, percentile) as u64 }
}
pub fn stddev(&self) -> f64 {
unsafe { hdr_stddev(self.histo) }
}
pub fn mean(&self) -> f64 {
unsafe { hdr_mean(self.histo) }
}
pub fn values_are_equivalent(&self, a: u64, b: u64) -> bool {
unsafe { hdr_values_are_equivalent(self.histo, a as int64_t, b as int64_t) }
}
pub fn lowest_equivalent_value(&self, value: u64) -> u64 {
unsafe { hdr_lowest_equivalent_value(self.histo, value as int64_t) as u64 }
}
pub fn count_at_value(&self, value: u64) -> u64 {
unsafe { hdr_count_at_value(self.histo, value as int64_t) as u64 }
}
#[allow(dead_code)]
fn count_at_index(&self, index: u32) -> u64 {
unsafe { hdr_count_at_index(self.histo, index as int32_t) as u64 }
}
#[allow(dead_code)]
fn value_at_index(&self, index: u32) -> u64 {
unsafe { hdr_value_at_index(self.histo, index as int32_t) as u64 }
}
pub fn linear_iter<'a>(&'a self, value_units_per_bucket: u64) -> LinearIter<'a> {
let mut ret = LinearIter { iter: Default::default(), histo: PhantomData };
unsafe { hdr_iter_linear_init(&mut ret.iter, self.histo, value_units_per_bucket as int64_t) };
ret
}
pub fn log_iter<'a>(&'a self, value_units_per_bucket: u64, log_base: f64) -> LogIter<'a> {
let mut ret = LogIter { iter: Default::default(), histo: PhantomData };
unsafe { hdr_iter_log_init(&mut ret.iter, self.histo, value_units_per_bucket as int64_t, log_base) };
ret
}
pub fn recorded_iter<'a>(&'a self) -> RecordedIter<'a> {
let mut ret = RecordedIter { iter: Default::default(), histo: PhantomData };
unsafe { hdr_iter_recorded_init(&mut ret.iter, self.histo) };
ret
}
pub fn percentile_iter<'a>(&'a self, ticks_per_half_distance: u32) -> PercentileIter<'a> {
let mut ret = PercentileIter { iter: Default::default(), histo: PhantomData };
unsafe { hdr_iter_percentile_init(&mut ret.iter, self.histo, ticks_per_half_distance as int32_t) };
ret
}
pub fn encode(&self) -> Result<String> {
let mut p : *mut c_char = ptr::null_mut();
let r = unsafe { hdr_log_encode(self.histo, &mut p) };
if r != 0 || p.is_null() {
Err(HistogramErr(str::from_utf8(unsafe { CStr::from_ptr(hdr_strerror(r)) }.to_bytes()).unwrap()))
} else {
let sz = unsafe { libc::strlen(p) as usize };
let s = unsafe {
let mut v = Vec::with_capacity(sz);
v.set_len(sz);
ptr::copy(p as *const u8, v.as_mut_ptr(), sz);
libc::free(p as *mut c_void);
String::from_utf8_unchecked(v)
};
Ok(s)
}
}
pub fn decode(base64: &String) -> Result<Histogram> {
let bytes = base64.as_bytes();
let mut h : *mut hdr_histogram = ptr::null_mut();
let r = unsafe { hdr_log_decode(&mut h, bytes.as_ptr() as *const c_char, bytes.len() as size_t) };
if r != 0 || h.is_null() {
Err(HistogramErr(str::from_utf8(unsafe { CStr::from_ptr(hdr_strerror(r)) }.to_bytes()).unwrap()))
} else {
Ok(Histogram { histo: h, owned: true })
}
}
fn get_memory_size(&self) -> usize { unsafe { hdr_get_memory_size(self.histo) as usize } }
#[cfg(test)]
fn get_counts_len(&self) -> u32 { unsafe { (*self.histo).counts_len as u32 } }
}
impl Drop for Histogram {
fn drop(&mut self) {
if self.owned && !self.histo.is_null() {
unsafe { libc::free(self.histo as *mut c_void) }
}
}
}
impl Clone for Histogram {
fn clone(&self) -> Self {
let sz = self.get_memory_size();
let p = unsafe { libc::malloc(sz as size_t) as *mut hdr_histogram };
if p.is_null() { panic!("allocation of hdr_histogram failed"); }
unsafe { ptr::copy(self.histo as *const u8, p as *mut u8, sz) };
Histogram { histo: p, owned: true }
}
}
#[derive(PartialEq,Eq,PartialOrd,Ord,Clone,Copy,Debug)]
pub struct CountIterItem {
pub count_added_in_this_iteration_step: u64,
pub count: u64,
pub value: u64,
pub highest_equivalent_value: u64,
pub median_equivalent_value: u64,
pub lowest_equivalent_value: u64,
value_iterated_from: u64,
value_iterated_to: u64,
}
#[derive(PartialEq,PartialOrd,Clone,Copy,Debug)]
pub struct PercentileIterItem {
pub percentile: f64,
pub count: u64,
pub value: u64,
pub highest_equivalent_value: u64,
pub median_equivalent_value: u64,
pub lowest_equivalent_value: u64,
value_iterated_from: u64,
value_iterated_to: u64,
}
pub struct LinearIter<'a> {
iter: hdr_iter,
histo: PhantomData<&'a Histogram>,
}
impl<'a> Iterator for LinearIter<'a> {
type Item = CountIterItem;
fn next(&mut self) -> Option<CountIterItem> {
if unsafe { hdr_iter_next(&mut self.iter) } {
let lin : &hdr_iter_linear = unsafe { mem::transmute(&self.iter.union) };
Some(CountIterItem { count_added_in_this_iteration_step: lin.count_added_in_this_iteration_step as u64,
count: self.iter.count as u64,
value: self.iter.value as u64,
highest_equivalent_value: self.iter.highest_equivalent_value as u64,
lowest_equivalent_value: self.iter.lowest_equivalent_value as u64,
median_equivalent_value: self.iter.median_equivalent_value as u64,
value_iterated_from: self.iter.value_iterated_from as u64,
value_iterated_to: self.iter.value_iterated_to as u64 })
} else {
None
}
}
}
pub struct LogIter<'a> {
iter: hdr_iter,
histo: PhantomData<&'a Histogram>,
}
impl<'a> Iterator for LogIter<'a> {
type Item = CountIterItem;
fn next(&mut self) -> Option<CountIterItem> {
if unsafe { hdr_iter_next(&mut self.iter) } {
let log : &hdr_iter_log = unsafe { mem::transmute(&self.iter.union) };
Some(CountIterItem { count_added_in_this_iteration_step: log.count_added_in_this_iteration_step as u64,
count: self.iter.count as u64,
value: self.iter.value as u64,
highest_equivalent_value: self.iter.highest_equivalent_value as u64,
lowest_equivalent_value: self.iter.lowest_equivalent_value as u64,
median_equivalent_value: self.iter.median_equivalent_value as u64,
value_iterated_from: self.iter.value_iterated_from as u64,
value_iterated_to: self.iter.value_iterated_to as u64 })
} else {
None
}
}
}
pub struct RecordedIter<'a> {
iter: hdr_iter,
histo: PhantomData<&'a Histogram>,
}
impl<'a> Iterator for RecordedIter<'a> {
type Item = CountIterItem;
fn next(&mut self) -> Option<CountIterItem> {
if unsafe { hdr_iter_next(&mut self.iter) } {
let rec : &hdr_iter_recorded = unsafe { mem::transmute(&self.iter.union) };
Some(CountIterItem { count_added_in_this_iteration_step: rec.count_added_in_this_iteration_step as u64,
count: self.iter.count as u64,
value: self.iter.value as u64,
highest_equivalent_value: self.iter.highest_equivalent_value as u64,
lowest_equivalent_value: self.iter.lowest_equivalent_value as u64,
median_equivalent_value: self.iter.median_equivalent_value as u64,
value_iterated_from: self.iter.value_iterated_from as u64,
value_iterated_to: self.iter.value_iterated_to as u64 })
} else {
None
}
}
}
pub struct PercentileIter<'a> {
iter: hdr_iter,
histo: PhantomData<&'a Histogram>,
}
impl<'a> Iterator for PercentileIter<'a> {
type Item = PercentileIterItem;
fn next(&mut self) -> Option<PercentileIterItem> {
if unsafe { hdr_iter_next(&mut self.iter) } {
let perc : &hdr_iter_percentiles = unsafe { mem::transmute(&self.iter.union) };
Some(PercentileIterItem { percentile: perc.percentile,
count: self.iter.count as u64,
value: self.iter.value as u64,
highest_equivalent_value: self.iter.highest_equivalent_value as u64,
lowest_equivalent_value: self.iter.lowest_equivalent_value as u64,
median_equivalent_value: self.iter.median_equivalent_value as u64,
value_iterated_from: self.iter.value_iterated_from as u64,
value_iterated_to: self.iter.value_iterated_to as u64 })
} else {
None
}
}
}