Skip to content

Commit

Permalink
Fix overflow in quicktime calibration loop
Browse files Browse the repository at this point in the history
Fixes #529
  • Loading branch information
Technohacker committed Dec 5, 2024
1 parent 0cf7694 commit 1c6ef5e
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/viztracer/modules/quicktime.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,23 @@ 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, 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

0 comments on commit 1c6ef5e

Please sign in to comment.