diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2014-05-29 12:34:28 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2014-05-29 12:38:57 +0300 |
commit | 35e2816614ab2a1aa585b1ae2547683a69ed9d22 (patch) | |
tree | bb7c8923c2c5ba311b9f042d7dbb3436f65ff3fa | |
parent | ac4200fa46eebfc9227130739ae7f867e3fd0a20 (diff) | |
download | vmod-binlog-35e2816614ab2a1aa585b1ae2547683a69ed9d22.tar.gz vmod-binlog-35e2816614ab2a1aa585b1ae2547683a69ed9d22.tar.bz2 |
Use per-thread environment for packet construction.
* src/binlog.c (binlog_config) <inst_cur>
<env,state,timestamp>: Remove.
(binlog_env): New struct.
(thread_once.thread_key): New statics.
(binlog_env_get,binlog_env_init): New functions.
(vmod_init): Don't create env here.
Register make_key.
(vmod_start): Use binlog_env
(vmod_pack): Likewise.
(vmod_commit): Likewise.
* src/binlogsel.c (interval_add): Update timemask if START_TIME
or STOP_TIME are set on tmask.
-rw-r--r-- | src/binlog.c | 107 | ||||
-rw-r--r-- | src/binlogsel.c | 8 |
2 files changed, 85 insertions, 30 deletions
diff --git a/src/binlog.c b/src/binlog.c index bf7fa97..4c3f40e 100644 --- a/src/binlog.c +++ b/src/binlog.c @@ -68,10 +68,12 @@ struct binlog_config { struct packinst *inst_head; /* compiled format */ - struct packinst *inst_cur; /* current instruction */ - struct packenv *env; /* pack environment */ - enum binlog_state state; /* binlog machine state */ - time_t timestamp; /* timestamp for the entry being built */ }; -static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; - +struct binlog_env { + enum binlog_state state; /* binlog machine state */ + struct binlog_config *conf; + time_t timestamp; /* timestamp for the entry being built */ + struct packinst *inst_cur; /* current instruction */ + struct packenv *env; /* pack environment */ +}; + void @@ -96,2 +98,36 @@ binlog_debug(const char *fmt, ...) +static pthread_once_t thread_once = PTHREAD_ONCE_INIT; +static pthread_key_t thread_key; +static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + +static struct binlog_env * +binlog_env_get(void) +{ + struct binlog_env *ep = pthread_getspecific(thread_key); + if (!ep) { + ep = malloc(sizeof(*ep)); + AN(ep); + memset(ep, 0, sizeof *ep); + ep->state = state_init; + if (pthread_setspecific(thread_key, ep)) { + binlog_error("cannot set thread-specific data"); + abort(); + } + } + return ep; +} + +void +binlog_env_init(struct binlog_env *ep, struct binlog_config *conf, time_t ts) +{ + if (!ep->env) { + ep->env = packenv_create(packsize(conf->inst_head)); + AN(ep->env); + } + packenv_init(ep->env); + ep->state = state_start; + ep->inst_cur = packinit(conf->inst_head); + ep->timestamp = ts; +} + void @@ -209,2 +245,16 @@ getindexpat(const char *name) +static void +env_free(void *f) +{ + struct binlog_env *ep = f; + packenv_free(ep->env); + free(ep); +} + +static void +make_key() +{ + pthread_key_create(&thread_key, env_free); +} + void @@ -254,6 +304,4 @@ vmod_init(struct sess *sp, struct vmod_priv *priv, } - conf->recsize = packsize(conf->inst_head); - conf->env = packenv_create(conf->recsize); - AN(conf->env); - conf->recsize += offsetof(struct binlog_record,data); + conf->recsize = packsize(conf->inst_head) + + offsetof(struct binlog_record,data); conf->format = strdup(format); @@ -370,2 +418,4 @@ vmod_init(struct sess *sp, struct vmod_priv *priv, conf->stoptime = time(NULL); + + pthread_once(&thread_once, make_key); pthread_mutex_init(&conf->mutex, NULL); @@ -644,2 +694,3 @@ vmod_start(struct sess *sp, struct vmod_priv *priv) time_t ts; + struct binlog_env *ep; @@ -657,6 +708,4 @@ vmod_start(struct sess *sp, struct vmod_priv *priv) - packenv_init(conf->env); - conf->state = state_start; - conf->inst_cur = packinit(conf->inst_head); - conf->timestamp = ts; + ep = binlog_env_get(); + binlog_env_init(ep, conf, ts); } @@ -667,2 +716,3 @@ vmod_pack(struct sess *sp, struct vmod_priv *priv, const char *str) struct binlog_config *conf = priv->priv; + struct binlog_env *ep = binlog_env_get(); char *argv[2]; @@ -672,3 +722,3 @@ vmod_pack(struct sess *sp, struct vmod_priv *priv, const char *str) - switch (conf->state) { + switch (ep->state) { case state_start: @@ -677,3 +727,3 @@ vmod_pack(struct sess *sp, struct vmod_priv *priv, const char *str) default: - binlog_error("pack called in wrong state (%d)", conf->state); + binlog_error("pack called in wrong state (%d)", ep->state); return; @@ -681,3 +731,3 @@ vmod_pack(struct sess *sp, struct vmod_priv *priv, const char *str) - if (!conf->inst_cur) { + if (!ep->inst_cur) { binlog_error("format spec exhausted"); @@ -688,9 +738,9 @@ vmod_pack(struct sess *sp, struct vmod_priv *priv, const char *str) argv[1] = NULL; - conf->env->argv = argv; - conf->env->argc = 2; - conf->env->argi = 0; + ep->env->argv = argv; + ep->env->argc = 2; + ep->env->argi = 0; - conf->inst_cur = packinnext(conf->inst_cur, conf->env); + ep->inst_cur = packinnext(ep->inst_cur, ep->env); - conf->state = state_pack; + ep->state = state_pack; } @@ -701,2 +751,3 @@ vmod_commit(struct sess *sp, struct vmod_priv *priv) struct binlog_config *conf = priv->priv; + struct binlog_env *ep = binlog_env_get(); @@ -707,3 +758,3 @@ vmod_commit(struct sess *sp, struct vmod_priv *priv) - switch (conf->state) { + switch (ep->state) { case state_start: @@ -712,3 +763,3 @@ vmod_commit(struct sess *sp, struct vmod_priv *priv) case state_pack: - if (conf->inst_cur) + if (ep->inst_cur) binlog_error("committing incomplete binlog record"); @@ -716,3 +767,3 @@ vmod_commit(struct sess *sp, struct vmod_priv *priv) default: - binlog_error("pack called in wrong state (%d)", conf->state); + binlog_error("pack called in wrong state (%d)", ep->state); return; @@ -728,4 +779,4 @@ vmod_commit(struct sess *sp, struct vmod_priv *priv) conf->recsize); - p->ts = conf->timestamp; - memcpy(p->data, conf->env->buf_base, conf->env->buf_size); + p->ts = ep->timestamp; + memcpy(p->data, ep->env->buf_base, ep->env->buf_size); conf->base->recnum++; @@ -733,3 +784,3 @@ vmod_commit(struct sess *sp, struct vmod_priv *priv) AZ(pthread_mutex_unlock(&conf->mutex)); - conf->state = state_init; + ep->state = state_init; } diff --git a/src/binlogsel.c b/src/binlogsel.c index 80f5b83..fad0881 100644 --- a/src/binlogsel.c +++ b/src/binlogsel.c @@ -94,4 +94,6 @@ interval_add(const char *name, int tmask, time_t start, time_t end) if (tmask & START_TIME) { - if (!(timemask & START_TIME) || start_time > start) + if (!(timemask & START_TIME) || start_time > start) { start_time = start; + timemask |= START_TIME; + } } else @@ -100,4 +102,6 @@ interval_add(const char *name, int tmask, time_t start, time_t end) if (tmask & STOP_TIME) { - if (!(timemask & STOP_TIME) || to_time < end) + if (!(timemask & STOP_TIME) || to_time < end) { to_time = end; + timemask |= STOP_TIME; + } } else |