Skip to content

Calculate and report more granular process_cpu_seconds_total#71

Merged
czechboy0 merged 3 commits intoapple:mainfrom
kukushechkin:48-more-granular-cpu-seconds
Dec 11, 2025
Merged

Calculate and report more granular process_cpu_seconds_total#71
czechboy0 merged 3 commits intoapple:mainfrom
kukushechkin:48-more-granular-cpu-seconds

Conversation

@kukushechkin
Copy link
Copy Markdown
Contributor

@kukushechkin kukushechkin commented Dec 9, 2025

This is a fix for #48

Motivation:

process_cpu_seconds_total metrics is reported as an integer value now, making it impractical to use it to calculate the CPU usage %.

Modifications:

  • process_cpu_seconds_total is now calculated use rusage() as a sum of system and user time
  • process_cpu_usage is no longer reported

Result:

process_cpu_seconds_total can now be used to calculate CPU usage % with rate(process_cpu_seconds_total):

Screenshot 2025-12-09 at 14 39 17

@kukushechkin kukushechkin added the 🆕 semver/minor Adds new public API. label Dec 9, 2025
@kukushechkin kukushechkin added this to the 1.0.0 milestone Dec 9, 2025
@kukushechkin kukushechkin linked an issue Dec 9, 2025 that may be closed by this pull request
@kukushechkin
Copy link
Copy Markdown
Contributor Author

One discussion point — is there any practical reason to use a Counter instead of a Gauge for the cpu_seconds_total, when rusage already reports cumulative CPU seconds?

@kukushechkin kukushechkin marked this pull request as ready for review December 9, 2025 14:55
@kukushechkin kukushechkin added ⚠️ semver/major Breaks existing public API. and removed 🆕 semver/minor Adds new public API. labels Dec 9, 2025
Comment thread Sources/SystemMetrics/SystemMetricsMonitor+Linux.swift
@czechboy0
Copy link
Copy Markdown
Contributor

One discussion point — is there any practical reason to use a Counter instead of a Gauge for the cpu_seconds_total, when rusage already reports cumulative CPU seconds?

No I'd keep it a Gauge, it's an absolute value reported from elsewhere, rather than a diff. I assume Gauge is simpler in this implementation?

@kukushechkin
Copy link
Copy Markdown
Contributor Author

I assume Gauge is simpler in this implementation?

Yes, just (almost) transparently push the value we get from rusage. With the Counter we would need to artificially track "previous known cpu time" state.

@czechboy0 czechboy0 merged commit 2f5844b into apple:main Dec 11, 2025
33 of 34 checks passed
@kukushechkin kukushechkin deleted the 48-more-granular-cpu-seconds branch December 11, 2025 13:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚠️ semver/major Breaks existing public API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Request for more finegrained cpu-usage metric

3 participants