Skip to content

Add processor_count and processor_quota APIs - #98

Open
samuel-williams-shopify wants to merge 1 commit into
ruby:masterfrom
samuel-williams-shopify:processor-count-and-quota
Open

samuel-williams-shopify wants to merge 1 commit into
ruby:masterfrom
samuel-williams-shopify:processor-count-and-quota

Conversation

@samuel-williams-shopify

Copy link
Copy Markdown

This adds two processor-capacity APIs without changing the behavior of Etc.nprocessors:

  • Etc.processor_count is an alias for Etc.nprocessors and returns an Integer.
  • Etc.processor_quota returns the usable processor capacity as a Float. On Linux it accounts for cgroup v1/v2 CPU bandwidth quotas, capped by the affinity-aware processor count; otherwise it falls back to Etc.processor_count.to_f.

The cgroup lookup resolves the process membership through /proc/self/cgroup and /proc/self/mountinfo, considers limits inherited from ancestor cgroups, and falls back safely when cgroup information is unavailable or malformed.

This implements the separate fractional API discussed in Feature #21797, while preserving Etc.nprocessors compatibility, and is an alternative API shape to #75.

Tests cover the aliases and return types, cgroup v1 and v2 fractional quotas, inherited limits, unlimited/malformed values, escaped mount paths, and Ractor use.

Tested with:

rake test
# 24 tests, 4637 assertions, 0 failures, 0 errors

rake build
# etc 1.4.6 built to pkg/etc-1.4.6.gem

Assisted-By: devx/874cdf9e-3541-4d85-9ae8-b4ebb7e4db80
@samuel-williams-shopify

Copy link
Copy Markdown
Author

After experimenting with this code, I wonder if we shouldn't bother supporting cgroups v1. It's more complexity and every system where it's relevant is using cgroups v2 now.

This branch has not been deployed

No deployments
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