diff --git a/src/utils_latency.c b/src/utils_latency.c
index 1275f8909ca1f46f8341066daac0eac1915f3422..30f13c70d1d869cc056c70879801e1fab25bc018 100644 (file)
--- a/src/utils_latency.c
+++ b/src/utils_latency.c
if (lc->max < latency)
lc->max = latency;
- /* A latency of _exactly_ 1.0 ms should be stored in the buffer 0, so
+ /* A latency of _exactly_ 1.0 ms is stored in the buffer 0, so
* subtract one from the cdtime_t value so that exactly 1.0 ms get sorted
* accordingly. */
bin = (latency - 1) / lc->bin_width;
{
cdtime_t lower_bin;
cdtime_t upper_bin;
- double p;
double sum = 0;
- size_t i;
if ((lc == NULL) || (lc->num == 0))
return (0);
if (upper && (upper < lower))
return (0);
- /* A latency of _exactly_ 1.0 ms should be stored in the buffer 0 */
+ /* A latency of _exactly_ 1.0 ms is stored in the buffer 0 */
lower_bin = (lower - 1) / lc->bin_width;
if (upper)
}
sum = 0;
- for (i = lower_bin; i <= upper_bin; i++)
+ for (size_t i = lower_bin; i <= upper_bin; i++)
{
sum += lc->histogram[i];
}
- p = ((double)lower - (double)(lower_bin + 0) * (double)lc->bin_width - (double)DOUBLE_TO_CDTIME_T(0.001)) / (double)lc->bin_width;
- sum -= p * lc->histogram[lower_bin];
+ /* Approximate ratio of requests below "lower" */
+ cdtime_t lower_bin_boundary = lower_bin * lc->bin_width;
+
+ /* When bin width is 0.125 (for example), then bin 0 stores
+ * values for interval [0, 0.124) (excluding).
+ * With lower = 0.100, the ratio should be 0.099 / 0.125.
+ * I.e. ratio = 0.100 - 0.000 - 0.001
+ */
+ double ratio = (double)(lower - lower_bin_boundary - DOUBLE_TO_CDTIME_T(0.001))
+ / (double)lc->bin_width;
+ sum -= ratio * lc->histogram[lower_bin];
- if (upper && upper < (upper_bin + 1) * lc->bin_width)
+ /* Approximate ratio of requests above "upper" */
+ cdtime_t upper_bin_boundary = (upper_bin + 1) * lc->bin_width;
+ if (upper)
{
- // p = ((upper_bin + 1) * bin_width - upper ) / bin_width;
- p = ((double)(upper_bin + 1) * (double)lc->bin_width - (double)upper) / (double)lc->bin_width;
- sum -= p * lc->histogram[upper_bin];
+ assert (upper <= upper_bin_boundary);
+ double ratio = (double)(upper_bin_boundary - upper) / (double)lc->bin_width;
+ sum -= ratio * lc->histogram[upper_bin];
}
- return sum / (CDTIME_T_TO_DOUBLE (now - lc->start_time));
+ return sum / (CDTIME_T_TO_DOUBLE (now - lc->start_time));
} /* }}} double latency_counter_get_rate */
-
/* vim: set sw=2 sts=2 et fdm=marker : */