Skip to content

issue: 5170344 Fix IB HCA core clock unit scaling in time converter - #640

Open
pasis wants to merge 1 commit into
Mellanox:vNextfrom
pasis:bugfix/issue-5170344-ib-time-converter
Open

issue: 5170344 Fix IB HCA core clock unit scaling in time converter#640
pasis wants to merge 1 commit into
Mellanox:vNextfrom
pasis:bugfix/issue-5170344-ib-time-converter

Conversation

@pasis

@pasis pasis commented Jul 23, 2026

Copy link
Copy Markdown
Member

Description

time_converter_ib_ctx scaled the NIC core clock frequency with the wrong factor, corrupting hardware timestamp to system time conversion.

The constructor receives ibv_device_attr_ex::hca_core_clock, which rdma-core reports in kHz. To store it as Hz it must be multiplied by 1000, but the code multiplied by USEC_PER_SEC (1,000,000), leaving the stored clock 1000x too large.

The USEC_PER_SEC factor was only correct for the legacy experimental verbs field (ibv_exp_device_attr::hca_core_clock, expressed in MHz). The migration to rdma-core changed the unit to kHz without updating the multiplier.

Effect: in calculate_delta() tv_sec = hw_time_diff / hca_core_clock comes out 1000x too small, so converted timestamps drift from the raw hardware time and the error grows linearly. TS_CONVERSION_MODE_RAW is affected for the lifetime of the converter; TS_CONVERSION_MODE_SYNC is affected until the periodic fix_hw_clock_deviation() recomputes the rate empirically.

The kHz-to-Hz scale is applied through a dedicated HZ_PER_KHZ constant instead of the coincidentally-equal MSEC_PER_SEC, and the constructor parameter is renamed hca_core_clock_khz to document the input unit.

What

Fix time_converter_ib_ctx inaccuracy.

Why ?

Bugfix: fix time_converter_ib_ctx inaccuracy.

Change type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Tests
  • Other

Check list

  • Code follows the style de facto guidelines of this project
  • Comments have been inserted in hard to understand places
  • Documentation has been updated (if necessary)
  • Test has been added (if possible)

Summary by CodeRabbit

  • Bug Fixes
    • Improved hardware clock conversion for more accurate timestamp handling.
    • Clarified clock-rate units to ensure values provided in kHz are interpreted correctly.

time_converter_ib_ctx scaled the NIC core clock frequency with the wrong
factor, corrupting hardware timestamp to system time conversion.

The constructor receives ibv_device_attr_ex::hca_core_clock, which
rdma-core reports in kHz. To store it as Hz it must be multiplied by 1000,
but the code multiplied by USEC_PER_SEC (1,000,000), leaving the stored
clock 1000x too large.

The USEC_PER_SEC factor was only correct for the legacy experimental
verbs field (ibv_exp_device_attr::hca_core_clock, expressed in MHz). The
migration to rdma-core changed the unit to kHz without updating the
multiplier.

Effect: in calculate_delta() tv_sec = hw_time_diff / hca_core_clock comes
out 1000x too small, so converted timestamps drift from the raw hardware
time and the error grows linearly. TS_CONVERSION_MODE_RAW is affected for
the lifetime of the converter; TS_CONVERSION_MODE_SYNC is affected until
the periodic fix_hw_clock_deviation() recomputes the rate empirically.

The kHz-to-Hz scale is applied through a dedicated HZ_PER_KHZ constant
instead of the coincidentally-equal MSEC_PER_SEC, and the constructor
parameter is renamed hca_core_clock_khz to document the input unit.

Signed-off-by: Dmytro Podgornyi <dmytrop@nvidia.com>
@coderabbitai

coderabbitai Bot commented Jul 23, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Enterprise

Run ID: 287e46d0-91aa-4c22-9805-4f37af2b54e5

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • ✅ Review completed - (🔄 Check again to review again)
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@greptile-apps

