caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Accuracy of Gc.stat ()
@ 2003-11-19 17:52 Daniel Bünzli
  2003-11-21 16:46 ` Damien Doligez
  2003-11-22  0:20 ` Kim Nguyen
  0 siblings, 2 replies; 9+ messages in thread
From: Daniel Bünzli @ 2003-11-19 17:52 UTC (permalink / raw)
  To: caml-list

Since my first attempt [1] didn't really get through, I try to 
reformulate my post.

To compare different implementations of a function I would like to 
profile its execution in time and heap memory usage (at least orders of 
magnitude). To do so, I use the code given at the end of this email. 
Basically I do a full major collection, get the gc statistics via the 
Gc.stat () function, run my function, call again Gc.stat (), and 
substract the former statistics to the latters.

My questions are :

1) What is the accuracy of these results ?

E.g. I read in the documentation of the Gc module that the field 
minor_words is only an approximation in programs compiled to native 
code. Is it also true for the other fields ? Would the figure 
minor+major-promoted be accurate ? How much can I trust the figures I 
get ?

2) When I start profiling should I prefer a Gc.compact to a 
Gc.full_major ?

3) Is it possible to know at runtime whether we are running native code 
or interpreted bytecode ?

Regarding time profiling, a binding in the Unix module to the 
getrusage() function would definitvely be nice.

Thanks for your answers,

Daniel

[1] <http://caml.inria.fr/archives/200311/msg00217.html>

-- profile.ml --

type t = { minor_bytes : float;
	   promoted_bytes : float;
	   major_bytes : float;
	   allocated_bytes : float;
	
	   minor_collections : float;
	   major_collections : float;
	
	   user_time : float;
	   system_time : float }

(* Bytes per words *)
let bpw = float_of_int (Sys.word_size / 8)

(* Heap allocation overhead due to profiling *)
let heap_overhead =
   let s = Gc.stat() in
   ignore(Unix.times());
   ignore(Unix.times());
   let s' = Gc.stat() in
   ((s'.Gc.minor_words +. s'.Gc.major_words -. s'.Gc.promoted_words) -.
   (s.Gc.minor_words +. s.Gc.major_words -. s.Gc.promoted_words)) *. bpw

let execution_stats_n n f a =
   Gc.full_major ();
   let s = Gc.stat () in
   let t = Unix.times () in
   for i = 1 to n do
     ignore(f a)
   done;
   let t' = Unix.times () in
   let s' = Gc.stat () in
   let mi, pro, ma =
     ((s'.Gc.minor_words -. s.Gc.minor_words) *. bpw) -. heap_overhead,
     (s'.Gc.promoted_words -. s.Gc.promoted_words) *. bpw,
     (s'.Gc.major_words -. s.Gc.major_words) *. bpw in
   let n' = float_of_int n in
   { minor_bytes = mi /. n';
     promoted_bytes = pro /. n';
     major_bytes = ma /. n';
     allocated_bytes = (mi +. ma -. pro) /. n';

     minor_collections =
       (float_of_int (s'.Gc.minor_collections - s.Gc.minor_collections)) 
/. n';
     major_collections =
       (float_of_int (s'.Gc.major_collections - s.Gc.major_collections)) 
/. n';

     user_time = (t'.Unix.tms_utime -. t.Unix.tms_utime) /. n';
     system_time = (t'.Unix.tms_stime -. t.Unix.tms_stime) /. n'
   }

let execution_stats f a = execution_stats_n 1000 f a
let execution f a = execution_stats_n 1 f a

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2003-11-22 14:31 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-11-19 17:52 [Caml-list] Accuracy of Gc.stat () Daniel Bünzli
2003-11-21 16:46 ` Damien Doligez
2003-11-22  0:20 ` Kim Nguyen
2003-11-22 11:43   ` Richard Jones
2003-11-22 11:49     ` Richard Jones
2003-11-22 14:20       ` Self-detection of native code execution (Was Re: [Caml-list] Accuracy of Gc.stat ()) Daniel Bünzli
2003-11-22 14:28         ` Richard Jones
2003-11-22 14:28       ` [Caml-list] Accuracy of Gc.stat () Kim Nguyen
2003-11-22 14:31         ` Richard Jones

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).