From 073e1c427235efcc3d9cd6c7b6c31eb348c2ddfa Mon Sep 17 00:00:00 2001 From: Zach Date: Wed, 29 Jun 2022 10:29:15 -0400 Subject: [PATCH] Add ability to clear statistics Signed-off-by: Zach --- rttest/include/rttest/rttest.h | 4 +++ rttest/src/rttest.cpp | 54 ++++++++++++++++++++++++++++------ 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/rttest/include/rttest/rttest.h b/rttest/include/rttest/rttest.h index 9e42ef3..18bc195 100644 --- a/rttest/include/rttest/rttest.h +++ b/rttest/include/rttest/rttest.h @@ -175,6 +175,10 @@ int rttest_calculate_statistics(struct rttest_results * results); /// \return Error code if results struct is NULL int rttest_get_statistics(struct rttest_results * results); +/// \brief Clear all statistics in the sample buffer +/// \return Error code to propagate to main +int rttest_clear_statistics(); + /// \brief Get latency sample at the given iteration. /// \param[in] iteration Iteration of the test to get the sample from /// \param[out] The resulting sample: time in nanoseconds between the expected diff --git a/rttest/src/rttest.cpp b/rttest/src/rttest.cpp index 04ca18e..2e8592e 100644 --- a/rttest/src/rttest.cpp +++ b/rttest/src/rttest.cpp @@ -72,6 +72,10 @@ class Rttest rttest_sample_buffer sample_buffer; struct rusage prev_usage; + // The iteration that the results were cleared at + // Gets set when cleared_statistics() is called + size_t cleared_iteration = 0; + pthread_t thread_id; int record_jitter( @@ -122,6 +126,8 @@ class Rttest int calculate_statistics(struct rttest_results * results); + void clear_statistics(); + int get_sample_at(const size_t iteration, int64_t & sample) const; int write_results(); @@ -754,23 +760,25 @@ int Rttest::calculate_statistics(struct rttest_results * output) return -1; } - output->min_latency = *std::min_element( - this->sample_buffer.latency_samples.begin(), this->sample_buffer.latency_samples.end()); - output->max_latency = *std::max_element( - this->sample_buffer.latency_samples.begin(), this->sample_buffer.latency_samples.end()); + std::vector latency_samples( + this->sample_buffer.latency_samples.begin() + this->cleared_iteration + 1, + this->sample_buffer.latency_samples.end()); + + output->min_latency = *std::min_element(latency_samples.begin(), latency_samples.end()); + output->max_latency = *std::max_element(latency_samples.begin(), latency_samples.end()); output->mean_latency = std::accumulate( - this->sample_buffer.latency_samples.begin(), - this->sample_buffer.latency_samples.end(), 0.0) / this->sample_buffer.latency_samples.size(); + latency_samples.begin(), + latency_samples.end(), 0.0) / latency_samples.size(); // Calculate standard deviation and try to avoid overflow - output->latency_stddev = calculate_stddev(this->sample_buffer.latency_samples); + output->latency_stddev = calculate_stddev(latency_samples); output->minor_pagefaults = std::accumulate( - this->sample_buffer.minor_pagefaults.begin(), + this->sample_buffer.minor_pagefaults.begin() + this->cleared_iteration + 1, this->sample_buffer.minor_pagefaults.end(), 0); output->major_pagefaults = std::accumulate( - this->sample_buffer.major_pagefaults.begin(), + this->sample_buffer.major_pagefaults.begin() + this->cleared_iteration + 1, this->sample_buffer.major_pagefaults.end(), 0); return 0; @@ -785,6 +793,34 @@ int rttest_calculate_statistics(struct rttest_results * results) return thread_rttest_instance->calculate_statistics(results); } +void Rttest::clear_statistics() +{ + size_t i; + if (this->params.iterations == 0) { + i = 0; + } else { + i = this->results.iteration; + } + this->cleared_iteration = i; + + // Reset the properties of the current results + this->results.max_latency = this->sample_buffer.latency_samples[i]; + this->results.min_latency = this->results.max_latency; + this->results.mean_latency = this->results.max_latency; + this->results.minor_pagefaults = this->sample_buffer.minor_pagefaults[i]; + this->results.major_pagefaults = this->sample_buffer.major_pagefaults[i]; +} + +int rttest_clear_statistics() +{ + auto thread_rttest_instance = get_rttest_thread_instance(pthread_self()); + if (!thread_rttest_instance) { + return -1; + } + thread_rttest_instance->clear_statistics(); + return 0; +} + int rttest_get_statistics(struct rttest_results * output) { if (output == NULL) {