Skip to content

Deduplicate GC and PAL cgroup implementations - #137578

Merged
jkotas merged 10 commits into
dotnet:mainfrom
am11:chore/dedup-cgroup
Nov 17, 2026
Merged

jkotas merged 10 commits into
dotnet:mainfrom
am11:chore/dedup-cgroup

Conversation

@am11

@am11 am11 commented Nov 15, 2026

Copy link
Copy Markdown
Member

Move the cgroup CPU limit into gc/unix/cgroup.cpp next to the memory limit, sharing the cgroup discovery code, and move GetCurrentProcessCpuCount from utilcode to the VM so it can use it. Delete the PAL copy and PAL_GetCpuLimit.

@github-actions github-actions Bot added the area-PAL-coreclr only for closed issues label Nov 15, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 15, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 4 pipeline(s).
11 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@am11
am11 requested a review from jkotas November 15, 2026 08:12
Move the cgroup CPU limit into `gc/unix/cgroup.cpp` next to the memory limit,
sharing the cgroup discovery code, and move `GetCurrentProcessCpuCount` from
`utilcode` to the VM so it can use it. Delete the PAL copy and `PAL_GetCpuLimit`.

Move `PAL_GetTotalCpuCount` and `PAL_GetLogicalCpuCountFromOS` to minipal as
`minipal_get_cpu_total_count` and `minipal_get_cpu_affinity_count`.

Co-authored-by: Cursor <[email protected]>
@am11
am11 force-pushed the chore/dedup-cgroup branch from 3892422 to 7c3962c Compare November 15, 2026 08:49
`SimpleRWLock` is compiled into the DAC, which does not have
`GetCurrentProcessCpuCount` now that it moved to the VM.

Co-authored-by: Cursor <[email protected]>
@AustinWise

Copy link
Copy Markdown
Contributor

Thanks for cleaning this up. This contributes to #99171. There is still a copy of this code in NativeAOT: https://github.com/dotnet/runtime/blob/main/src/coreclr/nativeaot/Runtime/unix/cgroupcpu.cpp

Delete `nativeaot/Runtime/unix/cgroupcpu.cpp`. NativeAOT already compiles
`gc/unix/cgroup.cpp`, and `GCToOSInterface::Initialize` initializes it.

Co-authored-by: Cursor <[email protected]>
@am11

am11 commented Nov 15, 2026

Copy link
Copy Markdown
Member Author

Good point, thanks. NativeAOT now uses the GC copy too, and cgroupcpu.cpp is deleted.

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.

🔵 Needs a closer look

CPU-quota discovery now shares GC lifecycle state, requiring cgroup v1/v2 and CoreCLR/NativeAOT startup/shutdown validation that this source-only review could not perform.

No open issues 🎉

What changed in this PR

Consolidates Unix cgroup CPU-limit handling alongside the GC’s memory-limit implementation, removing duplicate PAL and NativeAOT code.

Changes:

  • Shares cgroup discovery and CPU-quota queries between CoreCLR and NativeAOT.
  • Moves process CPU-count calculation into the VM and adds reusable minipal CPU-count helpers.
  • Removes obsolete implementations, declarations, exports, and build entries.
File Description
src/​native/​minipal/​minipalconfig.h.in Exposes affinity-query availability.
src/​native/​minipal/​cpucount.h Declares total and affinity CPU-count helpers.
src/​native/​minipal/​cpucount.c Implements shared CPU-count helpers.
src/​native/​minipal/​configure.cmake Detects sched_getaffinity.
src/​coreclr/​vm/​util.hpp Declares the relocated process CPU-count function.
src/​coreclr/​vm/​util.cpp Combines CPU affinity, configuration, and shared cgroup limits.
src/​coreclr/​vm/​simplerwlock.hpp Avoids the VM CPU-count query in diagnostic builds.
src/​coreclr/​utilcode/​util.cpp Uses minipal and removes process CPU-count logic.
src/​coreclr/​pal/​src/​misc/​sysinfo.cpp Replaces PAL CPU-count helpers with minipal.
src/​coreclr/​pal/​src/​misc/​cgroup.cpp Deletes duplicate cgroup implementation.
src/​coreclr/​pal/​src/​init/​pal.cpp Removes PAL cgroup initialization and cleanup.
src/​coreclr/​pal/​src/​include/​pal/​cgroup.h Deletes obsolete cgroup lifecycle declarations.
src/​coreclr/​pal/​src/​CMakeLists.txt Removes the deleted PAL source from the build.
src/​coreclr/​pal/​inc/​pal.h Removes obsolete CPU-count and quota APIs.
src/​coreclr/​nativeaot/​Runtime/​unix/​PalUnix.cpp Uses shared GC cgroup CPU-limit queries.
src/​coreclr/​nativeaot/​Runtime/​unix/​cgroupcpu.h Deletes duplicate CPU-limit declarations.
src/​coreclr/​nativeaot/​Runtime/​unix/​cgroupcpu.cpp Deletes NativeAOT’s duplicate cgroup implementation.
src/​coreclr/​nativeaot/​Runtime/​CMakeLists.txt Removes the deleted NativeAOT source from the build.
src/​coreclr/​inc/​utilcode.h Removes the relocated CPU-count declaration.
src/​coreclr/​gc/​unix/​cgroup.h Declares the shared CPU-limit query.
src/​coreclr/​gc/​unix/​cgroup.cpp Adds CPU-quota parsing and lazy shared initialization.
src/​coreclr/​dlls/​mscordac/​mscordac_unixexports.src Removes obsolete PAL export entries.

Review effort: Balanced

@jkotas
jkotas requested a review from janvorli November 16, 2026 04:28

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.

🔵 Needs a closer look

Earlier shared cgroup initialization affects CoreCLR and NativeAOT startup, but static-only review leaves lifecycle, container CPU-quota, and cross-platform behavior needing maintainer validation.

No open issues 🎉

Review effort: Balanced

@am11

am11 commented Nov 16, 2026

Copy link
Copy Markdown
Member Author

@AaronRobinsonMSFT, if this one goes in first, it will be easier to rebase #137579 branch and resolve the feedback.

Comment thread src/coreclr/gc/unix/cgroup.cpp Resolved
am11 and others added 2 commits November 16, 2026 22:15
`gc/unix/cgroup.cpp` becomes `minipal/cgroup.c`, so the VM and NativeAOT
no longer use code from the GC directory. The GC keeps its memory limit and
usage policy in `gcenv.unix.cpp`.

Co-authored-by: Cursor <[email protected]>
Comment thread src/native/minipal/cgroup.h Resolved
@jkotas
jkotas requested review from janvorli and a balanced review from Copilot November 16, 2026 20:29

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.

🔵 Needs a closer look

Shared cgroup ownership and lifecycle changes span CoreCLR, NativeAOT, and standalone GC, requiring maintainer validation of startup/shutdown and platform-specific CPU limits that this source-only review could not perform.

No open issues 🎉

Review effort: Balanced

Comment thread src/native/minipal/cpucount.c Resolved
Comment thread src/native/minipal/cpucount.c Resolved

@jkotas jkotas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you!

@jkotas

jkotas commented Nov 17, 2026

Copy link
Copy Markdown
Member

/ba-g osx dead letter

@jkotas
jkotas merged commit 72957fe into dotnet:main Nov 17, 2026
105 of 109 checks passed
@am11
am11 deleted the chore/dedup-cgroup branch November 17, 2026 23:45
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-PAL-coreclr only for closed issues community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants