2016-02-18 07:11:59 -08:00
|
|
|
/**************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* OCaml */
|
|
|
|
/* */
|
|
|
|
/* Damien Doligez, projet Para, INRIA Rocquencourt */
|
|
|
|
/* */
|
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
|
|
|
/* en Automatique. */
|
|
|
|
/* */
|
|
|
|
/* All rights reserved. This file is distributed under the terms of */
|
|
|
|
/* the GNU Lesser General Public License version 2.1, with the */
|
|
|
|
/* special exception on linking described in the file LICENSE. */
|
|
|
|
/* */
|
|
|
|
/**************************************************************************/
|
1995-08-09 08:06:35 -07:00
|
|
|
|
1998-10-07 12:01:42 -07:00
|
|
|
#include <limits.h>
|
2015-11-20 08:54:26 -08:00
|
|
|
#include <math.h>
|
1998-10-07 12:01:42 -07:00
|
|
|
|
2014-12-27 06:41:49 -08:00
|
|
|
#include "caml/compact.h"
|
|
|
|
#include "caml/custom.h"
|
|
|
|
#include "caml/config.h"
|
|
|
|
#include "caml/fail.h"
|
|
|
|
#include "caml/finalise.h"
|
|
|
|
#include "caml/freelist.h"
|
|
|
|
#include "caml/gc.h"
|
|
|
|
#include "caml/gc_ctrl.h"
|
|
|
|
#include "caml/major_gc.h"
|
|
|
|
#include "caml/misc.h"
|
|
|
|
#include "caml/mlvalues.h"
|
|
|
|
#include "caml/roots.h"
|
|
|
|
#include "caml/weak.h"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2014-05-12 00:29:24 -07:00
|
|
|
#if defined (NATIVE_CODE) && defined (NO_NAKED_POINTERS)
|
|
|
|
#define NATIVE_CODE_AND_NO_NAKED_POINTERS
|
|
|
|
#else
|
|
|
|
#undef NATIVE_CODE_AND_NO_NAKED_POINTERS
|
|
|
|
#endif
|
|
|
|
|
2015-12-23 01:50:41 -08:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
static inline double fmin(double a, double b) {
|
|
|
|
return (a < b) ? a : b;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-09-22 07:21:50 -07:00
|
|
|
uintnat caml_percent_free;
|
2009-11-04 04:25:47 -08:00
|
|
|
uintnat caml_major_heap_increment;
|
2008-01-03 01:37:10 -08:00
|
|
|
CAMLexport char *caml_heap_start;
|
2003-12-31 06:20:40 -08:00
|
|
|
char *caml_gc_sweep_hp;
|
2013-11-21 09:02:55 -08:00
|
|
|
int caml_gc_phase; /* always Phase_mark, Pase_clean,
|
|
|
|
Phase_sweep, or Phase_idle */
|
1995-05-04 03:15:53 -07:00
|
|
|
static value *gray_vals;
|
2003-12-31 06:20:40 -08:00
|
|
|
static value *gray_vals_cur, *gray_vals_end;
|
1995-05-04 03:15:53 -07:00
|
|
|
static asize_t gray_vals_size;
|
|
|
|
static int heap_is_pure; /* The heap is pure if the only gray objects
|
|
|
|
below [markhp] are also in [gray_vals]. */
|
2005-09-22 07:21:50 -07:00
|
|
|
uintnat caml_allocated_words;
|
|
|
|
uintnat caml_dependent_size, caml_dependent_allocated;
|
2004-06-14 08:17:43 -07:00
|
|
|
double caml_extra_heap_resources;
|
2014-12-24 12:18:22 -08:00
|
|
|
uintnat caml_fl_wsz_at_phase_change = 0;
|
2002-05-28 09:57:31 -07:00
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
extern char *caml_fl_merge; /* Defined in freelist.c. */
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
static char *markhp, *chunk, *limit;
|
|
|
|
|
2015-12-25 02:07:13 -08:00
|
|
|
int caml_gc_subphase; /* Subphase_{mark_roots,mark_main,mark_final} */
|
2014-03-07 00:10:12 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Ephemerons:
|
2015-11-29 08:25:21 -08:00
|
|
|
During mark phase the list caml_ephe_list_head of ephemerons
|
|
|
|
is iterated by different pointers that follow the invariants:
|
|
|
|
caml_ephe_list_head ->* ephes_checked_if_pure ->* ephes_to_check ->* null
|
|
|
|
| | |
|
|
|
|
(1) (2) (3)
|
|
|
|
|
|
|
|
At the start of mark phase, (1) and (2) are empty.
|
2014-03-07 00:10:12 -08:00
|
|
|
|
|
|
|
In mark phase:
|
|
|
|
- the ephemerons in (1) have a data alive or none
|
2015-11-29 08:25:21 -08:00
|
|
|
(nb: new ephemerons are added in this part by weak.c)
|
|
|
|
- the ephemerons in (2) have at least a white key or are white
|
|
|
|
if ephe_list_pure is true, otherwise they are in an unknown state and
|
|
|
|
must be checked again.
|
2014-03-07 00:10:12 -08:00
|
|
|
- the ephemerons in (3) are in an unknown state and must be checked
|
|
|
|
|
2015-11-29 08:25:21 -08:00
|
|
|
At the end of mark phase, (3) is empty and ephe_list_pure is true.
|
|
|
|
The ephemeron in (1) and (2) will be cleaned (white keys and datas
|
|
|
|
replaced by none or the ephemeron is removed from the list if it is white)
|
|
|
|
in clean phase.
|
|
|
|
|
|
|
|
In clean phase:
|
|
|
|
caml_ephe_list_head ->* ephes_to_check ->* null
|
|
|
|
| |
|
|
|
|
(1) (3)
|
|
|
|
|
|
|
|
In clean phase, (2) is not used, ephes_to_check is initialized at
|
|
|
|
caml_ephe_list_head:
|
|
|
|
- the ephemerons in (1) are clean.
|
|
|
|
- the ephemerons in (3) should be cleaned or removed if white.
|
2014-03-07 00:10:12 -08:00
|
|
|
|
|
|
|
*/
|
2015-11-29 08:25:21 -08:00
|
|
|
static int ephe_list_pure;
|
|
|
|
/** The ephemerons is pure if since the start of its iteration
|
|
|
|
no value have been darken. */
|
|
|
|
static value *ephes_checked_if_pure;
|
|
|
|
static value *ephes_to_check;
|
1997-02-24 11:24:39 -08:00
|
|
|
|
2015-11-20 08:54:26 -08:00
|
|
|
int caml_major_window = 1;
|
|
|
|
double caml_major_ring[Max_major_window] = { 0. };
|
|
|
|
int caml_major_ring_index = 0;
|
|
|
|
double caml_major_work_credit = 0.0;
|
|
|
|
double caml_gc_clock = 0.0;
|
|
|
|
|
2008-01-11 03:55:36 -08:00
|
|
|
#ifdef DEBUG
|
|
|
|
static unsigned long major_gc_counter = 0;
|
|
|
|
#endif
|
|
|
|
|
2015-07-17 07:31:05 -07:00
|
|
|
void (*caml_major_gc_hook)(void) = NULL;
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void realloc_gray_vals (void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
value *new;
|
|
|
|
|
|
|
|
Assert (gray_vals_cur == gray_vals_end);
|
2014-12-24 12:18:22 -08:00
|
|
|
if (gray_vals_size < caml_stat_heap_wsz / 32){
|
2005-09-22 07:21:50 -07:00
|
|
|
caml_gc_message (0x08, "Growing gray_vals to %"
|
|
|
|
ARCH_INTNAT_PRINTF_FORMAT "uk bytes\n",
|
|
|
|
(intnat) gray_vals_size * sizeof (value) / 512);
|
1995-05-04 03:15:53 -07:00
|
|
|
new = (value *) realloc ((char *) gray_vals,
|
|
|
|
2 * gray_vals_size * sizeof (value));
|
|
|
|
if (new == NULL){
|
2003-12-29 14:15:02 -08:00
|
|
|
caml_gc_message (0x08, "No room for growing gray_vals\n", 0);
|
1995-05-04 03:15:53 -07:00
|
|
|
gray_vals_cur = gray_vals;
|
|
|
|
heap_is_pure = 0;
|
|
|
|
}else{
|
|
|
|
gray_vals = new;
|
|
|
|
gray_vals_cur = gray_vals + gray_vals_size;
|
|
|
|
gray_vals_size *= 2;
|
|
|
|
gray_vals_end = gray_vals + gray_vals_size;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
gray_vals_cur = gray_vals + gray_vals_size / 2;
|
|
|
|
heap_is_pure = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
void caml_darken (value v, value *p /* not used */)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2014-05-12 00:29:24 -07:00
|
|
|
#ifdef NATIVE_CODE_AND_NO_NAKED_POINTERS
|
2015-11-20 08:54:26 -08:00
|
|
|
if (Is_block (v) && !Is_young (v) && Wosize_val (v) > 0) {
|
2014-05-12 00:29:24 -07:00
|
|
|
#else
|
1995-07-10 02:48:27 -07:00
|
|
|
if (Is_block (v) && Is_in_heap (v)) {
|
2014-05-12 00:29:24 -07:00
|
|
|
#endif
|
2005-10-25 09:24:13 -07:00
|
|
|
header_t h = Hd_val (v);
|
|
|
|
tag_t t = Tag_hd (h);
|
|
|
|
if (t == Infix_tag){
|
|
|
|
v -= Infix_offset_val(v);
|
|
|
|
h = Hd_val (v);
|
|
|
|
t = Tag_hd (h);
|
|
|
|
}
|
2015-07-17 07:31:05 -07:00
|
|
|
#ifdef NATIVE_CODE_AND_NO_NAKED_POINTERS
|
|
|
|
/* We insist that naked pointers to outside the heap point to things that
|
|
|
|
look like values with headers coloured black. This isn't always
|
|
|
|
strictly necessary but is essential in certain cases---in particular
|
|
|
|
when the value is allocated in a read-only section. (For the values
|
|
|
|
where it would be safe it is a performance improvement since we avoid
|
|
|
|
putting them on the grey list.) */
|
|
|
|
CAMLassert (Is_in_heap (v) || Is_black_hd (h));
|
|
|
|
#endif
|
2005-10-25 09:24:13 -07:00
|
|
|
CAMLassert (!Is_blue_hd (h));
|
|
|
|
if (Is_white_hd (h)){
|
2013-12-25 11:15:39 -08:00
|
|
|
ephe_list_pure = 0;
|
2005-10-25 09:24:13 -07:00
|
|
|
if (t < No_scan_tag){
|
|
|
|
Hd_val (v) = Grayhd_hd (h);
|
|
|
|
*gray_vals_cur++ = v;
|
|
|
|
if (gray_vals_cur >= gray_vals_end) realloc_gray_vals ();
|
|
|
|
}else{
|
|
|
|
Hd_val (v) = Blackhd_hd (h);
|
|
|
|
}
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void start_cycle (void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2003-12-31 06:20:40 -08:00
|
|
|
Assert (caml_gc_phase == Phase_idle);
|
1995-05-04 03:15:53 -07:00
|
|
|
Assert (gray_vals_cur == gray_vals);
|
2003-12-29 14:15:02 -08:00
|
|
|
caml_gc_message (0x01, "Starting new major GC cycle\n", 0);
|
2015-11-20 08:54:26 -08:00
|
|
|
caml_darken_all_roots_start ();
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_gc_phase = Phase_mark;
|
2013-11-21 09:02:55 -08:00
|
|
|
caml_gc_subphase = Subphase_mark_roots;
|
1995-05-04 03:15:53 -07:00
|
|
|
markhp = NULL;
|
2013-12-25 11:15:39 -08:00
|
|
|
ephe_list_pure = 1;
|
2015-11-29 08:25:21 -08:00
|
|
|
ephes_checked_if_pure = &caml_ephe_list_head;
|
|
|
|
ephes_to_check = &caml_ephe_list_head;
|
1999-11-08 09:05:45 -08:00
|
|
|
#ifdef DEBUG
|
2008-01-11 03:55:36 -08:00
|
|
|
++ major_gc_counter;
|
2004-01-02 11:23:29 -08:00
|
|
|
caml_heap_check ();
|
1999-11-08 09:05:45 -08:00
|
|
|
#endif
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2015-11-20 08:54:26 -08:00
|
|
|
/* We may stop the slice inside values, in order to avoid large latencies
|
|
|
|
on large arrays. In this case, [current_value] is the partially-marked
|
|
|
|
value and [current_index] is the index of the next field to be marked.
|
|
|
|
*/
|
|
|
|
static value current_value = 0;
|
|
|
|
static mlsize_t current_index = 0;
|
|
|
|
|
2014-02-23 06:51:27 -08:00
|
|
|
/* For instrumentation */
|
2015-11-20 08:54:26 -08:00
|
|
|
#ifdef CAML_INSTR
|
|
|
|
#define INSTR(x) x
|
|
|
|
#else
|
|
|
|
#define INSTR(x) /**/
|
|
|
|
#endif
|
|
|
|
|
2014-03-06 10:18:58 -08:00
|
|
|
static void init_sweep_phase(void)
|
|
|
|
{
|
|
|
|
/* Phase_clean is done. */
|
|
|
|
/* Initialise the sweep phase. */
|
|
|
|
caml_gc_sweep_hp = caml_heap_start;
|
|
|
|
caml_fl_init_merge ();
|
|
|
|
caml_gc_phase = Phase_sweep;
|
|
|
|
chunk = caml_heap_start;
|
|
|
|
caml_gc_sweep_hp = chunk;
|
|
|
|
limit = chunk + Chunk_size (chunk);
|
|
|
|
caml_fl_wsz_at_phase_change = caml_fl_cur_wsz;
|
|
|
|
if (caml_major_gc_hook) (*caml_major_gc_hook)();
|
|
|
|
}
|
|
|
|
|
2013-12-25 11:15:39 -08:00
|
|
|
/* auxillary function of mark_slice */
|
2014-02-23 06:51:27 -08:00
|
|
|
static inline value* mark_slice_darken(value *gray_vals_ptr, value v, int i,
|
2013-12-25 11:15:39 -08:00
|
|
|
int in_ephemeron, int *slice_pointers)
|
2014-02-23 06:51:27 -08:00
|
|
|
{
|
|
|
|
value child;
|
|
|
|
header_t chd;
|
|
|
|
|
|
|
|
child = Field (v, i);
|
|
|
|
|
|
|
|
#ifdef NATIVE_CODE_AND_NO_NAKED_POINTERS
|
|
|
|
if (Is_block (child)
|
|
|
|
&& ! Is_young (child)
|
|
|
|
&& Wosize_val (child) > 0 /* Atoms never need to be marked. */
|
|
|
|
/* Closure blocks contain code pointers at offsets that cannot
|
|
|
|
be reliably determined, so we always use the page table when
|
|
|
|
marking such values. */
|
|
|
|
&& (!(Tag_val (v) == Closure_tag || Tag_val (v) == Infix_tag) ||
|
|
|
|
Is_in_heap (child))) {
|
|
|
|
#else
|
|
|
|
if (Is_block (child) && Is_in_heap (child)) {
|
|
|
|
#endif
|
|
|
|
INSTR (++ *slice_pointers;)
|
|
|
|
chd = Hd_val (child);
|
|
|
|
if (Tag_hd (chd) == Forward_tag){
|
|
|
|
value f = Forward_val (child);
|
2013-12-25 11:15:39 -08:00
|
|
|
if ((in_ephemeron && Is_long(f)) ||
|
|
|
|
(Is_block (f)
|
|
|
|
&& (!Is_in_value_area(f) || Tag_val (f) == Forward_tag
|
|
|
|
|| Tag_val (f) == Lazy_tag || Tag_val (f) == Double_tag))){
|
2014-02-23 06:51:27 -08:00
|
|
|
/* Do not short-circuit the pointer. */
|
|
|
|
}else{
|
2013-12-25 11:15:39 -08:00
|
|
|
/* The variable child is not changed because it must be mark alive */
|
2014-02-23 06:51:27 -08:00
|
|
|
Field (v, i) = f;
|
2013-12-25 11:15:39 -08:00
|
|
|
if (Is_block (f) && Is_young (f) && !Is_young (child)){
|
|
|
|
if(in_ephemeron){
|
|
|
|
add_to_ephe_ref_table (&caml_ephe_ref_table, v, i);
|
|
|
|
}else{
|
|
|
|
add_to_ref_table (&caml_ref_table, &Field (v, i));
|
|
|
|
}
|
|
|
|
}
|
2014-02-23 06:51:27 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (Tag_hd(chd) == Infix_tag) {
|
|
|
|
child -= Infix_offset_val(child);
|
|
|
|
chd = Hd_val(child);
|
|
|
|
}
|
|
|
|
#ifdef NATIVE_CODE_AND_NO_NAKED_POINTERS
|
|
|
|
/* See [caml_darken] for a description of this assertion. */
|
|
|
|
CAMLassert (Is_in_heap (child) || Is_black_hd (chd));
|
|
|
|
#endif
|
|
|
|
if (Is_white_hd (chd)){
|
2013-12-25 11:15:39 -08:00
|
|
|
ephe_list_pure = 0;
|
2014-02-23 06:51:27 -08:00
|
|
|
Hd_val (child) = Grayhd_hd (chd);
|
|
|
|
*gray_vals_ptr++ = child;
|
|
|
|
if (gray_vals_ptr >= gray_vals_end) {
|
|
|
|
gray_vals_cur = gray_vals_ptr;
|
|
|
|
realloc_gray_vals ();
|
|
|
|
gray_vals_ptr = gray_vals_cur;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return gray_vals_ptr;
|
|
|
|
}
|
|
|
|
|
2014-03-07 00:10:12 -08:00
|
|
|
static value* mark_ephe_aux (value *gray_vals_ptr, intnat *work,
|
2013-12-25 11:15:39 -08:00
|
|
|
int *slice_pointers)
|
|
|
|
{
|
2015-12-25 13:18:40 -08:00
|
|
|
value v, data, key;
|
2013-12-25 11:15:39 -08:00
|
|
|
header_t hd;
|
|
|
|
mlsize_t size, i;
|
|
|
|
|
2015-11-29 08:25:21 -08:00
|
|
|
v = *ephes_to_check;
|
2013-12-25 11:15:39 -08:00
|
|
|
hd = Hd_val(v);
|
|
|
|
Assert(Tag_val (v) == Abstract_tag);
|
2016-01-21 09:07:55 -08:00
|
|
|
data = Field(v,CAML_EPHE_DATA_OFFSET);
|
2015-12-25 13:18:40 -08:00
|
|
|
if ( data != caml_ephe_none &&
|
|
|
|
Is_block (data) && Is_in_heap (data) && Is_white_val (data)){
|
2013-12-25 11:15:39 -08:00
|
|
|
|
|
|
|
int alive_data = 1;
|
|
|
|
|
|
|
|
/* The liveness of the ephemeron is one of the condition */
|
|
|
|
if (Is_white_hd (hd)) alive_data = 0;
|
|
|
|
|
|
|
|
/* The liveness of the keys not caml_ephe_none is the other condition */
|
|
|
|
size = Wosize_hd (hd);
|
2016-01-21 09:07:55 -08:00
|
|
|
for (i = CAML_EPHE_FIRST_KEY; alive_data && i < size; i++){
|
2015-12-25 13:18:40 -08:00
|
|
|
key = Field (v, i);
|
2013-12-25 11:15:39 -08:00
|
|
|
ephemeron_again:
|
2015-12-25 13:18:40 -08:00
|
|
|
if (key != caml_ephe_none &&
|
|
|
|
Is_block (key) && Is_in_heap (key)){
|
|
|
|
if (Tag_val (key) == Forward_tag){
|
|
|
|
value f = Forward_val (key);
|
|
|
|
if (Is_long (f) ||
|
|
|
|
(Is_block (f) &&
|
|
|
|
(!Is_in_value_area(f) || Tag_val (f) == Forward_tag
|
|
|
|
|| Tag_val (f) == Lazy_tag || Tag_val (f) == Double_tag))){
|
|
|
|
/* Do not short-circuit the pointer. */
|
|
|
|
}else{
|
|
|
|
Field (v, i) = key = f;
|
|
|
|
goto ephemeron_again;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Is_white_val (key)){
|
|
|
|
alive_data = 0;
|
2013-12-25 11:15:39 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-07 00:10:12 -08:00
|
|
|
*work -= Whsize_wosize(i);
|
2013-12-25 11:15:39 -08:00
|
|
|
|
|
|
|
if (alive_data){
|
2016-01-21 09:07:55 -08:00
|
|
|
gray_vals_ptr = mark_slice_darken(gray_vals_ptr,v,
|
|
|
|
CAML_EPHE_DATA_OFFSET,
|
|
|
|
/*in_ephemeron=*/1,
|
2013-12-25 11:15:39 -08:00
|
|
|
slice_pointers);
|
2014-03-07 00:10:12 -08:00
|
|
|
} else { /* not triggered move to the next one */
|
2016-01-21 09:07:55 -08:00
|
|
|
ephes_to_check = &Field(v,CAML_EPHE_LINK_OFFSET);
|
2014-03-07 00:10:12 -08:00
|
|
|
return gray_vals_ptr;
|
2013-12-25 11:15:39 -08:00
|
|
|
}
|
|
|
|
} else { /* a simily weak pointer or an already alive data */
|
|
|
|
*work -= 1;
|
|
|
|
}
|
|
|
|
|
2014-03-07 00:10:12 -08:00
|
|
|
/* all keys black or data none or black
|
|
|
|
move the ephemerons from (3) to the end of (1) */
|
2015-11-29 08:25:21 -08:00
|
|
|
if ( ephes_checked_if_pure == ephes_to_check ) {
|
2014-03-07 00:10:12 -08:00
|
|
|
/* corner case and optim */
|
2016-01-21 09:07:55 -08:00
|
|
|
ephes_checked_if_pure = &Field(v,CAML_EPHE_LINK_OFFSET);
|
2015-11-29 08:25:21 -08:00
|
|
|
ephes_to_check = ephes_checked_if_pure;
|
2014-03-07 00:10:12 -08:00
|
|
|
} else {
|
|
|
|
/* - remove v from the list (3) */
|
2016-01-21 09:07:55 -08:00
|
|
|
*ephes_to_check = Field(v,CAML_EPHE_LINK_OFFSET);
|
2014-03-07 00:10:12 -08:00
|
|
|
/* - insert it at the end of (1) */
|
2016-01-21 09:07:55 -08:00
|
|
|
Field(v,CAML_EPHE_LINK_OFFSET) = *ephes_checked_if_pure;
|
2015-11-29 08:25:21 -08:00
|
|
|
*ephes_checked_if_pure = v;
|
2016-01-21 09:07:55 -08:00
|
|
|
ephes_checked_if_pure = &Field(v,CAML_EPHE_LINK_OFFSET);
|
2014-03-07 00:10:12 -08:00
|
|
|
}
|
2013-12-25 11:15:39 -08:00
|
|
|
return gray_vals_ptr;
|
|
|
|
}
|
|
|
|
|
2014-03-07 00:10:12 -08:00
|
|
|
|
|
|
|
|
2005-09-22 07:21:50 -07:00
|
|
|
static void mark_slice (intnat work)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2015-11-20 08:54:26 -08:00
|
|
|
value *gray_vals_ptr; /* Local copy of [gray_vals_cur] */
|
2014-02-23 06:51:27 -08:00
|
|
|
value v;
|
|
|
|
header_t hd;
|
2015-11-20 08:54:26 -08:00
|
|
|
mlsize_t size, i, start, end; /* [start] is a local copy of [current_index] */
|
|
|
|
#ifdef CAML_INSTR
|
|
|
|
int slice_fields = 0;
|
|
|
|
#endif
|
2014-02-23 06:51:27 -08:00
|
|
|
int slice_pointers = 0; /** gcc removes it when not in CAML_INSTR */
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
caml_gc_message (0x40, "Marking %ld words\n", work);
|
2008-01-11 03:55:36 -08:00
|
|
|
caml_gc_message (0x40, "Subphase = %ld\n", caml_gc_subphase);
|
1995-07-24 05:46:59 -07:00
|
|
|
gray_vals_ptr = gray_vals_cur;
|
2015-11-20 08:54:26 -08:00
|
|
|
v = current_value;
|
|
|
|
start = current_index;
|
1995-05-04 03:15:53 -07:00
|
|
|
while (work > 0){
|
2015-11-20 08:54:26 -08:00
|
|
|
if (v == 0 && gray_vals_ptr > gray_vals){
|
|
|
|
CAMLassert (start == 0);
|
1995-07-24 05:46:59 -07:00
|
|
|
v = *--gray_vals_ptr;
|
2015-11-20 08:54:26 -08:00
|
|
|
CAMLassert (Is_gray_val (v));
|
|
|
|
}
|
|
|
|
if (v != 0){
|
1995-07-17 09:11:08 -07:00
|
|
|
hd = Hd_val(v);
|
|
|
|
Assert (Is_gray_hd (hd));
|
2002-09-18 06:59:27 -07:00
|
|
|
size = Wosize_hd (hd);
|
2015-11-20 08:54:26 -08:00
|
|
|
end = start + work;
|
1995-07-17 09:11:08 -07:00
|
|
|
if (Tag_hd (hd) < No_scan_tag){
|
2015-11-20 08:54:26 -08:00
|
|
|
start = size < start ? size : start;
|
|
|
|
end = size < end ? size : end;
|
|
|
|
CAMLassert (end > start);
|
|
|
|
INSTR (slice_fields += end - start;)
|
|
|
|
INSTR (if (size > end)
|
|
|
|
CAML_INSTR_INT ("major/mark/slice/remain", size - end);)
|
|
|
|
for (i = start; i < end; i++){
|
2013-12-25 11:15:39 -08:00
|
|
|
gray_vals_ptr = mark_slice_darken(gray_vals_ptr,v,i,
|
|
|
|
/*in_ephemeron=*/ 0,
|
|
|
|
&slice_pointers);
|
1995-07-17 09:11:08 -07:00
|
|
|
}
|
2015-11-20 08:54:26 -08:00
|
|
|
if (end < size){
|
|
|
|
work = 0;
|
|
|
|
start = end;
|
|
|
|
/* [v] doesn't change. */
|
|
|
|
CAMLassert (Is_gray_val (v));
|
|
|
|
}else{
|
|
|
|
CAMLassert (end == size);
|
|
|
|
Hd_val (v) = Blackhd_hd (hd);
|
|
|
|
work -= Whsize_wosize(end - start);
|
|
|
|
start = 0;
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
/* The block doesn't contain any pointers. */
|
|
|
|
CAMLassert (start == 0);
|
|
|
|
Hd_val (v) = Blackhd_hd (hd);
|
|
|
|
work -= Whsize_wosize(size);
|
|
|
|
v = 0;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
}else if (markhp != NULL){
|
|
|
|
if (markhp == limit){
|
1997-05-19 08:42:21 -07:00
|
|
|
chunk = Chunk_next (chunk);
|
|
|
|
if (chunk == NULL){
|
|
|
|
markhp = NULL;
|
|
|
|
}else{
|
|
|
|
markhp = chunk;
|
|
|
|
limit = chunk + Chunk_size (chunk);
|
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
}else{
|
1997-05-19 08:42:21 -07:00
|
|
|
if (Is_gray_val (Val_hp (markhp))){
|
|
|
|
Assert (gray_vals_ptr == gray_vals);
|
2015-11-20 08:54:26 -08:00
|
|
|
CAMLassert (v == 0 && start == 0);
|
|
|
|
v = Val_hp (markhp);
|
1997-05-19 08:42:21 -07:00
|
|
|
}
|
|
|
|
markhp += Bhsize_hp (markhp);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
}else if (!heap_is_pure){
|
|
|
|
heap_is_pure = 1;
|
2003-12-31 06:20:40 -08:00
|
|
|
chunk = caml_heap_start;
|
1995-05-04 03:15:53 -07:00
|
|
|
markhp = chunk;
|
1997-05-13 07:45:38 -07:00
|
|
|
limit = chunk + Chunk_size (chunk);
|
2016-01-20 00:20:42 -08:00
|
|
|
} else if (caml_gc_subphase == Subphase_mark_roots) {
|
|
|
|
gray_vals_cur = gray_vals_ptr;
|
|
|
|
work = caml_darken_all_roots_slice (work);
|
|
|
|
gray_vals_ptr = gray_vals_cur;
|
|
|
|
if (work > 0){
|
|
|
|
caml_gc_subphase = Subphase_mark_main;
|
|
|
|
}
|
2015-11-29 08:25:21 -08:00
|
|
|
} else if (*ephes_to_check != (value) NULL) {
|
2013-12-25 11:15:39 -08:00
|
|
|
/* Continue to scan the list of ephe */
|
2014-03-07 00:10:12 -08:00
|
|
|
gray_vals_ptr = mark_ephe_aux(gray_vals_ptr,&work,&slice_pointers);
|
2013-12-25 11:15:39 -08:00
|
|
|
} else if (!ephe_list_pure){
|
|
|
|
/* We must scan again the list because some value have been darken */
|
|
|
|
ephe_list_pure = 1;
|
2015-11-29 08:25:21 -08:00
|
|
|
ephes_to_check = ephes_checked_if_pure;
|
2008-01-11 08:13:18 -08:00
|
|
|
}else{
|
|
|
|
switch (caml_gc_subphase){
|
2013-11-21 09:02:55 -08:00
|
|
|
case Subphase_mark_main: {
|
|
|
|
/* Subphase_mark_main is done.
|
|
|
|
Mark finalised values. */
|
|
|
|
gray_vals_cur = gray_vals_ptr;
|
|
|
|
caml_final_update ();
|
|
|
|
gray_vals_ptr = gray_vals_cur;
|
|
|
|
if (gray_vals_ptr > gray_vals){
|
|
|
|
v = *--gray_vals_ptr;
|
|
|
|
CAMLassert (start == 0);
|
|
|
|
}
|
|
|
|
/* Complete the marking */
|
2015-11-29 08:25:21 -08:00
|
|
|
ephes_to_check = ephes_checked_if_pure;
|
2013-11-21 09:02:55 -08:00
|
|
|
caml_gc_subphase = Subphase_mark_final;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Subphase_mark_final: {
|
2014-03-06 10:18:58 -08:00
|
|
|
if (caml_ephe_list_head != (value) NULL){
|
|
|
|
/* Initialise the clean phase. */
|
|
|
|
caml_gc_phase = Phase_clean;
|
2015-11-29 08:25:21 -08:00
|
|
|
ephes_to_check = &caml_ephe_list_head;
|
2014-03-06 10:18:58 -08:00
|
|
|
work = 0;
|
|
|
|
} else {
|
2015-12-25 02:07:13 -08:00
|
|
|
/* Initialise the sweep phase,
|
|
|
|
shortcut the unneeded clean phase. */
|
2014-03-06 10:18:58 -08:00
|
|
|
init_sweep_phase();
|
|
|
|
work = 0;
|
|
|
|
}
|
2008-01-11 08:13:18 -08:00
|
|
|
}
|
|
|
|
break;
|
2013-11-21 09:02:55 -08:00
|
|
|
default: Assert (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gray_vals_cur = gray_vals_ptr;
|
|
|
|
current_value = v;
|
|
|
|
current_index = start;
|
|
|
|
INSTR (CAML_INSTR_INT ("major/mark/slice/fields#", slice_fields);)
|
|
|
|
INSTR (CAML_INSTR_INT ("major/mark/slice/pointers#", slice_pointers);)
|
|
|
|
}
|
|
|
|
|
2015-12-25 02:07:13 -08:00
|
|
|
/* Clean ephemerons */
|
2013-11-21 09:02:55 -08:00
|
|
|
static void clean_slice (intnat work)
|
|
|
|
{
|
2013-12-25 11:15:39 -08:00
|
|
|
value v;
|
2013-11-21 09:02:55 -08:00
|
|
|
|
|
|
|
caml_gc_message (0x40, "Cleaning %ld words\n", work);
|
|
|
|
while (work > 0){
|
2015-11-29 08:25:21 -08:00
|
|
|
v = *ephes_to_check;
|
2014-03-07 00:10:12 -08:00
|
|
|
if (v != (value) NULL){
|
|
|
|
if (Is_white_val (v)){
|
|
|
|
/* The whole array is dead, remove it from the list. */
|
2016-01-21 09:07:55 -08:00
|
|
|
*ephes_to_check = Field (v, CAML_EPHE_LINK_OFFSET);
|
2014-03-07 00:10:12 -08:00
|
|
|
work -= 1;
|
|
|
|
}else{
|
2013-12-25 11:15:39 -08:00
|
|
|
caml_ephe_clean(v);
|
2016-01-21 09:07:55 -08:00
|
|
|
ephes_to_check = &Field (v, CAML_EPHE_LINK_OFFSET);
|
2013-12-25 11:15:39 -08:00
|
|
|
work -= Whsize_val (v);
|
2008-01-11 08:13:18 -08:00
|
|
|
}
|
2014-03-07 00:10:12 -08:00
|
|
|
}else{ /* End of list reached */
|
|
|
|
/* Phase_clean is done. */
|
|
|
|
/* Initialise the sweep phase. */
|
|
|
|
init_sweep_phase();
|
|
|
|
work = 0;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-22 07:21:50 -07:00
|
|
|
static void sweep_slice (intnat work)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
char *hp;
|
|
|
|
header_t hd;
|
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
caml_gc_message (0x40, "Sweeping %ld words\n", work);
|
1995-05-04 03:15:53 -07:00
|
|
|
while (work > 0){
|
2003-12-31 06:20:40 -08:00
|
|
|
if (caml_gc_sweep_hp < limit){
|
|
|
|
hp = caml_gc_sweep_hp;
|
1995-05-04 03:15:53 -07:00
|
|
|
hd = Hd_hp (hp);
|
|
|
|
work -= Whsize_hd (hd);
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_gc_sweep_hp += Bhsize_hd (hd);
|
1995-05-04 03:15:53 -07:00
|
|
|
switch (Color_hd (hd)){
|
2000-01-02 08:10:00 -08:00
|
|
|
case Caml_white:
|
2000-02-10 06:04:59 -08:00
|
|
|
if (Tag_hd (hd) == Custom_tag){
|
2000-02-11 04:03:31 -08:00
|
|
|
void (*final_fun)(value) = Custom_ops_val(Val_hp(hp))->finalize;
|
2000-02-10 06:04:59 -08:00
|
|
|
if (final_fun != NULL) final_fun(Val_hp(hp));
|
1997-05-19 08:42:21 -07:00
|
|
|
}
|
2014-12-16 11:36:35 -08:00
|
|
|
caml_gc_sweep_hp = (char *) caml_fl_merge_block (Val_hp (hp));
|
1997-05-19 08:42:21 -07:00
|
|
|
break;
|
2000-01-02 08:10:00 -08:00
|
|
|
case Caml_blue:
|
1997-05-19 08:42:21 -07:00
|
|
|
/* Only the blocks of the free-list are blue. See [freelist.c]. */
|
2004-01-02 11:23:29 -08:00
|
|
|
caml_fl_merge = Bp_hp (hp);
|
1997-05-19 08:42:21 -07:00
|
|
|
break;
|
2000-01-02 08:10:00 -08:00
|
|
|
default: /* gray or black */
|
|
|
|
Assert (Color_hd (hd) == Caml_black);
|
1997-05-19 08:42:21 -07:00
|
|
|
Hd_hp (hp) = Whitehd_hd (hd);
|
|
|
|
break;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
2003-12-31 06:20:40 -08:00
|
|
|
Assert (caml_gc_sweep_hp <= limit);
|
1995-05-04 03:15:53 -07:00
|
|
|
}else{
|
1997-05-13 07:45:38 -07:00
|
|
|
chunk = Chunk_next (chunk);
|
1995-05-04 03:15:53 -07:00
|
|
|
if (chunk == NULL){
|
1997-05-19 08:42:21 -07:00
|
|
|
/* Sweeping is done. */
|
2004-01-02 11:23:29 -08:00
|
|
|
++ caml_stat_major_collections;
|
1997-05-13 07:45:38 -07:00
|
|
|
work = 0;
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_gc_phase = Phase_idle;
|
1995-05-04 03:15:53 -07:00
|
|
|
}else{
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_gc_sweep_hp = chunk;
|
1997-05-19 08:42:21 -07:00
|
|
|
limit = chunk + Chunk_size (chunk);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-20 08:54:26 -08:00
|
|
|
#ifdef CAML_INSTR
|
|
|
|
static char *mark_slice_name[] = {
|
|
|
|
/* 0 */ NULL,
|
|
|
|
/* 1 */ NULL,
|
|
|
|
/* 2 */ NULL,
|
|
|
|
/* 3 */ NULL,
|
|
|
|
/* 4 */ NULL,
|
|
|
|
/* 5 */ NULL,
|
|
|
|
/* 6 */ NULL,
|
|
|
|
/* 7 */ NULL,
|
|
|
|
/* 8 */ NULL,
|
|
|
|
/* 9 */ NULL,
|
|
|
|
/* 10 */ "major/mark_roots",
|
|
|
|
/* 11 */ "major/mark_main",
|
|
|
|
/* 12 */ "major/mark_weak1",
|
|
|
|
/* 13 */ "major/mark_weak2",
|
|
|
|
/* 14 */ "major/mark_final",
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* The main entry point for the major GC. Called about once for each
|
|
|
|
minor GC. [howmuch] is the amount of work to do:
|
|
|
|
-1 if the GC is triggered automatically
|
|
|
|
0 to let the GC compute the amount of work
|
|
|
|
[n] to make the GC do enough work to (on average) free [n] words
|
2002-02-05 09:11:33 -08:00
|
|
|
*/
|
2015-11-20 08:54:26 -08:00
|
|
|
void caml_major_collection_slice (intnat howmuch)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2015-11-20 08:54:26 -08:00
|
|
|
double p, dp, filt_p, spend;
|
2005-09-22 07:21:50 -07:00
|
|
|
intnat computed_work;
|
2015-11-20 08:54:26 -08:00
|
|
|
int i;
|
2000-01-07 08:51:58 -08:00
|
|
|
/*
|
1997-05-21 08:28:15 -07:00
|
|
|
Free memory at the start of the GC cycle (garbage + free list) (assumed):
|
2014-12-24 12:18:22 -08:00
|
|
|
FM = caml_stat_heap_wsz * caml_percent_free
|
2003-12-31 06:20:40 -08:00
|
|
|
/ (100 + caml_percent_free)
|
2002-06-05 05:11:15 -07:00
|
|
|
|
|
|
|
Assuming steady state and enforcing a constant allocation rate, then
|
|
|
|
FM is divided in 2/3 for garbage and 1/3 for free list.
|
|
|
|
G = 2 * FM / 3
|
2004-06-14 08:17:43 -07:00
|
|
|
G is also the amount of memory that will be used during this cycle
|
2002-06-05 05:11:15 -07:00
|
|
|
(still assuming steady state).
|
|
|
|
|
|
|
|
Proportion of G consumed since the previous slice:
|
2003-12-31 06:20:40 -08:00
|
|
|
PH = caml_allocated_words / G
|
|
|
|
= caml_allocated_words * 3 * (100 + caml_percent_free)
|
2014-12-24 12:18:22 -08:00
|
|
|
/ (2 * caml_stat_heap_wsz * caml_percent_free)
|
2004-06-14 08:17:43 -07:00
|
|
|
Proportion of extra-heap resources consumed since the previous slice:
|
|
|
|
PE = caml_extra_heap_resources
|
1995-05-04 03:15:53 -07:00
|
|
|
Proportion of total work to do in this slice:
|
1998-08-07 11:43:39 -07:00
|
|
|
P = max (PH, PE)
|
2015-11-20 08:54:26 -08:00
|
|
|
|
|
|
|
Here, we insert a time-based filter on the P variable to avoid large
|
|
|
|
latency spikes in the GC, so the P below is a smoothed-out version of
|
|
|
|
the P above.
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
Amount of marking work for the GC cycle:
|
2014-12-24 12:18:22 -08:00
|
|
|
MW = caml_stat_heap_wsz * 100 / (100 + caml_percent_free)
|
2015-11-20 08:54:26 -08:00
|
|
|
+ caml_incremental_roots_count
|
1995-05-04 03:15:53 -07:00
|
|
|
Amount of sweeping work for the GC cycle:
|
2014-12-24 12:18:22 -08:00
|
|
|
SW = caml_stat_heap_wsz
|
2008-12-03 10:09:09 -08:00
|
|
|
|
|
|
|
In order to finish marking with a non-empty free list, we will
|
|
|
|
use 40% of the time for marking, and 60% for sweeping.
|
|
|
|
|
2015-11-20 08:54:26 -08:00
|
|
|
Let MT be the time spent marking, ST the time spent sweeping, and TT
|
|
|
|
the total time for this cycle. We have:
|
|
|
|
MT = 40/100 * TT
|
|
|
|
ST = 60/100 * TT
|
2008-12-03 10:09:09 -08:00
|
|
|
|
2015-11-20 08:54:26 -08:00
|
|
|
Amount of time to spend on this slice:
|
|
|
|
T = P * TT = P * MT / (40/100) = P * ST / (60/100)
|
|
|
|
|
|
|
|
Since we must do MW work in MT time or SW work in ST time, the amount
|
|
|
|
of work for this slice is:
|
|
|
|
MS = P * MW / (40/100) if marking
|
|
|
|
SS = P * SW / (60/100) if sweeping
|
2008-12-03 10:09:09 -08:00
|
|
|
|
|
|
|
Amount of marking work for a marking slice:
|
|
|
|
MS = P * MW / (40/100)
|
2015-11-20 08:54:26 -08:00
|
|
|
MS = P * (caml_stat_heap_wsz * 250 / (100 + caml_percent_free)
|
|
|
|
+ 2.5 * caml_incremental_roots_count)
|
2008-12-03 10:09:09 -08:00
|
|
|
Amount of sweeping work for a sweeping slice:
|
|
|
|
SS = P * SW / (60/100)
|
2014-12-24 12:18:22 -08:00
|
|
|
SS = P * caml_stat_heap_wsz * 5 / 3
|
2008-12-03 10:09:09 -08:00
|
|
|
|
|
|
|
This slice will either mark MS words or sweep SS words.
|
1995-05-04 03:15:53 -07:00
|
|
|
*/
|
|
|
|
|
2015-07-29 15:19:24 -07:00
|
|
|
if (caml_major_slice_begin_hook != NULL) (*caml_major_slice_begin_hook) ();
|
2015-11-20 08:54:26 -08:00
|
|
|
CAML_INSTR_SETUP (tmr, "major");
|
1997-05-13 07:45:38 -07:00
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
p = (double) caml_allocated_words * 3.0 * (100 + caml_percent_free)
|
2014-12-24 12:18:22 -08:00
|
|
|
/ caml_stat_heap_wsz / caml_percent_free / 2.0;
|
2004-06-14 08:17:43 -07:00
|
|
|
if (caml_dependent_size > 0){
|
|
|
|
dp = (double) caml_dependent_allocated * (100 + caml_percent_free)
|
|
|
|
/ caml_dependent_size / caml_percent_free;
|
|
|
|
}else{
|
|
|
|
dp = 0.0;
|
|
|
|
}
|
|
|
|
if (p < dp) p = dp;
|
|
|
|
if (p < caml_extra_heap_resources) p = caml_extra_heap_resources;
|
2015-11-20 08:54:26 -08:00
|
|
|
if (p > 0.3) p = 0.3;
|
|
|
|
CAML_INSTR_INT ("major/work/extra#",
|
|
|
|
(uintnat) (caml_extra_heap_resources * 1000000));
|
1998-08-07 11:43:39 -07:00
|
|
|
|
2015-11-20 08:54:26 -08:00
|
|
|
caml_gc_message (0x40, "ordered work = %ld words\n", howmuch);
|
2008-01-11 03:55:36 -08:00
|
|
|
caml_gc_message (0x40, "allocated_words = %"
|
2005-09-22 07:21:50 -07:00
|
|
|
ARCH_INTNAT_PRINTF_FORMAT "u\n",
|
|
|
|
caml_allocated_words);
|
|
|
|
caml_gc_message (0x40, "extra_heap_resources = %"
|
|
|
|
ARCH_INTNAT_PRINTF_FORMAT "uu\n",
|
|
|
|
(uintnat) (caml_extra_heap_resources * 1000000));
|
2015-11-20 08:54:26 -08:00
|
|
|
caml_gc_message (0x40, "raw work-to-do = %"
|
|
|
|
ARCH_INTNAT_PRINTF_FORMAT "du\n",
|
|
|
|
(intnat) (p * 1000000));
|
|
|
|
|
|
|
|
for (i = 0; i < caml_major_window; i++){
|
|
|
|
caml_major_ring[i] += p / caml_major_window;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (caml_gc_clock >= 1.0){
|
|
|
|
caml_gc_clock -= 1.0;
|
|
|
|
++caml_major_ring_index;
|
|
|
|
if (caml_major_ring_index >= caml_major_window){
|
|
|
|
caml_major_ring_index = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (howmuch == -1){
|
|
|
|
/* auto-triggered GC slice: spend work credit on the current bucket,
|
|
|
|
then do the remaining work, if any */
|
|
|
|
/* Note that the minor GC guarantees that the major slice is called in
|
|
|
|
automatic mode (with [howmuch] = -1) at least once per clock tick.
|
|
|
|
This means we never leave a non-empty bucket behind. */
|
|
|
|
spend = fmin (caml_major_work_credit,
|
|
|
|
caml_major_ring[caml_major_ring_index]);
|
|
|
|
caml_major_work_credit -= spend;
|
|
|
|
filt_p = caml_major_ring[caml_major_ring_index] - spend;
|
|
|
|
caml_major_ring[caml_major_ring_index] = 0.0;
|
|
|
|
}else{
|
|
|
|
/* forced GC slice: do work and add it to the credit */
|
|
|
|
if (howmuch == 0){
|
|
|
|
/* automatic setting: size of next bucket
|
|
|
|
we do not use the current bucket, as it may be empty */
|
|
|
|
int i = caml_major_ring_index + 1;
|
|
|
|
if (i >= caml_major_window) i = 0;
|
|
|
|
filt_p = caml_major_ring[i];
|
|
|
|
}else{
|
|
|
|
/* manual setting */
|
|
|
|
filt_p = (double) howmuch * 3.0 * (100 + caml_percent_free)
|
|
|
|
/ caml_stat_heap_wsz / caml_percent_free / 2.0;
|
|
|
|
}
|
|
|
|
caml_major_work_credit += filt_p;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = filt_p;
|
|
|
|
|
|
|
|
caml_gc_message (0x40, "filtered work-to-do = %"
|
|
|
|
ARCH_INTNAT_PRINTF_FORMAT "du\n",
|
|
|
|
(intnat) (p * 1000000));
|
|
|
|
|
|
|
|
if (caml_gc_phase == Phase_idle){
|
2015-12-04 05:40:37 -08:00
|
|
|
if (caml_young_ptr == caml_young_alloc_end){
|
|
|
|
/* We can only start a major GC cycle if the minor allocation arena
|
|
|
|
is empty, otherwise we'd have to treat it as a set of roots. */
|
|
|
|
start_cycle ();
|
|
|
|
CAML_INSTR_TIME (tmr, "major/roots");
|
|
|
|
}
|
2015-11-20 08:54:26 -08:00
|
|
|
p = 0;
|
|
|
|
goto finished;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p < 0){
|
|
|
|
p = 0;
|
|
|
|
goto finished;
|
|
|
|
}
|
1998-08-07 11:43:39 -07:00
|
|
|
|
2013-11-21 09:02:55 -08:00
|
|
|
if (caml_gc_phase == Phase_mark || caml_gc_phase == Phase_clean){
|
2015-11-20 08:54:26 -08:00
|
|
|
computed_work = (intnat) (p * (caml_stat_heap_wsz * 250
|
|
|
|
/ (100 + caml_percent_free)
|
|
|
|
+ caml_incremental_roots_count));
|
2002-02-05 09:11:33 -08:00
|
|
|
}else{
|
2014-12-24 12:18:22 -08:00
|
|
|
computed_work = (intnat) (p * caml_stat_heap_wsz * 5 / 3);
|
2002-02-05 09:11:33 -08:00
|
|
|
}
|
2003-12-29 14:15:02 -08:00
|
|
|
caml_gc_message (0x40, "computed work = %ld words\n", computed_work);
|
2003-12-31 06:20:40 -08:00
|
|
|
if (caml_gc_phase == Phase_mark){
|
2015-11-20 08:54:26 -08:00
|
|
|
CAML_INSTR_INT ("major/work/mark#", computed_work);
|
|
|
|
mark_slice (computed_work);
|
|
|
|
CAML_INSTR_TIME (tmr, mark_slice_name[caml_gc_subphase]);
|
2003-12-29 14:15:02 -08:00
|
|
|
caml_gc_message (0x02, "!", 0);
|
2013-11-21 09:02:55 -08:00
|
|
|
}else if (caml_gc_phase == Phase_clean){
|
|
|
|
clean_slice (computed_work);
|
|
|
|
caml_gc_message (0x02, "%%", 0);
|
1995-05-04 03:15:53 -07:00
|
|
|
}else{
|
2003-12-31 06:20:40 -08:00
|
|
|
Assert (caml_gc_phase == Phase_sweep);
|
2015-11-20 08:54:26 -08:00
|
|
|
CAML_INSTR_INT ("major/work/sweep#", computed_work);
|
|
|
|
sweep_slice (computed_work);
|
|
|
|
CAML_INSTR_TIME (tmr, "major/sweep");
|
2003-12-29 14:15:02 -08:00
|
|
|
caml_gc_message (0x02, "$", 0);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
1997-05-13 07:45:38 -07:00
|
|
|
|
2015-11-20 08:54:26 -08:00
|
|
|
if (caml_gc_phase == Phase_idle){
|
|
|
|
caml_compact_heap_maybe ();
|
|
|
|
CAML_INSTR_TIME (tmr, "major/check_and_compact");
|
|
|
|
}
|
|
|
|
|
|
|
|
finished:
|
|
|
|
caml_gc_message (0x40, "work-done = %"
|
|
|
|
ARCH_INTNAT_PRINTF_FORMAT "du\n",
|
|
|
|
(intnat) (p * 1000000));
|
|
|
|
|
|
|
|
/* if some of the work was not done, take it back from the credit
|
|
|
|
or spread it over the buckets. */
|
|
|
|
p = filt_p - p;
|
|
|
|
spend = fmin (p, caml_major_work_credit);
|
|
|
|
caml_major_work_credit -= spend;
|
|
|
|
if (p > spend){
|
|
|
|
p -= spend;
|
|
|
|
p /= caml_major_window;
|
|
|
|
for (i = 0; i < caml_major_window; i++) caml_major_ring[i] += p;
|
|
|
|
}
|
1997-05-13 07:45:38 -07:00
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
caml_stat_major_words += caml_allocated_words;
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_allocated_words = 0;
|
2004-06-14 08:17:43 -07:00
|
|
|
caml_dependent_allocated = 0;
|
|
|
|
caml_extra_heap_resources = 0.0;
|
2015-07-29 15:19:24 -07:00
|
|
|
if (caml_major_slice_end_hook != NULL) (*caml_major_slice_end_hook) ();
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2015-11-20 08:54:26 -08:00
|
|
|
/* This does not call [caml_compact_heap_maybe] because the estimates of
|
1997-05-13 07:45:38 -07:00
|
|
|
free and live memory are only valid for a cycle done incrementally.
|
2015-11-20 08:54:26 -08:00
|
|
|
Besides, this function itself is called by [caml_compact_heap_maybe].
|
1997-05-13 07:45:38 -07:00
|
|
|
*/
|
2003-12-31 06:20:40 -08:00
|
|
|
void caml_finish_major_cycle (void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2003-12-31 06:20:40 -08:00
|
|
|
if (caml_gc_phase == Phase_idle) start_cycle ();
|
|
|
|
while (caml_gc_phase == Phase_mark) mark_slice (LONG_MAX);
|
2013-11-21 09:02:55 -08:00
|
|
|
while (caml_gc_phase == Phase_clean) clean_slice (LONG_MAX);
|
2003-12-31 06:20:40 -08:00
|
|
|
Assert (caml_gc_phase == Phase_sweep);
|
|
|
|
while (caml_gc_phase == Phase_sweep) sweep_slice (LONG_MAX);
|
|
|
|
Assert (caml_gc_phase == Phase_idle);
|
2004-01-02 11:23:29 -08:00
|
|
|
caml_stat_major_words += caml_allocated_words;
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_allocated_words = 0;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2015-11-20 08:54:26 -08:00
|
|
|
/* Call this function to make sure [bsz] is greater than or equal
|
|
|
|
to both [Heap_chunk_min] and the current heap increment.
|
2002-05-16 08:06:31 -07:00
|
|
|
*/
|
2015-11-20 08:54:26 -08:00
|
|
|
asize_t caml_clip_heap_chunk_wsz (asize_t wsz)
|
2002-05-16 08:06:31 -07:00
|
|
|
{
|
2015-11-20 08:54:26 -08:00
|
|
|
asize_t result = wsz;
|
2014-02-20 05:04:01 -08:00
|
|
|
uintnat incr;
|
2002-05-16 08:06:31 -07:00
|
|
|
|
2014-12-24 12:18:22 -08:00
|
|
|
/* Compute the heap increment as a word size. */
|
2014-02-20 05:04:01 -08:00
|
|
|
if (caml_major_heap_increment > 1000){
|
2014-12-24 12:18:22 -08:00
|
|
|
incr = caml_major_heap_increment;
|
2014-02-20 05:04:01 -08:00
|
|
|
}else{
|
2014-12-24 12:18:22 -08:00
|
|
|
incr = caml_stat_heap_wsz / 100 * caml_major_heap_increment;
|
2014-02-20 05:04:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (result < incr){
|
|
|
|
result = incr;
|
2002-05-16 08:06:31 -07:00
|
|
|
}
|
2015-11-20 08:54:26 -08:00
|
|
|
if (result < Heap_chunk_min){
|
|
|
|
result = Heap_chunk_min;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
2002-05-16 08:06:31 -07:00
|
|
|
return result;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2014-12-12 07:18:04 -08:00
|
|
|
/* [heap_size] is a number of bytes */
|
2003-12-31 06:20:40 -08:00
|
|
|
void caml_init_major_heap (asize_t heap_size)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2015-11-20 08:54:26 -08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
caml_stat_heap_wsz = caml_clip_heap_chunk_wsz (Wsize_bsize (heap_size));
|
2014-12-24 12:18:22 -08:00
|
|
|
caml_stat_top_heap_wsz = caml_stat_heap_wsz;
|
|
|
|
Assert (Bsize_wsize (caml_stat_heap_wsz) % Page_size == 0);
|
2015-09-11 04:58:31 -07:00
|
|
|
caml_heap_start =
|
|
|
|
(char *) caml_alloc_for_heap (Bsize_wsize (caml_stat_heap_wsz));
|
2003-12-31 06:20:40 -08:00
|
|
|
if (caml_heap_start == NULL)
|
2015-11-20 08:54:26 -08:00
|
|
|
caml_fatal_error ("Fatal error: cannot allocate initial major heap.\n");
|
2003-12-31 06:20:40 -08:00
|
|
|
Chunk_next (caml_heap_start) = NULL;
|
2015-11-20 08:54:26 -08:00
|
|
|
caml_stat_heap_wsz = Wsize_bsize (Chunk_size (caml_heap_start));
|
2004-01-02 11:23:29 -08:00
|
|
|
caml_stat_heap_chunks = 1;
|
2015-11-20 08:54:26 -08:00
|
|
|
caml_stat_top_heap_wsz = caml_stat_heap_wsz;
|
2002-05-28 09:57:31 -07:00
|
|
|
|
2008-01-03 01:37:10 -08:00
|
|
|
if (caml_page_table_add(In_heap, caml_heap_start,
|
2015-09-11 04:58:31 -07:00
|
|
|
caml_heap_start + Bsize_wsize (caml_stat_heap_wsz))
|
|
|
|
!= 0) {
|
2015-11-20 08:54:26 -08:00
|
|
|
caml_fatal_error ("Fatal error: cannot allocate "
|
|
|
|
"initial page table.\n");
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
1998-08-07 11:43:39 -07:00
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
caml_fl_init_merge ();
|
|
|
|
caml_make_free_blocks ((value *) caml_heap_start,
|
2014-12-24 12:18:22 -08:00
|
|
|
caml_stat_heap_wsz, 1, Caml_white);
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_gc_phase = Phase_idle;
|
1995-05-04 03:15:53 -07:00
|
|
|
gray_vals_size = 2048;
|
|
|
|
gray_vals = (value *) malloc (gray_vals_size * sizeof (value));
|
1995-08-08 06:37:34 -07:00
|
|
|
if (gray_vals == NULL)
|
2008-01-03 01:37:10 -08:00
|
|
|
caml_fatal_error ("Fatal error: not enough memory for the gray cache.\n");
|
1995-05-04 03:15:53 -07:00
|
|
|
gray_vals_cur = gray_vals;
|
|
|
|
gray_vals_end = gray_vals + gray_vals_size;
|
|
|
|
heap_is_pure = 1;
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_allocated_words = 0;
|
2004-06-14 08:17:43 -07:00
|
|
|
caml_extra_heap_resources = 0.0;
|
2015-11-20 08:54:26 -08:00
|
|
|
for (i = 0; i < Max_major_window; i++) caml_major_ring[i] = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void caml_set_major_window (int w){
|
|
|
|
uintnat total = 0;
|
|
|
|
int i;
|
|
|
|
if (w == caml_major_window) return;
|
|
|
|
CAMLassert (w <= Max_major_window);
|
|
|
|
/* Collect the current work-to-do from the buckets. */
|
|
|
|
for (i = 0; i < caml_major_window; i++){
|
|
|
|
total += caml_major_ring[i];
|
|
|
|
}
|
|
|
|
/* Redistribute to the new buckets. */
|
|
|
|
for (i = 0; i < w; i++){
|
|
|
|
caml_major_ring[i] = total / w;
|
|
|
|
}
|
|
|
|
caml_major_window = w;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|