1 (**************************************************************************)
2 (* *)
3 (* OCaml *)
4 (* *)
5 (* Damien Doligez and Francois Rouaix, INRIA Rocquencourt *)
6 (* Ported to Caml Special Light by John Malecki and Xavier Leroy *)
7 (* *)
8 (* Copyright 1996 Institut National de Recherche en Informatique et *)
9 (* en Automatique. *)
10 (* *)
11 (* All rights reserved. This file is distributed under the terms of *)
12 (* the GNU Lesser General Public License version 2.1, with the *)
13 (* special exception on linking described in the file LICENSE. *)
14 (* *)
15 (**************************************************************************)
16
17 (* Run-time library for profiled programs *)
18
19 type profiling_counters = (string * (string * int array)) list
20
21 let counters = ref ([] : profiling_counters);;
22 let incr a i = a.(i) <- a.(i) + 1;;
23
24 exception Bad_profile
25
26 let dump_counters () =
27 let dumpfile =
28 try Sys.getenv "OCAMLPROF_DUMP" with Not_found -> "ocamlprof.dump"
29 in
30 begin try
31 let ic = open_in_bin dumpfile in
32 let prevl = (input_value ic : profiling_counters) in
33 close_in ic;
34 List.iter2
35 (fun (curname, (curmodes,curcount)) (prevname, (prevmodes,prevcount)) ->
36 if curname <> prevname
37 || curmodes <> prevmodes
38 || Array.length curcount <> Array.length prevcount
39 then raise Bad_profile)
40 !counters prevl;
41 List.iter2
42 (fun (_curname, (_,curcount)) (_prevname, (_,prevcount)) ->
43 for i = 0 to Array.length curcount - 1 do
44 curcount.(i) <- curcount.(i) + prevcount.(i)
45 done)
46 !counters prevl
47 with _ -> ()
48 end;
49 begin try
50 let oc = open_out_bin dumpfile in
51 output_value oc !counters;
52 close_out oc
53 with _ -> ()
54 end
55
56 let _ = at_exit dump_counters
57