1 /**
2 * collectd - src/utils_latency.c
3 * Copyright (C) 2013 Florian Forster
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors:
24 * Florian Forster <ff at octo.it>
25 **/
27 #include "collectd.h"
28 #include "utils_latency.h"
29 #include "common.h"
31 #ifndef LATENCY_HISTOGRAM_SIZE
32 # define LATENCY_HISTOGRAM_SIZE 1000
33 #endif
35 struct latency_counter_s
36 {
37 cdtime_t start_time;
39 cdtime_t sum;
40 size_t num;
42 cdtime_t min;
43 cdtime_t max;
45 int histogram[LATENCY_HISTOGRAM_SIZE];
46 };
48 latency_counter_t *latency_counter_create () /* {{{ */
49 {
50 latency_counter_t *lc;
52 lc = malloc (sizeof (*lc));
53 if (lc == NULL)
54 return (NULL);
56 latency_counter_reset (lc);
57 return (lc);
58 } /* }}} latency_counter_t *latency_counter_create */
60 void latency_counter_destroy (latency_counter_t *lc) /* {{{ */
61 {
62 sfree (lc);
63 } /* }}} void latency_counter_destroy */
65 void latency_counter_add (latency_counter_t *lc, cdtime_t latency) /* {{{ */
66 {
67 size_t latency_ms;
69 if ((lc == NULL) || (latency == 0))
70 return;
72 lc->sum += latency;
73 lc->num++;
75 if ((lc->min == 0) && (lc->max == 0))
76 lc->min = lc->max = latency;
77 if (lc->min > latency)
78 lc->min = latency;
79 if (lc->max < latency)
80 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
84 * accordingly. */
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) /* {{{ */
91 {
92 if (lc == NULL)
93 return;
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) /* {{{ */
100 {
101 if (lc == NULL)
102 return (0);
103 return (lc->min);
104 } /* }}} cdtime_t latency_counter_get_min */
106 cdtime_t latency_counter_get_max (latency_counter_t *lc) /* {{{ */
107 {
108 if (lc == NULL)
109 return (0);
110 return (lc->max);
111 } /* }}} cdtime_t latency_counter_get_max */
113 cdtime_t latency_counter_get_sum (latency_counter_t *lc) /* {{{ */
114 {
115 if (lc == NULL)
116 return (0);
117 return (lc->sum);
118 } /* }}} cdtime_t latency_counter_get_sum */
120 size_t latency_counter_get_num (latency_counter_t *lc) /* {{{ */
121 {
122 if (lc == NULL)
123 return (0);
124 return (lc->num);
125 } /* }}} size_t latency_counter_get_num */
127 cdtime_t latency_counter_get_average (latency_counter_t *lc) /* {{{ */
128 {
129 double average;
131 if (lc == NULL)
132 return (0);
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,
139 double percent)
140 {
141 double percent_upper;
142 double percent_lower;
143 double ms_upper;
144 double ms_lower;
145 double ms_interpolated;
146 int sum;
147 size_t i;
149 if ((lc == NULL) || !((percent > 0.0) && (percent < 100.0)))
150 return (0);
152 /* Find index i so that at least "percent" events are within i+1 ms. */
153 percent_upper = 0.0;
154 percent_lower = 0.0;
155 sum = 0;
156 for (i = 0; i < LATENCY_HISTOGRAM_SIZE; i++)
157 {
158 percent_lower = percent_upper;
159 sum += lc->histogram[i];
160 if (sum == 0)
161 percent_upper = 0.0;
162 else
163 percent_upper = 100.0 * ((double) sum) / ((double) lc->num);
165 if (percent_upper >= percent)
166 break;
167 }
169 if (i >= LATENCY_HISTOGRAM_SIZE)
170 return (0);
172 assert (percent_upper >= percent);
173 assert (percent_lower < percent);
175 ms_upper = (double) (i + 1);
176 ms_lower = (double) i;
177 if (i == 0)
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 : */