]> granicus.if.org Git - gc/blob - reclaim.c
2011-04-18 Ivan Maidanski <ivmai@mail.ru>
[gc] / reclaim.c
1 /*
2  * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3  * Copyright (c) 1991-1996 by Xerox Corporation.  All rights reserved.
4  * Copyright (c) 1996-1999 by Silicon Graphics.  All rights reserved.
5  * Copyright (c) 1999-2004 Hewlett-Packard Development Company, L.P.
6  *
7  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
9  *
10  * Permission is hereby granted to use or copy this program
11  * for any purpose,  provided the above notices are retained on all copies.
12  * Permission to modify the code and to distribute modified code is granted,
13  * provided the above notices are retained, and a notice that the code was
14  * modified is included with the above copyright notice.
15  */
16
17 #include "private/gc_priv.h"
18
19 #include <stdio.h>
20
21 GC_INNER signed_word GC_bytes_found = 0;
22                         /* Number of bytes of memory reclaimed     */
23                         /* minus the number of bytes originally    */
24                         /* on free lists which we had to drop.     */
25
26 #if defined(PARALLEL_MARK)
27   GC_INNER word GC_fl_builder_count = 0;
28         /* Number of threads currently building free lists without      */
29         /* holding GC lock.  It is not safe to collect if this is       */
30         /* nonzero.                                                     */
31 #endif /* PARALLEL_MARK */
32
33 /* We defer printing of leaked objects until we're done with the GC     */
34 /* cycle, since the routine for printing objects needs to run outside   */
35 /* the collector, e.g. without the allocation lock.                     */
36 #ifndef MAX_LEAKED
37 # define MAX_LEAKED 40
38 #endif
39 STATIC ptr_t GC_leaked[MAX_LEAKED] = { NULL };
40 STATIC unsigned GC_n_leaked = 0;
41
42 GC_INNER GC_bool GC_have_errors = FALSE;
43
44 GC_INLINE void GC_add_leaked(ptr_t leaked)
45 {
46     GC_have_errors = TRUE;
47     /* FIXME: Prevent adding an object while printing leaked ones.      */
48     if (GC_n_leaked < MAX_LEAKED) {
49       GC_leaked[GC_n_leaked++] = leaked;
50       /* Make sure it's not reclaimed this cycle */
51       GC_set_mark_bit(leaked);
52     }
53 }
54
55 /* Print all objects on the list after printing any smashed objects.    */
56 /* Clear both lists.  Called without the allocation lock held.          */
57 GC_INNER void GC_print_all_errors(void)
58 {
59     static GC_bool printing_errors = FALSE;
60     GC_bool have_errors;
61     unsigned i;
62     DCL_LOCK_STATE;
63
64     LOCK();
65     if (printing_errors) {
66         UNLOCK();
67         return;
68     }
69     have_errors = GC_have_errors;
70     printing_errors = TRUE;
71     UNLOCK();
72
73     if (GC_debugging_started) {
74       GC_print_all_smashed();
75     } else {
76       have_errors = FALSE;
77     }
78
79     for (i = 0; i < GC_n_leaked; ++i) {
80         ptr_t p = GC_leaked[i];
81         if (HDR(p) -> hb_obj_kind == PTRFREE) {
82             GC_err_printf("Leaked atomic object at ");
83         } else {
84             GC_err_printf("Leaked composite object at ");
85         }
86         GC_print_heap_obj(p);
87         GC_err_printf("\n");
88         GC_free(p);
89         GC_leaked[i] = 0;
90         have_errors = TRUE;
91     }
92     GC_n_leaked = 0;
93
94     if (have_errors
95 #       ifndef GC_ABORT_ON_LEAK
96           && GETENV("GC_ABORT_ON_LEAK") != NULL
97 #       endif
98         ) {
99       ABORT("Leaked or smashed objects encountered");
100     }
101
102     printing_errors = FALSE;
103 }
104
105
106 /*
107  * reclaim phase
108  *
109  */
110
111 /* Test whether a block is completely empty, i.e. contains no marked    */
112 /* objects.  This does not require the block to be in physical memory.  */
113 GC_INNER GC_bool GC_block_empty(hdr *hhdr)
114 {
115     return (hhdr -> hb_n_marks == 0);
116 }
117
118 STATIC GC_bool GC_block_nearly_full(hdr *hhdr)
119 {
120     return (hhdr -> hb_n_marks > 7 * HBLK_OBJS(hhdr -> hb_sz)/8);
121 }
122
123 /* FIXME: This should perhaps again be specialized for USE_MARK_BYTES   */
124 /* and USE_MARK_BITS cases.                                             */
125
126 /*
127  * Restore unmarked small objects in h of size sz to the object
128  * free list.  Returns the new list.
129  * Clears unmarked objects.  Sz is in bytes.
130  */
131 STATIC ptr_t GC_reclaim_clear(struct hblk *hbp, hdr *hhdr, size_t sz,
132                               ptr_t list, signed_word *count)
133 {
134     word bit_no = 0;
135     word *p, *q, *plim;
136     signed_word n_bytes_found = 0;
137
138     GC_ASSERT(hhdr == GC_find_header((ptr_t)hbp));
139     GC_ASSERT(sz == hhdr -> hb_sz);
140     GC_ASSERT((sz & (BYTES_PER_WORD-1)) == 0);
141     p = (word *)(hbp->hb_body);
142     plim = (word *)(hbp->hb_body + HBLKSIZE - sz);
143
144     /* go through all words in block */
145         while (p <= plim) {
146             if( mark_bit_from_hdr(hhdr, bit_no) ) {
147                 p = (word *)((ptr_t)p + sz);
148             } else {
149                 n_bytes_found += sz;
150                 /* object is available - put on list */
151                     obj_link(p) = list;
152                     list = ((ptr_t)p);
153                 /* Clear object, advance p to next object in the process */
154                     q = (word *)((ptr_t)p + sz);
155 #                   ifdef USE_MARK_BYTES
156                       GC_ASSERT(!(sz & 1)
157                                 && !((word)p & (2 * sizeof(word) - 1)));
158                       p[1] = 0;
159                       p += 2;
160                       while (p < q) {
161                         CLEAR_DOUBLE(p);
162                         p += 2;
163                       }
164 #                   else
165                       p++; /* Skip link field */
166                       while (p < q) {
167                         *p++ = 0;
168                       }
169 #                   endif
170             }
171             bit_no += MARK_BIT_OFFSET(sz);
172         }
173     *count += n_bytes_found;
174     return(list);
175 }
176
177 /* The same thing, but don't clear objects: */
178 STATIC ptr_t GC_reclaim_uninit(struct hblk *hbp, hdr *hhdr, size_t sz,
179                                ptr_t list, signed_word *count)
180 {
181     word bit_no = 0;
182     word *p, *plim;
183     signed_word n_bytes_found = 0;
184
185     GC_ASSERT(sz == hhdr -> hb_sz);
186     p = (word *)(hbp->hb_body);
187     plim = (word *)((ptr_t)hbp + HBLKSIZE - sz);
188
189     /* go through all words in block */
190         while (p <= plim) {
191             if( !mark_bit_from_hdr(hhdr, bit_no) ) {
192                 n_bytes_found += sz;
193                 /* object is available - put on list */
194                     obj_link(p) = list;
195                     list = ((ptr_t)p);
196             }
197             p = (word *)((ptr_t)p + sz);
198             bit_no += MARK_BIT_OFFSET(sz);
199         }
200     *count += n_bytes_found;
201     return(list);
202 }
203
204 /* Don't really reclaim objects, just check for unmarked ones: */
205 STATIC void GC_reclaim_check(struct hblk *hbp, hdr *hhdr, word sz)
206 {
207     word bit_no;
208     ptr_t p, plim;
209     GC_ASSERT(sz == hhdr -> hb_sz);
210
211     /* go through all words in block */
212     p = hbp->hb_body;
213     plim = p + HBLKSIZE - sz;
214     for (bit_no = 0; p <= plim; p += sz, bit_no += MARK_BIT_OFFSET(sz)) {
215       if (!mark_bit_from_hdr(hhdr, bit_no)) {
216         GC_add_leaked(p);
217       }
218     }
219 }
220
221 /*
222  * Generic procedure to rebuild a free list in hbp.
223  * Also called directly from GC_malloc_many.
224  * Sz is now in bytes.
225  */
226 GC_INNER ptr_t GC_reclaim_generic(struct hblk * hbp, hdr *hhdr, size_t sz,
227                                   GC_bool init, ptr_t list,
228                                   signed_word *count)
229 {
230     ptr_t result;
231
232     GC_ASSERT(GC_find_header((ptr_t)hbp) == hhdr);
233 #   ifndef GC_DISABLE_INCREMENTAL
234       GC_remove_protection(hbp, 1, (hhdr)->hb_descr == 0 /* Pointer-free? */);
235 #   endif
236     if (init || GC_debugging_started) {
237       result = GC_reclaim_clear(hbp, hhdr, sz, list, count);
238     } else {
239       GC_ASSERT((hhdr)->hb_descr == 0 /* Pointer-free block */);
240       result = GC_reclaim_uninit(hbp, hhdr, sz, list, count);
241     }
242     if (IS_UNCOLLECTABLE(hhdr -> hb_obj_kind)) GC_set_hdr_marks(hhdr);
243     return result;
244 }
245
246 /*
247  * Restore unmarked small objects in the block pointed to by hbp
248  * to the appropriate object free list.
249  * If entirely empty blocks are to be completely deallocated, then
250  * caller should perform that check.
251  */
252 STATIC void GC_reclaim_small_nonempty_block(struct hblk *hbp,
253                                             GC_bool report_if_found)
254 {
255     hdr *hhdr = HDR(hbp);
256     size_t sz = hhdr -> hb_sz;
257     int kind = hhdr -> hb_obj_kind;
258     struct obj_kind * ok = &GC_obj_kinds[kind];
259     void **flh = &(ok -> ok_freelist[BYTES_TO_GRANULES(sz)]);
260
261     hhdr -> hb_last_reclaimed = (unsigned short) GC_gc_no;
262
263     if (report_if_found) {
264         GC_reclaim_check(hbp, hhdr, sz);
265     } else {
266         *flh = GC_reclaim_generic(hbp, hhdr, sz,
267                                   ok -> ok_init,
268                                   *flh, &GC_bytes_found);
269     }
270 }
271
272 /*
273  * Restore an unmarked large object or an entirely empty blocks of small objects
274  * to the heap block free list.
275  * Otherwise enqueue the block for later processing
276  * by GC_reclaim_small_nonempty_block.
277  * If report_if_found is TRUE, then process any block immediately, and
278  * simply report free objects; do not actually reclaim them.
279  */
280 STATIC void GC_reclaim_block(struct hblk *hbp, word report_if_found)
281 {
282     hdr * hhdr = HDR(hbp);
283     size_t sz = hhdr -> hb_sz;  /* size of objects in current block     */
284     struct obj_kind * ok = &GC_obj_kinds[hhdr -> hb_obj_kind];
285     struct hblk ** rlh;
286
287     if( sz > MAXOBJBYTES ) {  /* 1 big object */
288         if( !mark_bit_from_hdr(hhdr, 0) ) {
289             if (report_if_found) {
290               GC_add_leaked((ptr_t)hbp);
291             } else {
292               size_t blocks = OBJ_SZ_TO_BLOCKS(sz);
293               if (blocks > 1) {
294                 GC_large_allocd_bytes -= blocks * HBLKSIZE;
295               }
296               GC_bytes_found += sz;
297               GC_freehblk(hbp);
298             }
299         } else {
300             if (hhdr -> hb_descr != 0) {
301               GC_composite_in_use += sz;
302             } else {
303               GC_atomic_in_use += sz;
304             }
305         }
306     } else {
307         GC_bool empty = GC_block_empty(hhdr);
308 #       ifdef PARALLEL_MARK
309           /* Count can be low or one too high because we sometimes      */
310           /* have to ignore decrements.  Objects can also potentially   */
311           /* be repeatedly marked by each marker.                       */
312           /* Here we assume two markers, but this is extremely          */
313           /* unlikely to fail spuriously with more.  And if it does, it */
314           /* should be looked at.                                       */
315           GC_ASSERT(hhdr -> hb_n_marks <= 2 * (HBLKSIZE/sz + 1) + 16);
316 #       else
317           GC_ASSERT(sz * hhdr -> hb_n_marks <= HBLKSIZE);
318 #       endif
319         if (hhdr -> hb_descr != 0) {
320           GC_composite_in_use += sz * hhdr -> hb_n_marks;
321         } else {
322           GC_atomic_in_use += sz * hhdr -> hb_n_marks;
323         }
324         if (report_if_found) {
325           GC_reclaim_small_nonempty_block(hbp, (GC_bool)report_if_found);
326         } else if (empty) {
327           GC_bytes_found += HBLKSIZE;
328           GC_freehblk(hbp);
329         } else if (GC_find_leak || !GC_block_nearly_full(hhdr)) {
330           /* group of smaller objects, enqueue the real work */
331           rlh = &(ok -> ok_reclaim_list[BYTES_TO_GRANULES(sz)]);
332           hhdr -> hb_next = *rlh;
333           *rlh = hbp;
334         } /* else not worth salvaging. */
335         /* We used to do the nearly_full check later, but we    */
336         /* already have the right cache context here.  Also     */
337         /* doing it here avoids some silly lock contention in   */
338         /* GC_malloc_many.                                      */
339     }
340 }
341
342 #if !defined(NO_DEBUGGING)
343 /* Routines to gather and print heap block info         */
344 /* intended for debugging.  Otherwise should be called  */
345 /* with lock.                                           */
346
347 struct Print_stats
348 {
349         size_t number_of_blocks;
350         size_t total_bytes;
351 };
352
353 #ifdef USE_MARK_BYTES
354
355 /* Return the number of set mark bits in the given header       */
356 STATIC int GC_n_set_marks(hdr *hhdr)
357 {
358     int result = 0;
359     int i;
360     size_t sz = hhdr -> hb_sz;
361     int offset = (int)MARK_BIT_OFFSET(sz);
362     int limit = (int)FINAL_MARK_BIT(sz);
363
364     for (i = 0; i < limit; i += offset) {
365         result += hhdr -> hb_marks[i];
366     }
367     GC_ASSERT(hhdr -> hb_marks[limit]);
368     return(result);
369 }
370
371 #else
372
373 /* Number of set bits in a word.  Not performance critical.     */
374 static int set_bits(word n)
375 {
376     word m = n;
377     int result = 0;
378
379     while (m > 0) {
380         if (m & 1) result++;
381         m >>= 1;
382     }
383     return(result);
384 }
385
386 /* Return the number of set mark bits in the given header       */
387 STATIC int GC_n_set_marks(hdr *hhdr)
388 {
389     int result = 0;
390     int i;
391     int n_mark_words;
392 #   ifdef MARK_BIT_PER_OBJ
393       int n_objs = (int)HBLK_OBJS(hhdr -> hb_sz);
394
395       if (0 == n_objs) n_objs = 1;
396       n_mark_words = divWORDSZ(n_objs + WORDSZ - 1);
397 #   else /* MARK_BIT_PER_GRANULE */
398       n_mark_words = MARK_BITS_SZ;
399 #   endif
400     for (i = 0; i < n_mark_words - 1; i++) {
401         result += set_bits(hhdr -> hb_marks[i]);
402     }
403 #   ifdef MARK_BIT_PER_OBJ
404       result += set_bits((hhdr -> hb_marks[n_mark_words - 1])
405                          << (n_mark_words * WORDSZ - n_objs));
406 #   else
407       result += set_bits(hhdr -> hb_marks[n_mark_words - 1]);
408 #   endif
409     return(result - 1);
410 }
411
412 #endif /* !USE_MARK_BYTES  */
413
414 STATIC void GC_print_block_descr(struct hblk *h,
415                                  word /* struct PrintStats */ raw_ps)
416 {
417     hdr * hhdr = HDR(h);
418     size_t bytes = hhdr -> hb_sz;
419     struct Print_stats *ps;
420     unsigned n_marks = GC_n_set_marks(hhdr);
421
422     if (hhdr -> hb_n_marks != n_marks) {
423       GC_printf("(%u:%u,%u!=%u)", hhdr -> hb_obj_kind, (unsigned)bytes,
424                 (unsigned)hhdr -> hb_n_marks, n_marks);
425     } else {
426       GC_printf("(%u:%u,%u)", hhdr -> hb_obj_kind,
427                 (unsigned)bytes, n_marks);
428     }
429     bytes += HBLKSIZE-1;
430     bytes &= ~(HBLKSIZE-1);
431
432     ps = (struct Print_stats *)raw_ps;
433     ps->total_bytes += bytes;
434     ps->number_of_blocks++;
435 }
436
437 void GC_print_block_list(void)
438 {
439     struct Print_stats pstats;
440
441     GC_printf("(kind(0=ptrfree,1=normal,2=unc.):size_in_bytes, #_marks_set)\n");
442     pstats.number_of_blocks = 0;
443     pstats.total_bytes = 0;
444     GC_apply_to_all_blocks(GC_print_block_descr, (word)&pstats);
445     GC_printf("\nblocks = %lu, bytes = %lu\n",
446               (unsigned long)pstats.number_of_blocks,
447               (unsigned long)pstats.total_bytes);
448 }
449
450 /* Currently for debugger use only: */
451 void GC_print_free_list(int kind, size_t sz_in_granules)
452 {
453     struct obj_kind * ok = &GC_obj_kinds[kind];
454     ptr_t flh = ok -> ok_freelist[sz_in_granules];
455     struct hblk *lastBlock = 0;
456     int n;
457
458     for (n = 1; flh; n++) {
459         struct hblk *block = HBLKPTR(flh);
460         if (block != lastBlock) {
461           GC_printf("\nIn heap block at %p:\n\t", block);
462           lastBlock = block;
463         }
464         GC_printf("%d: %p;", n, flh);
465         flh = obj_link(flh);
466     }
467 }
468
469 #endif /* !NO_DEBUGGING */
470
471 /*
472  * Clear all obj_link pointers in the list of free objects *flp.
473  * Clear *flp.
474  * This must be done before dropping a list of free gcj-style objects,
475  * since may otherwise end up with dangling "descriptor" pointers.
476  * It may help for other pointer-containing objects.
477  */
478 STATIC void GC_clear_fl_links(void **flp)
479 {
480     void *next = *flp;
481
482     while (0 != next) {
483        *flp = 0;
484        flp = &(obj_link(next));
485        next = *flp;
486     }
487 }
488
489 /*
490  * Perform GC_reclaim_block on the entire heap, after first clearing
491  * small object free lists (if we are not just looking for leaks).
492  */
493 GC_INNER void GC_start_reclaim(GC_bool report_if_found)
494 {
495     unsigned kind;
496
497 #   if defined(PARALLEL_MARK)
498       GC_ASSERT(0 == GC_fl_builder_count);
499 #   endif
500     /* Reset in use counters.  GC_reclaim_block recomputes them. */
501       GC_composite_in_use = 0;
502       GC_atomic_in_use = 0;
503     /* Clear reclaim- and free-lists */
504       for (kind = 0; kind < GC_n_kinds; kind++) {
505         void **fop;
506         void **lim;
507         struct hblk ** rlp;
508         struct hblk ** rlim;
509         struct hblk ** rlist = GC_obj_kinds[kind].ok_reclaim_list;
510         GC_bool should_clobber = (GC_obj_kinds[kind].ok_descriptor != 0);
511
512         if (rlist == 0) continue;       /* This kind not used.  */
513         if (!report_if_found) {
514             lim = &(GC_obj_kinds[kind].ok_freelist[MAXOBJGRANULES+1]);
515             for( fop = GC_obj_kinds[kind].ok_freelist; fop < lim; fop++ ) {
516               if (*fop != 0) {
517                 if (should_clobber) {
518                   GC_clear_fl_links(fop);
519                 } else {
520                   *fop = 0;
521                 }
522               }
523             }
524         } /* otherwise free list objects are marked,    */
525           /* and its safe to leave them                 */
526         rlim = rlist + MAXOBJGRANULES+1;
527         for( rlp = rlist; rlp < rlim; rlp++ ) {
528             *rlp = 0;
529         }
530       }
531
532
533   /* Go through all heap blocks (in hblklist) and reclaim unmarked objects */
534   /* or enqueue the block for later processing.                            */
535     GC_apply_to_all_blocks(GC_reclaim_block, (word)report_if_found);
536
537 # ifdef EAGER_SWEEP
538     /* This is a very stupid thing to do.  We make it possible anyway,  */
539     /* so that you can convince yourself that it really is very stupid. */
540     GC_reclaim_all((GC_stop_func)0, FALSE);
541 # endif
542 # if defined(PARALLEL_MARK)
543     GC_ASSERT(0 == GC_fl_builder_count);
544 # endif
545
546 }
547
548 /*
549  * Sweep blocks of the indicated object size and kind until either the
550  * appropriate free list is nonempty, or there are no more blocks to
551  * sweep.
552  */
553 GC_INNER void GC_continue_reclaim(size_t sz /* granules */, int kind)
554 {
555     hdr * hhdr;
556     struct hblk * hbp;
557     struct obj_kind * ok = &(GC_obj_kinds[kind]);
558     struct hblk ** rlh = ok -> ok_reclaim_list;
559     void **flh = &(ok -> ok_freelist[sz]);
560
561     if (rlh == 0) return;       /* No blocks of this kind.      */
562     rlh += sz;
563     while ((hbp = *rlh) != 0) {
564         hhdr = HDR(hbp);
565         *rlh = hhdr -> hb_next;
566         GC_reclaim_small_nonempty_block(hbp, FALSE);
567         if (*flh != 0) break;
568     }
569 }
570
571 /*
572  * Reclaim all small blocks waiting to be reclaimed.
573  * Abort and return FALSE when/if (*stop_func)() returns TRUE.
574  * If this returns TRUE, then it's safe to restart the world
575  * with incorrectly cleared mark bits.
576  * If ignore_old is TRUE, then reclaim only blocks that have been
577  * recently reclaimed, and discard the rest.
578  * Stop_func may be 0.
579  */
580 GC_INNER GC_bool GC_reclaim_all(GC_stop_func stop_func, GC_bool ignore_old)
581 {
582     word sz;
583     unsigned kind;
584     hdr * hhdr;
585     struct hblk * hbp;
586     struct obj_kind * ok;
587     struct hblk ** rlp;
588     struct hblk ** rlh;
589 #   ifndef SMALL_CONFIG
590       CLOCK_TYPE start_time = 0; /* initialized to prevent warning. */
591       CLOCK_TYPE done_time;
592
593       if (GC_print_stats == VERBOSE)
594         GET_TIME(start_time);
595 #   endif
596
597     for (kind = 0; kind < GC_n_kinds; kind++) {
598         ok = &(GC_obj_kinds[kind]);
599         rlp = ok -> ok_reclaim_list;
600         if (rlp == 0) continue;
601         for (sz = 1; sz <= MAXOBJGRANULES; sz++) {
602             rlh = rlp + sz;
603             while ((hbp = *rlh) != 0) {
604                 if (stop_func != (GC_stop_func)0 && (*stop_func)()) {
605                     return(FALSE);
606                 }
607                 hhdr = HDR(hbp);
608                 *rlh = hhdr -> hb_next;
609                 if (!ignore_old || hhdr -> hb_last_reclaimed == GC_gc_no - 1) {
610                     /* It's likely we'll need it this time, too */
611                     /* It's been touched recently, so this      */
612                     /* shouldn't trigger paging.                */
613                     GC_reclaim_small_nonempty_block(hbp, FALSE);
614                 }
615             }
616         }
617     }
618 #   ifndef SMALL_CONFIG
619       if (GC_print_stats == VERBOSE) {
620         GET_TIME(done_time);
621         GC_log_printf("Disposing of reclaim lists took %lu msecs\n",
622                       MS_TIME_DIFF(done_time,start_time));
623       }
624 #   endif
625     return(TRUE);
626 }