From 5ee98d9ecff13889197f01b3fcfabacc70f53785 Mon Sep 17 00:00:00 2001 From: yalog Date: Sun, 5 Jun 2011 00:29:28 +0800 Subject: [PATCH] have done some testing and midify:mem_pool,log --- core/event.c | 4 ++-- core/log.c | 42 +++++++++++++++++++++--------------------- core/log.h | 9 ++++++++- core/mem_pool.c | 39 +++++++++++++++++++++++++++------------ core/mem_pool.h | 2 +- core/signal.c | 9 ++++++++- core/thread_pool.c | 2 +- main/http_main.c | 21 +++++++++++++++++++++ main/http_request.c | 12 +++++++++++- modules/module.h | 19 +++++++++++++++++++ 10 files changed, 119 insertions(+), 40 deletions(-) create mode 100644 modules/module.h diff --git a/core/event.c b/core/event.c index 187b7b7..346990a 100644 --- a/core/event.c +++ b/core/event.c @@ -257,7 +257,7 @@ void event_process() log_debug("connection overload and discard"); } else { - http_init_connection(c); //初始化一个HTTP连接 + http_init_connection(c); //初始化一个HTTP连接,功能的衔接点(link) log_access("connection reached"); } }while(1); @@ -266,7 +266,7 @@ void event_process() //将发生的事件,放入事件队列中 log_debug("A event Occurred"); c = event_list[i].data.ptr; - //在这里debug一下events的值 + //在这里debug一下events的值,这里可以对event增加一个ready字段,发生事件后就标志,处理完后就取消 if ((event_list[i].events & EPOLLIN) && c->read.active) { event_posted_add(c.read); } diff --git a/core/log.c b/core/log.c index 161013f..e54597d 100644 --- a/core/log.c +++ b/core/log.c @@ -12,10 +12,9 @@ #include #include #include +#include -#define DEBUG 0 -#define ERROR 1 -#define LOG_LEVEL DEBUG +#include "log.h" //这是日志文件的配置 //#define error_log "/var/log/mihttpd_error" @@ -27,7 +26,9 @@ #define access_log "./mihttpd_access" FILE *mi_error; -FILE *mi_debug; +#ifdef DEBUG + FILE *mi_debug; +#endif FILE *mi_access; /* @@ -69,25 +70,24 @@ void log_init() } } - if (LOG_LEVEL == DEBUG ) { - if (stat(debug_log, &s) == -1) { - if (errno == ENOENT) { //文件不存在,创建 - if ((mi_debug = fopen(debug_log, "w")) == NULL) { - printf("debug_log init failure\n"); - exit(1); - } - } - } - else { - if ((mi_debug = fopen(debug_log, "a")) == NULL) { + #ifdef DEBUG + if (stat(debug_log, &s) == -1) { + if (errno == ENOENT) { //文件不存在,创建 + if ((mi_debug = fopen(debug_log, "w")) == NULL) { printf("debug_log init failure\n"); exit(1); } } } + else { + if ((mi_debug = fopen(debug_log, "a")) == NULL) { + printf("debug_log init failure\n"); + exit(1); + } + } + #endif - fd = open(error_log, O_APPEND); - dup2(fd, STDIN_FILENO); + fd = open("/dev/null", O_WRONLY); dup2(fd, STDOUT_FILENO); dup2(fd, STDERR_FILENO); } @@ -102,13 +102,13 @@ void _log_error(int error_no, char *msg, char *filename, int line) void _log_debug(char *msg, char *filename,int line) { - if (LOG_LEVEL == DEBUG) { - fprintf(mi_debug, "%s:%d %s\n", filename, line, msg); - } + fprintf(mi_debug, "%s:%d %s\n", filename, line, msg); } void log_access(char *msg) { //记录访问时间 - fprintf(mi_access, "2011-6-2 %s\n", msg); + time_t timep; + time(&timep); + fprintf(mi_access, "%s ::%s\n", msg, ctime(&timep)); } \ No newline at end of file diff --git a/core/log.h b/core/log.h index f343dbe..d6d1f64 100644 --- a/core/log.h +++ b/core/log.h @@ -7,8 +7,15 @@ #ifndef _LOG_H_ #define _LOG_H_ +#define DEBUG 1 + #define log_error(error_no, msg) _log_error(error_no, msg, __FILE__, __LINE__) -#define log_debug(msg) _log_debug(msg, __FILE__, __LINE__) + +#ifdef DEBUG + #define log_debug(msg) _log_debug(msg, __FILE__, __LINE__) +#else + #define log_debug(msg) +#endif void _log_error(int error_no, char *msg, char *filename, int line); void _log_debug(char *msg, char *filename,int line); diff --git a/core/mem_pool.c b/core/mem_pool.c index 6ec80d2..20cc607 100644 --- a/core/mem_pool.c +++ b/core/mem_pool.c @@ -10,8 +10,9 @@ #include #include "mem_pool.h" +#include "log.h" -#define MEM_POOL_SIZE 4096*4 //内存池创建时为1024*8 +#define MEM_POOL_SIZE 4096*4 //内存池创建时为16k static mem_chunk_t *create_chunk(mem_size_t); @@ -28,18 +29,22 @@ mem_pool_t *mem_pool_create() log_error(0, "malloc() memery use up"); return NULL; } - pool = (mem_pool_t *)(chunk + sizeof(mem_chunk_t)); + pool = (mem_pool_t *)((char *)chunk + sizeof(mem_chunk_t)); chunk->size = MEM_POOL_SIZE; chunk->free_size = MEM_POOL_SIZE - sizeof(mem_pool_t) - sizeof(mem_chunk_t); chunk->next = NULL; - chunk->first_avail = pool + sizeof(mem_pool_t); - chunk->endp = chunk + MEM_POOL_SIZE - 1; + chunk->first_avail = (char *)(pool) + sizeof(mem_pool_t); + chunk->endp = (char *)(chunk) + MEM_POOL_SIZE - 1; pool->first = chunk; pool->last = chunk; pool->current = chunk; pthread_mutex_init(&pool->lock, 0); + + log_debug("memory pool created"); + + return pool; } /* @@ -47,7 +52,7 @@ mem_pool_t *mem_pool_create() */ void mem_pool_destroy(mem_pool_t *pool) { - mem_chunk_t *cur_chunk, next_chunk; + mem_chunk_t *cur_chunk, *next_chunk; next_chunk = pool->first; do { @@ -56,6 +61,8 @@ void mem_pool_destroy(mem_pool_t *pool) free(cur_chunk); } while(next_chunk != NULL); + + pthread_mutex_destroy(&pool->lock); } /* @@ -84,9 +91,9 @@ void *mem_palloc(mem_pool_t *pool, mem_size_t size) } } - mem_addr = chunk->fist_avail; + mem_addr = chunk->first_avail; chunk->first_avail += size; - chunk->free -=size; + chunk->free_size -= size; return mem_addr; } @@ -98,16 +105,24 @@ static mem_chunk_t *create_chunk(mem_size_t size) { mem_chunk_t *chunk; - chunk = (mem_chunk_t *)malloc(MEM_ALIGN(size + sizeof(mem_chunk_t))); + if (size + sizeof(mem_chunk_t) <= MEM_CHUNK_DEFAULT_SIZE) { + size = MEM_CHUNK_DEFAULT_SIZE; //保证内存块的最小分配 + } + else { + size = MEM_ALIGN(size + sizeof(mem_chunk_t)); + } + + chunk = (mem_chunk_t *)malloc(size); if (chunk == NULL) { log_error(0, "malloc() memery use up"); return NULL; } - chunk->size =MEM_ALIGN(size + sizeof(mem_chunk_t)); - chunk->free_size = MEM_ALIGN(size + sizeof(mem_chunk_t)) - sizeof(mem_chunk_t); + chunk->size = size; + chunk->free_size = size - sizeof(mem_chunk_t); chunk->next = NULL; - chunk->first_avail = chunk + sizeof(mem_chunk_t); - chunk->endp = chunk + MEM_POOL_SIZE - 1; + chunk->first_avail = (char *)chunk + sizeof(mem_chunk_t); + chunk->endp = (char *)chunk + size - 1; + log_debug("memory chunk alloced"); return chunk; } \ No newline at end of file diff --git a/core/mem_pool.h b/core/mem_pool.h index c01e5e3..314a2cc 100644 --- a/core/mem_pool.h +++ b/core/mem_pool.h @@ -24,7 +24,7 @@ typedef int mem_size_t; typedef struct mem_chunk_s{ mem_size_t size; //块的尺寸 mem_size_t free_size; //块剩余的 - mem_chunk_s *next; + struct mem_chunk_s *next; char *first_avail; //标志可以用内存的起始位置 char *endp; //标志可以用内存的结束位置 }mem_chunk_t; diff --git a/core/signal.c b/core/signal.c index 9598b08..079ccfd 100644 --- a/core/signal.c +++ b/core/signal.c @@ -5,4 +5,11 @@ */ /* * 这个文件主要实现对信号的处理 -*/ \ No newline at end of file +*/ + +/* +* 信号初始化 +*/ +void sig_init() +{ +} \ No newline at end of file diff --git a/core/thread_pool.c b/core/thread_pool.c index f8c4317..ffc09b9 100644 --- a/core/thread_pool.c +++ b/core/thread_pool.c @@ -36,7 +36,7 @@ static void *worker(void *data) event_t *ev; for (;;) { //循环处理事件 - sem_wait(&posted_event_num); //没有需要处理的事件时,就等待 + sem_wait(&posted_event_num); //没有需要处理的事件时,就等待,当然这会引起线程切换导致一些开销 log_debug("process a new event"); ev = event_posted_get(); diff --git a/main/http_main.c b/main/http_main.c index 47736fe..80e79eb 100644 --- a/main/http_main.c +++ b/main/http_main.c @@ -6,6 +6,11 @@ /* * 这个是关于整个服务器启动和初始化相关操作的文件 */ +#include +#include + + +#define WORK_DIR "/var/www/" /* * 整个系统的入口函数; @@ -13,5 +18,21 @@ */ int main(int agrc, char *agrv[]) { + int pid; + + //创建守护进程 + pid = fork(); + if (pid > 0) { + exit(0); + } + else if (pid < 0) { + printf("fork() Mihttpd start abort\n"); + exit(1); + } + setsid(); + + //底层初始化 + + return 0; } \ No newline at end of file diff --git a/main/http_request.c b/main/http_request.c index c5773e3..eecb438 100644 --- a/main/http_request.c +++ b/main/http_request.c @@ -5,4 +5,14 @@ */ /* * 所有与请求信息及处理相关才操作的实现 -*/ \ No newline at end of file +*/ + +#include "../core/event.h" + +/* +* 初始化请求 +*/ +void http_init_request(event_t *ev) +{ +} + diff --git a/modules/module.h b/modules/module.h new file mode 100644 index 0000000..c6499a0 --- /dev/null +++ b/modules/module.h @@ -0,0 +1,19 @@ +/* +* Mihttpd/modules/module.h +* +* (c) 2011 yalog +*/ +/* +* ļҪעhttpչģ +*/ +#include "" + +struct module { + void (* handler)(request_t *); //ģĴع +}; + +//עģر +struct module modules[] = { + &handler_cgi, + NULL +}; \ No newline at end of file