Skip to content

Commit d3d8532

Browse files
committed
CR feedback: improve docs, cleanup implementation, a few renames
1 parent a1cf59c commit d3d8532

File tree

5 files changed

+53
-33
lines changed

5 files changed

+53
-33
lines changed

spellcheck.dic

+6-4
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@
1414
100ms
1515
100ns
1616
10ms
17-
12.5%
17+
10μs
1818
~12
19+
120s
20+
12.5%
1921
±1m
2022
±1ms
2123
1ms
2224
1s
23-
250ms
2425
25%
26+
250ms
2527
2x
2628
~4
2729
443
@@ -119,11 +121,11 @@ GID
119121
goroutines
120122
Growable
121123
gzip
122-
hashmaps
123124
H2
125+
hashmaps
124126
HashMaps
125-
HdrHistogram
126127
hashsets
128+
HdrHistogram
127129
ie
128130
Illumos
129131
impl

tokio/src/runtime/builder.rs

+28-2
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ impl Builder {
11691169
/// better granularity with low memory usage, use [`LogHistogram`] instead.
11701170
///
11711171
/// # Examples
1172-
/// Configure a `LogHistogram` with default configuration:
1172+
/// Configure a [`LogHistogram`] with [default configuration]:
11731173
/// ```
11741174
/// use tokio::runtime;
11751175
/// use tokio::runtime::{HistogramConfiguration, LogHistogram};
@@ -1183,7 +1183,7 @@ impl Builder {
11831183
/// .unwrap();
11841184
/// ```
11851185
///
1186-
/// Configure a linear histogram
1186+
/// Configure a linear histogram with 100 buckets, each 10μs wide
11871187
/// ```
11881188
/// use tokio::runtime;
11891189
/// use std::time::Duration;
@@ -1198,7 +1198,33 @@ impl Builder {
11981198
/// .unwrap();
11991199
/// ```
12001200
///
1201+
/// Configure a [`LogHistogram`] with the following settings:
1202+
/// - Measure times from 100ns to 120s
1203+
/// - Max error of 0.1
1204+
/// - No more than 1024 buckets
1205+
/// ```
1206+
/// use std::time::Duration;
1207+
/// use tokio::runtime;
1208+
/// use tokio::runtime::{HistogramConfiguration, LogHistogram};
1209+
///
1210+
/// let rt = runtime::Builder::new_multi_thread()
1211+
/// .enable_metrics_poll_count_histogram()
1212+
/// .metrics_poll_count_histogram_configuration(
1213+
/// HistogramConfiguration::log(LogHistogram::builder()
1214+
/// .max_value(Duration::from_secs(120))
1215+
/// .min_value(Duration::from_nanos(100))
1216+
/// .max_error(0.1)
1217+
/// .max_buckets(1024)
1218+
/// .expect("configuration uses 488 buckets")
1219+
/// )
1220+
/// )
1221+
/// .build()
1222+
/// .unwrap();
1223+
/// ```
1224+
///
1225+
///
12011226
/// [`LogHistogram`]: crate::runtime::LogHistogram
1227+
/// [default configuration]: crate::runtime::LogHistogramBuilder
12021228
pub fn metrics_poll_count_histogram_configuration(&mut self, configuration: HistogramConfiguration) -> &mut Self {
12031229
self.metrics_poll_count_histogram.histogram_type = configuration.inner;
12041230
self

tokio/src/runtime/metrics/histogram.rs

+15-23
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ pub(crate) struct Histogram {
1515
/// The histogram buckets
1616
buckets: Box<[MetricAtomicU64]>,
1717

18-
/// Bucket scale, linear or log
19-
configuration: HistogramType,
18+
/// The type of the histogram
19+
///
20+
/// This handles `fn(bucket) -> Range` and `fn(value) -> bucket`
21+
histogram_type: HistogramType,
2022
}
2123

2224
#[derive(Debug, Clone)]
@@ -98,6 +100,7 @@ cfg_unstable! {
98100
pub(crate) enum HistogramType {
99101
/// Linear histogram with fixed width buckets
100102
Linear(LinearHistogram),
103+
101104
/// Old log histogram where each bucket doubles in size
102105
LogLegacy(LegacyLogHistogram),
103106

@@ -196,7 +199,7 @@ impl Histogram {
196199
}
197200

198201
pub(crate) fn bucket_range(&self, bucket: usize) -> Range<u64> {
199-
self.configuration.bucket_range(bucket)
202+
self.histogram_type.bucket_range(bucket)
200203
}
201204
}
202205

@@ -206,7 +209,7 @@ impl HistogramBatch {
206209

207210
HistogramBatch {
208211
buckets,
209-
configuration: histogram.configuration,
212+
configuration: histogram.histogram_type,
210213
}
211214
}
212215

@@ -215,7 +218,7 @@ impl HistogramBatch {
215218
}
216219

217220
pub(crate) fn submit(&self, histogram: &Histogram) {
218-
debug_assert_eq!(self.configuration, histogram.configuration);
221+
debug_assert_eq!(self.configuration, histogram.histogram_type);
219222
debug_assert_eq!(self.buckets.len(), histogram.buckets.len());
220223

221224
for i in 0..self.buckets.len() {
@@ -240,33 +243,22 @@ impl HistogramBuilder {
240243
}
241244

242245
pub(crate) fn legacy_mut(&mut self, f: impl Fn(&mut LegacyBuilder)) {
243-
if let Some(legacy) = &mut self.legacy {
244-
f(legacy)
245-
} else {
246-
let mut legacy = LegacyBuilder::default();
247-
f(&mut legacy);
248-
self.legacy = Some(legacy)
249-
}
246+
let legacy = self.legacy.get_or_insert_with(|| LegacyBuilder::default());
247+
f(legacy);
250248
}
251249

252250
pub(crate) fn build(&self) -> Histogram {
253-
let configuration = match &self.legacy {
251+
let histogram_type = match &self.legacy {
254252
Some(legacy) => {
255-
let mut resolution = legacy.resolution;
256-
257-
assert!(resolution > 0);
258-
259-
if matches!(legacy.scale, HistogramScale::Log) {
260-
resolution = resolution.next_power_of_two();
261-
}
253+
assert!(legacy.resolution > 0);
262254
match legacy.scale {
263255
HistogramScale::Linear => HistogramType::Linear(LinearHistogram {
264256
num_buckets: legacy.num_buckets,
265-
bucket_width: resolution,
257+
bucket_width: legacy.resolution,
266258
}),
267259
HistogramScale::Log => HistogramType::LogLegacy(LegacyLogHistogram {
268260
num_buckets: legacy.num_buckets,
269-
first_bucket_width: resolution,
261+
first_bucket_width: legacy.resolution.next_power_of_two(),
270262
}),
271263
}
272264
}
@@ -279,7 +271,7 @@ impl HistogramBuilder {
279271
.map(|_| MetricAtomicU64::new(0))
280272
.collect::<Vec<_>>()
281273
.into_boxed_slice(),
282-
configuration,
274+
histogram_type: histogram_type,
283275
}
284276
}
285277
}

tokio/src/runtime/metrics/histogram/h2_histogram.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl LogHistogram {
120120
/// The log-scaled histogram implements an H2 histogram where the first bucket covers
121121
/// the range from 0 to [`LogHistogramBuilder::min_value`] and the final bucket covers
122122
/// [`LogHistogramBuilder::max_value`] to infinity. The precision is bounded to the specified
123-
/// [`LogHistogramBuilder::precision`]. Specifically, the precision is the next smallest value
123+
/// [`LogHistogramBuilder::max_error`]. Specifically, the precision is the next smallest value
124124
/// of `2^-p` such that it is smaller than the requested precision.
125125
///
126126
/// Depending on the selected parameters, the number of buckets required is variable. To ensure
@@ -162,7 +162,7 @@ impl LogHistogramBuilder {
162162
/// # Panics
163163
/// - `precision` < 0
164164
/// - `precision` > 1
165-
pub fn precision(mut self, max_error: f64) -> Self {
165+
pub fn max_error(mut self, max_error: f64) -> Self {
166166
if max_error < 0.0 {
167167
panic!("precision must be >= 0");
168168
};
@@ -460,7 +460,7 @@ mod test {
460460
#[test]
461461
fn max_buckets_enforcement() {
462462
let error = LogHistogram::builder()
463-
.precision(0.001)
463+
.max_error(0.001)
464464
.max_buckets(5)
465465
.expect_err("this produces way more than 5 buckets");
466466
let num_buckets = match error {

tokio/tests/rt_unstable_metrics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ fn log_histogram() {
400400
LogHistogram::builder()
401401
.max_value(Duration::from_secs(60))
402402
.min_value(Duration::from_nanos(100))
403-
.precision(0.25),
403+
.max_error(0.25),
404404
))
405405
.build()
406406
.unwrap();

0 commit comments

Comments
 (0)