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

Fix overflow in quicktime calibration loop #530

Merged
merged 1 commit into from
Dec 5, 2024
Merged
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
20 changes: 16 additions & 4 deletions src/viztracer/modules/quicktime.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,24 @@ quicktime_init()
start_ns[i] = get_system_ns();
int64_t start_after = get_system_ts();
start_ts[i] = start_before + (start_after - start_before) / 2;
t0_ts += start_ts[i];
t0_ns += start_ns[i];
}

t0_ts /= CALIBRATE_SIZE;
t0_ns /= CALIBRATE_SIZE;
// Do the expensive average calculation outside the measurement loop
int64_t ts_remainder = 0;
int64_t ns_remainder = 0;
for (int i = 0; i < CALIBRATE_SIZE; i++)
{
// Divide by CALIBRATE_SIZE at each step instead of accumulate-and-divide to avoid overflow
t0_ts += start_ts[i] / CALIBRATE_SIZE;
t0_ns += start_ns[i] / CALIBRATE_SIZE;

// Also accumulate the remainders, which are unlikely to overflow
ts_remainder += start_ts[i] % CALIBRATE_SIZE;
ns_remainder += start_ns[i] % CALIBRATE_SIZE;
}
// Then finally add the average remainder
t0_ts += ts_remainder / CALIBRATE_SIZE;
t0_ns += ns_remainder / CALIBRATE_SIZE;

// Now let's find the base time

Expand Down
Loading