1234567891011121314151617181920212223242526272829303132333435363738394041424344454647(* SPDX-License-Identifier: MIT *)(* Copyright (C) 2023-2024 formalsec *)(* Written by the Smtml programmers *)letrun_and_time_call~usef=letstart=Unix.gettimeofday()inletresult=f()inletstop=Unix.gettimeofday()inuse(stop-.start);resultletquery_log_path:Fpath.toption=letenv_var="QUERY_LOG_PATH"inmatchBos.OS.Env.varenv_varwithSomep->Some(Fpath.vp)|None->Nonelet[@inlinenever]protectmf=Mutex.lockm;matchf()with|x->Mutex.unlockm;x|exceptione->Mutex.unlockm;letbt=Printexc.get_raw_backtrace()inPrintexc.raise_with_backtraceebt(* If the environment variable [QUERY_LOG_PATH] is set, stores and writes
all queries sent to the solver (with their timestamps) to the given file *)letwrite=matchquery_log_pathwith|None->fun__->()|Somepath->letlog_entries:(Expr.tlist*int64)listref=ref[]inletclose()=letentries=List.rev!log_entriesinletbytes=Marshal.to_stringentries[]inmatchBos.OS.File.writepathbyteswith|Ok()->()|Error(`Msge)->Fmt.failwith"Failed to write log: %s"einat_exitclose;Sys.set_signalSys.sigterm(Sys.Signal_handle(fun_->close()));(* write *)letmutex=Mutex.create()infunassumptionstime->letentry=(assumptions,time)inprotectmutex(fun()->log_entries:=entry::!log_entries)