Commit 0505991b authored by Hardy Simpson's avatar Hardy Simpson

optmize: kick buf_printf, add buf_printf_dec/hex (220 060 448)

parent 2f548663
......@@ -6,18 +6,19 @@
[ ] 兼容性问题
zlog.h内,__VA_ARGS__的跨平台
[ ] c++接口的测试和实现
[ ] spec,level采用直接数组的方式重写
[ ] linux fsync->fdatasync, open..
[ ] 去除auto tools的使用,自行makefile[redis]
[ ] async file输出的增加
[ ] rcat
[ ] rule_name
[ ] 增加trace级别
[ ] hex那段重写,内置到buf内
[ ] format,spec,level采用直接数组的方式重写
--- 1.1.0 ---
[ ] 把__LINE__ 变成string, 需要改接口
[ ] 寻找快速的方法把pid变成字符串
[x] 建立bit位来判断,目前的实现已经足够快了
[x] 把__LINE__ 变成string, 需要改接口
[x] 建立bit位来判断,无锁判断
[o] 寻找快速的方法把us, srcline变成字符串
[o] 在一开始就判断是否需要输出,
[o] 使用pthread_key_t线程私有变量来存储zlog_thread_t,仅有1%不到的提高
[o] 增加%D,缓存strftime的结果
......
......@@ -14,14 +14,14 @@ sys 0m0.130s
-------------------------------------------------
v1.1.0
$ time ./test_press_zlog 1 10 100000
real 0m1.427s
user 0m2.270s
sys 0m0.320s
real 0m1.107s
user 0m1.500s
sys 0m0.340s
$ time ./test_press_zlog2 1 10 100000
real 0m1.295s
user 0m2.140s
sys 0m0.160s
real 0m0.898s
user 0m1.480s
sys 0m0.150s
-------------------------------------------------
v1.0.7
$ time ./test_press_zlog 1 10 100000
......
This diff is collapsed.
......@@ -22,6 +22,7 @@
#include <stdarg.h>
#include <time.h>
#include <stdint.h>
typedef struct zlog_buf_s {
char *start;
......@@ -44,17 +45,22 @@ void zlog_buf_profile(zlog_buf_t * a_buf, int flag);
#define zlog_buf_restart(a_buf) do { \
a_buf->tail = a_buf->start; \
} while(0);
} while(0)
#define zlog_buf_len(a_buf) (a_buf->tail - a_buf->start)
#define zlog_buf_str(a_buf) (a_buf->start)
#define zlog_buf_seal(a_buf) do {*(a_buf)->tail = '\0';} while (0)
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);
int zlog_buf_adjust_append(zlog_buf_t * a_buf, const char *str, size_t str_len,
int left_adjust, size_t min_width, size_t max_width);
int zlog_buf_printf_num(zlog_buf_t * a_buf, uint64_t ui64, int is_hex, int width);
int zlog_buf_printf_dec32(zlog_buf_t * a_buf, uint32_t ui32, int width);
int zlog_buf_printf_dec64(zlog_buf_t * a_buf, uint64_t ui64, int width);
int zlog_buf_printf_hex(zlog_buf_t * a_buf, uint32_t ui32, int width);
#endif
......@@ -53,7 +53,6 @@ typedef struct {
struct timeval time_stamp;
time_t last_sec;
struct tm local_time;
char us[6 + 1];
char D_time_str[10 + 1 + 8 + 1]; /* YYYY-mm-dd HH:mm:ss */
char time_str[MAXLEN_CFG_LINE + 1];
......
......@@ -33,7 +33,7 @@
#define ZLOG_DEFAULT_TIME_FMT "%F %T"
#define ZLOG_HEX_HEAD \
" 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF"
"\n 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF"
/*******************************************************************************/
void zlog_spec_profile(zlog_spec_t * a_spec, int flag)
......@@ -55,8 +55,6 @@ void zlog_spec_profile(zlog_spec_t * a_spec, int flag)
#define zlog_spec_fetch_time do {\
if (!a_thread->event->time_stamp.tv_sec) { \
gettimeofday(&(a_thread->event->time_stamp), NULL); \
sprintf(a_thread->event->us, "%6.6ld", \
(long)a_thread->event->time_stamp.tv_usec); \
\
if (a_thread->event->time_stamp.tv_sec != a_thread->event->last_sec) { \
/* localtime_r is slow on linux, do it once per second */ \
......@@ -110,14 +108,14 @@ static int zlog_spec_write_ms(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zl
{
/* do fetch time every event once */
zlog_spec_fetch_time;
return zlog_buf_append(a_buf, a_thread->event->us, 3);
return zlog_buf_printf_dec32(a_buf, (a_thread->event->time_stamp.tv_usec / 1000), 3);
}
static int zlog_spec_write_us(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf)
{
/* do fetch time every event once */
zlog_spec_fetch_time;
return zlog_buf_append(a_buf, a_thread->event->us, 6);
return zlog_buf_printf_dec32(a_buf, a_thread->event->time_stamp.tv_usec, 6);
}
static int zlog_spec_write_mdc(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf)
......@@ -164,12 +162,12 @@ static int zlog_spec_write_srcfile_neat(zlog_spec_t * a_spec, zlog_thread_t * a_
static int zlog_spec_write_srcline(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf)
{
return zlog_buf_printf(a_buf, "%ld", a_thread->event->line);
return zlog_buf_printf_dec64(a_buf, a_thread->event->line, 0);
}
static int zlog_spec_write_srcfunc(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf)
{
zc_assert(a_thread->event->func, -1);
zc_assert(a_thread->event->func, 0);
return zlog_buf_append(a_buf, a_thread->event->func, a_thread->event->func_len);
}
......@@ -246,7 +244,7 @@ static int zlog_spec_write_usrmsg(zlog_spec_t * a_spec, zlog_thread_t * a_thread
a_thread->event->str_format,
a_thread->event->str_args);
} else {
return zlog_buf_printf(a_buf, "format=(null)");
return zlog_buf_append(a_buf, "format=(null)", sizeof("format=(null)")-1);
}
} else if (a_thread->event->generate_cmd == ZLOG_HEX) {
int rc;
......@@ -255,22 +253,11 @@ static int zlog_spec_write_usrmsg(zlog_spec_t * a_spec, zlog_thread_t * a_thread
/* thread buf start == null or len <= 0 */
if (a_thread->event->hex_buf == NULL) {
rc = zlog_buf_printf(a_buf, "hex_buf=(null)");
goto zlog_hex_exit;
} else if (a_thread->event->hex_buf_len <= 0) {
rc = zlog_buf_printf(a_buf, "(hex_buf_len=%ld) <= 0",
(long) a_thread->event->hex_buf_len);
goto zlog_hex_exit;
}
rc = zlog_buf_printf(a_buf, "hex_buf_len=[%ld]\n",
(long) a_thread->event->hex_buf_len);
if (rc) {
rc = zlog_buf_append(a_buf, "buf=(null)", sizeof("buf=(null)")-1);
goto zlog_hex_exit;
}
rc = zlog_buf_append(a_buf, ZLOG_HEX_HEAD,
sizeof(ZLOG_HEX_HEAD)-1);
rc = zlog_buf_append(a_buf, ZLOG_HEX_HEAD, sizeof(ZLOG_HEX_HEAD)-1);
if (rc) {
goto zlog_hex_exit;
}
......@@ -282,67 +269,48 @@ static int zlog_spec_write_usrmsg(zlog_spec_t * a_spec, zlog_thread_t * a_thread
unsigned char c;
rc = zlog_buf_append(a_buf, "\n", 1);
if (rc) {
goto zlog_hex_exit;
}
if (rc) goto zlog_hex_exit;
rc = zlog_buf_printf(a_buf, "%010.10ld ",
line_offset + 1);
if (rc) {
goto zlog_hex_exit;
}
rc = zlog_buf_printf_dec64(a_buf, line_offset + 1, 10);
if (rc) goto zlog_hex_exit;
rc = zlog_buf_append(a_buf, " ", 3);
if (rc) goto zlog_hex_exit;
for (byte_offset = 0; byte_offset < 16; byte_offset++) {
if (line_offset * 16 + byte_offset <
a_thread->event->hex_buf_len) {
c = *((unsigned char *)a_thread->event->
hex_buf + line_offset * 16 +
byte_offset);
rc = zlog_buf_printf(a_buf, "%02x ", c);
if (rc) {
goto zlog_hex_exit;
}
if (line_offset * 16 + byte_offset < a_thread->event->hex_buf_len) {
c = *((unsigned char *)a_thread->event->hex_buf
+ line_offset * 16 + byte_offset);
rc = zlog_buf_printf_hex(a_buf, c, 2);
if (rc) goto zlog_hex_exit;
rc = zlog_buf_append(a_buf, " ", 1);
if (rc) goto zlog_hex_exit;
} else {
rc = zlog_buf_append(a_buf, " ", 3);
if (rc) {
goto zlog_hex_exit;
}
if (rc) goto zlog_hex_exit;
}
}
rc = zlog_buf_append(a_buf, " ", 2);
if (rc) {
goto zlog_hex_exit;
}
if (rc) goto zlog_hex_exit;
for (byte_offset = 0; byte_offset < 16; byte_offset++) {
if (line_offset * 16 + byte_offset <
a_thread->event->hex_buf_len) {
c = *((unsigned char *)a_thread->event->
hex_buf + line_offset * 16 +
byte_offset);
if (line_offset * 16 + byte_offset < a_thread->event->hex_buf_len) {
c = *((unsigned char *)a_thread->event->hex_buf
+ line_offset * 16 + byte_offset);
if (c >= 32 && c <= 126) {
rc = zlog_buf_printf(a_buf, "%c", c);
if (rc) {
goto zlog_hex_exit;
}
rc = zlog_buf_append(a_buf,(char*)&c, 1);
if (rc) goto zlog_hex_exit;
} else {
rc = zlog_buf_append(a_buf, ".",
1);
if (rc) {
goto zlog_hex_exit;
}
rc = zlog_buf_append(a_buf, ".", 1);
if (rc) goto zlog_hex_exit;
}
} else {
rc = zlog_buf_append(a_buf, " ", 1);
if (rc) {
goto zlog_hex_exit;
}
if (rc) goto zlog_hex_exit;
}
}
if (line_offset * 16 + byte_offset >=
a_thread->event->hex_buf_len) {
if (line_offset * 16 + byte_offset >= a_thread->event->hex_buf_len) {
break;
}
......
......@@ -21,6 +21,17 @@
#include <limits.h>
#define ZLOG_INT32_LEN sizeof("-2147483648") - 1
#define ZLOG_INT64_LEN sizeof("-9223372036854775808") - 1
#if ((__GNU__ == 2) && (__GNUC_MINOR__ < 8))
#define ZLOG_MAX_UINT32_VALUE (uint32_t) 0xffffffffLL
#else
#define ZLOG_MAX_UINT32_VALUE (uint32_t) 0xffffffff
#endif
#define ZLOG_MAX_INT32_VALUE (uint32_t) 0x7fffffff
#define MAXLEN_PATH 1024
#define MAXLEN_CFG_LINE (MAXLEN_PATH * 4)
......
[formats]
simple = "%D %m%n"
simple = "%D.%ms %m%n"
simple2 = "%D.%us %m%n"
[rules]
my_cat.* >stdout;simple
my_cat.* >stdout;
my_cat.* >stdout;simple2
......@@ -78,9 +78,10 @@ int main(int argc, char** argv)
FILE *fp;
char *dmp;
long dmp_len = 0;
int ntimes;
if (argc != 2) {
printf("useage: test_hex [file]\n");
if (argc != 3) {
printf("useage: test_hex [file] [ntimes]\n");
exit(1);
}
......@@ -89,6 +90,8 @@ int main(int argc, char** argv)
printf("fopen[%s] fail\n", argv[1]);
exit(1);
}
ntimes = atoi(argv[2]);
zlog_category_t *zc;
......@@ -108,9 +111,8 @@ int main(int argc, char** argv)
rc = ReadTotalFile(fp, &dmp, &dmp_len);
while(ntimes--) HZLOG_DEBUG(zc, dmp, dmp_len);
HZLOG_DEBUG(zc, dmp, dmp_len);
rc = system("ps -o \"pid,ppid,pcpu,pmem,rssize,start_time,args\" | grep test_hex");
fclose(fp);
free(dmp);
......
[rules]
*.* >stdout;
*.* "hex.log";
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment