1995-10-24 08:37:39 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
1995-10-24 08:37:39 -07:00
|
|
|
(* *)
|
|
|
|
(* Damien Doligez and Francois Rouaix, INRIA Rocquencourt *)
|
|
|
|
(* Ported to Caml Special Light by John Malecki *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
1995-10-24 08:37:39 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
open Printf
|
|
|
|
|
|
|
|
open Location
|
|
|
|
open Parsetree
|
|
|
|
|
2004-06-16 08:41:59 -07:00
|
|
|
(* User programs must not use identifiers that start with these prefixes. *)
|
2004-06-16 09:58:46 -07:00
|
|
|
let idprefix = "__ocaml_prof_";;
|
2004-06-16 08:41:59 -07:00
|
|
|
let modprefix = "OCAML__prof_";;
|
1999-08-03 10:59:18 -07:00
|
|
|
|
1995-10-24 08:37:39 -07:00
|
|
|
(* Errors specific to the profiler *)
|
|
|
|
exception Profiler of string
|
|
|
|
|
|
|
|
(* Modes *)
|
|
|
|
let instr_fun = ref false
|
|
|
|
and instr_match = ref false
|
1997-05-19 08:42:21 -07:00
|
|
|
and instr_if = ref false
|
1995-10-24 08:37:39 -07:00
|
|
|
and instr_loops = ref false
|
1997-05-19 08:42:21 -07:00
|
|
|
and instr_try = ref false
|
1995-10-24 08:37:39 -07:00
|
|
|
|
|
|
|
let cur_point = ref 0
|
|
|
|
and inchan = ref stdin
|
|
|
|
and outchan = ref stdout
|
|
|
|
|
1999-02-26 01:41:53 -08:00
|
|
|
(* To copy source fragments *)
|
2014-04-29 04:56:17 -07:00
|
|
|
let copy_buffer = Bytes.create 256
|
1999-02-26 01:41:53 -08:00
|
|
|
|
|
|
|
let copy_chars_unix nchars =
|
|
|
|
let n = ref nchars in
|
|
|
|
while !n > 0 do
|
|
|
|
let m = input !inchan copy_buffer 0 (min !n 256) in
|
|
|
|
if m = 0 then raise End_of_file;
|
|
|
|
output !outchan copy_buffer 0 m;
|
|
|
|
n := !n - m
|
|
|
|
done
|
|
|
|
|
|
|
|
let copy_chars_win32 nchars =
|
2012-12-19 01:25:21 -08:00
|
|
|
for _i = 1 to nchars do
|
1999-02-26 01:41:53 -08:00
|
|
|
let c = input_char !inchan in
|
|
|
|
if c <> '\r' then output_char !outchan c
|
|
|
|
done
|
|
|
|
|
|
|
|
let copy_chars =
|
|
|
|
match Sys.os_type with
|
2000-08-10 02:58:08 -07:00
|
|
|
"Win32" | "Cygwin" -> copy_chars_win32
|
1999-02-26 01:41:53 -08:00
|
|
|
| _ -> copy_chars_unix
|
|
|
|
|
1995-10-24 08:37:39 -07:00
|
|
|
let copy next =
|
2002-11-04 02:49:35 -08:00
|
|
|
assert (next >= !cur_point);
|
|
|
|
seek_in !inchan !cur_point;
|
|
|
|
copy_chars (next - !cur_point);
|
|
|
|
cur_point := next;
|
|
|
|
;;
|
1995-10-24 08:37:39 -07:00
|
|
|
|
1999-09-08 10:42:52 -07:00
|
|
|
let prof_counter = ref 0;;
|
1995-10-24 08:37:39 -07:00
|
|
|
|
|
|
|
let instr_mode = ref false
|
|
|
|
|
1999-09-08 10:42:52 -07:00
|
|
|
type insert = Open | Close;;
|
|
|
|
let to_insert = ref ([] : (insert * int) list);;
|
1999-08-03 10:59:18 -07:00
|
|
|
|
1999-09-08 10:42:52 -07:00
|
|
|
let insert_action st en =
|
|
|
|
to_insert := (Open, st) :: (Close, en) :: !to_insert
|
|
|
|
;;
|
|
|
|
|
|
|
|
(* Producing instrumented code *)
|
|
|
|
let add_incr_counter modul (kind,pos) =
|
|
|
|
copy pos;
|
|
|
|
match kind with
|
1999-10-14 06:41:12 -07:00
|
|
|
| Open ->
|
2005-03-24 09:20:54 -08:00
|
|
|
fprintf !outchan "(%sProfiling.incr %s%s_cnt %d; "
|
|
|
|
modprefix idprefix modul !prof_counter;
|
1999-09-08 10:42:52 -07:00
|
|
|
incr prof_counter;
|
2005-03-24 09:20:54 -08:00
|
|
|
| Close -> fprintf !outchan ")";
|
1999-09-08 10:42:52 -07:00
|
|
|
;;
|
1995-10-24 08:37:39 -07:00
|
|
|
|
2014-08-22 06:45:02 -07:00
|
|
|
let counters = ref (Array.make 0 0)
|
1995-10-24 08:37:39 -07:00
|
|
|
|
|
|
|
(* User defined marker *)
|
|
|
|
let special_id = ref ""
|
|
|
|
|
|
|
|
(* Producing results of profile run *)
|
1999-09-08 10:42:52 -07:00
|
|
|
let add_val_counter (kind,pos) =
|
|
|
|
if kind = Open then begin
|
|
|
|
copy pos;
|
2006-09-20 04:14:37 -07:00
|
|
|
fprintf !outchan "(* %s%d *) " !special_id !counters.(!prof_counter);
|
1999-09-08 10:42:52 -07:00
|
|
|
incr prof_counter;
|
|
|
|
end
|
|
|
|
;;
|
1995-10-24 08:37:39 -07:00
|
|
|
|
|
|
|
(* ************* rewrite ************* *)
|
|
|
|
|
1999-09-08 10:42:52 -07:00
|
|
|
let insert_profile rw_exp ex =
|
2002-11-01 09:06:47 -08:00
|
|
|
let st = ex.pexp_loc.loc_start.Lexing.pos_cnum
|
|
|
|
and en = ex.pexp_loc.loc_end.Lexing.pos_cnum
|
1999-09-08 10:42:52 -07:00
|
|
|
and gh = ex.pexp_loc.loc_ghost
|
|
|
|
in
|
|
|
|
if gh || st = en then
|
|
|
|
rw_exp true ex
|
1998-05-23 07:12:38 -07:00
|
|
|
else begin
|
1999-09-08 10:42:52 -07:00
|
|
|
insert_action st en;
|
|
|
|
rw_exp false ex;
|
1998-01-12 08:54:22 -08:00
|
|
|
end
|
|
|
|
;;
|
1995-10-24 08:37:39 -07:00
|
|
|
|
|
|
|
|
|
|
|
let pos_len = ref 0
|
|
|
|
|
|
|
|
let init_rewrite modes mod_name =
|
|
|
|
cur_point := 0;
|
|
|
|
if !instr_mode then begin
|
2005-03-24 09:20:54 -08:00
|
|
|
fprintf !outchan "module %sProfiling = Profiling;; " modprefix;
|
2014-08-22 06:45:02 -07:00
|
|
|
fprintf !outchan "let %s%s_cnt = Array.make 000000000" idprefix mod_name;
|
1995-10-24 08:37:39 -07:00
|
|
|
pos_len := pos_out !outchan;
|
2006-09-20 04:14:37 -07:00
|
|
|
fprintf !outchan
|
1999-08-03 10:59:18 -07:00
|
|
|
" 0;; Profiling.counters := \
|
2005-03-24 09:20:54 -08:00
|
|
|
(\"%s\", (\"%s\", %s%s_cnt)) :: !Profiling.counters;; "
|
|
|
|
mod_name modes idprefix mod_name;
|
1995-10-24 08:37:39 -07:00
|
|
|
end
|
|
|
|
|
1999-09-08 10:42:52 -07:00
|
|
|
let final_rewrite add_function =
|
2014-08-22 06:45:02 -07:00
|
|
|
to_insert := List.sort (fun x y -> compare (snd x) (snd y)) !to_insert;
|
1999-09-08 10:42:52 -07:00
|
|
|
prof_counter := 0;
|
|
|
|
List.iter add_function !to_insert;
|
1995-10-24 08:37:39 -07:00
|
|
|
copy (in_channel_length !inchan);
|
|
|
|
if !instr_mode then begin
|
1999-09-08 10:42:52 -07:00
|
|
|
let len = string_of_int !prof_counter in
|
2005-03-24 09:20:54 -08:00
|
|
|
if String.length len > 9 then raise (Profiler "too many counters");
|
1999-09-08 10:42:52 -07:00
|
|
|
seek_out !outchan (!pos_len - String.length len);
|
|
|
|
output_string !outchan len
|
|
|
|
end;
|
2001-11-27 07:09:12 -08:00
|
|
|
(* Cannot close because outchan is stdout and Format doesn't like
|
|
|
|
a closed stdout.
|
|
|
|
close_out !outchan;
|
|
|
|
*)
|
1999-09-08 10:42:52 -07:00
|
|
|
;;
|
|
|
|
|
|
|
|
let rec rewrite_patexp_list iflag l =
|
2013-06-03 08:14:19 -07:00
|
|
|
rewrite_exp_list iflag (List.map (fun x -> x.pvb_expr) l)
|
1995-10-24 08:37:39 -07:00
|
|
|
|
2013-04-15 09:23:22 -07:00
|
|
|
and rewrite_cases iflag l =
|
|
|
|
List.iter
|
|
|
|
(fun pc ->
|
|
|
|
begin match pc.pc_guard with
|
|
|
|
| None -> ()
|
|
|
|
| Some g -> rewrite_exp iflag g
|
|
|
|
end;
|
|
|
|
rewrite_exp iflag pc.pc_rhs
|
|
|
|
)
|
|
|
|
l
|
1995-10-24 08:37:39 -07:00
|
|
|
|
1999-09-08 10:42:52 -07:00
|
|
|
and rewrite_labelexp_list iflag l =
|
|
|
|
rewrite_exp_list iflag (List.map snd l)
|
1995-10-24 08:37:39 -07:00
|
|
|
|
1999-09-08 10:42:52 -07:00
|
|
|
and rewrite_exp_list iflag l =
|
|
|
|
List.iter (rewrite_exp iflag) l
|
1995-10-24 08:37:39 -07:00
|
|
|
|
1999-09-08 10:42:52 -07:00
|
|
|
and rewrite_exp iflag sexp =
|
|
|
|
if iflag then insert_profile rw_exp sexp
|
|
|
|
else rw_exp false sexp
|
1995-10-24 08:37:39 -07:00
|
|
|
|
1999-09-08 10:42:52 -07:00
|
|
|
and rw_exp iflag sexp =
|
1995-10-24 08:37:39 -07:00
|
|
|
match sexp.pexp_desc with
|
2014-05-12 03:41:21 -07:00
|
|
|
Pexp_ident _lid -> ()
|
|
|
|
| Pexp_constant _cst -> ()
|
1995-10-24 08:37:39 -07:00
|
|
|
|
|
|
|
| Pexp_let(_, spat_sexp_list, sbody) ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_patexp_list iflag spat_sexp_list;
|
|
|
|
rewrite_exp iflag sbody
|
1995-10-24 08:37:39 -07:00
|
|
|
|
2013-04-17 04:43:29 -07:00
|
|
|
| Pexp_function caselist ->
|
2004-06-11 06:22:19 -07:00
|
|
|
if !instr_fun then
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_function iflag caselist
|
1995-10-24 08:37:39 -07:00
|
|
|
else
|
2013-04-15 09:23:22 -07:00
|
|
|
rewrite_cases iflag caselist
|
1995-10-24 08:37:39 -07:00
|
|
|
|
2013-04-17 04:43:29 -07:00
|
|
|
| Pexp_fun (_, _, p, e) ->
|
|
|
|
let l = [{pc_lhs=p; pc_guard=None; pc_rhs=e}] in
|
|
|
|
if !instr_fun then
|
|
|
|
rewrite_function iflag l
|
|
|
|
else
|
|
|
|
rewrite_cases iflag l
|
|
|
|
|
1995-10-24 08:37:39 -07:00
|
|
|
| Pexp_match(sarg, caselist) ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_exp iflag sarg;
|
1999-09-14 10:20:03 -07:00
|
|
|
if !instr_match && not sexp.pexp_loc.loc_ghost then
|
1995-10-24 08:37:39 -07:00
|
|
|
rewrite_funmatching caselist
|
|
|
|
else
|
2013-04-15 09:23:22 -07:00
|
|
|
rewrite_cases iflag caselist
|
1995-10-24 08:37:39 -07:00
|
|
|
|
|
|
|
| Pexp_try(sbody, caselist) ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_exp iflag sbody;
|
1999-09-14 10:20:03 -07:00
|
|
|
if !instr_try && not sexp.pexp_loc.loc_ghost then
|
1995-10-24 08:37:39 -07:00
|
|
|
rewrite_trymatching caselist
|
|
|
|
else
|
2013-04-15 09:23:22 -07:00
|
|
|
rewrite_cases iflag caselist
|
1995-10-24 08:37:39 -07:00
|
|
|
|
|
|
|
| Pexp_apply(sfunct, sargs) ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_exp iflag sfunct;
|
1999-11-30 08:07:38 -08:00
|
|
|
rewrite_exp_list iflag (List.map snd sargs)
|
1995-10-24 08:37:39 -07:00
|
|
|
|
|
|
|
| Pexp_tuple sexpl ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_exp_list iflag sexpl
|
1995-10-24 08:37:39 -07:00
|
|
|
|
2013-04-17 02:46:52 -07:00
|
|
|
| Pexp_construct(_, None) -> ()
|
|
|
|
| Pexp_construct(_, Some sarg) ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_exp iflag sarg
|
1995-10-24 08:37:39 -07:00
|
|
|
|
1999-11-30 08:07:38 -08:00
|
|
|
| Pexp_variant(_, None) -> ()
|
|
|
|
| Pexp_variant(_, Some sarg) ->
|
|
|
|
rewrite_exp iflag sarg
|
|
|
|
|
1999-09-08 10:42:52 -07:00
|
|
|
| Pexp_record(lid_sexp_list, None) ->
|
|
|
|
rewrite_labelexp_list iflag lid_sexp_list
|
|
|
|
| Pexp_record(lid_sexp_list, Some sexp) ->
|
|
|
|
rewrite_exp iflag sexp;
|
|
|
|
rewrite_labelexp_list iflag lid_sexp_list
|
1995-10-24 08:37:39 -07:00
|
|
|
|
|
|
|
| Pexp_field(sarg, _) ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_exp iflag sarg
|
1995-10-24 08:37:39 -07:00
|
|
|
|
|
|
|
| Pexp_setfield(srecord, _, snewval) ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_exp iflag srecord;
|
|
|
|
rewrite_exp iflag snewval
|
1995-10-24 08:37:39 -07:00
|
|
|
|
|
|
|
| Pexp_array(sargl) ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_exp_list iflag sargl
|
1995-10-24 08:37:39 -07:00
|
|
|
|
|
|
|
| Pexp_ifthenelse(scond, sifso, None) ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_exp iflag scond;
|
1999-09-14 10:20:03 -07:00
|
|
|
rewrite_ifbody iflag sexp.pexp_loc.loc_ghost sifso
|
1995-10-24 08:37:39 -07:00
|
|
|
| Pexp_ifthenelse(scond, sifso, Some sifnot) ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_exp iflag scond;
|
1999-09-14 10:20:03 -07:00
|
|
|
rewrite_ifbody iflag sexp.pexp_loc.loc_ghost sifso;
|
|
|
|
rewrite_ifbody iflag sexp.pexp_loc.loc_ghost sifnot
|
2006-09-20 04:14:37 -07:00
|
|
|
|
1995-10-24 08:37:39 -07:00
|
|
|
| Pexp_sequence(sexp1, sexp2) ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_exp iflag sexp1;
|
|
|
|
rewrite_exp iflag sexp2
|
1995-10-24 08:37:39 -07:00
|
|
|
|
|
|
|
| Pexp_while(scond, sbody) ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_exp iflag scond;
|
1999-09-14 10:20:03 -07:00
|
|
|
if !instr_loops && not sexp.pexp_loc.loc_ghost
|
|
|
|
then insert_profile rw_exp sbody
|
1999-09-08 10:42:52 -07:00
|
|
|
else rewrite_exp iflag sbody
|
1995-10-24 08:37:39 -07:00
|
|
|
|
|
|
|
| Pexp_for(_, slow, shigh, _, sbody) ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_exp iflag slow;
|
|
|
|
rewrite_exp iflag shigh;
|
1999-09-14 10:20:03 -07:00
|
|
|
if !instr_loops && not sexp.pexp_loc.loc_ghost
|
|
|
|
then insert_profile rw_exp sbody
|
1999-09-08 10:42:52 -07:00
|
|
|
else rewrite_exp iflag sbody
|
1995-10-24 08:37:39 -07:00
|
|
|
|
2013-04-17 05:23:44 -07:00
|
|
|
| Pexp_constraint(sarg, _) | Pexp_coerce(sarg, _, _) ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_exp iflag sarg
|
1995-10-24 08:37:39 -07:00
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
| Pexp_send (sobj, _) ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_exp iflag sobj
|
1996-04-22 04:15:41 -07:00
|
|
|
|
|
|
|
| Pexp_new _ -> ()
|
|
|
|
|
|
|
|
| Pexp_setinstvar (_, sarg) ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_exp iflag sarg
|
1996-04-22 04:15:41 -07:00
|
|
|
|
|
|
|
| Pexp_override l ->
|
1999-09-08 10:42:52 -07:00
|
|
|
List.iter (fun (_, sexp) -> rewrite_exp iflag sexp) l
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
| Pexp_letmodule (_, smod, sexp) ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_mod iflag smod;
|
|
|
|
rewrite_exp iflag sexp
|
1998-06-24 12:22:26 -07:00
|
|
|
|
2000-12-04 07:37:05 -08:00
|
|
|
| Pexp_assert (cond) -> rewrite_exp iflag cond
|
|
|
|
|
2002-01-20 09:39:10 -08:00
|
|
|
| Pexp_lazy (expr) -> rewrite_exp iflag expr
|
|
|
|
|
2002-04-18 00:27:47 -07:00
|
|
|
| Pexp_poly (sexp, _) -> rewrite_exp iflag sexp
|
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
| Pexp_object cl ->
|
|
|
|
List.iter (rewrite_class_field iflag) cl.pcstr_fields
|
2003-11-25 01:20:45 -08:00
|
|
|
|
2009-11-01 13:52:29 -08:00
|
|
|
| Pexp_newtype (_, sexp) -> rewrite_exp iflag sexp
|
2013-05-16 06:34:53 -07:00
|
|
|
| Pexp_open (_ovf, _, e) -> rewrite_exp iflag e
|
2010-10-21 16:59:33 -07:00
|
|
|
| Pexp_pack (smod) -> rewrite_mod iflag smod
|
2013-02-28 08:51:59 -08:00
|
|
|
| Pexp_extension _ -> ()
|
2009-10-06 05:51:42 -07:00
|
|
|
|
1999-09-14 10:20:03 -07:00
|
|
|
and rewrite_ifbody iflag ghost sifbody =
|
|
|
|
if !instr_if && not ghost then
|
1999-09-08 10:42:52 -07:00
|
|
|
insert_profile rw_exp sifbody
|
1995-10-24 08:37:39 -07:00
|
|
|
else
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_exp iflag sifbody
|
1995-10-24 08:37:39 -07:00
|
|
|
|
|
|
|
(* called only when !instr_fun *)
|
|
|
|
and rewrite_annotate_exp_list l =
|
|
|
|
List.iter
|
1999-09-14 10:20:03 -07:00
|
|
|
(function
|
2013-04-15 09:23:22 -07:00
|
|
|
| {pc_guard=Some scond; pc_rhs=sbody} ->
|
|
|
|
insert_profile rw_exp scond;
|
|
|
|
insert_profile rw_exp sbody;
|
2013-04-17 05:23:44 -07:00
|
|
|
| {pc_rhs={pexp_desc = Pexp_constraint(sbody, _)}} (* let f x : t = e *)
|
1999-09-14 10:20:03 -07:00
|
|
|
-> insert_profile rw_exp sbody
|
2013-04-15 09:23:22 -07:00
|
|
|
| {pc_rhs=sexp} -> insert_profile rw_exp sexp)
|
1995-10-24 08:37:39 -07:00
|
|
|
l
|
|
|
|
|
1999-09-08 10:42:52 -07:00
|
|
|
and rewrite_function iflag = function
|
2014-05-12 03:41:21 -07:00
|
|
|
| [{pc_lhs=_; pc_guard=None;
|
2013-04-17 04:43:29 -07:00
|
|
|
pc_rhs={pexp_desc = (Pexp_function _|Pexp_fun _)} as sexp}] ->
|
|
|
|
rewrite_exp iflag sexp
|
1995-10-24 08:37:39 -07:00
|
|
|
| l -> rewrite_funmatching l
|
|
|
|
|
2006-09-20 04:14:37 -07:00
|
|
|
and rewrite_funmatching l =
|
2013-04-15 09:23:22 -07:00
|
|
|
rewrite_annotate_exp_list l
|
1995-10-24 08:37:39 -07:00
|
|
|
|
|
|
|
and rewrite_trymatching l =
|
2013-04-15 09:23:22 -07:00
|
|
|
rewrite_annotate_exp_list l
|
1995-10-24 08:37:39 -07:00
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
(* Rewrite a class definition *)
|
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
and rewrite_class_field iflag cf =
|
|
|
|
match cf.pcf_desc with
|
2013-04-10 04:17:41 -07:00
|
|
|
Pcf_inherit (_, cexpr, _) -> rewrite_class_expr iflag cexpr
|
|
|
|
| Pcf_val (_, _, Cfk_concrete (_, sexp)) -> rewrite_exp iflag sexp
|
|
|
|
| Pcf_method (_, _,
|
2013-04-17 04:43:29 -07:00
|
|
|
Cfk_concrete (_, ({pexp_desc = (Pexp_function _|Pexp_fun _)}
|
|
|
|
as sexp))) ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_exp iflag sexp
|
2013-04-10 04:17:41 -07:00
|
|
|
| Pcf_method (_, _, Cfk_concrete(_, sexp)) ->
|
2012-05-30 07:52:37 -07:00
|
|
|
let loc = cf.pcf_loc in
|
1999-09-14 10:20:03 -07:00
|
|
|
if !instr_fun && not loc.loc_ghost then insert_profile rw_exp sexp
|
1999-09-08 10:42:52 -07:00
|
|
|
else rewrite_exp iflag sexp
|
2013-04-10 04:17:41 -07:00
|
|
|
| Pcf_initializer sexp ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_exp iflag sexp
|
2013-04-10 04:17:41 -07:00
|
|
|
| Pcf_method (_, _, Cfk_virtual _)
|
|
|
|
| Pcf_val (_, _, Cfk_virtual _)
|
|
|
|
| Pcf_constraint _ -> ()
|
2014-05-04 13:42:34 -07:00
|
|
|
| Pcf_attribute _ -> ()
|
2013-04-10 10:54:54 -07:00
|
|
|
| Pcf_extension _ -> ()
|
1998-06-24 12:22:26 -07:00
|
|
|
|
1999-09-08 10:42:52 -07:00
|
|
|
and rewrite_class_expr iflag cexpr =
|
1998-06-24 12:22:26 -07:00
|
|
|
match cexpr.pcl_desc with
|
|
|
|
Pcl_constr _ -> ()
|
2012-05-30 07:52:37 -07:00
|
|
|
| Pcl_structure st ->
|
|
|
|
List.iter (rewrite_class_field iflag) st.pcstr_fields
|
1999-11-30 08:07:38 -08:00
|
|
|
| Pcl_fun (_, _, _, cexpr) ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_class_expr iflag cexpr
|
1998-06-24 12:22:26 -07:00
|
|
|
| Pcl_apply (cexpr, exprs) ->
|
1999-11-30 08:07:38 -08:00
|
|
|
rewrite_class_expr iflag cexpr;
|
|
|
|
List.iter (rewrite_exp iflag) (List.map snd exprs)
|
1998-06-24 12:22:26 -07:00
|
|
|
| Pcl_let (_, spat_sexp_list, cexpr) ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_patexp_list iflag spat_sexp_list;
|
|
|
|
rewrite_class_expr iflag cexpr
|
1998-06-24 12:22:26 -07:00
|
|
|
| Pcl_constraint (cexpr, _) ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_class_expr iflag cexpr
|
2013-04-10 10:26:55 -07:00
|
|
|
| Pcl_extension _ -> ()
|
1998-06-24 12:22:26 -07:00
|
|
|
|
1999-09-08 10:42:52 -07:00
|
|
|
and rewrite_class_declaration iflag cl =
|
|
|
|
rewrite_class_expr iflag cl.pci_expr
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1995-10-24 08:37:39 -07:00
|
|
|
(* Rewrite a module expression or structure expression *)
|
|
|
|
|
1999-09-08 10:42:52 -07:00
|
|
|
and rewrite_mod iflag smod =
|
1995-10-24 08:37:39 -07:00
|
|
|
match smod.pmod_desc with
|
2014-05-12 03:41:21 -07:00
|
|
|
Pmod_ident _ -> ()
|
1999-09-08 10:42:52 -07:00
|
|
|
| Pmod_structure sstr -> List.iter (rewrite_str_item iflag) sstr
|
2014-05-12 03:41:21 -07:00
|
|
|
| Pmod_functor(_param, _smty, sbody) -> rewrite_mod iflag sbody
|
1999-09-08 10:42:52 -07:00
|
|
|
| Pmod_apply(smod1, smod2) -> rewrite_mod iflag smod1; rewrite_mod iflag smod2
|
2014-05-12 03:41:21 -07:00
|
|
|
| Pmod_constraint(smod, _smty) -> rewrite_mod iflag smod
|
2010-10-21 16:59:33 -07:00
|
|
|
| Pmod_unpack(sexp) -> rewrite_exp iflag sexp
|
2013-03-04 04:54:57 -08:00
|
|
|
| Pmod_extension _ -> ()
|
1995-10-24 08:37:39 -07:00
|
|
|
|
1999-09-08 10:42:52 -07:00
|
|
|
and rewrite_str_item iflag item =
|
1995-10-24 08:37:39 -07:00
|
|
|
match item.pstr_desc with
|
2013-04-11 07:07:32 -07:00
|
|
|
Pstr_eval (exp, _attrs) -> rewrite_exp iflag exp
|
2013-06-03 08:14:19 -07:00
|
|
|
| Pstr_value(_, exps)
|
|
|
|
-> List.iter (fun x -> rewrite_exp iflag x.pvb_expr) exps
|
2013-03-06 02:12:21 -08:00
|
|
|
| Pstr_module x -> rewrite_mod iflag x.pmb_expr
|
|
|
|
(* todo: Pstr_recmodule?? *)
|
1999-09-08 10:42:52 -07:00
|
|
|
| Pstr_class classes -> List.iter (rewrite_class_declaration iflag) classes
|
1995-10-24 08:37:39 -07:00
|
|
|
| _ -> ()
|
|
|
|
|
|
|
|
(* Rewrite a .ml file *)
|
1999-09-08 10:42:52 -07:00
|
|
|
let rewrite_file srcfile add_function =
|
1995-10-24 08:37:39 -07:00
|
|
|
inchan := open_in_bin srcfile;
|
|
|
|
let lb = Lexing.from_channel !inchan in
|
|
|
|
Location.input_name := srcfile;
|
2002-11-01 09:06:47 -08:00
|
|
|
Location.init lb srcfile;
|
1999-09-08 10:42:52 -07:00
|
|
|
List.iter (rewrite_str_item false) (Parse.implementation lb);
|
|
|
|
final_rewrite add_function;
|
1995-10-24 08:37:39 -07:00
|
|
|
close_in !inchan
|
|
|
|
|
1999-08-03 10:59:18 -07:00
|
|
|
(* Copy a non-.ml file without change *)
|
|
|
|
let null_rewrite srcfile =
|
|
|
|
inchan := open_in_bin srcfile;
|
|
|
|
copy (in_channel_length !inchan);
|
|
|
|
close_in !inchan
|
|
|
|
;;
|
|
|
|
|
1995-10-24 08:37:39 -07:00
|
|
|
(* Setting flags from saved config *)
|
|
|
|
let set_flags s =
|
|
|
|
for i = 0 to String.length s - 1 do
|
|
|
|
match String.get s i with
|
|
|
|
'f' -> instr_fun := true
|
|
|
|
| 'm' -> instr_match := true
|
|
|
|
| 'i' -> instr_if := true
|
|
|
|
| 'l' -> instr_loops := true
|
|
|
|
| 't' -> instr_try := true
|
|
|
|
| 'a' -> instr_fun := true; instr_match := true;
|
1997-05-19 08:42:21 -07:00
|
|
|
instr_if := true; instr_loops := true;
|
|
|
|
instr_try := true
|
1995-10-24 08:37:39 -07:00
|
|
|
| _ -> ()
|
|
|
|
done
|
|
|
|
|
|
|
|
(* Command-line options *)
|
|
|
|
|
|
|
|
let modes = ref "fm"
|
1996-11-16 07:49:37 -08:00
|
|
|
let dumpfile = ref "ocamlprof.dump"
|
1995-10-24 08:37:39 -07:00
|
|
|
|
|
|
|
(* Process a file *)
|
|
|
|
|
2002-11-04 02:49:35 -08:00
|
|
|
let process_intf_file filename = null_rewrite filename;;
|
|
|
|
|
|
|
|
let process_impl_file filename =
|
|
|
|
let modname = Filename.basename(Filename.chop_extension filename) in
|
2004-04-15 09:19:37 -07:00
|
|
|
(* FIXME should let modname = String.capitalize modname *)
|
1999-08-03 10:59:18 -07:00
|
|
|
if !instr_mode then begin
|
|
|
|
(* Instrumentation mode *)
|
|
|
|
set_flags !modes;
|
|
|
|
init_rewrite !modes modname;
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_file filename (add_incr_counter modname);
|
1999-08-03 10:59:18 -07:00
|
|
|
end else begin
|
|
|
|
(* Results mode *)
|
|
|
|
let ic = open_in_bin !dumpfile in
|
|
|
|
let allcounters =
|
|
|
|
(input_value ic : (string * (string * int array)) list) in
|
|
|
|
close_in ic;
|
|
|
|
let (modes, cv) =
|
|
|
|
try
|
|
|
|
List.assoc modname allcounters
|
|
|
|
with Not_found ->
|
|
|
|
raise(Profiler("Module " ^ modname ^ " not used in this profile."))
|
|
|
|
in
|
|
|
|
counters := cv;
|
|
|
|
set_flags modes;
|
|
|
|
init_rewrite modes modname;
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_file filename add_val_counter;
|
1999-08-03 10:59:18 -07:00
|
|
|
end
|
2002-11-04 02:49:35 -08:00
|
|
|
;;
|
|
|
|
|
|
|
|
let process_anon_file filename =
|
|
|
|
if Filename.check_suffix filename ".ml" then
|
|
|
|
process_impl_file filename
|
|
|
|
else
|
|
|
|
process_intf_file filename
|
|
|
|
;;
|
1995-10-24 08:37:39 -07:00
|
|
|
|
|
|
|
(* Main function *)
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
open Format
|
1995-10-24 08:37:39 -07:00
|
|
|
|
1996-10-24 07:17:48 -07:00
|
|
|
let usage = "Usage: ocamlprof <options> <files>\noptions are:"
|
|
|
|
|
2006-09-20 04:14:37 -07:00
|
|
|
let print_version () =
|
|
|
|
printf "ocamlprof, version %s@." Sys.ocaml_version;
|
|
|
|
exit 0;
|
|
|
|
;;
|
|
|
|
|
2010-05-20 07:06:29 -07:00
|
|
|
let print_version_num () =
|
|
|
|
printf "%s@." Sys.ocaml_version;
|
|
|
|
exit 0;
|
|
|
|
;;
|
|
|
|
|
1995-10-24 08:37:39 -07:00
|
|
|
let main () =
|
|
|
|
try
|
2007-02-09 05:31:15 -08:00
|
|
|
Warnings.parse_options false "a";
|
1996-10-24 07:17:48 -07:00
|
|
|
Arg.parse [
|
|
|
|
"-f", Arg.String (fun s -> dumpfile := s),
|
2002-11-04 02:49:35 -08:00
|
|
|
"<file> Use <file> as dump file (default ocamlprof.dump)";
|
1996-10-24 07:17:48 -07:00
|
|
|
"-F", Arg.String (fun s -> special_id := s),
|
2002-11-04 02:49:35 -08:00
|
|
|
"<s> Insert string <s> with the counts";
|
|
|
|
"-impl", Arg.String process_impl_file,
|
|
|
|
"<file> Process <file> as a .ml file";
|
|
|
|
"-instrument", Arg.Set instr_mode, " (undocumented)";
|
|
|
|
"-intf", Arg.String process_intf_file,
|
|
|
|
"<file> Process <file> as a .mli file";
|
2006-09-20 04:14:37 -07:00
|
|
|
"-m", Arg.String (fun s -> modes := s), "<flags> (undocumented)";
|
|
|
|
"-version", Arg.Unit print_version,
|
|
|
|
" Print version and exit";
|
2010-05-20 07:06:29 -07:00
|
|
|
"-vnum", Arg.Unit print_version_num,
|
|
|
|
" Print version number and exit";
|
2002-11-04 02:49:35 -08:00
|
|
|
] process_anon_file usage;
|
1995-10-24 08:37:39 -07:00
|
|
|
exit 0
|
2013-09-12 07:45:03 -07:00
|
|
|
with
|
|
|
|
| Profiler msg ->
|
|
|
|
fprintf Format.err_formatter "@[%s@]@." msg;
|
|
|
|
exit 2
|
|
|
|
| exn ->
|
|
|
|
Location.report_exception Format.err_formatter exn
|
1995-10-24 08:37:39 -07:00
|
|
|
|
|
|
|
let _ = main ()
|