refactor ex_data_runtime & fix all leak memory

This commit is contained in:
liuwentan
2023-04-05 21:09:19 +08:00
parent 5d545d6dbf
commit fb3896c078
26 changed files with 438 additions and 693 deletions

View File

@@ -26,9 +26,6 @@ target_link_libraries(adapter_hs_gtest maat_frame_static gtest_static)
add_executable(maat_ex_data_gtest maat_ex_data_gtest.cpp)
target_link_libraries(maat_ex_data_gtest maat_frame_static gtest_static)
add_executable(maat_garbage_collection_gtest maat_garbage_collection_gtest.cpp)
target_link_libraries(maat_garbage_collection_gtest maat_frame_static gtest_static)
file(COPY table_info.conf DESTINATION ./)
file(COPY file_test_tableinfo.conf DESTINATION ./)
file(COPY literal_expr.conf DESTINATION ./)

View File

@@ -59,14 +59,23 @@ TEST(EXDataRuntime, Update) {
ASSERT_GT(table_id, 0);
int ex_data_counter = 0;
struct ex_data_runtime *ex_data_rt = ex_data_runtime_new(table_id, ex_container_free, g_logger);
struct ex_data_schema *ex_schema = ex_data_schema_new(ex_data_new_cb, ex_data_free_cb, ex_data_dup_cb,
0, &ex_data_counter);
struct ex_data_runtime *ex_data_rt = ex_data_runtime_new(table_id, g_logger);
struct ex_container_schema *container_schema = ALLOC(struct ex_container_schema, 1);
container_schema->custom_data_free = NULL;
container_schema->table_id = table_id;
container_schema->ex_schema.new_func = ex_data_new_cb;
container_schema->ex_schema.free_func = ex_data_free_cb;
container_schema->ex_schema.dup_func = ex_data_dup_cb;
container_schema->ex_schema.argl = 0;
container_schema->ex_schema.argp = &ex_data_counter;
container_schema->set_flag = 1;
ex_data_runtime_set_ex_container_schema(ex_data_rt, container_schema);
const char *row1 = "1\t192.168.0.1\tmahuateng\t1\t0";
const char *key1 = "192.168.0.1";
size_t key1_len = strlen(key1);
void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, ex_schema, table_name, row1, key1, key1_len);
void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, table_name, row1, key1, key1_len);
EXPECT_EQ(ex_data_counter, 1);
struct ex_container *ex_container = ex_container_new(ex_data, NULL);
@@ -76,14 +85,14 @@ TEST(EXDataRuntime, Update) {
const char *row2 = "2\t192.168.0.2\tliyanhong\t1\t0";
const char *key2 = "192.168.0.2";
size_t key2_len = strlen(key2);
ex_data = ex_data_runtime_row2ex_data(ex_data_rt, ex_schema, table_name, row2, key2, key2_len);
ex_data = ex_data_runtime_row2ex_data(ex_data_rt, table_name, row2, key2, key2_len);
ex_container = ex_container_new(ex_data, NULL);
ret = ex_data_runtime_add_ex_container(ex_data_rt, key2, key2_len, ex_container);
EXPECT_EQ(ret, 0);
ex_data_runtime_commit(ex_data_rt);
void *res_data1 = ex_data_runtime_get_ex_data_by_key(ex_data_rt, ex_schema, "192.168.0.1", 11);
void *res_data1 = ex_data_runtime_get_ex_data_by_key(ex_data_rt, "192.168.0.1", 11);
EXPECT_TRUE(res_data1 != NULL);
struct user_info *info = (struct user_info *)res_data1;
@@ -92,7 +101,7 @@ TEST(EXDataRuntime, Update) {
ex_data_free_cb(table_id, (void **)&res_data1, 0, NULL);
ex_container = NULL;
void *res_data2 = ex_data_runtime_get_ex_data_by_key(ex_data_rt, ex_schema, "192.168.0.2", 11);
void *res_data2 = ex_data_runtime_get_ex_data_by_key(ex_data_rt, "192.168.0.2", 11);
EXPECT_TRUE(res_data2 != NULL);
info = (struct user_info *)res_data2;
@@ -101,7 +110,6 @@ TEST(EXDataRuntime, Update) {
ex_data_free_cb(table_id, (void **)&res_data2, 0, NULL);
ex_data_runtime_free(ex_data_rt);
ex_data_schema_free(ex_schema);
}
int main(int argc, char ** argv)

View File

@@ -95,6 +95,9 @@ int write_config_to_redis(char *redis_ip, int redis_port, int redis_db,
redisReply *reply = maat_cmd_wrap_redis_command(c, "flushdb");
if (NULL == reply) {
return -1;
} else {
freeReplyObject(reply);
reply = NULL;
}
if (0 == access(json_iris_path, F_OK)) {
@@ -2569,7 +2572,7 @@ TEST_F(CompileTable, CompileRuleUpdate) {
int ret = compile_table_set_line(maat_instance, compile_table_name, MAAT_OP_ADD, compile_id, NULL, 1, 0);
EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S);
sleep(WAIT_FOR_EFFECTIVE_S * 2);
ret = compile_table_set_line(maat_instance, compile_table_name, MAAT_OP_DEL, compile_id, NULL, 1, 0);
EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S);
@@ -4144,7 +4147,7 @@ TEST_F(MaatCmdTest, SetFile) {
ret = maat_cmd_set_line(maat_instance, &line_rule);
EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S);//wait for callback triggered.
sleep(WAIT_FOR_EFFECTIVE_S * 2);//wait for callback triggered.
EXPECT_EQ(g_test_foregin_read_OK, 1);
g_test_foreign_del_OK = 0;
@@ -4185,7 +4188,7 @@ TEST_F(MaatCmdTest, SetFile) {
ret = maat_cmd_set_line(maat_instance, &line_rule);
EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S);//wait for callback triggered.
sleep(WAIT_FOR_EFFECTIVE_S * 2);//wait for callback triggered.
EXPECT_EQ(g_test_foregin_read_OK, 1);
}
@@ -4497,7 +4500,7 @@ TEST_F(MaatCmdTest, UpdateBoolPlugin) {
EXPECT_GT(ret, 0);
}
sleep(WAIT_FOR_EFFECTIVE_S);
sleep(WAIT_FOR_EFFECTIVE_S * 2);
int ex_data_counter = 0;
ret = maat_plugin_table_ex_schema_register(maat_instance, table_name,
@@ -4528,7 +4531,7 @@ TEST_F(MaatCmdTest, UpdateBoolPlugin) {
ret = maat_cmd_set_line(maat_instance, &line_rule);
EXPECT_GT(ret, 0);
}
sleep(WAIT_FOR_EFFECTIVE_S);
sleep(WAIT_FOR_EFFECTIVE_S * 2);
memset(results, 0, sizeof(results));
ret = maat_bool_plugin_table_get_ex_data(maat_instance, table_id, items, 3,
@@ -4607,7 +4610,7 @@ TEST_F(MaatCmdTest, GroupInMassCompiles) {
target_compile_id, 0, "null", 2, 0);
EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S);
sleep(WAIT_FOR_EFFECTIVE_S * 2);
const char* http_url1 = "https://www.baidu.com/s?wd=tsg";
const char* http_url2 = "https://www.baidu.com/s?wd=zhengzhou&rsv_spt=1&rsv_iqid=0x8b4cae8100000560&issp=1&f=8&rsv_bp=1";
@@ -5014,7 +5017,7 @@ TEST_F(MaatCmdTest, GroupEdit) {
scan_app_id, scan_app_id, NULL, 0);
EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S);
sleep(WAIT_FOR_EFFECTIVE_S * 2);
table_id = maat_get_table_id(maat_instance, ip_table_name);
ASSERT_GT(table_id, 0);
@@ -5086,7 +5089,7 @@ TEST_F(MaatCmdTest, CompileDelete_TSG6548) {
IPv4, "192.168.73.163", "192.168.73.180", 0, 65535, 0);
EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S);
sleep(WAIT_FOR_EFFECTIVE_S * 2);
uint32_t ip_addr;
inet_pton(AF_INET, "192.168.73.169", &ip_addr);

View File

@@ -100,6 +100,9 @@ static int write_config_to_redis(char *redis_ip, int redis_port, int redis_db,
redisReply *reply = maat_cmd_wrap_redis_command(c, "flushdb");
if (NULL == reply) {
return -1;
} else {
freeReplyObject(reply);
reply = NULL;
}
if (0 == access(json_iris_path, F_OK)) {

View File

@@ -1,48 +0,0 @@
#include <limits.h>
#include "maat_utils.h"
#include "maat_garbage_collection.h"
#include <gtest/gtest.h>
struct user_info {
char user_name[NAME_MAX];
int user_id;
};
struct user_info *user_info_new(const char *user_name, int user_id)
{
struct user_info *info = ALLOC(struct user_info, 1);
memcpy(info->user_name, user_name, sizeof(info->user_name));
info->user_id = user_id;
return info;
}
void user_info_free(struct user_info *user_info)
{
free(user_info);
}
void garbage_user_info_free(void *user_info, void *arg)
{
struct user_info *info = (struct user_info *)user_info;
user_info_free(info);
}
TEST(garbage_collection, basic)
{
struct maat_garbage_bin *garbage_bin = maat_garbage_bin_new(2);
struct user_info *info = user_info_new("zhangsan", 101);
maat_garbage_bagging(garbage_bin, info, NULL, garbage_user_info_free);
maat_garbage_collect_routine(garbage_bin);
maat_garbage_bin_free(garbage_bin);
}
int main(int argc, char ** argv)
{
int ret=0;
::testing::InitGoogleTest(&argc, argv);
ret=RUN_ALL_TESTS();
return ret;
}

View File

@@ -168,6 +168,8 @@ TEST(redis_mode, maat_scan_string) {
redisReply *reply = maat_cmd_wrap_redis_command(c, "flushdb");
EXPECT_TRUE(reply != NULL);
freeReplyObject(reply);
reply = NULL;
if (access(json_iris_path, F_OK) < 0) {
char tmp_iris_path[128] = {0};

View File

@@ -16,13 +16,13 @@ void data_free(void *user_ctx, void *data)
}
TEST(rcu_hash_new, invalid_input_parameter) {
struct rcu_hash_table *htable = rcu_hash_new(NULL);
struct rcu_hash_table *htable = rcu_hash_new(NULL, NULL);
EXPECT_TRUE(htable == NULL);
}
TEST(rcu_hash_add_one_node, single_thread) {
/* add one node to hash */
struct rcu_hash_table *htable = rcu_hash_new(data_free);
struct rcu_hash_table *htable = rcu_hash_new(data_free, NULL);
EXPECT_TRUE(htable != NULL);
struct user_data *data = ALLOC(struct user_data, 1);
@@ -69,7 +69,7 @@ TEST(rcu_hash_add_one_node, single_thread) {
TEST(rcu_hash_add_multi_node, single_thread) {
/* add multi node to hash */
struct rcu_hash_table *htable = rcu_hash_new(data_free);
struct rcu_hash_table *htable = rcu_hash_new(data_free, NULL);
EXPECT_TRUE(htable != NULL);
struct user_data *data0 = ALLOC(struct user_data, 1);
@@ -163,7 +163,7 @@ TEST(rcu_hash_add_multi_node, single_thread) {
TEST(rcu_hash_del_one_node, single_thread) {
/* case1: add and del before commit */
struct rcu_hash_table *htable = rcu_hash_new(data_free);
struct rcu_hash_table *htable = rcu_hash_new(data_free, NULL);
EXPECT_TRUE(htable != NULL);
struct user_data *data = ALLOC(struct user_data, 1);
@@ -243,7 +243,7 @@ TEST(rcu_hash_del_one_node, single_thread) {
TEST(rcu_hash_del_multi_node, single_thread) {
/* case1: add and del before commit */
struct rcu_hash_table *htable = rcu_hash_new(data_free);
struct rcu_hash_table *htable = rcu_hash_new(data_free, NULL);
EXPECT_TRUE(htable != NULL);
struct user_data *data1 = ALLOC(struct user_data, 1);
@@ -288,7 +288,7 @@ TEST(rcu_hash_del_multi_node, single_thread) {
}
TEST(rcu_hash_add_with_same_key, single_thread) {
struct rcu_hash_table *htable = rcu_hash_new(data_free);
struct rcu_hash_table *htable = rcu_hash_new(data_free, NULL);
EXPECT_TRUE(htable != NULL);
char key[64] = "http_url";
@@ -327,7 +327,7 @@ TEST(rcu_hash_add_with_same_key, single_thread) {
}
TEST(rcu_hash_del_with_same_key, single_thread) {
struct rcu_hash_table *htable = rcu_hash_new(data_free);
struct rcu_hash_table *htable = rcu_hash_new(data_free, NULL);
EXPECT_TRUE(htable != NULL);
char key[64] = "http_url";
@@ -372,7 +372,7 @@ TEST(rcu_hash_del_with_same_key, single_thread) {
}
TEST(rcu_hash_modify_with_same_key, single_thread) {
struct rcu_hash_table *htable = rcu_hash_new(data_free);
struct rcu_hash_table *htable = rcu_hash_new(data_free, NULL);
EXPECT_TRUE(htable != NULL);
char key[64] = "http_url";
@@ -457,95 +457,13 @@ TEST(global_rcu_hash_del, single_thread) {
res = rcu_hash_find(g_htable, key, key_len);
EXPECT_TRUE(res == NULL);
}
#define THREAD_NUM 10
struct thread_param {
int thread_id;
int test_count;
unsigned long long time_elapse_ms;
};
void *rcu_scan_thread(void *arg)
{
struct thread_param *param = (struct thread_param *)arg;
int hit_cnt = 0;
void **data_array = NULL;
sleep(2);
for (int index = 0; index < param->test_count; index++) {
size_t count = rcu_hash_list(g_htable, &data_array);
for (size_t i = 0; i < count; i++) {
struct user_data *user_data = (struct user_data *)data_array[i];
if (user_data != NULL && user_data->id == 101) {
hit_cnt++;
}
}
}
int *is_all_hit = ALLOC(int, 1);
*is_all_hit = (hit_cnt == param->test_count) ? 1 : 0;
printf("thread[%d]: hit_cnt:%d\n", param->thread_id, hit_cnt);
return is_all_hit;
}
void *rcu_update_thread(void *arg)
{
const char *key = "http_url";
size_t key_len = strlen(key);
for (int i = 0; i < 10; i++) {
rcu_hash_del(g_htable, key, key_len);
rcu_hash_commit(g_htable);
sleep(1);
struct user_data *data = ALLOC(struct user_data, 1);
data->id = 101;
char name[64] = "www.baidu.com";
memcpy(data->name, name, strlen(name));
rcu_hash_add(g_htable, key, key_len, data);
rcu_hash_commit(g_htable);
}
int *is_all_hit = ALLOC(int, 1);
*is_all_hit = 1;
return is_all_hit;
}
TEST(rcu_hash_update, multi_thread) {
pthread_t threads[THREAD_NUM + 1];
struct thread_param t_param[THREAD_NUM + 1];
for (int i = 0; i < THREAD_NUM + 1; i++) {
t_param[i].thread_id = i;
t_param[i].test_count = 1000 * 1000;
t_param[i].time_elapse_ms = 0;
if (i < THREAD_NUM) {
pthread_create(&threads[i], NULL, rcu_scan_thread, t_param+i);
} else {
pthread_create(&threads[i], NULL, rcu_update_thread, t_param+i);
}
}
int *is_all_hit = NULL;
unsigned long long time_elapse_ms = 0, scan_count = 0;
for (int i = 0; i < THREAD_NUM + 1; i++) {
pthread_join(threads[i], (void **)&is_all_hit);
time_elapse_ms += t_param[i].time_elapse_ms;
scan_count += t_param[i].test_count;
EXPECT_EQ(*is_all_hit, 1);
*is_all_hit = 0;
free(is_all_hit);
}
}
int main(int argc, char ** argv)
{
int ret=0;
::testing::InitGoogleTest(&argc, argv);
g_htable = rcu_hash_new(data_free);
g_htable = rcu_hash_new(data_free, NULL);
ret=RUN_ALL_TESTS();