Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Make System.coreCount aware of cgroup2 #2394

Merged
merged 6 commits into from
Jul 31, 2023
Merged

Conversation

PeterAdams-A
Copy link
Contributor

Motivation:

The count already appears to be aware of cgroup1
but doesn't have logic to support cgroup2.

Modifications:

Add logic to parse /sys/fs/cgroup/cpu.max
For details see - https://stackoverflow.com/questions/65551215/get-docker-cpu-memory-limit-inside-container/65554131#65554131

Result:

Core count will adapt if constrained by a cgroup on more modern kernels.

@Lukasa Lukasa added the 🆕 semver/minor Adds new public API. label Mar 23, 2023
@@ -70,5 +72,15 @@ enum Linux {
else { return nil }
return (quota - 1 + period) / period // always round up if fractional CPU quota requested
}

// cgroup2
static func coreCount(maxPath: String) -> Int? {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can I propose we rename these functions to be substantially more explicative of what they do? The little comments doesn't help much.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've tried a rename, we'll see how people like the names. Making the name unique also allows defaulting the arguments so I've done that too - let me know if you think that's a bad idea.

@@ -93,7 +93,9 @@ public enum System {
.map { $0.ProcessorMask.nonzeroBitCount }
.reduce(0, +)
#elseif os(Linux) || os(Android)
if let quota = Linux.coreCount(quota: Linux.cfsQuotaPath, period: Linux.cfsPeriodPath) {
if let quota2 = Linux.coreCount(maxPath: Linux.cfsCpuMaxPath) {
Copy link
Member

Choose a reason for hiding this comment

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

Should we adapt the documentation of coreCount to also include details of how c groups are used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added a brief bit of extra commentary. There is scope for broader review here. The idea of using this number to setup your threads loop group is mentioned - in most cases this is probably the wrong thing to do.

I also wonder if the logic could be improved - I think it's probably possible to be both in a cgroup and a cpuset. I do wonder if CPUSet combined with multiple threads is a good idea or not - suspect anyone doing this has probably put in quite a lot of thought.

@Lukasa Lukasa added 🔨 semver/patch No public API change. and removed 🆕 semver/minor Adds new public API. labels Mar 24, 2023
Copy link
Contributor

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

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

I'm happy with this

@PeterAdams-A PeterAdams-A force-pushed the cgroups2 branch 2 times, most recently from a9751b8 to 67b6eba Compare July 31, 2023 10:20
@FranzBusch FranzBusch enabled auto-merge (squash) July 31, 2023 11:17
@FranzBusch FranzBusch disabled auto-merge July 31, 2023 11:48
@FranzBusch FranzBusch enabled auto-merge (squash) July 31, 2023 11:48
@PeterAdams-A PeterAdams-A disabled auto-merge July 31, 2023 12:04
@PeterAdams-A PeterAdams-A enabled auto-merge (squash) July 31, 2023 12:05
@PeterAdams-A
Copy link
Contributor Author

@swift-nio-bot test this please

PeterAdams-A and others added 6 commits July 31, 2023 13:20
Motivation:

The count already appears to be aware of cgroup1
but doesn't have logic to support cgroup2.

Modifications:

Add logic to parse /sys/fs/cgroup/cpu.max
For details see - https://stackoverflow.com/questions/65551215/get-docker-cpu-memory-limit-inside-container/65554131#65554131

Result:

Core count will adapt if constrained by a cgroup on more modern kernels.
Co-authored-by: Max Desiatov <m_desiatov@apple.com>
Co-authored-by: Max Desiatov <m_desiatov@apple.com>
Co-authored-by: Gwynne Raskind <gwynne@darkrainfall.org>
Co-authored-by: Gwynne Raskind <gwynne@darkrainfall.org>
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
🔨 semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants