diff options
| author | chenzizhan <[email protected]> | 2024-07-04 15:31:24 +0800 |
|---|---|---|
| committer | chenzizhan <[email protected]> | 2024-07-04 15:31:24 +0800 |
| commit | 277de12fc277f1fc050eb5b92e8958126baa08e3 (patch) | |
| tree | 799818a9cd6f09743c78ef094042117cbe7785b4 | |
| parent | da2b236902f842903bd7643e824454eff286a15d (diff) | |
revert `array has less variable`
| -rw-r--r-- | src/cube.c | 23 | ||||
| -rw-r--r-- | src/fieldstat.c | 81 | ||||
| -rw-r--r-- | src/tags/heavy_keeper.c | 12 |
3 files changed, 58 insertions, 58 deletions
@@ -36,6 +36,7 @@ struct cube_manager { struct cell { struct metric **metrics; + size_t metrics_len; size_t max_n_metric; struct fieldstat_tag_list tags; // cell identifier }; @@ -306,7 +307,7 @@ void cube_manager_reset(struct cube_manager *pthis) struct metric *find_metric_in_cell(const struct cell *cell, int metric_id) { - if (metric_id >= cell->max_n_metric) { + if (metric_id >= cell->metrics_len) { return NULL; } return cell->metrics[metric_id]; @@ -321,6 +322,10 @@ void add_metric_to_cell(struct cell *cell, struct metric *metric, int metric_id) } cell->metrics[metric_id] = metric; + + if (metric_id >= cell->metrics_len) { + cell->metrics_len = metric_id + 1; + } } struct metric *add_or_find_metric_in_cell(const struct metric_manifest *manifest, struct cell *cell) @@ -339,6 +344,7 @@ struct cell *cell_new(const struct exdata_new_args *args) { struct cell *pthis = malloc(sizeof(struct cell)); pthis->metrics = calloc(DEFAULT_N_METRIC, sizeof(struct metric *)); pthis->max_n_metric = DEFAULT_N_METRIC; + pthis->metrics_len = 0; pthis->tags.n_tag = args->n_tags; pthis->tags.tag = malloc(sizeof(struct fieldstat_tag) * args->n_tags); @@ -347,7 +353,7 @@ struct cell *cell_new(const struct exdata_new_args *args) { } void cell_free(struct cell *pthis) { - for (size_t i = 0; i < pthis->max_n_metric; i++) { + for (size_t i = 0; i < pthis->metrics_len; i++) { metric_free(pthis->metrics[i]); } free(pthis->metrics); @@ -365,7 +371,8 @@ struct cell *cell_copy(const struct cell *src) { struct cell *pthis = malloc(sizeof(struct cell)); pthis->metrics = calloc(src->max_n_metric, sizeof(struct metric *)); pthis->max_n_metric = src->max_n_metric; - for (size_t i = 0; i < src->max_n_metric; i++) { + pthis->metrics_len = src->metrics_len; + for (size_t i = 0; i < src->metrics_len; i++) { if (src->metrics[i] == NULL) { continue; } @@ -381,7 +388,7 @@ struct cell *cell_copy(const struct cell *src) { } void cell_reset(struct cell *pthis) { - for (size_t i = 0; i < pthis->max_n_metric; i++) { + for (size_t i = 0; i < pthis->metrics_len; i++) { if (pthis->metrics[i] == NULL) { continue; } @@ -390,7 +397,7 @@ void cell_reset(struct cell *pthis) { } void cell_merge(struct cell *dest, const struct cell *src) { - for (size_t i = 0; i < src->max_n_metric; i++) { + for (size_t i = 0; i < src->metrics_len; i++) { const struct metric *metric_src = src->metrics[i]; if (metric_src == NULL) { continue; @@ -833,7 +840,7 @@ const struct metric *get_metric_by_tag_list(const struct cube *cube, const struc return NULL; } - if (metric_id < 0 || metric_id >= data->max_n_metric) { + if (metric_id < 0 || metric_id >= data->metrics_len) { *ret = FS_ERR_INVALID_METRIC_ID; return NULL; } @@ -934,9 +941,9 @@ void cube_get_cells_used_by_metric(const struct cube *cube, const struct fieldst return; } - *metric_id_out = (int *)malloc(sizeof(int) * cell_data->max_n_metric); + *metric_id_out = (int *)malloc(sizeof(int) * cell_data->metrics_len); int n_metric = 0; - for (int i = 0; i < cell_data->max_n_metric; i++) { + for (int i = 0; i < cell_data->metrics_len; i++) { if (cell_data->metrics[i] != NULL) { (*metric_id_out)[n_metric] = i; n_metric++; diff --git a/src/fieldstat.c b/src/fieldstat.c index dca5246..cd689c9 100644 --- a/src/fieldstat.c +++ b/src/fieldstat.c @@ -20,8 +20,8 @@ struct fieldstat { struct metric_manifest **manifests; // TODO: 把那个哈希表再加回去 - size_t max_n_manifests; - // TODO: 就三个吧,还有一个count + size_t manifest_size; + size_t manifest_cnt; struct cube_manager *cube_manager; }; @@ -130,10 +130,7 @@ bool is_tag_array_same(const struct fieldstat_tag *tags1, const struct fieldstat bool is_metric_name_duplicate(const struct fieldstat *instance, const char *name) { - for (size_t i = 0; i < instance->max_n_manifests; i++) { - if (instance->manifests[i] == NULL) { - continue; - } + for (size_t i = 0; i < instance->manifest_cnt; i++) { if (strcmp(instance->manifests[i]->name, name) == 0) { return true; } @@ -148,15 +145,15 @@ struct fieldstat *fieldstat_new() { struct fieldstat *instance = calloc(1, sizeof(struct fieldstat)); - instance->max_n_manifests = DEFAULT_N_METRIC; - instance->manifests = calloc(instance->max_n_manifests, sizeof(struct metric_manifest *)); + instance->manifest_size = DEFAULT_N_METRIC; + instance->manifests = calloc(instance->manifest_size, sizeof(struct metric_manifest *)); + instance->manifest_cnt = 0; instance->cube_manager = cube_manager_new(); return instance; } -void fieldstat_cube_free(struct fieldstat *instance, int cube_id); void fieldstat_free(struct fieldstat *instance) { if (instance == NULL) { @@ -165,10 +162,8 @@ void fieldstat_free(struct fieldstat *instance) cube_manager_free(instance->cube_manager); - for (size_t i = 0; i < instance->max_n_manifests; i++) { - if (instance->manifests[i] != NULL) { - metric_manifest_free(instance->manifests[i]); - } + for (size_t i = 0; i < instance->manifest_cnt; i++) { + metric_manifest_free(instance->manifests[i]); } free(instance->manifests); @@ -248,7 +243,7 @@ int fieldstat_cube_set_primary_metric(struct fieldstat *instance, int cube_id, i if (cube == NULL) { return FS_ERR_INVALID_CUBE_ID; } - if (metric_id < 0 || metric_id >= instance->max_n_manifests) { + if (metric_id < 0 || metric_id >= instance->manifest_cnt) { return FS_ERR_INVALID_METRIC_ID; } if (instance->manifests[metric_id] == NULL) { @@ -269,24 +264,21 @@ int fieldstat_cube_set_primary_metric(struct fieldstat *instance, int cube_id, i void add_manifest_to_instance(struct fieldstat *instance, const struct metric_manifest *manifest, int metric_id) { - if (metric_id >= instance->max_n_manifests) { - instance->manifests = realloc(instance->manifests, sizeof(struct metric_manifest *) * instance->max_n_manifests * 2); - memset(instance->manifests + instance->max_n_manifests, 0, sizeof(struct metric_manifest *) * (instance->max_n_manifests)); - instance->max_n_manifests *= 2; + if (metric_id >= instance->manifest_size) { + instance->manifests = realloc(instance->manifests, sizeof(struct metric_manifest *) * instance->manifest_size * 2); + memset(instance->manifests + instance->manifest_size, 0, sizeof(struct metric_manifest *) * (instance->manifest_size)); + instance->manifest_size *= 2; } instance->manifests[metric_id] = (struct metric_manifest *)manifest; + if (metric_id >= instance->manifest_cnt) { + instance->manifest_cnt = metric_id + 1; + } } static int append_manifest_to_instance(struct fieldstat *instance, const struct metric_manifest *metric) { - int metric_id = 0; - for ( ;metric_id < instance->max_n_manifests; metric_id++) { - if (instance->manifests[metric_id] == NULL) { - break; - } - } - + int metric_id = instance->manifest_cnt; add_manifest_to_instance(instance, metric, metric_id); return metric_id; @@ -386,7 +378,7 @@ int check_before_add(const struct fieldstat *instance, int cube_id, int metric_i return FS_ERR_INVALID_CUBE_ID; } - if (metric_id < 0 || metric_id >= instance->max_n_manifests) { + if (metric_id < 0 || metric_id >= instance->manifest_cnt) { return FS_ERR_INVALID_METRIC_ID; } const struct metric_manifest *metric = instance->manifests[metric_id]; @@ -461,7 +453,7 @@ int fieldstat_merge(struct fieldstat *instance, struct fieldstat *src) return FS_ERR_NULL_HANDLER; } - int metric_len_src = src->max_n_manifests; + int metric_len_src = src->manifest_cnt; for (int i = 0; i < metric_len_src; i++) { const struct metric_manifest *metric_src = src->manifests[i]; const struct metric_manifest *metric_dst = instance->manifests[i]; @@ -496,12 +488,11 @@ struct fieldstat *fieldstat_fork(const struct fieldstat *instance) struct fieldstat *new_instance = calloc(1, sizeof(struct fieldstat)); new_instance->cube_manager = cube_manager_fork(instance->cube_manager); - new_instance->manifests = calloc(instance->max_n_manifests, sizeof(struct metric_manifest *)); - new_instance->max_n_manifests = instance->max_n_manifests; - for (size_t i = 0; i < instance->max_n_manifests; i++) { - if (instance->manifests[i] != NULL) { - new_instance->manifests[i] = metric_manifest_copy(instance->manifests[i]); - } + new_instance->manifests = calloc(instance->manifest_size, sizeof(struct metric_manifest *)); + new_instance->manifest_size = instance->manifest_size; + new_instance->manifest_cnt = instance->manifest_cnt; + for (size_t i = 0; i < instance->manifest_cnt; i++) { + new_instance->manifests[i] = metric_manifest_copy(instance->manifests[i]); } return new_instance; @@ -509,16 +500,16 @@ struct fieldstat *fieldstat_fork(const struct fieldstat *instance) void calibrate_metrics_in_instance(const struct fieldstat *master, struct fieldstat *replica) { - if (replica->max_n_manifests < master->max_n_manifests) { - replica->manifests = (struct metric_manifest **)realloc(replica->manifests, sizeof(struct metric_manifest *) * master->max_n_manifests); - memset(replica->manifests + replica->max_n_manifests, 0, sizeof(struct metric_manifest *) * (master->max_n_manifests - replica->max_n_manifests)); - replica->max_n_manifests = master->max_n_manifests; + if (replica->manifest_size < master->manifest_size) { + replica->manifests = (struct metric_manifest **)realloc(replica->manifests, sizeof(struct metric_manifest *) * master->manifest_size); + memset(replica->manifests + replica->manifest_size, 0, sizeof(struct metric_manifest *) * (master->manifest_size - replica->manifest_size)); + replica->manifest_size = master->manifest_size; } - size_t longer_arr_len = master->max_n_manifests > replica->max_n_manifests ? master->max_n_manifests : replica->max_n_manifests; + size_t longer_arr_len = master->manifest_cnt > replica->manifest_cnt ? master->manifest_cnt : replica->manifest_cnt; for (size_t i = 0; i < longer_arr_len; i++) { - const struct metric_manifest *metric_master = i >= master->max_n_manifests ? NULL : master->manifests[i]; - struct metric_manifest *metric_target = i >= replica->max_n_manifests ? NULL : replica->manifests[i]; + const struct metric_manifest *metric_master = i >= master->manifest_cnt ? NULL : master->manifests[i]; + struct metric_manifest *metric_target = i >= replica->manifest_cnt ? NULL : replica->manifests[i]; if (metric_master == NULL && metric_target == NULL) { continue; } @@ -542,6 +533,8 @@ void calibrate_metrics_in_instance(const struct fieldstat *master, struct fields // metric same, no need to do anything } + + replica->manifest_cnt = master->manifest_cnt; } int fieldstat_calibrate(const struct fieldstat *master, struct fieldstat *replica) @@ -562,10 +555,10 @@ void fieldstat_get_cubes(const struct fieldstat *instance, int **cube_ids, int * void fieldstat_get_metrics(const struct fieldstat *instance, int **metric_id_out, size_t *n_metric) { - int *tmp_ids = (int *)malloc(sizeof(int) * instance->max_n_manifests); + int *tmp_ids = (int *)malloc(sizeof(int) * instance->manifest_cnt); *metric_id_out = tmp_ids; int cnt = 0; - for (int i = 0; i < instance->max_n_manifests; i++) { + for (int i = 0; i < instance->manifest_cnt; i++) { if (instance->manifests[i] != NULL) { tmp_ids[cnt] = i; cnt ++; @@ -664,7 +657,7 @@ void fieldstat_tag_list_arr_free(struct fieldstat_tag_list *tag_list, size_t n_c const char *fieldstat_get_metric_name(const struct fieldstat *instance, int metric_id) { - if (metric_id < 0 || metric_id >= instance->max_n_manifests) { + if (metric_id < 0 || metric_id >= instance->manifest_cnt) { return NULL; } const struct metric_manifest *metric = instance->manifests[metric_id]; @@ -677,7 +670,7 @@ const char *fieldstat_get_metric_name(const struct fieldstat *instance, int metr enum metric_type fieldstat_get_metric_type(const struct fieldstat *instance, int metric_id) { - if (instance == NULL || metric_id < 0 || metric_id >= instance->max_n_manifests) { + if (instance == NULL || metric_id < 0 || metric_id >= instance->manifest_cnt) { return (enum metric_type)(-1); } const struct metric_manifest *metric = instance->manifests[metric_id]; diff --git a/src/tags/heavy_keeper.c b/src/tags/heavy_keeper.c index f887a94..85c9dad 100644 --- a/src/tags/heavy_keeper.c +++ b/src/tags/heavy_keeper.c @@ -609,12 +609,12 @@ int heavy_keeper_add(struct heavy_keeper *heavy_keeper, const char *key, size_t struct Bucket *bucket = find_bucket_by_key(heavy_keeper, j, key, key_len); if (bucket->finger_print == FP) { - // If a flow is not in the min-heap, then the estimated flow size should be no larger than nmin. - // or if the min-heap is not full(nmin == 0), every flow should be taken into account, so of course it should be added. + // If a key is not in the min-heap, then the estimated key size should be no larger than nmin. + // or if the min-heap is not full(nmin == 0), every key should be taken into account, so of course it should be added. // in neither case, bucket->count > nMin && not_in_sorted_set happen. - // The flows whose counts are both larger than nmin and not in min-heap must have the same xxhash value, and its FP stored in bucket represents another tag, - // In this case, the sketch won't be updated. This flow is expected to be taken into account in another array, - // where its FP is different from the one it should collided with, so that element flows won't be missed. + // The keys whose counts are both larger than nmin and not in min-heap must have the same xxhash value, and its FP stored in bucket represents another tag, + // In this case, the sketch won't be updated. This key is expected to be taken into account in another array, + // where its FP is different from the one it should collided with, so that element keys won't be missed. if (not_in_sorted_set) { unsigned tmp_min = sorted_set_get_min_count(summary); if (bucket->count > tmp_min) { @@ -716,7 +716,7 @@ static void heavy_keeper_merge_sketch(struct heavy_keeper *dest, const struct he if (bucket_dest->finger_print == bucket_src->finger_print) { bucket_dest->count = safe_add(bucket_dest->count, bucket_src->count); } else { - if (bucket_dest->count < bucket_src->count) { // bucket_src stores the elephant flow. + if (bucket_dest->count < bucket_src->count) { bucket_dest->count = bucket_src->count; bucket_dest->finger_print = bucket_src->finger_print; } |
