Blame SOURCES/runtime-mono-memfunc-cgroup.patch

8c3e2c
Index: a/src/mono/mono/utils/memfuncs.c
8c3e2c
===================================================================
8c3e2c
--- a/src/mono/mono/utils/memfuncs.c
8c3e2c
+++ b/src/mono/mono/utils/memfuncs.c
8c3e2c
@@ -343,6 +343,9 @@ mono_determine_physical_ram_available_si
8c3e2c
 	host_page_size (host, &page_size);
8c3e2c
 	return (guint64) vmstat.free_count * page_size;
8c3e2c
 
8c3e2c
+#elif defined (__FreeBSD__) || defined (__linux__) || defined (__APPLE__)
8c3e2c
+	return (getPhysicalMemoryAvail());
8c3e2c
+
8c3e2c
 #elif defined (HAVE_SYSCONF)
8c3e2c
 	gint64 page_size = -1, num_pages = -1;
8c3e2c
 
8c3e2c
Index: a/src/mono/mono/utils/memfuncs.h
8c3e2c
===================================================================
8c3e2c
--- a/src/mono/mono/utils/memfuncs.h
8c3e2c
+++ b/src/mono/mono/utils/memfuncs.h
8c3e2c
@@ -24,5 +24,10 @@ MONO_COMPONENT_API void mono_gc_memmove_
8c3e2c
 void mono_gc_memmove_aligned (void *dest, const void *src, size_t size);
8c3e2c
 guint64 mono_determine_physical_ram_size (void);
8c3e2c
 guint64 mono_determine_physical_ram_available_size (void);
8c3e2c
+#if defined (__FreeBSD__) || defined (__linux__) || defined (__APPLE__)
8c3e2c
+size_t getRestrictedPhysicalMemoryLimit(void);
8c3e2c
+gboolean getPhysicalMemoryUsed(size_t *);
8c3e2c
+size_t getPhysicalMemoryAvail(void);
8c3e2c
+#endif
8c3e2c
 
8c3e2c
 #endif
