2016-02-18 07:11:59 -08:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* OCaml *)
|
|
|
|
(* *)
|
2019-09-26 13:34:12 -07:00
|
|
|
(* Damien Doligez, projet Para, INRIA Rocquencourt *)
|
|
|
|
(* Jacques-Henri Jourdan, projet Gallium, INRIA Paris *)
|
2016-02-18 07:11:59 -08:00
|
|
|
(* *)
|
2019-04-23 00:27:31 -07:00
|
|
|
(* Copyright 1996-2016 Institut National de Recherche en Informatique *)
|
|
|
|
(* et en Automatique. *)
|
2016-02-18 07:11:59 -08:00
|
|
|
(* *)
|
|
|
|
(* 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
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
type stat = {
|
2001-02-05 06:59:24 -08:00
|
|
|
minor_words : float;
|
|
|
|
promoted_words : float;
|
|
|
|
major_words : float;
|
1995-05-04 03:15:53 -07:00
|
|
|
minor_collections : int;
|
|
|
|
major_collections : int;
|
1995-08-08 06:37:34 -07:00
|
|
|
heap_words : int;
|
1995-05-04 03:15:53 -07:00
|
|
|
heap_chunks : int;
|
|
|
|
live_words : int;
|
|
|
|
live_blocks : int;
|
|
|
|
free_words : int;
|
|
|
|
free_blocks : int;
|
|
|
|
largest_free : int;
|
1997-05-13 07:45:38 -07:00
|
|
|
fragments : int;
|
2002-02-01 04:23:58 -08:00
|
|
|
compactions : int;
|
|
|
|
top_heap_words : int;
|
2010-04-27 00:55:08 -07:00
|
|
|
stack_size : int;
|
2020-06-12 07:42:05 -07:00
|
|
|
forced_major_collections: int;
|
2016-04-28 07:00:18 -07:00
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
type control = {
|
|
|
|
mutable minor_heap_size : int;
|
|
|
|
mutable major_heap_increment : int;
|
|
|
|
mutable space_overhead : int;
|
1998-08-07 11:45:00 -07:00
|
|
|
mutable verbose : int;
|
1997-05-13 07:45:38 -07:00
|
|
|
mutable max_overhead : int;
|
2002-02-01 04:23:58 -08:00
|
|
|
mutable stack_limit : int;
|
2008-12-03 10:09:09 -08:00
|
|
|
mutable allocation_policy : int;
|
2015-11-20 08:54:26 -08:00
|
|
|
window_size : int;
|
2018-11-06 04:42:48 -08:00
|
|
|
custom_major_ratio : int;
|
|
|
|
custom_minor_ratio : int;
|
|
|
|
custom_minor_max_size : int;
|
2016-04-28 07:00:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
external stat : unit -> stat = "caml_gc_stat"
|
|
|
|
external quick_stat : unit -> stat = "caml_gc_quick_stat"
|
|
|
|
external counters : unit -> (float * float * float) = "caml_gc_counters"
|
2016-05-12 08:49:55 -07:00
|
|
|
external minor_words : unit -> (float [@unboxed])
|
2017-03-08 02:24:15 -08:00
|
|
|
= "caml_gc_minor_words" "caml_gc_minor_words_unboxed"
|
2016-04-28 07:00:18 -07:00
|
|
|
external get : unit -> control = "caml_gc_get"
|
|
|
|
external set : control -> unit = "caml_gc_set"
|
|
|
|
external minor : unit -> unit = "caml_gc_minor"
|
|
|
|
external major_slice : int -> int = "caml_gc_major_slice"
|
|
|
|
external major : unit -> unit = "caml_gc_major"
|
|
|
|
external full_major : unit -> unit = "caml_gc_full_major"
|
|
|
|
external compact : unit -> unit = "caml_gc_compaction"
|
2017-03-08 02:24:15 -08:00
|
|
|
external get_minor_free : unit -> int = "caml_get_minor_free"
|
2015-11-20 08:54:26 -08:00
|
|
|
external get_bucket : int -> int = "caml_get_major_bucket" [@@noalloc]
|
|
|
|
external get_credit : unit -> int = "caml_get_major_credit" [@@noalloc]
|
|
|
|
external huge_fallback_count : unit -> int = "caml_gc_huge_fallback_count"
|
2019-11-15 04:52:35 -08:00
|
|
|
external eventlog_pause : unit -> unit = "caml_eventlog_pause"
|
|
|
|
external eventlog_resume : unit -> unit = "caml_eventlog_resume"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2016-04-28 07:00:18 -07:00
|
|
|
open Printf
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let print_stat c =
|
|
|
|
let st = stat () in
|
2020-06-12 07:42:05 -07:00
|
|
|
fprintf c "minor_collections: %d\n" st.minor_collections;
|
|
|
|
fprintf c "major_collections: %d\n" st.major_collections;
|
|
|
|
fprintf c "compactions: %d\n" st.compactions;
|
|
|
|
fprintf c "forced_major_collections: %d\n" st.forced_major_collections;
|
2015-11-20 08:54:26 -08:00
|
|
|
fprintf c "\n";
|
|
|
|
let l1 = String.length (sprintf "%.0f" st.minor_words) in
|
|
|
|
fprintf c "minor_words: %*.0f\n" l1 st.minor_words;
|
|
|
|
fprintf c "promoted_words: %*.0f\n" l1 st.promoted_words;
|
|
|
|
fprintf c "major_words: %*.0f\n" l1 st.major_words;
|
|
|
|
fprintf c "\n";
|
|
|
|
let l2 = String.length (sprintf "%d" st.top_heap_words) in
|
|
|
|
fprintf c "top_heap_words: %*d\n" l2 st.top_heap_words;
|
|
|
|
fprintf c "heap_words: %*d\n" l2 st.heap_words;
|
|
|
|
fprintf c "live_words: %*d\n" l2 st.live_words;
|
|
|
|
fprintf c "free_words: %*d\n" l2 st.free_words;
|
|
|
|
fprintf c "largest_free: %*d\n" l2 st.largest_free;
|
|
|
|
fprintf c "fragments: %*d\n" l2 st.fragments;
|
|
|
|
fprintf c "\n";
|
1995-05-04 03:15:53 -07:00
|
|
|
fprintf c "live_blocks: %d\n" st.live_blocks;
|
|
|
|
fprintf c "free_blocks: %d\n" st.free_blocks;
|
2016-04-28 07:00:18 -07:00
|
|
|
fprintf c "heap_chunks: %d\n" st.heap_chunks
|
|
|
|
|
1999-11-29 12:02:33 -08:00
|
|
|
|
|
|
|
let allocated_bytes () =
|
2001-02-05 06:59:24 -08:00
|
|
|
let (mi, pro, ma) = counters () in
|
|
|
|
(mi +. ma -. pro) *. float_of_int (Sys.word_size / 8)
|
2000-01-07 08:51:58 -08:00
|
|
|
|
2016-04-28 07:00:18 -07:00
|
|
|
|
|
|
|
external finalise : ('a -> unit) -> 'a -> unit = "caml_final_register"
|
2016-07-06 09:34:12 -07:00
|
|
|
external finalise_last : (unit -> unit) -> 'a -> unit =
|
|
|
|
"caml_final_register_called_without_value"
|
2016-04-28 07:00:18 -07:00
|
|
|
external finalise_release : unit -> unit = "caml_final_release"
|
2001-02-05 06:59:24 -08:00
|
|
|
|
|
|
|
|
2016-04-28 07:00:18 -07:00
|
|
|
type alarm = bool ref
|
|
|
|
type alarm_rec = {active : alarm; f : unit -> unit}
|
2001-02-05 06:59:24 -08:00
|
|
|
|
2001-08-28 06:22:24 -07:00
|
|
|
let rec call_alarm arec =
|
|
|
|
if !(arec.active) then begin
|
|
|
|
finalise call_alarm arec;
|
|
|
|
arec.f ();
|
2016-04-28 07:00:18 -07:00
|
|
|
end
|
|
|
|
|
2001-02-05 06:59:24 -08:00
|
|
|
|
|
|
|
let create_alarm f =
|
2001-08-28 06:22:24 -07:00
|
|
|
let arec = { active = ref true; f = f } in
|
|
|
|
finalise call_alarm arec;
|
2003-08-07 07:17:59 -07:00
|
|
|
arec.active
|
2001-02-05 06:59:24 -08:00
|
|
|
|
2016-04-28 07:00:18 -07:00
|
|
|
|
|
|
|
let delete_alarm a = a := false
|
2019-04-23 00:27:31 -07:00
|
|
|
|
|
|
|
module Memprof =
|
|
|
|
struct
|
2020-11-17 03:17:52 -08:00
|
|
|
type allocation_source = Normal | Marshal | Custom
|
2019-09-04 05:36:23 -07:00
|
|
|
type allocation =
|
|
|
|
{ n_samples : int;
|
|
|
|
size : int;
|
2020-11-17 03:17:52 -08:00
|
|
|
source : allocation_source;
|
2019-09-04 05:36:23 -07:00
|
|
|
callstack : Printexc.raw_backtrace }
|
|
|
|
|
Gc.Memprof.start: take a record instead of 5 optional parameters
The Gc.Memprof module provides a low-level API, that will hopefully be
paired with user libraries that provide high-level instrumentation
choices.
A natural question is: how are the higher-level API going to expose
their choice of instrumentation to their users? With the current
Memprof.start API (before this patch), they would have to either
provide their own `start` function wrapping Memprof.start, or provide
a tuple of callbacks for to their users to pass to Memprof.start
themselves.
val start : params -> unit
(* or *)
val callback : params ->
((allocation -> foo option) * (allocation -> bar option) * ... )
With an explicit record, it is easier for libraries to expose an
instrumentation choice (possibility parametrized over
user-provided settings):
val tracker : params -> (foo, bar) Gc.Memprof.tracker
In addition, providing a record instead of optional parameters makes
it much easier to provide "default settings" (helper functions) that
instantiates the types `'minor` and `'ḿajor`, see for example
`simple_tracker` in this patch (which stores the same information for
the minor and major heap, and does not observe promotion), or to later
define checking predicates that can verify that a given choice of
callbacks is sensible (for example: providing a major-dealloc callback
but no promotion callback (dropping all tracked value on promotion) is
probably not a good idea).
Bootstrap: to avoid requiring an awkward bootstrap, this commit keeps
the (now unused) function caml_memprof_start_byt unchanged -- it is
used in the bootstrap binaries, so removing it would break the
build. The intention is to remove it in the following commit.
2020-01-21 12:42:03 -08:00
|
|
|
type ('minor, 'major) tracker = {
|
|
|
|
alloc_minor: allocation -> 'minor option;
|
|
|
|
alloc_major: allocation -> 'major option;
|
|
|
|
promote: 'minor -> 'major option;
|
|
|
|
dealloc_minor: 'minor -> unit;
|
|
|
|
dealloc_major: 'major -> unit;
|
|
|
|
}
|
|
|
|
|
|
|
|
let null_tracker = {
|
|
|
|
alloc_minor = (fun _ -> None);
|
|
|
|
alloc_major = (fun _ -> None);
|
|
|
|
promote = (fun _ -> None);
|
|
|
|
dealloc_minor = (fun _ -> ());
|
|
|
|
dealloc_major = (fun _ -> ());
|
|
|
|
}
|
|
|
|
|
2020-01-10 07:59:20 -08:00
|
|
|
external c_start :
|
Gc.Memprof.start: take a record instead of 5 optional parameters
The Gc.Memprof module provides a low-level API, that will hopefully be
paired with user libraries that provide high-level instrumentation
choices.
A natural question is: how are the higher-level API going to expose
their choice of instrumentation to their users? With the current
Memprof.start API (before this patch), they would have to either
provide their own `start` function wrapping Memprof.start, or provide
a tuple of callbacks for to their users to pass to Memprof.start
themselves.
val start : params -> unit
(* or *)
val callback : params ->
((allocation -> foo option) * (allocation -> bar option) * ... )
With an explicit record, it is easier for libraries to expose an
instrumentation choice (possibility parametrized over
user-provided settings):
val tracker : params -> (foo, bar) Gc.Memprof.tracker
In addition, providing a record instead of optional parameters makes
it much easier to provide "default settings" (helper functions) that
instantiates the types `'minor` and `'ḿajor`, see for example
`simple_tracker` in this patch (which stores the same information for
the minor and major heap, and does not observe promotion), or to later
define checking predicates that can verify that a given choice of
callbacks is sensible (for example: providing a major-dealloc callback
but no promotion callback (dropping all tracked value on promotion) is
probably not a good idea).
Bootstrap: to avoid requiring an awkward bootstrap, this commit keeps
the (now unused) function caml_memprof_start_byt unchanged -- it is
used in the bootstrap binaries, so removing it would break the
build. The intention is to remove it in the following commit.
2020-01-21 12:42:03 -08:00
|
|
|
float -> int -> ('minor, 'major) tracker -> unit
|
|
|
|
= "caml_memprof_start"
|
2019-09-04 05:36:23 -07:00
|
|
|
|
|
|
|
let start
|
|
|
|
~sampling_rate
|
|
|
|
?(callstack_size = max_int)
|
Gc.Memprof.start: take a record instead of 5 optional parameters
The Gc.Memprof module provides a low-level API, that will hopefully be
paired with user libraries that provide high-level instrumentation
choices.
A natural question is: how are the higher-level API going to expose
their choice of instrumentation to their users? With the current
Memprof.start API (before this patch), they would have to either
provide their own `start` function wrapping Memprof.start, or provide
a tuple of callbacks for to their users to pass to Memprof.start
themselves.
val start : params -> unit
(* or *)
val callback : params ->
((allocation -> foo option) * (allocation -> bar option) * ... )
With an explicit record, it is easier for libraries to expose an
instrumentation choice (possibility parametrized over
user-provided settings):
val tracker : params -> (foo, bar) Gc.Memprof.tracker
In addition, providing a record instead of optional parameters makes
it much easier to provide "default settings" (helper functions) that
instantiates the types `'minor` and `'ḿajor`, see for example
`simple_tracker` in this patch (which stores the same information for
the minor and major heap, and does not observe promotion), or to later
define checking predicates that can verify that a given choice of
callbacks is sensible (for example: providing a major-dealloc callback
but no promotion callback (dropping all tracked value on promotion) is
probably not a good idea).
Bootstrap: to avoid requiring an awkward bootstrap, this commit keeps
the (now unused) function caml_memprof_start_byt unchanged -- it is
used in the bootstrap binaries, so removing it would break the
build. The intention is to remove it in the following commit.
2020-01-21 12:42:03 -08:00
|
|
|
tracker =
|
|
|
|
c_start sampling_rate callstack_size tracker
|
2020-01-10 07:59:20 -08:00
|
|
|
|
|
|
|
external stop : unit -> unit = "caml_memprof_stop"
|
2019-04-23 00:27:31 -07:00
|
|
|
end
|