Skip to content

Commit

Permalink
scratch-buffers: Support any number of stacks
Browse files Browse the repository at this point in the history
Instead of being tied to GStrings only, support registering new kinds
of stacks, which will be automatiaclly free'd on thread shutdown. For
convenience, wrappers are provided for GString-based scratch buffer
acquire and release, and that stack is automatically registered and
free'd by the library itself.

This fixes elastic#2.

Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
Signed-off-by: Balazs Scheidler <bazsi@balabit.hu>
  • Loading branch information
bazsi committed Feb 16, 2013
1 parent 945c11d commit 4b462ce
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 96 deletions.
3 changes: 2 additions & 1 deletion lib/mainloop.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2012 BalaBit IT Ltd, Budapest, Hungary
* Copyright (c) 2002-2013 BalaBit IT Ltd, Budapest, Hungary
* Copyright (c) 1998-2012 Balázs Scheidler
*
* This library is free software; you can redistribute it and/or
Expand Down Expand Up @@ -292,6 +292,7 @@ main_loop_io_worker_thread_start(void *cookie)
gint id;

dns_cache_init();
scratch_buffers_init();
g_static_mutex_lock(&main_loop_io_workers_idmap_lock);
/* NOTE: this algorithm limits the number of I/O worker threads to 64,
* since the ID map is stored in a single 64 bit integer. If we ever need
Expand Down
80 changes: 62 additions & 18 deletions lib/scratch-buffers.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2011-2012 BalaBit IT Ltd, Budapest, Hungary
* Copyright (c) 2011-2012 Gergely Nagy <algernon@balabit.hu>
* Copyright (c) 2011-2013 BalaBit IT Ltd, Budapest, Hungary
* Copyright (c) 2011-2013 Gergely Nagy <algernon@balabit.hu>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -28,42 +28,86 @@

TLS_BLOCK_START
{
GTrashStack *scratch_buffers;
GTrashStack *sb_gstrings;
GList *sb_registry;
}
TLS_BLOCK_END;

#define local_scratch_buffers __tls_deref(scratch_buffers)
/* GStrings */

ScratchBuffer *
scratch_buffer_acquire(void)
#define local_sb_gstrings __tls_deref(sb_gstrings)

GTrashStack *
sb_gstring_acquire_buffer(void)
{
ScratchBuffer *sb;
SBGString *sb;

sb = g_trash_stack_pop(&local_scratch_buffers);
sb = g_trash_stack_pop(&local_sb_gstrings);
if (!sb)
{
sb = g_new(ScratchBuffer, 1);
g_string_steal(sb_string(sb));
sb = g_new(SBGString, 1);
g_string_steal(sb_gstring_string(sb));
}
else
g_string_set_size(sb_string(sb), 0);
return sb;
g_string_set_size(sb_gstring_string(sb), 0);

return (GTrashStack *) sb;
}

void
scratch_buffer_release(ScratchBuffer *sb)
sb_gstring_release_buffer(GTrashStack *s)
{
g_trash_stack_push(&local_scratch_buffers, sb);
SBGString *sb = (SBGString *) s;

g_trash_stack_push(&local_sb_gstrings, sb);
}

void
scratch_buffers_free(void)
sb_gstring_free_stack(void)
{
ScratchBuffer *sb;
SBGString *sb;

while ((sb = g_trash_stack_pop(&local_scratch_buffers)) != NULL)
while ((sb = g_trash_stack_pop(&local_sb_gstrings)) != NULL)
{
g_free(sb_string(sb)->str);
g_free(sb_gstring_string(sb)->str);
g_free(sb);
}
}

ScratchBufferStack SBGStringStack = {
.acquire_buffer = sb_gstring_acquire_buffer,
.release_buffer = sb_gstring_release_buffer,
.free_stack = sb_gstring_free_stack
};

/* Global API */

#define local_sb_registry __tls_deref(sb_registry)

void
scratch_buffers_register(ScratchBufferStack *stack)
{
local_sb_registry = g_list_append(local_sb_registry, stack);
}

void
scratch_buffers_init(void)
{
local_sb_registry = NULL;
scratch_buffers_register(&SBGStringStack);
}

static void
scratch_buffers_free_stack(gpointer data, gpointer user_data)
{
ScratchBufferStack *s = (ScratchBufferStack *) data;

s->free_stack();
}

