Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fusedump: read packets lazily from the stream #83

Open
wants to merge 1 commit into
base: 0.4.0-gamma
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/fusedump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,9 @@ let parse_packet host query_table fd =
in
Some (now, packet)

let parse_session filename =
let packet_stream_of_fd fd =
let header_len = 4 + 1 + 1 + 2 in
let bheader = Bytes.create header_len in
with_open_fd filename @@ fun fd ->
let header_read = Unix.read fd bheader 0 header_len in
let header = Bytes.to_string bheader in
(if header_read <> header_len then failwith "couldn't read header");
Expand All @@ -211,11 +210,15 @@ let parse_session filename =
| c -> failf "unknown host type '%c'" c
in
let query_table = Hashtbl.create 128 in
let rec read_next session = match parse_packet host query_table fd with
| None -> List.rev session
| Some packet -> read_next (packet::session)
let rec read_next () = match parse_packet host query_table fd with
| None -> `Eof
| Some packet -> `Next(packet, read_next)
in
read_next []
read_next ()

let rec iter f = function
| `Eof -> ()
| `Next(packet, rest) -> f packet; iter f (rest ())

let pretty_print time (t, p) =
Printf.printf "%s%s\n%!"
Expand All @@ -228,8 +231,9 @@ let pretty_print time (t, p) =

let show time session_file =
try
let session = parse_session session_file in
List.iter (pretty_print time) session;
with_open_fd session_file @@ fun fd ->
let session = packet_stream_of_fd fd in
iter (pretty_print time) session;
`Ok ()
with exn ->
`Error (false,
Expand Down