summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchenzizhan <[email protected]>2023-11-22 16:30:51 +0800
committerchenzizhan <[email protected]>2023-11-22 16:30:51 +0800
commitd852831c317699ff9a2a3b98dd32364dbf43d249 (patch)
treeab47135b0fb7362edac4a2ee608488b4e323ae92
parentacb5ba9106ff03fc8e1adc2d748ba607eb3fb4f1 (diff)
copy tag selectively(tag_hash_key_store)
-rw-r--r--build_and_run_all_test.sh4
-rw-r--r--src/fieldstat.c1
-rw-r--r--src/tags/my_ut_hash.c49
-rw-r--r--src/tags/my_ut_hash.h4
-rw-r--r--test/unit_test_cell_manager.cpp1
5 files changed, 40 insertions, 19 deletions
diff --git a/build_and_run_all_test.sh b/build_and_run_all_test.sh
index 643d087..9450f01 100644
--- a/build_and_run_all_test.sh
+++ b/build_and_run_all_test.sh
@@ -7,8 +7,8 @@ make
cd test
for f in *test*; do
echo "Running $f"
- ./$f
- # valgrind --tool=memcheck --leak-check=full ./$f
+ # ./$f
+ valgrind --tool=memcheck --leak-check=full ./$f
wait
done
# lcov --capture --directory . --output-file AnyName.info
diff --git a/src/fieldstat.c b/src/fieldstat.c
index 2e71952..b109ad3 100644
--- a/src/fieldstat.c
+++ b/src/fieldstat.c
@@ -282,6 +282,7 @@ struct fs_cube *fieldstat_cube_info_init(const struct fieldstat_tag *shared_tags
cube->n_shared_tags = n_tag;
struct tag_hash_key *shared_tag_key = tag_hash_key_construct_with_fieldstat_tag(shared_tags, n_tag);
+ tag_hash_key_store(shared_tag_key);
cube->key_tag = shared_tag_key;
cube->max_n_metric = 64;
diff --git a/src/tags/my_ut_hash.c b/src/tags/my_ut_hash.c
index 9d96cd5..13051bf 100644
--- a/src/tags/my_ut_hash.c
+++ b/src/tags/my_ut_hash.c
@@ -11,12 +11,12 @@
#include "serializer.h"
-
struct tag_hash_key {
struct fieldstat_tag *tags; // not used for hash, just to recover the original tags.
size_t n_my_tag;
unsigned int hashv;
+ bool is_deep_copy;
};
unsigned int tag_hash_key_cal_hash_value(const struct tag_hash_key *my_hash_key)
@@ -34,25 +34,27 @@ int tag_hash_key_cmp(const struct tag_hash_key *a, const struct tag_hash_key *b)
return 1;
}
for (size_t i = 0; i < a->n_my_tag; i++) {
- if (strcmp(a->tags[i].key, b->tags[i].key) != 0) {
+ const struct fieldstat_tag *tag_a = &a->tags[i];
+ const struct fieldstat_tag *tag_b = &b->tags[i];
+ if (strcmp(tag_a->key, tag_b->key) != 0) {
return 1;
}
- if (a->tags[i].type != b->tags[i].type) {
+ if (tag_a->type != tag_b->type) {
return 1;
}
- switch (a->tags[i].type) {
+ switch (tag_a->type) {
case TAG_INTEGER:
- if (a->tags[i].value_longlong != b->tags[i].value_longlong) {
+ if (tag_a->value_longlong != tag_b->value_longlong) {
return 1;
}
break;
case TAG_DOUBLE:
- if (a->tags[i].value_double != b->tags[i].value_double) {
+ if (tag_a->value_double != tag_b->value_double) {
return 1;
}
break;
case TAG_CSTRING:
- if (strcmp(a->tags[i].value_str, b->tags[i].value_str) != 0) {
+ if (strcmp(tag_a->value_str, tag_b->value_str) != 0) {
return 1;
}
break;
@@ -220,14 +222,9 @@ struct tag_hash_key *tag_hash_key_construct_with_fieldstat_tag(const struct fiel
{
struct tag_hash_key *ret_hash_key = (struct tag_hash_key *)malloc(sizeof(struct tag_hash_key));
- if (n_src == 0) {
- ret_hash_key->tags = NULL;
- } else {
- ret_hash_key->tags = (struct fieldstat_tag *)malloc(sizeof(struct fieldstat_tag) * n_src);
- for (int i = 0; i < n_src; i++) {
- fieldtag_copy(&ret_hash_key->tags[i], &src[i]);
- }
- }
+ ret_hash_key->tags = n_src == 0 ? NULL : (struct fieldstat_tag *)src;
+ ret_hash_key->n_my_tag = n_src;
+ ret_hash_key->is_deep_copy = false;
ret_hash_key->n_my_tag = n_src;
@@ -237,6 +234,23 @@ struct tag_hash_key *tag_hash_key_construct_with_fieldstat_tag(const struct fiel
return ret_hash_key;
}
+void tag_hash_key_store(struct tag_hash_key *tag_key)
+{
+ if (tag_key->is_deep_copy) {
+ return;
+ }
+ tag_key->is_deep_copy = true;
+ if (tag_key->n_my_tag == 0) {
+ return;
+ }
+ struct fieldstat_tag *new_tags = (struct fieldstat_tag *)malloc(sizeof(struct fieldstat_tag) * tag_key->n_my_tag);
+ for (int i = 0; i < tag_key->n_my_tag; i++) {
+ fieldtag_copy(&new_tags[i], &tag_key->tags[i]);
+ }
+ tag_key->tags = new_tags;
+}
+
+
void tag_hash_key_convert_to_fieldstat_tag(const struct tag_hash_key *tag_key, struct fieldstat_tag **out, size_t *n_out)
{
size_t ret_n_tag = tag_key->n_my_tag;
@@ -259,6 +273,7 @@ struct tag_hash_key *tag_hash_key_copy(const struct tag_hash_key *src)
struct tag_hash_key *dst = (struct tag_hash_key *)malloc(sizeof(struct tag_hash_key));
dst->n_my_tag = src->n_my_tag;
dst->hashv = src->hashv;
+ dst->is_deep_copy = true;
if (dst->n_my_tag == 0) {
dst->tags = NULL;
@@ -274,7 +289,9 @@ struct tag_hash_key *tag_hash_key_copy(const struct tag_hash_key *src)
void tag_hash_key_free(struct tag_hash_key *tag_key)
{
- fieldtag_list_free(tag_key->tags, tag_key->n_my_tag);
+ if (tag_key->is_deep_copy) {
+ fieldtag_list_free(tag_key->tags, tag_key->n_my_tag);
+ }
free(tag_key);
}
diff --git a/src/tags/my_ut_hash.h b/src/tags/my_ut_hash.h
index 903747a..d641399 100644
--- a/src/tags/my_ut_hash.h
+++ b/src/tags/my_ut_hash.h
@@ -10,11 +10,13 @@ extern "C"
struct tag_hash_key;
+// note that: As to improve performance, the tag_hash_key is a shallow copy of fieldstat_tag, just after been constructed by `tag_hash_key_construct_with_fieldstat_tag`. Use `tag_hash_key_store` to make it a deep copy.
struct tag_hash_key *tag_hash_key_construct_with_fieldstat_tag(const struct fieldstat_tag *src, size_t n_src);
void tag_hash_key_convert_to_fieldstat_tag(const struct tag_hash_key *tag_key, struct fieldstat_tag **out, size_t *n_out);
-struct tag_hash_key *tag_hash_key_copy(const struct tag_hash_key *src);
+struct tag_hash_key *tag_hash_key_copy(const struct tag_hash_key *src); // Deep copy
void tag_hash_key_free(struct tag_hash_key *tag_key);
unsigned tag_hash_key_cal_fnv_hash(const struct tag_hash_key *tag, unsigned seed);
+void tag_hash_key_store(struct tag_hash_key *tag_key); // let the shallow copy of fieldstat_tag in tag_key to be deep copy, if they are shallow copy
void fieldtag_list_free(struct fieldstat_tag *tags, size_t n_tags);
char *tag_hash_key_get_compound_key(const struct tag_hash_key *tag);
diff --git a/test/unit_test_cell_manager.cpp b/test/unit_test_cell_manager.cpp
index 4134216..958d982 100644
--- a/test/unit_test_cell_manager.cpp
+++ b/test/unit_test_cell_manager.cpp
@@ -22,6 +22,7 @@ struct tag_hash_key *test_gen_tag_key(const char *key, int value)
};
struct tag_hash_key *tag_key = tag_hash_key_construct_with_fieldstat_tag(&tag, 1);
+ tag_hash_key_store(tag_key);
free((void *)tag.value_str);