Skip to content

libbeat/common: make Round results identical across architectures#51967

Open
AndersonQ wants to merge 1 commit into
elastic:mainfrom
AndersonQ:fix-libbeat-Round
Open

libbeat/common: make Round results identical across architectures#51967
AndersonQ wants to merge 1 commit into
elastic:mainfrom
AndersonQ:fix-libbeat-Round

Conversation

@AndersonQ

Copy link
Copy Markdown
Member

Proposed commit message

    libbeat/common: make Round results identical across architectures
    
    Round scales a value and then rounds it. The rounding step relied on
    floating-point behaviour that is not guaranteed to be identical across CPU
    architectures, so for values sitting exactly on a rounding midpoint (for
    example 0.50005) amd64 and arm64 could disagree: the same input produced a
    different rounded number depending on which architecture the beat ran on.
    This surfaced as the libbeat arm64 unit tests failing after the Go 1.26
    upgrade.
    
    Rounding now uses the standard library's math.Round on the scaled value, a
    well-defined operation that every architecture computes the same way. Results
    are now stable and portable regardless of CPU or compiler, and the behaviour
    the tests pin down is unchanged.
    
    Assisted-By: Claude Code

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • [ ] I have made corresponding changes to the documentation
  • [ ] I have made corresponding change to the default configuration files
  • [ ] I have added tests that prove my fix is effective or that my feature works. Where relevant, I have used the stresstest.sh script to run them under stress conditions and race detector to verify their stability. - the existing tests are now passing
  • [ ] I have added an entry in ./changelog/fragments using the changelog tool.

Disruptive User Impact

None

How to test this PR locally

run on ARM:

go test -v -run TestRound ./libbeat/common/

Related issues

  • N/A

Round scales a value and then rounds it. The rounding step relied on
floating-point behaviour that is not guaranteed to be identical across CPU
architectures, so for values sitting exactly on a rounding midpoint (for
example 0.50005) amd64 and arm64 could disagree: the same input produced a
different rounded number depending on which architecture the beat ran on.
This surfaced as the libbeat arm64 unit tests failing after the Go 1.26
upgrade.

Rounding now uses the standard library's math.Round on the scaled value, a
well-defined operation that every architecture computes the same way. Results
are now stable and portable regardless of CPU or compiler, and the behaviour
the tests pin down is unchanged.

Assisted-By: Claude Code
@AndersonQ AndersonQ self-assigned this Jul 15, 2026
Copilot AI review requested due to automatic review settings July 15, 2026 09:31
@AndersonQ AndersonQ requested a review from a team as a code owner July 15, 2026 09:31
@AndersonQ AndersonQ added the Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team label Jul 15, 2026
@AndersonQ AndersonQ requested a review from orestisfl July 15, 2026 09:31
@AndersonQ AndersonQ added the backport-active-all Automated backport with mergify to all the active branches label Jul 15, 2026
@AndersonQ AndersonQ requested a review from khushijain21 July 15, 2026 09:31
@infra-vault-gh-plugin-prod

Copy link
Copy Markdown

Pinging @elastic/elastic-agent-data-plane (Team:Elastic-Agent-Data-Plane)

@botelastic botelastic Bot added needs_team Indicates that the issue/PR needs a Team:* label and removed needs_team Indicates that the issue/PR needs a Team:* label labels Jul 15, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🤖 GitHub comments

Just comment with:

  • run docs-build : Re-trigger the docs validation. (use unformatted text in the comment!)
  • /test : Run the Buildkite pipeline.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR updates libbeat/common.Round to make rounding deterministic across CPU architectures by using math.Round on the scaled value, avoiding architecture-dependent edge behavior at midpoint boundaries (e.g. 0.50005 at 4 decimal places).

Changes:

  • Replace custom midpoint rounding logic (Modf + Ceil/Floor) with math.Round on the scaled value.
  • Update the Round function comment to explicitly describe half-away-from-zero behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@andrzej-stencel andrzej-stencel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This change mirrors https://github.com/elastic/elastic-agent-system-metrics/pull/295/changes#diff-5ecedbe075db3830efdb44b74505a8748573ed54c5679946f377d3a7db1adf70 👍

I'd suggest to add tests for negative values, as it is changing with this change.

@mergify

mergify Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-active-all Automated backport with mergify to all the active branches skip-changelog Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants