aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/profile.ml
blob: 3e1d04607305d4ae64707d175c62fefa72ffc5da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311

(* $Id$ *)

(* This program is a small time-profiler for Caml-Special-Light *)

(* It requires the UNIX library *)

(* Adapted from Christophe Raffalli *)

(* To use it, link it with the program you want to profile (don not forget
"-cclib -lunix -custom unix.cma" among the link options).

To trace a function "f" you first need to get a key for it by using :

let fkey = declare_profile "f"

(the string is used to print the profile infomation). Warning: this
function does a side effect. Choose the ident you want instead "fkey".

Then if the function has one ONE argument add the following just after
the definition of "f" or just after the declare_profile if this one
follows the defintion of f.

let f = profile fkey f

If f has two arguments do the same with profile2, idem with 3 and
4. For more than 4 arguments ... modify the function profile yourself,
it is very easy (look the differences between profile and profile2.

If you want to profile two mutually recursive functions, you had better
to rename them :

let fkey = declare_profile "f"
let gkey = declare_profile "g"
let f' = .... f' ... g'
and g' = .... f' .... g'


let f = profile fkey f'
let g = profile gkey g'

Before the program quits, you should call "print_profile ()". It
produces a result of the following kind:

f 5.32 7.10
g 4.00 4.00
main 0.12 9.44
total -9.44 0.00

- The first column is the name of the function.

- The third column give the time (utime + stime) spend inside the function.

- The second column give the time spend inside the function minus the
time spend in other profiled functions called by it

The last line can be ignored (there is a bug if the down-right digit is non
zero)

*)

type profile_key = float ref * float ref

let actif = ref true

let tot_ptr = ref 0.0 and tot_ptr' = ref 0.0

let prof_table = ref ["total",(tot_ptr,tot_ptr')]

let stack = ref [tot_ptr']

let ajoute ((name,(ptr1,ptr1')) as e) l =
  try let (ptr,ptr') = List.assoc name l in
  ptr  := !ptr  +. !ptr1;
  ptr' := !ptr' +. !ptr1';
  l
  with Not_found -> e::l


let magic = 1248

let append_profile name =
  if List.length !prof_table <> 1 then begin
    let prof_table =
      if name = "" then !prof_table
      else
	let new_prof_table =
          try 
            let c = open_in name in
            if input_binary_int c <> magic 
            then Printf.printf "Bad already existing recording file\n";
            let old_prof_table = input_value c in
            close_in c;
            List.fold_right ajoute !prof_table old_prof_table
          with Sys_error _ ->
            (Printf.printf "Non existent or unaccessible recording file\n";
             !prof_table)
	in begin
          (try
             let c = open_out_gen 
                       [Open_creat;Open_wronly;Open_trunc;Open_binary] 0o644 name in
             output_binary_int c magic;
             output_value c new_prof_table;
             close_out c
           with Sys_error _ -> Printf.printf "Unable to create recording file");
            new_prof_table
	  end
    in
    print_newline ();
    Printf.printf "%-25s  %11s  %11s\n" 
      "Function name" "Proper time" "Total time";
    let l = Sort.list (fun (_,(_,p)) (_,(_,p')) -> !p > !p') prof_table in
    List.iter (fun (name,(ptr,ptr')) ->
		 Printf.printf "%-25s  %11.2f  %11.2f\n" name !ptr' !ptr) l;
    Gc.print_stat stdout
  end


let recording_file = ref ""
let set_recording s = recording_file := s

let print_profile () = append_profile !recording_file

let reset_profile () = 
  List.iter (fun (name,(p,p')) -> p:=0.0; p':=0.0) !prof_table

let init_profile () = 
  tot_ptr:= 0.0; tot_ptr':=0.0; prof_table :=["total",(tot_ptr,tot_ptr')] 

let declare_profile name = let ptr = ref 0.0 and ptr' = ref 0.0 in
prof_table := (name,(ptr,ptr'))::!prof_table;(ptr,ptr')

let profile (ptr,ptr') f =
  (fun x ->
     let (ut,st) = System.process_time () in
     stack := ptr'::!stack;
     try
       let r = f x in
       let (ut',st') = System.process_time () in
       let t = (ut' -. ut) +. (st' -. st) in
       (match !stack with
	    _::(ptr'::_ as s) -> stack:=s; ptr' := !ptr' -. t
	  | _ -> failwith "bug in profile");
       ptr := !ptr +. t;
       ptr' := !ptr' +. t;
       r
     with e ->
       let (ut',st') = System.process_time () in
       let t = (ut' -. ut) +. (st' -. st) in
       (match !stack with
	    _::(ptr'::_ as s) -> stack:=s; ptr' := !ptr' -. t
	  | _ -> failwith "bug in profile");
       ptr := !ptr +. t;
       ptr' := !ptr' +. t;
       raise e)



let profile2 (ptr,ptr') f =
  (fun x y ->
     let (ut,st) = System.process_time () in
     stack := ptr'::!stack;
     try
       let r = f x y in
       let (ut',st') = System.process_time () in
       let t = (ut' -. ut) +. (st' -. st) in
       (match !stack with
	    _::(ptr'::_ as s) -> stack:=s; ptr' := !ptr' -. t
	  | _ -> failwith "bug in profile");
       ptr := !ptr +. t;
       ptr' := !ptr' +. t;
       r
     with e ->
       let (ut',st') = System.process_time () in
       let t = (ut' -. ut) +. (st' -. st) in
       (match !stack with
	    _::(ptr'::_ as s) -> stack:=s; ptr' := !ptr' -. t
	  | _ -> failwith "bug in profile");
       ptr := !ptr +. t;
       ptr' := !ptr' +. t;
       raise e)



let profile3 (ptr,ptr') f =
  (fun x y z ->
     let (ut,st) = System.process_time () in
     stack := ptr'::!stack;
     try
       let r = f x y z in
       let (ut',st') = System.process_time () in
       let t = (ut' -. ut) +. (st' -. st) in
       (match !stack with
	    _::(ptr'::_ as s) -> stack:=s; ptr' := !ptr' -. t
	  | _ -> failwith "bug in profile");
       ptr := !ptr +. t;
       ptr' := !ptr' +. t;
       r
     with e ->
       let (ut',st') = System.process_time () in
       let t = (ut' -. ut) +. (st' -. st) in
       (match !stack with
	    _::(ptr'::_ as s) -> stack:=s; ptr' := !ptr' -. t
	  | _ -> failwith "bug in profile");
       ptr := !ptr +. t;
       ptr' := !ptr' +. t;
       raise e)



let profile4 (ptr,ptr') f =
  (fun x y z t ->
     let (ut,st) = System.process_time () in
     stack := ptr'::!stack;
     try
       let r = f x y z t in
       let (ut',st') = System.process_time () in
       let t = (ut' -. ut) +. (st' -. st) in
       (match !stack with
	    _::(ptr'::_ as s) -> stack:=s; ptr' := !ptr' -. t
	  | _ -> failwith "bug in profile");
       ptr := !ptr +. t;
       ptr' := !ptr' +. t;
       r
     with e ->
       let (ut',st') = System.process_time () in
       let t = (ut' -. ut) +. (st' -. st) in
       (match !stack with
	    _::(ptr'::_ as s) -> stack:=s; ptr' := !ptr' -. t
	  | _ -> failwith "bug in profile");
       ptr := !ptr +. t;
       ptr' := !ptr' +. t;
       raise e)



let profile5 (ptr,ptr') f =
  (fun x y z t u ->
     let (ut,st) = System.process_time () in
     stack := ptr'::!stack;
     try
       let r = f x y z t u in
       let (ut',st') = System.process_time () in
       let t = (ut' -. ut) +. (st' -. st) in
       (match !stack with
	    _::(ptr'::_ as s) -> stack:=s; ptr' := !ptr' -. t
	  | _ -> failwith "bug in profile");
       ptr := !ptr +. t;
       ptr' := !ptr' +. t;
       r
     with e ->
       let (ut',st') = System.process_time () in
       let t = (ut' -. ut) +. (st' -. st) in
       (match !stack with
	    _::(ptr'::_ as s) -> stack:=s; ptr' := !ptr' -. t
	  | _ -> failwith "bug in profile");
       ptr := !ptr +. t;
       ptr' := !ptr' +. t;
       raise e)



let profile6 (ptr,ptr') f =
  (fun x y z t u v ->
     let (ut,st) = System.process_time () in
     stack := ptr'::!stack;
     try
       let r = f x y z t u v in
       let (ut',st') = System.process_time () in
       let t = (ut' -. ut) +. (st' -. st) in
       (match !stack with
	    _::(ptr'::_ as s) -> stack:=s; ptr' := !ptr' -. t
	  | _ -> failwith "bug in profile");
       ptr := !ptr +. t;
       ptr' := !ptr' +. t;
       r
     with e ->
       let (ut',st') = System.process_time () in
       let t = (ut' -. ut) +. (st' -. st) in
       (match !stack with
	    _::(ptr'::_ as s) -> stack:=s; ptr' := !ptr' -. t
	  | _ -> failwith "bug in profile");
       ptr := !ptr +. t;
       ptr' := !ptr' +. t;
       raise e)



let profile7 (ptr,ptr') f =
  (fun x y z t u v w ->
     let (ut,st) = System.process_time () in
     stack := ptr'::!stack;
     try
       let r = f x y z t u v w in
       let (ut',st') = System.process_time () in
       let t = (ut' -. ut) +. (st' -. st) in
       (match !stack with
	    _::(ptr'::_ as s) -> stack:=s; ptr' := !ptr' -. t
	  | _ -> failwith "bug in profile");
       ptr := !ptr +. t;
       ptr' := !ptr' +. t;
       r
     with e ->
       let (ut',st') = System.process_time () in
       let t = (ut' -. ut) +. (st' -. st) in
       (match !stack with
	    _::(ptr'::_ as s) -> stack:=s; ptr' := !ptr' -. t
	  | _ -> failwith "bug in profile");
       ptr := !ptr +. t;
       ptr' := !ptr' +. t;
       raise e)