diff options
| author | chenzizhan <[email protected]> | 2023-12-18 10:36:10 +0800 |
|---|---|---|
| committer | chenzizhan <[email protected]> | 2023-12-18 10:36:10 +0800 |
| commit | c19b0d08be3ce70641c1c5707d3e67fa3c50c72e (patch) | |
| tree | 79be6982308e69ccd35b905536dc0fe79edc47d9 | |
| parent | a3bd4934eb46e67d76489e2fec597e9cb30a84de (diff) | |
feat: fieldstat easy resetv4.4.4
| -rw-r--r-- | include/fieldstat/fieldstat_easy.h | 4 | ||||
| -rw-r--r-- | src/fieldstat_easy.c | 29 | ||||
| -rw-r--r-- | test/test_easy_fs.cpp | 17 |
3 files changed, 47 insertions, 3 deletions
diff --git a/include/fieldstat/fieldstat_easy.h b/include/fieldstat/fieldstat_easy.h index b222644..8b12780 100644 --- a/include/fieldstat/fieldstat_easy.h +++ b/include/fieldstat/fieldstat_easy.h @@ -21,6 +21,10 @@ struct fieldstat_easy *fieldstat_easy_new(int max_thread_num, const char *name, */ void fieldstat_easy_free(struct fieldstat_easy *fse); /* + * reset the instance. Will fail and return -1 if fieldstat_easy_enable_auto_output is called, since the behavior is hard to define. +*/ +int fieldstat_easy_reset(struct fieldstat_easy *fse); +/* * enable auto output. both data of accumulated and delta will be output. * @param output_path: output file path. Should be identical to the one in python config. * @param interval_second: output interval in second. diff --git a/src/fieldstat_easy.c b/src/fieldstat_easy.c index 61f17c5..a3fc148 100644 --- a/src/fieldstat_easy.c +++ b/src/fieldstat_easy.c @@ -256,9 +256,11 @@ struct fieldstat *merge_all_instance(struct fieldstat_easy *fse) } // add the outputted data - pthread_spin_lock(&fse->outputting_lock); - fieldstat_merge(dst, fse->accumulate); - pthread_spin_unlock(&fse->outputting_lock); + if (fse->output_thread_running) { + pthread_spin_lock(&fse->outputting_lock); + fieldstat_merge(dst, fse->accumulate); + pthread_spin_unlock(&fse->outputting_lock); + } return dst; } @@ -318,3 +320,24 @@ int fieldstat_easy_histogram_record(struct fieldstat_easy *fse, int thread_id, i return ret; } + +int fieldstat_easy_reset(struct fieldstat_easy *fse) +{ + // We can't let the user reset operation affects auto passive output, just let it fail. + if (fse->output_thread_running) { + printf("fieldstat_easy_reset: reset is not allowed when auto output is enabled.\n"); + return -1; + } + + for (int i = 0; i < fse->max_thread_num; i++) { + pthread_spin_lock(&fse->fsu[i].lock); + fieldstat_reset(fse->fsu[i].active); + // fsu.read_only: Only for output_to_file. + pthread_spin_unlock(&fse->fsu[i].lock); + } + + // accumulate: Only has data when output_to_file. + // delta: Only for output_to_file. + + return 0; +}
\ No newline at end of file diff --git a/test/test_easy_fs.cpp b/test/test_easy_fs.cpp index 54e22f6..d6f9177 100644 --- a/test/test_easy_fs.cpp +++ b/test/test_easy_fs.cpp @@ -199,6 +199,23 @@ TEST(test_easy_fieldstat, ensure_data_racing_of_two_output_and_of_incyby) remove(FILENAME); } +TEST(test_easy_fieldstat, reset) +{ + struct fieldstat_easy *fse = fieldstat_easy_new(3, NULL, NULL, 0); + int counter_id = fieldstat_easy_register_counter(fse, "metric counter"); + fieldstat_easy_counter_incrby(fse, 0, counter_id, &TEST_TAG_INT, 1, 1); + fieldstat_easy_counter_incrby(fse, 1, counter_id, &TEST_TAG_INT, 1, 1); + + fieldstat_easy_reset(fse); + char *buff = NULL; + size_t buff_len = 0; + fieldstat_easy_output(fse, &buff, &buff_len); + EXPECT_STREQ(buff, "[]"); + + free(buff); + fieldstat_easy_free(fse); +} + int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); |