8c3e2c
Index: a/src/mono/mono/utils/mono-cgroup.c
8c3e2c
===================================================================
8c3e2c
--- /dev/null
8c3e2c
+++ b/src/mono/mono/utils/mono-cgroup.c
8c3e2c
@@ -0,0 +1,709 @@
8c3e2c
+// Licensed to the .NET Foundation under one or more agreements.
8c3e2c
+// The .NET Foundation licenses this file to you under the MIT license.
8c3e2c
+
8c3e2c
+/*++
8c3e2c
+
8c3e2c
+Module Name:
8c3e2c
+
8c3e2c
+    mono-cgroup.cpp
8c3e2c
+
8c3e2c
+Abstract:
8c3e2c
+    Read the memory limit for the current process
8c3e2c
+--*/
8c3e2c
+#ifdef __FreeBSD__
8c3e2c
+#define _WITH_GETLINE
8c3e2c
+#endif
8c3e2c
+
8c3e2c
+#include <config.h>
8c3e2c
+#include <unistd.h>
8c3e2c
+#include <stdlib.h>
8c3e2c
+#include <stdint.h>
8c3e2c
+#include <glib.h>
8c3e2c
+#include <stdio.h>
8c3e2c
+#include <string.h>
8c3e2c
+#include <sys/resource.h>
8c3e2c
+#if defined(__APPLE__) || defined(__FreeBSD__)
8c3e2c
+#include <sys/param.h>
8c3e2c
+#include <sys/mount.h>
8c3e2c
+#else
8c3e2c
+#include <sys/vfs.h>
8c3e2c
+#endif
8c3e2c
+#include <errno.h>
8c3e2c
+#include <limits.h>
8c3e2c
+
8c3e2c
+#ifndef SIZE_T_MAX
8c3e2c
+# define SIZE_T_MAX (~(size_t)0)
8c3e2c
+#endif
8c3e2c
+
8c3e2c
+#define CGROUP2_SUPER_MAGIC 0x63677270
8c3e2c
+#define TMPFS_MAGIC 0x01021994
8c3e2c
+
8c3e2c
+#define PROC_MOUNTINFO_FILENAME "/proc/self/mountinfo"
8c3e2c
+#define PROC_CGROUP_FILENAME "/proc/self/cgroup"
8c3e2c
+#define PROC_STATM_FILENAME "/proc/self/statm"
8c3e2c
+#define CGROUP1_MEMORY_LIMIT_FILENAME "/memory.limit_in_bytes"
8c3e2c
+#define CGROUP2_MEMORY_LIMIT_FILENAME "/memory.max"
8c3e2c
+#define CGROUP_MEMORY_STAT_FILENAME "/memory.stat"
8c3e2c
+
8c3e2c
+static void initialize(void);
8c3e2c
+static gboolean readMemoryValueFromFile(const char *, guint64 *);
8c3e2c
+static gboolean getPhysicalMemoryLimit(guint64 *);
8c3e2c
+static gboolean getPhysicalMemoryUsage(size_t *);
8c3e2c
+static int findCGroupVersion(void);
8c3e2c
+static gboolean isCGroup1MemorySubsystem(const char *);
8c3e2c
+static char *findCGroupPath(gboolean (*is_subsystem)(const char *));
8c3e2c
+static void findHierarchyMount(gboolean (*is_subsystem)(const char *), char **, char **);
8c3e2c
+static char *findCGroupPathForSubsystem(gboolean (*is_subsystem)(const char *));
8c3e2c
+static gboolean getCGroupMemoryLimit(guint64 *, const char *);
8c3e2c
+static gboolean getCGroupMemoryUsage(size_t *);
8c3e2c
+static size_t getPhysicalMemoryTotal(guint64);
8c3e2c
+
8c3e2c
+size_t getRestrictedPhysicalMemoryLimit(void);
8c3e2c
+gboolean getPhysicalMemoryUsed(size_t *);
8c3e2c
+size_t getPhysicalMemoryAvail(void);
8c3e2c
+
8c3e2c
+// the cgroup version number or 0 to indicate cgroups are not found or not enabled
8c3e2c
+static int s_cgroup_version;
8c3e2c
+
8c3e2c
+static char *s_memory_cgroup_path = NULL;
8c3e2c
+
8c3e2c
+static const char *s_mem_stat_key_names[4];
8c3e2c
+static size_t s_mem_stat_key_lengths[4];
8c3e2c
+static size_t s_mem_stat_n_keys = 0;
8c3e2c
+static long pageSize;
8c3e2c
+
8c3e2c
+/**
8c3e2c
+ * @initialize
8c3e2c
+ *
8c3e2c
+ * Initialize variables used by the calculation routines.
8c3e2c
+ */
8c3e2c
+static void 
8c3e2c
+initialize()
8c3e2c
+{
8c3e2c
+	s_cgroup_version = findCGroupVersion();
8c3e2c
+	s_memory_cgroup_path = findCGroupPath(s_cgroup_version == 1 ? &isCGroup1MemorySubsystem : NULL);
8c3e2c
+
8c3e2c
+	if (s_cgroup_version == 1) {
8c3e2c
+		s_mem_stat_n_keys = 4;
8c3e2c
+		s_mem_stat_key_names[0] = "total_inactive_anon ";
8c3e2c
+		s_mem_stat_key_names[1] = "total_active_anon ";
8c3e2c
+		s_mem_stat_key_names[2] = "total_dirty ";
8c3e2c
+		s_mem_stat_key_names[3] = "total_unevictable ";
8c3e2c
+	} else {
8c3e2c
+		s_mem_stat_n_keys = 3;
8c3e2c
+		s_mem_stat_key_names[0] = "anon ";
8c3e2c
+		s_mem_stat_key_names[1] = "file_dirty ";
8c3e2c
+		s_mem_stat_key_names[2] = "unevictable ";
8c3e2c
+	}
8c3e2c
+
8c3e2c
+	for (size_t i = 0; i < s_mem_stat_n_keys; i++)
8c3e2c
+		s_mem_stat_key_lengths[i] = strlen(s_mem_stat_key_names[i]);
8c3e2c
+
8c3e2c
+	pageSize = sysconf(_SC_PAGE_SIZE);
8c3e2c
+}
8c3e2c
+
8c3e2c
+/**
8c3e2c
+ * @readMemoryValueFromFile
8c3e2c
+ *
8c3e2c
+ * @param[in] filename - name of file containing value
8c3e2c
+ * @param[out] val - pointer to the result area
8c3e2c
+ * @returns True or False depending if value was found
8c3e2c
+ *
8c3e2c
+ * Read a value from a specified /sys/fs/cgroup/memory file
8c3e2c
+ */
8c3e2c
+static gboolean 
8c3e2c
+readMemoryValueFromFile(const char* filename, guint64* val)
8c3e2c
+{
8c3e2c
+	gboolean result = FALSE;
8c3e2c
+	char *line = NULL;
8c3e2c
+	size_t lineLen = 0;
8c3e2c
+	char *endptr = NULL;
8c3e2c
+	guint64 num = 0, multiplier;
8c3e2c
+	FILE *file = NULL;
8c3e2c
+
8c3e2c
+	if (val == NULL) {
8c3e2c
+		file = fopen(filename, "r");
8c3e2c
+		if (file != NULL) {
8c3e2c
+			if (getline(&line, &lineLen, file) != -1) {
8c3e2c
+				errno = 0;
8c3e2c
+				num = strtoull(line, &endptr, 0);
8c3e2c
+				if (line != endptr && errno == 0) {
8c3e2c
+					multiplier = 1;
8c3e2c
+
8c3e2c
+					switch (*endptr)
8c3e2c
+					{
8c3e2c
+					case 'g':
8c3e2c
+					case 'G': 
8c3e2c
+						multiplier = 1024;
8c3e2c
+					case 'm':
8c3e2c
+					case 'M': 
8c3e2c
+						multiplier = multiplier * 1024;
8c3e2c
+					case 'k':
8c3e2c
+					case 'K': 
8c3e2c
+						multiplier = multiplier * 1024;
8c3e2c
+					}
8c3e2c
+
8c3e2c
+					*val = num * multiplier;
8c3e2c
+					result = TRUE;
8c3e2c
+					if (*val / multiplier != num)
8c3e2c
+						result = FALSE;
8c3e2c
+				}
8c3e2c
+			}
8c3e2c
+		}
8c3e2c
+	}
8c3e2c
+
8c3e2c
+	if (file)
8c3e2c
+		fclose(file);
8c3e2c
+	free(line);
8c3e2c
+	return result;
8c3e2c
+}
8c3e2c
+
8c3e2c
+/**
8c3e2c
+ * @getPhysicalMemoryLimit
8c3e2c
+ *
8c3e2c
+ * @param[out] val - pointer to the result area
8c3e2c
+ * @returns True or False depending if a limit was found
8c3e2c
+ *
8c3e2c
+ * Interrogate the cgroup memory values to determine if there's
8c3e2c
+ * a limit on physical memory.
8c3e2c
+ */
8c3e2c
+static gboolean 
8c3e2c
+getPhysicalMemoryLimit(guint64 *val)
8c3e2c
+{
8c3e2c
+	if (s_mem_stat_n_keys == 0)
8c3e2c
+		initialize();
8c3e2c
+
8c3e2c
+	if (s_cgroup_version == 0)
8c3e2c
+		return FALSE;
8c3e2c
+	else if (s_cgroup_version == 1)
8c3e2c
+		return getCGroupMemoryLimit(val, CGROUP1_MEMORY_LIMIT_FILENAME);
8c3e2c
+	else if (s_cgroup_version == 2)
8c3e2c
+		return getCGroupMemoryLimit(val, CGROUP2_MEMORY_LIMIT_FILENAME);
8c3e2c
+	else {
8c3e2c
+		g_assert(!"Unknown cgroup version.");
8c3e2c
+		return FALSE;
8c3e2c
+	}
8c3e2c
+}
8c3e2c
+
8c3e2c
+/**
8c3e2c
+ * @getPhysicalMemoryUsage
8c3e2c
+ *
8c3e2c
+ * @param[out] val - pointer to the result area
8c3e2c
+ * @returns True or False depending if a usage value was found
8c3e2c
+ *
8c3e2c
+ * Interrogate the cgroup memory values to determine how much 
8c3e2c
+ * memory is in use.
8c3e2c
+ */
8c3e2c
+static gboolean 
8c3e2c
+getPhysicalMemoryUsage(size_t *val)
8c3e2c
+{
8c3e2c
+	if (s_cgroup_version == 0)
8c3e2c
+		return FALSE;
8c3e2c
+	else if (s_cgroup_version == 1)
8c3e2c
+		return getCGroupMemoryUsage(val);
8c3e2c
+	else if (s_cgroup_version == 2)
8c3e2c
+		return getCGroupMemoryUsage(val);
8c3e2c
+	else {
8c3e2c
+		g_assert(!"Unknown cgroup version.");
8c3e2c
+		return FALSE;
8c3e2c
+	}
8c3e2c
+}
8c3e2c
+
8c3e2c
+/**
8c3e2c
+ * @findGroupVersion
8c3e2c
+ *
8c3e2c
+ * @returns cgroup version
8c3e2c
+ *
8c3e2c
+ * Inspect the /sys/fs/cgroup hierachy to determine what version of 
8c3e2c
+ * group we are using
8c3e2c
+ */
8c3e2c
+static int 
8c3e2c
+findCGroupVersion()
8c3e2c
+{
8c3e2c
+	// It is possible to have both cgroup v1 and v2 enabled on a system.
8c3e2c
+	// Most non-bleeding-edge Linux distributions fall in this group. We
8c3e2c
+	// look at the file system type of /sys/fs/cgroup to determine which
8c3e2c
+	// one is the default. For more details, see:
8c3e2c
+	// https://systemd.io/CGROUP_DELEGATION/#three-different-tree-setups-
8c3e2c
+	// We dont care about the difference between the "legacy" and "hybrid"
8c3e2c
+	// modes because both of those involve cgroup v1 controllers managing
8c3e2c
+	// resources.
8c3e2c
+
8c3e2c
+
8c3e2c
+	struct statfs stats;
8c3e2c
+	int result = statfs("/sys/fs/cgroup", &stats);
8c3e2c
+	if (result != 0)
8c3e2c
+		return 0;
8c3e2c
+
8c3e2c
+	switch (stats.f_type) {
8c3e2c
+	case TMPFS_MAGIC: return 1;
8c3e2c
+	case CGROUP2_SUPER_MAGIC: return 2;
8c3e2c
+	default:
8c3e2c
+		g_assert(!"Unexpected file system type for /sys/fs/cgroup");
8c3e2c
+		return 0;
8c3e2c
+	}
8c3e2c
+}
8c3e2c
+
8c3e2c
+/**
8c3e2c
+ * @isCGroup1MemorySubsystem
8c3e2c
+ *
8c3e2c
+ * @param[in] strTok - Token for comparison
8c3e2c
+ * @returns True if token matches "memory"
8c3e2c
+ *
8c3e2c
+ * Check if we've found the memory component of /sys/fs/cgroup
8c3e2c
+ */
8c3e2c
+static gboolean 
8c3e2c
+isCGroup1MemorySubsystem(const char *strTok)
8c3e2c
+{
8c3e2c
+	return strcmp("memory", strTok) == 0;
8c3e2c
+}
8c3e2c
+
8c3e2c
+/**
8c3e2c
+ * @findCGroupPath
8c3e2c
+ *
8c3e2c
+ * @param[in] is_subsystem - Function used to compare tokens
8c3e2c
+ * @returns Path to cgroup
8c3e2c
+ *
8c3e2c
+ * Navigate the /sys/fs/cgroup to try and find the correct cgroup path
8c3e2c
+ */
8c3e2c
+static char *
8c3e2c
+findCGroupPath(gboolean (*is_subsystem)(const char *))
8c3e2c
+{
8c3e2c
+	char *cgroup_path = NULL;
8c3e2c
+	char *hierarchy_mount = NULL;
8c3e2c
+	char *hierarchy_root = NULL;
8c3e2c
+	char *cgroup_path_relative_to_mount = NULL;
8c3e2c
+	size_t common_path_prefix_len;
8c3e2c
+
8c3e2c
+	findHierarchyMount(is_subsystem, &hierarchy_mount, &hierarchy_root);
8c3e2c
+	if (hierarchy_mount != NULL && hierarchy_root != NULL) {
8c3e2c
+
8c3e2c
+		cgroup_path_relative_to_mount = findCGroupPathForSubsystem(is_subsystem);
8c3e2c
+		if (cgroup_path_relative_to_mount != NULL) {
8c3e2c
+
8c3e2c
+			cgroup_path = (char*)malloc(strlen(hierarchy_mount) + strlen(cgroup_path_relative_to_mount) + 1);
8c3e2c
+			if (cgroup_path != NULL) {
8c3e2c
+
8c3e2c
+				strcpy(cgroup_path, hierarchy_mount);
8c3e2c
+				// For a host cgroup, we need to append the relative path.
8c3e2c
+				// The root and cgroup path can share a common prefix of the path that should not be appended.
8c3e2c
+				// Example 1 (docker):
8c3e2c
+				// hierarchy_mount:               /sys/fs/cgroup/cpu
8c3e2c
+				// hierarchy_root:                /docker/87ee2de57e51bc75175a4d2e81b71d162811b179d549d6601ed70b58cad83578
8c3e2c
+				// cgroup_path_relative_to_mount: /docker/87ee2de57e51bc75175a4d2e81b71d162811b179d549d6601ed70b58cad83578/my_named_cgroup
8c3e2c
+				// append do the cgroup_path:     /my_named_cgroup
8c3e2c
+				// final cgroup_path:             /sys/fs/cgroup/cpu/my_named_cgroup
8c3e2c
+				//
8c3e2c
+				// Example 2 (out of docker)
8c3e2c
+				// hierarchy_mount:               /sys/fs/cgroup/cpu
8c3e2c
+				// hierarchy_root:                /
8c3e2c
+				// cgroup_path_relative_to_mount: /my_named_cgroup
8c3e2c
+				// append do the cgroup_path:     /my_named_cgroup
8c3e2c
+				// final cgroup_path:             /sys/fs/cgroup/cpu/my_named_cgroup
8c3e2c
+				common_path_prefix_len = strlen(hierarchy_root);
8c3e2c
+				if ((common_path_prefix_len == 1) || 
8c3e2c
+				    (strncmp(hierarchy_root, cgroup_path_relative_to_mount, common_path_prefix_len) != 0))
8c3e2c
+					common_path_prefix_len = 0;
8c3e2c
+
8c3e2c
+				g_assert((cgroup_path_relative_to_mount[common_path_prefix_len] == '/') || 
8c3e2c
+				         (cgroup_path_relative_to_mount[common_path_prefix_len] == '\0'));
8c3e2c
+
8c3e2c
+				strcat(cgroup_path, cgroup_path_relative_to_mount + common_path_prefix_len);
8c3e2c
+			}
8c3e2c
+		}
8c3e2c
+	}
8c3e2c
+
8c3e2c
+	free(hierarchy_mount);
8c3e2c
+	free(hierarchy_root);
8c3e2c
+	free(cgroup_path_relative_to_mount);
8c3e2c
+	return cgroup_path;
8c3e2c
+}
8c3e2c
+
8c3e2c
+/**
8c3e2c
+ * @findHierarchyMount
8c3e2c
+ *
8c3e2c
+ * @param[in] is_subsystem - Comparison function
8c3e2c
+ * @param[out] pmountpath -
8c3e2c
+ * @param[out] pmountroot -
8c3e2c
+ *
8c3e2c
+ * Check the /proc filesystem to determine the root and mount path of /sys/fs/cgroup data
8c3e2c
+ */
8c3e2c
+static void 
8c3e2c
+findHierarchyMount(gboolean (*is_subsystem)(const char *), char** pmountpath, char** pmountroot)
8c3e2c
+{
8c3e2c
+	char *line = NULL;
8c3e2c
+	size_t lineLen = 0, maxLineLen = 0;
8c3e2c
+	char *filesystemType = NULL;
8c3e2c
+	char *options = NULL;
8c3e2c
+	char *mountpath = NULL;
8c3e2c
+	char *mountroot = NULL;
8c3e2c
+
8c3e2c
+	FILE *mountinfofile = fopen(PROC_MOUNTINFO_FILENAME, "r");
8c3e2c
+	if (mountinfofile == NULL) 
8c3e2c
+		goto done;
8c3e2c
+
8c3e2c
+	while (getline(&line, &lineLen, mountinfofile) != -1) {
8c3e2c
+		if (filesystemType == NULL || lineLen > maxLineLen) {
8c3e2c
+			free(filesystemType);
8c3e2c
+			filesystemType = NULL;
8c3e2c
+			free(options);
8c3e2c
+			options = NULL;
8c3e2c
+			filesystemType = (char*)malloc(lineLen+1);
8c3e2c
+			if (filesystemType == NULL)
8c3e2c
+				goto done;
8c3e2c
+			options = (char*)malloc(lineLen+1);
8c3e2c
+			if (options == NULL)
8c3e2c
+				goto done;
8c3e2c
+			maxLineLen = lineLen;
8c3e2c
+		}
8c3e2c
+
8c3e2c
+		char *separatorChar = strstr(line, " - ");
8c3e2c
+
8c3e2c
+		// See man page of proc to get format for /proc/self/mountinfo file
8c3e2c
+		int sscanfRet = sscanf(separatorChar,
8c3e2c
+		   " - %s %*s %s",
8c3e2c
+		   filesystemType,
8c3e2c
+		   options);
8c3e2c
+		if (sscanfRet != 2) {
8c3e2c
+			g_assert(!"Failed to parse mount info file contents with sscanf.");
8c3e2c
+			goto done;
8c3e2c
+		}
8c3e2c
+
8c3e2c
+		if (strncmp(filesystemType, "cgroup", 6) == 0) {
8c3e2c
+			gboolean isSubsystemMatch = is_subsystem == NULL;
8c3e2c
+			if (!isSubsystemMatch) {
8c3e2c
+				char *context = NULL;
8c3e2c
+				char *strTok = strtok_r(options, ",", &context);
8c3e2c
+				while (!isSubsystemMatch && strTok != NULL)
8c3e2c
+				{
8c3e2c
+					isSubsystemMatch = is_subsystem(strTok);
8c3e2c
+					strTok = strtok_r(NULL, ",", &context);
8c3e2c
+				}
8c3e2c
+			}
8c3e2c
+			if (isSubsystemMatch) {
8c3e2c
+				mountpath = (char*)malloc(lineLen+1);
8c3e2c
+				if (mountpath == NULL)
8c3e2c
+					goto done;
8c3e2c
+				mountroot = (char*)malloc(lineLen+1);
8c3e2c
+				if (mountroot == NULL)
8c3e2c
+					goto done;
8c3e2c
+
8c3e2c
+				sscanfRet = sscanf(line,
8c3e2c
+					   "%*s %*s %*s %s %s ",
8c3e2c
+					   mountroot,
8c3e2c
+					   mountpath);
8c3e2c
+				if (sscanfRet != 2)
8c3e2c
+					g_assert(!"Failed to parse mount info file contents with sscanf.");
8c3e2c
+
8c3e2c
+				// assign the output arguments and clear the locals so we don't free them.
8c3e2c
+				*pmountpath = mountpath;
8c3e2c
+				*pmountroot = mountroot;
8c3e2c
+				mountpath = mountroot = NULL;
8c3e2c
+			}
8c3e2c
+		}
8c3e2c
+	}
8c3e2c
+done:
8c3e2c
+	free(mountpath);
8c3e2c
+	free(mountroot);
8c3e2c
+	free(filesystemType);
8c3e2c
+	free(options);
8c3e2c
+	free(line);
8c3e2c
+	if (mountinfofile)
8c3e2c
+		fclose(mountinfofile);
8c3e2c
+}
8c3e2c
+
8c3e2c
+/**
8c3e2c
+ * @findCGroupPathForSubsystem
8c3e2c
+ *
8c3e2c
+ * @param[in] is_subsystem - Comparison function
8c3e2c
+ * @returns cgroup path for the memory subsystem
8c3e2c
+ *
8c3e2c
+ * Check the /proc filesystem to determine the root and mount path of /sys/fs/cgroup data
8c3e2c
+ */
8c3e2c
+static char * 
8c3e2c
+findCGroupPathForSubsystem(gboolean (*is_subsystem)(const char *))
8c3e2c
+{
8c3e2c
+	char *line = NULL;
8c3e2c
+	size_t lineLen = 0;
8c3e2c
+	size_t maxLineLen = 0;
8c3e2c
+	char *subsystem_list = NULL;
8c3e2c
+	char *cgroup_path = NULL;
8c3e2c
+	gboolean result = FALSE;
8c3e2c
+
8c3e2c
+	FILE *cgroupfile = fopen(PROC_CGROUP_FILENAME, "r");
8c3e2c
+	if (cgroupfile == NULL)
8c3e2c
+		goto done;
8c3e2c
+
8c3e2c
+	while (!result && getline(&line, &lineLen, cgroupfile) != -1) {
8c3e2c
+		if (subsystem_list == NULL || lineLen > maxLineLen) {
8c3e2c
+			free(subsystem_list);
8c3e2c
+			subsystem_list = NULL;
8c3e2c
+			free(cgroup_path);
8c3e2c
+			cgroup_path = NULL;
8c3e2c
+			subsystem_list = (char*)malloc(lineLen+1);
8c3e2c
+			if (subsystem_list == NULL)
8c3e2c
+				goto done;
8c3e2c
+			cgroup_path = (char*)malloc(lineLen+1);
8c3e2c
+			if (cgroup_path == NULL)
8c3e2c
+				goto done;
8c3e2c
+			maxLineLen = lineLen;
8c3e2c
+		}
8c3e2c
+
8c3e2c
+		if (s_cgroup_version == 1) {
8c3e2c
+			// See man page of proc to get format for /proc/self/cgroup file
8c3e2c
+			int sscanfRet = sscanf(line,
8c3e2c
+			"%*[^:]:%[^:]:%s",
8c3e2c
+			subsystem_list,
8c3e2c
+			cgroup_path);
8c3e2c
+			if (sscanfRet != 2) {
8c3e2c
+				g_assert(!"Failed to parse cgroup info file contents with sscanf.");
8c3e2c
+				goto done;
8c3e2c
+			}
8c3e2c
+
8c3e2c
+			char* context = NULL;
8c3e2c
+			char* strTok = strtok_r(subsystem_list, ",", &context);
8c3e2c
+			while (strTok != NULL) {
8c3e2c
+				if (is_subsystem(strTok)) {
8c3e2c
+					result = TRUE;
8c3e2c
+					break;
8c3e2c
+				}
8c3e2c
+				strTok = strtok_r(NULL, ",", &context);
8c3e2c
+			}
8c3e2c
+		} else if (s_cgroup_version == 2) {
8c3e2c
+			// See https://www.kernel.org/doc/Documentation/cgroup-v2.txt
8c3e2c
+			// Look for a "0::/some/path"
8c3e2c
+			int sscanfRet = sscanf(line,
8c3e2c
+			"0::%s",
8c3e2c
+			cgroup_path);
8c3e2c
+			if (sscanfRet == 1)
8c3e2c
+			{
8c3e2c
+				result = TRUE;
8c3e2c
+			}
8c3e2c
+		} else {
8c3e2c
+			g_assert(!"Unknown cgroup version in mountinfo.");
8c3e2c
+			goto done;
8c3e2c
+		}
8c3e2c
+	}
8c3e2c
+done:
8c3e2c
+	free(subsystem_list);
8c3e2c
+	if (!result) {
8c3e2c
+		free(cgroup_path);
8c3e2c
+		cgroup_path = NULL;
8c3e2c
+	}
8c3e2c
+	free(line);
8c3e2c
+	if (cgroupfile)
8c3e2c
+		fclose(cgroupfile);
8c3e2c
+	return cgroup_path;
8c3e2c
+}
8c3e2c
+
8c3e2c
+/**
8c3e2c
+ * @getCGroupMemoryLimit
8c3e2c
+ *
8c3e2c
+ * @param[out] val - Memory limit
8c3e2c
+ * @param[in] filename - name of file from which to extract limit
8c3e2c
+ * @returns True if value found
8c3e2c
+ *
8c3e2c
+ * Extract memory limit from specified /sys/fs/cgroup/memory file
8c3e2c
+ */
8c3e2c
+static gboolean 
8c3e2c
+getCGroupMemoryLimit(guint64 *val, const char *filename)
8c3e2c
+{
8c3e2c
+	if (s_memory_cgroup_path == NULL)
8c3e2c
+		return FALSE;
8c3e2c
+
8c3e2c
+	char* mem_limit_filename = NULL;
8c3e2c
+	if (asprintf(&mem_limit_filename, "%s%s", s_memory_cgroup_path, filename) < 0)
8c3e2c
+		return FALSE;
8c3e2c
+
8c3e2c
+	gboolean result = readMemoryValueFromFile(mem_limit_filename, val);
8c3e2c
+	free(mem_limit_filename);
8c3e2c
+	return result;
8c3e2c
+}
8c3e2c
+
8c3e2c
+/**
8c3e2c
+ * @getCGroupMemoryUsage
8c3e2c
+ *
8c3e2c
+ * @param[out] val - Memory limit
8c3e2c
+ * @returns True if value found
8c3e2c
+ *
8c3e2c
+ * Extract memory usage from /sys/fs/cgroup/memory.stat file
8c3e2c
+ */
8c3e2c
+static gboolean 
8c3e2c
+getCGroupMemoryUsage(size_t *val)
8c3e2c
+{
8c3e2c
+	if (s_memory_cgroup_path == NULL)
8c3e2c
+		return FALSE;
8c3e2c
+
8c3e2c
+	char *stat_filename = NULL;
8c3e2c
+	if (asprintf(&stat_filename, "%s%s", s_memory_cgroup_path, CGROUP_MEMORY_STAT_FILENAME) < 0)
8c3e2c
+		return FALSE;
8c3e2c
+
8c3e2c
+	FILE *stat_file = fopen(stat_filename, "r");
8c3e2c
+	free(stat_filename);
8c3e2c
+	if (stat_file == NULL)
8c3e2c
+		return FALSE;
8c3e2c
+
8c3e2c
+	char *line = NULL;
8c3e2c
+	size_t lineLen = 0;
8c3e2c
+	size_t readValues = 0;
8c3e2c
+	char *endptr;
8c3e2c
+
8c3e2c
+	*val = 0;
8c3e2c
+	while (getline(&line, &lineLen, stat_file) != -1 && readValues < s_mem_stat_n_keys) {
8c3e2c
+		for (size_t i = 0; i < s_mem_stat_n_keys; i++) {
8c3e2c
+			if (strncmp(line, s_mem_stat_key_names[i], s_mem_stat_key_lengths[i]) == 0) {
8c3e2c
+				errno = 0;
8c3e2c
+				const char *startptr = line + s_mem_stat_key_lengths[i];
8c3e2c
+				*val += strtoll(startptr, &endptr, 10);
8c3e2c
+				if (endptr != startptr && errno == 0)
8c3e2c
+					readValues++;
8c3e2c
+
8c3e2c
+				break;
8c3e2c
+			}
8c3e2c
+		}
8c3e2c
+	}
8c3e2c
+
8c3e2c
+	fclose(stat_file);
8c3e2c
+	free(line);
8c3e2c
+
8c3e2c
+	if (readValues == s_mem_stat_n_keys)
8c3e2c
+		return TRUE;
8c3e2c
+
8c3e2c
+	return FALSE;
8c3e2c
+}
8c3e2c
+
8c3e2c
+/**
8c3e2c
+ * @getRestrictedPhysicalMemoryLimit
8c3e2c
+ *
8c3e2c
+ * @returns Physical memory limit
8c3e2c
+ *
8c3e2c
+ * Determine if there are any limits on memory and return the value
8c3e2c
+ * if so. Zero represents no limit.
8c3e2c
+ */
8c3e2c
+size_t 
8c3e2c
+getRestrictedPhysicalMemoryLimit()
8c3e2c
+{
8c3e2c
+	guint64 physical_memory_limit = 0;
8c3e2c
+
8c3e2c
+	if (s_mem_stat_n_keys == 0)
8c3e2c
+		initialize();
8c3e2c
+
8c3e2c
+	if (!getPhysicalMemoryLimit(&physical_memory_limit))
8c3e2c
+		return 0;
8c3e2c
+
8c3e2c
+	// If there's no memory limit specified on the container this
8c3e2c
+	// actually returns 0x7FFFFFFFFFFFF000 (2^63-1 rounded down to
8c3e2c
+	// 4k which is a common page size). So we know we are not
8c3e2c
+	// running in a memory restricted environment.
8c3e2c
+	if (physical_memory_limit > 0x7FFFFFFF00000000)
8c3e2c
+		return 0;
8c3e2c
+
8c3e2c
+	return (getPhysicalMemoryTotal(physical_memory_limit));
8c3e2c
+}
8c3e2c
+
8c3e2c
+/**
8c3e2c
+ * @getPhysicalMemoryTotal
8c3e2c
+ *
8c3e2c
+ * @param[in] physical_memory_limit - The max memory on the system
8c3e2c
+ * @returns Physical memory total
8c3e2c
+ *
8c3e2c
+ * Check the input limit against any system limits or actual memory on system
8c3e2c
+ */
8c3e2c
+static size_t
8c3e2c
+getPhysicalMemoryTotal(size_t physical_memory_limit)
8c3e2c
+{
8c3e2c
+	struct rlimit curr_rlimit;
8c3e2c
+	size_t rlimit_soft_limit = (size_t)RLIM_INFINITY;
8c3e2c
+	if (getrlimit(RLIMIT_AS, &curr_rlimit) == 0)
8c3e2c
+		rlimit_soft_limit = curr_rlimit.rlim_cur;
8c3e2c
+	physical_memory_limit = (physical_memory_limit < rlimit_soft_limit) ?
8c3e2c
+				 physical_memory_limit : rlimit_soft_limit;
8c3e2c
+
8c3e2c
+	// Ensure that limit is not greater than real memory size
8c3e2c
+	long pages = sysconf(_SC_PHYS_PAGES);
8c3e2c
+	if (pages != -1) {
8c3e2c
+		if (pageSize != -1) {
8c3e2c
+			physical_memory_limit = (physical_memory_limit < (size_t)pages * pageSize) ?
8c3e2c
+						 physical_memory_limit : (size_t)pages * pageSize;
8c3e2c
+		}
8c3e2c
+	}
8c3e2c
+
8c3e2c
+	if (physical_memory_limit > ULONG_MAX) {
8c3e2c
+		// It is observed in practice when the memory is unrestricted, Linux control
8c3e2c
+		// group returns a physical limit that is bigger than the address space
8c3e2c
+		return ULONG_MAX;
8c3e2c
+	} else
8c3e2c
+		return (size_t)physical_memory_limit;
8c3e2c
+}
8c3e2c
+
8c3e2c
+/**
8c3e2c
+ * @getPhysicalMemoryUsed
8c3e2c
+ *
8c3e2c
+ * @param[out] val - pointer to the memory usage value 
8c3e2c
+ * @returns True if we are able to determine usage
8c3e2c
+ *
8c3e2c
+ * Determine the amount of memory in use
8c3e2c
+ */
8c3e2c
+gboolean 
8c3e2c
+getPhysicalMemoryUsed(size_t *val)
8c3e2c
+{
8c3e2c
+	gboolean result = FALSE;
8c3e2c
+	size_t linelen;
8c3e2c
+	char *line = NULL;
8c3e2c
+
8c3e2c
+	if (val == NULL)
8c3e2c
+		return FALSE;
8c3e2c
+
8c3e2c
+	// Linux uses cgroup usage to trigger oom kills.
8c3e2c
+	if (getPhysicalMemoryUsage(val))
8c3e2c
+		return TRUE;
8c3e2c
+
8c3e2c
+	// process resident set size.
8c3e2c
+	FILE* file = fopen(PROC_STATM_FILENAME, "r");
8c3e2c
+	if (file != NULL && getline(&line, &linelen, file) != -1) {
8c3e2c
+		char* context = NULL;
8c3e2c
+		char* strTok = strtok_r(line, " ", &context);
8c3e2c
+		strTok = strtok_r(NULL, " ", &context);
8c3e2c
+
8c3e2c
+		errno = 0;
8c3e2c
+		*val = strtoull(strTok, NULL, 0);
8c3e2c
+		if (errno == 0) {
8c3e2c
+			if (pageSize != -1) {
8c3e2c
+				*val = *val * pageSize;
8c3e2c
+				result = TRUE;
8c3e2c
+			}
8c3e2c
+		}
8c3e2c
+	}
8c3e2c
+
8c3e2c
+	if (file)
8c3e2c
+		fclose(file);
8c3e2c
+	free(line);
8c3e2c
+	return result;
8c3e2c
+}
8c3e2c
+
8c3e2c
+/**
8c3e2c
+ * @getPhysicalMemoryAvail
8c3e2c
+ *
8c3e2c
+ * @returns Amount of memory available
8c3e2c
+ *
8c3e2c
+ * Determine the amount of memory available by examininig any limits and
8c3e2c
+ * checking what memory is in use.
8c3e2c
+ */
8c3e2c
+size_t
8c3e2c
+getPhysicalMemoryAvail()
8c3e2c
+{
8c3e2c
+	size_t max, used, avail, sysAvail;
8c3e2c
+
8c3e2c
+	max = getRestrictedPhysicalMemoryLimit();
8c3e2c
+
8c3e2c
+	if (max == 0)
8c3e2c
+		max = getPhysicalMemoryTotal(ULONG_MAX);
8c3e2c
+
8c3e2c
+	if (getPhysicalMemoryUsed(&used))
8c3e2c
+		avail = max - used;
8c3e2c
+	else
8c3e2c
+		avail = max;
8c3e2c
+
8c3e2c
+	sysAvail = sysconf(_SC_AVPHYS_PAGES) * pageSize;
8c3e2c
+	return (avail < sysAvail ? avail : sysAvail);
8c3e2c
+}
8c3e2c
Index: a/src/mono/mono/utils/CMakeLists.txt
8c3e2c
===================================================================
8c3e2c
--- a/src/mono/mono/utils/CMakeLists.txt
8c3e2c
+++ b/src/mono/mono/utils/CMakeLists.txt
8c3e2c
@@ -32,6 +32,7 @@ set(utils_common_sources
8c3e2c
     mono-sha1.c
8c3e2c
     mono-logger.c
8c3e2c
     mono-logger-internals.h
8c3e2c
+    mono-cgroup.c
8c3e2c
     mono-codeman.c
8c3e2c
     mono-counters.c
8c3e2c
     mono-compiler.h