Skip to content

Commit

Permalink
Get the physical memory usage from memory.stat instead of memory.usag…
Browse files Browse the repository at this point in the history
…e_in_bytes

memory.usage_in_bytes counts rss + cache. We would like only the RSS

which accounts for the current RAM usage.

Signed-off-by: Gregory LEOCADIE <[email protected]>
  • Loading branch information
Gregory LEOCADIE committed Oct 15, 2020
1 parent 3b2896b commit 4a825df
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 18 deletions.
45 changes: 36 additions & 9 deletions src/coreclr/src/gc/unix/cgroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Module Name:
#define CGROUP1_CFS_QUOTA_FILENAME "/cpu.cfs_quota_us"
#define CGROUP1_CFS_PERIOD_FILENAME "/cpu.cfs_period_us"
#define CGROUP2_CPU_MAX_FILENAME "/cpu.max"
#define MEM_STAT_FILENAME "/memory.stat"

extern bool ReadMemoryValueFromFile(const char* filename, uint64_t* val);

Expand Down Expand Up @@ -93,17 +94,43 @@ class CGroup

static bool GetPhysicalMemoryUsage(size_t *val)
{
if (s_cgroup_version == 0)
return false;
else if (s_cgroup_version == 1)
return GetCGroupMemoryUsage(val, CGROUP1_MEMORY_USAGE_FILENAME);
else if (s_cgroup_version == 2)
return GetCGroupMemoryUsage(val, CGROUP2_MEMORY_USAGE_FILENAME);
else
char *mem_stat_filename = nullptr;
bool result = false;

if (s_memory_cgroup_path == nullptr)
return result;

size_t len = strlen(s_memory_cgroup_path);
len += strlen(MEM_STAT_FILENAME);
mem_stat_filename = (char*)malloc(len+1);
if (mem_stat_filename == nullptr)
return result;

strcpy(mem_stat_filename, s_memory_cgroup_path);
strcat(mem_stat_filename, MEM_STAT_FILENAME);

FILE* file = fopen(mem_stat_filename, "r");
if (file != nullptr)
{
assert(!"Unknown cgroup version.");
return false;
char* line = nullptr;
size_t linelen;
while (getline(&line, &linelen, file) != -1)
{
size_t available;
int fieldsParsed = sscanf(line, "rss %zu" , &available);
if (fieldsParsed >= 1)
{
*val = available;
result = true;
break;
}
}
free(line);
fclose(file);
}

free(mem_stat_filename);
return result;
}

static bool GetCpuLimit(uint32_t *val)
Expand Down
46 changes: 37 additions & 9 deletions src/coreclr/src/pal/src/misc/cgroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ SET_DEFAULT_DEBUG_CHANNEL(MISC);
#define CGROUP1_CFS_QUOTA_FILENAME "/cpu.cfs_quota_us"
#define CGROUP1_CFS_PERIOD_FILENAME "/cpu.cfs_period_us"
#define CGROUP2_CPU_MAX_FILENAME "/cpu.max"
#define MEM_STAT_FILENAME "/memory.stat"

class CGroup
{
Expand Down Expand Up @@ -80,17 +81,44 @@ class CGroup

static bool GetPhysicalMemoryUsage(size_t *val)
{
if (s_cgroup_version == 0)
return false;
else if (s_cgroup_version == 1)
return GetCGroupMemoryUsage(val, CGROUP1_MEMORY_USAGE_FILENAME);
else if (s_cgroup_version == 2)
return GetCGroupMemoryUsage(val, CGROUP2_MEMORY_USAGE_FILENAME);
else
char *mem_stat_filename = nullptr;
bool result = false;

if (s_memory_cgroup_path == nullptr)
return result;

size_t len = strlen(s_memory_cgroup_path);
len += strlen(MEM_STAT_FILENAME);
mem_stat_filename = (char*)PAL_malloc(len+1);
if (mem_stat_filename == nullptr)
return result;

strcpy(mem_stat_filename, s_memory_cgroup_path);
strcat(mem_stat_filename, MEM_STAT_FILENAME);

FILE* file = fopen(mem_stat_filename, "r");
if (file != nullptr)
{
_ASSERTE(!"Unknown cgroup version.");
return false;
char* line = nullptr;
size_t linelen;
while (getline(&line, &linelen, file) != -1)
{
size_t available;
int fieldsParsed = sscanf_s(line, "rss %zu" , &available);
if (fieldsParsed >= 1)
{
*val = available;
result = true;
break;
}
}
free(line);
fclose(file);
}

PAL_free(mem_stat_filename);

return result;
}

static bool GetCpuLimit(UINT *val)
Expand Down

0 comments on commit 4a825df

Please sign in to comment.