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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
/*
* Copyright (c) 2002-2011 Balabit
* Copyright (c) 1998-2011 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 "logqueue.h"
#include "stats/stats-registry.h"
#include "stats/stats-cluster-single.h"
#include "messages.h"
#include "timeutils/misc.h"
void
log_queue_memory_usage_add(LogQueue *self, gsize value)
{
stats_counter_add(self->metrics.shared.memory_usage, value);
stats_counter_add(self->metrics.owned.memory_usage, value);
}
void
log_queue_memory_usage_sub(LogQueue *self, gsize value)
{
stats_counter_sub(self->metrics.shared.memory_usage, value);
stats_counter_sub(self->metrics.owned.memory_usage, value);
}
void
log_queue_queued_messages_add(LogQueue *self, gsize value)
{
stats_counter_add(self->metrics.shared.queued_messages, value);
stats_counter_add(self->metrics.owned.queued_messages, value);
}
void
log_queue_queued_messages_sub(LogQueue *self, gsize value)
{
stats_counter_sub(self->metrics.shared.queued_messages, value);
stats_counter_sub(self->metrics.owned.queued_messages, value);
}
void
log_queue_queued_messages_inc(LogQueue *self)
{
stats_counter_inc(self->metrics.shared.queued_messages);
stats_counter_inc(self->metrics.owned.queued_messages);
}
void
log_queue_queued_messages_dec(LogQueue *self)
{
stats_counter_dec(self->metrics.shared.queued_messages);
stats_counter_dec(self->metrics.owned.queued_messages);
}
void
log_queue_queued_messages_reset(LogQueue *self)
{
stats_counter_sub(self->metrics.shared.queued_messages,
stats_counter_get(self->metrics.owned.queued_messages));
stats_counter_set(self->metrics.owned.queued_messages, log_queue_get_length(self));
stats_counter_add(self->metrics.shared.queued_messages,
stats_counter_get(self->metrics.owned.queued_messages));
}
void
log_queue_dropped_messages_inc(LogQueue *self)
{
stats_counter_inc(self->metrics.shared.dropped_messages);
}
/*
* When this is called, it is assumed that the output thread is currently
* not running (since this is the function that wakes it up), thus we can
* access the length of the output queue without acquiring a lock. Memory
* barriers are covered by the use of the self->lock mutex, since
* push_notify is registered under the protection of self->lock and after
* that the length of the output queue will not change (since parallel_push
* is only registered if it has less than enough items).
*
* NOTE: self->lock must have been acquired before calling this function.
*/
void
log_queue_push_notify(LogQueue *self)
{
if (self->parallel_push_notify)
{
/* make sure the callback can call log_queue_check_items() again */
GDestroyNotify destroy = self->parallel_push_data_destroy;
gpointer user_data = self->parallel_push_data;
LogQueuePushNotifyFunc func = self->parallel_push_notify;
self->parallel_push_data = NULL;
self->parallel_push_data_destroy = NULL;
self->parallel_push_notify = NULL;
g_mutex_unlock(&self->lock);
func(user_data);
if (destroy && user_data)
destroy(user_data);
g_mutex_lock(&self->lock);
}
}
void
log_queue_reset_parallel_push(LogQueue *self)
{
g_mutex_lock(&self->lock);
self->parallel_push_notify = NULL;
self->parallel_push_data = NULL;
g_mutex_unlock(&self->lock);
}
void
log_queue_set_parallel_push(LogQueue *self, LogQueuePushNotifyFunc parallel_push_notify, gpointer user_data,
GDestroyNotify user_data_destroy)
{
g_mutex_lock(&self->lock);
self->parallel_push_notify = parallel_push_notify;
self->parallel_push_data = user_data;
self->parallel_push_data_destroy = user_data_destroy;
g_mutex_unlock(&self->lock);
}
/*
*
* @batch_items: the number of items processed in a batch (e.g. the number of items the consumer is preferred to process at a single invocation)
* @partial_batch: true is returned if some elements (but less than batch_items) are already buffered
* @timeout: the number of milliseconds that the consumer needs to wait before we can possibly proceed
*/
gboolean
log_queue_check_items(LogQueue *self, gint *timeout, LogQueuePushNotifyFunc parallel_push_notify, gpointer user_data,
GDestroyNotify user_data_destroy)
{
gint64 num_elements;
g_mutex_lock(&self->lock);
/* drop reference to the previous callback/userdata */
if (self->parallel_push_data && self->parallel_push_data_destroy)
self->parallel_push_data_destroy(self->parallel_push_data);
num_elements = log_queue_get_length(self);
if (num_elements == 0)
{
self->parallel_push_notify = parallel_push_notify;
self->parallel_push_data = user_data;
self->parallel_push_data_destroy = user_data_destroy;
g_mutex_unlock(&self->lock);
return FALSE;
}
/* consume the user_data reference as we won't use the callback */
if (user_data && user_data_destroy)
user_data_destroy(user_data);
self->parallel_push_notify = NULL;
self->parallel_push_data = NULL;
g_mutex_unlock(&self->lock);
/* recalculate buckets, throttle is only running in the output thread, no need to lock it. */
if (self->throttle > 0)
{
gint64 diff;
gint new_buckets;
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
/* throttling is enabled, calculate new buckets */
if (self->last_throttle_check.tv_sec != 0)
{
diff = timespec_diff_nsec(&now, &self->last_throttle_check);
}
else
{
diff = 0;
self->last_throttle_check = now;
}
new_buckets = (self->throttle * diff) / NSEC_PER_SEC;
if (new_buckets)
{
/* if new_buckets is zero, we don't save the current time as
* last_throttle_check. The reason is that new_buckets could be
* rounded to zero when only a minimal interval passes between
* poll iterations.
*/
self->throttle_buckets = MIN(self->throttle, self->throttle_buckets + new_buckets);
self->last_throttle_check = now;
}
if (num_elements && self->throttle_buckets == 0)
{
if (timeout)
{
/* we are unable to send because of throttling, make sure that we
* wake up when the rate limits lets us send at least 1 message */
*timeout = (1000 / self->throttle) + 1;
msg_debug("Throttling output",
evt_tag_int("wait", *timeout));
}
return FALSE;
}
}
return TRUE;
}
static void
_register_shared_counters(LogQueue *self, gint stats_level, StatsClusterKeyBuilder *builder)
{
if (!builder)
return;
stats_cluster_key_builder_push(builder);
{
stats_cluster_key_builder_set_name(builder, "output_events_total");
self->metrics.shared.output_events_sc_key = stats_cluster_key_builder_build_logpipe(builder);
}
stats_cluster_key_builder_pop(builder);
stats_cluster_key_builder_push(builder);
{
stats_cluster_key_builder_set_legacy_alias(builder,
self->metrics.shared.output_events_sc_key->legacy.component,
self->metrics.shared.output_events_sc_key->legacy.id,
self->metrics.shared.output_events_sc_key->legacy.instance);
stats_cluster_key_builder_set_legacy_alias_name(builder, "memory_usage");
self->metrics.shared.memory_usage_sc_key = stats_cluster_key_builder_build_single(builder);
}
stats_cluster_key_builder_pop(builder);
stats_lock();
{
stats_register_counter(stats_level, self->metrics.shared.output_events_sc_key, SC_TYPE_QUEUED,
&self->metrics.shared.queued_messages);
stats_register_counter(stats_level, self->metrics.shared.output_events_sc_key, SC_TYPE_DROPPED,
&self->metrics.shared.dropped_messages);
stats_register_counter(stats_level, self->metrics.shared.memory_usage_sc_key, SC_TYPE_SINGLE_VALUE,
&self->metrics.shared.memory_usage);
}
stats_unlock();
}
static void
_register_owned_counters(LogQueue *self, gint stats_level, StatsClusterKeyBuilder *builder)
{
if (!builder)
return;
stats_cluster_key_builder_push(builder);
{
stats_cluster_key_builder_set_name(builder, "events");
self->metrics.owned.events_sc_key = stats_cluster_key_builder_build_single(builder);
stats_cluster_key_builder_set_name(builder, "memory_usage_bytes");
self->metrics.owned.memory_usage_sc_key = stats_cluster_key_builder_build_single(builder);
}
stats_cluster_key_builder_pop(builder);
stats_lock();
{
stats_register_counter(stats_level, self->metrics.owned.events_sc_key, SC_TYPE_SINGLE_VALUE,
&self->metrics.owned.queued_messages);
stats_register_counter(stats_level, self->metrics.owned.memory_usage_sc_key, SC_TYPE_SINGLE_VALUE,
&self->metrics.owned.memory_usage);
}
stats_unlock();
}
static void
_register_counters(LogQueue *self, gint stats_level, StatsClusterKeyBuilder *driver_sck_builder,
StatsClusterKeyBuilder *queue_sck_builder)
{
g_assert(!driver_sck_builder || queue_sck_builder);
_register_shared_counters(self, stats_level, driver_sck_builder);
_register_owned_counters(self, stats_level, queue_sck_builder);
}
static void
_unregister_shared_counters(LogQueue *self)
{
stats_lock();
{
if (self->metrics.shared.output_events_sc_key)
{
log_queue_queued_messages_sub(self, stats_counter_get(self->metrics.owned.queued_messages));
stats_unregister_counter(self->metrics.shared.output_events_sc_key, SC_TYPE_QUEUED,
&self->metrics.shared.queued_messages);
stats_unregister_counter(self->metrics.shared.output_events_sc_key, SC_TYPE_DROPPED,
&self->metrics.shared.dropped_messages);
stats_cluster_key_free(self->metrics.shared.output_events_sc_key);
}
if (self->metrics.shared.memory_usage_sc_key)
{
log_queue_memory_usage_sub(self, stats_counter_get(self->metrics.owned.memory_usage));
stats_unregister_counter(self->metrics.shared.memory_usage_sc_key, SC_TYPE_SINGLE_VALUE,
&self->metrics.shared.memory_usage);
stats_cluster_key_free(self->metrics.shared.memory_usage_sc_key);
}
}
stats_unlock();
}
static void
_unregister_owned_counters(LogQueue *self)
{
stats_lock();
{
if (self->metrics.owned.events_sc_key)
{
stats_unregister_counter(self->metrics.owned.events_sc_key, SC_TYPE_SINGLE_VALUE,
&self->metrics.owned.queued_messages);
stats_cluster_key_free(self->metrics.owned.events_sc_key);
}
if (self->metrics.owned.memory_usage_sc_key)
{
stats_unregister_counter(self->metrics.owned.memory_usage_sc_key, SC_TYPE_SINGLE_VALUE,
&self->metrics.owned.memory_usage);
stats_cluster_key_free(self->metrics.owned.memory_usage_sc_key);
}
}
stats_unlock();
}
static void
_unregister_counters(LogQueue *self)
{
_unregister_shared_counters(self);
_unregister_owned_counters(self);
}
void
log_queue_init_instance(LogQueue *self, const gchar *persist_name, gint stats_level,
StatsClusterKeyBuilder *driver_sck_builder, StatsClusterKeyBuilder *queue_sck_builder)
{
g_atomic_counter_set(&self->ref_cnt, 1);
self->free_fn = log_queue_free_method;
self->persist_name = persist_name ? g_strdup(persist_name) : NULL;
g_mutex_init(&self->lock);
_register_counters(self, stats_level, driver_sck_builder, queue_sck_builder);
}
void
log_queue_free_method(LogQueue *self)
{
_unregister_counters(self);
g_mutex_clear(&self->lock);
g_free(self->persist_name);
g_free(self);
}
|