1 (* Memory info command for virtual domains.
2 (C) Copyright 2008 Richard W.M. Jones, Red Hat Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 let (//) = Filename.concat
27 type pathname = string
31 kernel_version : string;
38 struct_total_size : int;
39 struct_fields : field list;
50 | FStructPointer of string
52 | FAnonListHeadPointer
53 | FListHeadPointer of (string * string) option
57 let string_of_info i =
58 sprintf "%s: %s (%d) %s" i.basename i.kernel_version i.kv_i i.arch
60 let rec string_of_structure s =
61 let fields = List.map string_of_field s.struct_fields in
62 let fields = String.concat "\n " fields in
63 sprintf "struct %s {\n %s\n}; /* total size = %d bytes */"
64 s.struct_name fields s.struct_total_size
66 and string_of_field f =
67 sprintf "%s %s; /* offset = %d, size = %d */"
68 (string_of_f_type f.field_type) f.field_name
69 f.field_offset f.field_size
71 and string_of_f_type = function
72 | FStructPointer struct_name -> sprintf "struct %s *" struct_name
73 | FVoidPointer -> "void *"
74 | FAnonListHeadPointer -> "struct list_head *"
75 | FListHeadPointer None ->
76 sprintf "struct /* self */ list_head *"
77 | FListHeadPointer (Some (struct_name, field_name)) ->
78 sprintf "struct /* to %s.%s */ list_head *" struct_name field_name
80 | FString width -> sprintf "char[%d]" width
82 (* Regular expressions. We really really should use ocaml-mikmatch ... *)
83 let re_oldformat = Pcre.regexp "^RPM: \\d+: \\(build \\d+\\) ([-\\w]+) ([\\w.]+) ([\\w.]+) \\(.*?\\) (\\w+)"
84 let re_keyvalue = Pcre.regexp "^(\\w+): (.*)"
86 let list_kernels path =
87 (* Get the *.info files from the kernels database. *)
88 let infos = Sys.readdir path in
89 let infos = Array.to_list infos in
90 let infos = List.filter (fun name -> String.ends_with name ".info") infos in
91 let infos = List.map ( (//) path) infos in
93 (* Parse in the *.info files. These have historically had a few different
94 * formats that we need to support.
96 let infos = List.mapi (
98 (* Get the basename (for getting the .data file later on). *)
99 let basename = Filename.chop_suffix filename ".info" in
101 let chan = open_in filename in
102 let line = input_line chan in
104 (* Kernel version string. *)
106 if Pcre.pmatch ~rex:re_oldformat line then (
107 (* If the file starts with "RPM: \d+: ..." then it's the
108 * original Fedora format. Everything in one line.
110 let subs = Pcre.exec ~rex:re_oldformat line in
111 (* let name = Pcre.get_substring subs 1 in *)
112 let version = Pcre.get_substring subs 2 in
113 let release = Pcre.get_substring subs 3 in
114 let arch = Pcre.get_substring subs 4 in
116 (* XXX Map name -> PAE, hugemem etc. *)
117 (* name, *) sprintf "%s-%s.%s" version release arch, arch
119 (* New-style "key: value" entries, up to end of file or the first
122 let (*name,*) version, release, arch =
123 (*ref "",*) ref "", ref "", ref "" in
126 let subs = Pcre.exec ~rex:re_keyvalue line in
127 let key = Pcre.get_substring subs 1 in
128 let value = Pcre.get_substring subs 2 in
129 (*if key = "Name" then name := value
130 else*) if key = "Version" then version := value
131 else if key = "Release" then release := value
132 else if key = "Architecture" then arch := value;
133 let line = input_line chan in
136 Not_found | End_of_file ->
140 let (*name,*) version, release, arch =
141 (*!name,*) !version, !release, !arch in
142 if (*name = "" ||*) version = "" || release = "" || arch = "" then
143 failwith (sprintf "%s: missing Name, Version, Release or Architecture key" filename);
144 (* XXX Map name -> PAE, hugemem etc. *)
145 (* name, *) sprintf "%s-%s.%s" version release arch, arch
148 (*printf "%s -> %s %s\n%!" basename version arch;*)
151 basename = basename; arch = arch;
152 kernel_version = version }
156 (* XXX This would be better as a proper lex/yacc parser.
157 * XXX Even better would be to have a proper interface to libdwarves.
159 let re_offsetsize = Pcre.regexp "/\\*\\s+(\\d+)\\s+(\\d+)\\s+\\*/"
160 let re_intfield = Pcre.regexp "(?:int|char)\\s+(\\w+);"
161 let re_ptrfield = Pcre.regexp "struct\\s+(\\w+)\\s*\\*\\s*(\\w+);"
162 let re_voidptrfield = Pcre.regexp "void\\s*\\*\\s*(\\w+);"
163 let re_strfield = Pcre.regexp "char\\s+(\\w+)\\[(\\d+)\\];"
164 let re_structopener = Pcre.regexp "(struct|union)\\s+.*{$"
165 let re_structcloser = Pcre.regexp "}\\s*(\\w+)?(\\[\\d+\\])?;"
167 let load_structures { basename = basename } struct_names =
168 (* For quick access to the opener strings, build a hash. *)
169 let openers = Hashtbl.create 13 in
172 let opener = sprintf "struct %s {" struct_name in
174 Hashtbl.add openers opener (closer, struct_name)
177 (* Now read the data file and parse out the structures of interest. *)
178 let file_exists name =
179 try Unix.access name [Unix.F_OK]; true
180 with Unix.Unix_error _ -> false
182 let close_process_in cmd chan =
183 match Unix.close_process_in chan with
184 | Unix.WEXITED 0 -> ()
186 eprintf "%s: command exited with code %d\n" cmd i; exit i
187 | Unix.WSIGNALED i ->
188 eprintf "%s: command exited with signal %d\n" cmd i; exit 1
190 eprintf "%s: command stopped by signal %d\n" cmd i; exit 1
193 (* Open the data file, uncompressing it on the fly if necessary. *)
195 if file_exists (basename ^ ".data") then
196 open_in (basename ^ ".data"), close_in
197 else if file_exists (basename ^ ".data.gz") then (
199 sprintf "gzip -cd %s" (Filename.quote (basename ^ ".data.gz")) in
200 Unix.open_process_in cmd, close_process_in cmd
202 else if file_exists (basename ^ ".data.bz2") then (
204 sprintf "bzip2 -cd %s" (Filename.quote (basename ^ ".data.bz2")) in
205 Unix.open_process_in cmd, close_process_in cmd
207 failwith (sprintf "%s: cannot find corresponding data file" basename) in
209 (* Read the data file in, looking for structures of interest to us. *)
210 let bodies = Hashtbl.create 13 in
212 let line = input_line chan in
214 (* If the line is an opener for one of the structures we
215 * are looking for, then for now just save all the text until
216 * we get to the closer line.
219 let closer, struct_name = Hashtbl.find openers line in
220 let rec loop2 lines =
221 let line = input_line chan in
222 let lines = line :: lines in
223 if String.starts_with line closer then List.rev lines
230 failwith (sprintf "%s: %s: %S not matched by closing %S" basename struct_name line closer) in
232 Hashtbl.replace bodies struct_name body
233 with Not_found -> ());
237 (try loop () with End_of_file -> ());
241 (* Now parse each structure body. *)
243 (* 'basename' is the source file, and second parameter ('body') is
244 * the list of text lines which covers this structure (minus the
245 * opener line). Result is the list of parsed fields from this
248 let rec parse basename = function
250 | [_] -> [] (* Just the closer line, finished. *)
251 | line :: lines when Pcre.pmatch ~rex:re_structopener line ->
252 (* Recursively parse a sub-structure. First search for the
253 * corresponding closer line.
255 let rec loop depth acc = function
257 eprintf "%s: %S has no matching close structure line\n%!"
260 | line :: lines when Pcre.pmatch ~rex:re_structopener line ->
261 loop (depth+1) (line :: acc) lines
263 when depth = 0 && Pcre.pmatch ~rex:re_structcloser line ->
266 when depth > 0 && Pcre.pmatch ~rex:re_structcloser line ->
267 loop (depth-1) (line :: acc) lines
268 | line :: lines -> loop depth (line :: acc) lines
270 let nested_body, rest = loop 0 [] lines in
272 (* Then parse the sub-structure. *)
273 let struct_name, nested_body =
274 match nested_body with
277 let subs = Pcre.exec ~rex:re_structcloser closer in
279 try Some (Pcre.get_substring subs 1) with Not_found -> None in
280 struct_name, List.rev nested_body in
281 let nested_fields = parse basename nested_body in
283 (* Prefix the sub-fields with the name of the structure. *)
285 match struct_name with
286 | None -> nested_fields
289 fun ({ field_name = name } as field) ->
290 let name = prefix ^ "'" ^ name in
291 { field with field_name = name }
294 (* Parse the rest. *)
295 nested_fields @ parse basename rest
297 | line :: lines when Pcre.pmatch ~rex:re_intfield line ->
299 let subs = Pcre.exec ~rex:re_intfield line in
300 let name = Pcre.get_substring subs 1 in
302 let subs = Pcre.exec ~rex:re_offsetsize line in
303 let offset = int_of_string (Pcre.get_substring subs 1) in
304 let size = int_of_string (Pcre.get_substring subs 2) in
306 { field_name = name; field_type = FInteger;
307 field_offset = offset; field_size = size } in
308 field :: parse basename lines
310 Not_found -> parse basename lines
313 | line :: lines when Pcre.pmatch ~rex:re_ptrfield line ->
314 (* A pointer-to-struct field. *)
315 let subs = Pcre.exec ~rex:re_ptrfield line in
316 let struct_name = Pcre.get_substring subs 1 in
317 let name = Pcre.get_substring subs 2 in
319 let subs = Pcre.exec ~rex:re_offsetsize line in
320 let offset = int_of_string (Pcre.get_substring subs 1) in
321 let size = int_of_string (Pcre.get_substring subs 2) in
323 if struct_name <> "list_head" then
324 FStructPointer struct_name
326 FAnonListHeadPointer in
328 { field_name = name; field_type = field_type;
329 field_offset = offset; field_size = size } in
330 field :: parse basename lines
332 Not_found -> parse basename lines
335 | line :: lines when Pcre.pmatch ~rex:re_voidptrfield line ->
337 let subs = Pcre.exec ~rex:re_voidptrfield line in
338 let name = Pcre.get_substring subs 1 in
340 let subs = Pcre.exec ~rex:re_offsetsize line in
341 let offset = int_of_string (Pcre.get_substring subs 1) in
342 let size = int_of_string (Pcre.get_substring subs 2) in
344 { field_name = name; field_type = FVoidPointer;
345 field_offset = offset; field_size = size } in
346 field :: parse basename lines
348 Not_found -> parse basename lines
351 | line :: lines when Pcre.pmatch ~rex:re_strfield line ->
352 (* A string (char array) field. *)
353 let subs = Pcre.exec ~rex:re_strfield line in
354 let name = Pcre.get_substring subs 1 in
355 let width = int_of_string (Pcre.get_substring subs 2) in
357 let subs = Pcre.exec ~rex:re_offsetsize line in
358 let offset = int_of_string (Pcre.get_substring subs 1) in
359 let size = int_of_string (Pcre.get_substring subs 2) in
361 { field_name = name; field_type = FString width;
362 field_offset = offset; field_size = size } in
363 field :: parse basename lines
365 Not_found -> parse basename lines
369 (* Just ignore any other field we can't parse. *)
373 let structures = List.filter_map (
376 try Some (Hashtbl.find bodies struct_name)
377 with Not_found -> None in
381 let body = List.tl body in (* Don't care about opener line. *)
382 let fields = parse basename body in
384 (* Compute total size of the structure. *)
386 let fields = List.map (
387 fun { field_offset = offset;
388 field_size = size } -> offset + size
390 List.fold_left max 0 fields in
392 (* Sort the structure fields by field offset. They are
393 * probably already in this order, but just make sure.
395 let cmp { field_offset = o1 } { field_offset = o2 } = compare o1 o2 in
396 let fields = List.sort ~cmp fields in
400 { struct_name = struct_name;
401 struct_fields = fields;
402 struct_total_size = total_size }
408 (* XXX This loop is O(n^3), luckily n is small! *)
409 let transpose good_struct_names kernels =
414 fun (info, structures) ->
416 let s = List.assoc struct_name structures in
422 (* Sort the kernels, which makes the generated output more stable
423 * and makes patches more useful.
425 let kernels = List.sort kernels in
430 let get_fields structures =
431 (* Use a hash table to accumulate the fields as we find them.
432 * The key is the field name. The value is the field type and the
433 * kernel version where first seen (for error reporting). If
434 * we meet the field again, we check its type hasn't changed.
435 * Finally, we can use the hash to pull out all field names and
438 let h = Hashtbl.create 13 in
440 (* A hash to check for fields which aren't always available by
441 * counting the number of times we see each field.
444 let h = Hashtbl.create 13 in
445 let count field_name =
447 try Hashtbl.find h field_name
448 with Not_found -> let r = ref 0 in Hashtbl.add h field_name r; r in
451 let get field_name = try !(Hashtbl.find h field_name) with Not_found -> 0 in
456 fun ({kernel_version = version},
457 {struct_name = struct_name; struct_fields = fields}) ->
459 fun {field_name = name; field_type = typ} ->
462 let (field_type, version_first_seen) = Hashtbl.find h name in
463 if typ <> field_type then (
464 eprintf "Error: %s.%s: field changed type between kernel versions.\n"
466 eprintf "In version %s it had type %s.\n"
467 version_first_seen (string_of_f_type field_type);
468 eprintf "In version %s it had type %s.\n"
469 version (string_of_f_type typ);
470 eprintf "The code cannot handle fields which change type like this.\n";
471 eprintf "See extract/codegen/pahole_parser.mli for more details.\n";
475 Hashtbl.add h name (typ, version)
479 let nr_kernels = List.length structures in
483 fun name (typ, _) fields ->
484 let always_available = get name = nr_kernels in
485 (name, (typ, always_available)) :: fields