Permalink
Browse files

bugfix: let OS release zlog_thread_t when thread exit, simple zlog_re…

…load() and zlog_fini()
  • Loading branch information...
1 parent ab49e4d commit 15631df3f82bfc8309e6c83211cfa8e1c3088602 @HardySimpson committed Oct 11, 2012
Showing with 103 additions and 414 deletions.
  1. +4 −3 TODO
  2. +2 −6 src/makefile
  3. +25 −64 src/thread.c
  4. +3 −7 src/thread.h
  5. +0 −131 src/thread_list.c
  6. +0 −38 src/thread_list.h
  7. +67 −163 src/zlog.c
  8. +2 −2 test/test_press_zlog.conf
View
7 TODO
@@ -1,19 +1,21 @@
[p] 使用valgrind测试性能
[ ] hzlog的可定制
+[ ] hex那段重写,内置到buf内,参考od的设计
[ ] 分类匹配的可定制化, rcat
[ ] 自行管理文件缓存,替代stdio
+[ ] 减少dynamic文件名open的次数,通过日期改变智能推断, file_table?
[ ] async file输出的增加
[ ] 兼容性问题 zlog.h内
[ ] 增加trace级别
-[ ] hex那段重写,内置到buf内
[ ] gettid()
[ ] 性能对比, log4x, pantheios, glog
[ ] perl, python, go, c++支持
[ ] redis对接,协议设计
[ ] 和rsyslog对接的问题
[ ] linux fsync->fdatasync, open..
-[ ] 减少dynamic文件名open的次数,通过日期改变智能推断, file_table?
+--- 1.2.4 ---
+[o] 不再维持thread_list链表,每次写日志时判断配置有无更新,来刷新每线程的缓存大小
--- 1.2.0 ---
[o] rotate和zip的方案
[o] 把一部分static变量化为全局变量,减少接口参数
@@ -73,7 +75,6 @@
[o] 文件的权限设置
[o] 输出函数自定义
--- 0.9 ---
-[o] 研究ilog读配置文件, buf
[o] 采用更加面向对象的方法来写
[o] 使用arraylist来代替linklist为内部数据结构
[o] 改进配置文件的格式
View
@@ -19,7 +19,6 @@ OBJ= \
rule.o \
spec.o \
thread.o \
- thread_list.o \
zc_arraylist.o \
zc_hashtable.o \
zc_profile.o \
@@ -101,9 +100,6 @@ spec.o: spec.c fmacros.h spec.h event.h zc_defs.h zc_profile.h \
mdc.h level_list.h level.h
thread.o: thread.c zc_defs.h zc_profile.h zc_arraylist.h zc_hashtable.h \
zc_xplatform.h zc_util.h event.h buf.h thread.h mdc.h
-thread_list.o: thread_list.c zc_defs.h zc_profile.h zc_arraylist.h \
- zc_hashtable.h zc_xplatform.h zc_util.h thread_list.h thread.h event.h \
- buf.h mdc.h
zc_arraylist.o: zc_arraylist.c zc_defs.h zc_profile.h zc_arraylist.h \
zc_hashtable.h zc_xplatform.h zc_util.h
zc_hashtable.o: zc_hashtable.c zc_defs.h zc_profile.h zc_arraylist.h \
@@ -114,7 +110,7 @@ zc_util.o: zc_util.c zc_defs.h zc_profile.h zc_arraylist.h zc_hashtable.h \
zlog-chk-conf.o: zlog-chk-conf.c fmacros.h zlog.h
zlog.o: zlog.c fmacros.h conf.h zc_defs.h zc_profile.h zc_arraylist.h \
zc_hashtable.h zc_xplatform.h zc_util.h format.h thread.h event.h buf.h \
- mdc.h rotater.h category_table.h category.h thread_list.h record_table.h \
+ mdc.h rotater.h category_table.h category.h record_table.h \
record.h rule.h
$(DYLIBNAME): $(OBJ)
@@ -129,7 +125,7 @@ static: $(STLIBNAME)
# Binaries:
zlog-chk-conf: zlog-chk-conf.o $(STLIBNAME)
- $(CC) -o $@ $(REAL_CFLAGS) $(REAL_LDFLAGS) zlog-chk-conf.o -lzlog
+ $(CC) -o $@ $(REAL_CFLAGS) zlog-chk-conf.o -static -L. -lzlog $(REAL_LDFLAGS)
.c.o:
$(CC) -std=c99 -pedantic -c $(REAL_CFLAGS) $<
View
@@ -72,7 +72,7 @@ void zlog_thread_del(zlog_thread_t * a_thread)
return;
}
-zlog_thread_t *zlog_thread_new(size_t buf_size_min, size_t buf_size_max)
+zlog_thread_t *zlog_thread_new(int init_version, size_t buf_size_min, size_t buf_size_max)
{
zlog_thread_t *a_thread;
@@ -82,6 +82,8 @@ zlog_thread_t *zlog_thread_new(size_t buf_size_min, size_t buf_size_max)
return NULL;
}
+ a_thread->init_version = init_version;
+
a_thread->mdc = zlog_mdc_new();
if (!a_thread->mdc) {
zc_error("zlog_mdc_new fail");
@@ -134,84 +136,43 @@ zlog_thread_t *zlog_thread_new(size_t buf_size_min, size_t buf_size_max)
}
/*******************************************************************************/
-int zlog_thread_update_msg_buf(zlog_thread_t * a_thread, size_t buf_size_min, size_t buf_size_max)
+int zlog_thread_resize_msg_buf(zlog_thread_t * a_thread, int init_version, size_t buf_size_min, size_t buf_size_max)
{
+ zlog_buf_t *pre_msg_buf_new = NULL;
+ zlog_buf_t *msg_buf_new = NULL;
zc_assert(a_thread, -1);
- /* 1st, mv msg_buf msg_buf_backup */
- if (a_thread->pre_msg_buf_backup) zlog_buf_del(a_thread->pre_msg_buf_backup);
- if (a_thread->msg_buf_backup) zlog_buf_del(a_thread->msg_buf_backup);
- a_thread->pre_msg_buf_backup = a_thread->pre_msg_buf;
- a_thread->msg_buf_backup = a_thread->msg_buf;
-
+ if ( (a_thread->msg_buf->size_min == buf_size_min)
+ && (a_thread->msg_buf->size_max == buf_size_max)) {
+ zc_debug("buf size not changed, no need resize");
+ a_thread->init_version = init_version;
+ return 0;
+ }
- /* 2nd, gen new buf */
- a_thread->pre_msg_buf = zlog_buf_new(buf_size_min, buf_size_max, "..." FILE_NEWLINE);
- if (!a_thread->pre_msg_buf) {
+ pre_msg_buf_new = zlog_buf_new(buf_size_min, buf_size_max, "..." FILE_NEWLINE);
+ if (!pre_msg_buf_new) {
zc_error("zlog_buf_new fail");
goto err;
}
- a_thread->msg_buf = zlog_buf_new(buf_size_min, buf_size_max, "..." FILE_NEWLINE);
- if (!a_thread->msg_buf) {
+ msg_buf_new = zlog_buf_new(buf_size_min, buf_size_max, "..." FILE_NEWLINE);
+ if (!msg_buf_new) {
zc_error("zlog_buf_new fail");
goto err;
}
+ zlog_buf_del(a_thread->pre_msg_buf);
+ a_thread->pre_msg_buf = pre_msg_buf_new;
+
+ zlog_buf_del(a_thread->msg_buf);
+ a_thread->msg_buf = msg_buf_new;
+
+ a_thread->init_version = init_version;
return 0;
err:
- if (a_thread->pre_msg_buf) zlog_buf_del(a_thread->pre_msg_buf);
- if (a_thread->msg_buf) zlog_buf_del(a_thread->msg_buf);
- a_thread->pre_msg_buf = NULL;
- a_thread->msg_buf = NULL;
+ if (pre_msg_buf_new) zlog_buf_del(pre_msg_buf_new);
+ if (msg_buf_new) zlog_buf_del(msg_buf_new);
return -1;
}
-void zlog_thread_commit_msg_buf(zlog_thread_t * a_thread)
-{
- zc_assert(a_thread, );
- if (!a_thread->pre_msg_buf_backup && !a_thread->msg_buf_backup) {
- zc_warn("backup is null, never update before");
- return;
- }
-
- if (a_thread->pre_msg_buf_backup) zlog_buf_del(a_thread->pre_msg_buf_backup);
- a_thread->pre_msg_buf_backup = NULL;
- if (a_thread->msg_buf_backup) zlog_buf_del(a_thread->msg_buf_backup);
- a_thread->msg_buf_backup = NULL;
- return;
-}
-
-void zlog_thread_rollback_msg_buf(zlog_thread_t * a_thread)
-{
- zc_assert(a_thread,);
- if (!a_thread->pre_msg_buf_backup || !a_thread->msg_buf_backup) {
- zc_warn("backup is null, never update before");
- return;
- }
-
- if (a_thread->pre_msg_buf) {
- /* update success */
- zlog_buf_del(a_thread->pre_msg_buf);
- a_thread->pre_msg_buf = a_thread->pre_msg_buf_backup;
- a_thread->pre_msg_buf_backup = NULL;
- } else {
- /* update fail */
- a_thread->pre_msg_buf = a_thread->pre_msg_buf_backup;
- a_thread->pre_msg_buf_backup = NULL;
- }
-
- if (a_thread->msg_buf) {
- /* update success */
- zlog_buf_del(a_thread->msg_buf);
- a_thread->msg_buf = a_thread->msg_buf_backup;
- a_thread->msg_buf_backup = NULL;
- } else {
- /* update fail */
- a_thread->msg_buf = a_thread->msg_buf_backup;
- a_thread->msg_buf_backup = NULL;
- }
- return;
-}
-
/*******************************************************************************/
View
@@ -26,6 +26,7 @@
#include "mdc.h"
typedef struct {
+ int init_version;
zlog_mdc_t *mdc;
zlog_event_t *event;
@@ -34,18 +35,13 @@ typedef struct {
zlog_buf_t *archive_path_buf;
zlog_buf_t *pre_msg_buf;
zlog_buf_t *msg_buf;
-
- zlog_buf_t *pre_msg_buf_backup;
- zlog_buf_t *msg_buf_backup;
} zlog_thread_t;
void zlog_thread_del(zlog_thread_t * a_thread);
void zlog_thread_profile(zlog_thread_t * a_thread, int flag);
-zlog_thread_t *zlog_thread_new(size_t buf_size_min, size_t buf_size_max);
+zlog_thread_t *zlog_thread_new(int init_version, size_t buf_size_min, size_t buf_size_max);
-int zlog_thread_update_msg_buf(zlog_thread_t * a_thread, size_t buf_size_min, size_t buf_size_max);
-void zlog_thread_commit_msg_buf(zlog_thread_t * a_thread);
-void zlog_thread_rollback_msg_buf(zlog_thread_t * a_thread);
+int zlog_thread_resize_msg_buf(zlog_thread_t * a_thread, int init_version, size_t buf_size_min, size_t buf_size_max);
#endif
View
@@ -1,131 +0,0 @@
-/*
- * This file is part of the zlog Library.
- *
- * Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
- *
- * The zlog 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 3 of the License, or
- * (at your option) any later version.
- *
- * The zlog 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 the zlog Library. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include <errno.h>
-#include <pthread.h>
-#include "zc_defs.h"
-#include "thread_list.h"
-
-
-void zlog_thread_list_profile(zc_arraylist_t * threads, int flag)
-{
- int i;
- zlog_thread_t *a_thread;
-
- zc_assert(threads,);
- zc_profile(flag, "--thread_list[%p]--", threads);
- zc_arraylist_foreach(threads, i, a_thread) {
- zlog_thread_profile(a_thread, flag);
- }
- return;
-}
-
-/*******************************************************************************/
-void zlog_thread_list_del(zc_arraylist_t * threads)
-{
- zc_assert(threads,);
- zc_arraylist_del(threads);
- zc_debug("zlog_thread_list_del[%p]", threads);
- return;
-}
-
-zc_arraylist_t *zlog_thread_list_new(void)
-{
- zc_arraylist_t *threads;
-
- threads = zc_arraylist_new((zc_arraylist_del_fn)zlog_thread_del);
- if (!threads) {
- zc_error("zc_arraylist_new fail");
- return NULL;
- } else {
- zlog_thread_list_profile(threads, ZC_DEBUG);
- return threads;
- }
-}
-
-/*******************************************************************************/
-int zlog_thread_list_update_msg_buf(zc_arraylist_t * threads, size_t buf_size_min, size_t buf_size_max)
-{
- int i;
- zlog_thread_t *a_thread;
-
- zc_assert(threads, -1);
- zc_arraylist_foreach(threads, i, a_thread) {
- if (zlog_thread_update_msg_buf(a_thread, buf_size_min, buf_size_max)) {
- zc_error("zlog_thread_update_msg_buf fail, try rollback");
- return -1;
- }
- }
-
- return 0;
-}
-
-void zlog_thread_list_commit_msg_buf(zc_arraylist_t * threads)
-{
- int i;
- zlog_thread_t *a_thread;
-
- zc_assert(threads,);
- zc_arraylist_foreach(threads, i, a_thread) {
- zlog_thread_commit_msg_buf(a_thread);
- }
- return;
-}
-
-void zlog_thread_list_rollback_msg_buf(zc_arraylist_t * threads)
-{
- int i;
- zlog_thread_t *a_thread;
-
- zc_assert(threads,);
- zc_arraylist_foreach(threads, i, a_thread) {
- zlog_thread_rollback_msg_buf(a_thread);
- }
- return;
-}
-
-/*******************************************************************************/
-zlog_thread_t *zlog_thread_list_new_thread(zc_arraylist_t * threads, pthread_key_t key,
- size_t buf_size_min, size_t buf_size_max)
-{
- int rc;
- zlog_thread_t *a_thread;
- a_thread = zlog_thread_new(buf_size_min, buf_size_max);
- if (!a_thread) {
- zc_error("zlog_thread_new fail");
- return NULL;
- }
-
- if (zc_arraylist_add(threads, a_thread)) {
- zc_error("zc_arraylist_put fail");
- goto err;
- }
-
- rc = pthread_setspecific(key, a_thread);
- if (rc) {
- zc_error("pthread_setspecific fail, rc[%d]");
- goto err;
- }
-
- return a_thread;
-err:
- zlog_thread_del(a_thread);
- return NULL;
-}
-/*******************************************************************************/
View
@@ -1,38 +0,0 @@
-/*
- * This file is part of the zlog Library.
- *
- * Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
- *
- * The zlog 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 3 of the License, or
- * (at your option) any later version.
- *
- * The zlog 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 the zlog Library. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#ifndef __zlog_thread_list_h
-#define __zlog_thread_list_h
-
-#include <pthread.h>
-#include "zc_defs.h"
-#include "thread.h"
-
-zc_arraylist_t *zlog_thread_list_new(void);
-void zlog_thread_list_del(zc_arraylist_t *threads);
-void zlog_thread_list_profile(zc_arraylist_t *threads, int flag);
-
-int zlog_thread_list_update_msg_buf(zc_arraylist_t * threads, size_t buf_size_min, size_t buf_size_max);
-void zlog_thread_list_commit_msg_buf(zc_arraylist_t * threads);
-void zlog_thread_list_rollback_msg_buf(zc_arraylist_t * threads);
-
-zlog_thread_t *zlog_thread_list_new_thread(zc_arraylist_t * threads, pthread_key_t key,
- size_t buf_size_min, size_t buf_size_max);
-
-#endif
Oops, something went wrong.

0 comments on commit 15631df

Please sign in to comment.