Skip to content
This repository
tag: v1157
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 450 lines (404 sloc) 14.557 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)

(* depends *)
module String = BaseString
module Marshal = BaseMarshal

let error fmt = OManager.error ("OpaTracker."^^fmt^^"\n")

type index = int
type info = string
type passname = string
type printer_id = string
type tracker_id = string
type filename = string
type 'a printer = Format.formatter -> 'a -> unit
type 'a outputer = Pervasives.out_channel -> 'a -> unit
type tag = int
type t =
    {
      index : index ;
      info : info ;
      mutable tags : (tag, Obj.t) Hashtbl.t option (* majority of case: None, better than empty hashtbl *)
    }
type iter_tracker = {
  track : 'tracked. (filename -> 'tracked printer -> 'tracked -> unit) ;
}
type 'env tracker = iter_tracker -> 'env -> unit

let info t = t.info
let next =
  let i = ref (-1) in
  (fun info -> incr(i); { index = !i ; info = info ; tags = None})

let next_tag =
  let i = ref (-1) in
  (fun () -> incr(i); !i)

let embed () =
  let tag = next_tag () in
  let set t alpha =
    let obj = Obj.repr alpha in
    match t.tags with
    | None ->
        let ht = Hashtbl.create 5 in
        Hashtbl.add ht tag obj;
        t.tags <- Some ht
    | Some ht -> Hashtbl.replace ht tag obj (* GC *)
  and get t =
    match t.tags with
    | None -> None
    | Some ht -> (
        try Some (Obj.obj (Hashtbl.find ht tag))
        with Not_found -> None
      )
  in set, get

let started = ref false
let directory_name = ref "_tracks"
let directory = ref (!directory_name)

let handle_open_out ?(append=false) file =
  try
    if append
    then
      Pervasives.open_out_gen
        [Open_wronly; Open_creat; Open_append; Open_text]
        0o666
        file
    else
      Pervasives.open_out file
  with
  | Sys_error s ->
      error "Output.start: cannot open_out %s : %s" file s

let handle_open_in file =
  try open_in file
  with
  | Sys_error s ->
      error "Input.start: cann open_int %s: %s" file s

let handle_close_out oc =
  try close_out oc
  with
  | Sys_error s ->
      error "Output.start: cannot close_out in %s : %s" !directory s

let handle_close_in ic =
  try close_in ic
  with
  | Sys_error s ->
      error "Input.start: cannot close_int in %s : %s" !directory s

let handle_mkdir path =
  if not (File.check_create_path path) then
    error "Output.start: cannot create directory %s" path

(* Conventions over name of files and directories. *)
(* <!> Keep it synchronized with 'opatrack.sh' and documentation *)
let name_passes_list = "passes.list"
let name_printers_list = "printers.list"
let name_trackers_list = "trackers.list"
let name_printers_dir = "printers"
let name_trackers_dir = "trackers"
let name_internal_dir = "internal"
let name_files_dir = "files"
let name_check_dir = "check"
let name_time_dir = "time"
let name_all_time = "all.time"
let marshal_filename = "marshal"

let passes_list = ref stdout
let printers_list = ref stdout
let trackers_list = ref stdout

(* caching everything for a image of the system file *)
let passes = ( ( Hashtbl.create 10 ) : (passname, unit) Hashtbl.t )
(* association between passes and printers *)
let pass_printer = ( ( ListHashtbl.create 10 ) : (passname, printer_id) ListHashtbl.t )
let printer_pass = ( ( ListHashtbl.create 10 ) : (printer_id, passname) ListHashtbl.t )
(* association between passes and trackers *)
let pass_tracker = ( ( ListHashtbl.create 10 ) : (passname, tracker_id) ListHashtbl.t )
let tracker_pass = ( ( ListHashtbl.create 10 ) : (tracker_id, passname) ListHashtbl.t )
(* association between tracker_id and filenames *)
let tracker_oc = ( (Hashtbl.create 10 ) : (tracker_id, out_channel) Hashtbl.t )
let tracker_filename = ( ( ListHashtbl.create 10 ) : (tracker_id, filename) ListHashtbl.t )

(* names generation *)
let build_passname_filename pass = Printf.sprintf "pass_%s" pass
let build_passtime_filename pass = Printf.sprintf "pass_%s.time" pass
let build_printer_id_filename printer_id = printer_id
let build_tracker_id_filename tracker_id = tracker_id
let build_internal_filename file index = Printf.sprintf "%s.%d" file index
let build_check_filename file index = Printf.sprintf "%s.%d" file index
let build_tracker_id_list tracker_id = Printf.sprintf "%s.list" (build_tracker_id_filename tracker_id)

