Debian Codegraph @

plugins/micromega/persistent_cache.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10 (* *)
11 (* A persistent hashtable *)
12 (* *)
13 (* Frédéric Besson (Inria Rennes) 2009-2014 *)
14 (* *)
15 (************************************************************************)
16
17 (** Last PR that requires the regeneration of caches.
18 It is stored and checked after the Rocq magic number.
19 Incompatible caches are thrown away.
20 *)
21 let pcache_version = 15584l
22
23 module type PHashtable = sig
24 (* see documentation in [persistent_cache.mli] *)
25 type 'a t
26 type key
27
28 val open_in : string -> 'a t
29 val find : 'a t -> key -> 'a
30 val add : 'a t -> key -> 'a -> unit
31 val memo : string -> (key -> 'a) -> key -> 'a
32 val memo_cond : string -> (key -> bool) -> (key -> 'a) -> key -> 'a
33 end
34
35 open Hashtbl
36 open System
37
38 module PHashtable (Key : HashedType) : PHashtable with type key = Key.t = struct
39 open Unix
40
41 type key = Key.t
42
43 module Table :
44 sig
45 type 'a t
46 val empty : 'a t
47 val add : int -> 'a -> 'a t -> 'a t
48 val find : int -> 'a t -> 'a list
49 end =
50 struct
51 type 'a t = 'a list Int.Map.t
52 let empty = Int.Map.empty
53 let add h pos tab =
54 try Int.Map.modify h (fun _ l -> pos :: l) tab
55 with Not_found -> Int.Map.add h [pos] tab
56
57 let find h tab = Int.Map.find h tab
58 end
59 (* A mapping key hash -> file position *)
60
61 type 'a data = { pos : int option ; mutable obj : (Key.t * 'a) option }
62
63 type 'a t = {outch : out_channel; mutable htbl : 'a data Table.t; file : string }
64
65 let skip_blob ch =
66 let hd = Bytes.create Marshal.header_size in
67 let () = really_input ch hd 0 Marshal.header_size in
68 let len = Marshal.data_size hd 0 in
69 let pos = pos_in ch in
70 seek_in ch (pos + len)
71
72 let read_key_elem inch = match input_binary_int inch with
73 | hash ->
74 let pos = pos_in inch in
75 let () = skip_blob inch in
76 Some (hash, pos)
77 | exception End_of_file -> None
78
79 (**
80 We used to only lock/unlock regions.
81 Is-it more robust/portable to lock/unlock a fixed region e.g. [0;1]?
82 In case of locking failure, the cache is not used.
83 **)
84
85 type lock_kind = Read | Write
86
87 let lock kd fd =
88 let pos = lseek fd 0 SEEK_CUR in
89 let success =
90 try
91 ignore (lseek fd 0 SEEK_SET);
92 let lk = match kd with Read -> F_RLOCK | Write -> F_LOCK in
93 lockf fd lk 1; true
94 with Unix.Unix_error (_, _, _) -> false
95 in
96 ignore (lseek fd pos SEEK_SET);
97 success
98
99 let unlock fd =
100 let pos = lseek fd 0 SEEK_CUR in
101 let () =
102 try
103 ignore (lseek fd 0 SEEK_SET);
104 lockf fd F_ULOCK 1
105 with Unix.Unix_error (_, _, _) ->
106 (* Here, this is really bad news --
107 there is a pending lock which could cause a deadlock.
108 Should it be an anomaly or produce a warning ?
109 *)
110 ignore (lseek fd pos SEEK_SET)
111 in
112 ()
113
114 (* We make the assumption that an acquired lock can always be released *)
115
116 let do_under_lock kd fd f =
117 if lock kd fd then Some(Fun.protect f ~finally:(fun () -> unlock fd)) else None
118
119 let fopen_in = open_in_bin
120
121 let check_magic_number_and_version inch =
122 try
123 let magic = input_int32 inch in
124 let version = input_int32 inch in
125 magic = ObjFile.magic_number && version = pcache_version
126 with End_of_file -> false
127
128 let open_in (type a) f : a t =
129 let flags = [O_RDONLY; O_CREAT] in
130 let finch = openfile f flags 0o666 in
131 let inch = in_channel_of_descr finch in
132 let exception InvalidTableFormat in
133 let rec xload table =
134 match read_key_elem inch with
135 | None -> table
136 | Some (hash, pos) -> xload (Table.add hash { pos = Some pos; obj = None } table)
137 | exception e when CErrors.noncritical e -> raise InvalidTableFormat
138 in
139
140 try
141 (* Locking of the (whole) file while reading *)
142 let htbl = do_under_lock Read finch (fun () ->
143 let table = Table.empty in
144 if check_magic_number_and_version inch
145 then xload table
146 else raise InvalidTableFormat
147 ) in
148 let () = close_in_noerr inch in
149 let outch = out_channel_of_descr (openfile f [O_WRONLY; O_APPEND; O_CREAT] 0o666) in
150 { outch ; file = f; htbl = Option.default Table.empty htbl }
151 with InvalidTableFormat ->
152 let () = close_in_noerr inch in
153 let flags = [O_WRONLY; O_TRUNC; O_APPEND; O_CREAT] in
154 let out = openfile f flags 0o666 in
155 let outch = out_channel_of_descr out in
156 output_int32 outch ObjFile.magic_number;
157 output_int32 outch pcache_version;
158 {outch; htbl=Table.empty; file = f}
159
160 let add t k e =
161 let {outch} = t in
162 let fd = descr_of_out_channel outch in
163 let h = Key.hash k land 0x7FFFFFFF in
164 let dump () =
165 let () = output_binary_int outch h in
166 let () = Marshal.to_channel outch (k, e) [] in
167 let () = flush outch in
168 ()
169 in
170 let _ = do_under_lock Write fd dump in
171 t.htbl <- Table.add h { pos=None; obj = Some (k, e) } t.htbl
172
173 let find t k =
174 let {outch; htbl = tbl} = t in
175 let h = Key.hash k land 0x7FFFFFFF in
176 let lpos = Table.find h tbl in
177 (* First look for already live data *)
178 let find data = match data.obj with
179 | Some (k', v) -> if Key.equal k k' then Some v else None
180 | None -> None
181 in
182 match CList.find_map find lpos with
183 | Some res -> res
184 | None ->
185 (* Otherwise perform I/O and look at the disk cache *)
186 let lpos = List.filter (fun data -> Option.is_empty data.obj) lpos in
187 let () = if CList.is_empty lpos then raise Not_found in
188 let ch = fopen_in t.file in
189 let find data =
190 match data.pos with
191 | None -> None
192 | Some pos ->
193 let () = seek_in ch pos in
194 match Marshal.from_channel ch with
195 | (k', v) ->
196 if Key.equal k k' then
197 (* Store the data in memory *)
198 let () = data.obj <- Some (k, v) in
199 Some v
200 else None
201 | exception End_of_file | exception Failure _ -> None
202 in
203 let lookup () = CList.find_map find lpos in
204 let res = do_under_lock Read (descr_of_in_channel ch) lookup in
205 let () = close_in_noerr ch in
206 match res with
207 | Some (Some v) -> v
208 | None | Some None -> raise Not_found
209
210 let memo cache f =
211 let tbl = lazy (try Some (open_in cache) with e when CErrors.noncritical e -> None) in
212 fun x ->
213 match Lazy.force tbl with
214 | None -> f x
215 | Some tbl -> (
216 try find tbl x
217 with Not_found ->
218 let res = f x in
219 add tbl x res; res )
220
221 let memo_cond cache cond f =
222 let tbl = lazy (try Some (open_in cache) with e when CErrors.noncritical e -> None) in
223 fun x ->
224 match Lazy.force tbl with
225 | None -> f x
226 | Some tbl ->
227 if cond x then begin
228 try find tbl x
229 with Not_found ->
230 let res = f x in
231 add tbl x res; res
232 end
233 else f x
234 end
235
236 (* Local Variables: *)
237 (* coding: utf-8 *)
238 (* End: *)