Skip to content

Commit

Permalink
optmize: call localtime_r per second, speed up!
Browse files Browse the repository at this point in the history
  • Loading branch information
HardySimpson committed Jun 14, 2012
1 parent 5382a47 commit 3b7fd70
Show file tree
Hide file tree
Showing 24 changed files with 403 additions and 441 deletions.
85 changes: 44 additions & 41 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
[x] 使用writev来写日志,提高效率,达到一次形成,多次输出的目的
[x] 尝试信号量的方法来打成线程安全的转档互斥问题
[x] zlog.h变长参数宏的问题(参考glib的log解决)
[x] rule的dynamic] file可以从format获取
[x] 修改arraylist的读取长度,循环等
[x] 运行期间指定是否采用线程
[x] buf_append 优化——为了安全,不优化这个
[x] 用某种自动编译器以及文档生成器
[x] 输出到管道,有名管道和文件一样
[x] 打印堆栈。。可能需要语言支持
[x] zlog-input -c category -p priority -f conf
[x] 采用精细的reo en方法来建立用fo en来智能判断需不需要重开文件,操作系统本身已经做得够好,无须做
---
[p] 使用valgrind测试性能
[p] 自己实现部分的strftime,进一步提高效率
[ ] 增加man age, df, 案例
[ ] 和rsyslog对接的问题
[ ] hzlog的可定制话
[ ] 分类匹配的可定制化
[ ] 兼容性问题
zlog.h内,__VA_ARGS__的跨平台

--- 1.0.6 ---
[o] 每秒取一次localtime_r,加速1倍以上
--- 1.0.5 ---
[o] 修复1.0.4的问题,reopen文件时用读写锁保护
--- 1.0.4 ---
[o] 测试多线程的效率
[o] 缓存静态文件的FILE*和fd,优化,加速3倍
--- 1.0.3 ---
[o] 基于日志笔数自动刷新配置
[o] 基于日志笔数自动做sync到硬盘操作
--- 1.0.2 ---
[o] 增加同步写入到硬盘选项,打开后极为费时
--- 1.0.1 ---
[o] 采用配置文件作为锁文件
--- 1.0.0 ---
[o] 改变配置文件的布局,去掉@ &这种符号,改用[global],[format]这样的形式来
[o] 把reload改成原子性的,失败用旧的
[o] 优化spec_gen_msg部分, spec内采用自己写的buf_append对齐来代替buf_sprintf
[o] 引入__func__
[o] 文件的权限设置
[o] 输出函数自定义
--- 0.9 ---
[o] 研究ilog读配置文件, buf
[o] 采用更加面向对象的方法来写
[o] 使用arraylist来代替linklist为内部数据结构
Expand Down Expand Up @@ -41,37 +59,23 @@
[o] ylog, 更简单的接口, 改名为dzlog的一堆接口来解决
[o] zlog-gen-conf
[o] rotate控制个数
--- 0.9 ---
[o] 改变配置文件的布局,去掉@ &这种符号,改用[global],[format]这样的形式来
[o] 把reload改成原子性的,失败用旧的
[o] 优化spec_gen_msg部分, spec内采用自己写的buf_append对齐来代替buf_sprintf
[o] 引入__func__
[o] 文件的权限设置
[o] 输出函数自定义
--- 1.0.0 ---
[o] 采用配置文件作为锁文件
--- 1.0.1 ---
[o] 增加同步写入到硬盘选项,打开后极为费时
--- 1.0.2 ---
[o] 基于日志笔数自动刷新配置
[o] 基于日志笔数自动做sync到硬盘操作
--- 1.0.3 ---
[o] 测试多线程的效率
[o] 缓存静态文件的FILE*和fd,优化
--- 1.0.4 ---
--- 废弃想法 ---
[x] 使用writev来写日志,提高效率,达到一次形成,多次输出的目的
[x] 尝试信号量的方法来打成线程安全的转档互斥问题
[x] zlog.h变长参数宏的问题(参考glib的log解决)
[x] rule的dynamic] file可以从format获取
[x] 修改arraylist的读取长度,循环等
[x] 运行期间指定是否采用线程
[x] buf_append 优化——为了安全,不优化这个
[x] 用某种自动编译器以及文档生成器
[x] 输出到管道,有名管道和文件一样
[x] 打印堆栈。。可能需要语言支持
[x] zlog-input -c category -p priority -f conf
[x] 采用精细的reo en方法来建立用fo en来智能判断需不需要重开文件,操作系统本身已经做得够好,无须做

