Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to clear statistics #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we adding 1 here? Won't that always skip a sample somewhere along the way?

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];
Comment on lines +806 to +811
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could be wrong in my understanding here, but this isn't really "clearing" the results. What it is doing is setting a marker at the current set of samples, and then we'll only consider samples from this point forward.

If my understanding is correct, then I think we should rename this function to reflect that usage more. Maybe "mark_statistics" or something like that?

}

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