summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchenzizhan <[email protected]>2023-07-26 17:44:22 +0800
committerchenzizhan <[email protected]>2023-07-26 17:44:22 +0800
commit32e33f087084fc576fe6ebcae25d6a7ddebe1f77 (patch)
treef9767fcb7b95463099adcf248e51a9c57db35ad0
parent78229d953247c7913eae65824afd18da36f557e0 (diff)
malloc+memset to calloc
-rw-r--r--src/exporter/cjson_exporter.c3
-rw-r--r--src/fieldstat.c29
-rw-r--r--src/metrics/metric.c9
-rw-r--r--src/tags/cell_manager.c47
-rw-r--r--src/tags/heavy_keeper.c5
-rw-r--r--test/CMakeLists.txt2
6 files changed, 57 insertions, 38 deletions
diff --git a/src/exporter/cjson_exporter.c b/src/exporter/cjson_exporter.c
index 8b6c185..315c8cc 100644
--- a/src/exporter/cjson_exporter.c
+++ b/src/exporter/cjson_exporter.c
@@ -131,8 +131,7 @@ long long get_current_time()
/* iter */
/* -------------------------------------------------------------------------- */
struct cell_iter *cell_iter_new(const struct fieldstat *instance) {
- struct cell_iter *iter = malloc(sizeof(struct cell_iter));
- memset(iter, 0, sizeof(struct cell_iter));
+ struct cell_iter *iter = calloc(1, sizeof(struct cell_iter));
iter->instance = instance;
fieldstat_get_cubes(instance, &iter->cube_ids, &iter->n_cube);
diff --git a/src/fieldstat.c b/src/fieldstat.c
index 6086e35..3c976bb 100644
--- a/src/fieldstat.c
+++ b/src/fieldstat.c
@@ -47,14 +47,11 @@ struct fieldstat {
struct fieldstat *fieldstat_new()
{
- struct fieldstat *instance = malloc(sizeof(struct fieldstat));
- memset(instance, 0, sizeof(struct fieldstat));
+ struct fieldstat *instance = calloc(1, sizeof(struct fieldstat));
instance->max_n_cube = 100;
- instance->cube = malloc(sizeof(struct fs_cube *) * instance->max_n_cube);
- memset(instance->cube, 0, sizeof(struct fs_cube *) * instance->max_n_cube);
- instance->cube_version = malloc(sizeof(unsigned long) * instance->max_n_cube);
- memset(instance->cube_version, 0, sizeof(unsigned long) * instance->max_n_cube);
+ instance->cube = calloc(instance->max_n_cube, sizeof(struct fs_cube *));
+ instance->cube_version = calloc(instance->max_n_cube, sizeof(unsigned long));
return instance;
}
@@ -178,8 +175,7 @@ int fieldstat_append_cube_to_instance(struct fieldstat *instance, struct fs_cube
instance->max_n_cube *= 2;
instance->cube = realloc(instance->cube, sizeof(struct fs_cube *) * instance->max_n_cube);
unsigned long *old_ver_arr = instance->cube_version;
- instance->cube_version = malloc(sizeof(unsigned long) * instance->max_n_cube);
- memset(instance->cube_version, 0, sizeof(unsigned long) * instance->max_n_cube);
+ instance->cube_version = calloc(instance->max_n_cube, sizeof(unsigned long));
memcpy(instance->cube_version, old_ver_arr, sizeof(unsigned long) * instance->valid_cube_arr_length);
free(old_ver_arr);
}
@@ -188,12 +184,11 @@ int fieldstat_append_cube_to_instance(struct fieldstat *instance, struct fs_cube
return instance->valid_cube_arr_length - 1;
}
-void fieldstat_cube_info_init(struct fs_cube *cube, const struct fieldstat_tag *shared_tags, size_t n_tag, enum sampling_mode mode, size_t max_n_cell)
+struct fs_cube *fieldstat_cube_info_init(const struct fieldstat_tag *shared_tags, size_t n_tag, enum sampling_mode mode, size_t max_n_cell)
{
- memset(cube, 0, sizeof(struct fs_cube));
+ struct fs_cube *cube = calloc(1, sizeof(struct fs_cube));
cube->sampling_mode = mode;
cube->max_n_cell = max_n_cell;
- cube->next_cell_id = 0;
cube->shared_tags = malloc(sizeof(struct fieldstat_tag) * n_tag);
for (int i = 0; i < n_tag; i++) {
struct fieldstat_tag *dest_tag = &cube->shared_tags[i];
@@ -220,13 +215,13 @@ void fieldstat_cube_info_init(struct fs_cube *cube, const struct fieldstat_tag *
cube->max_n_metric = 16;
cube->metrics = malloc(sizeof(struct metric *) * cube->max_n_metric);
- cube->n_metric = 0;
+
+ return cube;
}
struct fs_cube *fieldstat_cube_new(const struct fieldstat_tag *shared_tags, size_t n_tag, enum sampling_mode mode, size_t max_n_cell)
{
- struct fs_cube *cube = malloc(sizeof(struct fs_cube));
- fieldstat_cube_info_init(cube, shared_tags, n_tag, mode, max_n_cell);
+ struct fs_cube *cube = fieldstat_cube_info_init(shared_tags, n_tag, mode, max_n_cell);
cube->cell_manager = cell_manager_new(mode, max_n_cell);
@@ -558,8 +553,7 @@ struct fieldstat *fieldstat_deserialize(const char *blob, size_t blob_size)
fieldtag_list_deserialize(mpack_node_bin_data(shared_tags_node), mpack_node_bin_size(shared_tags_node), &shared_tags, &n_shared_tags);
int sampling_mode = mpack_node_i8(mpack_node_map_cstr(cube, "sampling_mode"));
int max_n_cell = mpack_node_i64(mpack_node_map_cstr(cube, "max_n_cell"));
- struct fs_cube *fs_cube = malloc(sizeof(struct fs_cube));
- fieldstat_cube_info_init(fs_cube, shared_tags, n_shared_tags, sampling_mode, max_n_cell);
+ struct fs_cube *fs_cube = fieldstat_cube_info_init(shared_tags, n_shared_tags, sampling_mode, max_n_cell);
fieldtag_list_free(shared_tags, n_shared_tags);
mpack_node_t metrics_node = mpack_node_map_cstr(cube, "metrics");
@@ -587,8 +581,7 @@ struct fieldstat *fieldstat_deserialize(const char *blob, size_t blob_size)
struct fs_cube *fieldstat_cube_dup(const struct fs_cube *cube)
{
- struct fs_cube *cube_dup = malloc(sizeof(struct fs_cube));
- fieldstat_cube_info_init(cube_dup, cube->shared_tags, cube->n_shared_tags, cube->sampling_mode, cube->max_n_cell);
+ struct fs_cube *cube_dup = fieldstat_cube_info_init(cube->shared_tags, cube->n_shared_tags, cube->sampling_mode, cube->max_n_cell);
struct cell_manager *cm_src = cube->cell_manager;
struct cell_manager *cm_dup = cell_manager_copy(cm_src);
diff --git a/src/metrics/metric.c b/src/metrics/metric.c
index 2d352a6..af36494 100644
--- a/src/metrics/metric.c
+++ b/src/metrics/metric.c
@@ -398,13 +398,11 @@ void metric_info_free(struct metric_info *info)
struct metric *metric_new(const char *name, enum metric_type type, struct metric_parameter *para)
{
- struct metric *pthis = (struct metric *)malloc(sizeof(struct metric));
- memset(pthis, 0, sizeof(struct metric));
+ struct metric *pthis = (struct metric *)calloc(1, sizeof(struct metric));
pthis->info = metric_info_new(name, type, para);
pthis->scheme = &g_metric_scheme_table[type];
- pthis->data_array = (struct metric_measure_data **)malloc(sizeof(struct metric_measure_data *) * MAX_N_DATA_ARRAY_ITEM_INIT);
pthis->max_n_array_item = MAX_N_DATA_ARRAY_ITEM_INIT;
- memset(pthis->data_array, 0, sizeof(struct metric_measure_data *) * MAX_N_DATA_ARRAY_ITEM_INIT);
+ pthis->data_array = (struct metric_measure_data **)calloc(MAX_N_DATA_ARRAY_ITEM_INIT, sizeof(struct metric_measure_data *));
return pthis;
}
@@ -451,9 +449,8 @@ struct metric *metric_copy_structure(const struct metric *src)
dst->scheme = &g_metric_scheme_table[src->info->type];
dst->n_array_item = 0;
- dst->data_array = (struct metric_measure_data **)malloc(sizeof(struct metric_measure_data *) * src->max_n_array_item);
dst->max_n_array_item = src->max_n_array_item;
- memset(dst->data_array, 0, sizeof(struct metric_measure_data *) * src->max_n_array_item);
+ dst->data_array = (struct metric_measure_data **)calloc(src->max_n_array_item, sizeof(struct metric_measure_data *));
return dst;
}
diff --git a/src/tags/cell_manager.c b/src/tags/cell_manager.c
index 42efe81..fe8415b 100644
--- a/src/tags/cell_manager.c
+++ b/src/tags/cell_manager.c
@@ -33,16 +33,14 @@ struct cell_manager {
struct cell_manager *cell_manager_new_without_map(enum sampling_mode sampling_mode, int max_cell_num)
{
- struct cell_manager *cell_manager = (struct cell_manager *)malloc(sizeof(struct cell_manager));
- memset(cell_manager, 0, sizeof(struct cell_manager));
+ struct cell_manager *cell_manager = (struct cell_manager *)calloc(1, sizeof(struct cell_manager));
cell_manager->sampling_mode = sampling_mode;
cell_manager->max_cell_num = max_cell_num;
cell_manager->next_cell_id = 0;
cell_manager->id_tag_array_len = max_cell_num;
- cell_manager->id_tag_array = (const struct tag_hash_key **)malloc(sizeof(struct tag_hash_key *) * cell_manager->id_tag_array_len);
- memset(cell_manager->id_tag_array, 0, sizeof(struct tag_hash_key *) * cell_manager->id_tag_array_len);
+ cell_manager->id_tag_array = (const struct tag_hash_key **)calloc(max_cell_num, sizeof(struct tag_hash_key *));
cell_manager->max_cell_id = -1;
return cell_manager;
@@ -84,8 +82,8 @@ void cell_manager_reset(struct cell_manager *pthis)
free(pthis->id_tag_array);
pthis->max_cell_id = -1;
- pthis->id_tag_array = (const struct tag_hash_key **)malloc(sizeof(struct tag_hash_key *) * pthis->id_tag_array_len);
- memset(pthis->id_tag_array, 0, sizeof(struct tag_hash_key *) * pthis->id_tag_array_len);
+
+ pthis->id_tag_array = (const struct tag_hash_key **)calloc(pthis->id_tag_array_len, sizeof(struct tag_hash_key *));
}
void cell_manager_free(struct cell_manager *pthis)
@@ -118,8 +116,8 @@ void cell_manager_id_tag_array_add(struct cell_manager *pthis, int cell_id, cons
while (cell_id >= new_len) {
new_len *= 2;
}
- struct tag_hash_key **new_array = (struct tag_hash_key **)malloc(sizeof(struct tag_hash_key *) * new_len);
- memset(new_array, 0, sizeof(struct tag_hash_key *) * new_len);
+
+ struct tag_hash_key **new_array = (struct tag_hash_key **)calloc(new_len, sizeof(struct tag_hash_key *));
memcpy(new_array, pthis->id_tag_array, sizeof(struct tag_hash_key *) * pthis->id_tag_array_len);
free(pthis->id_tag_array);
pthis->id_tag_array = (const struct tag_hash_key **)new_array;
@@ -133,6 +131,39 @@ void cell_manager_id_tag_array_add(struct cell_manager *pthis, int cell_id, cons
}
}
+// todo: 如果一个用例仅做添加操作,那不需要id tag map
+// 在query / merge/ serialize时再生成?
+// 最麻烦的是,这样就有两种instance,感觉有点怪。
+int cell_manager_update_id_tag_map(struct cell_manager *pthis)
+{
+ if (pthis->id_tag_array != NULL) {
+ printf("ERR: id_tag_array is not NULL when update_id_tag_map\n");
+ return -1;
+ }
+
+ size_t id_tag_array_len = pthis->max_cell_num;
+ if (pthis->sampling_mode == SAMPLING_MODE_TOPK && pthis->next_cell_id > id_tag_array_len) {
+ id_tag_array_len = pthis->next_cell_id;
+ }
+ pthis->id_tag_array = calloc(id_tag_array_len, sizeof(struct tag_hash_key *));
+
+ if (pthis->sampling_mode == SAMPLING_MODE_TOPK) {
+ struct heavy_keeper_result *result = heavy_keeper_query(pthis->topk_tag_id_map);
+ for (size_t i = 0; i < result->n_key; i++) {
+ cell_manager_id_tag_array_add(pthis, result->cell_id[i], result->tags[i]);
+ }
+ heavy_keeper_result_free(result);
+ } else {
+ struct tag_id_map *node = NULL;
+ struct tag_id_map *tmp = NULL;
+ HASH_ITER(hh, pthis->comprehensive_tag_id_map, node, tmp) {
+ pthis->id_tag_array[node->cell_id] = node->tag;
+ }
+ }
+
+ return 0;
+}
+
int cell_manager_get_cell_id_by_tag(const struct cell_manager *pthis, const struct tag_hash_key *tag)
{
int cell_id = -1;
diff --git a/src/tags/heavy_keeper.c b/src/tags/heavy_keeper.c
index 063ed48..9755009 100644
--- a/src/tags/heavy_keeper.c
+++ b/src/tags/heavy_keeper.c
@@ -33,9 +33,8 @@ struct heavy_keeper {
struct Bucket *new_sketch(struct heavy_keeper_options *params) {
size_t array_len = (size_t)params->array_num * (size_t)params->max_bucket_num;
- size_t alloc_size = array_len * sizeof(struct Bucket);
- struct Bucket *ret = (struct Bucket *)malloc(alloc_size);
- memset(ret, 0, alloc_size);
+
+ struct Bucket *ret = (struct Bucket *)calloc(array_len, sizeof(struct Bucket));
return ret;
}
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 7572948..a20f916 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -42,7 +42,7 @@ function (add_unit_test file_name)
endfunction()
# add_unit_test(test_exporter_json)
-# add_unit_test(test_fuzz_test)
+add_unit_test(test_fuzz_test)
add_unit_test(test_merge)
add_unit_test(test_metric_counter)
# add_unit_test(test_metric_histogram)