[p] 使用valgrind测试性能
[ ] 增加man age, df, 案例
[ ] 和rsyslog对接的问题
[ ] hzlog的可定制话
[ ] 分类匹配的可定制化
[ ] 兼容性问题
zlog.h内,__VA_ARGS__的跨平台
==========
编写文章:

==========

打算做一个集中日志系统,设计如下

1.应用程序调用日志库,日志库写本地硬盘日志
Expand All @@ -80,4 +84,3 @@
4.集中日志服务器将日志存入数据库,共实时显示,事后分析用

本机的服务进程和集中日志服务器都打算使用rsyslog,比较专业
日志库打算用log4j,logback之类的,但问题在于第2步
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AC_PREREQ(2.59)
AC_INIT([zlog], [1.0.5], [[email protected]])
AC_INIT([zlog], [1.0.6], [[email protected]])
AM_INIT_AUTOMAKE([foreign -Wall -Werror])
AC_CONFIG_SRCDIR([libzlog/zlog.h])
AC_CONFIG_HEADER([config.h])
Expand Down
2 changes: 1 addition & 1 deletion devconf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

# from configure,Makefile.am->Makefile, developer use

CFLAGS="-Wall -Werror -g -O0 -std=c99" ./configure --prefix=/opt/develop/ --enable-test
CFLAGS="-Wall -Werror -g -O2 -std=c99" ./configure --prefix=/opt/develop/ --enable-test
7 changes: 4 additions & 3 deletions doc/UsersGuide-CN.lyx
Original file line number Diff line number Diff line change
Expand Up @@ -1786,7 +1786,8 @@ reference "sub:时间字符"
\begin_inset Text

\begin_layout Plain Layout
环境变量,后面也要跟小括号,内涵环境变量的键。变量的值在zlog_init()的时候就求出来了
环境变量,后面也要跟小括号,内涵环境变量的键。变量的值在zlog_init()的时候就求出来了。所以只含有%E的文件名被认为是静态路径文件,相比动态路径文件写日
志会有一定的性能提升。
\end_layout

\end_inset
Expand Down Expand Up @@ -4814,7 +4815,8 @@ sys 0m6.950s
&default "%d(%F %T) %P [%p:%F:%L] %m%n"
\end_layout

\begin_layout Standard
\end_deeper
\begin_layout Itemize
用户自定义输出详见
\begin_inset CommandInset ref
LatexCommand ref
Expand All @@ -4825,7 +4827,6 @@ reference "sec:用户自定义输出"

\end_layout

\end_deeper
\begin_layout Section
配置文件工具
\end_layout
Expand Down
8 changes: 5 additions & 3 deletions doc/UsersGuide-EN.lyx
Original file line number Diff line number Diff line change
Expand Up @@ -1950,7 +1950,9 @@ for more detail.
\begin_layout Plain Layout
Used to output the environment variable.
The key of environment variable should be enclose between parenthesis.Vaule
is fixed at zlog_init()
is fixed at zlog_init().
So the log path contains only %E is through to be static log path.Logging
will be faster than dynamic log path.
\end_layout

\end_inset
Expand Down Expand Up @@ -5092,7 +5094,8 @@ It is optional.
"%d(%F %T) %V [%p:%F:%L] %m%n"
\end_layout

\begin_layout Standard
\end_deeper
\begin_layout Itemize
see
\begin_inset CommandInset ref
LatexCommand ref
Expand All @@ -5103,7 +5106,6 @@ reference "sec:User-defined-Output"
for more details for $.
\end_layout

\end_deeper
\begin_layout Section
Configure File Tools
\end_layout
Expand Down
7 changes: 7 additions & 0 deletions libzlog/buf.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ zlog_buf_t *zlog_buf_new(size_t buf_size_min, size_t buf_size_max,
}
}

/*******************************************************************************/
void zlog_buf_restart(zlog_buf_t * a_buf)
{
a_buf->end = a_buf->start;
return;
}

