-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy patherl_gc.c
4230 lines (3645 loc) · 119 KB
/
erl_gc.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
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 2002-2025. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* %CopyrightEnd%
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#define ERL_WANT_GC_INTERNALS__
#include "sys.h"
#include "erl_vm.h"
#include "global.h"
#include "erl_process.h"
#include "erl_db.h"
#include "beam_catches.h"
#include "erl_binary.h"
#include "erl_bits.h"
#include "erl_map.h"
#include "error.h"
#include "big.h"
#include "erl_gc.h"
#include "dtrace-wrapper.h"
#include "erl_bif_unique.h"
#include "dist.h"
#include "erl_nfunc_sched.h"
#include "erl_proc_sig_queue.h"
#include "beam_common.h"
#include "beam_bp.h"
#define ERTS_INACT_WR_PB_LEAVE_MUCH_LIMIT 1
#define ERTS_INACT_WR_PB_LEAVE_MUCH_PERCENTAGE 20
#define ERTS_INACT_WR_PB_LEAVE_LIMIT 10
#define ERTS_INACT_WR_PB_LEAVE_PERCENTAGE 10
#define ERTS_LONG_GC_MAX_NMSIGS 1000
#if defined(DEBUG) || 0
#define ERTS_GC_DEBUG
#else
#undef ERTS_GC_DEBUG
#endif
#ifdef ERTS_GC_DEBUG
# define ERTS_GC_ASSERT ASSERT
#else
# define ERTS_GC_ASSERT(B) ((void) 1)
#endif
#if defined(DEBUG) && 0
# define HARDDEBUG 1
#endif
/*
* Returns number of elements in an array.
*/
#define ALENGTH(a) (sizeof(a)/sizeof(a[0]))
/* Actual stack usage, note that this may include words in the redzone. */
# define STACK_SZ_ON_HEAP(p) (STACK_START(p) - STACK_TOP(p))
# define OverRunCheck(P) \
if ((P)->stop < (P)->htop) { \
erts_fprintf(stderr, "hend=%p\n", (P)->hend); \
erts_fprintf(stderr, "stop=%p\n", (P)->stop); \
erts_fprintf(stderr, "htop=%p\n", (P)->htop); \
erts_fprintf(stderr, "heap=%p\n", (P)->heap); \
erts_exit(ERTS_ABORT_EXIT, "%s, line %d: %T: Overrun stack and heap\n", \
__FILE__,__LINE__,(P)->common.id); \
}
#ifdef DEBUG
#define ErtsGcQuickSanityCheck(P) \
do { \
ASSERT((P)->heap < (P)->hend); \
ASSERT((p)->abandoned_heap || (P)->heap_sz == (P)->hend - (P)->heap); \
ASSERT((P)->heap <= (P)->htop && (P)->htop <= (P)->hend); \
ASSERT((P)->heap <= (P)->stop && (P)->stop <= (P)->hend); \
ASSERT(((P)->heap <= (P)->high_water && (P)->high_water <= (P)->hend)); \
OverRunCheck((P)); \
} while (0)
#else
#define ErtsGcQuickSanityCheck(P) \
do { \
OverRunCheck((P)); \
} while (0)
#endif
/*
* This structure describes the rootset for the GC.
*/
typedef struct roots {
Eterm* v; /* Pointers to vectors with terms to GC
* (e.g. the stack).
*/
Uint sz; /* Size of each vector. */
} Roots;
typedef struct {
Roots def[32]; /* Default storage. */
Roots* roots; /* Pointer to root set array. */
Uint size; /* Storage size. */
int num_roots; /* Number of root arrays. */
} Rootset;
static void copy_erlang_stack(Process *p, Eterm *new_heap, SWord new_sz);
static Uint setup_rootset(Process*, Eterm*, int, Rootset*);
static void cleanup_rootset(Rootset *rootset);
static Eterm *full_sweep_heaps(Process *p,
ErlHeapFragment *live_hf_end,
int hibernate,
Eterm *n_heap, Eterm* n_htop,
char *oh, Uint oh_size,
Eterm *objv, int nobj);
static int garbage_collect(Process* p, ErlHeapFragment *live_hf_end,
Uint need, Eterm* objv, int nobj, int fcalls,
Uint max_young_gen_usage);
static int major_collection(Process* p, ErlHeapFragment *live_hf_end,
Uint need, Eterm* objv, int nobj,
Uint ygen_usage, Uint *recl);
static int minor_collection(Process* p, ErlHeapFragment *live_hf_end,
Uint need, Eterm* objv, int nobj,
Uint ygen_usage, Uint *recl);
static void do_minor(Process *p, ErlHeapFragment *live_hf_end,
char *mature, Uint mature_size,
Uint new_sz, Eterm* objv, int nobj);
static Eterm *sweep_new_heap(Eterm *n_hp, Eterm *n_htop,
char* old_heap, Uint old_heap_size);
static Eterm *sweep_heaps(Eterm *n_hp, Eterm *n_htop,
char* old_heap, Uint old_heap_size);
static Eterm* sweep_literal_area(Eterm* n_hp, Eterm* n_htop,
char* old_heap, Uint old_heap_size,
char* src, Uint src_size);
static Eterm* sweep_literals_to_old_heap(Eterm* heap_ptr, Eterm* heap_end, Eterm* htop,
char* src, Uint src_size);
static Eterm* collect_live_heap_frags(Process* p, ErlHeapFragment *live_hf_end,
Eterm* htop);
static int adjust_after_fullsweep(Process *p, int need, Eterm *objv, int nobj);
static void shrink_new_heap(Process *p, Uint new_sz, Eterm *objv, int nobj);
static void grow_new_heap(Process *p, Uint new_sz, Eterm* objv, int nobj);
static void sweep_off_heap(Process *p, int fullsweep);
static void offset_heap(Eterm* hp, Uint sz, Sint offs, char* area, Uint area_sz);
static void offset_stack(Eterm *stack, Uint sz,
Sint heap_offset, Sint stack_offset,
char* area, Uint area_sz);
static void offset_heap_ptr(Eterm* hp, Uint sz, Sint offs, char* area, Uint area_sz);
static void offset_rootset(Process *p, Sint heap_offs, Sint stack_offs,
char* area, Uint area_sz, Eterm* objv, int nobj);
static void offset_off_heap(Process* p, Sint offs, char* area, Uint area_sz);
static void offset_mqueue(Process *p, Sint offs, char* area, Uint area_sz);
static int has_reached_max_heap_size(Process *p, Uint total_heap_size);
static int reached_max_heap_size(Process *p, Uint total_heap_size,
Uint extra_heap_size, Uint extra_old_heap_size);
static void init_gc_info(ErtsGCInfo *gcip);
static Uint64 next_vheap_size(Process* p, Uint64 vheap, Uint64 vheap_sz);
#ifdef HARDDEBUG
static void disallow_heap_frag_ref_in_heap(Process *p, Eterm *heap, Eterm *htop);
static void disallow_heap_frag_ref_in_old_heap(Process* p);
#endif
#if defined(ARCH_64)
# define MAX_HEAP_SIZES 154
#else
# define MAX_HEAP_SIZES 59
#endif
static Sint heap_sizes[MAX_HEAP_SIZES]; /* Suitable heap sizes. */
static int num_heap_sizes; /* Number of heap sizes. */
Uint erts_test_long_gc_sleep; /* Only used for testing... */
typedef struct {
Process *proc;
Eterm ref;
Eterm ref_heap[ERTS_REF_THING_SIZE];
Uint req_sched;
erts_atomic32_t refc;
} ErtsGCInfoReq;
static struct {
erts_mtx_t mtx;
ErtsGCInfo info;
} dirty_gc;
static void move_msgs_to_heap(Process *c_p)
{
Uint64 pre_oh, post_oh;
pre_oh = c_p->off_heap.overhead;
erts_proc_sig_move_msgs_to_heap(c_p);
post_oh = c_p->off_heap.overhead;
if (pre_oh != post_oh) {
/* Got new binaries; update bin vheap size... */
c_p->bin_vheap_sz = next_vheap_size(c_p, post_oh,
c_p->bin_vheap_sz);
}
}
static ERTS_INLINE int
gc_cost(Uint gc_moved_live_words, Uint resize_moved_words)
{
Sint reds;
reds = gc_moved_live_words/10;
reds += resize_moved_words/100;
if (reds < 1)
return 1;
if (reds > INT_MAX)
return INT_MAX;
return (int) reds;
}
ERTS_SCHED_PREF_QUICK_ALLOC_IMPL(gcireq,
ErtsGCInfoReq,
5,
ERTS_ALC_T_GC_INFO_REQ)
/*
* Initialize GC global data.
*/
void
erts_init_gc(void)
{
int i = 0, ix;
Sint max_heap_size = 0;
ERTS_CT_ASSERT(offsetof(BinRef,thing_word) == offsetof(struct erl_off_heap_header,thing_word));
ERTS_CT_ASSERT(offsetof(BinRef,thing_word) == offsetof(ErlFunThing,thing_word));
ERTS_CT_ASSERT(offsetof(BinRef,thing_word) == offsetof(ExternalThing,header));
ERTS_CT_ASSERT(offsetof(BinRef,next) == offsetof(struct erl_off_heap_header,next));
ERTS_CT_ASSERT(offsetof(BinRef,next) == offsetof(FunRef,next));
ERTS_CT_ASSERT(offsetof(BinRef,next) == offsetof(ExternalThing,next));
erts_test_long_gc_sleep = 0;
/*
* Heap sizes start growing in a Fibonacci sequence.
*
* Fib growth is not really ok for really large heaps, for
* example is fib(35) == 14meg, whereas fib(36) == 24meg;
* we really don't want that growth when the heaps are that big.
*/
/* Growth stage 1 - Fibonacci + 1*/
/* 12,38 will hit size 233, the old default */
heap_sizes[0] = 12;
heap_sizes[1] = 38;
for(i = 2; i < 23; i++) {
/* one extra word for block header */
heap_sizes[i] = heap_sizes[i-1] + heap_sizes[i-2] + 1;
}
/* for 32 bit we want max_heap_size to be MAX(32bit) / 4 [words]
* for 64 bit we want max_heap_size to be MAX(52bit) / 8 [words]
*/
max_heap_size = sizeof(Eterm) < 8 ? (Sint)((~(Uint)0)/(sizeof(Eterm))) :
(Sint)(((Uint64)1 << 53)/sizeof(Eterm));
/* Growth stage 2 - 20% growth */
/* At 1.3 mega words heap, we start to slow down. */
for (i = 23; i < ALENGTH(heap_sizes); i++) {
heap_sizes[i] = heap_sizes[i-1] + heap_sizes[i-1]/5;
if ((heap_sizes[i] < 0) || heap_sizes[i] > max_heap_size) {
/* Size turned negative. Discard this last size. */
i--;
break;
}
}
num_heap_sizes = i;
for (ix = 0; ix < erts_no_schedulers; ix++) {
ErtsSchedulerData *esdp = ERTS_SCHEDULER_IX(ix);
init_gc_info(&esdp->gc_info);
}
erts_mtx_init(&dirty_gc.mtx, "dirty_gc_info", NIL,
ERTS_LOCK_FLAGS_PROPERTY_STATIC | ERTS_LOCK_FLAGS_CATEGORY_GENERIC);
init_gc_info(&dirty_gc.info);
init_gcireq_alloc();
}
/*
* Find the next heap size equal to or greater than the given size (if offset == 0).
*
* If offset is 1, the next higher heap size is returned (always greater than size).
*/
Uint
erts_next_heap_size(Uint size, Uint offset)
{
if (size < heap_sizes[0]) {
return heap_sizes[0];
} else {
Sint* low = heap_sizes;
Sint* high = heap_sizes + num_heap_sizes;
Sint* mid;
while (low < high) {
mid = low + (high-low) / 2;
if (size < mid[0]) {
high = mid;
} else if (size == mid[0]) {
ASSERT(mid+offset-heap_sizes < num_heap_sizes);
return mid[offset];
} else if (size < mid[1]) {
ASSERT(mid[0] < size && size <= mid[1]);
ASSERT(mid+offset-heap_sizes < num_heap_sizes);
return mid[offset+1];
} else {
low = mid + 1;
}
}
erts_exit(ERTS_ERROR_EXIT, "no next heap size found: %beu, offset %beu\n", size, offset);
}
return 0;
}
/*
* Return the next heap size to use. Make sure we never return
* a smaller heap size than the minimum heap size for the process.
* (Use of the erlang:hibernate/3 BIF could have shrunk the
* heap below the minimum heap size.)
*/
static Uint
next_heap_size(Process* p, Uint size, Uint offset)
{
size = erts_next_heap_size(size, offset);
return size < p->min_heap_size ? p->min_heap_size : size;
}
Eterm
erts_heap_sizes(Process* p)
{
int i;
int n = 0;
int big = 0;
Eterm res = NIL;
Eterm* hp;
Eterm* bigp;
for (i = num_heap_sizes-1; i >= 0; i--) {
n += 2;
if (!IS_SSMALL(heap_sizes[i])) {
big += BIG_UINT_HEAP_SIZE;
}
}
/*
* We store all big numbers first on the heap, followed
* by all the cons cells.
*/
bigp = HAlloc(p, n+big);
hp = bigp+big;
for (i = num_heap_sizes-1; i >= 0; i--) {
Eterm num;
Sint sz = heap_sizes[i];
if (IS_SSMALL(sz)) {
num = make_small(sz);
} else {
num = uint_to_big(sz, bigp);
bigp += BIG_UINT_HEAP_SIZE;
}
res = CONS(hp, num, res);
hp += 2;
}
return res;
}
void
erts_offset_heap(Eterm* hp, Uint sz, Sint offs, Eterm* low, Eterm* high)
{
offset_heap(hp, sz, offs, (char*) low, ((char *)high)-((char *)low));
}
void
erts_offset_heap_ptr(Eterm* hp, Uint sz, Sint offs,
Eterm* low, Eterm* high)
{
offset_heap_ptr(hp, sz, offs, (char *) low, ((char *)high)-((char *)low));
}
#define ptr_within(ptr, low, high) ((ptr) < (high) && (ptr) >= (low))
void
erts_offset_off_heap(ErlOffHeap *ohp, Sint offs, Eterm* low, Eterm* high)
{
if (ohp->first && ptr_within((Eterm *)ohp->first, low, high)) {
Eterm** uptr = (Eterm**) (void *) &ohp->first;
*uptr += offs;
}
}
#undef ptr_within
Eterm
erts_gc_after_bif_call_lhf(Process* p, ErlHeapFragment *live_hf_end,
Eterm result, Eterm* regs, Uint arity)
{
int cost;
if (!p->mbuf) {
/* Must have GC:d in BIF call... invalidate live_hf_end */
live_hf_end = ERTS_INVALID_HFRAG_PTR;
}
if (p->flags & (F_HIBERNATE_SCHED | F_DISABLE_GC)) {
if ((p->flags & F_DISABLE_GC)
&& p->live_hf_end == ERTS_INVALID_HFRAG_PTR
&& is_non_value(result)
&& p->freason == TRAP) {
/* This is first trap with disabled GC. Save live_hf_end marker. */
p->live_hf_end = live_hf_end;
}
/*else:
* a subsequent trap with disabled GC
*
* OR
*
* We just hibernated. We do *not* want to mess
* up the hibernation by an ordinary GC...
*/
return result;
}
if (p->sig_qs.flags & (FS_ON_HEAP_MSGQ|FS_OFF_HEAP_MSGQ_CHNG)) {
erts_proc_lock(p, ERTS_PROC_LOCK_MSGQ);
erts_proc_sig_fetch(p);
erts_proc_unlock(p, ERTS_PROC_LOCK_MSGQ);
}
if (is_non_value(result)) {
if (p->freason == TRAP) {
cost = garbage_collect(p, live_hf_end, 0, regs, p->arity, p->fcalls, 0);
} else {
cost = garbage_collect(p, live_hf_end, 0, regs, arity, p->fcalls, 0);
}
} else {
Eterm val[1];
val[0] = result;
cost = garbage_collect(p, live_hf_end, 0, val, 1, p->fcalls, 0);
if (ERTS_PROC_IS_EXITING(p)) {
result = THE_NON_VALUE;
}
else {
result = val[0];
}
}
BUMP_REDS(p, cost);
return result;
}
Eterm
erts_gc_after_bif_call(Process* p, Eterm result, Eterm* regs, Uint arity)
{
return erts_gc_after_bif_call_lhf(p, ERTS_INVALID_HFRAG_PTR,
result, regs, arity);
}
static ERTS_INLINE void assert_no_active_writers(Process *p)
{
#ifdef DEBUG
BinRef *br = (BinRef*)p->wrt_bins;
while (br) {
ASSERT(br->thing_word == HEADER_BIN_REF);
ERTS_ASSERT(!((br->val)->intern.flags & BIN_FLAG_ACTIVE_WRITER));
br = (BinRef*)br->next;
}
#endif
}
#define ERTS_DELAY_GC_EXTRA_FREE 40
#define ERTS_ABANDON_HEAP_COST 10
static int
delay_garbage_collection(Process *p, int need, int fcalls)
{
ErlHeapFragment *hfrag;
Eterm *orig_heap, *orig_hend, *orig_htop, *orig_stop;
Eterm *hend;
Uint hsz, ssz;
int reds_left;
ERTS_HOLE_CHECK(p);
if (need == 0) {
if (p->flags & (F_DIRTY_MAJOR_GC|F_DIRTY_MINOR_GC)) {
ASSERT(!ERTS_SCHEDULER_IS_DIRTY(erts_proc_sched_data(p)));
goto force_reschedule;
}
return 1;
}
/*
* Satisfy need in a heap fragment...
*/
ASSERT(need > 0);
orig_heap = p->heap;
orig_hend = p->hend;
orig_htop = p->htop;
orig_stop = p->stop;
ssz = orig_hend - orig_stop;
hsz = ssz + need + ERTS_DELAY_GC_EXTRA_FREE + S_RESERVED;
/* Allocate one extra word at the end to save the high water mark. */
hfrag = new_message_buffer(hsz + 1);
copy_erlang_stack(p, &hfrag->mem[0], hsz);
p->heap = p->htop = &hfrag->mem[0];
hend = &hfrag->mem[hsz];
/* Save the original high water mark at the end of the current
* heap to make it possible to do a minor GC later. */
if (p->abandoned_heap) {
*hend = (Eterm) (p->hend[0]);
} else {
*hend = (Eterm) p->high_water;
}
p->hend = hend;
if (p->abandoned_heap) {
/*
* Active heap already in a fragment; adjust it and
* save it into mbuf list...
*/
ErlHeapFragment *hfrag = ((ErlHeapFragment *)
(((char *) orig_heap)
- offsetof(ErlHeapFragment, mem)));
Uint used = orig_htop - orig_heap;
hfrag->used_size = used;
p->mbuf_sz += used;
ASSERT(hfrag->used_size <= hfrag->alloc_size-1);
ASSERT(!hfrag->off_heap.first && !hfrag->off_heap.overhead);
hfrag->next = p->mbuf;
p->mbuf = hfrag;
}
else {
/* Do not leave a hole in the abandoned heap... */
if (orig_htop < orig_hend) {
erts_write_heap_filler(orig_htop, orig_hend-orig_htop);
if (orig_htop + 1 < orig_hend) {
orig_hend[-1] = (Uint) (orig_htop - orig_heap);
p->flags |= F_ABANDONED_HEAP_USE;
}
}
p->abandoned_heap = orig_heap;
erts_adjust_memory_break(p, orig_htop - p->high_water);
}
/* Keep the high water mark pointing into the current heap to ensure
* that the test for the safe range in the update_record_in_place (JIT)
* stays honest. */
p->high_water = p->heap;
#ifdef CHECK_FOR_HOLES
p->last_htop = p->htop;
p->heap_hfrag = hfrag;
#endif
force_reschedule:
/* Make sure that we do a proper GC as soon as possible... */
p->flags |= F_FORCE_GC;
reds_left = ERTS_REDS_LEFT(p, fcalls);
ASSERT(CONTEXT_REDS - reds_left >= erts_proc_sched_data(p)->virtual_reds);
if (reds_left > ERTS_ABANDON_HEAP_COST) {
int vreds = reds_left - ERTS_ABANDON_HEAP_COST;
erts_proc_sched_data((p))->virtual_reds += vreds;
}
ERTS_CHK_MBUF_SZ(p);
ASSERT(CONTEXT_REDS >= erts_proc_sched_data(p)->virtual_reds);
return reds_left;
}
static ERTS_FORCE_INLINE Uint
young_gen_usage(Process *p, Uint *ext_msg_usage)
{
Uint hsz;
Eterm *aheap;
ERTS_CHK_MBUF_SZ(p);
hsz = p->mbuf_sz;
if (p->sig_qs.flags & FS_ON_HEAP_MSGQ) {
ERTS_FOREACH_SIG_PRIVQS(
p, mp,
{
/*
* We leave not yet decoded distribution messages
* as they are in the queue since it is not
* possible to determine a maximum size until
* actual decoding. However, we use their estimated
* size when calculating need, and by this making
* it more likely that they will fit on the heap
* when actually decoded.
*
* We however ignore off heap messages...
*/
if (ERTS_SIG_IS_MSG(mp)
&& mp->data.attached
&& mp->data.attached != ERTS_MSG_COMBINED_HFRAG) {
Uint sz = erts_msg_attached_data_size(mp);
if (ERTS_SIG_IS_EXTERNAL_MSG(mp))
*ext_msg_usage += sz;
hsz += sz;
}
});
}
hsz += p->htop - p->heap;
aheap = p->abandoned_heap;
if (aheap) {
/* used in orig heap */
if (p->flags & F_ABANDONED_HEAP_USE)
hsz += aheap[p->heap_sz-1];
else
hsz += p->heap_sz;
}
return hsz;
}
static Eterm*
get_orig_heap(Process *p, Eterm **p_htop, Eterm **p_high_water) {
Eterm *aheap = p->abandoned_heap;
Eterm *htop;
/* See delay_garbage_collection(). */
ASSERT(aheap != NULL);
if (p->flags & F_ABANDONED_HEAP_USE) {
htop = aheap + aheap[p->heap_sz-1];
} else {
htop = aheap + p->heap_sz;
}
*p_htop = htop;
if (p_high_water) {
Eterm *high_water;
high_water = (Eterm *)(p->hend[0]);
ASSERT(aheap <= high_water);
ASSERT(high_water <= htop);
/* The high water pointer must be aligned to a word boundary. */
ASSERT(((UWord) high_water) % sizeof(UWord) == 0);
*p_high_water = high_water;
}
return aheap;
}
static ERTS_INLINE void
check_for_possibly_long_gc(Process *p, Uint ygen_usage)
{
int major;
Sint sz;
major = (p->flags & F_NEED_FULLSWEEP) || GEN_GCS(p) >= MAX_GEN_GCS(p);
sz = ygen_usage;
sz += p->hend - p->stop;
if (major)
sz += p->old_htop - p->old_heap;
if (p->sig_qs.flags & FS_ON_HEAP_MSGQ) {
Sint len, max_len = ERTS_POTENTIALLY_LONG_GC_HSIZE - sz;
if (max_len < 0)
len = -1;
else
len = erts_proc_sig_privqs_len(p, max_len, ERTS_LONG_GC_MAX_NMSIGS);
if (len < 0)
sz = ERTS_POTENTIALLY_LONG_GC_HSIZE;
else
sz += (Uint) len;
}
if (sz >= ERTS_POTENTIALLY_LONG_GC_HSIZE) {
ASSERT(!(p->flags & (F_DISABLE_GC|F_DELAY_GC)));
p->flags |= major ? F_DIRTY_MAJOR_GC : F_DIRTY_MINOR_GC;
erts_schedule_dirty_sys_execution(p);
}
}
/*
* Garbage collect a process.
*
* p: Pointer to the process structure.
* need: Number of Eterm words needed on the heap.
* objv: Array of terms to add to rootset; that is to preserve.
* nobj: Number of objects in objv.
*/
static int
garbage_collect(Process* p, ErlHeapFragment *live_hf_end,
Uint need, Eterm* objv, int nobj, int fcalls,
Uint max_young_gen_usage)
{
Uint reclaimed_now = 0;
Uint ygen_usage;
Uint ext_msg_usage = 0;
Eterm gc_trace_end_tag;
int reds;
ErtsMonotonicTime start_time;
ErtsSchedulerData *esdp = erts_proc_sched_data(p);
erts_aint32_t state;
#ifdef USE_VM_PROBES
DTRACE_CHARBUF(pidbuf, DTRACE_TERM_BUF_SIZE);
#endif
ERTS_MSACC_PUSH_STATE();
ERTS_UNDEF(start_time, 0);
ERTS_CHK_MBUF_SZ(p);
ASSERT(CONTEXT_REDS - ERTS_REDS_LEFT(p, fcalls) >= esdp->virtual_reds);
state = erts_atomic32_read_nob(&p->state);
if ((p->flags & (F_DISABLE_GC|F_DELAY_GC)) || state & ERTS_PSFLG_EXITING) {
delay_gc_before_start:
return delay_garbage_collection(p, need, fcalls);
}
ygen_usage = max_young_gen_usage ? max_young_gen_usage : young_gen_usage(p, &ext_msg_usage);
if (!ERTS_SCHEDULER_IS_DIRTY(esdp)) {
check_for_possibly_long_gc(p, ygen_usage);
if (p->flags & (F_DIRTY_MAJOR_GC|F_DIRTY_MINOR_GC))
goto delay_gc_before_start;
}
if (p->abandoned_heap)
live_hf_end = ERTS_INVALID_HFRAG_PTR;
else if (p->live_hf_end != ERTS_INVALID_HFRAG_PTR)
live_hf_end = p->live_hf_end;
ERTS_MSACC_SET_STATE_CACHED(ERTS_MSACC_STATE_GC);
erts_atomic32_read_bor_nob(&p->state, ERTS_PSFLG_GC);
if (erts_system_monitor_long_gc != 0)
start_time = erts_get_monotonic_time(esdp);
ERTS_CHK_OFFHEAP(p);
ErtsGcQuickSanityCheck(p);
#ifdef DEBUG
erts_dbg_check_no_empty_boxed_non_literal_on_heap(p, NULL);
#endif
#ifdef USE_VM_PROBES
*pidbuf = '\0';
if (DTRACE_ENABLED(gc_major_start)
|| DTRACE_ENABLED(gc_major_end)
|| DTRACE_ENABLED(gc_minor_start)
|| DTRACE_ENABLED(gc_minor_end)) {
dtrace_proc_str(p, pidbuf);
}
#endif
if (p->abandoned_heap)
erts_adjust_memory_break(p, p->htop - p->heap + p->mbuf_sz);
else
erts_adjust_memory_break(p, p->htop - p->high_water + p->mbuf_sz);
/*
* Test which type of GC to do.
*/
if (GEN_GCS(p) < MAX_GEN_GCS(p) && !(FLAGS(p) & F_NEED_FULLSWEEP)) {
if (ERTS_IS_P_TRACED_FL(p, F_TRACE_GC)) {
trace_gc(p, am_gc_minor_start, need, THE_NON_VALUE);
}
DTRACE2(gc_minor_start, pidbuf, need);
reds = minor_collection(p, live_hf_end, need + ext_msg_usage, objv, nobj,
ygen_usage, &reclaimed_now);
DTRACE2(gc_minor_end, pidbuf, reclaimed_now);
if (reds == -1) {
if (ERTS_IS_P_TRACED_FL(p, F_TRACE_GC)) {
trace_gc(p, am_gc_minor_end, reclaimed_now, THE_NON_VALUE);
}
if (!ERTS_SCHEDULER_IS_DIRTY(esdp)) {
p->flags |= F_NEED_FULLSWEEP;
check_for_possibly_long_gc(p, ygen_usage);
if (p->flags & F_DIRTY_MAJOR_GC)
goto delay_gc_after_start;
}
goto do_major_collection;
}
if (ERTS_SCHEDULER_IS_DIRTY(esdp))
p->flags &= ~F_DIRTY_MINOR_GC;
gc_trace_end_tag = am_gc_minor_end;
} else {
do_major_collection:
ERTS_MSACC_SET_STATE_CACHED_X(ERTS_MSACC_STATE_GC_FULL);
if (ERTS_IS_P_TRACED_FL(p, F_TRACE_GC)) {
trace_gc(p, am_gc_major_start, need, THE_NON_VALUE);
}
DTRACE2(gc_major_start, pidbuf, need);
reds = major_collection(p, live_hf_end, need + ext_msg_usage, objv, nobj,
ygen_usage, &reclaimed_now);
if (ERTS_SCHEDULER_IS_DIRTY(esdp))
p->flags &= ~(F_DIRTY_MAJOR_GC|F_DIRTY_MINOR_GC);
DTRACE2(gc_major_end, pidbuf, reclaimed_now);
gc_trace_end_tag = am_gc_major_end;
ERTS_MSACC_SET_STATE_CACHED_X(ERTS_MSACC_STATE_GC);
}
/* Max heap size has been reached and the process was configured
to be killed, so we kill it and set it in a delayed garbage
collecting state. There should be no gc_end trace or
long_gc/large_gc triggers when this happens as process was
killed before a GC could be done. */
if (reds == -2) {
int res;
erts_set_self_exiting(p, am_killed);
delay_gc_after_start:
/* erts_send_exit_signal looks for ERTS_PSFLG_GC, so
we have to remove it after the signal is sent */
erts_atomic32_read_band_nob(&p->state, ~ERTS_PSFLG_GC);
/* We have to make sure that we have space for need on the heap */
res = delay_garbage_collection(p, need, fcalls);
ERTS_MSACC_POP_STATE();
return res;
}
/*
* Finish.
*/
assert_no_active_writers(p);
ERTS_CHK_OFFHEAP(p);
ErtsGcQuickSanityCheck(p);
erts_atomic32_read_band_nob(&p->state, ~ERTS_PSFLG_GC);
if (ERTS_IS_P_TRACED_FL(p, F_TRACE_GC)) {
trace_gc(p, gc_trace_end_tag, reclaimed_now, THE_NON_VALUE);
}
if (erts_system_monitor_long_gc != 0) {
ErtsMonotonicTime end_time;
Uint gc_time;
if (erts_test_long_gc_sleep)
while (0 != erts_milli_sleep(erts_test_long_gc_sleep));
end_time = erts_get_monotonic_time(esdp);
gc_time = (Uint) ERTS_MONOTONIC_TO_MSEC(end_time - start_time);
if (gc_time && gc_time > erts_system_monitor_long_gc) {
monitor_long_gc(p, gc_time);
}
}
if (erts_system_monitor_large_heap != 0) {
Uint size = HEAP_SIZE(p);
size += OLD_HEAP(p) ? OLD_HEND(p) - OLD_HEAP(p) : 0;
if (size >= erts_system_monitor_large_heap)
monitor_large_heap(p);
}
if (ERTS_SCHEDULER_IS_DIRTY(esdp)) {
erts_mtx_lock(&dirty_gc.mtx);
dirty_gc.info.garbage_cols++;
dirty_gc.info.reclaimed += reclaimed_now;
erts_mtx_unlock(&dirty_gc.mtx);
}
else
{
esdp->gc_info.garbage_cols++;
esdp->gc_info.reclaimed += reclaimed_now;
}
FLAGS(p) &= ~(F_FORCE_GC|F_HIBERNATED);
p->live_hf_end = ERTS_INVALID_HFRAG_PTR;
ERTS_MSACC_POP_STATE();
#ifdef CHECK_FOR_HOLES
/*
* We intentionally do not rescan the areas copied by the GC.
* We trust the GC not to leave any holes.
*/
p->last_htop = p->htop;
p->last_mbuf = 0;
#endif
#ifdef DEBUG
/*
* The scanning for pointers from the old_heap into the new_heap or
* heap fragments turned out to be costly, so we remember how far we
* have scanned this time and will start scanning there next time.
* (We will not detect wild writes into the old heap, or modifications
* of the old heap in-between garbage collections.)
*/
p->last_old_htop = p->old_htop;
#endif
ASSERT(!p->mbuf);
ASSERT(!ERTS_IS_GC_DESIRED(p));
ASSERT(need <= HEAP_LIMIT(p) - HEAP_TOP(p));
return reds;
}
int
erts_garbage_collect_nobump(Process* p, Uint need, Eterm* objv, int nobj, int fcalls)
{
int reds, reds_left;
if (p->sig_qs.flags & (FS_ON_HEAP_MSGQ|FS_OFF_HEAP_MSGQ_CHNG)) {
erts_proc_lock(p, ERTS_PROC_LOCK_MSGQ);
erts_proc_sig_fetch(p);
erts_proc_unlock(p, ERTS_PROC_LOCK_MSGQ);
}
reds = garbage_collect(p, ERTS_INVALID_HFRAG_PTR, need, objv, nobj, fcalls, 0);
reds_left = ERTS_REDS_LEFT(p, fcalls);
if (reds > reds_left)
reds = reds_left;
ASSERT(CONTEXT_REDS - (reds_left - reds) >= erts_proc_sched_data(p)->virtual_reds);
return reds;
}
void
erts_garbage_collect(Process* p, Uint need, Eterm* objv, int nobj)
{
int reds;
if (p->sig_qs.flags & (FS_ON_HEAP_MSGQ|FS_OFF_HEAP_MSGQ_CHNG)) {
erts_proc_lock(p, ERTS_PROC_LOCK_MSGQ);
erts_proc_sig_fetch(p);
erts_proc_unlock(p, ERTS_PROC_LOCK_MSGQ);
}
reds = garbage_collect(p, ERTS_INVALID_HFRAG_PTR, need, objv, nobj, p->fcalls, 0);
BUMP_REDS(p, reds);
ASSERT(CONTEXT_REDS - ERTS_BIF_REDS_LEFT(p)
>= erts_proc_sched_data(p)->virtual_reds);
}
/*
* Place all living data on a the new heap; deallocate any old heap.
* Meant to be used by hibernate/3.
*/
static int
garbage_collect_hibernate(Process* p, int check_long_gc)
{
Uint heap_size;
Eterm* heap;
Eterm* htop;
Uint actual_size;
char* area;
Uint area_sz;
Sint offs;
int reds;
if (p->flags & F_DISABLE_GC)
ERTS_INTERNAL_ERROR("GC disabled");
if (p->sig_qs.flags & FS_ON_HEAP_MSGQ) {
erts_proc_lock(p, ERTS_PROC_LOCK_MSGQ);
erts_proc_sig_fetch(p);
erts_proc_unlock(p, ERTS_PROC_LOCK_MSGQ);
}
if (ERTS_SCHEDULER_IS_DIRTY(erts_proc_sched_data(p))) {
p->flags &= ~(F_DIRTY_GC_HIBERNATE|F_DIRTY_MAJOR_GC|F_DIRTY_MINOR_GC);
} else if (check_long_gc) {
Uint flags = p->flags;
p->flags |= F_NEED_FULLSWEEP;
check_for_possibly_long_gc(p, ((p->htop - p->heap) +
p->mbuf_sz + S_RESERVED));