Skip to content

Exclude cgroup implementation from non-Linux Unix builds#126738

Open
Copilot wants to merge 6 commits intomainfrom
copilot/exclude-cgroup-variants-non-linux
Open

Exclude cgroup implementation from non-Linux Unix builds#126738
Copilot wants to merge 6 commits intomainfrom
copilot/exclude-cgroup-variants-non-linux

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 10, 2026

cgroup code was being compiled and linked on macOS and FreeBSD even though cgroups are a Linux-specific mechanism. While this caused no runtime harm (functions returned 0/false due to missing /sys/fs/cgroup), the code was dead weight on those platforms.

Fixes #99363

Copilot AI requested review from Copilot and removed request for Copilot April 10, 2026 01:17
Copilot AI linked an issue Apr 10, 2026 that may be closed by this pull request
…GET_ANDROID guards

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/bd7043b4-36df-4c0e-ad58-eb91a492b4ae

Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot April 10, 2026 01:47
Copilot AI changed the title [WIP] Exclude all cgroup.cpp variants from build on non-linux Unix OSes Exclude cgroup implementation from non-Linux Unix builds Apr 10, 2026
Copilot AI requested a review from jkotas April 10, 2026 01:51
Copilot AI requested review from Copilot and removed request for Copilot April 10, 2026 01:58

size_t GetRestrictedPhysicalMemoryLimit()
{
#if defined(TARGET_LINUX) || defined(TARGET_ANDROID)
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.

@copilot Merge these ifdefs so that there is one big ifdef over the whole file, duplicate the method implementations as needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in da6f367GetRestrictedPhysicalMemoryLimit and GetPhysicalMemoryUsed are now inside the single #if defined(TARGET_LINUX) || defined(TARGET_ANDROID) block, with stub implementations (return 0 / return false) in the #else branch. There are no more inner ifdefs in the file.

…Limit and GetPhysicalMemoryUsed in gc/unix/cgroup.cpp

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/d2623ab2-097f-49ca-b51a-2e9ab2cba834

Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot April 10, 2026 02:04
Copilot AI requested a review from jkotas April 10, 2026 02:05
…nditional ifdefs in all cgroup files

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/e040e085-21b6-41eb-abf7-86e5718740f8

Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot April 10, 2026 02:16
Copilot AI requested a review from jkotas April 10, 2026 02:18
…in gc/unix/cgroup.cpp

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/ee4e840a-c95a-429b-ae39-f6218c0f9540

Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot April 10, 2026 02:25
@jkotas jkotas marked this pull request as ready for review April 10, 2026 02:32
@jkotas jkotas requested review from Copilot and janvorli April 10, 2026 02:32
@jkotas jkotas closed this Apr 10, 2026
@jkotas jkotas reopened this Apr 10, 2026
@@ -10,31 +10,24 @@ Module Name:
Abstract:
Read cpu limits for the current process
Copy link
Copy Markdown
Member

@jkotas jkotas Apr 10, 2026

Choose a reason for hiding this comment

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

It would be nice to dedup these files too, not doing that in this PR (we have separate issue for that).

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR removes cgroup implementation code from non-Linux Unix builds by gating the cgroup logic behind a single #if defined(TARGET_LINUX) || defined(TARGET_ANDROID) in each cgroup implementation, leaving lightweight stubs for other platforms.

Changes:

  • Wrapped PAL, GC, and NativeAOT cgroup implementations in a top-level Linux/Android-only preprocessor guard.
  • Removed non-Linux include/implementation branches and simplified cgroup version detection to an unconditional statfs("/sys/fs/cgroup", ...) on Linux/Android.
  • Added non-Linux/Android stub implementations for the public entrypoints.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/coreclr/pal/src/misc/cgroup.cpp PAL cgroup CPU limit implementation now Linux/Android-only with non-Linux stubs.
src/coreclr/nativeaot/Runtime/unix/cgroupcpu.cpp NativeAOT cgroup CPU limit implementation now Linux/Android-only with non-Linux stubs.
src/coreclr/gc/unix/cgroup.cpp GC cgroup memory/usage implementation now Linux/Android-only with non-Linux stubs.

BOOL
PALAPI
PAL_GetCpuLimit(UINT* val)
{
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

In the non-Linux/Android stub implementation, val is unused. With common Unix build flags (e.g., -Wunused-parameter/-Werror), this can produce a build break. Consider explicitly marking it unused (e.g., casting to void or using the project’s unused-parameter macro) before returning FALSE.

Suggested change
{
{
(void)val;

Copilot uses AI. Check for mistakes.
}

bool GetCpuLimit(uint32_t* val)
{
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

In the non-Linux/Android stub, val is unused. This can trigger -Wunused-parameter warnings (and potentially -Werror failures) on non-Linux Unix builds. Mark the parameter as unused (e.g., (void)val; / UNREFERENCED_PARAMETER(val)) in the stub implementation.

Suggested change
{
{
(void)val;

Copilot uses AI. Check for mistakes.
}

bool GetPhysicalMemoryUsed(size_t* val)
{
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

In the non-Linux/Android stub, val is unused. This can trigger -Wunused-parameter warnings (and potentially -Werror failures) on macOS/FreeBSD builds—the primary targets of this change. Mark the parameter as unused in the stub (e.g., cast to void / use the project’s unused-parameter macro).

Suggested change
{
{
(void)val;

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exclude all cgroup.cpp variants from build on non-linux Unix OSes.

3 participants