void
scratch_buffers_free(void)
{
g_list_foreach(local_sb_registry, scratch_buffers_free_stack, NULL);
g_list_free(local_sb_registry);
}
41 changes: 34 additions & 7 deletions lib/scratch-buffers.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2011 BalaBit IT Ltd, Budapest, Hungary
* Copyright (c) 2011 Gergely Nagy <algernon@balabit.hu>
* Copyright (c) 2011-2013 BalaBit IT Ltd, Budapest, Hungary
* Copyright (c) 2011-2013 Gergely Nagy <algernon@balabit.hu>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand All @@ -27,17 +27,44 @@

#include <glib.h>

/* Global API */

typedef struct
{
GTrashStack *(*acquire_buffer)(void);
void (*release_buffer)(GTrashStack *stack);
void (*free_stack)(void);
} ScratchBufferStack;

static inline GTrashStack *
scratch_buffer_acquire(ScratchBufferStack *stack)
{
return stack->acquire_buffer();
}

static inline void
scratch_buffer_release(ScratchBufferStack *stack, GTrashStack *buffer)
{
stack->release_buffer(buffer);
}

void scratch_buffers_register(ScratchBufferStack *stack);
void scratch_buffers_init(void);
void scratch_buffers_free(void);

/* GStrings */

typedef struct
{
GTrashStack stackp;
GString s;
} ScratchBuffer;
} SBGString;

ScratchBuffer *scratch_buffer_acquire(void);
void scratch_buffer_release(ScratchBuffer *sb);
extern ScratchBufferStack SBGStringStack;

#define sb_string(buffer) (&buffer->s)
#define sb_gstring_acquire() ((SBGString *)scratch_buffer_acquire(&SBGStringStack))
#define sb_gstring_release(b) (scratch_buffer_release(&SBGStringStack, (GTrashStack *)b))

void scratch_buffers_free(void);
#define sb_gstring_string(buffer) (&buffer->s)

#endif
34 changes: 18 additions & 16 deletions lib/value-pairs.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2011-2012 BalaBit IT Ltd, Budapest, Hungary
* Copyright (c) 2011-2012 Gergely Nagy <algernon@balabit.hu>
* Copyright (c) 2011-2013 BalaBit IT Ltd, Budapest, Hungary
* Copyright (c) 2011-2013 Gergely Nagy <algernon@balabit.hu>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -193,22 +193,22 @@ vp_pairs_foreach(gpointer data, gpointer user_data)
LogMessage *msg = ((gpointer *)user_data)[2];
gint32 seq_num = GPOINTER_TO_INT (((gpointer *)user_data)[3]);
GTree *scope_set = ((gpointer *)user_data)[5];
ScratchBuffer *sb = scratch_buffer_acquire();
SBGString *sb = sb_gstring_acquire();
VPPairConf *vpc = (VPPairConf *)data;

log_template_format((LogTemplate *)vpc->template, msg, NULL, LTZ_LOCAL,
seq_num, NULL, sb_string(sb));
seq_num, NULL, sb_gstring_string(sb));

if (!sb_string(sb)->str[0])
if (!sb_gstring_string(sb)->str[0])
{
scratch_buffer_release(sb);
sb_gstring_release(sb);
return;
}

g_tree_insert(scope_set, vp_transform_apply(vp, vpc->name),
sb_string(sb)->str);
g_string_steal(sb_string(sb));
scratch_buffer_release(sb);
sb_gstring_string(sb)->str);
g_string_steal(sb_gstring_string(sb));
sb_gstring_release(sb);
}

/* runs over the LogMessage nv-pairs, and inserts them unless excluded */
Expand Down Expand Up @@ -246,7 +246,7 @@ static void
vp_merge_set(ValuePairs *vp, LogMessage *msg, gint32 seq_num, ValuePairSpec *set, GTree *dest)
{
gint i;
ScratchBuffer *sb = scratch_buffer_acquire();
SBGString *sb = sb_gstring_acquire();

for (i = 0; set[i].name; i++)
{
Expand All @@ -265,28 +265,30 @@ vp_merge_set(ValuePairs *vp, LogMessage *msg, gint32 seq_num, ValuePairSpec *set
switch (set[i].type)
{
case VPT_MACRO:
log_macro_expand(sb_string(sb), set[i].id, FALSE, NULL, LTZ_LOCAL, seq_num, NULL, msg);
log_macro_expand(sb_gstring_string(sb), set[i].id, FALSE,
NULL, LTZ_LOCAL, seq_num, NULL, msg);
break;
case VPT_NVPAIR:
{
const gchar *nv;
gssize len;

nv = log_msg_get_value(msg, (NVHandle) set[i].id, &len);
g_string_append_len(sb_string(sb), nv, len);
g_string_append_len(sb_gstring_string(sb), nv, len);
break;
}
default:
g_assert_not_reached();
}

if (!sb_string(sb)->str[0])
if (!sb_gstring_string(sb)->str[0])
continue;

g_tree_insert(dest, vp_transform_apply(vp, set[i].name), sb_string(sb)->str);
g_string_steal(sb_string(sb));
g_tree_insert(dest, vp_transform_apply(vp, set[i].name),
sb_gstring_string(sb)->str);
g_string_steal(sb_gstring_string(sb));
}
scratch_buffer_release(sb);
sb_gstring_release(sb);
}

void
Expand Down
38 changes: 20 additions & 18 deletions lib/vptransform.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2011-2012 BalaBit IT Ltd, Budapest, Hungary
* Copyright (c) 2011-2012 Gergely Nagy <algernon@balabit.hu>
* Copyright (c) 2011-2013 BalaBit IT Ltd, Budapest, Hungary
* Copyright (c) 2011-2013 Gergely Nagy <algernon@balabit.hu>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -31,7 +31,7 @@

#include <string.h>

typedef void (*VPTransFunc)(ValuePairsTransform *t, ScratchBuffer *name);
typedef void (*VPTransFunc)(ValuePairsTransform *t, SBGString *name);
typedef void (*VPTransDestroyFunc)(ValuePairsTransform *t);

struct _ValuePairsTransformSet
Expand Down Expand Up @@ -92,19 +92,19 @@ value_pairs_transform_free(ValuePairsTransform *t)
}

static inline void
value_pairs_transform_apply(ValuePairsTransform *t, ScratchBuffer *key)
value_pairs_transform_apply(ValuePairsTransform *t, SBGString *key)
{
t->transform(t, key);
}

/* add_prefix() */

static void
vp_trans_add_prefix(ValuePairsTransform *t, ScratchBuffer *key)
vp_trans_add_prefix(ValuePairsTransform *t, SBGString *key)
{
VPTransAddPrefix *self = (VPTransAddPrefix *)t;

g_string_prepend(sb_string(key), self->prefix);
g_string_prepend(sb_gstring_string(key), self->prefix);
}

static void
Expand Down Expand Up @@ -132,11 +132,11 @@ value_pairs_new_transform_add_prefix (const gchar *prefix)
/* shift() */

static void
vp_trans_shift(ValuePairsTransform *t, ScratchBuffer* key)
vp_trans_shift(ValuePairsTransform *t, SBGString* key)
{
VPTransShift *self = (VPTransShift *)t;

g_string_erase(sb_string(key), 0, self->amount);
g_string_erase(sb_gstring_string(key), 0, self->amount);
}

ValuePairsTransform *
Expand All @@ -156,15 +156,17 @@ value_pairs_new_transform_shift (gint amount)
/* replace() */

static void
vp_trans_replace(ValuePairsTransform *t, ScratchBuffer *key)
vp_trans_replace(ValuePairsTransform *t, SBGString *key)
{
VPTransReplace *self = (VPTransReplace *)t;

if (strncmp(self->old_prefix, sb_string(key)->str, self->old_prefix_len) != 0)
if (strncmp(self->old_prefix, sb_gstring_string(key)->str,
self->old_prefix_len) != 0)
return;

g_string_erase(sb_string(key), 0, self->old_prefix_len);
g_string_prepend_len(sb_string(key), self->new_prefix, self->new_prefix_len);
g_string_erase(sb_gstring_string(key), 0, self->old_prefix_len);
g_string_prepend_len(sb_gstring_string(key),
self->new_prefix, self->new_prefix_len);
}

static void
Expand Down Expand Up @@ -238,11 +240,11 @@ value_pairs_transform_set_apply(ValuePairsTransformSet *vpts, gchar *key)
if (g_pattern_match_string(vpts->pattern, key))
{
GList *l;
ScratchBuffer *sb;
SBGString *sb;
gchar *new_key;

sb = scratch_buffer_acquire ();
g_string_assign(sb_string(sb), key);
sb = sb_gstring_acquire ();
g_string_assign(sb_gstring_string(sb), key);

l = vpts->transforms;
while (l)
Expand All @@ -251,9 +253,9 @@ value_pairs_transform_set_apply(ValuePairsTransformSet *vpts, gchar *key)
l = l->next;
}

new_key = sb_string(sb)->str;
g_string_steal(sb_string(sb));
scratch_buffer_release (sb);
new_key = sb_gstring_string(sb)->str;
g_string_steal(sb_gstring_string(sb));
sb_gstring_release (sb);

return new_key;
}
Expand Down
Loading

0 comments on commit 4b462ce

Please sign in to comment.