Skip to content

Commit

Permalink
bugfix: let OS release zlog_thread_t when thread exit, simple zlog_re…
Browse files Browse the repository at this point in the history
…load() and zlog_fini()
  • Loading branch information
HardySimpson committed Oct 11, 2012
1 parent ab49e4d commit 15631df
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 414 deletions.
7 changes: 4 additions & 3 deletions TODO
Original file line number Diff line number Diff line change
@@ -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变量化为全局变量,减少接口参数
Expand Down Expand Up @@ -73,7 +75,6 @@
[o] 文件的权限设置
[o] 输出函数自定义
--- 0.9 ---
[o] 研究ilog读配置文件, buf
[o] 采用更加面向对象的方法来写
[o] 使用arraylist来代替linklist为内部数据结构
[o] 改进配置文件的格式
Expand Down
8 changes: 2 additions & 6 deletions src/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ OBJ= \
rule.o \
spec.o \
thread.o \
thread_list.o \
zc_arraylist.o \
zc_hashtable.o \
zc_profile.o \
Expand Down Expand Up @@ -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 \
Expand All @@ -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)
Expand All @@ -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) $<
Expand Down
89 changes: 25 additions & 64 deletions src/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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");
Expand Down Expand Up @@ -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;
}

/*******************************************************************************/
10 changes: 3 additions & 7 deletions src/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "mdc.h"

typedef struct {
int init_version;
zlog_mdc_t *mdc;
zlog_event_t *event;

Expand All @@ -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
131 changes: 0 additions & 131 deletions src/thread_list.c

This file was deleted.

38 changes: 0 additions & 38 deletions src/thread_list.h

This file was deleted.

Loading

0 comments on commit 15631df

Please sign in to comment.