| 1 |
use crate::util::metric_atomics::MetricAtomicU64; |
| 2 |
use std::sync::atomic::Ordering::Relaxed; |
| 3 |
|
| 4 |
use std::cmp; |
| 5 |
use std::ops::Range; |
| 6 |
|
| 7 |
#[derive(Debug)] |
| 8 |
pub(crate) struct Histogram { |
| 9 |
/// The histogram buckets |
| 10 |
buckets: Box<[MetricAtomicU64]>, |
| 11 |
|
| 12 |
/// Bucket scale, linear or log |
| 13 |
scale: HistogramScale, |
| 14 |
|
| 15 |
/// Minimum resolution |
| 16 |
resolution: u64, |
| 17 |
} |
| 18 |
|
| 19 |
#[derive(Debug, Clone)] |
| 20 |
pub(crate) struct HistogramBuilder { |
| 21 |
/// Histogram scale |
| 22 |
pub(crate) scale: HistogramScale, |
| 23 |
|
| 24 |
/// Must be a power of 2 |
| 25 |
pub(crate) resolution: u64, |
| 26 |
|
| 27 |
/// Number of buckets |
| 28 |
pub(crate) num_buckets: usize, |
| 29 |
} |
| 30 |
|
| 31 |
#[derive(Debug)] |
| 32 |
pub(crate) struct HistogramBatch { |
| 33 |
buckets: Box<[u64]>, |
| 34 |
scale: HistogramScale, |
| 35 |
resolution: u64, |
| 36 |
} |
| 37 |
|
| 38 |
cfg_unstable! { |
| 39 |
/// Whether the histogram used to aggregate a metric uses a linear or |
| 40 |
/// logarithmic scale. |
| 41 |
#[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 42 |
#[non_exhaustive] |
| 43 |
pub enum HistogramScale { |
| 44 |
/// Linear bucket scale |
| 45 |
Linear, |
| 46 |
|
| 47 |
/// Logarithmic bucket scale |
| 48 |
Log, |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
impl Histogram { |
| 53 |
pub(crate) fn num_buckets(&self) -> usize { |
| 54 |
self.buckets.len() |
| 55 |
} |
| 56 |
|
| 57 |
cfg_64bit_metrics! { |
| 58 |
pub(crate) fn get(&self, bucket: usize) -> u64 { |
| 59 |
self.buckets[bucket].load(Relaxed) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
pub(crate) fn bucket_range(&self, bucket: usize) -> Range<u64> { |
| 64 |
match self.scale { |
| 65 |
HistogramScale::Log => Range { |
| 66 |
start: if bucket == 0 { |
| 67 |
0 |
| 68 |
} else { |
| 69 |
self.resolution << (bucket - 1) |
| 70 |
}, |
| 71 |
end: if bucket == self.buckets.len() - 1 { |
| 72 |
u64::MAX |
| 73 |
} else { |
| 74 |
self.resolution << bucket |
| 75 |
}, |
| 76 |
}, |
| 77 |
HistogramScale::Linear => Range { |
| 78 |
start: self.resolution * bucket as u64, |
| 79 |
end: if bucket == self.buckets.len() - 1 { |
| 80 |
u64::MAX |
| 81 |
} else { |
| 82 |
self.resolution * (bucket as u64 + 1) |
| 83 |
}, |
| 84 |
}, |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
impl HistogramBatch { |
| 90 |
pub(crate) fn from_histogram(histogram: &Histogram) -> HistogramBatch { |
| 91 |
let buckets = vec![0; histogram.buckets.len()].into_boxed_slice(); |
| 92 |
|
| 93 |
HistogramBatch { |
| 94 |
buckets, |
| 95 |
scale: histogram.scale, |
| 96 |
resolution: histogram.resolution, |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
pub(crate) fn measure(&mut self, value: u64, count: u64) { |
| 101 |
self.buckets[self.value_to_bucket(value)] += count; |
| 102 |
} |
| 103 |
|
| 104 |
pub(crate) fn submit(&self, histogram: &Histogram) { |
| 105 |
debug_assert_eq!(self.scale, histogram.scale); |
| 106 |
debug_assert_eq!(self.resolution, histogram.resolution); |
| 107 |
debug_assert_eq!(self.buckets.len(), histogram.buckets.len()); |
| 108 |
|
| 109 |
for i in 0..self.buckets.len() { |
| 110 |
histogram.buckets[i].store(self.buckets[i], Relaxed); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
fn value_to_bucket(&self, value: u64) -> usize { |
| 115 |
match self.scale { |
| 116 |
HistogramScale::Linear => { |
| 117 |
let max = self.buckets.len() - 1; |
| 118 |
cmp::min(value / self.resolution, max as u64) as usize |
| 119 |
} |
| 120 |
HistogramScale::Log => { |
| 121 |
let max = self.buckets.len() - 1; |
| 122 |
|
| 123 |
if value < self.resolution { |
| 124 |
0 |
| 125 |
} else { |
| 126 |
let significant_digits = 64 - value.leading_zeros(); |
| 127 |
let bucket_digits = 64 - (self.resolution - 1).leading_zeros(); |
| 128 |
cmp::min(significant_digits as usize - bucket_digits as usize, max) |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
impl HistogramBuilder { |
| 136 |
pub(crate) fn new() -> HistogramBuilder { |
| 137 |
HistogramBuilder { |
| 138 |
scale: HistogramScale::Linear, |
| 139 |
// Resolution is in nanoseconds. |
| 140 |
resolution: 100_000, |
| 141 |
num_buckets: 10, |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
pub(crate) fn build(&self) -> Histogram { |
| 146 |
let mut resolution = self.resolution; |
| 147 |
|
| 148 |
assert!(resolution > 0); |
| 149 |
|
| 150 |
if matches!(self.scale, HistogramScale::Log) { |
| 151 |
resolution = resolution.next_power_of_two(); |
| 152 |
} |
| 153 |
|
| 154 |
Histogram { |
| 155 |
buckets: (0..self.num_buckets) |
| 156 |
.map(|_| MetricAtomicU64::new(0)) |
| 157 |
.collect::<Vec<_>>() |
| 158 |
.into_boxed_slice(), |
| 159 |
resolution, |
| 160 |
scale: self.scale, |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
impl Default for HistogramBuilder { |
| 166 |
fn default() -> HistogramBuilder { |
| 167 |
HistogramBuilder::new() |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
#[cfg(all(test, target_has_atomic = "64"))] |
| 172 |
mod test { |
| 173 |
use super::*; |
| 174 |
|
| 175 |
macro_rules! assert_bucket_eq { |
| 176 |
($h:expr, $bucket:expr, $val:expr) => {{ |
| 177 |
assert_eq!($h.buckets[$bucket], $val); |
| 178 |
}}; |
| 179 |
} |
| 180 |
|
| 181 |
#[test] |
| 182 |
fn log_scale_resolution_1() { |
| 183 |
let h = HistogramBuilder { |
| 184 |
scale: HistogramScale::Log, |
| 185 |
resolution: 1, |
| 186 |
num_buckets: 10, |
| 187 |
} |
| 188 |
.build(); |
| 189 |
|
| 190 |
assert_eq!(h.bucket_range(0), 0..1); |
| 191 |
assert_eq!(h.bucket_range(1), 1..2); |
| 192 |
assert_eq!(h.bucket_range(2), 2..4); |
| 193 |
assert_eq!(h.bucket_range(3), 4..8); |
| 194 |
assert_eq!(h.bucket_range(9), 256..u64::MAX); |
| 195 |
|
| 196 |
let mut b = HistogramBatch::from_histogram(&h); |
| 197 |
|
| 198 |
b.measure(0, 1); |
| 199 |
assert_bucket_eq!(b, 0, 1); |
| 200 |
assert_bucket_eq!(b, 1, 0); |
| 201 |
|
| 202 |
b.measure(1, 1); |
| 203 |
assert_bucket_eq!(b, 0, 1); |
| 204 |
assert_bucket_eq!(b, 1, 1); |
| 205 |
assert_bucket_eq!(b, 2, 0); |
| 206 |
|
| 207 |
b.measure(2, 1); |
| 208 |
assert_bucket_eq!(b, 0, 1); |
| 209 |
assert_bucket_eq!(b, 1, 1); |
| 210 |
assert_bucket_eq!(b, 2, 1); |
| 211 |
|
| 212 |
b.measure(3, 1); |
| 213 |
assert_bucket_eq!(b, 0, 1); |
| 214 |
assert_bucket_eq!(b, 1, 1); |
| 215 |
assert_bucket_eq!(b, 2, 2); |
| 216 |
|
| 217 |
b.measure(4, 1); |
| 218 |
assert_bucket_eq!(b, 0, 1); |
| 219 |
assert_bucket_eq!(b, 1, 1); |
| 220 |
assert_bucket_eq!(b, 2, 2); |
| 221 |
assert_bucket_eq!(b, 3, 1); |
| 222 |
|
| 223 |
b.measure(100, 1); |
| 224 |
assert_bucket_eq!(b, 7, 1); |
| 225 |
|
| 226 |
b.measure(128, 1); |
| 227 |
assert_bucket_eq!(b, 8, 1); |
| 228 |
|
| 229 |
b.measure(4096, 1); |
| 230 |
assert_bucket_eq!(b, 9, 1); |
| 231 |
} |
| 232 |
|
| 233 |
#[test] |
| 234 |
fn log_scale_resolution_2() { |
| 235 |
let h = HistogramBuilder { |
| 236 |
scale: HistogramScale::Log, |
| 237 |
resolution: 2, |
| 238 |
num_buckets: 10, |
| 239 |
} |
| 240 |
.build(); |
| 241 |
|
| 242 |
assert_eq!(h.bucket_range(0), 0..2); |
| 243 |
assert_eq!(h.bucket_range(1), 2..4); |
| 244 |
assert_eq!(h.bucket_range(2), 4..8); |
| 245 |
assert_eq!(h.bucket_range(3), 8..16); |
| 246 |
assert_eq!(h.bucket_range(9), 512..u64::MAX); |
| 247 |
|
| 248 |
let mut b = HistogramBatch::from_histogram(&h); |
| 249 |
|
| 250 |
b.measure(0, 1); |
| 251 |
assert_bucket_eq!(b, 0, 1); |
| 252 |
assert_bucket_eq!(b, 1, 0); |
| 253 |
|
| 254 |
b.measure(1, 1); |
| 255 |
assert_bucket_eq!(b, 0, 2); |
| 256 |
assert_bucket_eq!(b, 1, 0); |
| 257 |
|
| 258 |
b.measure(2, 1); |
| 259 |
assert_bucket_eq!(b, 0, 2); |
| 260 |
assert_bucket_eq!(b, 1, 1); |
| 261 |
assert_bucket_eq!(b, 2, 0); |
| 262 |
|
| 263 |
b.measure(3, 1); |
| 264 |
assert_bucket_eq!(b, 0, 2); |
| 265 |
assert_bucket_eq!(b, 1, 2); |
| 266 |
assert_bucket_eq!(b, 2, 0); |
| 267 |
|
| 268 |
b.measure(4, 1); |
| 269 |
assert_bucket_eq!(b, 0, 2); |
| 270 |
assert_bucket_eq!(b, 1, 2); |
| 271 |
assert_bucket_eq!(b, 2, 1); |
| 272 |
|
| 273 |
b.measure(5, 1); |
| 274 |
assert_bucket_eq!(b, 0, 2); |
| 275 |
assert_bucket_eq!(b, 1, 2); |
| 276 |
assert_bucket_eq!(b, 2, 2); |
| 277 |
|
| 278 |
b.measure(6, 1); |
| 279 |
assert_bucket_eq!(b, 0, 2); |
| 280 |
assert_bucket_eq!(b, 1, 2); |
| 281 |
assert_bucket_eq!(b, 2, 3); |
| 282 |
|
| 283 |
b.measure(7, 1); |
| 284 |
assert_bucket_eq!(b, 0, 2); |
| 285 |
assert_bucket_eq!(b, 1, 2); |
| 286 |
assert_bucket_eq!(b, 2, 4); |
| 287 |
|
| 288 |
b.measure(8, 1); |
| 289 |
assert_bucket_eq!(b, 0, 2); |
| 290 |
assert_bucket_eq!(b, 1, 2); |
| 291 |
assert_bucket_eq!(b, 2, 4); |
| 292 |
assert_bucket_eq!(b, 3, 1); |
| 293 |
|
| 294 |
b.measure(100, 1); |
| 295 |
assert_bucket_eq!(b, 6, 1); |
| 296 |
|
| 297 |
b.measure(128, 1); |
| 298 |
assert_bucket_eq!(b, 7, 1); |
| 299 |
|
| 300 |
b.measure(4096, 1); |
| 301 |
assert_bucket_eq!(b, 9, 1); |
| 302 |
|
| 303 |
for bucket in h.buckets.iter() { |
| 304 |
assert_eq!(bucket.load(Relaxed), 0); |
| 305 |
} |
| 306 |
|
| 307 |
b.submit(&h); |
| 308 |
|
| 309 |
for i in 0..h.buckets.len() { |
| 310 |
assert_eq!(h.buckets[i].load(Relaxed), b.buckets[i]); |
| 311 |
} |
| 312 |
|
| 313 |
b.submit(&h); |
| 314 |
|
| 315 |
for i in 0..h.buckets.len() { |
| 316 |
assert_eq!(h.buckets[i].load(Relaxed), b.buckets[i]); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
#[test] |
| 321 |
fn linear_scale_resolution_1() { |
| 322 |
let h = HistogramBuilder { |
| 323 |
scale: HistogramScale::Linear, |
| 324 |
resolution: 1, |
| 325 |
num_buckets: 10, |
| 326 |
} |
| 327 |
.build(); |
| 328 |
|
| 329 |
assert_eq!(h.bucket_range(0), 0..1); |
| 330 |
assert_eq!(h.bucket_range(1), 1..2); |
| 331 |
assert_eq!(h.bucket_range(2), 2..3); |
| 332 |
assert_eq!(h.bucket_range(3), 3..4); |
| 333 |
assert_eq!(h.bucket_range(9), 9..u64::MAX); |
| 334 |
|
| 335 |
let mut b = HistogramBatch::from_histogram(&h); |
| 336 |
|
| 337 |
b.measure(0, 1); |
| 338 |
assert_bucket_eq!(b, 0, 1); |
| 339 |
assert_bucket_eq!(b, 1, 0); |
| 340 |
|
| 341 |
b.measure(1, 1); |
| 342 |
assert_bucket_eq!(b, 0, 1); |
| 343 |
assert_bucket_eq!(b, 1, 1); |
| 344 |
assert_bucket_eq!(b, 2, 0); |
| 345 |
|
| 346 |
b.measure(2, 1); |
| 347 |
assert_bucket_eq!(b, 0, 1); |
| 348 |
assert_bucket_eq!(b, 1, 1); |
| 349 |
assert_bucket_eq!(b, 2, 1); |
| 350 |
assert_bucket_eq!(b, 3, 0); |
| 351 |
|
| 352 |
b.measure(3, 1); |
| 353 |
assert_bucket_eq!(b, 0, 1); |
| 354 |
assert_bucket_eq!(b, 1, 1); |
| 355 |
assert_bucket_eq!(b, 2, 1); |
| 356 |
assert_bucket_eq!(b, 3, 1); |
| 357 |
|
| 358 |
b.measure(5, 1); |
| 359 |
assert_bucket_eq!(b, 5, 1); |
| 360 |
|
| 361 |
b.measure(4096, 1); |
| 362 |
assert_bucket_eq!(b, 9, 1); |
| 363 |
|
| 364 |
for bucket in h.buckets.iter() { |
| 365 |
assert_eq!(bucket.load(Relaxed), 0); |
| 366 |
} |
| 367 |
|
| 368 |
b.submit(&h); |
| 369 |
|
| 370 |
for i in 0..h.buckets.len() { |
| 371 |
assert_eq!(h.buckets[i].load(Relaxed), b.buckets[i]); |
| 372 |
} |
| 373 |
|
| 374 |
b.submit(&h); |
| 375 |
|
| 376 |
for i in 0..h.buckets.len() { |
| 377 |
assert_eq!(h.buckets[i].load(Relaxed), b.buckets[i]); |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
#[test] |
| 382 |
fn linear_scale_resolution_100() { |
| 383 |
let h = HistogramBuilder { |
| 384 |
scale: HistogramScale::Linear, |
| 385 |
resolution: 100, |
| 386 |
num_buckets: 10, |
| 387 |
} |
| 388 |
.build(); |
| 389 |
|
| 390 |
assert_eq!(h.bucket_range(0), 0..100); |
| 391 |
assert_eq!(h.bucket_range(1), 100..200); |
| 392 |
assert_eq!(h.bucket_range(2), 200..300); |
| 393 |
assert_eq!(h.bucket_range(3), 300..400); |
| 394 |
assert_eq!(h.bucket_range(9), 900..u64::MAX); |
| 395 |
|
| 396 |
let mut b = HistogramBatch::from_histogram(&h); |
| 397 |
|
| 398 |
b.measure(0, 1); |
| 399 |
assert_bucket_eq!(b, 0, 1); |
| 400 |
assert_bucket_eq!(b, 1, 0); |
| 401 |
|
| 402 |
b.measure(50, 1); |
| 403 |
assert_bucket_eq!(b, 0, 2); |
| 404 |
assert_bucket_eq!(b, 1, 0); |
| 405 |
|
| 406 |
b.measure(100, 1); |
| 407 |
assert_bucket_eq!(b, 0, 2); |
| 408 |
assert_bucket_eq!(b, 1, 1); |
| 409 |
assert_bucket_eq!(b, 2, 0); |
| 410 |
|
| 411 |
b.measure(101, 1); |
| 412 |
assert_bucket_eq!(b, 0, 2); |
| 413 |
assert_bucket_eq!(b, 1, 2); |
| 414 |
assert_bucket_eq!(b, 2, 0); |
| 415 |
|
| 416 |
b.measure(200, 1); |
| 417 |
assert_bucket_eq!(b, 0, 2); |
| 418 |
assert_bucket_eq!(b, 1, 2); |
| 419 |
assert_bucket_eq!(b, 2, 1); |
| 420 |
|
| 421 |
b.measure(299, 1); |
| 422 |
assert_bucket_eq!(b, 0, 2); |
| 423 |
assert_bucket_eq!(b, 1, 2); |
| 424 |
assert_bucket_eq!(b, 2, 2); |
| 425 |
|
| 426 |
b.measure(222, 1); |
| 427 |
assert_bucket_eq!(b, 0, 2); |
| 428 |
assert_bucket_eq!(b, 1, 2); |
| 429 |
assert_bucket_eq!(b, 2, 3); |
| 430 |
|
| 431 |
b.measure(300, 1); |
| 432 |
assert_bucket_eq!(b, 0, 2); |
| 433 |
assert_bucket_eq!(b, 1, 2); |
| 434 |
assert_bucket_eq!(b, 2, 3); |
| 435 |
assert_bucket_eq!(b, 3, 1); |
| 436 |
|
| 437 |
b.measure(888, 1); |
| 438 |
assert_bucket_eq!(b, 8, 1); |
| 439 |
|
| 440 |
b.measure(4096, 1); |
| 441 |
assert_bucket_eq!(b, 9, 1); |
| 442 |
|
| 443 |
for bucket in h.buckets.iter() { |
| 444 |
assert_eq!(bucket.load(Relaxed), 0); |
| 445 |
} |
| 446 |
|
| 447 |
b.submit(&h); |
| 448 |
|
| 449 |
for i in 0..h.buckets.len() { |
| 450 |
assert_eq!(h.buckets[i].load(Relaxed), b.buckets[i]); |
| 451 |
} |
| 452 |
|
| 453 |
b.submit(&h); |
| 454 |
|
| 455 |
for i in 0..h.buckets.len() { |
| 456 |
assert_eq!(h.buckets[i].load(Relaxed), b.buckets[i]); |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
#[test] |
| 461 |
fn inc_by_more_than_one() { |
| 462 |
let h = HistogramBuilder { |
| 463 |
scale: HistogramScale::Linear, |
| 464 |
resolution: 100, |
| 465 |
num_buckets: 10, |
| 466 |
} |
| 467 |
.build(); |
| 468 |
|
| 469 |
let mut b = HistogramBatch::from_histogram(&h); |
| 470 |
|
| 471 |
b.measure(0, 3); |
| 472 |
assert_bucket_eq!(b, 0, 3); |
| 473 |
assert_bucket_eq!(b, 1, 0); |
| 474 |
|
| 475 |
b.measure(50, 5); |
| 476 |
assert_bucket_eq!(b, 0, 8); |
| 477 |
assert_bucket_eq!(b, 1, 0); |
| 478 |
|
| 479 |
b.measure(100, 2); |
| 480 |
assert_bucket_eq!(b, 0, 8); |
| 481 |
assert_bucket_eq!(b, 1, 2); |
| 482 |
assert_bucket_eq!(b, 2, 0); |
| 483 |
|
| 484 |
b.measure(101, 19); |
| 485 |
assert_bucket_eq!(b, 0, 8); |
| 486 |
assert_bucket_eq!(b, 1, 21); |
| 487 |
assert_bucket_eq!(b, 2, 0); |
| 488 |
|
| 489 |
for bucket in h.buckets.iter() { |
| 490 |
assert_eq!(bucket.load(Relaxed), 0); |
| 491 |
} |
| 492 |
|
| 493 |
b.submit(&h); |
| 494 |
|
| 495 |
for i in 0..h.buckets.len() { |
| 496 |
assert_eq!(h.buckets[i].load(Relaxed), b.buckets[i]); |
| 497 |
} |
| 498 |
|
| 499 |
b.submit(&h); |
| 500 |
|
| 501 |
for i in 0..h.buckets.len() { |
| 502 |
assert_eq!(h.buckets[i].load(Relaxed), b.buckets[i]); |
| 503 |
} |
| 504 |
} |
| 505 |
} |