Debian Codegraph @

dev/bench/profparser.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Coq Proof Assistant / The Coq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 let die fmt = Printf.kfprintf (fun _ -> exit 1) stderr (fmt^^"\n%!")
12
13 module YB = Yojson.Basic
14 module YBU = YB.Util
15
16 let assoc a b : YB.t = CList.assoc_f String.equal a b
17
18 (* Profile files can be large, we want to parse 1 record at a time and
19 only keep the info we're interested in (ie the "command" events).
20
21 We use undocumented Yojson.Basic.read_comma to detect the end.
22 *)
23
24 let rec find_cmds acc (lstate,lex as ch) =
25 let v = YB.from_lexbuf lstate ~stream:true lex in
26 let fname = Option.get lstate.Yojson.fname in
27 let lnum = lstate.Yojson.lnum in
28 let is_last = try YB.read_comma lstate lex; false with Yojson.Json_error _ -> true in
29 let acc = match v with
30 | `Assoc l -> begin match assoc "name" l with
31 | `String "command" -> (lnum,l) :: acc
32 | _ -> acc
33 end
34 | _ -> die "File %S line %d: unrecognised value" fname lnum
35 in
36 if is_last then acc else find_cmds acc ch
37
38 type 'ch channel = {
39 open_in : string -> 'ch;
40 close_in : 'ch -> unit;
41 really_input : 'ch -> Bytes.t -> int -> int -> unit;
42 input : 'ch -> Bytes.t -> int -> int -> int;
43 }
44
45 let file_channel = {
46 open_in = open_in;
47 close_in = close_in;
48 really_input = really_input;
49 input = input;
50 }
51
52 let gzip_channel = {
53 open_in = Gzip.open_in;
54 close_in = Gzip.close_in;
55 really_input = Gzip.really_input;
56 input = Gzip.input;
57 }
58
59 type any_channel = AnyChannel : 'ch channel -> any_channel
60
61 let channel_for fname =
62 if CString.is_suffix ".json" fname then AnyChannel file_channel
63 else AnyChannel gzip_channel
64
65 let input_exactly ch_fns ch expected =
66 let buf = Bytes.create (String.length expected) in
67 ch_fns.really_input ch buf 0 (String.length expected);
68 assert (Bytes.to_string buf = expected)
69
70 let read_file fname =
71 let AnyChannel ch_fns = channel_for fname in
72 let ch = ch_fns.open_in fname in
73 try
74 (* ignore initial line *)
75 let () = input_exactly ch_fns ch {|{ "traceEvents": [|} in
76 let lex = Lexing.from_function ~with_positions:false (fun buf n -> ch_fns.input ch buf 0 n) in
77 let lstate = Yojson.init_lexer ~fname ~lnum:2 () in
78 let cmds = find_cmds [] (lstate,lex) in
79 ch_fns.close_in ch;
80 cmds
81 with e -> ch_fns.close_in ch; raise e
82
83 open BenchUtil
84
85 let force_string lnum = function
86 | `String s -> s
87 | _ -> die "line %d: malformed value (expected string)" lnum
88
89 let get_ts (lnum, l) = match assoc "ts" l with
90 | `Int ts -> ts
91 | _ -> die "line %d: malformed ts" lnum
92
93 let get_src_info (lnum, l) = match assoc "args" l with
94 | `Assoc l ->
95 let hdr = force_string lnum (assoc "cmd" l) in
96 let line = match assoc "line" l with
97 | `Int l -> l
98 | `String l -> int_of_string l
99 | _ -> die "line %d: malformed line number" lnum
100 in
101 hdr, line
102 | _ -> die "line %d: malformed args" lnum
103
104 let hdr_regex = Str.regexp {|^Chars \([0-9]+\) - \([0-9]+\) |}
105
106 let get_src_chars ~lnum hdr =
107 if not (Str.string_match hdr_regex hdr 0) then die "line %d: malformed command header" lnum
108 else
109 { start_char = int_of_string @@ Str.matched_group 1 hdr;
110 stop_char = int_of_string @@ Str.matched_group 2 hdr; }
111
112 let mk_memory (lnum, l) =
113 let args = assoc "args" l in
114 try Some {
115 major_words = YBU.(to_string @@ member "major_words" args) ;
116 minor_words = YBU.(to_string @@ member "minor_words" args);
117 major_collect = YBU.(to_int @@ member "major_collect" args);
118 minor_collect = YBU.(to_int @@ member "minor_collect" args);
119 }
120 with YBU.Type_error (msg,_) -> die "line %d: %s" lnum msg
121
122 let mk_time start stop =
123 let time = stop - start in
124 (* time unit is microsecond *)
125 let timeq = Q.(div (of_int time) (of_int 1000000)) in
126 let timef = (float_of_int time) /. 1e6 in
127 let str =
128 (* 3 significant digits *)
129 if timef > 100. then Printf.sprintf "%.0f" timef
130 else if timef > 10. then Printf.sprintf "%.1f" timef
131 else if timef > 1. then Printf.sprintf "%.2f" timef
132 else if timef > 0.1 then Printf.sprintf "%.3f" timef
133 else if timef > 0.01 then Printf.sprintf "%.4f" timef
134 else if timef > 0.001 then Printf.sprintf "%.5f" timef
135 else Printf.sprintf "%.6f" timef
136 in
137 { str;
138 q = timeq; }
139
140 let get_instr (lnum, l) =
141 let args = assoc "args" l in
142 YBU.(to_int @@ member "instr" args)
143
144 let rec process_cmds acc = function
145 | [] -> acc
146 | end_event :: start_event :: rest ->
147 let hdr, line = get_src_info start_event in
148 let start_ts = get_ts start_event in
149 let end_ts = get_ts end_event in
150 let src_chars = get_src_chars ~lnum:(fst start_event) hdr in
151 let time = mk_time start_ts end_ts in
152 let memory = mk_memory end_event in
153 let instructions = Some (get_instr end_event) in
154 process_cmds ((src_chars, { time; memory; instructions; }) :: acc) rest
155 | [_] -> die "ill parenthesized events"
156
157 let parse ~file =
158 let cmds = read_file file in
159 let cmds = process_cmds [] cmds in
160 cmds