From 15969bc7743f49d10baab2113e911f5ea80a3a62 Mon Sep 17 00:00:00 2001 From: Water-Melon Date: Wed, 11 Oct 2023 10:10:35 +0000 Subject: [PATCH] optimize performance --- include/mln_alloc.h | 20 ++++++++--------- include/mln_event.h | 51 +++++++++++++++++++++--------------------- include/mln_fheap.h | 14 ++++++------ include/mln_file.h | 14 ++++++------ include/mln_hash.h | 20 ++++++++--------- include/mln_iothread.h | 12 +++++----- include/mln_log.h | 8 +++---- src/mln_event.c | 4 ++-- src/mln_log.c | 2 +- 9 files changed, 72 insertions(+), 73 deletions(-) diff --git a/include/mln_alloc.h b/include/mln_alloc.h index 87abebd9..a6476e19 100644 --- a/include/mln_alloc.h +++ b/include/mln_alloc.h @@ -47,6 +47,8 @@ struct mln_alloc_shm_attr_s { * But in Linux, no such kind of problem. */ typedef struct mln_alloc_blk_s { + struct mln_alloc_blk_s *prev; + struct mln_alloc_blk_s *next; mln_alloc_t *pool; void *data; mln_alloc_chunk_t *chunk; @@ -54,8 +56,6 @@ typedef struct mln_alloc_blk_s { mln_size_t is_large:1; mln_size_t in_used:1; mln_size_t padding:30; - struct mln_alloc_blk_s *prev; - struct mln_alloc_blk_s *next; } mln_alloc_blk_t; struct mln_alloc_chunk_s { @@ -78,6 +78,8 @@ struct mln_alloc_mgr_s { }; typedef struct mln_alloc_shm_s { + struct mln_alloc_shm_s *prev; + struct mln_alloc_shm_s *next; mln_alloc_t *pool; void *addr; mln_size_t size; @@ -85,17 +87,9 @@ typedef struct mln_alloc_shm_s { mln_u32_t base:31; mln_u32_t large:1; mln_u8_t bitmap[M_ALLOC_SHM_BITMAP_LEN]; - struct mln_alloc_shm_s *prev; - struct mln_alloc_shm_s *next; } mln_alloc_shm_t; struct mln_alloc_s { - mln_alloc_mgr_t mgr_tbl[M_ALLOC_MGR_LEN]; - struct mln_alloc_s *parent; - mln_alloc_chunk_t *large_used_head; - mln_alloc_chunk_t *large_used_tail; - mln_alloc_shm_t *shm_head; - mln_alloc_shm_t *shm_tail; void *mem; mln_size_t shm_size; void *locker; @@ -104,6 +98,12 @@ struct mln_alloc_s { #if defined(WIN32) HANDLE map_handle; #endif + struct mln_alloc_s *parent; + mln_alloc_mgr_t mgr_tbl[M_ALLOC_MGR_LEN]; + mln_alloc_chunk_t *large_used_head; + mln_alloc_chunk_t *large_used_tail; + mln_alloc_shm_t *shm_head; + mln_alloc_shm_t *shm_tail; }; diff --git a/include/mln_event.h b/include/mln_event.h index 343ba944..33bdc6ad 100644 --- a/include/mln_event.h +++ b/include/mln_event.h @@ -66,6 +66,15 @@ enum mln_event_type { }; typedef struct mln_event_fd_s { + int fd; + mln_u32_t active_flag; + mln_u32_t in_process:1; + mln_u32_t is_clear:1; + mln_u32_t in_active:1; + mln_u32_t rd_oneshot:1; + mln_u32_t wr_oneshot:1; + mln_u32_t err_oneshot:1; + mln_u32_t padding:26; void *rcv_data; ev_fd_handler rcv_handler; void *snd_data; @@ -76,15 +85,6 @@ typedef struct mln_event_fd_s { ev_fd_handler timeout_handler; mln_fheap_node_t *timeout_node; mln_u64_t end_us; - int fd; - mln_u32_t active_flag; - mln_u32_t in_process:1; - mln_u32_t is_clear:1; - mln_u32_t in_active:1; - mln_u32_t rd_oneshot:1; - mln_u32_t wr_oneshot:1; - mln_u32_t err_oneshot:1; - mln_u32_t padding:26; } mln_event_fd_t; typedef struct mln_event_tm_s { @@ -94,35 +94,26 @@ typedef struct mln_event_tm_s { } mln_event_tm_t; struct mln_event_desc_s { + struct mln_event_desc_s *prev; + struct mln_event_desc_s *next; + struct mln_event_desc_s *act_prev; + struct mln_event_desc_s *act_next; enum mln_event_type type; mln_u32_t flag; union { - mln_event_fd_t fd; mln_event_tm_t tm; + mln_event_fd_t fd; } data; - struct mln_event_desc_s *prev; - struct mln_event_desc_s *next; - struct mln_event_desc_s *act_prev; - struct mln_event_desc_s *act_next; }; struct mln_event_s { + pthread_mutex_t fd_lock; + pthread_mutex_t timer_lock; + pthread_mutex_t cb_lock; dispatch_callback callback; void *callback_data; - mln_rbtree_t *ev_fd_tree; - mln_event_desc_t *ev_fd_wait_head; - mln_event_desc_t *ev_fd_wait_tail; - mln_event_desc_t *ev_fd_active_head; - mln_event_desc_t *ev_fd_active_tail; - mln_fheap_t *ev_fd_timeout_heap; - mln_fheap_t *ev_timer_heap; mln_u32_t is_break:1; mln_u32_t padding:31; - int rd_fd; - int wr_fd; - pthread_mutex_t fd_lock; - pthread_mutex_t timer_lock; - pthread_mutex_t cb_lock; #if defined(MLN_EPOLL) int epollfd; int unusedfd; @@ -135,6 +126,14 @@ struct mln_event_s { fd_set wr_set; fd_set err_set; #endif + + mln_rbtree_t *ev_fd_tree; + mln_event_desc_t *ev_fd_wait_head; + mln_event_desc_t *ev_fd_wait_tail; + mln_event_desc_t *ev_fd_active_head; + mln_event_desc_t *ev_fd_active_tail; + mln_fheap_t *ev_fd_timeout_heap; + mln_fheap_t *ev_timer_heap; }; #define mln_event_break_set(ev) ((ev)->is_break = 1); diff --git a/include/mln_fheap.h b/include/mln_fheap.h index 8c9a6d47..1f549e1b 100644 --- a/include/mln_fheap.h +++ b/include/mln_fheap.h @@ -54,16 +54,16 @@ typedef struct mln_fheap_node_s { } mln_fheap_node_t; typedef struct { - void *pool; - fheap_pool_alloc_handler pool_alloc; - fheap_pool_free_handler pool_free; - void *min_val; + mln_fheap_node_t *root_list; + mln_fheap_node_t *min; fheap_cmp cmp; fheap_copy copy; fheap_key_free key_free; - mln_fheap_node_t *min; - mln_fheap_node_t *root_list; mln_size_t num; + void *min_val; + void *pool; + fheap_pool_alloc_handler pool_alloc; + fheap_pool_free_handler pool_free; } mln_fheap_t; /* @@ -94,7 +94,7 @@ mln_fheap_del_child(mln_fheap_node_t **root, mln_fheap_node_t *node) node->left->right = node->right; } } else { - // if (node->right == node) abort(); + /*if (node->right == node) abort();*/ node->right->left = node->left; node->left->right = node->right; } diff --git a/include/mln_file.h b/include/mln_file.h index da80761a..182bd422 100644 --- a/include/mln_file.h +++ b/include/mln_file.h @@ -14,18 +14,18 @@ typedef struct mln_fileset_s mln_fileset_t; typedef struct mln_file_s { - mln_string_t *file_path; + struct mln_file_s *prev; + struct mln_file_s *next; int fd; mln_u32_t is_tmp:1; + mln_string_t *file_path; + mln_fileset_t *fset; + mln_rbtree_node_t *node; + size_t refer_cnt; + size_t size; time_t mtime; time_t ctime; time_t atime; - size_t size; - size_t refer_cnt; - struct mln_file_s *prev; - struct mln_file_s *next; - mln_fileset_t *fset; - mln_rbtree_node_t *node; } mln_file_t; struct mln_fileset_s { diff --git a/include/mln_hash.h b/include/mln_hash.h index 6c49fed3..ded71c11 100644 --- a/include/mln_hash.h +++ b/include/mln_hash.h @@ -28,9 +28,6 @@ typedef enum mln_hash_flag { } mln_hash_flag_t; struct mln_hash_attr { - void *pool; - hash_pool_alloc_handler pool_alloc; - hash_pool_free_handler pool_free; hash_calc_handler hash; hash_cmp_handler cmp; hash_free_handler free_key; @@ -38,13 +35,16 @@ struct mln_hash_attr { mln_u64_t len_base; mln_u32_t expandable:1; mln_u32_t calc_prime:1; + void *pool; + hash_pool_alloc_handler pool_alloc; + hash_pool_free_handler pool_free; }; typedef struct mln_hash_entry_s { - void *val; - void *key; struct mln_hash_entry_s *prev; struct mln_hash_entry_s *next; + void *val; + void *key; } mln_hash_entry_t; typedef struct { @@ -53,19 +53,19 @@ typedef struct { } mln_hash_mgr_t; struct mln_hash_s { - void *pool; - hash_pool_alloc_handler pool_alloc; - hash_pool_free_handler pool_free; + mln_hash_mgr_t *tbl; + mln_u64_t len; hash_calc_handler hash; hash_cmp_handler cmp; hash_free_handler free_key; hash_free_handler free_val; - mln_hash_mgr_t *tbl; - mln_u64_t len; mln_u32_t nr_nodes; mln_u32_t threshold; mln_u32_t expandable:1; mln_u32_t calc_prime:1; + void *pool; + hash_pool_alloc_handler pool_alloc; + hash_pool_free_handler pool_free; }; extern int diff --git a/include/mln_iothread.h b/include/mln_iothread.h index 7133aeb6..aeb62e31 100644 --- a/include/mln_iothread.h +++ b/include/mln_iothread.h @@ -20,14 +20,14 @@ typedef void *(*mln_iothread_entry_t)(void *); typedef void (*mln_iothread_msg_process_t)(mln_iothread_t *, mln_iothread_ep_type_t, mln_iothread_msg_t *); struct mln_iothread_msg_s { + struct mln_iothread_msg_s *prev; + struct mln_iothread_msg_s *next; mln_u32_t feedback:1; mln_u32_t hold:1; mln_u32_t padding:30; mln_u32_t type; void *data; pthread_mutex_t mutex; - struct mln_iothread_msg_s *prev; - struct mln_iothread_msg_s *next; }; struct mln_iothread_attr { @@ -38,19 +38,19 @@ struct mln_iothread_attr { }; struct mln_iothread_s { - pthread_t *tids; pthread_mutex_t io_lock; pthread_mutex_t user_lock; int io_fd; int user_fd; - mln_iothread_entry_t entry; - void *args; mln_iothread_msg_process_t handler; mln_iothread_msg_t *io_head; mln_iothread_msg_t *io_tail; + mln_iothread_entry_t entry; + void *args; + pthread_t *tids; + mln_u32_t nthread; mln_iothread_msg_t *user_head; mln_iothread_msg_t *user_tail; - mln_u32_t nthread; }; #define mln_iothread_sockfd_get(p,t) ((t) == io_thread? (p)->io_fd: (p)->user_fd) diff --git a/include/mln_log.h b/include/mln_log.h index bcba4541..7b9f1a66 100644 --- a/include/mln_log.h +++ b/include/mln_log.h @@ -21,15 +21,15 @@ typedef enum { } mln_log_level_t; typedef struct { - char dir_path[M_LOG_PATH_LEN/2]; - char pid_path[M_LOG_PATH_LEN]; - char log_path[M_LOG_PATH_LEN]; + mln_spin_t thread_lock; int fd; mln_u32_t in_daemon:1; mln_u32_t init:1; mln_u32_t padding:30; mln_log_level_t level; - mln_spin_t thread_lock; + char dir_path[M_LOG_PATH_LEN/2]; + char pid_path[M_LOG_PATH_LEN]; + char log_path[M_LOG_PATH_LEN]; } mln_log_t; diff --git a/src/mln_event.c b/src/mln_event.c index d5317205..a41e03c0 100644 --- a/src/mln_event.c +++ b/src/mln_event.c @@ -68,9 +68,9 @@ mln_event_fd_timeout_set(mln_event_t *ev, mln_event_desc_t *ed, int timeout_ms); /*varliables*/ mln_event_desc_t fheap_min = { + NULL, NULL, NULL, NULL, M_EV_TM, 0, - {{NULL, NULL, 0}}, - NULL, NULL, NULL, NULL + {(mln_event_tm_t){NULL, NULL, 0}}, }; mln_event_t *mln_event_new(void) diff --git a/src/mln_log.c b/src/mln_log.c index 4604ca8e..c65acb27 100644 --- a/src/mln_log.c +++ b/src/mln_log.c @@ -45,7 +45,7 @@ static mln_logger_t _logger = _mln_sys_log_process; char log_err_level[] = "Log level permission deny."; char log_err_fmt[] = "Log message format error."; char log_path_cmd[] = "log_path"; -mln_log_t g_log = {{0},{0},{0},STDERR_FILENO,0,0,0,none,(mln_spin_t)0}; +mln_log_t g_log = {(mln_spin_t)0, STDERR_FILENO, 0, 0, 0, none, {0},{0},{0}}; /* * file lock