greptile-apps Bot commented Jul 23, 2026

Copy link
Copy Markdown

Greptile Summary

This PR fixes a 1000× scaling error in time_converter_ib_ctx where ibv_device_attr_ex::hca_core_clock (reported in kHz by rdma-core) was multiplied by USEC_PER_SEC (1,000,000) instead of the correct HZ_PER_KHZ (1,000), causing the stored hca_core_clock to be 1000× too large and hardware timestamps to be converted incorrectly.

  • Replaces hca_core_clock * USEC_PER_SEC with hca_core_clock_khz * HZ_PER_KHZ in the constructor, and renames the parameter to hca_core_clock_khz to document the input unit in both the header and implementation.
  • Introduces a new HZ_PER_KHZ constant (= 1000L) in clock.h, following the style of the existing time-unit constants and giving the scaling factor a self-documenting name.

Confidence Score: 5/5

The change is a minimal, targeted fix to a single multiplication constant; all callers already pass the kHz value from ibv_device_attr_ex and are unmodified.

The three-file change is self-contained: a constant rename in clock.h, a parameter rename in the header, and a multiplier correction in the constructor. The empirical clock-rate correction path in fix_hw_clock_deviation() derives hca_core_clock from measured tick/nanosecond ratios and is unaffected. The get_hca_core_clock() accessor has no callers outside the class. The fix straightforwardly corrects the documented 1000x scale error with no risk of introducing new issues.

No files require special attention; the change is narrow and the callers in ib_ctx_handler.cpp are unmodified and correctly pass kHz values.

Important Files Changed

Filename Overview
src/core/dev/time_converter_ib_ctx.cpp Constructor now correctly scales the kHz input to Hz using HZ_PER_KHZ; parameter rename propagated correctly; fix_hw_clock_deviation empirical path is unaffected and correct.
src/core/dev/time_converter_ib_ctx.h Parameter rename to hca_core_clock_khz propagated correctly to the declaration; no other changes.
src/utils/clock.h Added HZ_PER_KHZ (1000L) constant, consistent with the existing time-unit macro style.

Reviews (1): Last reviewed commit: "issue: 5170344 Fix IB HCA core clock uni..." | Re-trigger Greptile

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/core/dev/time_converter_ib_ctx.cpp (1)

40-40: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Add regression coverage for the kHz-to-Hz contract.

Please add or verify a test covering a representative HCA clock value and TS_CONVERSION_MODE_RAW, asserting that hca_core_clock_khz * 1000 is stored and used as Hz. This would prevent a recurrence of the original 1000× scaling bug.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/core/dev/time_converter_ib_ctx.cpp` at line 40, Add regression coverage
around the time-converter initialization that uses a representative HCA clock
value with TS_CONVERSION_MODE_RAW, asserting hca_core_clock_khz is converted by
multiplying by 1000 before being stored and used as Hz. Verify the test would
fail if the kHz-to-Hz scaling regresses by 1000×.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/core/dev/time_converter_ib_ctx.cpp`:
- Line 40: Add regression coverage around the time-converter initialization that
uses a representative HCA clock value with TS_CONVERSION_MODE_RAW, asserting
hca_core_clock_khz is converted by multiplying by 1000 before being stored and
used as Hz. Verify the test would fail if the kHz-to-Hz scaling regresses by
1000×.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Enterprise

Run ID: 9809d450-415f-49eb-a24d-ee8904cda8ef

📥 Commits

Reviewing files that changed from the base of the PR and between df117ad and 5048147.

📒 Files selected for processing (3)
  • src/core/dev/time_converter_ib_ctx.cpp
  • src/core/dev/time_converter_ib_ctx.h
  • src/utils/clock.h

@pasis
pasis requested a review from tomerdbz July 24, 2026 07:31
@pasis
pasis requested a review from BasharRadya August 26, 2026 18:18
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.

1 participant