Skip to content

Commit

Permalink
Avoid overflow on Windows high resolution timer queries (#791)
Browse files Browse the repository at this point in the history
* Avoid overflow on Windows high resolution timer queries once system has been up long enough
  • Loading branch information
bretambrose authored Apr 16, 2021
1 parent be3e4ff commit 8a98947
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
9 changes: 9 additions & 0 deletions include/aws/common/clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ AWS_STATIC_IMPL uint64_t aws_timestamp_convert(
enum aws_timestamp_unit convert_to,
uint64_t *remainder);

/**
* More general form of aws_timestamp_convert that takes arbitrary frequencies rather than the timestamp enum.
*/
AWS_STATIC_IMPL uint64_t aws_timestamp_convert_u64(
uint64_t timestamp,
uint64_t convert_from_frequency,
uint64_t convert_to_frequency,
uint64_t *remainder);

/**
* Get ticks in nanoseconds (usually 100 nanosecond precision) on the high resolution clock (most-likely TSC). This
* clock has no bearing on the actual system time. On success, timestamp will be set.
Expand Down
23 changes: 16 additions & 7 deletions include/aws/common/clock.inl
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ AWS_EXTERN_C_BEGIN
* to 0 first if you care about that kind of thing. If conversion would lead to integer overflow, the timestamp
* returned will be the highest possible time that is representable, i.e. UINT64_MAX.
*/
AWS_STATIC_IMPL uint64_t aws_timestamp_convert(
AWS_STATIC_IMPL uint64_t aws_timestamp_convert_u64(
uint64_t timestamp,
enum aws_timestamp_unit convert_from,
enum aws_timestamp_unit convert_to,
uint64_t convert_from_frequency,
uint64_t convert_to_frequency,
uint64_t *remainder) {
uint64_t diff = 0;

if (convert_to > convert_from) {
diff = convert_to / convert_from;
if (convert_to_frequency > convert_from_frequency) {
diff = convert_to_frequency / convert_from_frequency;
return aws_mul_u64_saturating(timestamp, diff);
} else if (convert_to < convert_from) {
diff = convert_from / convert_to;
} else if (convert_to_frequency < convert_from_frequency) {
diff = convert_from_frequency / convert_to_frequency;

if (remainder) {
*remainder = timestamp % diff;
Expand All @@ -42,6 +42,15 @@ AWS_STATIC_IMPL uint64_t aws_timestamp_convert(
}
}

AWS_STATIC_IMPL uint64_t aws_timestamp_convert(
uint64_t timestamp,
enum aws_timestamp_unit convert_from,
enum aws_timestamp_unit convert_to,
uint64_t *remainder) {

return aws_timestamp_convert_u64(timestamp, convert_from, convert_to, remainder);
}

AWS_EXTERN_C_END

#endif /* AWS_COMMON_CLOCK_INL */
5 changes: 2 additions & 3 deletions source/windows/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ int aws_high_res_clock_get_ticks(uint64_t *timestamp) {
LARGE_INTEGER ticks, frequency;
/* QPC runs on sub-microsecond precision, convert to nanoseconds */
if (QueryPerformanceFrequency(&frequency) && QueryPerformanceCounter(&ticks)) {
*timestamp = aws_timestamp_convert((uint64_t)ticks.QuadPart, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_MICROS, NULL) /
(uint64_t)frequency.QuadPart;
*timestamp =
aws_timestamp_convert((uint64_t)ticks.QuadPart, (uint64_t)frequency.QuadPart, AWS_TIMESTAMP_NANOS, NULL);

*timestamp = aws_timestamp_convert(*timestamp, AWS_TIMESTAMP_MICROS, AWS_TIMESTAMP_NANOS, NULL);
return AWS_OP_SUCCESS;
}

Expand Down

0 comments on commit 8a98947

Please sign in to comment.