Skip to content

fix(ksmd): parse general_profit as a signed value - #3894

Merged
SuperQ merged 3 commits into
prometheus:masterfrom
Sanchit2662:fix/ksmd-negative-general-profit
Nov 18, 2026
Merged

SuperQ merged 3 commits into
prometheus:masterfrom
Sanchit2662:fix/ksmd-negative-general-profit

Conversation

@Sanchit2662

@Sanchit2662 Sanchit2662 commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The kernel prints /sys/kernel/mm/ksm/general_profit as a signed number (sysfs_emit(buf, "%ld\n", ...)). It goes negative when KSM costs more memory than it saves, for example -81920.

The ksmd collector reads every file with readUintFromFile, which uses strconv.ParseUint. On a negative value, parsing fails and Update returns the error. The loop stops there, so every metric after full_scans_total is lost and node_scrape_collector_success{collector="ksmd"} goes to 0.

I hit this while reading the code added in #3778. With a fake sysfs where general_profit is -81920 and the other files are 1:

err=strconv.ParseUint: parsing "-81920": invalid syntax   metrics_emitted=1

Only 1 of the 10 metrics comes out.

Fix

  • Added readIntFromFile in helper.go, which uses ParseInt.
  • In ksmd_linux.go, general_profit is read with it. All other files still use readUintFromFile.
  • The metric is already a gauge, so negative values are fine.

Testing

  • Changed general_profit in sys.ttar to -81920 and updated e2e-output.txt to match.
  • Added TestKsmdMetrics, which reads the fixture and checks that Update returns no error and the metrics have the right values. It fails without the fix (ParseUint: parsing "-81920") and passes with it.
  • Ran the built binary with --collector.ksmd --path.sysfs pointing at the fixtures. All 10 ksmd metrics come out and the collector reports success.
  • The full ./collector test package passes on Linux, and gofmt and go vet are clean.

The shared fixture now only has a negative value. Positive values go through the same integer parsing as before, and the other metrics in the test cover them.

The kernel prints /sys/kernel/mm/ksm/general_profit as a signed long,
and it is negative when KSM costs more memory than it saves. It was read
with ParseUint like the other files, so a negative value made Update
return an error and drop every ksmd metric after full_scans, failing
the collector for that scrape.

Read general_profit with ParseInt. Use a negative value in the sys
fixture and add a ksmd unit test, and update the e2e output.

Signed-off-by: Sanchit2662 <sanchit2662@gmail.com>
Comment thread collector/ksmd_linux_test.go Resolved
The negative general_profit case is covered by the e2e output test, so
remove the separate unit test. Update e2e-64k-page-output.txt (used on
aarch64 and ppc64le) to match the changed sys fixture.

Signed-off-by: Sanchit2662 <sanchit2662@gmail.com>
@Sanchit2662
Sanchit2662 requested a review from SuperQ November 12, 2026 16:24
Signed-off-by: Sanchit2662 <sanchit2662@gmail.com>
@Sanchit2662
Sanchit2662 requested a review from SuperQ November 13, 2026 16:30
@Sanchit2662

Copy link
Copy Markdown
Contributor Author

Fixed the ARM CI failure by updating the general_profit value in e2e-64k-page-output.txt to -81920.

@SuperQ

SuperQ commented Nov 18, 2026

Copy link
Copy Markdown
Member

Thanks!

@SuperQ
SuperQ merged commit 6813bc2 into prometheus:master Nov 18, 2026
18 checks passed
@SuperQ SuperQ mentioned this pull request Nov 18, 2026
SuperQ added a commit that referenced this pull request Nov 18, 2026
The behavior of `node_cpu_frequency_hertz` has changed. There are two different sources
of data for this metric. One from the `cpu` collector, one from the `cpufreq` collector.
Due to historical feature migrations the `cpufreq` collector shares the same subsystem
as the `cpu` collector. This caused conflicting data behavior. An attempt to fix this
was introduced in #3318. This behavior has been reverted to the previous behavior in #3795.

A new flag (`--collector.cpufreq.enable-cpufreq-prefix`) has been introduced that gives
the `cpufreq` a dedicated metric name subsystem (`node_cpufreq_...`). This will likely
be the default in v2.

* [CHANGE] netstat: Use procfs parsers for netstat, snmp and snmp6 #3796
* [CHANGE] edac: Fix no data results #3863
* [CHANGE] nvme: Deprecate metric nvme_namespace_capacity_bytes #3899
* [FEATURE] Add tainted collector for /proc/sys/kernel/tainted #3745
* [FEATURE] Add resctrl collector #3797
* [ENHANCEMENT] netstat: Add new FreeBSD metrics #3334
* [ENHANCEMENT] Add `chip` label to DRM metrics #3572
* [ENHANCEMENT] diskstats: add device-mapper backing device info metric #3676
* [ENHANCEMENT] textfile: support UTF-8 metric and label names #3729
* [ENHANCEMENT] ksmd: add general_profit metric #3778 #3894
* [BUGFIX] edac: Fix printf-style formatting in slog call #3759
* [BUGFIX] fibre_channel: fix crash when attempting to dereference invalid counters #3573
* [BUGFIX] thermal: Fix collecting Darwin temperatures without CPU power status #3767
* [BUGFIX] fibrechannel: Fix for ppc64le #3769
* [BUGFIX] cpu: Revert dup cpu frequency, add node_cpufreq flag #3795
* [BUGFIX] infiniband: skip sysfs reads for excluded devices #3824
* [BUGFIX] bcache: Fix btree_read_average_duration_seconds unit #3828

Signed-off-by: Ben Kochie <superq@gmail.com>
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.

2 participants