Skip to content

Commit 480574c

Browse files
committed
Add ability to clear statistics
1 parent 38a217b commit 480574c

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

rttest/include/rttest/rttest.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ int rttest_calculate_statistics(struct rttest_results * results);
175175
/// \return Error code if results struct is NULL
176176
int rttest_get_statistics(struct rttest_results * results);
177177

178+
/// \brief Clear all statistics in the sample buffer
179+
/// \return Error code to propagate to main
180+
int rttest_clear_statistics();
181+
178182
/// \brief Get latency sample at the given iteration.
179183
/// \param[in] iteration Iteration of the test to get the sample from
180184
/// \param[out] The resulting sample: time in nanoseconds between the expected

rttest/src/rttest.cpp

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ class Rttest
7272
rttest_sample_buffer sample_buffer;
7373
struct rusage prev_usage;
7474

75+
// The iteration that the results were cleared at
76+
// Gets set when cleared_statistics() is called
77+
size_t cleared_iteration = 0;
78+
7579
pthread_t thread_id;
7680

7781
int record_jitter(
@@ -122,6 +126,8 @@ class Rttest
122126

123127
int calculate_statistics(struct rttest_results * results);
124128

129+
void clear_statistics();
130+
125131
int get_sample_at(const size_t iteration, int64_t & sample) const;
126132

127133
int write_results();
@@ -754,23 +760,25 @@ int Rttest::calculate_statistics(struct rttest_results * output)
754760
return -1;
755761
}
756762

757-
output->min_latency = *std::min_element(
758-
this->sample_buffer.latency_samples.begin(), this->sample_buffer.latency_samples.end());
759-
output->max_latency = *std::max_element(
760-
this->sample_buffer.latency_samples.begin(), this->sample_buffer.latency_samples.end());
763+
std::vector<int64_t> latency_samples(
764+
this->sample_buffer.latency_samples.begin() + this->cleared_iteration + 1,
765+
this->sample_buffer.latency_samples.end());
766+
767+
output->min_latency = *std::min_element(latency_samples.begin(), latency_samples.end());
768+
output->max_latency = *std::max_element(latency_samples.begin(), latency_samples.end());
761769
output->mean_latency = std::accumulate(
762-
this->sample_buffer.latency_samples.begin(),
763-
this->sample_buffer.latency_samples.end(), 0.0) / this->sample_buffer.latency_samples.size();
770+
latency_samples.begin(),
771+
latency_samples.end(), 0.0) / latency_samples.size();
764772

765773
// Calculate standard deviation and try to avoid overflow
766-
output->latency_stddev = calculate_stddev(this->sample_buffer.latency_samples);
774+
output->latency_stddev = calculate_stddev(latency_samples);
767775

768776
output->minor_pagefaults = std::accumulate(
769-
this->sample_buffer.minor_pagefaults.begin(),
777+
this->sample_buffer.minor_pagefaults.begin() + this->cleared_iteration + 1,
770778
this->sample_buffer.minor_pagefaults.end(), 0);
771779

772780
output->major_pagefaults = std::accumulate(
773-
this->sample_buffer.major_pagefaults.begin(),
781+
this->sample_buffer.major_pagefaults.begin() + this->cleared_iteration + 1,
774782
this->sample_buffer.major_pagefaults.end(), 0);
775783

776784
return 0;
@@ -785,6 +793,34 @@ int rttest_calculate_statistics(struct rttest_results * results)
785793
return thread_rttest_instance->calculate_statistics(results);
786794
}
787795

796+
void Rttest::clear_statistics()
797+
{
798+
size_t i;
799+
if (this->params.iterations == 0) {
800+
i = 0;
801+
} else {
802+
i = this->results.iteration;
803+
}
804+
this->cleared_iteration = i;
805+
806+
// Reset the properties of the current results
807+
this->results.max_latency = this->sample_buffer.latency_samples[i];
808+
this->results.min_latency = this->results.max_latency;
809+
this->results.mean_latency = this->results.max_latency;
810+
this->results.minor_pagefaults = this->sample_buffer.minor_pagefaults[i];
811+
this->results.major_pagefaults = this->sample_buffer.major_pagefaults[i];
812+
}
813+
814+
int rttest_clear_statistics()
815+
{
816+
auto thread_rttest_instance = get_rttest_thread_instance(pthread_self());
817+
if (!thread_rttest_instance) {
818+
return -1;
819+
}
820+
thread_rttest_instance->clear_statistics();
821+
return 0;
822+
}
823+
788824
int rttest_get_statistics(struct rttest_results * output)
789825
{
790826
if (output == NULL) {

0 commit comments

Comments
 (0)