Skip to content

bperf: Avoid "time going back" in per thread reader #380

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions hbt/src/perf_event/BPerfPerThreadReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,21 @@ int BPerfPerThreadReader::read(struct BPerfThreadData* data) {
initial_clock_drift_;
time_after_offset_update =
data->monoTime - raw_thread_data.offset_update_time;

// We use rdtsc to estimate enabled and running time. However,
// kernel occasionally adjusts the algorithm of tsc based timer.
// Therefore, extra care is need to avoid "time going back" issue.
// Please refer to the comment before kernel function
// ktime_get_mono_fast_ns() for more information.
//
// Here, we make time_after_offset_update 0.1% smaller. So that
// the time never goes back. This is not an issue for time_enabled
// and time_running, as they are only used adjust PMC reading. However,
// cpuTime may show some spikes.

time_after_offset_update = time_after_offset_update * 1023;
time_after_offset_update = time_after_offset_update >> 10;

data->cpuTime =
raw_thread_data.runtime_until_offset_update + time_after_offset_update;

Expand Down