Skip to content

Use CLOCK_MONOTONIC_RAW instead of CLOCK_MONOTONIC#5099

Merged
renecannao merged 2 commits intosysown:v3.0from
wazir-ahmed:clock-monotonic-raw
Sep 29, 2025
Merged

Use CLOCK_MONOTONIC_RAW instead of CLOCK_MONOTONIC#5099
renecannao merged 2 commits intosysown:v3.0from
wazir-ahmed:clock-monotonic-raw

Conversation

@wazir-ahmed
Copy link
Member

@wazir-ahmed wazir-ahmed commented Sep 3, 2025

  • Use CLOCK_MONOTONIC_RAW when available and fallback to CLOCK_MONOTONIC
  • Ensure QueryInfo::end_time is always be greater than or equal to start_time.

Closes #4950.

Signed-off-by: Wazir Ahmed <wazir@proxysql.com>
…t_time`

Signed-off-by: Wazir Ahmed <wazir@proxysql.com>
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @wazir-ahmed, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refines the system's timekeeping mechanisms to enhance the reliability and precision of performance metrics, particularly for query execution times. It achieves this by adopting a more robust clock source and implementing safeguards to prevent logical errors in time interval calculations.

Highlights

  • Improved Time Measurement Accuracy: The system now prioritizes CLOCK_MONOTONIC_RAW for time measurements, falling back to CLOCK_MONOTONIC if RAW is unavailable. This ensures more stable and accurate elapsed time calculations, as CLOCK_MONOTONIC_RAW is not subject to system time adjustments like NTP.
  • Ensured Query Time Consistency: New set_end_time methods have been introduced for Query_Info (MySQL) and PgSQL_Query_Info (PostgreSQL) to guarantee that a query's end_time is always greater than or equal to its start_time. This addresses potential inconsistencies in query duration tracking.
  • Standardized Clock Usage: A new macro, PROXYSQL_CLOCK_MONOTONIC, has been defined and applied across the codebase to centralize the choice between CLOCK_MONOTONIC_RAW and CLOCK_MONOTONIC, promoting consistency and maintainability in time-related operations.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly implements the switch to CLOCK_MONOTONIC_RAW when available, which is a good improvement for time measurements as it's not affected by NTP adjustments. The fallback logic to ensure end_time is always greater than or equal to start_time is also a solid addition for robustness.

My review focuses on improving maintainability by addressing some code duplication. I've identified two instances of duplicated logic that could be refactored into shared components. These changes will make the codebase cleaner and easier to maintain in the long run.

Comment on lines +109 to +124
inline void Query_Info::set_end_time(unsigned long long time) {
end_time = time;

#ifndef CLOCK_MONOTONIC_RAW
if (start_time <= end_time)
return;

// If start_time is greater than end_time, assign current monotonic time
end_time = monotonic_time();
if (start_time <= end_time)
return;

// If start_time is still greater than end_time, set the difference to 0
end_time = start_time;
#endif // CLOCK_MONOTONIC_RAW
}

Choose a reason for hiding this comment

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

medium

The implementation of set_end_time is identical to the one in PgSQL_Query_Info::set_end_time. To improve maintainability and avoid code duplication, this logic should be extracted into a shared helper function. For example, you could create a helper function in gen_utils.h and call it from both methods.

Comment on lines +196 to +211
inline void PgSQL_Query_Info::set_end_time(unsigned long long time) {
end_time = time;

#ifndef CLOCK_MONOTONIC_RAW
if (start_time <= end_time)
return;

// If start_time is greater than end_time, assign current monotonic time
end_time = monotonic_time();
if (start_time <= end_time)
return;

// If start_time is still greater than end_time, set the difference to 0
end_time = start_time;
#endif // CLOCK_MONOTONIC_RAW
}

Choose a reason for hiding this comment

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

medium

The implementation of set_end_time is duplicated in Query_Info::set_end_time. To improve maintainability and avoid code duplication, this logic should be extracted into a shared helper function. For example, you could create a helper function in gen_utils.h and call it from both methods.

Comment on lines +22 to +26
#ifdef CLOCK_MONOTONIC_RAW
#define PROXYSQL_CLOCK_MONOTONIC CLOCK_MONOTONIC_RAW
#else
#define PROXYSQL_CLOCK_MONOTONIC CLOCK_MONOTONIC
#endif

Choose a reason for hiding this comment

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

medium

The definition of PROXYSQL_CLOCK_MONOTONIC is duplicated from include/gen_utils.h. To avoid this duplication, consider moving this macro to a more specific, shared header file that can be included by both gen_utils.h and this file. Alternatively, if dependencies allow, test/tap/tap/utils.h could include include/gen_utils.h directly.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Sep 3, 2025

@renecannao renecannao merged commit e1b7afe into sysown:v3.0 Sep 29, 2025
6 of 7 checks passed
@noizu noizu added this to the Release 3.0.3 milestone Sep 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Evaluate the use of CLOCK_MONOTONIC_RAW clock in clock_gettime()

3 participants