gc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /* Copyright 1995-2003,2006,2008-2014,2016-2018,2020
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. /* #define DEBUGINFO */
  16. #ifdef HAVE_CONFIG_H
  17. # include <config.h>
  18. #endif
  19. #include <stdio.h>
  20. #include <errno.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <math.h>
  24. #include <unistd.h>
  25. #include "arrays.h"
  26. #include "async.h"
  27. #include "bdw-gc.h"
  28. #include "deprecation.h"
  29. #include "dynwind.h"
  30. #include "eval.h"
  31. #include "gen-scmconfig.h"
  32. #include "gsubr.h"
  33. #include "hashtab.h"
  34. #include "hooks.h"
  35. #include "list.h"
  36. #include "modules.h"
  37. #include "numbers.h"
  38. #include "pairs.h"
  39. #include "ports.h"
  40. #include "simpos.h"
  41. #include "smob.h"
  42. #include "stackchk.h"
  43. #include "stime.h"
  44. #include "strings.h"
  45. #include "struct.h"
  46. #include "symbols.h"
  47. #include "vectors.h"
  48. #ifdef GUILE_DEBUG_MALLOC
  49. #include "debug-malloc.h"
  50. #endif
  51. #include "gc.h"
  52. /* For GC_set_start_callback. */
  53. #include <gc/gc_mark.h>
  54. /* Size in bytes of the initial heap. This should be about the size of
  55. result of 'guile -c "(display (assq-ref (gc-stats)
  56. 'heap-total-allocated))"'. */
  57. #define DEFAULT_INITIAL_HEAP_SIZE (256 * 1024 * SIZEOF_UINTPTR_T)
  58. /* Set this to != 0 if every cell that is accessed shall be checked:
  59. */
  60. int scm_debug_cell_accesses_p = 0;
  61. int scm_expensive_debug_cell_accesses_p = 0;
  62. /* Set this to 0 if no additional gc's shall be performed, otherwise set it to
  63. * the number of cell accesses after which a gc shall be called.
  64. */
  65. int scm_debug_cells_gc_interval = 0;
  66. /* Hash table that keeps a reference to objects the user wants to protect from
  67. garbage collection. */
  68. static SCM scm_protects;
  69. static int needs_gc_after_nonlocal_exit = 0;
  70. /* Arrange to throw an exception on failed allocations. */
  71. static void*
  72. scm_oom_fn (size_t nbytes)
  73. {
  74. needs_gc_after_nonlocal_exit = 1;
  75. scm_report_out_of_memory ();
  76. return NULL;
  77. }
  78. /* Called within GC -- cannot allocate GC memory. */
  79. static void
  80. scm_gc_warn_proc (char *fmt, GC_word arg)
  81. {
  82. /* avoid scm_current_warning_port() b/c the GC lock is already taken
  83. and the fluid ref might require it */
  84. fprintf (stderr, fmt, arg);
  85. }
  86. void
  87. scm_gc_after_nonlocal_exit (void)
  88. {
  89. if (needs_gc_after_nonlocal_exit)
  90. {
  91. needs_gc_after_nonlocal_exit = 0;
  92. GC_gcollect_and_unmap ();
  93. }
  94. }
  95. /* Hooks. */
  96. scm_t_c_hook scm_before_gc_c_hook;
  97. scm_t_c_hook scm_before_mark_c_hook;
  98. scm_t_c_hook scm_before_sweep_c_hook;
  99. scm_t_c_hook scm_after_sweep_c_hook;
  100. scm_t_c_hook scm_after_gc_c_hook;
  101. static void
  102. run_before_gc_c_hook (void)
  103. {
  104. if (!SCM_I_CURRENT_THREAD)
  105. /* GC while a thread is spinning up; punt. */
  106. return;
  107. scm_c_hook_run (&scm_before_gc_c_hook, NULL);
  108. }
  109. /* GC Statistics Keeping
  110. */
  111. unsigned long scm_gc_ports_collected = 0;
  112. static long gc_time_taken = 0;
  113. static long gc_start_time = 0;
  114. static unsigned long protected_obj_count = 0;
  115. SCM_SYMBOL (sym_gc_time_taken, "gc-time-taken");
  116. SCM_SYMBOL (sym_heap_size, "heap-size");
  117. SCM_SYMBOL (sym_heap_free_size, "heap-free-size");
  118. SCM_SYMBOL (sym_heap_total_allocated, "heap-total-allocated");
  119. SCM_SYMBOL (sym_heap_allocated_since_gc, "heap-allocated-since-gc");
  120. SCM_SYMBOL (sym_protected_objects, "protected-objects");
  121. SCM_SYMBOL (sym_times, "gc-times");
  122. /* {Scheme Interface to GC}
  123. */
  124. extern int scm_gc_malloc_yield_percentage;
  125. SCM_DEFINE (scm_gc_stats, "gc-stats", 0, 0, 0,
  126. (),
  127. "Return an association list of statistics about Guile's current\n"
  128. "use of storage.\n")
  129. #define FUNC_NAME s_scm_gc_stats
  130. {
  131. SCM answer;
  132. GC_word heap_size, free_bytes, unmapped_bytes, bytes_since_gc, total_bytes;
  133. size_t gc_times;
  134. GC_get_heap_usage_safe (&heap_size, &free_bytes, &unmapped_bytes,
  135. &bytes_since_gc, &total_bytes);
  136. gc_times = GC_get_gc_no ();
  137. answer =
  138. scm_list_n (scm_cons (sym_gc_time_taken, scm_from_long (gc_time_taken)),
  139. scm_cons (sym_heap_size, scm_from_size_t (heap_size)),
  140. scm_cons (sym_heap_free_size, scm_from_size_t (free_bytes)),
  141. scm_cons (sym_heap_total_allocated,
  142. scm_from_size_t (total_bytes)),
  143. scm_cons (sym_heap_allocated_since_gc,
  144. scm_from_size_t (bytes_since_gc)),
  145. scm_cons (sym_protected_objects,
  146. scm_from_ulong (protected_obj_count)),
  147. scm_cons (sym_times, scm_from_size_t (gc_times)),
  148. SCM_UNDEFINED);
  149. return answer;
  150. }
  151. #undef FUNC_NAME
  152. SCM_DEFINE (scm_gc_dump, "gc-dump", 0, 0, 0,
  153. (void),
  154. "Dump information about the garbage collector's internal data "
  155. "structures and memory usage to the standard output.")
  156. #define FUNC_NAME s_scm_gc_dump
  157. {
  158. GC_dump ();
  159. return SCM_UNSPECIFIED;
  160. }
  161. #undef FUNC_NAME
  162. SCM_DEFINE (scm_object_address, "object-address", 1, 0, 0,
  163. (SCM obj),
  164. "Return an integer that for the lifetime of @var{obj} is uniquely\n"
  165. "returned by this function for @var{obj}")
  166. #define FUNC_NAME s_scm_object_address
  167. {
  168. return scm_from_ulong (SCM_UNPACK (obj));
  169. }
  170. #undef FUNC_NAME
  171. SCM_DEFINE (scm_gc_disable, "gc-disable", 0, 0, 0,
  172. (),
  173. "Disables the garbage collector. Nested calls are permitted. "
  174. "GC is re-enabled once @code{gc-enable} has been called the "
  175. "same number of times @code{gc-disable} was called.")
  176. #define FUNC_NAME s_scm_gc_disable
  177. {
  178. GC_disable ();
  179. return SCM_UNSPECIFIED;
  180. }
  181. #undef FUNC_NAME
  182. SCM_DEFINE (scm_gc_enable, "gc-enable", 0, 0, 0,
  183. (),
  184. "Enables the garbage collector.")
  185. #define FUNC_NAME s_scm_gc_enable
  186. {
  187. GC_enable ();
  188. return SCM_UNSPECIFIED;
  189. }
  190. #undef FUNC_NAME
  191. SCM_DEFINE (scm_gc, "gc", 0, 0, 0,
  192. (),
  193. "Scans all of SCM objects and reclaims for further use those that are\n"
  194. "no longer accessible.")
  195. #define FUNC_NAME s_scm_gc
  196. {
  197. scm_i_gc ("call");
  198. /* If you're calling scm_gc(), you probably want synchronous
  199. finalization. */
  200. GC_invoke_finalizers ();
  201. return SCM_UNSPECIFIED;
  202. }
  203. #undef FUNC_NAME
  204. void
  205. scm_i_gc (const char *what)
  206. {
  207. GC_gcollect ();
  208. }
  209. /* {GC Protection Helper Functions}
  210. */
  211. /*
  212. * If within a function you need to protect one or more scheme objects from
  213. * garbage collection, pass them as parameters to one of the
  214. * scm_remember_upto_here* functions below. These functions don't do
  215. * anything, but since the compiler does not know that they are actually
  216. * no-ops, it will generate code that calls these functions with the given
  217. * parameters. Therefore, you can be sure that the compiler will keep those
  218. * scheme values alive (on the stack or in a register) up to the point where
  219. * scm_remember_upto_here* is called. In other words, place the call to
  220. * scm_remember_upto_here* _behind_ the last code in your function, that
  221. * depends on the scheme object to exist.
  222. *
  223. * Example: We want to make sure that the string object str does not get
  224. * garbage collected during the execution of 'some_function' in the code
  225. * below, because otherwise the characters belonging to str would be freed and
  226. * 'some_function' might access freed memory. To make sure that the compiler
  227. * keeps str alive on the stack or in a register such that it is visible to
  228. * the conservative gc we add the call to scm_remember_upto_here_1 _after_ the
  229. * call to 'some_function'. Note that this would not be necessary if str was
  230. * used anyway after the call to 'some_function'.
  231. * char *chars = scm_i_string_chars (str);
  232. * some_function (chars);
  233. * scm_remember_upto_here_1 (str); // str will be alive up to this point.
  234. */
  235. /* Remove any macro versions of these while defining the functions.
  236. Functions are always included in the library, for upward binary
  237. compatibility and in case combinations of GCC and non-GCC are used. */
  238. #undef scm_remember_upto_here_1
  239. #undef scm_remember_upto_here_2
  240. void
  241. scm_remember_upto_here_1 (SCM obj SCM_UNUSED)
  242. {
  243. /* Empty. Protects a single object from garbage collection. */
  244. }
  245. void
  246. scm_remember_upto_here_2 (SCM obj1 SCM_UNUSED, SCM obj2 SCM_UNUSED)
  247. {
  248. /* Empty. Protects two objects from garbage collection. */
  249. }
  250. void
  251. scm_remember_upto_here (SCM obj SCM_UNUSED, ...)
  252. {
  253. /* Empty. Protects any number of objects from garbage collection. */
  254. }
  255. /*
  256. These crazy functions prevent garbage collection
  257. of arguments after the first argument by
  258. ensuring they remain live throughout the
  259. function because they are used in the last
  260. line of the code block.
  261. It'd be better to have a nice compiler hint to
  262. aid the conservative stack-scanning GC. --03/09/00 gjb */
  263. SCM
  264. scm_return_first (SCM elt, ...)
  265. {
  266. return elt;
  267. }
  268. int
  269. scm_return_first_int (int i, ...)
  270. {
  271. return i;
  272. }
  273. SCM
  274. scm_permanent_object (SCM obj)
  275. {
  276. return (scm_gc_protect_object (obj));
  277. }
  278. /* Protect OBJ from the garbage collector. OBJ will not be freed, even if all
  279. other references are dropped, until the object is unprotected by calling
  280. scm_gc_unprotect_object (OBJ). Calls to scm_gc_protect/unprotect_object nest,
  281. i. e. it is possible to protect the same object several times, but it is
  282. necessary to unprotect the object the same number of times to actually get
  283. the object unprotected. It is an error to unprotect an object more often
  284. than it has been protected before. The function scm_protect_object returns
  285. OBJ.
  286. */
  287. /* Implementation note: For every object X, there is a counter which
  288. scm_gc_protect_object (X) increments and scm_gc_unprotect_object (X) decrements.
  289. */
  290. static scm_i_pthread_mutex_t gc_protect_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
  291. SCM
  292. scm_gc_protect_object (SCM obj)
  293. {
  294. SCM handle;
  295. scm_dynwind_begin (0);
  296. scm_i_dynwind_pthread_mutex_lock (&gc_protect_lock);
  297. handle = scm_hashq_create_handle_x (scm_protects, obj, scm_from_int (0));
  298. SCM_SETCDR (handle, scm_sum (SCM_CDR (handle), scm_from_int (1)));
  299. protected_obj_count ++;
  300. scm_dynwind_end ();
  301. return obj;
  302. }
  303. /* Remove any protection for OBJ established by a prior call to
  304. scm_protect_object. This function returns OBJ.
  305. See scm_protect_object for more information. */
  306. SCM
  307. scm_gc_unprotect_object (SCM obj)
  308. {
  309. SCM handle;
  310. scm_dynwind_begin (0);
  311. scm_i_dynwind_pthread_mutex_lock (&gc_protect_lock);
  312. handle = scm_hashq_get_handle (scm_protects, obj);
  313. if (scm_is_false (handle))
  314. {
  315. fprintf (stderr, "scm_unprotect_object called on unprotected object\n");
  316. abort ();
  317. }
  318. else
  319. {
  320. SCM count = scm_difference (SCM_CDR (handle), scm_from_int (1));
  321. if (scm_is_eq (count, scm_from_int (0)))
  322. scm_hashq_remove_x (scm_protects, obj);
  323. else
  324. SCM_SETCDR (handle, count);
  325. }
  326. protected_obj_count --;
  327. scm_dynwind_end ();
  328. return obj;
  329. }
  330. void
  331. scm_gc_register_root (SCM *p)
  332. {
  333. /* Nothing. */
  334. }
  335. void
  336. scm_gc_unregister_root (SCM *p)
  337. {
  338. /* Nothing. */
  339. }
  340. void
  341. scm_gc_register_roots (SCM *b, unsigned long n)
  342. {
  343. SCM *p = b;
  344. for (; p < b + n; ++p)
  345. scm_gc_register_root (p);
  346. }
  347. void
  348. scm_gc_unregister_roots (SCM *b, unsigned long n)
  349. {
  350. SCM *p = b;
  351. for (; p < b + n; ++p)
  352. scm_gc_unregister_root (p);
  353. }
  354. void
  355. scm_storage_prehistory ()
  356. {
  357. GC_set_all_interior_pointers (0);
  358. GC_set_finalize_on_demand (1);
  359. #if (GC_VERSION_MAJOR == 7 && GC_VERSION_MINOR == 4 \
  360. && GC_VERSION_MICRO == 0)
  361. /* BDW-GC 7.4.0 has a bug making it loop indefinitely when using more
  362. than one marker thread: <https://github.com/ivmai/bdwgc/pull/30>.
  363. Work around it by asking for one marker thread. */
  364. setenv ("GC_MARKERS", "1", 1);
  365. #endif
  366. #if SCM_I_GSC_USE_NULL_THREADS
  367. /* If we have disabled threads in Guile, ensure that the GC doesn't
  368. spawn any marker threads. */
  369. setenv ("GC_MARKERS", "1", 1);
  370. #endif
  371. GC_INIT ();
  372. size_t heap_size = GC_get_heap_size ();
  373. if (heap_size < DEFAULT_INITIAL_HEAP_SIZE)
  374. GC_expand_hp (DEFAULT_INITIAL_HEAP_SIZE - heap_size);
  375. /* We only need to register a displacement for those types for which the
  376. higher bits of the type tag are used to store a pointer (that is, a
  377. pointer to an 8-octet aligned region). */
  378. GC_REGISTER_DISPLACEMENT (scm_tc3_cons);
  379. GC_REGISTER_DISPLACEMENT (scm_tc3_struct);
  380. /* GC_REGISTER_DISPLACEMENT (scm_tc3_unused); */
  381. /* Sanity check. */
  382. if (!GC_is_visible (&scm_protects))
  383. abort ();
  384. scm_c_hook_init (&scm_before_gc_c_hook, 0, SCM_C_HOOK_NORMAL);
  385. scm_c_hook_init (&scm_before_mark_c_hook, 0, SCM_C_HOOK_NORMAL);
  386. scm_c_hook_init (&scm_before_sweep_c_hook, 0, SCM_C_HOOK_NORMAL);
  387. scm_c_hook_init (&scm_after_sweep_c_hook, 0, SCM_C_HOOK_NORMAL);
  388. scm_c_hook_init (&scm_after_gc_c_hook, 0, SCM_C_HOOK_NORMAL);
  389. }
  390. void
  391. scm_init_gc_protect_object ()
  392. {
  393. scm_protects = scm_c_make_hash_table (31);
  394. #if 0
  395. /* We can't have a cleanup handler since we have no thread to run it
  396. in. */
  397. #ifdef HAVE_ATEXIT
  398. atexit (cleanup);
  399. #else
  400. #ifdef HAVE_ON_EXIT
  401. on_exit (cleanup, 0);
  402. #endif
  403. #endif
  404. #endif
  405. }
  406. SCM scm_after_gc_hook;
  407. static SCM after_gc_async_cell;
  408. /* The function after_gc_async_thunk causes the execution of the
  409. * after-gc-hook. It is run after the gc, as soon as the asynchronous
  410. * events are handled by the evaluator.
  411. */
  412. static SCM
  413. after_gc_async_thunk (void)
  414. {
  415. /* Fun, no? Hook-run *and* run-hook? */
  416. scm_c_hook_run (&scm_after_gc_c_hook, NULL);
  417. scm_c_run_hook (scm_after_gc_hook, SCM_EOL);
  418. return SCM_UNSPECIFIED;
  419. }
  420. /* The function queue_after_gc_hook is run by the scm_before_gc_c_hook
  421. * at the end of the garbage collection. The only purpose of this
  422. * function is to mark the after_gc_async (which will eventually lead to
  423. * the execution of the after_gc_async_thunk).
  424. */
  425. static void *
  426. queue_after_gc_hook (void * hook_data SCM_UNUSED,
  427. void *fn_data SCM_UNUSED,
  428. void *data SCM_UNUSED)
  429. {
  430. scm_thread *t = SCM_I_CURRENT_THREAD;
  431. if (scm_is_false (SCM_CDR (after_gc_async_cell)))
  432. {
  433. SCM_SETCDR (after_gc_async_cell, t->pending_asyncs);
  434. t->pending_asyncs = after_gc_async_cell;
  435. }
  436. return NULL;
  437. }
  438. static void *
  439. start_gc_timer (void * hook_data SCM_UNUSED,
  440. void *fn_data SCM_UNUSED,
  441. void *data SCM_UNUSED)
  442. {
  443. if (!gc_start_time)
  444. gc_start_time = scm_c_get_internal_run_time ();
  445. return NULL;
  446. }
  447. static void *
  448. accumulate_gc_timer (void * hook_data SCM_UNUSED,
  449. void *fn_data SCM_UNUSED,
  450. void *data SCM_UNUSED)
  451. {
  452. if (gc_start_time)
  453. {
  454. long now = scm_c_get_internal_run_time ();
  455. gc_time_taken += now - gc_start_time;
  456. gc_start_time = 0;
  457. }
  458. return NULL;
  459. }
  460. static size_t bytes_until_gc = DEFAULT_INITIAL_HEAP_SIZE;
  461. static scm_i_pthread_mutex_t bytes_until_gc_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
  462. void
  463. scm_gc_register_allocation (size_t size)
  464. {
  465. scm_i_pthread_mutex_lock (&bytes_until_gc_lock);
  466. if (bytes_until_gc - size > bytes_until_gc)
  467. {
  468. bytes_until_gc = GC_get_heap_size ();
  469. scm_i_pthread_mutex_unlock (&bytes_until_gc_lock);
  470. GC_gcollect ();
  471. }
  472. else
  473. {
  474. bytes_until_gc -= size;
  475. scm_i_pthread_mutex_unlock (&bytes_until_gc_lock);
  476. }
  477. }
  478. void
  479. scm_init_gc ()
  480. {
  481. /* `GC_INIT ()' was invoked in `scm_storage_prehistory ()'. */
  482. scm_after_gc_hook = scm_make_hook (SCM_INUM0);
  483. scm_c_define ("after-gc-hook", scm_after_gc_hook);
  484. /* When the async is to run, the cdr of the gc_async pair gets set to
  485. the asyncs queue of the current thread. */
  486. after_gc_async_cell = scm_cons (scm_c_make_gsubr ("%after-gc-thunk", 0, 0, 0,
  487. after_gc_async_thunk),
  488. SCM_BOOL_F);
  489. scm_c_hook_add (&scm_before_gc_c_hook, queue_after_gc_hook, NULL, 0);
  490. scm_c_hook_add (&scm_before_gc_c_hook, start_gc_timer, NULL, 0);
  491. scm_c_hook_add (&scm_after_gc_c_hook, accumulate_gc_timer, NULL, 0);
  492. GC_set_oom_fn (scm_oom_fn);
  493. GC_set_warn_proc (scm_gc_warn_proc);
  494. GC_set_start_callback (run_before_gc_c_hook);
  495. #include "gc.x"
  496. }
  497. void
  498. scm_gc_sweep (void)
  499. #define FUNC_NAME "scm_gc_sweep"
  500. {
  501. /* FIXME */
  502. fprintf (stderr, "%s: doing nothing\n", FUNC_NAME);
  503. }
  504. #undef FUNC_NAME