File: correlation.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (201 lines) | stat: -rw-r--r-- 5,864 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (c) 2002-2013, 2015 Balabit
 * Copyright (c) 1998-2013, 2015 Balázs Scheidler
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; 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 "correlation.h"
#include "correlation-key.h"
#include "correlation-context.h"
#include "timeutils/cache.h"
#include "timeutils/misc.h"

void
correlation_state_tx_begin(CorrelationState *self)
{
  g_mutex_lock(&self->lock);
}

void
correlation_state_tx_end(CorrelationState *self)
{
  g_mutex_unlock(&self->lock);
}

CorrelationContext *
correlation_state_tx_lookup_context(CorrelationState *self, const CorrelationKey *key)
{
  return g_hash_table_lookup(self->state, key);
}

void
correlation_state_tx_store_context(CorrelationState *self, CorrelationContext *context, gint timeout)
{
  g_assert(context->timer == NULL);

  g_hash_table_insert(self->state, &context->key, context);
  context->timer = timer_wheel_add_timer(self->timer_wheel, timeout, self->expire_callback,
                                         correlation_context_ref(context), (GDestroyNotify) correlation_context_unref);
}

void
correlation_state_tx_remove_context(CorrelationState *self, CorrelationContext *context)
{
  /* NOTE: in expire callbacks our timer is already deleted and thus it is
   * set to NULL in which case we don't need to remove it again.  */

  if (context->timer)
    timer_wheel_del_timer(self->timer_wheel, context->timer);
  g_hash_table_remove(self->state, &context->key);
}

void
correlation_state_tx_update_context(CorrelationState *self, CorrelationContext *context, gint timeout)
{
  g_assert(context->timer != NULL);

  timer_wheel_mod_timer(self->timer_wheel, context->timer, timeout);
}

void
correlation_state_expire_all(CorrelationState *self, gpointer caller_context)
{
  g_mutex_lock(&self->lock);
  timer_wheel_expire_all(self->timer_wheel, caller_context);
  g_mutex_unlock(&self->lock);
}

void
correlation_state_advance_time(CorrelationState *self, gint timeout, gpointer caller_context)
{
  guint64  new_time;

  g_mutex_lock(&self->lock);
  new_time = timer_wheel_get_time(self->timer_wheel) + timeout;
  timer_wheel_set_time(self->timer_wheel, new_time, caller_context);
  g_mutex_unlock(&self->lock);
}

void
correlation_state_set_time(CorrelationState *self, guint64 sec, gpointer caller_context)
{
  struct timespec now;

  /* clamp the current time between the timestamp of the current message
   * (low limit) and the current system time (high limit).  This ensures
   * that incorrect clocks do not skew the current time know by the
   * correlation engine too much. */

  get_cached_realtime(&now);
  self->last_tick = now;

  if (sec < now.tv_sec)
    now.tv_sec = sec;

  g_mutex_lock(&self->lock);
  timer_wheel_set_time(self->timer_wheel, now.tv_sec, caller_context);
  g_mutex_unlock(&self->lock);
}

guint64
correlation_state_get_time(CorrelationState *self)
{
  return timer_wheel_get_time(self->timer_wheel);
}

gboolean
correlation_state_timer_tick(CorrelationState *self, gpointer caller_context)
{
  struct timespec now;
  glong diff;
  gboolean updated = FALSE;

  g_mutex_lock(&self->lock);
  get_cached_realtime(&now);
  diff = timespec_diff_usec(&now, &self->last_tick);

  if (diff > 1e6)
    {
      glong diff_sec = (glong)(diff / 1e6);

      timer_wheel_set_time(self->timer_wheel, timer_wheel_get_time(self->timer_wheel) + diff_sec, caller_context);
      /* update last_tick, take the fraction of the seconds not calculated into this update into account */

      self->last_tick = now;
      timespec_add_usec(&self->last_tick, - (glong)(diff - diff_sec * 1e6));
      updated = TRUE;
    }
  else if (diff < 0)
    {
      /* time moving backwards, this can only happen if the computer's time
       * is changed.  We don't update patterndb's idea of the time now, wait
       * another tick instead to update that instead.
       */
      self->last_tick = now;
    }
  g_mutex_unlock(&self->lock);
  return updated;
}


CorrelationState *
correlation_state_new(TWCallbackFunc expire_callback)
{
  CorrelationState *self = g_new0(CorrelationState, 1);

  g_mutex_init(&self->lock);
  self->state = g_hash_table_new_full(correlation_key_hash, correlation_key_equal, NULL,
                                      (GDestroyNotify) correlation_context_unref);
  self->timer_wheel = timer_wheel_new();
  get_cached_realtime(&self->last_tick);
  g_atomic_counter_set(&self->ref_cnt, 1);
  self->expire_callback = expire_callback;
  return self;
}

void
_free(CorrelationState *self)
{
  if (self->state)
    g_hash_table_destroy(self->state);
  timer_wheel_free(self->timer_wheel);
  g_mutex_clear(&self->lock);
  g_free(self);
}

CorrelationState *
correlation_state_ref(CorrelationState *self)
{
  g_assert(!self || g_atomic_counter_get(&self->ref_cnt) > 0);

  if (self)
    {
      g_atomic_counter_inc(&self->ref_cnt);
    }
  return self;
}

void
correlation_state_unref(CorrelationState *self)
{
  g_assert(!self || g_atomic_counter_get(&self->ref_cnt));

  if (self && (g_atomic_counter_dec_and_test(&self->ref_cnt)))
    _free(self);
}