-
Notifications
You must be signed in to change notification settings - Fork 135
/
gc_gms.c
2280 lines (1729 loc) · 67 KB
/
gc_gms.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2001-2011, Parrot Foundation.
=head1 NAME
src/gc/gc_gms.c - Generational M&S
=head1 DESCRIPTION
Generational, non-compacting, mark and sweep GC.
Objects are stored in N (up to 8) different lists; one for each generation.
Collection with lower number is younger. PObj_GC_generation_0_FLAG,
PObj_GC_generation_1_FLAG, PObj_GC_generation_2_FLAG determine which
generation an object is in. PObj_GC_on_dirty_list_FLAG is set by Write
Barrier.
"Dirty" objects are moved into "dirty_list" out of C<generation> list.
Invariant: new reference from old-to-new generation can be introduced either:
- from objects already in "dirty_list"
- or with moving referent object into "dirty_list"
Corollary: objects in "dirty_list" either:
- not referenced at all
- referenced from same or younger generation
- their referents are on "dirty_list" too
(Proof is very simple and left as exercise for reader)
Notation
1. "*A1:B2" is a live object A in generation 1 referenced by an object B in generation 2.
2. "B0" is a non-live object B in generation 0 not referenced by anything.
3. gn: generation n.
4. R: root set.
5. D: dirty list.
GC algorithm WILL be:
0. Pre-requirements:
a) Invariant described above to maintain dirty_list.
b) Maintain invariant by
i) "sealing" all objects in old generations with Write Barriers.
ii) Write Barrier will:
- Unseal object
- Set "on_dirty_list" flag
- Move object into "dirty_list"
c) gc_gms_mark_pmc_header ignores:
i) objects from generation older than C<self->gen_to_collect>.
ii) objects with on_dirty_list flag set.
iii) move objects to "work_list" for fully mark objects without recursion.
1. Trigger GC (based on phase of the Moon for simplicity. We can tune Moon later).
2. Choose K - how many collections we want to collect. Collections [0..K] will
be collected. Remember K in C<self->gen_to_collect>.
3. Move all objects from dirty_list which has all direct children in
generations not younger than object back to original lists. Reason for this is
"corollary of invariant". We can either collect such objects or they will be
marked by referents from "dirty_list". To perform calculation of youngest
child's generation we temporary override .mark_pmc_header with
C<gc_gms_get_youngest_generation> which iterate over direct children only.
XXX Step3 is wrong. Let's take next picture:
1) Object A(1) reference B(1).
2) We put C(0) to B(1).
3) B(1) moved to dirty_list.
4) We collect Gen1.
5) A(1) become A(2).
6) B(1*) stays in dirty_list
7) C(0) become C(1).
8) We collect Gen1 again.
i) B(1*) moved back to Gen1.
ii) B(1) is referenced only by A(2).
iii) A(2) isn't processed during GC.
iv) We collect B(1).
4. Trace root objects. According to "0. Pre-requirements" we will ignore all
"old" objects. All relevant objects are moved into "work_list".
5. Iterate over "dirty_set" calling VTABLE_mark on it. It will move all
children into "work_list".
6. Iterate over "work_list" calling VTABLE_mark on it.
7. Sweep generations starting from K:
- Destroy all dead objects
- Move live objects into generation max(K+1, N)
- Paint them white.
8. ...
9. Profit!
We are not cleaning "dirty_list" after this process to rescan it again on next
iteration. It allows us to keep track of old-to-new inter-generations
references between iterations. Objects from "dirty_list" which is ready to be
collected handled by "Step 3".
Pictures of GC steps.
TBD
=cut
*/
#include "parrot/parrot.h"
#include "parrot/gc_api.h"
#include "parrot/pointer_array.h"
#include "gc_private.h"
#include "fixed_allocator.h"
#define PANIC_OUT_OF_MEM(size) failed_allocation(__LINE__, (size))
/*
* Maximum number of collections
* NB:
* 1. Maximum number is 8 due limit number of bits in PMC.flags.
* 2. Don't forget to update gc_gms_select_generation_to_collect after changing it!
*/
#define MAX_GENERATIONS 2
/* We allocate additional space in front of PObj* to store additional pointer */
typedef struct pmc_alloc_struct {
void *ptr;
PMC pmc; /* NB: Value! */
} pmc_alloc_struct;
typedef struct string_alloc_struct {
void *ptr;
STRING str; /* NB: Value! */
} string_alloc_struct;
#define PMC2PAC(p) ((pmc_alloc_struct *)((char*)(p) - sizeof (void *)))
#define STR2PAC(p) ((string_alloc_struct *)((char*)(p) - sizeof (void *)))
/* Get generation from PObj->flags */
#define POBJ2GEN(pobj) \
((((pobj)->flags & PObj_GC_generation_0_FLAG) ? 1 : 0) \
+ (((pobj)->flags) & PObj_GC_generation_1_FLAG ? 2 : 0) \
+ (((pobj)->flags) & PObj_GC_generation_2_FLAG ? 4 : 0))
/* Get flags for generation number */
#define GEN2FLAGS(gen) \
(((gen) & 1 ? PObj_GC_generation_0_FLAG : 0) \
| ((gen) & 2 ? PObj_GC_generation_1_FLAG : 0) \
| ((gen) & 4 ? PObj_GC_generation_2_FLAG : 0))
#define SET_GEN_FLAGS(pmc, gen) PObj_flags_SETTO((pmc), ((pmc)->flags & ~PObj_GC_all_generation_FLAGS) | GEN2FLAGS(gen))
/* Private information */
typedef struct MarkSweep_GC {
/* Allocator for PMC headers */
struct Pool_Allocator *pmc_allocator;
/* During M&S gather new live objects in this list */
struct Parrot_Pointer_Array *work_list;
/* During M&S gather new live objects in this list */
struct Parrot_Pointer_Array *dirty_list;
/*
* During checking of dirty_list it will be set to generation of youngest
* child
*/
size_t youngest_child;
/* Currently allocate objects. */
struct Parrot_Pointer_Array *objects[MAX_GENERATIONS];
/* Allocator for strings */
struct Pool_Allocator *string_allocator;
/* MAX_GENERATIONS generations of strings */
struct Parrot_Pointer_Array *strings[MAX_GENERATIONS];
/* Fixed-size allocator */
struct Fixed_Allocator *fixed_size_allocator;
/* String GC */
struct String_GC string_gc;
/* Amount of allocated memory before trigger gc */
size_t gc_threshold;
/* During GC phase - which generation we are collecting */
size_t gen_to_collect;
/* GC blocking */
UINTVAL gc_mark_block_level; /* How many outstanding GC block
requests are there? */
UINTVAL gc_sweep_block_level; /* How many outstanding GC block
requests are there? */
UINTVAL num_early_gc_PMCs; /* how many PMCs want immediate destruction */
} MarkSweep_GC;
/* Callback to destroy PMC or free string storage */
typedef void (*sweep_cb)(PARROT_INTERP, PObj *obj);
/* HEADERIZER HFILE: src/gc/gc_private.h */
/* HEADERIZER BEGIN: static */
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
PARROT_DOES_NOT_RETURN
static void failed_allocation(unsigned int line, unsigned long size);
PARROT_MALLOC
PARROT_CAN_RETURN_NULL
static Buffer* gc_gms_allocate_buffer_header(PARROT_INTERP,
SHIM(size_t size))
__attribute__nonnull__(1);
static void gc_gms_allocate_buffer_storage(PARROT_INTERP,
ARGIN(Buffer *str),
size_t size)
__attribute__nonnull__(1)
__attribute__nonnull__(2);
PARROT_CAN_RETURN_NULL
static void* gc_gms_allocate_fixed_size_storage(PARROT_INTERP, size_t size)
__attribute__nonnull__(1);
PARROT_MALLOC
PARROT_CANNOT_RETURN_NULL
static void * gc_gms_allocate_memory_chunk(SHIM_INTERP, size_t size);
PARROT_MALLOC
PARROT_CANNOT_RETURN_NULL
static void * gc_gms_allocate_memory_chunk_zeroed(SHIM_INTERP, size_t size);
PARROT_MALLOC
PARROT_CAN_RETURN_NULL
static void* gc_gms_allocate_pmc_attributes(PARROT_INTERP, ARGMOD(PMC *pmc))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
FUNC_MODIFIES(*pmc);
PARROT_MALLOC
PARROT_CAN_RETURN_NULL
static PMC* gc_gms_allocate_pmc_header(PARROT_INTERP, UINTVAL flags)
__attribute__nonnull__(1);
PARROT_MALLOC
PARROT_CAN_RETURN_NULL
static STRING* gc_gms_allocate_string_header(PARROT_INTERP,
SHIM(UINTVAL flags))
__attribute__nonnull__(1);
static void gc_gms_allocate_string_storage(PARROT_INTERP,
ARGIN(STRING *str),
size_t size)
__attribute__nonnull__(1)
__attribute__nonnull__(2);
static void gc_gms_block_GC_mark(PARROT_INTERP)
__attribute__nonnull__(1);
static void gc_gms_block_GC_sweep(PARROT_INTERP)
__attribute__nonnull__(1);
static void gc_gms_check_sanity(PARROT_INTERP)
__attribute__nonnull__(1);
static void gc_gms_cleanup_dirty_list(PARROT_INTERP,
ARGIN(MarkSweep_GC *self),
ARGIN(Parrot_Pointer_Array *dirty_list),
size_t gen)
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3);
static void gc_gms_compact_memory_pool(PARROT_INTERP)
__attribute__nonnull__(1);
static size_t gc_gms_count_used_pmc_memory(PARROT_INTERP,
ARGIN(Parrot_Pointer_Array *list))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
static size_t gc_gms_count_used_string_memory(PARROT_INTERP,
ARGIN(Parrot_Pointer_Array *list))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
static void gc_gms_finalize(PARROT_INTERP)
__attribute__nonnull__(1);
static void gc_gms_free_buffer_header(PARROT_INTERP,
ARGFREE(Buffer *s),
SHIM(size_t size))
__attribute__nonnull__(1);
static void gc_gms_free_fixed_size_storage(PARROT_INTERP,
size_t size,
ARGMOD(void *data))
__attribute__nonnull__(1)
__attribute__nonnull__(3)
FUNC_MODIFIES(*data);
static void gc_gms_free_memory_chunk(SHIM_INTERP, ARGFREE(void *data));
static void gc_gms_free_pmc_attributes(PARROT_INTERP, ARGMOD(PMC *pmc))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
FUNC_MODIFIES(*pmc);
static void gc_gms_free_pmc_header(PARROT_INTERP, ARGFREE(PMC *pmc))
__attribute__nonnull__(1);
static void gc_gms_free_string_header(PARROT_INTERP, ARGFREE(STRING *s))
__attribute__nonnull__(1);
static size_t gc_gms_get_gc_info(PARROT_INTERP, Interpinfo_enum which)
__attribute__nonnull__(1);
static void gc_gms_get_youngest_generation(PARROT_INTERP, ARGIN(PMC *pmc))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
static unsigned int gc_gms_is_blocked_GC_mark(PARROT_INTERP)
__attribute__nonnull__(1);
static unsigned int gc_gms_is_blocked_GC_sweep(PARROT_INTERP)
__attribute__nonnull__(1);
static int gc_gms_is_pmc_ptr(PARROT_INTERP, ARGIN_NULLOK(void *ptr))
__attribute__nonnull__(1);
static int gc_gms_is_ptr_owned(PARROT_INTERP,
ARGIN_NULLOK(void *ptr),
ARGIN(Pool_Allocator *pool),
ARGIN(Parrot_Pointer_Array *list))
__attribute__nonnull__(1)
__attribute__nonnull__(3)
__attribute__nonnull__(4);
static int gc_gms_is_string_ptr(PARROT_INTERP, ARGIN_NULLOK(void *ptr))
__attribute__nonnull__(1);
static void gc_gms_iterate_live_strings(PARROT_INTERP,
string_iterator_callback callback,
ARGIN_NULLOK(void *data))
__attribute__nonnull__(1);
static void gc_gms_mark_and_sweep(PARROT_INTERP, UINTVAL flags)
__attribute__nonnull__(1);
static void gc_gms_mark_pmc_header(PARROT_INTERP, ARGIN(PMC *pmc))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
static void gc_gms_mark_str_header(PARROT_INTERP, ARGIN_NULLOK(STRING *str))
__attribute__nonnull__(1);
static void gc_gms_maybe_mark_and_sweep(PARROT_INTERP)
__attribute__nonnull__(1);
static void gc_gms_pmc_needs_early_collection(PARROT_INTERP,
ARGMOD(PMC *pmc))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
FUNC_MODIFIES(*pmc);
static void gc_gms_print_stats(PARROT_INTERP, ARGIN(const char* header))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
static void gc_gms_process_dirty_list(PARROT_INTERP,
ARGIN(MarkSweep_GC *self),
ARGIN(Parrot_Pointer_Array *dirty_list))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3);
static void gc_gms_process_work_list(PARROT_INTERP,
ARGIN(MarkSweep_GC *self),
ARGIN(Parrot_Pointer_Array *work_list))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3);
static void gc_gms_reallocate_buffer_storage(PARROT_INTERP,
ARGIN(Buffer *str),
size_t size)
__attribute__nonnull__(1)
__attribute__nonnull__(2);
PARROT_MALLOC
PARROT_CANNOT_RETURN_NULL
static void * gc_gms_reallocate_memory_chunk(SHIM_INTERP,
ARGFREE(void *from),
size_t size);
PARROT_MALLOC
PARROT_CANNOT_RETURN_NULL
static void * gc_gms_reallocate_memory_chunk_zeroed(SHIM_INTERP,
ARGFREE(void *data),
size_t newsize,
size_t oldsize);
static void gc_gms_reallocate_string_storage(PARROT_INTERP,
ARGIN(STRING *str),
size_t size)
__attribute__nonnull__(1)
__attribute__nonnull__(2);
static void gc_gms_seal_object(PARROT_INTERP, ARGIN(PMC *pmc))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
static size_t gc_gms_select_generation_to_collect(PARROT_INTERP)
__attribute__nonnull__(1);
static void gc_gms_sweep_pmc_cb(PARROT_INTERP, ARGIN(PObj *obj))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
static void gc_gms_sweep_pools(PARROT_INTERP, ARGIN(MarkSweep_GC *self))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
static void gc_gms_sweep_string_cb(PARROT_INTERP, ARGIN(PObj *obj))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
static void gc_gms_unblock_GC_mark(PARROT_INTERP)
__attribute__nonnull__(1);
static void gc_gms_unblock_GC_sweep(PARROT_INTERP)
__attribute__nonnull__(1);
static void gc_gms_unseal_object(PARROT_INTERP, ARGIN(PMC *pmc))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
static void gc_gms_validate_objects(PARROT_INTERP)
__attribute__nonnull__(1);
static void gc_gms_validate_pmc(PARROT_INTERP, ARGIN(PMC *pmc))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
static void gc_gms_validate_str(PARROT_INTERP, ARGIN(STRING *str))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
static void gc_gms_write_barrier(PARROT_INTERP, ARGIN(PMC *pmc))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
static int gen2flags(int gen);
static int pobj2gen(ARGIN(PObj *pmc))
__attribute__nonnull__(1);
#define ASSERT_ARGS_failed_allocation __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_gc_gms_allocate_buffer_header __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_allocate_buffer_storage \
__attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(str))
#define ASSERT_ARGS_gc_gms_allocate_fixed_size_storage \
__attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_allocate_memory_chunk __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_gc_gms_allocate_memory_chunk_zeroed \
__attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_gc_gms_allocate_pmc_attributes \
__attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(pmc))
#define ASSERT_ARGS_gc_gms_allocate_pmc_header __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_allocate_string_header __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_allocate_string_storage \
__attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(str))
#define ASSERT_ARGS_gc_gms_block_GC_mark __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_block_GC_sweep __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_check_sanity __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_cleanup_dirty_list __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(self) \
, PARROT_ASSERT_ARG(dirty_list))
#define ASSERT_ARGS_gc_gms_compact_memory_pool __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_count_used_pmc_memory __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(list))
#define ASSERT_ARGS_gc_gms_count_used_string_memory \
__attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(list))
#define ASSERT_ARGS_gc_gms_finalize __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_free_buffer_header __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_free_fixed_size_storage \
__attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(data))
#define ASSERT_ARGS_gc_gms_free_memory_chunk __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_gc_gms_free_pmc_attributes __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(pmc))
#define ASSERT_ARGS_gc_gms_free_pmc_header __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_free_string_header __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_get_gc_info __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_get_youngest_generation \
__attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(pmc))
#define ASSERT_ARGS_gc_gms_is_blocked_GC_mark __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_is_blocked_GC_sweep __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_is_pmc_ptr __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_is_ptr_owned __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(pool) \
, PARROT_ASSERT_ARG(list))
#define ASSERT_ARGS_gc_gms_is_string_ptr __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_iterate_live_strings __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_mark_and_sweep __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_mark_pmc_header __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(pmc))
#define ASSERT_ARGS_gc_gms_mark_str_header __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_maybe_mark_and_sweep __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_pmc_needs_early_collection \
__attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(pmc))
#define ASSERT_ARGS_gc_gms_print_stats __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(header))
#define ASSERT_ARGS_gc_gms_process_dirty_list __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(self) \
, PARROT_ASSERT_ARG(dirty_list))
#define ASSERT_ARGS_gc_gms_process_work_list __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(self) \
, PARROT_ASSERT_ARG(work_list))
#define ASSERT_ARGS_gc_gms_reallocate_buffer_storage \
__attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(str))
#define ASSERT_ARGS_gc_gms_reallocate_memory_chunk \
__attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_gc_gms_reallocate_memory_chunk_zeroed \
__attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_gc_gms_reallocate_string_storage \
__attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(str))
#define ASSERT_ARGS_gc_gms_seal_object __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(pmc))
#define ASSERT_ARGS_gc_gms_select_generation_to_collect \
__attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_sweep_pmc_cb __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(obj))
#define ASSERT_ARGS_gc_gms_sweep_pools __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(self))
#define ASSERT_ARGS_gc_gms_sweep_string_cb __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(obj))
#define ASSERT_ARGS_gc_gms_unblock_GC_mark __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_unblock_GC_sweep __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_unseal_object __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(pmc))
#define ASSERT_ARGS_gc_gms_validate_objects __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_validate_pmc __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(pmc))
#define ASSERT_ARGS_gc_gms_validate_str __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(str))
#define ASSERT_ARGS_gc_gms_write_barrier __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(pmc))
#define ASSERT_ARGS_gen2flags __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_pobj2gen __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(pmc))
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
/* HEADERIZER END: static */
/*
=head1 Functions
=over 4
=item C<static void gc_gms_mark_and_sweep(PARROT_INTERP, UINTVAL flags)>
This function is called from the GC API function C<Parrot_gc_mark_and_sweep>.
Flags can be a combination of these values:
GC_finish_FLAG
GC_lazy_FLAG
GC_trace_stack_FLAG
=cut
*/
/*
=item C<void Parrot_gc_gms_init(PARROT_INTERP, Parrot_GC_Init_Args *args)>
Initializes the generational collector
=cut
*/
void
Parrot_gc_gms_init(PARROT_INTERP, ARGIN(Parrot_GC_Init_Args *args))
{
ASSERT_ARGS(Parrot_gc_gms_init)
struct MarkSweep_GC *self;
/* We have to transfer ownership of memory to parent interp in threaded parrot */
interp->gc_sys->finalize_gc_system = NULL; /* gc_gms_finalize; */
interp->gc_sys->do_gc_mark = gc_gms_mark_and_sweep;
interp->gc_sys->compact_string_pool = gc_gms_compact_memory_pool;
/*
interp->gc_sys->mark_special = gc_gms_mark_special;
*/
interp->gc_sys->pmc_needs_early_collection = gc_gms_pmc_needs_early_collection;
interp->gc_sys->allocate_pmc_header = gc_gms_allocate_pmc_header;
interp->gc_sys->free_pmc_header = gc_gms_free_pmc_header;
interp->gc_sys->allocate_string_header = gc_gms_allocate_string_header;
interp->gc_sys->free_string_header = gc_gms_free_string_header;
interp->gc_sys->allocate_bufferlike_header = gc_gms_allocate_buffer_header;
interp->gc_sys->free_bufferlike_header = gc_gms_free_buffer_header;
interp->gc_sys->allocate_pmc_attributes = gc_gms_allocate_pmc_attributes;
interp->gc_sys->free_pmc_attributes = gc_gms_free_pmc_attributes;
interp->gc_sys->is_pmc_ptr = gc_gms_is_pmc_ptr;
interp->gc_sys->is_string_ptr = gc_gms_is_string_ptr;
interp->gc_sys->mark_pmc_header = gc_gms_mark_pmc_header;
interp->gc_sys->mark_str_header = gc_gms_mark_str_header;
interp->gc_sys->block_mark = gc_gms_block_GC_mark;
interp->gc_sys->unblock_mark = gc_gms_unblock_GC_mark;
interp->gc_sys->is_blocked_mark = gc_gms_is_blocked_GC_mark;
interp->gc_sys->block_sweep = gc_gms_block_GC_sweep;
interp->gc_sys->unblock_sweep = gc_gms_unblock_GC_sweep;
interp->gc_sys->is_blocked_sweep = gc_gms_is_blocked_GC_sweep;
interp->gc_sys->allocate_string_storage = gc_gms_allocate_string_storage;
interp->gc_sys->reallocate_string_storage = gc_gms_reallocate_string_storage;
interp->gc_sys->allocate_buffer_storage = gc_gms_allocate_buffer_storage;
interp->gc_sys->reallocate_buffer_storage = gc_gms_reallocate_buffer_storage;
interp->gc_sys->allocate_fixed_size_storage = gc_gms_allocate_fixed_size_storage;
interp->gc_sys->free_fixed_size_storage = gc_gms_free_fixed_size_storage;
/* We don't distinguish between chunk and chunk_with_pointers */
interp->gc_sys->allocate_memory_chunk = gc_gms_allocate_memory_chunk;
interp->gc_sys->reallocate_memory_chunk = gc_gms_reallocate_memory_chunk;
interp->gc_sys->allocate_memory_chunk_with_interior_pointers
= gc_gms_allocate_memory_chunk_zeroed;
interp->gc_sys->reallocate_memory_chunk_with_interior_pointers
= gc_gms_reallocate_memory_chunk_zeroed;
interp->gc_sys->free_memory_chunk = gc_gms_free_memory_chunk;
interp->gc_sys->iterate_live_strings = gc_gms_iterate_live_strings;
interp->gc_sys->write_barrier = gc_gms_write_barrier;
interp->gc_sys->get_gc_info = gc_gms_get_gc_info;
if (interp->parent_interpreter && interp->parent_interpreter->gc_sys) {
/* This is a "child" interpreter. Just reuse parent one */
self = (MarkSweep_GC*)interp->parent_interpreter->gc_sys->gc_private;
}
else {
size_t i;
self = mem_allocate_zeroed_typed(MarkSweep_GC);
self->pmc_allocator = Parrot_gc_pool_new(interp,
sizeof (pmc_alloc_struct));
self->string_allocator = Parrot_gc_pool_new(interp,
sizeof (string_alloc_struct));
/* Allocate list for gray objects */
self->work_list = NULL;
self->dirty_list = Parrot_pa_new(interp);
for (i = 0; i < MAX_GENERATIONS; i++) {
self->objects[i] = Parrot_pa_new(interp);
self->strings[i] = Parrot_pa_new(interp);
}
self->fixed_size_allocator = Parrot_gc_fixed_allocator_new(interp);
/* Collect every nM allocated. */
/* Hardcode for now. Will be configured via CLI */
//self->gc_threshold = 10 * 1024 * 1024;
self->gc_threshold = Parrot_sysmem_amount(interp) / 100;
Parrot_gc_str_initialize(interp, &self->string_gc);
}
interp->gc_sys->gc_private = self;
}
static void
gc_gms_mark_and_sweep(PARROT_INTERP, UINTVAL flags)
{
ASSERT_ARGS(gc_gms_mark_and_sweep)
MarkSweep_GC *self = (MarkSweep_GC *)interp->gc_sys->gc_private;
Parrot_Pointer_Array *list;
int i, gen = -1;
UNUSED(flags);
/* GC is blocked */
if (self->gc_mark_block_level)
return;
/* Ignore it. Will cleanup in gc_gms_finalize */
if (flags & GC_finish_FLAG)
return;
/* Ignore calls from String GC. We know better when to trigger GC */
if (flags & GC_strings_cb_FLAG)
return;
/* Block further GC calls */
++self->gc_mark_block_level;
self->work_list = Parrot_pa_new(interp);
interp->gc_sys->stats.gc_mark_runs++;
gc_gms_print_stats(interp, "Before");
gc_gms_check_sanity(interp);
/*
2. Choose K - how many collections we want to collect. Collections [0..K] will
be collected. Remember K in C<self->gen_to_collect>.
*/
self->gen_to_collect = gen = gc_gms_select_generation_to_collect(interp);
/*
3. Move all objects from collections younger K from dirty_list
back to original lists. Reason for this is "corollary of invariant". We can
either collect such objects or they will be marked by referents from
"dirty_list".
*/
gc_gms_cleanup_dirty_list(interp, self, self->dirty_list, gen);
gc_gms_print_stats(interp, "After cleanup");
/*
4. Trace root objects. According to "0. Pre-requirements" we will ignore all
"old" objects. All relevant objects are moved into "work_list".
*/
gc_gms_mark_pmc_header(interp, PMCNULL);
Parrot_gc_trace_root(interp, NULL, GC_TRACE_FULL);
if (interp->pdb && interp->pdb->debugger) {
Parrot_gc_trace_root(interp->pdb->debugger, NULL, (Parrot_gc_trace_type)0);
}
gc_gms_print_stats(interp, "After trace_roots");
gc_gms_check_sanity(interp);
/*
5. Iterate over "dirty_set" calling VTABLE_mark on it. It will move all
children into "work_list".
*/
gc_gms_process_dirty_list(interp, self, self->dirty_list);
gc_gms_print_stats(interp, "After dirty_list");
gc_gms_check_sanity(interp);
/*
6. Iterate over "work_list" calling VTABLE_mark on it.
*/
gc_gms_process_work_list(interp, self, self->work_list);
gc_gms_print_stats(interp, "After work_list");
gc_gms_check_sanity(interp);
/*
7. Sweep generations starting from K:
- Destroy all dead objects
- Move live objects into generation max(K+1, N)
- Paint them white.
*/
gc_gms_sweep_pools(interp, self);
gc_gms_check_sanity(interp);
/* Update some stats */
interp->gc_sys->stats.header_allocs_since_last_collect = 0;
interp->gc_sys->stats.mem_used_last_collect = 0;
self->gc_mark_block_level--;
/* We swept all dead objects */
self->num_early_gc_PMCs = 0;
/* Don't compact after nursery collection */
if (gen)
gc_gms_compact_memory_pool(interp);
gc_gms_check_sanity(interp);
gc_gms_print_stats(interp, "After");
Parrot_pa_destroy(interp, self->work_list);
self->work_list = NULL;
gc_gms_validate_objects(interp);
}
/*
=item C<static size_t gc_gms_select_generation_to_collect(PARROT_INTERP)>
Select how many generations we do want to collect.
=cut
*/
static size_t
gc_gms_select_generation_to_collect(PARROT_INTERP)
{
ASSERT_ARGS(gc_gms_select_generation_to_collect)
/* TODO Use less naive approach. E.g. count amount of allocated memory in
* older generations */
size_t runs = interp->gc_sys->stats.gc_mark_runs;
#if 0
if (runs % 100000000 == 0)
return 8;
if (runs % 10000000 == 0)
return 7;
if (runs % 1000000 == 0)
return 6;
if (runs % 100000 == 0)
return 5;
if (runs % 10000 == 0)
return 4;
if (runs % 1000 == 0)
return 3;
if (runs % 100 == 0)
return 2;
#endif
if (runs % 10 == 0)
return 1;
return 0;
}
/*
=item C<static void gc_gms_cleanup_dirty_list(PARROT_INTERP, MarkSweep_GC *self,
Parrot_Pointer_Array *dirty_list, size_t gen)>
Move all objects from collections younger K from dirty_list
back to original lists. Reason for this is "corollary of invariant". We can
either collect such objects or they will be marked by referents from
"dirty_list".
=cut
*/
static void
gc_gms_cleanup_dirty_list(PARROT_INTERP,
ARGIN(MarkSweep_GC *self),
ARGIN(Parrot_Pointer_Array *dirty_list),
size_t gen)
{
ASSERT_ARGS(gc_gms_cleanup_dirty_list)
/* Override with special version of mark */
interp->gc_sys->mark_pmc_header = gc_gms_get_youngest_generation;
POINTER_ARRAY_ITER(dirty_list,
pmc_alloc_struct *item = (pmc_alloc_struct *)ptr;
PMC *pmc = &(item->pmc);
size_t gen = POBJ2GEN(pmc);
self->youngest_child = gen;
if (PObj_custom_mark_TEST(pmc))
VTABLE_mark(interp, pmc);
if (PMC_metadata(pmc) && self->youngest_child > POBJ2GEN(PMC_metadata(pmc)))
self->youngest_child = POBJ2GEN(PMC_metadata(pmc));
/* All children aren't younger than us - get rid of it */
if (self->youngest_child >= gen) {
PObj_live_CLEAR(pmc);
PObj_GC_on_dirty_list_CLEAR(pmc);
Parrot_pa_remove(interp, dirty_list, item->ptr);
item->ptr = Parrot_pa_insert(interp, self->objects[gen], item);
gc_gms_seal_object(interp, pmc);
});
interp->gc_sys->mark_pmc_header = gc_gms_mark_pmc_header;
}
/*
=item C<static void gc_gms_process_dirty_list(PARROT_INTERP, MarkSweep_GC *self,
Parrot_Pointer_Array *dirty_list)>
Iterate over "dirty_set" calling VTABLE_mark on it. It will move all
children into "work_list".
=cut
*/
static void
gc_gms_process_dirty_list(PARROT_INTERP,
ARGIN(MarkSweep_GC *self),
ARGIN(Parrot_Pointer_Array *dirty_list))
{
ASSERT_ARGS(gc_gms_process_dirty_list)
POINTER_ARRAY_ITER(dirty_list,
PMC *pmc = &((pmc_alloc_struct *)ptr)->pmc;
if (PObj_custom_mark_TEST(pmc))
VTABLE_mark(interp, pmc);
if (PMC_metadata(pmc))
Parrot_gc_mark_PMC_alive(interp, PMC_metadata(pmc)););
}
/*
=item C<static void gc_gms_process_work_list(PARROT_INTERP, MarkSweep_GC *self,
Parrot_Pointer_Array *work_list)>
Process work list moving objects back to own generation
=cut
*/
static void
gc_gms_process_work_list(PARROT_INTERP,
ARGIN(MarkSweep_GC *self),
ARGIN(Parrot_Pointer_Array *work_list))
{
ASSERT_ARGS(gc_gms_process_work_list)
POINTER_ARRAY_ITER(work_list,
PMC *pmc = &((pmc_alloc_struct *)ptr)->pmc;
if (PObj_custom_mark_TEST(pmc))
VTABLE_mark(interp, pmc);
if (PMC_metadata(pmc))
Parrot_gc_mark_PMC_alive(interp, PMC_metadata(pmc)););
gc_gms_print_stats(interp, "Before cleaning work_list");
/* Move processed objects back to own generation */
POINTER_ARRAY_ITER(work_list,
pmc_alloc_struct *item = (pmc_alloc_struct *)ptr;