src/utils_latency{,_config}.[ch]: Reformat new code with clang-format.
[collectd.git] / src / utils_latency_config.c
1 /**
2  * collectd - src/utils_latency_config.c
3  * Copyright (C) 2013-2016   Florian octo 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 octo Forster <octo at collectd.org>
25  *   Pavel Rochnyack <pavel2000 at ngs.ru>
26  */
27
28 #include "collectd.h"
29 #include "common.h"
30 #include "utils_latency_config.h"
31
32 int latency_config_add_percentile(const char *plugin, latency_config_t *cl,
33                                   oconfig_item_t *ci) {
34   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) {
35     ERROR("%s plugin: \"%s\" requires exactly one numeric argument.", plugin,
36           ci->key);
37     return (-1);
38   }
39
40   double percent = ci->values[0].value.number;
41   double *tmp;
42
43   if ((percent <= 0.0) || (percent >= 100)) {
44     ERROR("%s plugin: The value for \"%s\" must be between 0 and 100, "
45           "exclusively.",
46           plugin, ci->key);
47     return (ERANGE);
48   }
49
50   tmp = realloc(cl->percentile,
51                 sizeof(*cl->percentile) * (cl->percentile_num + 1));
52   if (tmp == NULL) {
53     ERROR("%s plugin: realloc failed.", plugin);
54     return (ENOMEM);
55   }
56   cl->percentile = tmp;
57   cl->percentile[cl->percentile_num] = percent;
58   cl->percentile_num++;
59
60   return (0);
61 } /* int latency_config_add_percentile */
62
63 int latency_config_add_rate(const char *plugin, latency_config_t *cl,
64                             oconfig_item_t *ci) {
65   if ((ci->values_num != 2) || (ci->values[0].type != OCONFIG_TYPE_NUMBER) ||
66       (ci->values[1].type != OCONFIG_TYPE_NUMBER)) {
67     ERROR("%s plugin: \"%s\" requires exactly two numeric arguments.", plugin,
68           ci->key);
69     return (-1);
70   }
71
72   if (ci->values[1].value.number &&
73       ci->values[1].value.number <= ci->values[0].value.number) {
74     ERROR("%s plugin: MIN must be less than MAX in \"%s\".", plugin, ci->key);
75     return (-1);
76   }
77
78   if (ci->values[0].value.number < 0.001) {
79     ERROR("%s plugin: MIN must be greater or equal to 0.001 in \"%s\".", plugin,
80           ci->key);
81     return (-1);
82   }
83
84   cdtime_t lower = DOUBLE_TO_CDTIME_T(ci->values[0].value.number);
85   cdtime_t upper = DOUBLE_TO_CDTIME_T(ci->values[1].value.number);
86   cdtime_t *tmp;
87
88   tmp = realloc(cl->rates, sizeof(*cl->rates) * (cl->rates_num + 1) * 2);
89   if (tmp == NULL) {
90     ERROR("%s plugin: realloc failed.", plugin);
91     return (ENOMEM);
92   }
93   cl->rates = tmp;
94   cl->rates[cl->rates_num * 2] = lower;
95   cl->rates[cl->rates_num * 2 + 1] = upper;
96   cl->rates_num++;
97
98   return (0);
99 } /* int latency_config_add_rate */
100
101 int latency_config_copy(latency_config_t *dst, const latency_config_t src) {
102   *dst = (latency_config_t){
103       .rates = NULL,
104       .rates_num = src.rates_num,
105       .rates_type = NULL,
106       .percentile = NULL,
107       .percentile_num = src.percentile_num,
108       .percentile_type = NULL,
109   };
110
111   /* Copy percentiles configuration */
112   dst->percentile_num = src.percentile_num;
113   dst->percentile = calloc(src.percentile_num, sizeof(*dst->percentile));
114   if (dst->percentile == NULL)
115     return (-1);
116
117   memcpy(dst->percentile, src.percentile,
118          (sizeof(*dst->percentile) * (src.percentile_num)));
119
120   if (src.percentile_type != NULL) {
121     dst->percentile_type = strdup(src.percentile_type);
122     if (dst->percentile_type == NULL) {
123       latency_config_free(*dst);
124       return (-1);
125     }
126   }
127
128   /* Copy rates configuration */
129   dst->rates_num = src.rates_num;
130   dst->rates = calloc(src.rates_num * 2, sizeof(*dst->rates));
131   if (dst->rates == NULL) {
132     latency_config_free(*dst);
133     return (-1);
134   }
135
136   memcpy(dst->rates, src.rates, (sizeof(*dst->rates) * (src.rates_num) * 2));
137
138   if (src.rates_type != NULL) {
139     dst->rates_type = strdup(src.rates_type);
140     if (dst->rates_type == NULL) {
141       latency_config_free(*dst);
142       return (-1);
143     }
144   }
145
146   return (0);
147 } /* int latency_config_copy */
148
149 void latency_config_free(latency_config_t lc) {
150   sfree(lc.rates);
151   sfree(lc.rates_type);
152   sfree(lc.percentile);
153   sfree(lc.percentile_type);
154 } /* void latency_config_free */