Skip to content

Commit

Permalink
[dbwriter][multicore] no need for thread
Browse files Browse the repository at this point in the history
Summary: Multicore doesn't need an extra thread+queue to serialise writes to the database, a mutex is sufficient and each worker thread does its own writes.

Reviewed By: martintrojer

Differential Revision:
D69656883

Privacy Context Container: L1208441

fbshipit-source-id: 6c2c9590ff585099d02aad9461ef0d01bb3455da
  • Loading branch information
ngorogiannis authored and facebook-github-bot committed Feb 24, 2025
1 parent df0f820 commit 077b23f
Showing 1 changed file with 7 additions and 72 deletions.
79 changes: 7 additions & 72 deletions infer/src/base/DBWriterDomain.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,80 +7,15 @@
open! IStd
module L = Logging

(* a request equipped with fields necessary to implement client blocking until the request is completed. *)
type request =
{ command: DBWriterCommand.t
; mutex: Error_checking_mutex.t
; mutable completed_flag: bool
; completed: Condition.t }

let make_request command =
{ command
; mutex= Error_checking_mutex.create ()
; completed_flag= false
; completed= Condition.create () }


let wait_for_completion request =
let rec wait_loop () =
if not request.completed_flag then (
Condition.wait request.completed request.mutex ;
wait_loop () )
in
Error_checking_mutex.critical_section request.mutex ~f:wait_loop


let request_queue = Concurrent.Queue.create ()

let rec server_loop () =
let request = Concurrent.Queue.dequeue request_queue in
let command = request.command in
( try
DBWriterCommand.perform command ;
(* signal completion to client *)
Error_checking_mutex.critical_section request.mutex ~f:(fun () ->
request.completed_flag <- true ;
Condition.signal request.completed )
with exn ->
L.die InternalError "DBWriter thread crashed@\n%a@\n%s@." Exn.pp exn
(Printexc.get_backtrace ()) ) ;
match (command : DBWriterCommand.t) with
| Terminate ->
L.debug Analysis Quiet "Sqlite write daemon: terminating@." ;
ExecutionDuration.log ~prefix:"dbwriter.store_sql" Analysis !DBWriterCommand.store_sql_time
| _ ->
server_loop ()


let domain_server () =
Config.set_gc_params () ;
Database.new_database_connections Primary ;
server_loop ()


let send command =
let request = make_request command in
Concurrent.Queue.enqueue request request_queue ;
wait_for_completion request


let dbwriter_domain = ref None

let start () =
dbwriter_domain := Some (Domain.spawn domain_server) ;
send Start


let terminate () =
send Terminate ;
Option.iter !dbwriter_domain ~f:Domain.join

let dbwriter_command_mutex = Error_checking_mutex.create ()

let perform cmd =
match (cmd : DBWriterCommand.t) with
| Start ->
start ()
| Terminate ->
terminate ()
L.debug Analysis Quiet "Sqlite write daemon: terminating@." ;
ExecutionDuration.log ~prefix:"dbwriter.store_sql" Analysis !DBWriterCommand.store_sql_time ;
Error_checking_mutex.critical_section dbwriter_command_mutex ~f:(fun () ->
DBWriterCommand.perform cmd )
| _ ->
send cmd
Error_checking_mutex.critical_section dbwriter_command_mutex ~f:(fun () ->
DBWriterCommand.perform cmd )

0 comments on commit 077b23f

Please sign in to comment.