1995-10-24 08:37:39 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
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
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
|
|
|
open Printf
|
|
|
|
|
|
|
|
open Clflags
|
|
|
|
open Config
|
|
|
|
open Location
|
|
|
|
open Misc
|
|
|
|
open Parsetree
|
|
|
|
|
1999-08-03 10:59:18 -07:00
|
|
|
(* User programs must not use identifiers that start with this prefix. *)
|
|
|
|
let idprefix = "__ocaml_prof";;
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
(* In case we forgot something *)
|
1999-09-08 10:42:52 -07:00
|
|
|
(*exception Inversion of int * int*)
|
1995-10-24 08:37:39 -07:00
|
|
|
|
1999-02-26 01:41:53 -08:00
|
|
|
(* To copy source fragments *)
|
|
|
|
let copy_buffer = String.create 256
|
|
|
|
|
|
|
|
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 =
|
|
|
|
for i = 1 to nchars do
|
|
|
|
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 =
|
1999-09-08 10:42:52 -07:00
|
|
|
if next < !cur_point then begin
|
|
|
|
(*raise (Inversion(!cur_point, next));*)
|
|
|
|
(*fprintf stderr "warning: inversion at %d, %d\n" !cur_point next;*)
|
|
|
|
assert false
|
|
|
|
end else begin
|
|
|
|
seek_in !inchan !cur_point;
|
|
|
|
copy_chars (next - !cur_point);
|
|
|
|
cur_point := next;
|
|
|
|
end
|
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
|
|
|
|
| Close -> fprintf !outchan ")";
|
1999-10-14 06:41:12 -07:00
|
|
|
| Open ->
|
1999-09-08 10:42:52 -07:00
|
|
|
fprintf !outchan
|
1999-10-14 06:41:12 -07:00
|
|
|
"(%s_cnt_%s_.(%d) <- Pervasives.succ %s_cnt_%s_.(%d); "
|
1999-09-08 10:42:52 -07:00
|
|
|
idprefix modul !prof_counter idprefix modul !prof_counter;
|
|
|
|
incr prof_counter;
|
|
|
|
;;
|
1995-10-24 08:37:39 -07:00
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
let counters = ref (Array.create 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;
|
|
|
|
fprintf !outchan "(* %s%d *) " !special_id !counters.(!prof_counter);
|
|
|
|
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 =
|
|
|
|
let st = ex.pexp_loc.loc_start
|
|
|
|
and en = ex.pexp_loc.loc_end
|
|
|
|
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
|
1999-09-08 10:42:52 -07:00
|
|
|
fprintf !outchan "let %s_cnt_%s_ = Array.create 0000000" idprefix mod_name;
|
1995-10-24 08:37:39 -07:00
|
|
|
pos_len := pos_out !outchan;
|
|
|
|
fprintf !outchan
|
1999-08-03 10:59:18 -07:00
|
|
|
" 0;; Profiling.counters := \
|
|
|
|
(\"%s\", (\"%s\", %s_cnt_%s_)) :: !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 =
|
|
|
|
to_insert := Sort.list (fun x y -> snd x < snd y) !to_insert;
|
|
|
|
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
|
|
|
|
if String.length len > 7 then raise (Profiler "too many counters");
|
|
|
|
seek_out !outchan (!pos_len - String.length len);
|
|
|
|
output_string !outchan len
|
|
|
|
end;
|
|
|
|
close_out !outchan;
|
|
|
|
;;
|
|
|
|
|
|
|
|
let rec rewrite_patexp_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_patlexp_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_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
|
|
|
|
Pexp_ident lid -> ()
|
|
|
|
| Pexp_constant cst -> ()
|
|
|
|
|
|
|
|
| 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
|
|
|
|
1999-11-30 08:07:38 -08:00
|
|
|
| Pexp_function (_, _, caselist) ->
|
1999-09-14 10:20:03 -07:00
|
|
|
if !instr_fun && not sexp.pexp_loc.loc_ghost then
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_function iflag caselist
|
1995-10-24 08:37:39 -07:00
|
|
|
else
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_patlexp_list iflag caselist
|
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
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_patlexp_list 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
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_patexp_list 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
|
|
|
|
1997-06-16 11:10:35 -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
|
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
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
| Pexp_constraint(sarg, _, _) ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_exp iflag sarg
|
1995-10-24 08:37:39 -07:00
|
|
|
|
|
|
|
| Pexp_when(scond, sbody) ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_exp iflag scond;
|
|
|
|
rewrite_exp iflag sbody
|
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
|
|
|
|
| Pexp_assertfalse -> ()
|
|
|
|
|
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
|
|
|
|
| {pexp_desc = Pexp_when(scond, sbody)}
|
|
|
|
-> insert_profile rw_exp scond;
|
|
|
|
insert_profile rw_exp sbody;
|
|
|
|
| {pexp_desc = Pexp_constraint(sbody, _, _)} (* let f x : t = e *)
|
|
|
|
-> insert_profile rw_exp sbody
|
|
|
|
| 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
|
1999-09-14 10:20:03 -07:00
|
|
|
| [spat, ({pexp_desc = Pexp_function _} as sexp)] -> rewrite_exp iflag sexp
|
1995-10-24 08:37:39 -07:00
|
|
|
| l -> rewrite_funmatching l
|
|
|
|
|
|
|
|
and rewrite_funmatching l =
|
|
|
|
rewrite_annotate_exp_list (List.map snd l)
|
|
|
|
|
|
|
|
and rewrite_trymatching l =
|
|
|
|
rewrite_annotate_exp_list (List.map snd l)
|
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
(* Rewrite a class definition *)
|
|
|
|
|
1999-09-08 10:42:52 -07:00
|
|
|
and rewrite_class_field iflag =
|
1996-04-22 04:15:41 -07:00
|
|
|
function
|
1999-09-08 10:42:52 -07:00
|
|
|
Pcf_inher (cexpr, _) -> rewrite_class_expr iflag cexpr
|
|
|
|
| Pcf_val (_, _, sexp, _) -> rewrite_exp iflag sexp
|
1997-05-11 14:48:21 -07:00
|
|
|
| Pcf_meth (_, _, ({pexp_desc = Pexp_function _} as sexp), _) ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_exp iflag sexp
|
1999-09-14 10:20:03 -07:00
|
|
|
| Pcf_meth (_, _, sexp, loc) ->
|
|
|
|
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
|
1998-06-24 12:22:26 -07:00
|
|
|
| Pcf_let(_, spat_sexp_list, _) ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_patexp_list iflag spat_sexp_list
|
1998-06-24 12:22:26 -07:00
|
|
|
| Pcf_init sexp ->
|
1999-09-08 10:42:52 -07:00
|
|
|
rewrite_exp iflag sexp
|
1998-06-24 12:22:26 -07:00
|
|
|
| Pcf_virt _ | Pcf_cstr _ -> ()
|
|
|
|
|
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 _ -> ()
|
|
|
|
| Pcl_structure (_, fields) ->
|
1999-09-08 10:42:52 -07:00
|
|
|
List.iter (rewrite_class_field iflag) 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
|
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
|
|
|
|
Pmod_ident lid -> ()
|
1999-09-08 10:42:52 -07:00
|
|
|
| Pmod_structure sstr -> List.iter (rewrite_str_item iflag) sstr
|
|
|
|
| Pmod_functor(param, smty, sbody) -> rewrite_mod iflag sbody
|
|
|
|
| Pmod_apply(smod1, smod2) -> rewrite_mod iflag smod1; rewrite_mod iflag smod2
|
|
|
|
| Pmod_constraint(smod, smty) -> rewrite_mod iflag smod
|
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
|
1999-09-08 10:42:52 -07:00
|
|
|
Pstr_eval exp -> rewrite_exp iflag exp
|
|
|
|
| Pstr_value(_, exps)
|
|
|
|
-> List.iter (function (_,exp) -> rewrite_exp iflag exp) exps
|
|
|
|
| Pstr_module(name, smod) -> rewrite_mod iflag smod
|
|
|
|
| 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;
|
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 *)
|
|
|
|
|
|
|
|
let process_file filename =
|
|
|
|
if not (Filename.check_suffix filename ".ml") then
|
1999-08-03 10:59:18 -07:00
|
|
|
null_rewrite filename
|
|
|
|
else
|
|
|
|
let modname = Filename.basename(Filename.chop_suffix filename ".ml") in
|
|
|
|
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
|
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:"
|
|
|
|
|
1995-10-24 08:37:39 -07:00
|
|
|
let main () =
|
|
|
|
try
|
1996-10-24 07:17:48 -07:00
|
|
|
Arg.parse [
|
|
|
|
"-f", Arg.String (fun s -> dumpfile := s),
|
|
|
|
"<file> Use <file> as dump file (default ocamlprof.dump)";
|
|
|
|
"-F", Arg.String (fun s -> special_id := s),
|
|
|
|
"<s> Insert string <s> with the counts";
|
|
|
|
"-instrument", Arg.Set instr_mode, " (undocumented)";
|
1999-02-27 13:56:28 -08:00
|
|
|
"-m", Arg.String (fun s -> modes := s), "<flags> (undocumented)"
|
1996-10-24 07:17:48 -07:00
|
|
|
] process_file usage;
|
1995-10-24 08:37:39 -07:00
|
|
|
exit 0
|
|
|
|
with x ->
|
2000-03-06 14:12:09 -08:00
|
|
|
let report_error ppf = function
|
|
|
|
| Lexer.Error(err, start, stop) ->
|
|
|
|
fprintf ppf "@[%a%a@]@."
|
|
|
|
Location.print {loc_start = start; loc_end = stop; loc_ghost = false}
|
1995-10-24 08:37:39 -07:00
|
|
|
Lexer.report_error err
|
1997-08-22 01:55:41 -07:00
|
|
|
| Syntaxerr.Error err ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "@[%a@]@."
|
1997-08-22 01:55:41 -07:00
|
|
|
Syntaxerr.report_error err
|
1995-10-24 08:37:39 -07:00
|
|
|
| Profiler msg ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "@[%s@]@." msg
|
1999-09-08 10:42:52 -07:00
|
|
|
(*
|
1995-10-24 08:37:39 -07:00
|
|
|
| Inversion(pos, next) ->
|
|
|
|
print_string "Internal error: inversion at char "; print_int pos;
|
|
|
|
print_string ", "; print_int next
|
1999-09-08 10:42:52 -07:00
|
|
|
*)
|
1995-10-24 08:37:39 -07:00
|
|
|
| Sys_error msg ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "@[I/O error:@ %s@]@." msg
|
|
|
|
| x -> raise x in
|
|
|
|
report_error Format.err_formatter x;
|
|
|
|
exit 2
|
1995-10-24 08:37:39 -07:00
|
|
|
|
|
|
|
let _ = main ()
|