run finalizers asynchronously (in asyncs and/or pthreads)
[bpt/guile.git] / libguile / gc.c
1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2006, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
17 */
18
19 /* #define DEBUGINFO */
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include "libguile/gen-scmconfig.h"
26
27 #include <stdio.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <math.h>
32
33 #ifdef __ia64__
34 #include <ucontext.h>
35 extern unsigned long * __libc_ia64_register_backing_store_base;
36 #endif
37
38 #include "libguile/_scm.h"
39 #include "libguile/eval.h"
40 #include "libguile/stime.h"
41 #include "libguile/stackchk.h"
42 #include "libguile/struct.h"
43 #include "libguile/smob.h"
44 #include "libguile/arrays.h"
45 #include "libguile/async.h"
46 #include "libguile/ports.h"
47 #include "libguile/root.h"
48 #include "libguile/strings.h"
49 #include "libguile/vectors.h"
50 #include "libguile/hashtab.h"
51 #include "libguile/tags.h"
52
53 #include "libguile/private-gc.h"
54 #include "libguile/validate.h"
55 #include "libguile/deprecation.h"
56 #include "libguile/gc.h"
57 #include "libguile/dynwind.h"
58
59 #include "libguile/bdw-gc.h"
60
61 /* For GC_set_start_callback. */
62 #include <gc/gc_mark.h>
63
64 #ifdef GUILE_DEBUG_MALLOC
65 #include "libguile/debug-malloc.h"
66 #endif
67
68 #ifdef HAVE_UNISTD_H
69 #include <unistd.h>
70 #endif
71
72 /* Set this to != 0 if every cell that is accessed shall be checked:
73 */
74 int scm_debug_cell_accesses_p = 0;
75 int scm_expensive_debug_cell_accesses_p = 0;
76
77 /* Set this to 0 if no additional gc's shall be performed, otherwise set it to
78 * the number of cell accesses after which a gc shall be called.
79 */
80 int scm_debug_cells_gc_interval = 0;
81
82 /* Hash table that keeps a reference to objects the user wants to protect from
83 garbage collection. */
84 static SCM scm_protects;
85
86
87 #if (SCM_DEBUG_CELL_ACCESSES == 1)
88
89
90 /*
91
92 Assert that the given object is a valid reference to a valid cell. This
93 test involves to determine whether the object is a cell pointer, whether
94 this pointer actually points into a heap segment and whether the cell
95 pointed to is not a free cell. Further, additional garbage collections may
96 get executed after a user defined number of cell accesses. This helps to
97 find places in the C code where references are dropped for extremely short
98 periods.
99
100 */
101 void
102 scm_i_expensive_validation_check (SCM cell)
103 {
104 /* If desired, perform additional garbage collections after a user
105 * defined number of cell accesses.
106 */
107 if (scm_debug_cells_gc_interval)
108 {
109 static unsigned int counter = 0;
110
111 if (counter != 0)
112 {
113 --counter;
114 }
115 else
116 {
117 counter = scm_debug_cells_gc_interval;
118 scm_gc ();
119 }
120 }
121 }
122
123 /* Whether cell validation is already running. */
124 static int scm_i_cell_validation_already_running = 0;
125
126 void
127 scm_assert_cell_valid (SCM cell)
128 {
129 if (!scm_i_cell_validation_already_running && scm_debug_cell_accesses_p)
130 {
131 scm_i_cell_validation_already_running = 1; /* set to avoid recursion */
132
133 /*
134 During GC, no user-code should be run, and the guile core
135 should use non-protected accessors.
136 */
137 if (scm_gc_running_p)
138 return;
139
140 /*
141 Only scm_in_heap_p and rescanning the heap is wildly
142 expensive.
143 */
144 if (scm_expensive_debug_cell_accesses_p)
145 scm_i_expensive_validation_check (cell);
146
147 scm_i_cell_validation_already_running = 0; /* re-enable */
148 }
149 }
150
151
152
153 SCM_DEFINE (scm_set_debug_cell_accesses_x, "set-debug-cell-accesses!", 1, 0, 0,
154 (SCM flag),
155 "If @var{flag} is @code{#f}, cell access checking is disabled.\n"
156 "If @var{flag} is @code{#t}, cheap cell access checking is enabled,\n"
157 "but no additional calls to garbage collection are issued.\n"
158 "If @var{flag} is a number, strict cell access checking is enabled,\n"
159 "with an additional garbage collection after the given\n"
160 "number of cell accesses.\n"
161 "This procedure only exists when the compile-time flag\n"
162 "@code{SCM_DEBUG_CELL_ACCESSES} was set to 1.")
163 #define FUNC_NAME s_scm_set_debug_cell_accesses_x
164 {
165 if (scm_is_false (flag))
166 {
167 scm_debug_cell_accesses_p = 0;
168 }
169 else if (scm_is_eq (flag, SCM_BOOL_T))
170 {
171 scm_debug_cells_gc_interval = 0;
172 scm_debug_cell_accesses_p = 1;
173 scm_expensive_debug_cell_accesses_p = 0;
174 }
175 else
176 {
177 scm_debug_cells_gc_interval = scm_to_signed_integer (flag, 0, INT_MAX);
178 scm_debug_cell_accesses_p = 1;
179 scm_expensive_debug_cell_accesses_p = 1;
180 }
181 return SCM_UNSPECIFIED;
182 }
183 #undef FUNC_NAME
184
185
186 #endif /* SCM_DEBUG_CELL_ACCESSES == 1 */
187
188 \f
189
190 /* Compatibility. */
191
192 #ifndef HAVE_GC_GET_HEAP_USAGE_SAFE
193 static void
194 GC_get_heap_usage_safe (GC_word *pheap_size, GC_word *pfree_bytes,
195 GC_word *punmapped_bytes, GC_word *pbytes_since_gc,
196 GC_word *ptotal_bytes)
197 {
198 *pheap_size = GC_get_heap_size ();
199 *pfree_bytes = GC_get_free_bytes ();
200 #ifdef HAVE_GC_GET_UNMAPPED_BYTES
201 *punmapped_bytes = GC_get_unmapped_bytes ();
202 #else
203 *punmapped_bytes = 0;
204 #endif
205 *pbytes_since_gc = GC_get_bytes_since_gc ();
206 *ptotal_bytes = GC_get_total_bytes ();
207 }
208 #endif
209
210 #ifndef HAVE_GC_GET_FREE_SPACE_DIVISOR
211 static GC_word
212 GC_get_free_space_divisor (void)
213 {
214 return GC_free_space_divisor;
215 }
216 #endif
217
218 \f
219 /* Hooks. */
220 scm_t_c_hook scm_before_gc_c_hook;
221 scm_t_c_hook scm_before_mark_c_hook;
222 scm_t_c_hook scm_before_sweep_c_hook;
223 scm_t_c_hook scm_after_sweep_c_hook;
224 scm_t_c_hook scm_after_gc_c_hook;
225
226
227 static void
228 run_before_gc_c_hook (void)
229 {
230 if (!SCM_I_CURRENT_THREAD)
231 /* GC while a thread is spinning up; punt. */
232 return;
233
234 scm_c_hook_run (&scm_before_gc_c_hook, NULL);
235 }
236
237
238 /* GC Statistics Keeping
239 */
240 unsigned long scm_gc_ports_collected = 0;
241 static long gc_time_taken = 0;
242 static long gc_start_time = 0;
243
244 static unsigned long free_space_divisor;
245 static unsigned long minimum_free_space_divisor;
246 static double target_free_space_divisor;
247
248 static unsigned long protected_obj_count = 0;
249
250
251 SCM_SYMBOL (sym_gc_time_taken, "gc-time-taken");
252 SCM_SYMBOL (sym_heap_size, "heap-size");
253 SCM_SYMBOL (sym_heap_free_size, "heap-free-size");
254 SCM_SYMBOL (sym_heap_total_allocated, "heap-total-allocated");
255 SCM_SYMBOL (sym_heap_allocated_since_gc, "heap-allocated-since-gc");
256 SCM_SYMBOL (sym_protected_objects, "protected-objects");
257 SCM_SYMBOL (sym_times, "gc-times");
258
259
260 /* {Scheme Interface to GC}
261 */
262 static SCM
263 tag_table_to_type_alist (void *closure, SCM key, SCM val, SCM acc)
264 {
265 if (scm_is_integer (key))
266 {
267 int c_tag = scm_to_int (key);
268
269 char const * name = scm_i_tag_name (c_tag);
270 if (name != NULL)
271 {
272 key = scm_from_locale_string (name);
273 }
274 else
275 {
276 char s[100];
277 sprintf (s, "tag %d", c_tag);
278 key = scm_from_locale_string (s);
279 }
280 }
281
282 return scm_cons (scm_cons (key, val), acc);
283 }
284
285 SCM_DEFINE (scm_gc_live_object_stats, "gc-live-object-stats", 0, 0, 0,
286 (),
287 "Return an alist of statistics of the current live objects. ")
288 #define FUNC_NAME s_scm_gc_live_object_stats
289 {
290 SCM tab = scm_make_hash_table (scm_from_int (57));
291 SCM alist;
292
293 alist
294 = scm_internal_hash_fold (&tag_table_to_type_alist, NULL, SCM_EOL, tab);
295
296 return alist;
297 }
298 #undef FUNC_NAME
299
300 extern int scm_gc_malloc_yield_percentage;
301 SCM_DEFINE (scm_gc_stats, "gc-stats", 0, 0, 0,
302 (),
303 "Return an association list of statistics about Guile's current\n"
304 "use of storage.\n")
305 #define FUNC_NAME s_scm_gc_stats
306 {
307 SCM answer;
308 GC_word heap_size, free_bytes, unmapped_bytes, bytes_since_gc, total_bytes;
309 size_t gc_times;
310
311 GC_get_heap_usage_safe (&heap_size, &free_bytes, &unmapped_bytes,
312 &bytes_since_gc, &total_bytes);
313 gc_times = GC_gc_no;
314
315 answer =
316 scm_list_n (scm_cons (sym_gc_time_taken, scm_from_long (gc_time_taken)),
317 scm_cons (sym_heap_size, scm_from_size_t (heap_size)),
318 scm_cons (sym_heap_free_size, scm_from_size_t (free_bytes)),
319 scm_cons (sym_heap_total_allocated,
320 scm_from_size_t (total_bytes)),
321 scm_cons (sym_heap_allocated_since_gc,
322 scm_from_size_t (bytes_since_gc)),
323 scm_cons (sym_protected_objects,
324 scm_from_ulong (protected_obj_count)),
325 scm_cons (sym_times, scm_from_size_t (gc_times)),
326 SCM_UNDEFINED);
327
328 return answer;
329 }
330 #undef FUNC_NAME
331
332
333 SCM_DEFINE (scm_gc_dump, "gc-dump", 0, 0, 0,
334 (void),
335 "Dump information about the garbage collector's internal data "
336 "structures and memory usage to the standard output.")
337 #define FUNC_NAME s_scm_gc_dump
338 {
339 GC_dump ();
340
341 return SCM_UNSPECIFIED;
342 }
343 #undef FUNC_NAME
344
345
346 SCM_DEFINE (scm_object_address, "object-address", 1, 0, 0,
347 (SCM obj),
348 "Return an integer that for the lifetime of @var{obj} is uniquely\n"
349 "returned by this function for @var{obj}")
350 #define FUNC_NAME s_scm_object_address
351 {
352 return scm_from_ulong (SCM_UNPACK (obj));
353 }
354 #undef FUNC_NAME
355
356
357 SCM_DEFINE (scm_gc_disable, "gc-disable", 0, 0, 0,
358 (),
359 "Disables the garbage collector. Nested calls are permitted. "
360 "GC is re-enabled once @code{gc-enable} has been called the "
361 "same number of times @code{gc-disable} was called.")
362 #define FUNC_NAME s_scm_gc_disable
363 {
364 GC_disable ();
365 return SCM_UNSPECIFIED;
366 }
367 #undef FUNC_NAME
368
369 SCM_DEFINE (scm_gc_enable, "gc-enable", 0, 0, 0,
370 (),
371 "Enables the garbage collector.")
372 #define FUNC_NAME s_scm_gc_enable
373 {
374 GC_enable ();
375 return SCM_UNSPECIFIED;
376 }
377 #undef FUNC_NAME
378
379
380 SCM_DEFINE (scm_gc, "gc", 0, 0, 0,
381 (),
382 "Scans all of SCM objects and reclaims for further use those that are\n"
383 "no longer accessible.")
384 #define FUNC_NAME s_scm_gc
385 {
386 scm_i_gc ("call");
387 GC_invoke_finalizers ();
388 return SCM_UNSPECIFIED;
389 }
390 #undef FUNC_NAME
391
392 void
393 scm_i_gc (const char *what)
394 {
395 #ifndef HAVE_GC_SET_START_CALLBACK
396 run_before_gc_c_hook ();
397 #endif
398 GC_gcollect ();
399 }
400
401
402 \f
403 /* {GC Protection Helper Functions}
404 */
405
406
407 /*
408 * If within a function you need to protect one or more scheme objects from
409 * garbage collection, pass them as parameters to one of the
410 * scm_remember_upto_here* functions below. These functions don't do
411 * anything, but since the compiler does not know that they are actually
412 * no-ops, it will generate code that calls these functions with the given
413 * parameters. Therefore, you can be sure that the compiler will keep those
414 * scheme values alive (on the stack or in a register) up to the point where
415 * scm_remember_upto_here* is called. In other words, place the call to
416 * scm_remember_upto_here* _behind_ the last code in your function, that
417 * depends on the scheme object to exist.
418 *
419 * Example: We want to make sure that the string object str does not get
420 * garbage collected during the execution of 'some_function' in the code
421 * below, because otherwise the characters belonging to str would be freed and
422 * 'some_function' might access freed memory. To make sure that the compiler
423 * keeps str alive on the stack or in a register such that it is visible to
424 * the conservative gc we add the call to scm_remember_upto_here_1 _after_ the
425 * call to 'some_function'. Note that this would not be necessary if str was
426 * used anyway after the call to 'some_function'.
427 * char *chars = scm_i_string_chars (str);
428 * some_function (chars);
429 * scm_remember_upto_here_1 (str); // str will be alive up to this point.
430 */
431
432 /* Remove any macro versions of these while defining the functions.
433 Functions are always included in the library, for upward binary
434 compatibility and in case combinations of GCC and non-GCC are used. */
435 #undef scm_remember_upto_here_1
436 #undef scm_remember_upto_here_2
437
438 void
439 scm_remember_upto_here_1 (SCM obj SCM_UNUSED)
440 {
441 /* Empty. Protects a single object from garbage collection. */
442 }
443
444 void
445 scm_remember_upto_here_2 (SCM obj1 SCM_UNUSED, SCM obj2 SCM_UNUSED)
446 {
447 /* Empty. Protects two objects from garbage collection. */
448 }
449
450 void
451 scm_remember_upto_here (SCM obj SCM_UNUSED, ...)
452 {
453 /* Empty. Protects any number of objects from garbage collection. */
454 }
455
456 /*
457 These crazy functions prevent garbage collection
458 of arguments after the first argument by
459 ensuring they remain live throughout the
460 function because they are used in the last
461 line of the code block.
462 It'd be better to have a nice compiler hint to
463 aid the conservative stack-scanning GC. --03/09/00 gjb */
464 SCM
465 scm_return_first (SCM elt, ...)
466 {
467 return elt;
468 }
469
470 int
471 scm_return_first_int (int i, ...)
472 {
473 return i;
474 }
475
476
477 SCM
478 scm_permanent_object (SCM obj)
479 {
480 return (scm_gc_protect_object (obj));
481 }
482
483
484 /* Protect OBJ from the garbage collector. OBJ will not be freed, even if all
485 other references are dropped, until the object is unprotected by calling
486 scm_gc_unprotect_object (OBJ). Calls to scm_gc_protect/unprotect_object nest,
487 i. e. it is possible to protect the same object several times, but it is
488 necessary to unprotect the object the same number of times to actually get
489 the object unprotected. It is an error to unprotect an object more often
490 than it has been protected before. The function scm_protect_object returns
491 OBJ.
492 */
493
494 /* Implementation note: For every object X, there is a counter which
495 scm_gc_protect_object (X) increments and scm_gc_unprotect_object (X) decrements.
496 */
497
498
499
500 SCM
501 scm_gc_protect_object (SCM obj)
502 {
503 SCM handle;
504
505 /* This critical section barrier will be replaced by a mutex. */
506 /* njrev: Indeed; if my comment above is correct, there is the same
507 critsec/mutex inconsistency here. */
508 SCM_CRITICAL_SECTION_START;
509
510 handle = scm_hashq_create_handle_x (scm_protects, obj, scm_from_int (0));
511 SCM_SETCDR (handle, scm_sum (SCM_CDR (handle), scm_from_int (1)));
512
513 protected_obj_count ++;
514
515 SCM_CRITICAL_SECTION_END;
516
517 return obj;
518 }
519
520
521 /* Remove any protection for OBJ established by a prior call to
522 scm_protect_object. This function returns OBJ.
523
524 See scm_protect_object for more information. */
525 SCM
526 scm_gc_unprotect_object (SCM obj)
527 {
528 SCM handle;
529
530 /* This critical section barrier will be replaced by a mutex. */
531 /* njrev: and again. */
532 SCM_CRITICAL_SECTION_START;
533
534 if (scm_gc_running_p)
535 {
536 fprintf (stderr, "scm_unprotect_object called during GC.\n");
537 abort ();
538 }
539
540 handle = scm_hashq_get_handle (scm_protects, obj);
541
542 if (scm_is_false (handle))
543 {
544 fprintf (stderr, "scm_unprotect_object called on unprotected object\n");
545 abort ();
546 }
547 else
548 {
549 SCM count = scm_difference (SCM_CDR (handle), scm_from_int (1));
550 if (scm_is_eq (count, scm_from_int (0)))
551 scm_hashq_remove_x (scm_protects, obj);
552 else
553 SCM_SETCDR (handle, count);
554 }
555 protected_obj_count --;
556
557 SCM_CRITICAL_SECTION_END;
558
559 return obj;
560 }
561
562 void
563 scm_gc_register_root (SCM *p)
564 {
565 /* Nothing. */
566 }
567
568 void
569 scm_gc_unregister_root (SCM *p)
570 {
571 /* Nothing. */
572 }
573
574 void
575 scm_gc_register_roots (SCM *b, unsigned long n)
576 {
577 SCM *p = b;
578 for (; p < b + n; ++p)
579 scm_gc_register_root (p);
580 }
581
582 void
583 scm_gc_unregister_roots (SCM *b, unsigned long n)
584 {
585 SCM *p = b;
586 for (; p < b + n; ++p)
587 scm_gc_unregister_root (p);
588 }
589
590 \f
591
592
593 /*
594 MOVE THIS FUNCTION. IT DOES NOT HAVE ANYTHING TODO WITH GC.
595 */
596
597 /* Get an integer from an environment variable. */
598 int
599 scm_getenv_int (const char *var, int def)
600 {
601 char *end = 0;
602 char *val = getenv (var);
603 long res = def;
604 if (!val)
605 return def;
606 res = strtol (val, &end, 10);
607 if (end == val)
608 return def;
609 return res;
610 }
611
612 void
613 scm_storage_prehistory ()
614 {
615 GC_all_interior_pointers = 0;
616 free_space_divisor = scm_getenv_int ("GC_FREE_SPACE_DIVISOR", 3);
617 minimum_free_space_divisor = free_space_divisor;
618 target_free_space_divisor = free_space_divisor;
619 GC_set_free_space_divisor (free_space_divisor);
620 GC_set_finalize_on_demand (1);
621
622 GC_INIT ();
623
624 #if (! ((defined GC_VERSION_MAJOR) && (GC_VERSION_MAJOR >= 7))) \
625 && (defined SCM_I_GSC_USE_PTHREAD_THREADS)
626 /* When using GC 6.8, this call is required to initialize thread-local
627 freelists (shouldn't be necessary with GC 7.0). */
628 GC_init ();
629 #endif
630
631 GC_expand_hp (SCM_DEFAULT_INIT_HEAP_SIZE_2);
632
633 /* We only need to register a displacement for those types for which the
634 higher bits of the type tag are used to store a pointer (that is, a
635 pointer to an 8-octet aligned region). For `scm_tc3_struct', this is
636 handled in `scm_alloc_struct ()'. */
637 GC_REGISTER_DISPLACEMENT (scm_tc3_cons);
638 /* GC_REGISTER_DISPLACEMENT (scm_tc3_unused); */
639
640 /* Sanity check. */
641 if (!GC_is_visible (&scm_protects))
642 abort ();
643
644 scm_c_hook_init (&scm_before_gc_c_hook, 0, SCM_C_HOOK_NORMAL);
645 scm_c_hook_init (&scm_before_mark_c_hook, 0, SCM_C_HOOK_NORMAL);
646 scm_c_hook_init (&scm_before_sweep_c_hook, 0, SCM_C_HOOK_NORMAL);
647 scm_c_hook_init (&scm_after_sweep_c_hook, 0, SCM_C_HOOK_NORMAL);
648 scm_c_hook_init (&scm_after_gc_c_hook, 0, SCM_C_HOOK_NORMAL);
649 }
650
651 scm_i_pthread_mutex_t scm_i_gc_admin_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
652
653 void
654 scm_init_gc_protect_object ()
655 {
656 scm_protects = scm_c_make_hash_table (31);
657
658 #if 0
659 /* We can't have a cleanup handler since we have no thread to run it
660 in. */
661
662 #ifdef HAVE_ATEXIT
663 atexit (cleanup);
664 #else
665 #ifdef HAVE_ON_EXIT
666 on_exit (cleanup, 0);
667 #endif
668 #endif
669
670 #endif
671 }
672
673 \f
674
675 SCM scm_after_gc_hook;
676
677 static SCM after_gc_async_cell;
678
679 /* The function after_gc_async_thunk causes the execution of the
680 * after-gc-hook. It is run after the gc, as soon as the asynchronous
681 * events are handled by the evaluator.
682 */
683 static SCM
684 after_gc_async_thunk (void)
685 {
686 /* Fun, no? Hook-run *and* run-hook? */
687 scm_c_hook_run (&scm_after_gc_c_hook, NULL);
688 scm_c_run_hook (scm_after_gc_hook, SCM_EOL);
689 return SCM_UNSPECIFIED;
690 }
691
692
693 /* The function queue_after_gc_hook is run by the scm_before_gc_c_hook
694 * at the end of the garbage collection. The only purpose of this
695 * function is to mark the after_gc_async (which will eventually lead to
696 * the execution of the after_gc_async_thunk).
697 */
698 static void *
699 queue_after_gc_hook (void * hook_data SCM_UNUSED,
700 void *fn_data SCM_UNUSED,
701 void *data SCM_UNUSED)
702 {
703 /* If cell access debugging is enabled, the user may choose to perform
704 * additional garbage collections after an arbitrary number of cell
705 * accesses. We don't want the scheme level after-gc-hook to be performed
706 * for each of these garbage collections for the following reason: The
707 * execution of the after-gc-hook causes cell accesses itself. Thus, if the
708 * after-gc-hook was performed with every gc, and if the gc was performed
709 * after a very small number of cell accesses, then the number of cell
710 * accesses during the execution of the after-gc-hook will suffice to cause
711 * the execution of the next gc. Then, guile would keep executing the
712 * after-gc-hook over and over again, and would never come to do other
713 * things.
714 *
715 * To overcome this problem, if cell access debugging with additional
716 * garbage collections is enabled, the after-gc-hook is never run by the
717 * garbage collecter. When running guile with cell access debugging and the
718 * execution of the after-gc-hook is desired, then it is necessary to run
719 * the hook explicitly from the user code. This has the effect, that from
720 * the scheme level point of view it seems that garbage collection is
721 * performed with a much lower frequency than it actually is. Obviously,
722 * this will not work for code that depends on a fixed one to one
723 * relationship between the execution counts of the C level garbage
724 * collection hooks and the execution count of the scheme level
725 * after-gc-hook.
726 */
727
728 #if (SCM_DEBUG_CELL_ACCESSES == 1)
729 if (scm_debug_cells_gc_interval == 0)
730 #endif
731 {
732 scm_i_thread *t = SCM_I_CURRENT_THREAD;
733
734 if (scm_is_false (SCM_CDR (after_gc_async_cell)))
735 {
736 SCM_SETCDR (after_gc_async_cell, t->active_asyncs);
737 t->active_asyncs = after_gc_async_cell;
738 t->pending_asyncs = 1;
739 }
740 }
741
742 return NULL;
743 }
744
745 \f
746
747 static void *
748 start_gc_timer (void * hook_data SCM_UNUSED,
749 void *fn_data SCM_UNUSED,
750 void *data SCM_UNUSED)
751 {
752 if (!gc_start_time)
753 gc_start_time = scm_c_get_internal_run_time ();
754
755 return NULL;
756 }
757
758 static void *
759 accumulate_gc_timer (void * hook_data SCM_UNUSED,
760 void *fn_data SCM_UNUSED,
761 void *data SCM_UNUSED)
762 {
763 if (gc_start_time)
764 {
765 long now = scm_c_get_internal_run_time ();
766 gc_time_taken += now - gc_start_time;
767 gc_start_time = 0;
768 }
769
770 return NULL;
771 }
772
773 /* Return some idea of the memory footprint of a process, in bytes.
774 Currently only works on Linux systems. */
775 static size_t
776 get_image_size (void)
777 {
778 unsigned long size, resident, share;
779 size_t ret = 0;
780
781 FILE *fp = fopen ("/proc/self/statm", "r");
782
783 if (fp && fscanf (fp, "%lu %lu %lu", &size, &resident, &share) == 3)
784 ret = resident * 4096;
785
786 if (fp)
787 fclose (fp);
788
789 return ret;
790 }
791
792 /* These are discussed later. */
793 static size_t bytes_until_gc;
794 static scm_i_pthread_mutex_t bytes_until_gc_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
795
796 /* Make GC run more frequently when the process image size is growing,
797 measured against the number of bytes allocated through the GC.
798
799 If Guile is allocating at a GC-managed heap size H, libgc will tend
800 to limit the process image size to H*N. But if at the same time the
801 user program is mallocating at a rate M bytes per GC-allocated byte,
802 then the process stabilizes at H*N*M -- assuming that collecting data
803 will result in malloc'd data being freed. It doesn't take a very
804 large M for this to be a bad situation. To limit the image size,
805 Guile should GC more often -- the bigger the M, the more often.
806
807 Numeric functions that produce bigger and bigger integers are
808 pessimal, because M is an increasing function of time. Here is an
809 example of such a function:
810
811 (define (factorial n)
812 (define (fac n acc)
813 (if (<= n 1)
814 acc
815 (fac (1- n) (* n acc))))
816 (fac n 1))
817
818 It is possible for a process to grow for reasons that will not be
819 solved by faster GC. In that case M will be estimated as
820 artificially high for a while, and so GC will happen more often on
821 the Guile side. But when it stabilizes, Guile can ease back the GC
822 frequency.
823
824 The key is to measure process image growth, not mallocation rate.
825 For maximum effectiveness, Guile reacts quickly to process growth,
826 and exponentially backs down when the process stops growing.
827
828 See http://thread.gmane.org/gmane.lisp.guile.devel/12552/focus=12936
829 for further discussion.
830 */
831 static void *
832 adjust_gc_frequency (void * hook_data SCM_UNUSED,
833 void *fn_data SCM_UNUSED,
834 void *data SCM_UNUSED)
835 {
836 static size_t prev_image_size = 0;
837 static size_t prev_bytes_alloced = 0;
838 size_t image_size;
839 size_t bytes_alloced;
840
841 scm_i_pthread_mutex_lock (&bytes_until_gc_lock);
842 bytes_until_gc = GC_get_heap_size ();
843 scm_i_pthread_mutex_unlock (&bytes_until_gc_lock);
844
845 image_size = get_image_size ();
846 bytes_alloced = GC_get_total_bytes ();
847
848 #define HEURISTICS_DEBUG 0
849
850 #if HEURISTICS_DEBUG
851 fprintf (stderr, "prev image / alloced: %lu / %lu\n", prev_image_size, prev_bytes_alloced);
852 fprintf (stderr, " image / alloced: %lu / %lu\n", image_size, bytes_alloced);
853 fprintf (stderr, "divisor %lu / %f\n", free_space_divisor, target_free_space_divisor);
854 #endif
855
856 if (prev_image_size && bytes_alloced != prev_bytes_alloced)
857 {
858 double growth_rate, new_target_free_space_divisor;
859 double decay_factor = 0.5;
860 double hysteresis = 0.1;
861
862 growth_rate = ((double) image_size - prev_image_size)
863 / ((double)bytes_alloced - prev_bytes_alloced);
864
865 #if HEURISTICS_DEBUG
866 fprintf (stderr, "growth rate %f\n", growth_rate);
867 #endif
868
869 new_target_free_space_divisor = minimum_free_space_divisor;
870
871 if (growth_rate > 0)
872 new_target_free_space_divisor *= 1.0 + growth_rate;
873
874 #if HEURISTICS_DEBUG
875 fprintf (stderr, "new divisor %f\n", new_target_free_space_divisor);
876 #endif
877
878 if (new_target_free_space_divisor < target_free_space_divisor)
879 /* Decay down. */
880 target_free_space_divisor =
881 (decay_factor * target_free_space_divisor
882 + (1.0 - decay_factor) * new_target_free_space_divisor);
883 else
884 /* Jump up. */
885 target_free_space_divisor = new_target_free_space_divisor;
886
887 #if HEURISTICS_DEBUG
888 fprintf (stderr, "new target divisor %f\n", target_free_space_divisor);
889 #endif
890
891 if (free_space_divisor + 0.5 + hysteresis < target_free_space_divisor
892 || free_space_divisor - 0.5 - hysteresis > target_free_space_divisor)
893 {
894 free_space_divisor = lround (target_free_space_divisor);
895 #if HEURISTICS_DEBUG
896 fprintf (stderr, "new divisor %lu\n", free_space_divisor);
897 #endif
898 GC_set_free_space_divisor (free_space_divisor);
899 }
900 }
901
902 prev_image_size = image_size;
903 prev_bytes_alloced = bytes_alloced;
904
905 return NULL;
906 }
907
908 /* The adjust_gc_frequency routine handles transients in the process
909 image size. It can't handle instense non-GC-managed steady-state
910 allocation though, as it decays the FSD at steady-state down to its
911 minimum value.
912
913 The only real way to handle continuous, high non-GC allocation is to
914 let the GC know about it. This routine can handle non-GC allocation
915 rates that are similar in size to the GC-managed heap size.
916 */
917
918 void
919 scm_gc_register_allocation (size_t size)
920 {
921 scm_i_pthread_mutex_lock (&bytes_until_gc_lock);
922 if (bytes_until_gc - size > bytes_until_gc)
923 {
924 bytes_until_gc = GC_get_heap_size ();
925 scm_i_pthread_mutex_unlock (&bytes_until_gc_lock);
926 GC_gcollect ();
927 }
928 else
929 {
930 bytes_until_gc -= size;
931 scm_i_pthread_mutex_unlock (&bytes_until_gc_lock);
932 }
933 }
934
935
936 \f
937
938 char const *
939 scm_i_tag_name (scm_t_bits tag)
940 {
941 switch (tag & 0x7f) /* 7 bits */
942 {
943 case scm_tcs_struct:
944 return "struct";
945 case scm_tcs_cons_imcar:
946 return "cons (immediate car)";
947 case scm_tcs_cons_nimcar:
948 return "cons (non-immediate car)";
949 case scm_tc7_pointer:
950 return "foreign";
951 case scm_tc7_hashtable:
952 return "hashtable";
953 case scm_tc7_weak_set:
954 return "weak-set";
955 case scm_tc7_weak_table:
956 return "weak-table";
957 case scm_tc7_fluid:
958 return "fluid";
959 case scm_tc7_dynamic_state:
960 return "dynamic state";
961 case scm_tc7_frame:
962 return "frame";
963 case scm_tc7_objcode:
964 return "objcode";
965 case scm_tc7_vm:
966 return "vm";
967 case scm_tc7_vm_cont:
968 return "vm continuation";
969 case scm_tc7_wvect:
970 return "weak vector";
971 case scm_tc7_vector:
972 return "vector";
973 case scm_tc7_number:
974 switch (tag)
975 {
976 case scm_tc16_real:
977 return "real";
978 break;
979 case scm_tc16_big:
980 return "bignum";
981 break;
982 case scm_tc16_complex:
983 return "complex number";
984 break;
985 case scm_tc16_fraction:
986 return "fraction";
987 break;
988 }
989 break;
990 case scm_tc7_string:
991 return "string";
992 break;
993 case scm_tc7_stringbuf:
994 return "string buffer";
995 break;
996 case scm_tc7_symbol:
997 return "symbol";
998 break;
999 case scm_tc7_variable:
1000 return "variable";
1001 break;
1002 case scm_tc7_port:
1003 return "port";
1004 break;
1005 case scm_tc7_smob:
1006 {
1007 int k = 0xff & (tag >> 8);
1008 return (scm_smobs[k].name);
1009 }
1010 break;
1011 }
1012
1013 return NULL;
1014 }
1015
1016
1017
1018 \f
1019 void
1020 scm_init_gc ()
1021 {
1022 /* `GC_INIT ()' was invoked in `scm_storage_prehistory ()'. */
1023
1024 scm_after_gc_hook = scm_make_hook (SCM_INUM0);
1025 scm_c_define ("after-gc-hook", scm_after_gc_hook);
1026
1027 /* When the async is to run, the cdr of the gc_async pair gets set to
1028 the asyncs queue of the current thread. */
1029 after_gc_async_cell = scm_cons (scm_c_make_gsubr ("%after-gc-thunk", 0, 0, 0,
1030 after_gc_async_thunk),
1031 SCM_BOOL_F);
1032
1033 scm_c_hook_add (&scm_before_gc_c_hook, queue_after_gc_hook, NULL, 0);
1034 scm_c_hook_add (&scm_before_gc_c_hook, start_gc_timer, NULL, 0);
1035 scm_c_hook_add (&scm_after_gc_c_hook, accumulate_gc_timer, NULL, 0);
1036
1037 #if HAVE_GC_GET_HEAP_USAGE_SAFE
1038 /* GC_get_heap_usage does not take a lock, and so can run in the GC
1039 start hook. */
1040 scm_c_hook_add (&scm_before_gc_c_hook, adjust_gc_frequency, NULL, 0);
1041 #else
1042 /* GC_get_heap_usage might take a lock (and did from 7.2alpha1 to
1043 7.2alpha7), so call it in the after_gc_hook. */
1044 scm_c_hook_add (&scm_after_gc_c_hook, adjust_gc_frequency, NULL, 0);
1045 #endif
1046
1047 #ifdef HAVE_GC_SET_START_CALLBACK
1048 GC_set_start_callback (run_before_gc_c_hook);
1049 #endif
1050
1051 #include "libguile/gc.x"
1052 }
1053
1054
1055 void
1056 scm_gc_sweep (void)
1057 #define FUNC_NAME "scm_gc_sweep"
1058 {
1059 /* FIXME */
1060 fprintf (stderr, "%s: doing nothing\n", FUNC_NAME);
1061 }
1062 #undef FUNC_NAME
1063
1064 /*
1065 Local Variables:
1066 c-file-style: "gnu"
1067 End:
1068 */