forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gc-debug.c
883 lines (815 loc) · 28 KB
/
gc-debug.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
// This file is a part of Julia. License is MIT: http://julialang.org/license
#include "gc.h"
#include <inttypes.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
// Useful function in debugger to find page/region metadata
jl_gc_pagemeta_t *jl_gc_page_metadata(void *data)
{
return page_metadata(data);
}
region_t *jl_gc_find_region(void *ptr, int maybe)
{
return find_region(ptr, maybe);
}
// Find the memory block in the pool that owns the byte pointed to by p.
// For end of object pointer (which is always the case for pointer to a
// singleton object), this usually returns the same pointer which points to
// the next object but it can also return NULL if the pointer is pointing to
// the end of the page.
JL_DLLEXPORT jl_taggedvalue_t *jl_gc_find_taggedvalue_pool(char *p, size_t *osize_p)
{
region_t *r = find_region(p, 1);
// Not in the pool
if (!r)
return NULL;
char *page_begin = gc_page_data(p) + GC_PAGE_OFFSET;
// In the page header
if (p < page_begin)
return NULL;
size_t ofs = p - page_begin;
int pg_idx = page_index(r, page_begin);
// Check if this is a free page
if (!(r->allocmap[pg_idx / 32] & (uint32_t)(1 << (pg_idx % 32))))
return NULL;
jl_gc_pagemeta_t *pagemeta = &r->meta[pg_idx];
int osize = pagemeta->osize;
// Shouldn't be needed, just in case
if (osize == 0)
return NULL;
char *tag = (char*)p - ofs % osize;
// Points to an "object" that gets into the next page
if (tag + osize > gc_page_data(p) + GC_PAGE_SZ)
return NULL;
if (osize_p)
*osize_p = osize;
return (jl_taggedvalue_t*)tag;
}
// mark verification
#ifdef GC_VERIFY
jl_value_t *lostval = NULL;
static arraylist_t lostval_parents;
static arraylist_t lostval_parents_done;
int gc_verifying;
void add_lostval_parent(jl_value_t *parent)
{
for(int i = 0; i < lostval_parents_done.len; i++) {
if ((jl_value_t*)lostval_parents_done.items[i] == parent)
return;
}
for(int i = 0; i < lostval_parents.len; i++) {
if ((jl_value_t*)lostval_parents.items[i] == parent)
return;
}
arraylist_push(&lostval_parents, parent);
}
/*
How to debug a missing write barrier :
(or rather how I do it, if you know of a better way update this)
First, reproduce it with GC_VERIFY. It does change the allocation profile so if the error
is rare enough this may not be straightforward. If the backtracking goes well you should know
which object and which of its slots was written to without being caught by the write
barrier. Most times this allows you to take a guess. If this type of object is modified
by C code directly, look for missing jl_gc_wb() on pointer updates. Be aware that there are
innocent looking functions which allocate (and thus trigger marking) only on special cases.
If you cant find it, you can try the following :
- Ensure that should_timeout() is deterministic instead of clock based.
- Once you have a completly deterministic program which crashes on gc_verify, the addresses
should stay constant between different runs (with same binary, same environment ...).
Do not forget to turn off ASLR (linux: echo 0 > /proc/sys/kernel/randomize_va_space).
At this point you should be able to run under gdb and use a hw watch to look for writes
at the exact addr of the slot (use something like watch *slot_addr if *slot_addr == val).
- If it went well you are now stopped at the exact point the problem is happening.
Backtraces in JIT'd code wont work for me (but I'm not sure they should) so in that
case you can try to jl_throw(something) from gdb.
*/
// this does not yet detect missing writes from marked to marked_noesc
// the error is caught at the first long collection
static arraylist_t bits_save[4];
// set all mark bits to bits
// record the state of the region and can replay it in restore()
// restore _must_ be called as this will overwrite parts of the
// freelist in pools
static void clear_mark(int bits)
{
gcval_t *pv;
if (!gc_verifying) {
for (int i = 0; i < 4; i++) {
bits_save[i].len = 0;
}
}
bigval_t *v;
for (int i = 0;i < jl_n_threads;i++) {
v = jl_all_tls_states[i]->heap.big_objects;
while (v != NULL) {
void *gcv = &v->header;
if (!gc_verifying) arraylist_push(&bits_save[gc_bits(gcv)], gcv);
gc_bits(gcv) = bits;
v = v->next;
}
}
v = big_objects_marked;
while (v != NULL) {
void *gcv = &v->header;
if (!gc_verifying) arraylist_push(&bits_save[gc_bits(gcv)], gcv);
gc_bits(gcv) = bits;
v = v->next;
}
for (int h = 0; h < REGION_COUNT; h++) {
region_t *region = ®ions[h];
if (!region->pages)
break;
for (int pg_i = 0; pg_i < region->pg_cnt / 32; pg_i++) {
uint32_t line = region->allocmap[pg_i];
if (line) {
for (int j = 0; j < 32; j++) {
if ((line >> j) & 1) {
jl_gc_pagemeta_t *pg = page_metadata(region->pages[pg_i*32 + j].data + GC_PAGE_OFFSET);
jl_tls_states_t *ptls = jl_all_tls_states[pg->thread_n];
jl_gc_pool_t *pool = &ptls->heap.norm_pools[pg->pool_n];
pv = (gcval_t*)(pg->data + GC_PAGE_OFFSET);
char *lim = (char*)pv + GC_PAGE_SZ - GC_PAGE_OFFSET - pool->osize;
while ((char*)pv <= lim) {
if (!gc_verifying) arraylist_push(&bits_save[gc_bits(pv)], pv);
gc_bits(pv) = bits;
pv = (gcval_t*)((char*)pv + pool->osize);
}
}
}
}
}
}
}
static void restore(void)
{
for(int b = 0; b < 4; b++) {
for(int i = 0; i < bits_save[b].len; i++) {
gc_bits(bits_save[b].items[i]) = b;
}
}
}
static void gc_verify_track(void)
{
do {
arraylist_push(&lostval_parents_done, lostval);
jl_printf(JL_STDERR, "Now looking for %p =======\n", lostval);
clear_mark(GC_CLEAN);
pre_mark();
gc_mark_object_list(&to_finalize, 0);
gc_mark_object_list(&finalizer_list, 0);
gc_mark_object_list(&finalizer_list_marked, 0);
visit_mark_stack();
if (lostval_parents.len == 0) {
jl_printf(JL_STDERR, "Could not find the missing link. We missed a toplevel root. This is odd.\n");
break;
}
jl_value_t *lostval_parent = NULL;
for(int i = 0; i < lostval_parents.len; i++) {
lostval_parent = (jl_value_t*)lostval_parents.items[i];
int clean_len = bits_save[GC_CLEAN].len;
for(int j = 0; j < clean_len + bits_save[GC_QUEUED].len; j++) {
void *p = bits_save[j >= clean_len ? GC_QUEUED : GC_CLEAN].items[j >= clean_len ? j - clean_len : j];
if (jl_valueof(p) == lostval_parent) {
lostval = lostval_parent;
lostval_parent = NULL;
break;
}
}
if (lostval_parent != NULL) break;
}
if (lostval_parent == NULL) { // all parents of lostval were also scheduled for deletion
lostval = (jl_value_t*)arraylist_pop(&lostval_parents);
}
else {
jl_printf(JL_STDERR, "Missing write barrier found !\n");
jl_printf(JL_STDERR, "%p was written a reference to %p that was not recorded\n", lostval_parent, lostval);
jl_printf(JL_STDERR, "(details above)\n");
lostval = NULL;
}
restore();
} while(lostval != NULL);
}
void gc_verify(void)
{
lostval = NULL;
lostval_parents.len = 0;
lostval_parents_done.len = 0;
clear_mark(GC_CLEAN);
gc_verifying = 1;
pre_mark();
gc_mark_object_list(&to_finalize, 0);
gc_mark_object_list(&finalizer_list, 0);
gc_mark_object_list(&finalizer_list_marked, 0);
visit_mark_stack();
int clean_len = bits_save[GC_CLEAN].len;
for(int i = 0; i < clean_len + bits_save[GC_QUEUED].len; i++) {
gcval_t *v = (gcval_t*)bits_save[i >= clean_len ? GC_QUEUED : GC_CLEAN].items[i >= clean_len ? i - clean_len : i];
if (gc_marked(v)) {
jl_printf(JL_STDERR, "Error. Early free of %p type :", v);
jl_(jl_typeof(jl_valueof(v)));
jl_printf(JL_STDERR, "val : ");
jl_(jl_valueof(v));
jl_printf(JL_STDERR, "Let's try to backtrack the missing write barrier :\n");
lostval = jl_valueof(v);
break;
}
}
if (lostval == NULL) {
gc_verifying = 0;
restore(); // we did not miss anything
return;
}
restore();
gc_verify_track();
gc_debug_print_status();
gc_debug_critical_error();
abort();
}
#endif
#ifdef GC_DEBUG_ENV
JL_DLLEXPORT jl_gc_debug_env_t jl_gc_debug_env = {
GC_MARKED_NOESC,
0,
{0, UINT64_MAX, 0, 0, 0, {0, 0, 0}},
{0, UINT64_MAX, 0, 0, 0, {0, 0, 0}},
{0, UINT64_MAX, 0, 0, 0, {0, 0, 0}}
};
static char *gc_stack_lo;
static void gc_debug_alloc_setnext(jl_alloc_num_t *num)
{
uint64_t interv = num->interv;
if (num->random[0] && num->interv != 1) {
// Randomly trigger GC with the same average frequency
double scale = log(1.0 + 1.0 / (double)(num->interv - 1));
double randinterv = floor(fabs(log(erand48(num->random))) / scale) + 1;
interv = randinterv >= UINT64_MAX ? UINT64_MAX : (uint64_t)randinterv;
}
uint64_t next = num->num + interv;
if (!num->interv || next > num->max || interv > next)
next = UINT64_MAX;
num->next = next;
}
static void gc_debug_alloc_init(jl_alloc_num_t *num, const char *name)
{
static const char *fmt = "JULIA_GC_ALLOC_%s";
char *buff = (char*)alloca(strlen(fmt) + strlen(name) + 1);
sprintf(buff, fmt, name);
char *env = getenv(buff);
if (!env || !*env)
return;
if (*env == 'r') {
env++;
srand((unsigned)uv_hrtime());
for (int i = 0;i < 3;i++) {
while (num->random[i] == 0) {
num->random[i] = rand();
}
}
}
num->interv = 1;
num->max = UINT64_MAX;
sscanf(env, "%" SCNd64 ":%" SCNd64 ":%" SCNd64,
(int64_t*)&num->min, (int64_t*)&num->interv, (int64_t*)&num->max);
if (num->interv == 0)
num->interv = 1;
num->next = num->min;
}
static int gc_debug_alloc_check(jl_alloc_num_t *num)
{
if (++num->num < num->next)
return 0;
gc_debug_alloc_setnext(num);
return 1;
}
int gc_debug_check_pool(void)
{
return gc_debug_alloc_check(&jl_gc_debug_env.pool);
}
int gc_debug_check_other(void)
{
return gc_debug_alloc_check(&jl_gc_debug_env.other);
}
void gc_debug_print_status(void)
{
uint64_t pool_count = jl_gc_debug_env.pool.num;
uint64_t other_count = jl_gc_debug_env.other.num;
jl_safe_printf("Allocations: %" PRIu64 " "
"(Pool: %" PRIu64 "; Other: %" PRIu64 "); GC: %d\n",
pool_count + other_count, pool_count, other_count, gc_num.pause);
}
void gc_debug_critical_error(void)
{
gc_debug_print_status();
if (!jl_gc_debug_env.wait_for_debugger)
return;
jl_safe_printf("Waiting for debugger to attach\n");
while (1) {
sleep(1000);
}
}
void gc_debug_print(void)
{
if (!gc_debug_alloc_check(&jl_gc_debug_env.print))
return;
gc_debug_print_status();
}
static void gc_scrub_range(char *stack_lo, char *stack_hi)
{
stack_lo = (char*)((uintptr_t)stack_lo & ~(uintptr_t)15);
for (char **stack_p = (char**)stack_lo;
stack_p > (char**)stack_hi;stack_p--) {
char *p = *stack_p;
size_t osize;
jl_taggedvalue_t *tag = jl_gc_find_taggedvalue_pool(p, &osize);
if (osize <= sizeof_jl_taggedvalue_t || !tag || gc_marked(tag))
continue;
jl_gc_pagemeta_t *pg = page_metadata(tag);
// Make sure the sweep rebuild the freelist
pg->has_marked = 1;
pg->has_young = 1;
// Find the age bit
char *page_begin = gc_page_data(tag) + GC_PAGE_OFFSET;
int obj_id = (((char*)tag) - page_begin) / osize;
uint8_t *ages = pg->ages + obj_id / 8;
// Force this to be a young object to save some memory
// (especially on 32bit where it's more likely to have pointer-like
// bit patterns)
*ages &= ~(1 << (obj_id % 8));
// set mark to GC_MARKED_NOESC (young and marked)
memset(tag, 0xff, osize);
}
}
void gc_scrub(char *stack_hi)
{
gc_scrub_range(gc_stack_lo, stack_hi);
}
#else
void gc_debug_critical_error(void)
{
}
void gc_debug_print_status(void)
{
// May not be accurate but should be helpful enough
uint64_t pool_count = gc_num.poolalloc;
uint64_t big_count = gc_num.bigalloc;
jl_safe_printf("Allocations: %" PRIu64 " "
"(Pool: %" PRIu64 "; Big: %" PRIu64 "); GC: %d\n",
pool_count + big_count, pool_count, big_count, gc_num.pause);
}
#endif
#ifdef OBJPROFILE
static htable_t obj_counts[3];
static htable_t obj_sizes[3];
void objprofile_count(void *ty, int old, int sz)
{
if (gc_verifying) return;
if ((intptr_t)ty <= 0x10) {
ty = (void*)jl_buff_tag;
}
else if (ty != (void*)jl_buff_tag && ty != jl_malloc_tag &&
jl_typeof(ty) == (jl_value_t*)jl_datatype_type &&
((jl_datatype_t*)ty)->instance) {
ty = jl_singleton_tag;
}
void **bp = ptrhash_bp(&obj_counts[old], ty);
if (*bp == HT_NOTFOUND)
*bp = (void*)2;
else
(*((intptr_t*)bp))++;
bp = ptrhash_bp(&obj_sizes[old], ty);
if (*bp == HT_NOTFOUND)
*bp = (void*)(intptr_t)(1 + sz);
else
*((intptr_t*)bp) += sz;
}
void objprofile_reset(void)
{
for(int g=0; g < 3; g++) {
htable_reset(&obj_counts[g], 0);
htable_reset(&obj_sizes[g], 0);
}
}
static void objprofile_print(htable_t nums, htable_t sizes)
{
for(int i=0; i < nums.size; i+=2) {
if (nums.table[i+1] != HT_NOTFOUND) {
void *ty = nums.table[i];
int num = (intptr_t)nums.table[i + 1] - 1;
size_t sz = (uintptr_t)ptrhash_get(&sizes, ty) - 1;
static const int ptr_hex_width = 2 * sizeof(void*);
if (sz > 2e9) {
jl_printf(JL_STDERR, " %6d : %*.1f GB of (%*p) ",
num, 6, ((double)sz) / 1024 / 1024 / 1024,
ptr_hex_width, ty);
}
else if (sz > 2e6) {
jl_printf(JL_STDERR, " %6d : %*.1f MB of (%*p) ",
num, 6, ((double)sz) / 1024 / 1024,
ptr_hex_width, ty);
}
else if (sz > 2e3) {
jl_printf(JL_STDERR, " %6d : %*.1f kB of (%*p) ",
num, 6, ((double)sz) / 1024,
ptr_hex_width, ty);
}
else {
jl_printf(JL_STDERR, " %6d : %*d B of (%*p) ",
num, 6, (int)sz, ptr_hex_width, ty);
}
if (ty == (void*)jl_buff_tag)
jl_printf(JL_STDERR, "#<buffer>");
else if (ty == jl_malloc_tag)
jl_printf(JL_STDERR, "#<malloc>");
else if (ty == jl_singleton_tag)
jl_printf(JL_STDERR, "#<singletons>");
else
jl_static_show(JL_STDERR, (jl_value_t*)ty);
jl_printf(JL_STDERR, "\n");
}
}
}
void objprofile_printall(void)
{
jl_printf(JL_STDERR, "Transient mark :\n");
objprofile_print(obj_counts[0], obj_sizes[0]);
jl_printf(JL_STDERR, "Perm mark :\n");
objprofile_print(obj_counts[1], obj_sizes[1]);
jl_printf(JL_STDERR, "Remset :\n");
objprofile_print(obj_counts[2], obj_sizes[2]);
}
#endif
STATIC_INLINE double jl_ns2ms(int64_t t)
{
return t / (double)1e6;
}
STATIC_INLINE double jl_ns2s(int64_t t)
{
return t / (double)1e9;
}
#if defined(GC_TIME) || defined(GC_FINAL_STATS)
static uint64_t gc_premark_end;
static uint64_t gc_postmark_end;
void gc_settime_premark_end(void)
{
gc_premark_end = jl_hrtime();
}
void gc_settime_postmark_end(void)
{
gc_postmark_end = jl_hrtime();
}
#endif
#ifdef GC_FINAL_STATS
#include <malloc.h>
static double process_t0;
static size_t max_pg_count = 0;
static size_t total_freed_bytes = 0;
static uint64_t max_pause = 0;
static uint64_t total_sweep_time = 0;
static uint64_t total_mark_time = 0;
static uint64_t total_fin_time = 0;
void gc_final_count_page(size_t pg_cnt)
{
if (pg_cnt > max_pg_count) {
max_pg_count = pg_cnt;
}
}
void gc_final_pause_end(int64_t t0, int64_t tend)
{
uint64_t post_time = gc_postmark_end - gc_premark_end;
uint64_t sweep_pause = tend - gc_premark_end;
uint64_t pause = tend - t0;
total_freed_bytes += gc_num.freed;
total_sweep_time += sweep_pause - post_time;
total_fin_time += post_time;
max_pause = max_pause < pause ? pause : max_pause;
total_mark_time += gc_premark_end - t0;
}
void jl_print_gc_stats(JL_STREAM *s)
{
double gct = gc_num.total_time / 1e9;
malloc_stats();
double ptime = jl_clock_now() - process_t0;
jl_printf(s, "exec time\t%.5f sec\n", ptime);
if (gc_num.pause > 0) {
jl_printf(s, "gc time \t%.5f sec (%2.1f%%) in %d (%d full) collections\n",
jl_ns2s(gc_num.total_time),
jl_ns2s(gc_num.total_time) / ptime * 100,
gc_num.pause, gc_num.full_sweep);
jl_printf(s, "gc pause \t%.2f ms avg\n\t\t%2.0f ms max\n",
jl_ns2ms(gc_num.total_time) / gc_num.pause,
jl_ns2ms(max_pause));
jl_printf(s, "\t\t(%2d%% mark, %2d%% sweep, %2d%% finalizers)\n",
(int)(total_mark_time * 100 / gc_num.total_time),
(int)(total_sweep_time * 100 / gc_num.total_time),
(int)(total_fin_time * 100 / gc_num.total_time));
}
int i = 0;
while (i < REGION_COUNT && regions[i].pages) i++;
jl_printf(s, "max allocated regions : %d\n", i);
struct mallinfo mi = mallinfo();
jl_printf(s, "malloc size\t%d MB\n", mi.uordblks/1024/1024);
jl_printf(s, "max page alloc\t%ld MB\n", max_pg_count*GC_PAGE_SZ/1024/1024);
jl_printf(s, "total freed\t%" PRIuPTR " b\n", total_freed_bytes);
jl_printf(s, "free rate\t%.1f MB/sec\n", (total_freed_bytes/gct)/1024/1024);
}
#else
void jl_print_gc_stats(JL_STREAM *s)
{
}
#endif
#ifdef GC_TIME
static int64_t skipped_pages = 0;
static int64_t total_pages = 0;
static int64_t freed_pages = 0;
static int64_t pool_sweep_start;
void gc_time_pool_start(void)
{
skipped_pages = 0;
total_pages = 0;
freed_pages = 0;
pool_sweep_start = jl_hrtime();
}
void gc_time_count_page(int freedall, int pg_skpd)
{
freed_pages += freedall;
skipped_pages += pg_skpd;
total_pages++;
}
void gc_time_pool_end(int sweep_full)
{
double sweep_pool_sec = (jl_hrtime() - pool_sweep_start) / 1e9;
double sweep_gb = total_pages * GC_PAGE_SZ / (double)(1024 * 1024 * 1024);
double sweep_speed = sweep_gb / sweep_pool_sec;
jl_printf(JL_STDOUT,
"GC sweep pools end %.2f ms at %.1f GB/s "
"(skipped %.2f %% of %" PRId64 ", swept %" PRId64 " pgs, "
"%" PRId64 " freed with %" PRId64 " lazily) %s\n",
sweep_pool_sec * 1000, sweep_speed,
(total_pages ? ((double)skipped_pages * 100) / total_pages : 0),
total_pages, total_pages - skipped_pages,
freed_pages, lazy_freed_pages,
sweep_full ? "full" : "quick");
}
static int64_t big_total;
static int64_t big_freed;
static int64_t big_reset;
static int64_t big_sweep_start;
void gc_time_big_start(void)
{
big_total = 0;
big_freed = 0;
big_reset = 0;
big_sweep_start = jl_hrtime();
}
void gc_time_count_big(int old_bits, int bits)
{
big_total++;
big_reset += bits == GC_CLEAN;
big_freed += !(old_bits & GC_MARKED);
}
void gc_time_big_end(void)
{
double t_ms = jl_ns2ms(jl_hrtime() - big_sweep_start);
jl_printf(JL_STDOUT, "GC sweep big %.2f "
"(freed %" PRId64 " / %" PRId64 " with %" PRId64 " rst)\n",
t_ms, big_freed, big_total, big_reset);
}
static int64_t mallocd_array_total;
static int64_t mallocd_array_freed;
static int64_t mallocd_array_sweep_start;
void gc_time_mallocd_array_start(void)
{
mallocd_array_total = 0;
mallocd_array_freed = 0;
mallocd_array_sweep_start = jl_hrtime();
}
void gc_time_count_mallocd_array(int bits)
{
mallocd_array_total++;
mallocd_array_freed += !(bits & GC_MARKED);
}
void gc_time_mallocd_array_end(void)
{
double t_ms = jl_ns2ms(jl_hrtime() - big_sweep_start);
jl_printf(JL_STDOUT, "GC sweep arrays %.2f "
"(freed %" PRId64 " / %" PRId64 ")\n",
t_ms, mallocd_array_freed, mallocd_array_total);
}
void gc_time_mark_pause(int64_t t0, int64_t scanned_bytes,
int64_t perm_scanned_bytes)
{
int64_t last_remset_len = 0;
int64_t remset_nptr = 0;
for (int t_i = 0;t_i < jl_n_threads;t_i++) {
jl_tls_states_t *ptls = jl_all_tls_states[t_i];
last_remset_len += ptls->heap.last_remset->len;
remset_nptr = ptls->heap.remset_nptr;
}
jl_printf(JL_STDOUT, "GC mark pause %.2f ms | "
"scanned %" PRId64 " kB = %" PRId64 " + %" PRId64 " | "
"remset %" PRId64 " %" PRId64 "\n",
jl_ns2ms(gc_premark_end - t0),
(scanned_bytes + perm_scanned_bytes) / 1024,
scanned_bytes / 1024, perm_scanned_bytes / 1024,
last_remset_len, remset_nptr);
}
void gc_time_sweep_pause(uint64_t gc_end_t, int64_t actual_allocd,
int64_t live_bytes, int64_t estimate_freed,
int sweep_full)
{
uint64_t sweep_pause = gc_end_t - gc_premark_end;
int pct = actual_allocd ? (gc_num.freed * 100) / actual_allocd : -1;
jl_printf(JL_STDOUT, "GC sweep pause %.2f ms live %" PRId64 " kB "
"(freed %" PRId64 " kB EST %" PRId64 " kB "
"[error %" PRId64 "] = %d%% of allocd b %ld) "
"(%.2f ms in post_mark) %s | next in %" PRId64 " kB\n",
jl_ns2ms(sweep_pause), live_bytes / 1024,
gc_num.freed / 1024, estimate_freed / 1024,
gc_num.freed - estimate_freed, pct, gc_num.since_sweep / 1024,
jl_ns2ms(gc_postmark_end - gc_premark_end),
sweep_full ? "full" : "quick", -gc_num.allocd / 1024);
}
#endif
void gc_debug_init(void)
{
#ifdef GC_DEBUG_ENV
gc_stack_lo = (char*)gc_get_stack_ptr();
char *env = getenv("JULIA_GC_NO_GENERATIONAL");
if (env && strcmp(env, "0") != 0)
jl_gc_debug_env.always_full = 1;
env = getenv("JULIA_GC_WAIT_FOR_DEBUGGER");
jl_gc_debug_env.wait_for_debugger = env && strcmp(env, "0") != 0;
gc_debug_alloc_init(&jl_gc_debug_env.pool, "POOL");
gc_debug_alloc_init(&jl_gc_debug_env.other, "OTHER");
gc_debug_alloc_init(&jl_gc_debug_env.print, "PRINT");
#endif
#ifdef GC_VERIFY
for (int i = 0; i < 4; i++)
arraylist_new(&bits_save[i], 0);
arraylist_new(&lostval_parents, 0);
arraylist_new(&lostval_parents_done, 0);
#endif
#ifdef OBJPROFILE
for (int g = 0;g < 3;g++) {
htable_new(&obj_counts[g], 0);
htable_new(&obj_sizes[g], 0);
}
#endif
#ifdef GC_FINAL_STATS
process_t0 = jl_clock_now();
#endif
}
// GC summary stats
#ifdef MEMPROFILE
// TODO repair this and possibly merge with `gc_count_pool`
static size_t pool_stats(jl_gc_pool_t *p, size_t *pwaste, size_t *np,
size_t *pnold)
{
gcval_t *v;
jl_gc_pagemeta_t *pg = p->pages;
size_t osize = p->osize;
size_t nused=0, nfree=0, npgs=0, nold = 0;
while (pg != NULL) {
npgs++;
v = (gcval_t*)(pg->data + GC_PAGE_OFFSET);
char *lim = (char*)v + GC_PAGE_SZ - GC_PAGE_OFFSET - osize;
int i = 0;
while ((char*)v <= lim) {
if (!gc_marked(v)) {
nfree++;
}
else {
nused++;
if (gc_bits(v) == GC_MARKED) {
nold++;
}
}
v = (gcval_t*)((char*)v + osize);
i++;
}
jl_gc_pagemeta_t *nextpg = NULL;
pg = nextpg;
}
*pwaste = npgs * GC_PAGE_SZ - (nused * p->osize);
*np = npgs;
*pnold = nold;
if (npgs != 0) {
jl_printf(JL_STDOUT,
"%4d : %7d/%7d objects (%3d%% old), %5d pages, %5d kB, %5d kB waste\n",
p->osize,
nused,
nused+nfree,
nused ? (nold*100)/nused : 0,
npgs,
(nused*p->osize)/1024,
*pwaste/1024);
}
return nused*p->osize;
}
void gc_stats_all_pool(void)
{
size_t nb=0, w, tw=0, no=0,tp=0, nold=0,noldbytes=0, np, nol;
for (int i = 0; i < JL_GC_N_POOLS; i++) {
for (int t_i = 0;t_i < jl_n_threads;t_i++) {
jl_tls_states_t *ptls = jl_all_tls_states[t_i];
size_t b = pool_stats(&ptls->heap.norm_pools[i], &w, &np, &nol);
nb += b;
no += (b / ptls->heap.norm_pools[i].osize);
tw += w;
tp += np;
nold += nol;
noldbytes += nol * ptls->heap.norm_pools[i].osize;
}
}
jl_printf(JL_STDOUT,
"%d objects (%d%% old), %d kB (%d%% old) total allocated, %d total fragments (%d%% overhead), in %d pages\n",
no, (nold*100)/no, nb/1024, (noldbytes*100)/nb, tw, (tw*100)/nb, tp);
}
void gc_stats_big_obj(void)
{
bigval_t *v = current_heap->big_objects;
size_t nused=0, nbytes=0;
while (v != NULL) {
if (gc_marked(&v->_data)) {
nused++;
nbytes += v->sz&~3;
}
v = v->next;
}
v = big_objects_marked;
size_t nused_old=0, nbytes_old=0;
while (v != NULL) {
if (gc_marked(&v->_data)) {
nused_old++;
nbytes_old += v->sz&~3;
}
v = v->next;
}
mallocarray_t *ma = current_heap->mallocarrays;
while (ma != NULL) {
if (gc_marked(jl_astaggedvalue(ma->a))) {
nused++;
nbytes += array_nbytes(ma->a);
}
ma = ma->next;
}
jl_printf(JL_STDOUT, "%d kB (%d%% old) in %d large objects (%d%% old)\n", (nbytes + nbytes_old)/1024, nbytes + nbytes_old ? (nbytes_old*100)/(nbytes + nbytes_old) : 0, nused + nused_old, nused+nused_old ? (nused_old*100)/(nused + nused_old) : 0);
}
#endif //MEMPROFILE
// Simple and dumb way to count cells with different gc bits in allocated pages
// Use as ground truth for debugging memory-leak-like issues.
static int64_t poolobj_sizes[4];
static int64_t empty_pages;
static void gc_count_pool_page(jl_gc_pagemeta_t *pg)
{
int osize = pg->osize;
char *data = pg->data;
gcval_t *v = (gcval_t*)(data + GC_PAGE_OFFSET);
char *lim = (char*)v + GC_PAGE_SZ - GC_PAGE_OFFSET - osize;
int has_live = 0;
while ((char*)v <= lim) {
int bits = gc_bits(v);
if (bits & GC_MARKED)
has_live = 1;
poolobj_sizes[bits] += osize;
v = (gcval_t*)((char*)v + osize);
}
if (!has_live) {
empty_pages++;
}
}
static void gc_count_pool_region(region_t *region)
{
for (int pg_i = 0; pg_i < region->pg_cnt / 32; pg_i++) {
uint32_t line = region->allocmap[pg_i];
if (line) {
for (int j = 0; j < 32; j++) {
if ((line >> j) & 1) {
gc_count_pool_page(®ion->meta[pg_i*32 + j]);
}
}
}
}
}
void gc_count_pool(void)
{
memset(&poolobj_sizes, 0, sizeof(poolobj_sizes));
empty_pages = 0;
for (int i = 0; i < REGION_COUNT; i++) {
if (!regions[i].pages)
break;
gc_count_pool_region(®ions[i]);
}
jl_safe_printf("****** Pool stat: ******\n");
for (int i = 0;i < 4;i++)
jl_safe_printf("bits(%d): %" PRId64 "\n", i, poolobj_sizes[i]);
// empty_pages is inaccurate after the sweep since young objects are
// also GC_CLEAN
jl_safe_printf("free pages: % " PRId64 "\n", empty_pages);
jl_safe_printf("************************\n");
}
#ifdef __cplusplus
}
#endif