Use CLOCK_MONOTONIC_RAW instead of CLOCK_MONOTONIC#5099
Use CLOCK_MONOTONIC_RAW instead of CLOCK_MONOTONIC#5099renecannao merged 2 commits intosysown:v3.0from
CLOCK_MONOTONIC_RAW instead of CLOCK_MONOTONIC#5099Conversation
Signed-off-by: Wazir Ahmed <wazir@proxysql.com>
…t_time` Signed-off-by: Wazir Ahmed <wazir@proxysql.com>
There was a problem hiding this comment.
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_RAWfor time measurements, falling back toCLOCK_MONOTONICifRAWis unavailable. This ensures more stable and accurate elapsed time calculations, asCLOCK_MONOTONIC_RAWis not subject to system time adjustments like NTP. - Ensured Query Time Consistency: New
set_end_timemethods have been introduced forQuery_Info(MySQL) andPgSQL_Query_Info(PostgreSQL) to guarantee that a query'send_timeis always greater than or equal to itsstart_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 betweenCLOCK_MONOTONIC_RAWandCLOCK_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
-
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. ↩
There was a problem hiding this comment.
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.
| 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 | ||
| } |
There was a problem hiding this comment.
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.
| 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 | ||
| } |
There was a problem hiding this comment.
| #ifdef CLOCK_MONOTONIC_RAW | ||
| #define PROXYSQL_CLOCK_MONOTONIC CLOCK_MONOTONIC_RAW | ||
| #else | ||
| #define PROXYSQL_CLOCK_MONOTONIC CLOCK_MONOTONIC | ||
| #endif |
There was a problem hiding this comment.
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.
|



CLOCK_MONOTONIC_RAWwhen available and fallback toCLOCK_MONOTONICQueryInfo::end_timeis always be greater than or equal tostart_time.Closes #4950.