Commit b27c33d2 authored by Hardy Simpson's avatar Hardy Simpson

optimize: cache static file's descriptor

parent 194998b2
......@@ -28,9 +28,8 @@ missing
stamp-h1
tags
zlog-chk-conf
zlog-gen-conf
zlog-*.tar.gz
zlog.pc
press.log*
callgrind*
err.log
test_*
......@@ -42,4 +41,4 @@ doc/*.pdf
doc/obj.think
doc/zlog.3
doc/zlogtest.xls
zlog-*.tar.gz
test/press*
......@@ -56,6 +56,7 @@
[o] 基于日志笔数自动刷新配置
[o] 基于日志笔数自动做sync到硬盘操作
--- 1.0.3 ---
[o] 缓存静态文件的fd,优化
[p] 使用valgrind测试性能
[ ] 增加man age, df, 案例
......
......@@ -2,4 +2,4 @@
# from configure,Makefile.am->Makefile, developer use
CFLAGS="-Wall -Werror -g -O2 -std=c99" ./configure --prefix=/opt/develop/ --enable-test
CFLAGS="-Wall -Werror -g -O0 -std=c99" ./configure --prefix=/opt/develop/ --enable-test
......@@ -485,6 +485,7 @@ 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");
}
......
......@@ -26,6 +26,13 @@ typedef struct zlog_rotater_s zlog_rotater_t;
zlog_rotater_t *zlog_rotater_new(char *lock_file);
void zlog_rotater_del(zlog_rotater_t *a_rot);
/*
* return
* -1 fail
* 0 no rotate
* 1 rotate and success
*/
int zlog_rotater_rotate(zlog_rotater_t *a_rot,
char *file_path, long file_max_size, int file_max_count,
size_t msg_len);
......
This diff is collapsed.
......@@ -14,8 +14,10 @@ noinst_PROGRAMS = \
test_hex \
test_init \
test_press_zlog \
test_press_zlog2 \
test_press_syslog \
test_press_printf \
test_press_printf2 \
test_syslog \
test_conf \
test_mdc \
......@@ -34,7 +36,9 @@ test_hello_SOURCES = test_hello.c
test_hex_SOURCES = test_hex.c
test_init_SOURCES = test_init.c
test_press_zlog_SOURCES = test_press_zlog.c
test_press_zlog2_SOURCES = test_press_zlog2.c
test_press_printf_SOURCES = test_press_printf.c
test_press_printf2_SOURCES = test_press_printf2.c
test_press_syslog_SOURCES = test_press_syslog.c
test_syslog_SOURCES = test_syslog.c
test_mdc_SOURCES = test_mdc.c
......@@ -48,6 +52,7 @@ EXTRA_DIST = \
test_hex.conf \
test_init.conf \
test_press_zlog.conf \
test_press_zlog2.conf \
test_mdc.conf \
test_syslog.conf \
test_level.conf \
......
......@@ -22,29 +22,43 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <pthread.h>
static FILE *fp;
#include "zlog.h"
int work(long loop_count)
FILE *fp;
static long loop_count;
void * work(void *ptr)
{
while(loop_count-- > 0) {
long j = loop_count;
while(j-- > 0) {
fprintf(fp, "2012-05-16 17:24:58.282603 INFO 22471:test_press_zlog.c:33 loglog\n");
}
return 0;
}
int test(long process_count, long loop_count)
int test(long process_count, long thread_count)
{
long i;
pid_t pid;
long j;
for (i = 0; i < process_count; i++) {
pid = fork();
if (pid < 0) {
printf("fork fail\n");
} else if(pid == 0) {
work(loop_count);
pthread_t tid[thread_count];
for (j = 0; j < thread_count; j++) {
pthread_create(&(tid[j]), NULL, work, fp);
}
for (j = 0; j < thread_count; j++) {
pthread_join(tid[j], NULL);
}
return 0;
}
}
......@@ -59,19 +73,17 @@ int test(long process_count, long loop_count)
int main(int argc, char** argv)
{
FILE *fp;
if (argc != 3) {
fprintf(stderr, "test nprocess nloop\n");
if (argc != 4) {
fprintf(stderr, "test nprocess nthreads nloop\n");
exit(1);
}
fp = fopen("press.log", "a");
if (!fp) {
printf("fopen fail\n");
return 1;
}
fp = fopen("press.log", "a");
if (!fp) {
printf("fopen fail\n");
return 1;
}
loop_count = atol(argv[3]);
test(atol(argv[1]), atol(argv[2]));
fclose(fp);
......
/*
* This file is part of the zlog Library.
*
* Copyright (C) 2011 by Hardy Simpson <HardySimpson@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 <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "zlog.h"
static long loop_count;
void * work(void *ptr)
{
long j = loop_count;
static char log[] = "2012-05-16 17:24:58.282603 INFO 22471:test_press_zlog.c:33 loglog\n";
char file[20];
sprintf(file, "press.%ld.log", (long)ptr);
while(j-- > 0) {
int fd;
fd = open(file, O_CREAT | O_WRONLY | O_APPEND , 0755);
write(fd, log, sizeof(log)-1);
close(fd);
}
return 0;
}
int test(long process_count, long thread_count)
{
long i;
pid_t pid;
long j;
for (i = 0; i < process_count; i++) {
pid = fork();
if (pid < 0) {
printf("fork fail\n");
} else if(pid == 0) {
pthread_t tid[thread_count];
for (j = 0; j < thread_count; j++) {
pthread_create(&(tid[j]), NULL, work, (void*)j);
}
for (j = 0; j < thread_count; j++) {
pthread_join(tid[j], NULL);
}
return 0;
}
}
for (i = 0; i < process_count; i++) {
pid = wait(NULL);
}
return 0;
}
int main(int argc, char** argv)
{
if (argc != 4) {
fprintf(stderr, "test nprocess nthreads nloop\n");
exit(1);
}
loop_count = atol(argv[3]);
test(atol(argv[1]), atol(argv[2]));
return 0;
}
......@@ -22,31 +22,41 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <pthread.h>
#include "zlog.h"
static zlog_category_t *zc;
static long loop_count;
int work(long loop_count)
void * work(void *ptr)
{
while(loop_count-- > 0) {
long j = loop_count;
while(j-- > 0) {
ZLOG_INFO(zc, "loglog");
}
return 0;
}
int test(long process_count, long loop_count)
int test(long process_count, long thread_count)
{
long i;
pid_t pid;
long j;
for (i = 0; i < process_count; i++) {
pid = fork();
if (pid < 0) {
printf("fork fail\n");
} else if(pid == 0) {
work(loop_count);
pthread_t tid[thread_count];
for (j = 0; j < thread_count; j++) {
pthread_create(&(tid[j]), NULL, work, NULL);
}
for (j = 0; j < thread_count; j++) {
pthread_join(tid[j], NULL);
}
return 0;
}
}
......@@ -63,8 +73,8 @@ int main(int argc, char** argv)
{
int rc;
if (argc != 3) {
fprintf(stderr, "test nprocess nloop\n");
if (argc != 4) {
fprintf(stderr, "test nprocess nthreads nloop\n");
exit(1);
}
......@@ -81,6 +91,7 @@ int main(int argc, char** argv)
return 3;
}
loop_count = atol(argv[3]);
test(atol(argv[1]), atol(argv[2]));
zlog_fini();
......
[global]
default format = "%d(%F %T.%us) %-6V %p:%F:%L %m%n"
default format = "%d(%F %T.%us) %-6V %p:%t:%F:%L %m%n"
file perms = 0777
#fsync period = 1K
#reload conf period = 1K
[rules]
*.* "press.log",1M;
my_cat.* "press.log"
cat0.* "press.0.log"
cat1.* "press.1.log"
cat2.* "press.2.log"
cat3.* "press.3.log"
cat4.* "press.4.log"
cat5.* "press.5.log"
cat6.* "press.6.log"
cat7.* "press.7.log"
cat8.* "press.8.log"
cat9.* "press.9.log"
/*
* This file is part of the zlog Library.
*
* Copyright (C) 2011 by Hardy Simpson <HardySimpson@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 <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "zlog.h"
static long loop_count;
void * work(void *ptr)
{
long j = loop_count;
char category[20];
sprintf(category, "cat%ld", (long)ptr);
zlog_category_t *zc;
zc = zlog_get_category(category);
while(j-- > 0) {
ZLOG_INFO(zc, "loglog");
}
return 0;
}
int test(long process_count, long thread_count)
{
long i;
pid_t pid;
long j;
for (i = 0; i < process_count; i++) {
pid = fork();
if (pid < 0) {
printf("fork fail\n");
} else if(pid == 0) {
pthread_t tid[thread_count];
for (j = 0; j < thread_count; j++) {
pthread_create(&(tid[j]), NULL, work, (void*)j);
}
for (j = 0; j < thread_count; j++) {
pthread_join(tid[j], NULL);
}
return 0;
}
}
for (i = 0; i < process_count; i++) {
pid = wait(NULL);
}
return 0;
}
int main(int argc, char** argv)
{
int rc = 0;
if (argc != 4) {
fprintf(stderr, "test nprocess nthreads nloop\n");
exit(1);
}
rc = zlog_init("test_press_zlog2.conf");
if (rc) {
printf("init failed\n");
return 2;
}
loop_count = atol(argv[3]);
test(atol(argv[1]), atol(argv[2]));
zlog_fini();
return 0;
}
[global]
default format = "%d(%us) %c %m%n"
#file perms = 0777
#fsync period = 1K
#reload conf period = 1K
[rules]
cat0.* "press.0.log"
cat1.* "press.1.log"
cat2.* "press.2.log"
cat3.* "press.3.log"
cat4.* "press.4.log"
cat5.* "press.5.log"
cat6.* "press.6.log"
cat7.* "press.7.log"
cat8.* "press.8.log"
cat9.* "press.9.log"
......@@ -7,6 +7,6 @@ unset ZLOG_PROFILE_DEBUG_LOG
make -f makefile.linux clean
make -f makefile.linux
rm -f press.log
rm -f press*log
valgrind --tool=callgrind ./test_press_zlog 1 10000
valgrind --tool=callgrind ./test_press_zlog 1 10 10000
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