From 1b22879e90f6b57dbb4047cbc9f39420211d7f0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B4=94=E4=B8=80=E9=B8=A3?= Date: Tue, 8 Oct 2019 17:45:37 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=88=9B=E5=BB=BAhash?= =?UTF-8?q?=E8=A1=A8=E6=97=B6=E9=87=8D=E5=A4=8D=E6=89=93=E5=8D=B0=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/include/kni_utils.h | 15 +++++++- common/src/kni_utils.cpp | 70 +++++++++++++++++--------------------- entry/src/kni_entry.cpp | 11 +++--- 3 files changed, 53 insertions(+), 43 deletions(-) diff --git a/common/include/kni_utils.h b/common/include/kni_utils.h index 13e0424..5fc1478 100644 --- a/common/include/kni_utils.h +++ b/common/include/kni_utils.h @@ -172,6 +172,18 @@ enum kni_deploy_mode{ KNI_DEPLOY_MODE_NORMAL = 1, }; +struct kni_htable_opt{ + int mho_screen_print_ctrl; + int mho_thread_safe; + int mho_mutex_num; + int mho_hash_slot_size; + int mho_hash_max_element_num; + int mho_expire_time; + char mho_eliminate_type[KNI_SYMBOL_MAX]; + void *free_data_cb; + void *expire_notify_cb; +}; + int kni_addr_trans_v4(struct stream_tuple4_v4 *tuple4, char *output, int len); int kni_addr_trans_v6(struct stream_tuple4_v6 *tuple4, char *output, int len); uint16_t kni_ip_checksum(const void *buf, size_t hdr_len); @@ -186,4 +198,5 @@ char* kni_ipv4_errmsg_get(enum kni_ipv4hdr_parse_error _errno); char* kni_ipv6_errmsg_get(enum kni_ipv6hdr_parse_error _errno); char* kni_strdup(const char* s); -MESA_htable_handle kni_create_htable(const char *profile, const char *section, void *free_data_cb, void *expire_notify_cb, void *logger); \ No newline at end of file +MESA_htable_handle kni_create_htable(char *symbol, struct kni_htable_opt *opt, void *logger); +void kni_get_htable_opt(struct kni_htable_opt *opt, const char *profile, const char *section, void *free_data_cb, void *expire_notify_cb, void *logger); \ No newline at end of file diff --git a/common/src/kni_utils.cpp b/common/src/kni_utils.cpp index 3150ac1..676735f 100644 --- a/common/src/kni_utils.cpp +++ b/common/src/kni_utils.cpp @@ -261,57 +261,51 @@ static int __wrapper_MESA_htable_set_opt(MESA_htable_handle table, enum MESA_hta return ret; } -MESA_htable_handle kni_create_htable(const char *profile, const char *section, void *free_data_cb, void *expire_notify_cb, void *logger) -{ - int mho_screen_print_ctrl; - int mho_thread_safe; - int mho_mutex_num; - int mho_hash_slot_size; - int mho_hash_max_element_num; - int mho_expire_time; - char mho_eliminate_type[KNI_SYMBOL_MAX]; - MESA_load_profile_int_def(profile, section, "mho_screen_print_ctrl", &mho_screen_print_ctrl, 1); - MESA_load_profile_int_def(profile, section, "mho_thread_safe", &mho_thread_safe, 0); - MESA_load_profile_int_def(profile, section, "mho_mutex_num", &mho_mutex_num, 12); - MESA_load_profile_int_def(profile, section, "mho_hash_slot_size", &mho_hash_slot_size, 1234); - MESA_load_profile_int_def(profile, section, "mho_hash_max_element_num", &mho_hash_max_element_num, 12345); - MESA_load_profile_int_def(profile, section, "mho_expire_time", &mho_expire_time, 3600); - MESA_load_profile_string_def(profile, section, "mho_eliminate_type", mho_eliminate_type, sizeof(mho_eliminate_type), "FIFO"); +void kni_get_htable_opt(struct kni_htable_opt *opt, const char *profile, const char *section, void *free_data_cb, void *expire_notify_cb, void *logger){ + MESA_load_profile_int_def(profile, section, "mho_screen_print_ctrl", &(opt->mho_screen_print_ctrl), 1); + MESA_load_profile_int_def(profile, section, "mho_thread_safe", &(opt->mho_thread_safe), 0); + MESA_load_profile_int_def(profile, section, "mho_mutex_num", &(opt->mho_mutex_num), 12); + MESA_load_profile_int_def(profile, section, "mho_hash_slot_size", &(opt->mho_hash_slot_size), 1234); + MESA_load_profile_int_def(profile, section, "mho_hash_max_element_num", &(opt->mho_hash_max_element_num), 12345); + MESA_load_profile_int_def(profile, section, "mho_expire_time", &(opt->mho_expire_time), 0); + MESA_load_profile_string_def(profile, section, "mho_eliminate_type", opt->mho_eliminate_type, sizeof(opt->mho_eliminate_type), "FIFO"); KNI_LOG_ERROR(logger, "MESA_prof_load, [%s]:\n mho_screen_print_ctrl: %d\n mho_thread_safe: %d\n mho_mutex_num: %d\n" "mho_hash_slot_size: %d\n mho_hash_max_element_num: %d\n mho_expire_time: %d\n mho_eliminate_type: %s\n", section, - mho_screen_print_ctrl, mho_thread_safe, mho_mutex_num, mho_hash_slot_size, mho_hash_max_element_num, mho_expire_time, mho_eliminate_type); + opt->mho_screen_print_ctrl, opt->mho_thread_safe, opt->mho_mutex_num, opt->mho_hash_slot_size, opt->mho_hash_max_element_num, + opt->mho_expire_time, opt->mho_eliminate_type); + opt->free_data_cb = free_data_cb; + opt->expire_notify_cb = expire_notify_cb; +} + +MESA_htable_handle kni_create_htable(char *symbol, struct kni_htable_opt *opt, void *logger){ MESA_htable_handle htable = MESA_htable_born(); - if(htable == NULL) - { + if(htable == NULL){ KNI_LOG_ERROR(logger, "MESA_htable: failed at MESA_htable_born"); return NULL; } - __wrapper_MESA_htable_set_opt(htable, MHO_SCREEN_PRINT_CTRL, mho_screen_print_ctrl, logger, section); - __wrapper_MESA_htable_set_opt(htable, MHO_THREAD_SAFE, mho_thread_safe, logger, section); - __wrapper_MESA_htable_set_opt(htable, MHO_MUTEX_NUM, mho_mutex_num, logger, section); - __wrapper_MESA_htable_set_opt(htable, MHO_HASH_SLOT_SIZE, mho_hash_slot_size, logger, section); - __wrapper_MESA_htable_set_opt(htable, MHO_HASH_MAX_ELEMENT_NUM, mho_hash_max_element_num, logger, section); - __wrapper_MESA_htable_set_opt(htable, MHO_EXPIRE_TIME, mho_expire_time, logger, section); - if(strncmp(mho_eliminate_type, "LRU", KNI_SYMBOL_MAX) == 0) - { - __wrapper_MESA_htable_set_opt(htable, MHO_ELIMIMINATE_TYPE, HASH_ELIMINATE_ALGO_LRU, logger, section); + __wrapper_MESA_htable_set_opt(htable, MHO_SCREEN_PRINT_CTRL, opt->mho_screen_print_ctrl, logger, symbol); + __wrapper_MESA_htable_set_opt(htable, MHO_THREAD_SAFE, opt->mho_thread_safe, logger, symbol); + __wrapper_MESA_htable_set_opt(htable, MHO_MUTEX_NUM, opt->mho_mutex_num, logger, symbol); + __wrapper_MESA_htable_set_opt(htable, MHO_HASH_SLOT_SIZE, opt->mho_hash_slot_size, logger, symbol); + __wrapper_MESA_htable_set_opt(htable, MHO_HASH_MAX_ELEMENT_NUM, opt->mho_hash_max_element_num, logger, symbol); + __wrapper_MESA_htable_set_opt(htable, MHO_EXPIRE_TIME, opt->mho_expire_time, logger, symbol); + if(strncmp(opt->mho_eliminate_type, "LRU", KNI_SYMBOL_MAX) == 0){ + __wrapper_MESA_htable_set_opt(htable, MHO_ELIMIMINATE_TYPE, HASH_ELIMINATE_ALGO_LRU, logger, symbol); } - else - { - __wrapper_MESA_htable_set_opt(htable, MHO_ELIMIMINATE_TYPE, HASH_ELIMINATE_ALGO_FIFO, logger, section); + else{ + __wrapper_MESA_htable_set_opt(htable, MHO_ELIMIMINATE_TYPE, HASH_ELIMINATE_ALGO_FIFO, logger, symbol); } - if(free_data_cb != NULL){ + if(opt->free_data_cb != NULL){ __wrapper_MESA_htable_set_opt(htable, MHO_CBFUN_DATA_FREE, - (void *)free_data_cb, sizeof(free_data_cb), logger, section); + (void *)(opt->free_data_cb), sizeof(opt->free_data_cb), logger, symbol); } - if(expire_notify_cb != NULL){ + if(opt->expire_notify_cb != NULL){ __wrapper_MESA_htable_set_opt(htable, MHO_CBFUN_DATA_EXPIRE_NOTIFY, - (void *)expire_notify_cb, sizeof(free_data_cb), logger, section); + (void *)(opt->expire_notify_cb), sizeof(opt->expire_notify_cb), logger, symbol); } int ret = MESA_htable_mature(htable); - if(unlikely(ret != 0)) - { - KNI_LOG_ERROR(logger, "MESA_htable: failed at MESA_htable_mature, htable is %s", section); + if(unlikely(ret != 0)){ + KNI_LOG_ERROR(logger, "MESA_htable: failed at MESA_htable_mature, htable is %s", symbol); return NULL; } return htable; diff --git a/entry/src/kni_entry.cpp b/entry/src/kni_entry.cpp index fd13b1d..8c9dbc2 100644 --- a/entry/src/kni_entry.cpp +++ b/entry/src/kni_entry.cpp @@ -2298,8 +2298,10 @@ extern "C" int kni_init(){ g_kni_handle->send_logger = send_logger; //init traceid2pme_htable - traceid2pme_htable = kni_create_htable(profile, "traceid2pme_htable", NULL, - (void*)traceid2pme_htable_expire_notify_cb, local_logger); + struct kni_htable_opt opt; + memset(&opt, 0, sizeof(opt)); + kni_get_htable_opt(&opt, profile, "traceid2pme_htable", NULL, (void*)traceid2pme_htable_expire_notify_cb, local_logger); + traceid2pme_htable = kni_create_htable("traceid2pme_htable", &opt, local_logger); if(traceid2pme_htable == NULL){ KNI_LOG_ERROR(local_logger, "Failed at create traceid2pme_htable"); goto error_out; @@ -2308,9 +2310,10 @@ extern "C" int kni_init(){ //init tuple2stream_htable g_kni_handle->threads_handle = ALLOC(struct per_thread_handle, g_kni_handle->thread_count); + memset(&opt, 0, sizeof(opt)); + kni_get_htable_opt(&opt, profile, "tuple2stream_htable", (void*)tuple2stream_htable_data_free_cb, NULL, local_logger); for(int i = 0; i < g_kni_handle->thread_count; i++){ - MESA_htable_handle tuple2stream_htable = kni_create_htable(profile, "tuple2stream_htable", - (void*)tuple2stream_htable_data_free_cb, NULL, local_logger); + MESA_htable_handle tuple2stream_htable = kni_create_htable("tuple2stream_htable", &opt, local_logger); if(tuple2stream_htable == NULL){ KNI_LOG_ERROR(local_logger, "Failed at kni_create_htable, table = tuple2stream_htable"); goto error_out;