2 * collectd - src/utils_latency.c
3 * Copyright (C) 2013 Florian Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian Forster <ff at octo.it>
28 #include "utils_latency.h"
31 #ifndef LATENCY_HISTOGRAM_SIZE
32 # define LATENCY_HISTOGRAM_SIZE 1000
35 struct latency_counter_s
45 int histogram[LATENCY_HISTOGRAM_SIZE];
48 latency_counter_t *latency_counter_create () /* {{{ */
50 latency_counter_t *lc;
52 lc = malloc (sizeof (*lc));
56 latency_counter_reset (lc);
58 } /* }}} latency_counter_t *latency_counter_create */
60 void latency_counter_destroy (latency_counter_t *lc) /* {{{ */
63 } /* }}} void latency_counter_destroy */
65 void latency_counter_add (latency_counter_t *lc, cdtime_t latency) /* {{{ */
69 if ((lc == NULL) || (latency == 0))
75 if ((lc->min == 0) && (lc->max == 0))
76 lc->min = lc->max = latency;
77 if (lc->min > latency)
79 if (lc->max < latency)
82 /* A latency of _exactly_ 1.0 ms should be stored in the buffer 0, so
83 * subtract one from the cdtime_t value so that exactly 1.0 ms get sorted
85 latency_ms = (size_t) CDTIME_T_TO_MS (latency - 1);
86 if (latency_ms < STATIC_ARRAY_SIZE (lc->histogram))
87 lc->histogram[latency_ms]++;
88 } /* }}} void latency_counter_add */
90 void latency_counter_reset (latency_counter_t *lc) /* {{{ */
95 memset (lc, 0, sizeof (*lc));
96 lc->start_time = cdtime ();
97 } /* }}} void latency_counter_reset */
99 cdtime_t latency_counter_get_min (latency_counter_t *lc) /* {{{ */
104 } /* }}} cdtime_t latency_counter_get_min */
106 cdtime_t latency_counter_get_max (latency_counter_t *lc) /* {{{ */
111 } /* }}} cdtime_t latency_counter_get_max */
113 cdtime_t latency_counter_get_sum (latency_counter_t *lc) /* {{{ */
118 } /* }}} cdtime_t latency_counter_get_sum */
120 size_t latency_counter_get_num (latency_counter_t *lc) /* {{{ */
125 } /* }}} size_t latency_counter_get_num */
127 cdtime_t latency_counter_get_average (latency_counter_t *lc) /* {{{ */
134 average = CDTIME_T_TO_DOUBLE (lc->sum) / ((double) lc->num);
135 return (DOUBLE_TO_CDTIME_T (average));
136 } /* }}} cdtime_t latency_counter_get_average */
138 cdtime_t latency_counter_get_percentile (latency_counter_t *lc,
141 double percent_upper;
142 double percent_lower;
145 double ms_interpolated;
149 if ((lc == NULL) || !((percent > 0.0) && (percent < 100.0)))
152 /* Find index i so that at least "percent" events are within i+1 ms. */
156 for (i = 0; i < LATENCY_HISTOGRAM_SIZE; i++)
158 percent_lower = percent_upper;
159 sum += lc->histogram[i];
163 percent_upper = 100.0 * ((double) sum) / ((double) lc->num);
165 if (percent_upper >= percent)
169 if (i >= LATENCY_HISTOGRAM_SIZE)
172 assert (percent_upper >= percent);
173 assert (percent_lower < percent);
175 ms_upper = (double) (i + 1);
176 ms_lower = (double) i;
178 return (MS_TO_CDTIME_T (ms_upper));
180 ms_interpolated = (((percent_upper - percent) * ms_lower)
181 + ((percent - percent_lower) * ms_upper))
182 / (percent_upper - percent_lower);
184 return (MS_TO_CDTIME_T (ms_interpolated));
185 } /* }}} cdtime_t latency_counter_get_percentile */
187 /* vim: set sw=2 sts=2 et fdm=marker : */