syslog-ng source
stats-cluster.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002-2017 Balabit
3  * Copyright (c) 1998-2013 Balázs Scheidler
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * As an additional exemption you are allowed to compile & link against the
20  * OpenSSL libraries as published by the OpenSSL project. See the file
21  * COPYING for details.
22  *
23  */
24 #ifndef STATS_CLUSTER_H_INCLUDED
25 #define STATS_CLUSTER_H_INCLUDED 1
26 
27 #include "stats/stats-counter.h"
29 
30 enum
31 {
32  /* direction bits, used to distinguish between source/destination drivers */
33  SCS_SOURCE = 0x0100,
34  SCS_DESTINATION = 0x0200,
35  SCS_GROUP = 1,
46  SCS_SOURCE_MASK = 0xff
47 };
48 
49 typedef enum _StatsClusterUnit
50 {
51  SCU_NONE = 0,
52 
58 
64 
65 typedef enum _StatsClusterFrameOfReference
66 {
69 
70  /*
71  * Only applicable for counters with seconds, minutes or hours unit.
72  * Has a 1 second precision.
73  * Results in a positive value for timestamps older than the time of query.
74  */
77 
78 typedef struct _StatsCounterGroup StatsCounterGroup;
79 typedef struct _StatsCounterGroupInit StatsCounterGroupInit;
80 
82 {
84  const gchar **counter_names;
85  guint16 capacity;
86  gboolean (*get_type_label)(StatsCounterGroup *self, gint type, StatsClusterLabel *label);
87  void (*free_fn)(StatsCounterGroup *self);
88 };
89 
91 {
92  union
93  {
94  const gchar **names;
95  const gchar *name;
97  void (*init)(StatsCounterGroupInit *self, StatsCounterGroup *counter_group);
98  gboolean (*equals)(const StatsCounterGroupInit *self, const StatsCounterGroupInit *other);
99  void (*clone)(StatsCounterGroupInit *dst, const StatsCounterGroupInit *src);
100  void (*cloned_free)(StatsCounterGroupInit *self);
101 };
102 
103 gboolean stats_counter_group_init_equals(const StatsCounterGroupInit *self, const StatsCounterGroupInit *other);
104 
105 void stats_counter_group_free(StatsCounterGroup *self);
106 
108 {
109  const gchar *name;
110  const gchar *value;
111 };
112 
113 static inline StatsClusterLabel
114 stats_cluster_label(const gchar *name, const gchar *value)
115 {
116  return (StatsClusterLabel)
117  {
118  .name = name, .value = value
119  };
120 }
121 
123 {
124  const gchar *name;
125  StatsClusterLabel *labels;
126  gsize labels_len;
127 
128  struct
129  {
133 
134  struct
135  {
136  const gchar *id;
137  /* syslog-ng component/driver/subsystem that registered this cluster */
138  guint16 component;
139  const gchar *instance;
140  guint set: 1;
142  StatsCounterGroupInit counter_group_init;
143 };
144 
145 /* NOTE: This struct can only be used by the stats implementation and not by client code. */
146 
147 /* StatsCluster encapsulates a set of related counters that are registered
148  * to the same stats source. In a lot of cases, the same stats source uses
149  * multiple counters, so keeping them at the same location makes sense.
150  *
151  * This also improves performance for dynamic counters that relate to
152  * information found in the log stream. In that case multiple counters can
153  * be registered with a single hash lookup */
154 typedef struct _StatsCluster
155 {
156  StatsClusterKey key;
157  StatsCounterGroup counter_group;
158  guint16 use_count;
159  guint16 live_mask;
160  guint16 dynamic: 1;
161  gchar *query_key;
162 } StatsCluster;
163 
164 typedef void (*StatsForeachCounterFunc)(StatsCluster *sc, gint type, StatsCounterItem *counter, gpointer user_data);
165 
166 void stats_cluster_init(void);
167 void stats_cluster_deinit(void);
168 
169 guint stats_register_type(const gchar *type_name);
170 const gchar *stats_cluster_get_type_name(StatsCluster *self, gint type);
171 const gchar *stats_cluster_get_component_name(StatsCluster *self, gchar *buf, gsize buf_len);
172 
173 void stats_cluster_foreach_counter(StatsCluster *self, StatsForeachCounterFunc func, gpointer user_data);
174 
175 StatsClusterKey *stats_cluster_key_clone(StatsClusterKey *dst, const StatsClusterKey *src);
176 void stats_cluster_key_cloned_free(StatsClusterKey *self);
177 void stats_cluster_key_free(StatsClusterKey *self);
178 gboolean stats_cluster_key_equal(const StatsClusterKey *key1, const StatsClusterKey *key2);
179 guint stats_cluster_key_hash(const StatsClusterKey *self);
180 
183 void stats_cluster_untrack_counter(StatsCluster *self, gint type, StatsCounterItem **counter);
184 gboolean stats_cluster_is_alive(StatsCluster *self, gint type);
186 
187 static inline gboolean
188 stats_cluster_is_orphaned(StatsCluster *self)
189 {
190  return self->use_count == 0;
191 }
192 
193 static inline gboolean
194 stats_cluster_get_type_label(StatsCluster *self, gint type, StatsClusterLabel *label)
195 {
196  if (!self->counter_group.get_type_label)
197  return FALSE;
198 
199  return self->counter_group.get_type_label(&self->counter_group, type, label);
200 }
201 
202 StatsCluster *stats_cluster_new(const StatsClusterKey *key);
203 StatsCluster *stats_cluster_dynamic_new(const StatsClusterKey *key);
204 void stats_cluster_free(StatsCluster *self);
205 
206 void stats_cluster_key_set(StatsClusterKey *self, const gchar *name, StatsClusterLabel *labels, gsize labels_len,
207  StatsCounterGroupInit counter_group_ctor);
208 void stats_cluster_key_legacy_set(StatsClusterKey *self, guint16 component, const gchar *id, const gchar *instance,
209  StatsCounterGroupInit counter_group_ctor);
210 void stats_cluster_key_add_legacy_alias(StatsClusterKey *self, guint16 component, const gchar *id,
211  const gchar *instance,
212  StatsCounterGroupInit counter_group_ctor);
213 
214 static inline gboolean
215 stats_cluster_key_is_legacy(const StatsClusterKey *self)
216 {
217  return self->legacy.set;
218 }
219 static inline gboolean
220 stats_cluster_key_legacy_id_equal(const StatsClusterKey *self, const gchar *id)
221 {
222  return g_strcmp0(self->legacy.id, id) == 0;
223 }
224 
225 #endif
const gchar * name
Definition: debugger.c:265
#define self
Definition: rcptid.c:38
const gchar * stats_cluster_get_component_name(StatsCluster *self, gchar *buf, gsize buf_len)
Definition: stats-cluster.c:264
StatsClusterKey * stats_cluster_key_clone(StatsClusterKey *dst, const StatsClusterKey *src)
Definition: stats-cluster.c:140
guint stats_cluster_key_hash(const StatsClusterKey *self)
Definition: stats-cluster.c:319
void stats_cluster_free(StatsCluster *self)
Definition: stats-cluster.c:462
StatsClusterUnit
Definition: stats-cluster.h:50
@ SCU_MINUTES
Definition: stats-cluster.h:54
@ SCU_MILLISECONDS
Definition: stats-cluster.h:56
@ SCU_NANOSECONDS
Definition: stats-cluster.h:57
@ SCU_HOURS
Definition: stats-cluster.h:55
@ SCU_SECONDS
Definition: stats-cluster.h:53
@ SCU_NONE
Definition: stats-cluster.h:51
@ SCU_KIB
Definition: stats-cluster.h:60
@ SCU_BYTES
Definition: stats-cluster.h:59
@ SCU_MIB
Definition: stats-cluster.h:61
@ SCU_GIB
Definition: stats-cluster.h:62
void stats_cluster_key_add_legacy_alias(StatsClusterKey *self, guint16 component, const gchar *id, const gchar *instance, StatsCounterGroupInit counter_group_ctor)
Definition: stats-cluster.c:198
gboolean stats_cluster_key_equal(const StatsClusterKey *key1, const StatsClusterKey *key2)
Definition: stats-cluster.c:300
void stats_cluster_key_legacy_set(StatsClusterKey *self, guint16 component, const gchar *id, const gchar *instance, StatsCounterGroupInit counter_group_ctor)
Definition: stats-cluster.c:187
void stats_cluster_reset_counter_if_needed(StatsCluster *sc, StatsCounterItem *counter)
Definition: stats-cluster.c:391
StatsCluster * stats_cluster_dynamic_new(const StatsClusterKey *key)
Definition: stats-cluster.c:439
void(* StatsForeachCounterFunc)(StatsCluster *sc, gint type, StatsCounterItem *counter, gpointer user_data)
Definition: stats-cluster.h:164
void stats_counter_group_free(StatsCounterGroup *self)
Definition: stats-cluster.c:448
StatsCluster * stats_cluster_new(const StatsClusterKey *key)
Definition: stats-cluster.c:426
void stats_cluster_key_cloned_free(StatsClusterKey *self)
Definition: stats-cluster.c:205
StatsClusterFrameOfReference
Definition: stats-cluster.h:66
@ SCFOR_NONE
Definition: stats-cluster.h:67
@ SCFOR_ABSOLUTE
Definition: stats-cluster.h:68
@ SCFOR_RELATIVE_TO_TIME_OF_QUERY
Definition: stats-cluster.h:75
gboolean stats_counter_group_init_equals(const StatsCounterGroupInit *self, const StatsCounterGroupInit *other)
Definition: stats-cluster.c:285
guint stats_register_type(const gchar *type_name)
Definition: stats-cluster.c:59
@ SCS_CENTER
Definition: stats-cluster.h:37
@ SCS_SENDER
Definition: stats-cluster.h:39
@ SCS_GROUP
Definition: stats-cluster.h:35
@ SCS_FACILITY
Definition: stats-cluster.h:42
@ SCS_TAG
Definition: stats-cluster.h:43
@ SCS_PARSER
Definition: stats-cluster.h:45
@ SCS_SEVERITY
Definition: stats-cluster.h:41
@ SCS_HOST
Definition: stats-cluster.h:38
@ SCS_SOURCE_MASK
Definition: stats-cluster.h:46
@ SCS_DESTINATION
Definition: stats-cluster.h:34
@ SCS_GLOBAL
Definition: stats-cluster.h:36
@ SCS_SOURCE
Definition: stats-cluster.h:33
@ SCS_FILTER
Definition: stats-cluster.h:44
@ SCS_PROGRAM
Definition: stats-cluster.h:40
void stats_cluster_key_set(StatsClusterKey *self, const gchar *name, StatsClusterLabel *labels, gsize labels_len, StatsCounterGroupInit counter_group_ctor)
Definition: stats-cluster.c:162
void stats_cluster_foreach_counter(StatsCluster *self, StatsForeachCounterFunc func, gpointer user_data)
Definition: stats-cluster.c:225
void stats_cluster_key_free(StatsClusterKey *self)
Definition: stats-cluster.c:218
StatsCounterItem * stats_cluster_get_counter(StatsCluster *self, gint type)
Definition: stats-cluster.c:340
const gchar * stats_cluster_get_type_name(StatsCluster *self, gint type)
Definition: stats-cluster.c:238
gboolean stats_cluster_is_alive(StatsCluster *self, gint type)
Definition: stats-cluster.c:418
StatsCounterItem * stats_cluster_track_counter(StatsCluster *self, gint type)
Definition: stats-cluster.c:328
void stats_cluster_untrack_counter(StatsCluster *self, gint type, StatsCounterItem **counter)
Definition: stats-cluster.c:353
void stats_cluster_init(void)
Definition: stats-cluster.c:33
void stats_cluster_deinit(void)
Definition: stats-cluster.c:75
Definition: stats-cluster.h:155
guint16 live_mask
Definition: stats-cluster.h:159
guint16 use_count
Definition: stats-cluster.h:158
guint16 dynamic
Definition: stats-cluster.h:160
StatsClusterKey key
Definition: stats-cluster.h:156
gchar * query_key
Definition: stats-cluster.h:161
StatsCounterGroup counter_group
Definition: stats-cluster.h:157
Definition: stats-counter.h:67
Definition: stats-cluster.h:123
StatsCounterGroupInit counter_group_init
Definition: stats-cluster.h:142
guint16 component
Definition: stats-cluster.h:138
const gchar * id
Definition: stats-cluster.h:136
struct _StatsClusterKey::@83 formatting
guint set
Definition: stats-cluster.h:140
const gchar * instance
Definition: stats-cluster.h:139
gsize labels_len
Definition: stats-cluster.h:126
const gchar * name
Definition: stats-cluster.h:124
StatsClusterFrameOfReference frame_of_reference
Definition: stats-cluster.h:131
struct _StatsClusterKey::@84 legacy
StatsClusterLabel * labels
Definition: stats-cluster.h:125
StatsClusterUnit stored_unit
Definition: stats-cluster.h:130
Definition: stats-cluster.h:108
const gchar * name
Definition: stats-cluster.h:109
const gchar * value
Definition: stats-cluster.h:110
Definition: stats-cluster.h:91
void(* clone)(StatsCounterGroupInit *dst, const StatsCounterGroupInit *src)
Definition: stats-cluster.h:99
union _StatsCounterGroupInit::@82 counter
void(* init)(StatsCounterGroupInit *self, StatsCounterGroup *counter_group)
Definition: stats-cluster.h:97
void(* cloned_free)(StatsCounterGroupInit *self)
Definition: stats-cluster.h:100
const gchar * name
Definition: stats-cluster.h:95
const gchar ** names
Definition: stats-cluster.h:94
gboolean(* equals)(const StatsCounterGroupInit *self, const StatsCounterGroupInit *other)
Definition: stats-cluster.h:98
Definition: stats-cluster.h:82
StatsCounterItem * counters
Definition: stats-cluster.h:83
gboolean(* get_type_label)(StatsCounterGroup *self, gint type, StatsClusterLabel *label)
Definition: stats-cluster.h:86
void(* free_fn)(StatsCounterGroup *self)
Definition: stats-cluster.h:87
const gchar ** counter_names
Definition: stats-cluster.h:84
guint16 capacity
Definition: stats-cluster.h:85
GString * value
Definition: test_decode.c:28
struct tm key
Definition: cache.c:63