gc-freelist.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2006, 2008 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
  5. * License as published by the Free Software Foundation; either
  6. * version 2.1 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but 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 02110-1301 USA
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include <assert.h>
  21. #include <stdio.h>
  22. #include "libguile/private-gc.h"
  23. #include "libguile/gc.h"
  24. #include "libguile/deprecation.h"
  25. #include "libguile/private-gc.h"
  26. scm_t_cell_type_statistics scm_i_master_freelist;
  27. scm_t_cell_type_statistics scm_i_master_freelist2;
  28. #ifdef __MINGW32__
  29. scm_t_cell_type_statistics *scm_i_master_freelist_ptr = &scm_i_master_freelist;
  30. scm_t_cell_type_statistics *scm_i_master_freelist2_ptr = &scm_i_master_freelist2;
  31. #endif
  32. /*
  33. In older versions of GUILE GC there was extensive support for
  34. debugging freelists. This was useful, since the freelist was kept
  35. inside the heap, and writing to an object that was GC'd would mangle
  36. the list. Mark bits are now separate, and checking for sane cell
  37. access can be done much more easily by simply checking if the mark bit
  38. is unset before allocation. --hwn
  39. */
  40. #if (SCM_ENABLE_DEPRECATED == 1)
  41. #if defined(GUILE_DEBUG_FREELIST)
  42. SCM_DEFINE (scm_map_free_list, "map-free-list", 0, 0, 0,
  43. (),
  44. "DEPRECATED\n")
  45. #define FUNC_NAME "s_scm_map_free_list"
  46. {
  47. scm_c_issue_deprecation_warning ("map-free-list has been removed from GUILE. Doing nothing\n");
  48. return SCM_UNSPECIFIED;
  49. }
  50. #undef FUNC_NAME
  51. SCM_DEFINE (scm_gc_set_debug_check_freelist_x, "gc-set-debug-check-freelist!", 1, 0, 0,
  52. (SCM flag),
  53. "DEPRECATED.\n")
  54. #define FUNC_NAME "s_scm_gc_set_debug_check_freelist_x"
  55. {
  56. scm_c_issue_deprecation_warning ("gc-set-debug-check-freelist! has been removed from GUILE. Doing nothing\n");
  57. return SCM_UNSPECIFIED;
  58. }
  59. #undef FUNC_NAME
  60. #endif /* defined (GUILE_DEBUG) */
  61. #endif /* deprecated */
  62. /*
  63. This adjust FREELIST variables to decide wether or not to allocate
  64. more heap in the next GC run. It uses scm_gc_cells_collected and scm_gc_cells_collected1
  65. */
  66. void
  67. scm_i_adjust_min_yield (scm_t_cell_type_statistics *freelist)
  68. {
  69. /* min yield is adjusted upwards so that next predicted total yield
  70. * (allocated cells actually freed by GC) becomes
  71. * `min_yield_fraction' of total heap size. Note, however, that
  72. * the absolute value of min_yield will correspond to `collected'
  73. * on one master (the one which currently is triggering GC).
  74. *
  75. * The reason why we look at total yield instead of cells collected
  76. * on one list is that we want to take other freelists into account.
  77. * On this freelist, we know that (local) yield = collected cells,
  78. * but that's probably not the case on the other lists.
  79. *
  80. * (We might consider computing a better prediction, for example
  81. * by computing an average over multiple GC:s.)
  82. */
  83. if (freelist->min_yield_fraction)
  84. {
  85. /* Pick largest of last two yields. */
  86. long delta = ((SCM_HEAP_SIZE * freelist->min_yield_fraction / 100)
  87. - (long) SCM_MAX (scm_gc_cells_collected_1, scm_gc_cells_collected));
  88. #ifdef DEBUGINFO
  89. fprintf (stderr, " after GC = %lu, delta = %ld\n",
  90. (unsigned long) scm_cells_allocated,
  91. (long) delta);
  92. #endif
  93. if (delta > 0)
  94. freelist->min_yield += delta;
  95. }
  96. }
  97. static void
  98. scm_init_freelist (scm_t_cell_type_statistics *freelist,
  99. int span,
  100. int min_yield)
  101. {
  102. if (min_yield < 1)
  103. min_yield = 1;
  104. if (min_yield > 99)
  105. min_yield = 99;
  106. freelist->heap_segment_idx = -1;
  107. freelist->min_yield = 0;
  108. freelist->min_yield_fraction = min_yield;
  109. freelist->span = span;
  110. freelist->collected = 0;
  111. freelist->collected_1 = 0;
  112. freelist->heap_size = 0;
  113. }
  114. #if (SCM_ENABLE_DEPRECATED == 1)
  115. size_t scm_default_init_heap_size_1;
  116. int scm_default_min_yield_1;
  117. size_t scm_default_init_heap_size_2;
  118. int scm_default_min_yield_2;
  119. size_t scm_default_max_segment_size;
  120. #endif
  121. void
  122. scm_gc_init_freelist (void)
  123. {
  124. int init_heap_size_1
  125. = scm_getenv_int ("GUILE_INIT_SEGMENT_SIZE_1", SCM_DEFAULT_INIT_HEAP_SIZE_1);
  126. int init_heap_size_2
  127. = scm_getenv_int ("GUILE_INIT_SEGMENT_SIZE_2", SCM_DEFAULT_INIT_HEAP_SIZE_2);
  128. scm_init_freelist (&scm_i_master_freelist2, 2,
  129. scm_getenv_int ("GUILE_MIN_YIELD_2", SCM_DEFAULT_MIN_YIELD_2));
  130. scm_init_freelist (&scm_i_master_freelist, 1,
  131. scm_getenv_int ("GUILE_MIN_YIELD_1", SCM_DEFAULT_MIN_YIELD_1));
  132. scm_max_segment_size = scm_getenv_int ("GUILE_MAX_SEGMENT_SIZE", SCM_DEFAULT_MAX_SEGMENT_SIZE);
  133. if (scm_max_segment_size <= 0)
  134. scm_max_segment_size = SCM_DEFAULT_MAX_SEGMENT_SIZE;
  135. scm_i_make_initial_segment (init_heap_size_1, &scm_i_master_freelist);
  136. scm_i_make_initial_segment (init_heap_size_2, &scm_i_master_freelist2);
  137. #if (SCM_ENABLE_DEPRECATED == 1)
  138. if ( scm_default_init_heap_size_1 ||
  139. scm_default_min_yield_1||
  140. scm_default_init_heap_size_2||
  141. scm_default_min_yield_2||
  142. scm_default_max_segment_size)
  143. {
  144. scm_c_issue_deprecation_warning ("Tuning heap parameters with C variables is deprecated. Use environment variables instead.");
  145. }
  146. #endif
  147. }
  148. void
  149. scm_i_gc_sweep_freelist_reset (scm_t_cell_type_statistics *freelist)
  150. {
  151. freelist->collected_1 = freelist->collected;
  152. freelist->collected = 0;
  153. /*
  154. at the end we simply start with the lowest segment again.
  155. */
  156. freelist->heap_segment_idx = -1;
  157. }
  158. int
  159. scm_i_gc_grow_heap_p (scm_t_cell_type_statistics * freelist)
  160. {
  161. return SCM_MAX (freelist->collected,freelist->collected_1) < freelist->min_yield;
  162. }