Skip to content

Commit

Permalink
Add ability to clear statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
ZachG-gnu committed Jun 29, 2022
1 parent 38a217b commit 480574c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
4 changes: 4 additions & 0 deletions rttest/include/rttest/rttest.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 45 additions & 9 deletions rttest/src/rttest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<int64_t> 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;
Expand All @@ -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) {
Expand Down

0 comments on commit 480574c

Please sign in to comment.