Skip to content

Commit b953bc6

Browse files
authored
[mono][sgen] Prevent concurrent sweep from blocking major collections (#98154)
Sweeping is the final GC stage, that does a final iteration on the memory in order to prepare internal data structures for future allocation, free memory etc. For large objects this is done during GC while for small objects it can be done concurrently with the mutator, after we resume the world. We attempt to trigger the next major collectio when the heap grows with another third of its size at the moment of the last collection. This heap limit is the major trigger size and it can only be computed after the sweep job has finished. This is because sweep iterates each major block to determine if they have any marked objects, attempting to free the blocks if possible. Because there is a dependency for setting the major trigger size on the sweep job completion, before this change we were just blocking any new major collections to happen if concurrent sweep wasn't finished. This means that if the sweep job takes longer than expected and the mutator does excessive allocation in short amount of time, the memory usage can increase aggresively. It is unclear how relevant this scenario is in practice, but it is easy to reproduce in a micro benchmark allocating large objects. The fix relies on computing estimates for the current heap size and the soon to be computed trigger size. We estimate the current live number of major blocks by adding the number of major blocks allocated since the last collection to the number of blocks already traversed and determined to be live by sweep. This estimate will be a lower limit. We determine the number of major blocks during the previous collection by subtracting the number of major blocks before sweep starts by the number of blocks already traversed and determined to be free. This estimate will be an upper limit. If the lower limit of the current heap size exceeds the upper limit of the trigger size, then there is no point in waiting for sweep to finish. We know we will require a major collection.
1 parent c147560 commit b953bc6

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

src/mono/mono/sgen/sgen-gc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,8 @@ struct _SgenMajorCollector {
716716
gboolean (*ptr_is_from_pinned_alloc) (char *ptr);
717717
void (*report_pinned_memory_usage) (void);
718718
size_t (*get_num_major_sections) (void);
719+
size_t (*get_min_live_major_sections) (void);
720+
size_t (*get_max_last_major_survived_sections) (void);
719721
size_t (*get_num_empty_blocks) (void);
720722
size_t (*get_bytes_survived_last_sweep) (void);
721723
gboolean (*handle_gc_param) (const char *opt);

src/mono/mono/sgen/sgen-marksweep.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,7 @@ static size_t *sweep_num_blocks;
15241524

15251525
static volatile size_t num_major_sections_before_sweep;
15261526
static volatile size_t num_major_sections_freed_in_sweep;
1527+
static volatile size_t num_major_sections_survived_in_sweep;
15271528

15281529
static void
15291530
sgen_worker_clear_free_block_lists (WorkerData *worker)
@@ -1707,6 +1708,7 @@ ensure_block_is_checked_for_sweeping (guint32 block_index, gboolean wait, gboole
17071708

17081709
/* FIXME: Do we need the heap boundaries while we do nursery collections? */
17091710
update_heap_boundaries_for_block (block);
1711+
SGEN_ATOMIC_ADD_P (num_major_sections_survived_in_sweep, 1);
17101712
} else {
17111713
/*
17121714
* Blocks without live objects are removed from the
@@ -1842,6 +1844,7 @@ major_sweep (void)
18421844

18431845
num_major_sections_before_sweep = num_major_sections;
18441846
num_major_sections_freed_in_sweep = 0;
1847+
num_major_sections_survived_in_sweep = 0;
18451848

18461849
SGEN_ASSERT (0, !sweep_job, "We haven't finished the last sweep?");
18471850
if (concurrent_sweep) {
@@ -2319,6 +2322,28 @@ get_num_major_sections (void)
23192322
return num_major_sections;
23202323
}
23212324

2325+
// Conservative values for computing trigger size, without needing concurrent sweep to finish
2326+
// As concurrent sweep job advances in execution, these values get closer to the real value.
2327+
// This contains at least the number of blocks determined to be live by sweep job (which increases
2328+
// as sweep progresses) plus any new blocks allocated by the application.
2329+
static size_t
2330+
get_min_live_major_sections (void)
2331+
{
2332+
// Note that num_major_sections gets decremented for each freed block, so to obtain the real block count
2333+
// we would need to add back num_major_sections_freed_in_sweep, but this is racy so we are being conservative.
2334+
if (num_major_sections > num_major_sections_before_sweep)
2335+
return num_major_sections_survived_in_sweep + (num_major_sections - num_major_sections_before_sweep);
2336+
else
2337+
return num_major_sections_survived_in_sweep;
2338+
}
2339+
2340+
static size_t
2341+
get_max_last_major_survived_sections (void)
2342+
{
2343+
// num_major_sections_freed_in_sweep increases as sweep progresses.
2344+
return num_major_sections_before_sweep - num_major_sections_freed_in_sweep;
2345+
}
2346+
23222347
static size_t
23232348
get_num_empty_blocks (void)
23242349
{
@@ -2886,6 +2911,8 @@ sgen_marksweep_init_internal (SgenMajorCollector *collector, gboolean is_concurr
28862911
collector->ptr_is_from_pinned_alloc = ptr_is_from_pinned_alloc;
28872912
collector->report_pinned_memory_usage = major_report_pinned_memory_usage;
28882913
collector->get_num_major_sections = get_num_major_sections;
2914+
collector->get_min_live_major_sections = get_min_live_major_sections;
2915+
collector->get_max_last_major_survived_sections = get_max_last_major_survived_sections;
28892916
collector->get_num_empty_blocks = get_num_empty_blocks;
28902917
collector->get_bytes_survived_last_sweep = get_bytes_survived_last_sweep;
28912918
collector->handle_gc_param = major_handle_gc_param;

src/mono/mono/sgen/sgen-memory-governor.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,20 @@ sgen_memgov_calculate_minor_collection_allowance (void)
130130
}
131131
}
132132

133+
// This can be called while sweep is running to determine earlier if there is so much memory growth
134+
// that we know we will require a GC once sweep finishes.
135+
static gboolean
136+
sgen_need_major_collection_conservative (void)
137+
{
138+
size_t min_heap_size = sgen_los_memory_usage + sgen_major_collector.get_min_live_major_sections () * sgen_major_collector.section_size;
139+
140+
size_t max_last_collection_heap_size = last_collection_los_memory_usage + sgen_major_collector.get_max_last_major_survived_sections () * sgen_major_collector.section_size;
141+
size_t max_allowance = GDOUBLE_TO_SIZE (max_last_collection_heap_size * SGEN_DEFAULT_ALLOWANCE_HEAP_SIZE_RATIO);
142+
max_allowance = MAX (max_allowance, GDOUBLE_TO_SIZE (MIN_MINOR_COLLECTION_ALLOWANCE));
143+
144+
return min_heap_size > max_allowance;
145+
}
146+
133147
static size_t
134148
get_heap_size (void)
135149
{
@@ -184,9 +198,11 @@ sgen_need_major_collection (mword space_needed, gboolean *forced)
184198
return FALSE;
185199
}
186200

187-
/* FIXME: This is a cop-out. We should have some way of figuring this out. */
188-
if (!sgen_major_collector.have_swept ())
201+
if (!sgen_major_collector.have_swept ()) {
202+
if (sgen_need_major_collection_conservative ())
203+
return TRUE;
189204
return FALSE;
205+
}
190206

191207
if (space_needed > sgen_memgov_available_free_space ())
192208
return TRUE;

0 commit comments

Comments
 (0)