syslog-ng source
stats-counter.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002-2013 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_COUNTER_H_INCLUDED
25 #define STATS_COUNTER_H_INCLUDED 1
26 
27 #include "syslog-ng.h"
28 #include "atomic-gssize.h"
29 
30 /* FIXME: Had to turn these checkers off as multiple test cases show we still have
31  * counter under/overflows. We need to fix the root cause of those issues and
32  * turn these checks back on ASAP.
33  * https://github.com/syslog-ng/syslog-ng/issues/5528
34  */
35 #if ! defined(USE_CHECKED_COUNTER_OPS)
36 # define USE_CHECKED_COUNTER_OPS 0
37 #endif
38 #if ! defined(CHECKED_COUNTER_ADD)
39 # if SYSLOG_NG_ENABLE_DEBUG && USE_CHECKED_COUNTER_OPS
40 #define CHECKED_COUNTER_ADD(old_value_expr, add) \
41  do { \
42  gssize _old = (old_value_expr); \
43  gssize _add = (add); \
44  g_assert((_old <= G_MAXSSIZE - _add) && "stats counter add overflow!"); \
45  } while (0)
46 # else
47 # define CHECKED_COUNTER_ADD(old_value_expr, add) ((void)(old_value_expr))
48 # endif
49 #endif
50 
51 #if ! defined(CHECKED_COUNTER_SUB)
52 # if SYSLOG_NG_ENABLE_DEBUG && USE_CHECKED_COUNTER_OPS
53 #define CHECKED_COUNTER_SUB(old_value_expr, sub) \
54  do { \
55  gssize _old = (old_value_expr); \
56  gssize _sub = (sub); \
57  g_assert((_old >= _sub) && "stats counter sub underflow!"); \
58  } while (0)
59 # else
60 # define CHECKED_COUNTER_SUB(old_value_expr, sub) ((void)(old_value_expr))
61 # endif
62 #endif
63 
64 #define STATS_COUNTER_MAX_VALUE G_MAXSIZE
65 
66 typedef struct _StatsCounterItem
67 {
68  union
69  {
72  };
73  gchar *name;
74  gint type;
75  gboolean external;
77 
78 
79 static gboolean
80 stats_counter_read_only(StatsCounterItem *counter)
81 {
82  return counter->external;
83 }
84 
85 static inline void
86 stats_counter_add(StatsCounterItem *counter, gssize add)
87 {
88  if (counter)
89  {
90  g_assert(!stats_counter_read_only(counter));
91  CHECKED_COUNTER_ADD(atomic_gssize_add(&counter->value, add), add);
92  }
93 }
94 
95 static inline void
96 stats_counter_sub(StatsCounterItem *counter, gssize sub)
97 {
98  if (counter)
99  {
100  g_assert(!stats_counter_read_only(counter));
101  CHECKED_COUNTER_SUB(atomic_gssize_sub(&counter->value, sub), sub);
102  }
103 }
104 
105 static inline void
106 stats_counter_inc(StatsCounterItem *counter)
107 {
108  if (counter)
109  {
110  g_assert(!stats_counter_read_only(counter));
111  CHECKED_COUNTER_ADD(atomic_gssize_inc(&counter->value), 1);
112  }
113 }
114 
115 static inline void
116 stats_counter_dec(StatsCounterItem *counter)
117 {
118  if (counter)
119  {
120  g_assert(!stats_counter_read_only(counter));
121  CHECKED_COUNTER_SUB(atomic_gssize_dec(&counter->value), 1);
122  }
123 }
124 
125 static inline void
126 stats_counter_set(StatsCounterItem *counter, gsize value)
127 {
128  if (counter && !stats_counter_read_only(counter))
129  {
130  atomic_gssize_set(&counter->value, value);
131  }
132 }
133 
134 static inline gsize
135 stats_counter_get(StatsCounterItem *counter)
136 {
137  gsize result = 0;
138 
139  if (counter)
140  {
141  if (!counter->external)
142  result = atomic_gssize_get_unsigned(&counter->value);
143  else
144  result = atomic_gssize_get_unsigned(counter->value_ref);
145  }
146  return result;
147 }
148 
149 /* Can only store positive values. Fixes overflow on 32 bit machines until 2106 if using seconds. */
150 static inline void
151 stats_counter_set_time(StatsCounterItem *counter, gint64 value)
152 {
153  stats_counter_set(counter, (gsize) MAX(0, value));
154 }
155 
156 static inline gchar *
157 stats_counter_get_name(StatsCounterItem *counter)
158 {
159  if (counter)
160  return counter->name;
161  return NULL;
162 }
163 
164 static inline void
165 stats_counter_clear(StatsCounterItem *counter)
166 {
167  g_free(counter->name);
168  memset(counter, 0, sizeof(*counter));
169 }
170 
171 #endif
#define CHECKED_COUNTER_SUB(old_value_expr, sub)
Definition: stats-counter.h:60
#define CHECKED_COUNTER_ADD(old_value_expr, add)
Definition: stats-counter.h:47
Definition: stats-counter.h:67
gchar * name
Definition: stats-counter.h:73
atomic_gssize value
Definition: stats-counter.h:70
atomic_gssize * value_ref
Definition: stats-counter.h:71
gint type
Definition: stats-counter.h:74
gboolean external
Definition: stats-counter.h:75
Definition: atomic-gssize.h:33
GString * value
Definition: test_decode.c:28
GString * result
Definition: test_lexer_block.c:34