94da2112ec1971410f9b54870b9694ac35388b84
[collectd.git] / src / utils_latency.c
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  **/
26
27 #include "collectd.h"
28 #include "utils_latency.h"
29 #include "common.h"
30
31 #ifndef LATENCY_HISTOGRAM_SIZE
32 # define LATENCY_HISTOGRAM_SIZE 1000
33 #endif
34
35 struct latency_counter_s
36 {
37   cdtime_t start_time;
38
39   cdtime_t sum;
40   size_t num;
41
42   cdtime_t min;
43   cdtime_t max;
44
45   int histogram[LATENCY_HISTOGRAM_SIZE];
46 };
47
48 latency_counter_t *latency_counter_create () /* {{{ */
49 {
50   latency_counter_t *lc;
51
52   lc = malloc (sizeof (*lc));
53   if (lc == NULL)
54     return (NULL);
55
56   latency_counter_reset (lc);
57   return (lc);
58 } /* }}} latency_counter_t *latency_counter_create */
59
60 void latency_counter_destroy (latency_counter_t *lc) /* {{{ */
61 {
62   sfree (lc);
63 } /* }}} void latency_counter_destroy */
64
65 void latency_counter_add (latency_counter_t *lc, cdtime_t latency) /* {{{ */
66 {
67   size_t latency_ms;
68
69   if ((lc == NULL) || (latency == 0))
70     return;
71
72   lc->sum += latency;
73   lc->num++;
74
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;
81
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 */
89
90 void latency_counter_reset (latency_counter_t *lc) /* {{{ */
91 {
92   if (lc == NULL)
93     return;
94
95   memset (lc, 0, sizeof (*lc));
96   lc->start_time = cdtime ();
97 } /* }}} void latency_counter_reset */
98
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 */
105
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 */
112
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 */
119
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 */
126
127 cdtime_t latency_counter_get_average (latency_counter_t *lc) /* {{{ */
128 {
129   double average;
130
131   if (lc == NULL)
132     return (0);
133
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 */
137
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;
148
149   if ((lc == NULL) || !((percent > 0.0) && (percent < 100.0)))
150     return (0);
151
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);
164
165     if (percent_upper >= percent)
166       break;
167   }
168
169   if (i >= LATENCY_HISTOGRAM_SIZE)
170     return (0);
171
172   assert (percent_upper >= percent);
173   assert (percent_lower < percent);
174
175   ms_upper = (double) (i + 1);
176   ms_lower = (double) i;
177   if (i == 0)
178     return (MS_TO_CDTIME_T (ms_upper));
179
180   ms_interpolated = (((percent_upper - percent) * ms_lower)
181       + ((percent - percent_lower) * ms_upper))
182     / (percent_upper - percent_lower);
183
184   return (MS_TO_CDTIME_T (ms_interpolated));
185 } /* }}} cdtime_t latency_counter_get_percentile */
186
187 /* vim: set sw=2 sts=2 et fdm=marker : */