Skip to content

Commit

Permalink
[mono] Fix sgen_gc_info.memory_load_bytes (#53364)
Browse files Browse the repository at this point in the history
* [mono] Fix sgen_gc_info.memory_load_bytes

Set it to used memory size instead of available memory size

* Change the default size

For systems without information about available physical memory size

Also handle -1 return values from sysconf calls

* Scale the memory load by total_available/ram_size
  • Loading branch information
radekdoulik authored May 31, 2021
1 parent 3846a9c commit 93cf5df
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/mono/mono/sgen/sgen-memory-governor.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ update_gc_info (mword used_slots_size)

sgen_gc_info.heap_size_bytes = major_size + sgen_los_memory_usage_total;
sgen_gc_info.fragmented_bytes = sgen_gc_info.heap_size_bytes - sgen_los_memory_usage - major_size_in_use;
sgen_gc_info.memory_load_bytes = mono_determine_physical_ram_available_size ();
guint64 physical_ram_size = mono_determine_physical_ram_size ();
sgen_gc_info.memory_load_bytes = physical_ram_size ? sgen_gc_info.total_available_memory_bytes - (guint64)(((double)sgen_gc_info.total_available_memory_bytes*mono_determine_physical_ram_available_size ())/physical_ram_size) : 0;
sgen_gc_info.total_committed_bytes = major_size_in_use + sgen_los_memory_usage;
sgen_gc_info.total_promoted_bytes = sgen_total_promoted_size - total_promoted_size_start;
sgen_gc_info.total_major_size_bytes = major_size;
Expand Down
32 changes: 17 additions & 15 deletions src/mono/mono/utils/memfuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ mono_gc_memmove_atomic (void *dest, const void *src, size_t size)
mono_gc_memmove_aligned (dest, src, size);
}

#define _DEFAULT_MEM_SIZE 134217728

guint64
mono_determine_physical_ram_size (void)
{
Expand Down Expand Up @@ -267,30 +269,30 @@ mono_determine_physical_ram_size (void)

sysctl (mib, 2, &value, &size_sys, NULL, 0);
if (value == 0)
return 134217728;
return _DEFAULT_MEM_SIZE;

return (guint64)value;
#elif defined (HAVE_SYSCONF)
guint64 page_size = 0, num_pages = 0;
gint64 page_size = -1, num_pages = -1;

/* sysconf works on most *NIX operating systems, if your system doesn't have it or if it
* reports invalid values, please add your OS specific code below. */
#ifdef _SC_PAGESIZE
page_size = (guint64)sysconf (_SC_PAGESIZE);
page_size = (gint64)sysconf (_SC_PAGESIZE);
#endif

#ifdef _SC_PHYS_PAGES
num_pages = (guint64)sysconf (_SC_PHYS_PAGES);
num_pages = (gint64)sysconf (_SC_PHYS_PAGES);
#endif

if (!page_size || !num_pages) {
if (page_size == -1 || num_pages == -1) {
g_warning ("Your operating system's sysconf (3) function doesn't correctly report physical memory size!");
return 134217728;
return _DEFAULT_MEM_SIZE;
}

return page_size * num_pages;
return (guint64)page_size * (guint64)num_pages;
#else
return 134217728;
return _DEFAULT_MEM_SIZE;
#endif
}

Expand Down Expand Up @@ -342,25 +344,25 @@ mono_determine_physical_ram_available_size (void)
return (guint64) vmstat.free_count * page_size;

#elif defined (HAVE_SYSCONF)
guint64 page_size = 0, num_pages = 0;
gint64 page_size = -1, num_pages = -1;

/* sysconf works on most *NIX operating systems, if your system doesn't have it or if it
* reports invalid values, please add your OS specific code below. */
#ifdef _SC_PAGESIZE
page_size = (guint64)sysconf (_SC_PAGESIZE);
page_size = (gint64)sysconf (_SC_PAGESIZE);
#endif

#ifdef _SC_AVPHYS_PAGES
num_pages = (guint64)sysconf (_SC_AVPHYS_PAGES);
num_pages = (gint64)sysconf (_SC_AVPHYS_PAGES);
#endif

if (!page_size || !num_pages) {
if (page_size == -1 || num_pages == -1) {
g_warning ("Your operating system's sysconf (3) function doesn't correctly report physical memory size!");
return 0;
return _DEFAULT_MEM_SIZE;
}

return page_size * num_pages;
return (guint64)page_size * (guint64)num_pages;
#else
return 0;
return _DEFAULT_MEM_SIZE;
#endif
}

0 comments on commit 93cf5df

Please sign in to comment.