-
Notifications
You must be signed in to change notification settings - Fork 464
/
stats-cluster.c
288 lines (249 loc) · 7.01 KB
/
stats-cluster.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* Copyright (c) 2002-2013 Balabit
* Copyright (c) 1998-2013 Balázs Scheidler
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "stats/stats-cluster.h"
#include <string.h>
static StatsClusterKey *
_clone_stats_cluster_key(StatsClusterKey *dst, const StatsClusterKey *src)
{
dst->component = src->component;
dst->id = g_strdup(src->id ? : "");
dst->instance = g_strdup(src->instance ? : "");
dst->counter_group_init = src->counter_group_init;
return dst;
}
void
stats_cluster_key_set(StatsClusterKey *self, guint16 component, const gchar *id, const gchar *instance,
StatsCounterGroupInit counter_group_init)
{
self->component = component;
self->id = (id?id:"");
self->instance = (instance?instance:"");
self->counter_group_init = counter_group_init;
}
static void
_stats_cluster_key_cloned_free(StatsClusterKey *self)
{
g_free((gchar *)(self->id));
g_free((gchar *)(self->instance));
}
void
stats_cluster_foreach_counter(StatsCluster *self, StatsForeachCounterFunc func, gpointer user_data)
{
gint type;
for (type = 0; type < self->counter_group.capacity; type++)
{
if (self->live_mask & (1 << type))
{
func(self, type, &self->counter_group.counters[type], user_data);
}
}
}
const gchar *
stats_cluster_get_type_name(StatsCluster *self, gint type)
{
if (type < self->counter_group.capacity)
return self->counter_group.counter_names[type];
return "";
}
static const gchar *
_get_module_name(gint source)
{
static const gchar *module_names[SCS_MAX] =
{
"none",
"file",
"pipe",
"tcp",
"udp",
"tcp6",
"udp6",
"unix-stream",
"unix-dgram",
"syslog",
"network",
"internal",
"logstore",
"program",
"sql",
"sun-streams",
"usertty",
"group",
"center",
"host",
"global",
"mongodb",
"class",
"rule_id",
"tag",
"severity",
"facility",
"sender",
"smtp",
"amqp",
"stomp",
"redis",
"snmp",
"riemann",
"journald",
"java",
"http",
"python",
"filter",
"parser"
};
return module_names[source & SCS_SOURCE_MASK];
}
static const gchar *
_get_component_prefix(gint source)
{
return (source & SCS_SOURCE ? "src." : (source & SCS_DESTINATION ? "dst." : ""));
}
/* buf is a scratch area which is not always used, the return value is
* either a locally managed string or points to @buf. */
const gchar *
stats_cluster_get_component_name(StatsCluster *self, gchar *buf, gsize buf_len)
{
if ((self->key.component & SCS_SOURCE_MASK) == SCS_GROUP)
{
if (self->key.component & SCS_SOURCE)
return "source";
else if (self->key.component & SCS_DESTINATION)
return "destination";
else
g_assert_not_reached();
}
else
{
g_snprintf(buf, buf_len, "%s%s",
_get_component_prefix(self->key.component),
_get_module_name(self->key.component));
return buf;
}
}
gboolean
stats_counter_group_init_equals(const StatsCounterGroupInit *self, const StatsCounterGroupInit *other)
{
if (!self || !other)
return FALSE;
if (self == other)
return TRUE;
if (self->equals)
return self->equals(self, other);
return (self->init == other->init) && (self->counter_names == other->counter_names);
}
gboolean
stats_cluster_key_equal(const StatsClusterKey *key1, const StatsClusterKey *key2)
{
return key1->component == key2->component
&& strcmp(key1->id, key2->id) == 0
&& strcmp(key1->instance, key2->instance) == 0
&& stats_counter_group_init_equals(&key1->counter_group_init, &key2->counter_group_init);
}
gboolean
stats_cluster_equal(const StatsCluster *sc1, const StatsCluster *sc2)
{
return stats_cluster_key_equal(&sc1->key, &sc2->key);
}
guint
stats_cluster_hash(const StatsCluster *self)
{
return g_str_hash(self->key.id) + g_str_hash(self->key.instance) + self->key.component;
}
StatsCounterItem *
stats_cluster_track_counter(StatsCluster *self, gint type)
{
gint type_mask = 1 << type;
g_assert(type < self->counter_group.capacity);
self->live_mask |= type_mask;
self->use_count++;
return &self->counter_group.counters[type];
}
void
stats_cluster_untrack_counter(StatsCluster *self, gint type, StatsCounterItem **counter)
{
g_assert(self && (self->live_mask & (1 << type)) && &self->counter_group.counters[type] == (*counter));
g_assert(self->use_count > 0);
self->use_count--;
*counter = NULL;
}
static gchar *
_stats_build_query_key(StatsCluster *self)
{
GString *key = g_string_new("");
gchar buffer[64] = {0};
const gchar *component_name = stats_cluster_get_component_name(self, buffer, sizeof(buffer));
g_string_append(key, component_name);
if (self->key.id && self->key.id[0])
{
g_string_append_printf(key, ".%s", self->key.id);
}
if (self->key.instance && self->key.instance[0])
{
g_string_append_printf(key, ".%s", self->key.instance);
}
return g_string_free(key, FALSE);
}
gboolean
stats_cluster_is_alive(StatsCluster *self, gint type)
{
g_assert(type < self->counter_group.capacity);
return ((1<<type) & self->live_mask);
}
gboolean
stats_cluster_is_indexed(StatsCluster *self, gint type)
{
g_assert(type < self->counter_group.capacity);
return ((1<<type) & self->indexed_mask);
}
StatsCluster *
stats_cluster_new(const StatsClusterKey *key)
{
StatsCluster *self = g_new0(StatsCluster, 1);
_clone_stats_cluster_key(&self->key, key);
self->use_count = 0;
self->query_key = _stats_build_query_key(self);
key->counter_group_init.init(&self->key.counter_group_init, &self->counter_group);
g_assert(self->counter_group.capacity <= sizeof(self->live_mask)*8);
return self;
}
void
stats_counter_group_free(StatsCounterGroup *self)
{
if (self->free_fn)
self->free_fn(self);
}
static void
stats_cluster_free_counter_name(StatsCluster *self, gint type, StatsCounterItem *item, gpointer user_data)
{
g_free(item->name);
}
void
stats_cluster_free(StatsCluster *self)
{
stats_cluster_foreach_counter(self, stats_cluster_free_counter_name, NULL);
_stats_cluster_key_cloned_free(&self->key);
g_free(self->query_key);
stats_counter_group_free(&self->counter_group);
g_free(self);
}