(* for the `tracker directive *)
let build_track_name t = Printf.sprintf "index.%d" t.index

(*
Clear hashtbl for restart
*)
let clear_tables () =
  Hashtbl.clear passes ;
  ListHashtbl.clear pass_printer ;
  ListHashtbl.clear printer_pass ;
  ListHashtbl.clear pass_tracker ;
  ListHashtbl.clear tracker_pass ;
  Hashtbl.clear tracker_oc ;
  ListHashtbl.clear tracker_filename ;
  ()

(* keep list files coherent online *)
let output_pass passname =
  if not ( Hashtbl.mem passes passname )
  then (
    Printf.fprintf !passes_list "%s\n%!" (build_passname_filename passname);
    Hashtbl.add passes passname ()
  )

let output_printer passname printer_id =
  output_pass passname;
  if not ( ListHashtbl.mem printer_pass printer_id )
  then (
    Printf.fprintf !printers_list "%s\n%!" (build_printer_id_filename printer_id);
    ListHashtbl.add printer_pass printer_id passname;
    ListHashtbl.add pass_printer passname printer_id
  )

let output_tracker passname tracker_id =
  output_pass passname;
  if not ( Hashtbl.mem tracker_oc tracker_id )
  then (
    let path = Filename.concat !directory name_trackers_dir in
    let file = Filename.concat path (build_tracker_id_list tracker_id) in
    let oc = handle_open_out file in
    Hashtbl.add tracker_oc tracker_id oc
  );
  if not ( ListHashtbl.mem tracker_pass tracker_id )
  then (
    Printf.fprintf !trackers_list "%s\n%!" (build_tracker_id_filename tracker_id);
    ListHashtbl.add tracker_pass tracker_id passname;
    ListHashtbl.add pass_tracker passname tracker_id;
  )

let output_tracked tracker_id filename =
  let oc =
    try
      Hashtbl.find tracker_oc tracker_id
    with
    | Not_found -> error "Output.output_tracked: no channel available for tracker: %s" tracker_id
  in
  if not ( ListHashtbl.mem_cp tracker_filename (tracker_id, filename) )
  then (
    Printf.fprintf oc "%s\n%!" filename;
    ListHashtbl.add tracker_filename tracker_id filename
  )

let set_directory dirname =
  if !started then error "Output.set_directory: already started"
  else directory_name := dirname

let get_directory () = !directory

let finalize () =
  if not !started then () else (
    handle_close_out !passes_list;
    handle_close_out !printers_list;
    handle_close_out !trackers_list;
    Hashtbl.iter (fun _ oc -> handle_close_out oc) tracker_oc
  )

let all_time = ref 0.0

let start () =
  let new_directory =
    let directory mode =
      match mode with
      | `prelude -> Filename.concat !directory_name "prelude"
      | `init -> Filename.concat !directory_name "init"
      | `linking -> !directory_name
      | `compilation ->
          let compilation_directory = Option.default "" (ObjectFiles.get_compilation_directory ()) in
          Filename.concat compilation_directory !directory_name
    in
    let mode = ObjectFiles.compilation_mode () in
    if ObjectFiles.Arg.is_separated () then directory mode
    else if mode = `prelude then directory `linking else directory mode
  in
  let new_directory =
    if Filename.is_relative new_directory then
      Filename.concat (Sys.getcwd ()) new_directory
    else new_directory
  in
  let same_directory = new_directory = !directory in
  directory := new_directory ;
  if !started && same_directory then () else (
    if !started then (
      clear_tables () ;
      finalize () ;
    ) ;
    all_time := 0.0 ;
    handle_mkdir !directory ;
    let handle_open_out file = handle_open_out (Filename.concat !directory file) in
    started := true ;
    passes_list := handle_open_out name_passes_list ;
    printers_list := handle_open_out name_printers_list ;
    trackers_list := handle_open_out name_trackers_list ;
    let directory_trackers = Filename.concat !directory name_trackers_dir in
    handle_mkdir directory_trackers;
    let directory_time = Filename.concat !directory name_time_dir in
    handle_mkdir directory_time
  )

(*
+ should not be used, because printer_id and tracker_id should rather be abstract
types, with a constructor storing id in a table for PassHandler.Arg to be smart.
+ is used for filename generation of trackers
*)
let digest s = String.sub (Digest.to_hex (Digest.string s)) 0 8
let protect_filename s =
  if s <> "" && Base.String.is_word s then s else digest s

let print ~passname ~printer_id printer env =
  start () ;
  output_printer passname printer_id;
  let oc =
    let pass = build_passname_filename passname in
    let path = Filename.concat !directory pass in
    let path = Filename.concat path name_printers_dir in
    handle_mkdir path ;
    let file = Filename.concat path (build_printer_id_filename printer_id) in
    handle_open_out file
  in
  let fmt = Format.formatter_of_out_channel oc in
  printer fmt env ;
  Format.pp_print_flush fmt () ;
  handle_close_out oc

let track ~passname ~tracker_id tracker env =
  start () ;
  output_tracker passname tracker_id;
  let pass = build_passname_filename passname in
  let path = Filename.concat !directory pass in
  let path = Filename.concat path name_trackers_dir in
  let path = Filename.concat path (build_tracker_id_filename tracker_id) in
  handle_mkdir path ;
  let track filename printer tracked =
    let filename = protect_filename filename in
    (* add the filename to the list of tracked for this tracker_id *)
    output_tracked tracker_id filename;
    let file = Filename.concat path filename in
    let oc = handle_open_out file in
    let fmt = Format.formatter_of_out_channel oc in
    printer fmt tracked ;
    Format.pp_print_flush fmt () ;
    handle_close_out oc
  in
  tracker { track = track } env

let time ~passname time =
  start () ;
  output_pass passname;
  let oc =
    let path = Filename.concat !directory name_time_dir in
    handle_mkdir path ;
    let file = Filename.concat path (build_passtime_filename passname) in
    handle_open_out file
  in
  Printf.fprintf oc "%f\n%!" time;
  handle_close_out oc ;
  all_time := !all_time +. time ;
  let oc =
    let path = Filename.concat !directory name_time_dir in
    let file = Filename.concat path name_all_time in
    handle_open_out file
  in
  Printf.fprintf oc "%f\n%!" !all_time;
  handle_close_out oc

let global_env_stack : ((Obj.t -> unit) * (unit -> Obj.t)) list ref = ref []
let register_global_env (cap : ('a -> unit) * (unit -> 'a)) = global_env_stack := Obj.magic cap :: !global_env_stack
let register_global_ref r = register_global_env ((fun v -> r := v), (fun () -> !r))
let get_global_values () = List.map (fun (_,reader) -> Obj.obj (reader ())) !global_env_stack
let set_global_values values = List.iter2 (fun (writer,_) value -> writer (Obj.repr value)) !global_env_stack values

let marshal ~passname env =
  let (/) = Filename.concat in
  start ();
  output_pass passname;
  let pass = build_passname_filename passname in
  let oc =
    let path = !directory/pass in
    handle_mkdir path;
    let file = path/marshal_filename in
    handle_open_out file in
  Marshal.marshal_no_fun oc (env :: get_global_values ());
  handle_close_out oc

let unmarshal ~passname =
  let (/) = Filename.concat in
  start ();
  output_pass passname;
  let pass = build_passname_filename passname in
  let ic =
    let file = !directory/pass/marshal_filename in
    handle_open_in file in
  match Marshal.unmarshal_no_fun ic with
  | [] -> assert false
  | v :: values -> set_global_values values;
      handle_close_in ic;
      v

let current_passname = ref "default"
let set_current_passname passname = current_passname := passname
let make_fresh () =
  let t = Hashtbl.create 10 in
  (fun passname filename ->
     let key = passname, filename in
     let index = try Hashtbl.find t key with Not_found -> 0 in
     Hashtbl.add t key (succ index);
     index)
let internal_fresh = make_fresh ()
let internal ~filename fmt env =
  start () ;
  output_pass !current_passname ;
  let oc =
    let pass = build_passname_filename !current_passname in
    let path = Filename.concat !directory pass in
    let path = Filename.concat path name_internal_dir in
    handle_mkdir path ;
    let index = internal_fresh !current_passname filename in
    let filename = Filename.concat path (build_internal_filename filename index) in
    handle_open_out filename
  in
  let ff = Format.formatter_of_out_channel oc in
  fmt ff env ;
  Format.pp_print_flush ff () ;
  handle_close_out oc

let append_file =
  let table = Hashtbl.create 16 in
  (fun filename ->
     if Hashtbl.mem table filename
     then true
     else (
       Hashtbl.add table filename ();
       false
     ))

let file ~filename fmt env =
  start () ;
  output_pass !current_passname ;
  let filename =
    let pass = build_passname_filename !current_passname in
    let path = Filename.concat !directory pass in
    let path = Filename.concat path name_files_dir in
    handle_mkdir path ;
    let filename = Filename.concat path filename in
    filename
  in
  let oc =
    let append = append_file filename in
    handle_open_out ~append filename
  in
  fmt oc env ;
  Pervasives.flush oc ;
  handle_close_out oc ;
  filename


let check_fresh = make_fresh ()
let check_fail ~filename fmt env =
  start () ;
  output_pass !current_passname ;
  let oc =
    let pass = build_passname_filename !current_passname in
    let path = Filename.concat !directory pass in
    let path = Filename.concat path name_check_dir in
    handle_mkdir path ;
    let index = check_fresh !current_passname filename in
    let filename = Filename.concat path (build_check_filename filename index) in
    handle_open_out filename
  in
  let ff = Format.formatter_of_out_channel oc in
  fmt ff env ;
  Format.pp_print_flush ff () ;
  handle_close_out oc

(* exported in mli *)
let filename = build_track_name

let _ =
  Pervasives.at_exit finalize
Something went wrong with that request. Please try again.