Coccinelle release-1.0.0-rc11
[bpt/coccinelle.git] / tools / dir_stats.ml
CommitLineData
f537ebc4 1(*
17ba0788
C
2 * Copyright 2012, INRIA
3 * Julia Lawall, Gilles Muller
4 * Copyright 2010-2011, INRIA, University of Copenhagen
f537ebc4
C
5 * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
6 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
7 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
8 * This file is part of Coccinelle.
9 *
10 * Coccinelle is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, according to version 2 of the License.
13 *
14 * Coccinelle is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * The authors reserve the right to distribute this or future versions of
23 * Coccinelle under other licenses.
24 *)
25
26
34e49164
C
27(* for each marked thing, how often does it occur and in what files and
28directories *)
29
30let collect i =
31 let info = ref [] in
32 let rec loop _ =
33 let l = input_line i in
34 (if String.length l > 2 && String.get l 0 = '+'
35 then info := (String.sub l 1 (String.length l - 1))::!info);
36 loop() in
37 try loop()
38 with End_of_file -> List.rev !info
39
40let split l =
41 let rec loop acc = function
42 [] -> acc
43 | x::xs ->
44 if String.get x 0 = '+' (* the start of a new file *)
45 then
46 (match Str.split (Str.regexp " ") x with
47 _::x::_ -> loop ((x,[])::acc) xs
48 | _ -> failwith ("no file: "^x))
49 else
50 let acc =
51 match acc with
52 (file,instances)::rest -> (file,x::instances)::rest
53 | _ -> failwith "not possible" in
54 loop acc xs in
55 let res = List.rev (loop [] l) in
56 List.map (function (x,l) -> (x,List.rev l)) res
57
58let detect_alloc_free str l =
59 let try_add a f l =
60 let (same,diff) = List.partition (function (a1,f1) -> a = a1) l in
61 match same with
62 [(a1,f1)] -> if List.mem f f1 then l else (a1,f::f1) :: diff
63 | _ -> (a,[f])::l in
64 let rec loop acc = function
65 [] -> acc
66 | x::xs ->
67 match Str.split (Str.regexp (str^"\", ")) x with
68 _::matches ->
69 let acc =
70 List.fold_left
71 (function acc ->
72 function rest ->
73 (match Str.split (Str.regexp "[, )]+") rest with
74 alloc::free::_ -> try_add alloc free acc
75 | _ -> acc))
76 acc matches in
77 loop acc xs
78 | _ -> loop acc xs in
79 List.sort compare
80 (List.map (function (a,f) -> (a,List.sort compare f)) (loop [] l))
81
82let rec iterate str = function
83 [] -> []
84 | (x,l)::xs ->
85 List.fold_left
86 (function rest ->
87 function info ->
88 let (same,diff) =
89 List.partition (function (x1,l1) -> l1 = info) rest in
90 match same with
91 [(files,info)] -> (x::files,info)::diff
92 | _ -> ([x],info)::diff)
93 (iterate str xs) (detect_alloc_free str l)
94
95(* ------------------------------------------------------------------------ *)
96
97let get_dir d = Filename.dirname d
98
99let get_subsystem d =
100 let pieces = Str.split (Str.regexp "/") d in
101 let front = List.hd(List.tl pieces) in
102 match front with
103 "arch" | "drivers" -> front ^ "/" ^ (List.hd(List.tl(List.tl pieces)))
104 | _ -> front
105
106let rec remdup = function
107 [] -> []
108 | x::xs -> if List.mem x xs then remdup xs else x :: remdup xs
109
110let inc tbl key =
111 let cell =
112 (try let cell = Hashtbl.find tbl key in cell
113 with Not_found -> let c = ref 0 in Hashtbl.add tbl key c; c) in
114 cell := !cell + 1
115
116let files_per_protocol = Hashtbl.create(10)
117let dirs_per_protocol = Hashtbl.create(10)
118let subsystems_per_protocol = Hashtbl.create(10)
119let protocols_per_subsystem = Hashtbl.create(10)
120
121let collect_counts l =
122 List.iter
123 (function (files,(a,fs)) ->
124 let how_many_files = List.length files in
125 let how_many_dirs = remdup (List.map get_dir files) in
126 let how_many_subsystems = remdup (List.map get_subsystem files) in
127 let ct =
128 if how_many_files < 10
129 then how_many_files
130 else ((how_many_files / 10) * 10) in
131 inc files_per_protocol ct;
132 inc dirs_per_protocol (List.length how_many_dirs);
133 inc subsystems_per_protocol (List.length how_many_subsystems);
134 List.iter (inc protocols_per_subsystem) how_many_subsystems)
135 l
136
137let print_hashtable f tbl =
138 let l =
139 Hashtbl.fold
140 (function key -> function vl -> function rest ->
141 (key,!vl) :: rest)
142 tbl [] in
143 let l = List.sort compare l in
144 List.iter
145 (function (key,vl) ->
146 Printf.printf " "; f key; Printf.printf ": %d\n" vl)
147 l
148
149let print_range_int_hashtable range =
150 print_hashtable
151 (function x ->
152 if x < range
153 then Printf.printf "%d" x
154 else Printf.printf "%d-%d" x (x + range - 1))
155let print_int_hashtable =
156 print_hashtable (function x -> Printf.printf "%d" x)
157let print_string_hashtable =
158 print_hashtable (function x -> Printf.printf "%s" x)
159
160let histify _ =
161 Printf.printf "files per protocol:\n";
162 print_range_int_hashtable 10 files_per_protocol;
163 Printf.printf "dirs per protocol:\n";
164 print_int_hashtable dirs_per_protocol;
165 Printf.printf "subsystems per protocol:\n";
166 print_int_hashtable subsystems_per_protocol;
167 Printf.printf "protocols per subsystem:\n";
168 print_string_hashtable protocols_per_subsystem
169
170(* ------------------------------------------------------------------------ *)
171
172let dir = ref "p2"
173let file = ref ""
174let str = ref "detected allocator"
175
176let options = []
177let usage = ""
178
179let _ =
180 Arg.parse (Arg.align options) (fun x -> file := x) usage;
181 let i = open_in !file in
182 let l = collect i in
183 close_in i;
184 let l = split l in
185 let l = iterate !str l in
186 collect_counts l;
187 histify()
188
189