Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions src/coreclr/gc/unix/cgroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ Module Name:
Abstract:
Read the memory limit for the current process
--*/
#ifdef __FreeBSD__
#define _WITH_GETLINE
#endif
#include "cgroup.h"
#include <cstddef>

#if defined(TARGET_LINUX) || defined(TARGET_ANDROID)

#include <cstdint>
#include <cassert>
Expand All @@ -21,19 +22,12 @@ Module Name:
#include <stdio.h>
#include <string.h>
#include <sys/resource.h>
#if defined(__APPLE__) || defined(__FreeBSD__)
#include <sys/param.h>
#include <sys/mount.h>
#elif !defined(__HAIKU__)
#include <sys/vfs.h>
#endif
#include <errno.h>
#include <limits>

#include "config.gc.h"

#include "cgroup.h"

#ifndef SIZE_T_MAX
#define SIZE_T_MAX (~(size_t)0)
#endif
Expand Down Expand Up @@ -122,10 +116,6 @@ class CGroup
// modes because both of those involve cgroup v1 controllers managing
// resources.

#if !HAVE_NON_LEGACY_STATFS || TARGET_WASM
return 0;
#else

struct statfs stats;
int result = statfs("/sys/fs/cgroup", &stats);
if (result != 0)
Expand All @@ -142,7 +132,6 @@ class CGroup
// been seen in the wild.
return 1;
}
#endif
}

static bool IsCGroup1MemorySubsystem(const char *strTok){
Expand Down Expand Up @@ -656,3 +645,25 @@ bool GetPhysicalMemoryUsed(size_t* val)
free(line);
return result;
}

#else // !(TARGET_LINUX || TARGET_ANDROID)

void InitializeCGroup()
{
}

void CleanupCGroup()
{
}

size_t GetRestrictedPhysicalMemoryLimit()
{
return 0;
}

bool GetPhysicalMemoryUsed(size_t* val)
{
return false;
}

#endif // TARGET_LINUX || TARGET_ANDROID
33 changes: 17 additions & 16 deletions src/coreclr/nativeaot/Runtime/unix/cgroupcpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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).

--*/
#ifdef __FreeBSD__
#define _WITH_GETLINE
#endif

#include <cstdint>
#include "cgroupcpu.h"

#if defined(TARGET_LINUX) || defined(TARGET_ANDROID)

#include <cstddef>
#include <cassert>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/resource.h>
#if defined(__APPLE__) || defined(__FreeBSD__)
#include <sys/param.h>
#include <sys/mount.h>
#elif !defined(__HAIKU__)
#include <sys/vfs.h>
#endif
#include <errno.h>
#include <limits>

#include "config.gc.h"

#include "cgroupcpu.h"

#define CGROUP2_SUPER_MAGIC 0x63677270

#define BASE_TEN 10
Expand Down Expand Up @@ -90,10 +83,6 @@ class CGroup
// modes because both of those involve cgroup v1 controllers managing
// resources.

#if !HAVE_NON_LEGACY_STATFS
return 0;
#else

struct statfs stats;
int result = statfs("/sys/fs/cgroup", &stats);
if (result != 0)
Expand All @@ -110,7 +99,6 @@ class CGroup
// been seen in the wild.
return 1;
}
#endif
}

static bool IsCGroup1CpuSubsystem(const char *strTok){
Expand Down Expand Up @@ -510,3 +498,16 @@ bool GetCpuLimit(uint32_t* val)

return CGroup::GetCpuLimit(val);
}

#else // !(TARGET_LINUX || TARGET_ANDROID)

void InitializeCpuCGroup()
{
}

bool GetCpuLimit(uint32_t* val)
{
return false;
}

#endif // TARGET_LINUX || TARGET_ANDROID
31 changes: 22 additions & 9 deletions src/coreclr/pal/src/misc/cgroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ SET_DEFAULT_DEBUG_CHANNEL(MISC);
#include "pal/virtual.h"
#include "pal/cgroup.h"
#include <algorithm>
#if defined(__APPLE__) || defined(__FreeBSD__)
#include <sys/param.h>
#include <sys/mount.h>
#elif !defined(__HAIKU__)

#if defined(TARGET_LINUX) || defined(TARGET_ANDROID)

#include <sys/vfs.h>
#endif

#define CGROUP2_SUPER_MAGIC 0x63677270

Expand Down Expand Up @@ -87,9 +85,6 @@ class CGroup
// modes because both of those involve cgroup v1 controllers managing
// resources.

#if !HAVE_NON_LEGACY_STATFS || TARGET_OPENBSD || TARGET_WASM
return 0;
#else
struct statfs stats;
int result = statfs("/sys/fs/cgroup", &stats);

Expand All @@ -107,7 +102,6 @@ class CGroup
// been seen in the wild.
return 1;
}
#endif
}

static bool IsCGroup1CpuSubsystem(const char *strTok){
Expand Down Expand Up @@ -525,3 +519,22 @@ PAL_GetCpuLimit(UINT* val)

return CGroup::GetCpuLimit(val);
}

#else // !(TARGET_LINUX || TARGET_ANDROID)

void InitializeCGroup()
{
}

void CleanupCGroup()
{
}

BOOL
PALAPI
PAL_GetCpuLimit(UINT* val)
{
return FALSE;
}

#endif // TARGET_LINUX || TARGET_ANDROID
Loading