/*******************************************************************************/
int zlog_buf_printf(zlog_buf_t * a_buf, const char *format, ...)
{
Expand Down
5 changes: 1 addition & 4 deletions libzlog/buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ zlog_buf_t *zlog_buf_new(size_t buf_size_min, size_t buf_size_max,
void zlog_buf_del(zlog_buf_t * a_buf);
void zlog_buf_profile(zlog_buf_t * a_buf, int flag);

#define zlog_buf_restart(a_buf) do { \
a_buf->end = a_buf->start; \
} while(0);

void zlog_buf_restart(zlog_buf_t * a_buf);
int zlog_buf_printf(zlog_buf_t * a_buf, const char *format, ...);
int zlog_buf_vprintf(zlog_buf_t * a_buf, const char *format, va_list args);
int zlog_buf_append(zlog_buf_t * a_buf, const char *str, size_t str_len);
Expand Down
74 changes: 51 additions & 23 deletions libzlog/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,33 @@
#define ZLOG_CONF_DEFAULT_RULE "*.* >stdout"
#define ZLOG_CONF_DEFAULT_BUF_SIZE_MIN 1024
#define ZLOG_CONF_DEFAULT_BUF_SIZE_MAX (2 * 1024 * 1024)
#define ZLOG_CONF_DEFAULT_ROTATE_LOCK_FILE "/tmp/zlog.lock"
#define ZLOG_CONF_DEFAULT_FILE_PERMS 0600
#define ZLOG_CONF_DEFAULT_RELOAD_CONF_PERIOD 0
#define ZLOG_CONF_DEFAULT_FSYNC_PERIOD 0
#define ZLOG_CONF_BACKUP_ROTATE_LOCK_FILE "/tmp/zlog.lock"
/*******************************************************************************/

struct zlog_conf_s {
char file[MAXLEN_PATH + 1];
char mtime[20 + 1];

int strict_init;
size_t buf_size_min;
size_t buf_size_max;

char rotate_lock_file[MAXLEN_CFG_LINE + 1];
zlog_rotater_t *rotater;

zc_arraylist_t *levels;

char default_format_line[MAXLEN_CFG_LINE + 1];
zlog_format_t *default_format;

unsigned int file_perms;

zc_arraylist_t *formats;

zc_arraylist_t *rules;
};

void zlog_conf_profile(zlog_conf_t * a_conf, int flag)
{
int i;
Expand All @@ -62,8 +83,6 @@ void zlog_conf_profile(zlog_conf_t * a_conf, int flag)
zlog_format_profile(a_conf->default_format, flag);
}
zc_profile(flag, "---file perms[0%o]---", a_conf->file_perms);
zc_profile(flag, "---reload conf period[%ld]---", a_conf->reload_conf_period);
zc_profile(flag, "---fsync period[%ld]---", a_conf->fsync_period);

zc_profile(flag, "---rotate lock file[%s]---", a_conf->rotate_lock_file);
if (a_conf->rotater) zlog_rotater_profile(a_conf->rotater, flag);
Expand Down Expand Up @@ -136,23 +155,18 @@ zlog_conf_t *zlog_conf_new(char *conf_file)
a_conf->strict_init = 1;
a_conf->buf_size_min = ZLOG_CONF_DEFAULT_BUF_SIZE_MIN;
a_conf->buf_size_max = ZLOG_CONF_DEFAULT_BUF_SIZE_MAX;
if (has_conf_file) {
strcpy(a_conf->rotate_lock_file, a_conf->file);
} else {
strcpy(a_conf->rotate_lock_file, ZLOG_CONF_BACKUP_ROTATE_LOCK_FILE);
}
strcpy(a_conf->rotate_lock_file, ZLOG_CONF_DEFAULT_ROTATE_LOCK_FILE);
strcpy(a_conf->default_format_line, ZLOG_CONF_DEFAULT_FORMAT);
a_conf->file_perms = ZLOG_CONF_DEFAULT_FILE_PERMS;
a_conf->reload_conf_period = ZLOG_CONF_DEFAULT_RELOAD_CONF_PERIOD;
a_conf->fsync_period = ZLOG_CONF_DEFAULT_FSYNC_PERIOD;
/* set default configuration end */

a_conf->levels = zlog_level_list_new();
if (!a_conf->levels) {
zc_error("zlog_level_list_new fail");
rc = -1;
goto zlog_conf_new_exit;
}
}

a_conf->formats = zc_arraylist_new((zc_arraylist_del_fn) zlog_format_del);
if (!a_conf->formats) {
zc_error("zc_arraylist_new fail");
Expand Down Expand Up @@ -214,8 +228,7 @@ static int zlog_conf_build_without_file(zlog_conf_t * a_conf)
a_conf->levels,
a_conf->default_format,
a_conf->formats,
a_conf->file_perms,
a_conf->fsync_period);
a_conf->file_perms);
if (!default_rule) {
zc_error("zlog_rule_new fail");
return -1;
Expand Down Expand Up @@ -434,11 +447,6 @@ static int zlog_conf_parse_line(zlog_conf_t * a_conf, char *line, int *section)
} else if (STRCMP(word_1, ==, "default") && STRCMP(word_2, ==, "format")) {
/* so the input now is [format = "xxyy"], fit format's style */
strcpy(a_conf->default_format_line, line + nread);
} else if (STRCMP(word_1, ==, "reload") &&
STRCMP(word_2, ==, "conf") && STRCMP(word_3, ==, "period")) {
a_conf->reload_conf_period = zc_parse_byte_size(value);
} else if (STRCMP(word_1, ==, "fsync") && STRCMP(word_2, ==, "period")) {
a_conf->fsync_period = zc_parse_byte_size(value);
} else {
zc_error("name[%s] is not any one of global options", name);
if (a_conf->strict_init) return -1;
Expand Down Expand Up @@ -471,9 +479,7 @@ static int zlog_conf_parse_line(zlog_conf_t * a_conf, char *line, int *section)
a_conf->levels,
a_conf->default_format,
a_conf->formats,
a_conf->file_perms,
a_conf->fsync_period);

a_conf->file_perms);
if (!a_rule) {
zc_error("zlog_rule_new fail [%s]", line);
if (a_conf->strict_init) return -1;
Expand All @@ -495,3 +501,25 @@ static int zlog_conf_parse_line(zlog_conf_t * a_conf, char *line, int *section)
return 0;
}
/*******************************************************************************/
zc_arraylist_t *zlog_conf_get_rules(zlog_conf_t *a_conf)
{
zc_assert(a_conf, NULL);
return a_conf->rules;
}

void zlog_conf_get_buf_size(zlog_conf_t *a_conf,
size_t * buf_size_min, size_t * buf_size_max)
{
zc_assert(a_conf,);
zc_assert(buf_size_min,);
zc_assert(buf_size_max,);

*buf_size_min = a_conf->buf_size_min;
*buf_size_max = a_conf->buf_size_max;
}

char *zlog_conf_get_file(zlog_conf_t *a_conf)
{
zc_assert(a_conf, NULL);
return a_conf->file;
}
29 changes: 6 additions & 23 deletions libzlog/conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,16 @@

#include "zc_defs.h"
#include "format.h"
#include "rotater.h"

typedef struct zlog_conf_s {
char file[MAXLEN_PATH + 1];
char mtime[20 + 1];

int strict_init;
size_t buf_size_min;
size_t buf_size_max;

char rotate_lock_file[MAXLEN_CFG_LINE + 1];
zlog_rotater_t *rotater;

char default_format_line[MAXLEN_CFG_LINE + 1];
zlog_format_t *default_format;

unsigned int file_perms;
size_t fsync_period;
size_t reload_conf_period;

zc_arraylist_t *levels;
zc_arraylist_t *formats;
zc_arraylist_t *rules;
} zlog_conf_t;
typedef struct zlog_conf_s zlog_conf_t;

zlog_conf_t *zlog_conf_new(char *conf_file);
void zlog_conf_del(zlog_conf_t * a_conf);
void zlog_conf_profile(zlog_conf_t * a_conf, int flag);

zc_arraylist_t *zlog_conf_get_rules(zlog_conf_t *a_conf);
void zlog_conf_get_buf_size(zlog_conf_t *a_conf,
size_t * buf_size_min, size_t * buf_size_max);
char *zlog_conf_get_file(zlog_conf_t *a_conf);

#endif
11 changes: 6 additions & 5 deletions libzlog/rotater.c
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,13 @@ int zlog_rotater_rotate(zlog_rotater_t *a_rotater,

rc = stat(file_path, &info);
if (rc) {
zc_warn("stat [%s] fail, errno[%d], maybe in rotating", file_path, errno);
return 0;
zc_error("stat [%s] fail, errno[%d]", file_path, errno);
return -1;
} else {
/* file not so big, return */
if (info.st_size + msg_len < file_max_size) return 0;
if (info.st_size + msg_len < file_max_size) {
/* file not so big, return */
return 0;
}
}

rd = zlog_rotater_trylock(a_rotater);
Expand Down Expand Up @@ -485,7 +487,6 @@ int zlog_rotater_rotate(zlog_rotater_t *a_rotater,
rc = -1;
goto zlog_rotater_rotate_exit;
} else if (rc == 0) {
rc = 1;
zc_debug("zlog_rotater_file_ls_mv success");
}

Expand Down
Loading

0 comments on commit 3b7fd70

Please sign in to comment.