gc-segment.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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 <string.h>
  23. #include "libguile/_scm.h"
  24. #include "libguile/pairs.h"
  25. #include "libguile/gc.h"
  26. #include "libguile/private-gc.h"
  27. size_t scm_max_segment_size;
  28. scm_t_heap_segment *
  29. scm_i_make_empty_heap_segment (scm_t_cell_type_statistics *fl)
  30. {
  31. scm_t_heap_segment * shs = malloc (sizeof (scm_t_heap_segment));
  32. if (!shs)
  33. {
  34. fprintf (stderr, "scm_i_get_new_heap_segment: out of memory.\n");
  35. abort ();
  36. }
  37. shs->bounds[0] = NULL;
  38. shs->bounds[1] = NULL;
  39. shs->malloced = NULL;
  40. shs->span = fl->span;
  41. shs->freelist = fl;
  42. shs->next_free_card = NULL;
  43. return shs;
  44. }
  45. void
  46. scm_i_heap_segment_statistics (scm_t_heap_segment *seg, SCM tab)
  47. {
  48. scm_t_cell *p = seg->bounds[0];
  49. while (p < seg->bounds[1])
  50. {
  51. scm_i_card_statistics (p, tab, seg);
  52. p += SCM_GC_CARD_N_CELLS;
  53. }
  54. }
  55. /*
  56. Fill SEGMENT with memory both for data and mark bits.
  57. RETURN: 1 on success, 0 failure
  58. */
  59. int
  60. scm_i_initialize_heap_segment_data (scm_t_heap_segment * segment, size_t requested)
  61. {
  62. /*
  63. round upwards
  64. */
  65. int card_data_cell_count = (SCM_GC_CARD_N_CELLS - SCM_GC_CARD_N_HEADER_CELLS);
  66. int card_count =1 + (requested / sizeof (scm_t_cell)) / card_data_cell_count;
  67. /*
  68. one card extra due to alignment
  69. */
  70. size_t mem_needed = (1+card_count) * SCM_GC_SIZEOF_CARD
  71. + SCM_GC_CARD_BVEC_SIZE_IN_LONGS * card_count * SCM_SIZEOF_LONG
  72. ;
  73. scm_t_c_bvec_long * bvec_ptr = 0;
  74. scm_t_cell * memory = 0;
  75. /*
  76. We use calloc to alloc the heap. On GNU libc this is
  77. equivalent to mmapping /dev/zero
  78. */
  79. SCM_SYSCALL (memory = (scm_t_cell * ) calloc (1, mem_needed));
  80. if (memory == NULL)
  81. return 0;
  82. segment->malloced = memory;
  83. segment->bounds[0] = SCM_GC_CARD_UP (memory);
  84. segment->bounds[1] = segment->bounds[0] + card_count * SCM_GC_CARD_N_CELLS;
  85. segment->freelist->heap_size += scm_i_segment_cell_count (segment);
  86. bvec_ptr = (scm_t_c_bvec_long*) segment->bounds[1];
  87. /*
  88. Don't init the mem or the bitvector. This is handled by lazy
  89. sweeping.
  90. */
  91. segment->next_free_card = segment->bounds[0];
  92. segment->first_time = 1;
  93. return 1;
  94. }
  95. int
  96. scm_i_segment_card_count (scm_t_heap_segment * seg)
  97. {
  98. return (seg->bounds[1] - seg->bounds[0]) / SCM_GC_CARD_N_CELLS;
  99. }
  100. /*
  101. Return the number of available single-cell data cells.
  102. */
  103. int
  104. scm_i_segment_cell_count (scm_t_heap_segment * seg)
  105. {
  106. return scm_i_segment_card_count (seg) * (SCM_GC_CARD_N_CELLS - SCM_GC_CARD_N_HEADER_CELLS)
  107. + ((seg->span == 2) ? -1 : 0);
  108. }
  109. void
  110. scm_i_clear_segment_mark_space (scm_t_heap_segment *seg)
  111. {
  112. scm_t_cell * markspace = seg->bounds[1];
  113. memset (markspace, 0x00,
  114. scm_i_segment_card_count (seg) * SCM_GC_CARD_BVEC_SIZE_IN_LONGS * SCM_SIZEOF_LONG);
  115. }
  116. /*
  117. Sweep cards from SEG until we've gathered THRESHOLD cells
  118. RETURN:
  119. Freelist.
  120. */
  121. SCM
  122. scm_i_sweep_some_cards (scm_t_heap_segment *seg)
  123. {
  124. SCM cells = SCM_EOL;
  125. int threshold = 512;
  126. int collected = 0;
  127. int (*sweeper) (scm_t_cell *, SCM *, scm_t_heap_segment* )
  128. = (seg->first_time) ? &scm_i_init_card_freelist : &scm_i_sweep_card;
  129. scm_t_cell * next_free = seg->next_free_card;
  130. int cards_swept = 0;
  131. while (collected < threshold && next_free < seg->bounds[1])
  132. {
  133. collected += (*sweeper) (next_free, &cells, seg);
  134. next_free += SCM_GC_CARD_N_CELLS;
  135. cards_swept ++;
  136. }
  137. scm_gc_cells_swept += cards_swept * (SCM_GC_CARD_N_CELLS - SCM_GC_CARD_N_HEADER_CELLS);
  138. scm_gc_cells_collected += collected * seg->span;
  139. if (!seg->first_time)
  140. {
  141. scm_gc_cells_allocated_acc +=
  142. (scm_cells_allocated - scm_last_cells_allocated);
  143. scm_cells_allocated -= collected * seg->span;
  144. scm_last_cells_allocated = scm_cells_allocated;
  145. }
  146. seg->freelist->collected += collected * seg->span;
  147. if(next_free == seg->bounds[1])
  148. {
  149. seg->first_time = 0;
  150. }
  151. seg->next_free_card = next_free;
  152. return cells;
  153. }
  154. /*
  155. Force a sweep of this entire segment. This doesn't modify sweep
  156. statistics, it just frees the memory pointed to by to-be-swept
  157. cells.
  158. Implementation is slightly ugh.
  159. FIXME: if you do scm_i_sweep_segment(), and then allocate from this
  160. segment again, the statistics are off.
  161. */
  162. void
  163. scm_i_sweep_segment (scm_t_heap_segment * seg)
  164. {
  165. scm_t_cell * p = seg->next_free_card;
  166. int yield = scm_gc_cells_collected;
  167. int coll = seg->freelist->collected;
  168. unsigned long alloc = scm_cells_allocated ;
  169. unsigned long last_alloc = scm_last_cells_allocated;
  170. double last_total
  171. = scm_gc_cells_allocated_acc
  172. + (alloc - last_alloc);
  173. while (scm_i_sweep_some_cards (seg) != SCM_EOL)
  174. ;
  175. scm_gc_cells_collected = yield;
  176. /*
  177. * restore old stats.
  178. */
  179. scm_gc_cells_allocated_acc = last_total;
  180. scm_cells_allocated = alloc;
  181. scm_last_cells_allocated = alloc;
  182. seg->freelist->collected = coll;
  183. seg->next_free_card =p;
  184. }
  185. void
  186. scm_i_sweep_all_segments (char const *reason)
  187. {
  188. int i= 0;
  189. for (i = 0; i < scm_i_heap_segment_table_size; i++)
  190. {
  191. scm_i_sweep_segment (scm_i_heap_segment_table[i]);
  192. }
  193. }
  194. /*
  195. Heap segment table.
  196. The table is sorted by the address of the data itself. This makes
  197. for easy lookups. This is not portable: according to ANSI C,
  198. pointers can only be compared within the same object (i.e. the same
  199. block of malloced memory.). For machines with weird architectures,
  200. this should be revised.
  201. (Apparently, for this reason 1.6 and earlier had macros for pointer
  202. comparison. )
  203. perhaps it is worthwhile to remove the 2nd level of indirection in
  204. the table, but this certainly makes for cleaner code.
  205. */
  206. scm_t_heap_segment ** scm_i_heap_segment_table;
  207. size_t scm_i_heap_segment_table_size;
  208. scm_t_cell *lowest_cell;
  209. scm_t_cell *highest_cell;
  210. void
  211. scm_i_clear_mark_space (void)
  212. {
  213. int i = 0;
  214. for (; i < scm_i_heap_segment_table_size; i++)
  215. {
  216. scm_i_clear_segment_mark_space (scm_i_heap_segment_table[i]);
  217. }
  218. }
  219. /*
  220. RETURN: index of inserted segment.
  221. */
  222. int
  223. scm_i_insert_segment (scm_t_heap_segment * seg)
  224. {
  225. size_t size = (scm_i_heap_segment_table_size + 1) * sizeof (scm_t_heap_segment *);
  226. SCM_SYSCALL(scm_i_heap_segment_table = ((scm_t_heap_segment **)
  227. realloc ((char *)scm_i_heap_segment_table, size)));
  228. /*
  229. We can't alloc 4 more bytes. This is hopeless.
  230. */
  231. if (!scm_i_heap_segment_table)
  232. {
  233. fprintf (stderr, "scm_i_get_new_heap_segment: Could not grow heap segment table.\n");
  234. abort ();
  235. }
  236. if (!lowest_cell)
  237. {
  238. lowest_cell = seg->bounds[0];
  239. highest_cell = seg->bounds[1];
  240. }
  241. else
  242. {
  243. lowest_cell = SCM_MIN (lowest_cell, seg->bounds[0]);
  244. highest_cell = SCM_MAX (highest_cell, seg->bounds[1]);
  245. }
  246. {
  247. int i = 0;
  248. int j = 0;
  249. while (i < scm_i_heap_segment_table_size
  250. && scm_i_heap_segment_table[i]->bounds[0] <= seg->bounds[0])
  251. i++;
  252. /*
  253. We insert a new entry; if that happens to be before the
  254. "current" segment of a freelist, we must move the freelist index
  255. as well.
  256. */
  257. if (scm_i_master_freelist.heap_segment_idx >= i)
  258. scm_i_master_freelist.heap_segment_idx ++;
  259. if (scm_i_master_freelist2.heap_segment_idx >= i)
  260. scm_i_master_freelist2.heap_segment_idx ++;
  261. for (j = scm_i_heap_segment_table_size; j > i; --j)
  262. scm_i_heap_segment_table[j] = scm_i_heap_segment_table[j - 1];
  263. scm_i_heap_segment_table [i] = seg;
  264. scm_i_heap_segment_table_size ++;
  265. return i;
  266. }
  267. }
  268. SCM
  269. scm_i_sweep_some_segments (scm_t_cell_type_statistics * fl)
  270. {
  271. int i = fl->heap_segment_idx;
  272. SCM collected = SCM_EOL;
  273. if (i == -1)
  274. i++;
  275. for (;
  276. i < scm_i_heap_segment_table_size; i++)
  277. {
  278. if (scm_i_heap_segment_table[i]->freelist != fl)
  279. continue;
  280. collected = scm_i_sweep_some_cards (scm_i_heap_segment_table[i]);
  281. if (collected != SCM_EOL) /* Don't increment i */
  282. break;
  283. }
  284. fl->heap_segment_idx = i;
  285. return collected;
  286. }
  287. void
  288. scm_i_reset_segments (void)
  289. {
  290. int i = 0;
  291. for (; i < scm_i_heap_segment_table_size; i++)
  292. {
  293. scm_t_heap_segment * seg = scm_i_heap_segment_table[i];
  294. seg->next_free_card = seg->bounds[0];
  295. }
  296. }
  297. /*
  298. Return a hashtab with counts of live objects, with tags as keys.
  299. */
  300. SCM
  301. scm_i_all_segments_statistics (SCM tab)
  302. {
  303. int i = 0;
  304. for (; i < scm_i_heap_segment_table_size; i++)
  305. {
  306. scm_t_heap_segment * seg = scm_i_heap_segment_table[i];
  307. scm_i_heap_segment_statistics (seg, tab);
  308. }
  309. return tab;
  310. }
  311. /*
  312. Determine whether the given value does actually represent a cell in
  313. some heap segment. If this is the case, the number of the heap
  314. segment is returned. Otherwise, -1 is returned. Binary search is
  315. used to determine the heap segment that contains the cell.
  316. I think this function is too long to be inlined. --hwn
  317. */
  318. long int
  319. scm_i_find_heap_segment_containing_object (SCM obj)
  320. {
  321. if (!CELL_P (obj))
  322. return -1;
  323. if ((scm_t_cell* ) obj < lowest_cell || (scm_t_cell*) obj >= highest_cell)
  324. return -1;
  325. {
  326. scm_t_cell * ptr = SCM2PTR (obj);
  327. unsigned long int i = 0;
  328. unsigned long int j = scm_i_heap_segment_table_size - 1;
  329. if (ptr < scm_i_heap_segment_table[i]->bounds[0])
  330. return -1;
  331. else if (scm_i_heap_segment_table[j]->bounds[1] <= ptr)
  332. return -1;
  333. else
  334. {
  335. while (i < j)
  336. {
  337. if (ptr < scm_i_heap_segment_table[i]->bounds[1])
  338. {
  339. break;
  340. }
  341. else if (scm_i_heap_segment_table[j]->bounds[0] <= ptr)
  342. {
  343. i = j;
  344. break;
  345. }
  346. else
  347. {
  348. unsigned long int k = (i + j) / 2;
  349. if (k == i)
  350. return -1;
  351. else if (ptr < scm_i_heap_segment_table[k]->bounds[1])
  352. {
  353. j = k;
  354. ++i;
  355. if (ptr < scm_i_heap_segment_table[i]->bounds[0])
  356. return -1;
  357. }
  358. else if (scm_i_heap_segment_table[k]->bounds[0] <= ptr)
  359. {
  360. i = k;
  361. --j;
  362. if (scm_i_heap_segment_table[j]->bounds[1] <= ptr)
  363. return -1;
  364. }
  365. }
  366. }
  367. if (!SCM_DOUBLECELL_ALIGNED_P (obj) && scm_i_heap_segment_table[i]->span == 2)
  368. return -1;
  369. else if (SCM_GC_IN_CARD_HEADERP (ptr))
  370. return -1;
  371. else
  372. return i;
  373. }
  374. }
  375. }
  376. /*
  377. Important entry point: try to grab some memory, and make it into a
  378. segment.
  379. RETURN: the index of the segment.
  380. */
  381. int
  382. scm_i_get_new_heap_segment (scm_t_cell_type_statistics *freelist,
  383. policy_on_error error_policy)
  384. {
  385. size_t len;
  386. {
  387. /* Assure that the new segment is predicted to be large enough.
  388. *
  389. * New yield should at least equal GC fraction of new heap size, i.e.
  390. *
  391. * y + dh > f * (h + dh)
  392. *
  393. * y : yield
  394. * f : min yield fraction
  395. * h : heap size
  396. * dh : size of new heap segment
  397. *
  398. * This gives dh > (f * h - y) / (1 - f)
  399. */
  400. float f = freelist->min_yield_fraction / 100.0;
  401. float h = SCM_HEAP_SIZE;
  402. float min_cells
  403. = (f * h - scm_gc_cells_collected) / (1.0 - f);
  404. /* Make heap grow with factor 1.5 */
  405. len = freelist->heap_size / 2;
  406. #ifdef DEBUGINFO
  407. fprintf (stderr, "(%ld < %ld)", (long) len, (long) min_cells);
  408. #endif
  409. if (len < min_cells)
  410. len = (unsigned long) min_cells;
  411. len *= sizeof (scm_t_cell);
  412. /* force new sampling */
  413. freelist->collected = LONG_MAX;
  414. }
  415. if (len > scm_max_segment_size)
  416. len = scm_max_segment_size;
  417. if (len < SCM_MIN_HEAP_SEG_SIZE)
  418. len = SCM_MIN_HEAP_SEG_SIZE;
  419. {
  420. scm_t_heap_segment * seg = scm_i_make_empty_heap_segment (freelist);
  421. /* Allocate with decaying ambition. */
  422. while (len >= SCM_MIN_HEAP_SEG_SIZE)
  423. {
  424. if (scm_i_initialize_heap_segment_data (seg, len))
  425. {
  426. return scm_i_insert_segment (seg);
  427. }
  428. len /= 2;
  429. }
  430. }
  431. if (error_policy == abort_on_error)
  432. {
  433. fprintf (stderr, "scm_i_get_new_heap_segment: Could not grow heap.\n");
  434. abort ();
  435. }
  436. return -1;
  437. }
  438. void
  439. scm_i_make_initial_segment (int init_heap_size, scm_t_cell_type_statistics *freelist)
  440. {
  441. scm_t_heap_segment * seg = scm_i_make_empty_heap_segment (freelist);
  442. if (init_heap_size < 1)
  443. {
  444. init_heap_size = SCM_DEFAULT_INIT_HEAP_SIZE_1;
  445. }
  446. if (scm_i_initialize_heap_segment_data (seg, init_heap_size))
  447. {
  448. freelist->heap_segment_idx = scm_i_insert_segment (seg);
  449. }
  450. /*
  451. Why the fuck try twice? --hwn
  452. */
  453. if (!seg->malloced)
  454. {
  455. scm_i_initialize_heap_segment_data (seg, SCM_HEAP_SEG_SIZE);
  456. }
  457. if (freelist->min_yield_fraction)
  458. freelist->min_yield = (freelist->heap_size * freelist->min_yield_fraction
  459. / 100);
  460. }