Add -doc-stats options to print document statistics.
[SXSI/xpathcomp.git] / src / main.ml
1 (******************************************************************************)
2 (*  SXSI : XPath evaluator                                                    *)
3 (*  Kim Nguyen (Kim.Nguyen@nicta.com.au)                                      *)
4 (*  Copyright NICTA 2008                                                      *)
5 (*  Distributed under the terms of the LGPL (see LICENCE)                     *)
6 (******************************************************************************)
7 INCLUDE "utils.ml"
8
9 open Ata
10
11 let () = init_timer();;
12
13
14 let default_gc = Gc.get()
15 let tuned_gc = { default_gc with
16   Gc.minor_heap_size = 32*1024*1024;
17   Gc.major_heap_increment = 8*1024*1024;
18   Gc.max_overhead = 1000000;
19   Gc.space_overhead = 100;
20 }
21
22 let mk_runtime run auto doc arg count print outfile =
23   fun () ->
24     if !Options.do_perf then start_perf ();
25     let r = time ~count:!Options.repeat ~msg:"Execution time" (run auto doc) arg in
26     if !Options.do_perf then stop_perf ();
27     Logger.print Format.err_formatter "Number of results: %i@\n" (count r);
28     match outfile with
29         None -> ()
30       | Some file ->
31         time ~count:1 ~msg:"Serialization time" (print file doc) r
32 ;;
33
34 let main v query_string output =
35   Tag.init (Tree.tag_operations v);
36   if !Options.docstats then Tree.stats v;
37   let query =
38     time ~msg:"Parsing query" XPath.parse query_string
39   in
40   if !Options.verbose then begin
41     Logger.print Format.err_formatter "Parsed query:@\n%a@\n"
42       XPath.Ast.print query;
43   end;
44   let auto, bu_info =
45     time ~msg:"Compiling query" Compile.compile query
46   in
47   if !Options.verbose then Ata.print Format.err_formatter auto;
48   Gc.full_major();
49   Gc.compact();
50   Gc.set (tuned_gc);
51   let runtime =
52     match !Options.bottom_up, bu_info with
53
54     | true, Some [ (query, pattern) ] ->
55       if !Options.count_only then
56         let module R = ResJIT.Count in
57         let module M = Runtime.Make(R) in
58         mk_runtime M.bottom_up_run auto v (query, pattern) R.NS.length R.NS.serialize None
59       else
60         let module R = ResJIT.Mat in
61         let module M = Runtime.Make(R) in
62         mk_runtime M.bottom_up_run auto v (query, pattern) R.NS.length R.NS.serialize !Options.output_file
63
64     | _ ->
65       (* run the query top_down *)
66
67       if !Options.bottom_up then
68         Logger.print Format.err_formatter "Cannot run the query in bottom-up mode, using top-down evaluator@\n@?";
69       if !Options.count_only then
70         let module R = ResJIT.Count in
71         let module M = Runtime.Make(R) in
72         if !Options.twopass then
73           mk_runtime M.twopass_top_down_run auto v Tree.root R.NS.length R.NS.serialize None
74         else
75           mk_runtime M.top_down_run auto v Tree.root R.NS.length R.NS.serialize None
76       else
77         let module R = ResJIT.Mat in
78         let module M = Runtime.Make(R) in
79         mk_runtime M.top_down_run auto v Tree.root R.NS.length R.NS.serialize !Options.output_file
80   in
81   runtime ()
82 ;;
83
84 let () = Options.parse_cmdline()
85 ;;
86 let _ =
87   try
88     Printexc.record_backtrace true;
89
90     let document =
91       if Filename.check_suffix !Options.input_file ".g.bin" ||
92         Filename.check_suffix !Options.input_file ".g"
93       then
94         let is_index = Filename.check_suffix !Options.input_file ".g.bin" in
95         let g =
96           if is_index then
97             time ~msg:"Loading grammar" (Grammar2.load) !Options.input_file
98           else
99             let g = time ~msg:"Parsing grammar file" Grammar2.parse !Options.input_file in
100             if !Options.save_file <> "" then
101               time ~msg:"Saving index" (Grammar2.save g) !Options.save_file;
102             g
103         in
104         begin
105           (* TODO Factorise with main *)
106           Tag.init (Grammar2.tag_operations g);
107           let query =
108             time ~msg:"Parsing query" XPath.parse !Options.query
109           in
110           if !Options.verbose then begin
111             Printf.eprintf "Parsed query:\n%!";
112             XPath.Ast.print Format.err_formatter query;
113             Format.fprintf Format.err_formatter "\n%!"
114           end;
115           let auto, bu_info =
116             time ~msg:"Compiling query" (Compile.compile) query
117           in
118           if !Options.verbose then Ata.print Format.err_formatter auto;
119           Gc.full_major();
120           Gc.compact();
121           Gc.set (tuned_gc);
122           let runtime =
123             if !Options.count_only then
124               let module R = ResJIT.Make(NodeSet.Partial(NodeSet.Count)) in
125               let module M = Runtime.Make(R) in
126             (* mk_runtime run auto doc arg count print outfile  *)
127               mk_runtime M.grammar_run auto (Obj.magic g) () R.NS.length (Obj.magic R.NS.serialize) None
128             else
129               let module R = ResJIT.Mat in
130               let module M = Runtime.Make(R) in
131               (* mk_runtime run auto doc arg count print outfile  *)
132               mk_runtime M.grammar_run auto (Obj.magic g) () R.NS.length (Obj.magic R.NS.serialize) None
133           in
134           runtime ();
135           exit 0
136         end
137       else if Filename.check_suffix !Options.input_file ".srx"
138       then
139         time
140           ~msg:"Loading file"
141           (Tree.load
142              ~sample:!Options.sample_factor
143              ~load_text:(not !Options.disable_text_collection))
144           !Options.input_file
145       else
146         let v =
147           time
148             ~msg:"Parsing document"
149             (Tree.parse_xml_uri)
150             !Options.input_file
151         in
152         let () =
153           if !Options.save_file <> ""
154           then
155             time
156               ~msg:"Writing file to disk"
157               (Tree.save v)
158               !Options.save_file;
159         in
160         v
161     in
162     main document !Options.query !Options.output_file;
163     if !Options.verbose then
164       Logger.print Format.err_formatter "Maximum resident set size: %s @\n" (read_procmem());
165     Gc.full_major();
166     Profile.summary Format.err_formatter
167   with
168   | Ulexer.Loc.Exc_located ((x,y),e) ->
169     Logger.print Format.err_formatter "character %i-%i %s@\n" x y (Printexc.to_string e);
170     exit 1
171
172   | e ->
173     Logger.print Format.err_formatter "BACKTRACE: %s@\n@?" (Printexc.get_backtrace());
174     Logger.print Format.err_formatter "FATAL ERROR: %s@\n@?" (Printexc.to_string e);
175     exit 2
176
177