@@ -72,8 +72,11 @@ const MAX_TASKS_PER_OLD_GENERATION: usize = 200_000;
72
72
const PERCENTAGE_TO_COLLECT : usize = 30 ;
73
73
const TASK_BASE_MEMORY_USAGE : usize = 1_000 ;
74
74
const TASK_BASE_COMPUTE_DURATION_IN_MICROS : u64 = 1_000 ;
75
- pub const PERCENTAGE_TARGET_MEMORY : usize = 88 ;
76
- pub const PERCENTAGE_IDLE_TARGET_MEMORY : usize = 75 ;
75
+ pub const PERCENTAGE_MIN_TARGET_MEMORY : usize = 70 ;
76
+ pub const PERCENTAGE_MAX_TARGET_MEMORY : usize = 75 ;
77
+ pub const PERCENTAGE_MIN_IDLE_TARGET_MEMORY : usize = 55 ;
78
+ pub const PERCENTAGE_MAX_IDLE_TARGET_MEMORY : usize = 60 ;
79
+ pub const MAX_GC_STEPS : usize = 100 ;
77
80
78
81
struct OldGeneration {
79
82
tasks : Vec < TaskId > ,
@@ -82,6 +85,7 @@ struct OldGeneration {
82
85
83
86
#[ derive( Default ) ]
84
87
struct ProcessGenerationResult {
88
+ old_generations : usize ,
85
89
priority : Option < GcPriority > ,
86
90
content_dropped_count : usize ,
87
91
unloaded_count : usize ,
@@ -224,18 +228,22 @@ impl GcQueue {
224
228
& self ,
225
229
backend : & MemoryBackend ,
226
230
turbo_tasks : & dyn TurboTasksBackendApi < MemoryBackend > ,
227
- ) -> ProcessGenerationResult {
228
- let old_generation = {
231
+ ) -> Option < ProcessGenerationResult > {
232
+ let old_generation_info = {
229
233
let guard = & mut self . generations . lock ( ) ;
230
- guard. pop_back ( )
234
+ let len = guard. len ( ) ;
235
+ guard. pop_back ( ) . map ( |g| ( g, len) )
231
236
} ;
232
- let Some ( OldGeneration {
233
- mut tasks,
234
- generation,
235
- } ) = old_generation
237
+ let Some ( (
238
+ OldGeneration {
239
+ mut tasks,
240
+ generation,
241
+ } ,
242
+ old_generations,
243
+ ) ) = old_generation_info
236
244
else {
237
245
// No old generation to process
238
- return ProcessGenerationResult :: default ( ) ;
246
+ return None ;
239
247
} ;
240
248
// Check all tasks for the correct generation
241
249
let mut indices = Vec :: with_capacity ( tasks. len ( ) ) ;
@@ -254,7 +262,10 @@ impl GcQueue {
254
262
255
263
if indices. is_empty ( ) {
256
264
// No valid tasks in old generation to process
257
- return ProcessGenerationResult :: default ( ) ;
265
+ return Some ( ProcessGenerationResult {
266
+ old_generations,
267
+ ..ProcessGenerationResult :: default ( )
268
+ } ) ;
258
269
}
259
270
260
271
// Sorting based on sort_by_cached_key from std lib
@@ -329,23 +340,24 @@ impl GcQueue {
329
340
} ) ;
330
341
}
331
342
332
- ProcessGenerationResult {
343
+ Some ( ProcessGenerationResult {
344
+ old_generations,
333
345
priority : Some ( max_priority) ,
334
346
content_dropped_count,
335
347
unloaded_count,
336
348
already_unloaded_count,
337
- }
349
+ } )
338
350
}
339
351
340
- /// Run garbage collection on the queue.
352
+ /// Run garbage collection on the queue. Returns true, if some progress has
353
+ /// been made. Returns the number of old generations.
341
354
pub fn run_gc (
342
355
& self ,
343
356
backend : & MemoryBackend ,
344
357
turbo_tasks : & dyn TurboTasksBackendApi < MemoryBackend > ,
345
- ) -> Option < ( GcPriority , usize ) > {
358
+ ) -> Option < usize > {
346
359
let span = tracing:: trace_span!(
347
- parent: None ,
348
- "garbage collection" ,
360
+ "garbage collection step" ,
349
361
priority = Empty ,
350
362
deactivations_count = Empty ,
351
363
content_dropped_count = Empty ,
@@ -358,23 +370,27 @@ impl GcQueue {
358
370
count : deactivations_count,
359
371
} = self . process_deactivations ( backend, turbo_tasks) ;
360
372
361
- let ProcessGenerationResult {
373
+ if let Some ( ProcessGenerationResult {
374
+ old_generations,
362
375
priority,
363
376
content_dropped_count,
364
377
unloaded_count,
365
378
already_unloaded_count,
366
- } = self . process_old_generation ( backend, turbo_tasks) ;
379
+ } ) = self . process_old_generation ( backend, turbo_tasks)
380
+ {
381
+ span. record ( "deactivations_count" , deactivations_count) ;
382
+ span. record ( "content_dropped_count" , content_dropped_count) ;
383
+ span. record ( "unloaded_count" , unloaded_count) ;
384
+ span. record ( "already_unloaded_count" , already_unloaded_count) ;
385
+ if let Some ( priority) = & priority {
386
+ span. record ( "priority" , debug ( priority) ) ;
387
+ } else {
388
+ span. record ( "priority" , "" ) ;
389
+ }
367
390
368
- span. record ( "deactivations_count" , deactivations_count) ;
369
- span. record ( "content_dropped_count" , content_dropped_count) ;
370
- span. record ( "unloaded_count" , unloaded_count) ;
371
- span. record ( "already_unloaded_count" , already_unloaded_count) ;
372
- if let Some ( priority) = & priority {
373
- span. record ( "priority" , debug ( priority) ) ;
391
+ Some ( old_generations)
374
392
} else {
375
- span . record ( "priority" , "" ) ;
393
+ ( deactivations_count > 0 ) . then_some ( 0 )
376
394
}
377
-
378
- priority. map ( |p| ( p, content_dropped_count) )
379
395
}
380
396
}
0 commit comments