Skip to content

Commit 8b030f1

Browse files
committed
CP-307958: Replace mutexes with Atomic where possible
Atomic has lower overhead, and can be used for simple situations (incrementing an integer, setting a boolean). ``` Mutex+incr (ns): { monotonic-clock per run = 32.272091 (confidence: 32.365531 to 32.170138); r² = Some 0.999441 } Atomic.incr (ns): { monotonic-clock per run = 2.938678 (confidence: 2.944486 to 2.933688); r² = Some 0.999857 } ``` No functional change Signed-off-by: Edwin Török <[email protected]>
1 parent aea9ab5 commit 8b030f1

File tree

3 files changed

+14
-35
lines changed

3 files changed

+14
-35
lines changed

ocaml/database/db_connections.ml

+3-13
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,12 @@ let preferred_write_db () = List.hd (Db_conn_store.read_db_connections ())
6262
let exit_on_next_flush = ref false
6363

6464
(* db flushing thread refcount: the last thread out of the door does the exit(0) when flush_on_exit is true *)
65-
let with_lock = Xapi_stdext_threads.Threadext.Mutex.execute
65+
let db_flush_thread_refcount = Atomic.make 0
6666

67-
let db_flush_thread_refcount_m = Mutex.create ()
68-
69-
let db_flush_thread_refcount = ref 0
70-
71-
let inc_db_flush_thread_refcount () =
72-
with_lock db_flush_thread_refcount_m (fun () ->
73-
db_flush_thread_refcount := !db_flush_thread_refcount + 1
74-
)
67+
let inc_db_flush_thread_refcount () = Atomic.incr db_flush_thread_refcount
7568

7669
let dec_and_read_db_flush_thread_refcount () =
77-
with_lock db_flush_thread_refcount_m (fun () ->
78-
db_flush_thread_refcount := !db_flush_thread_refcount - 1 ;
79-
!db_flush_thread_refcount
80-
)
70+
Atomic.fetch_and_add db_flush_thread_refcount (-1)
8171

8272
let pre_exit_hook () =
8373
(* We're about to exit. Close the active redo logs. *)

ocaml/database/db_remote_cache_access_v1.ml

+2-5
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ module DBCacheRemoteListener = struct
66

77
exception DBCacheListenerUnknownMessageName of string
88

9-
let ctr_mutex = Mutex.create ()
10-
11-
let calls_processed = ref 0
9+
let calls_processed = Atomic.make 0
1210

1311
let success xml =
1412
let resp = XMLRPC.To.array [XMLRPC.To.string "success"; xml] in
@@ -34,8 +32,7 @@ module DBCacheRemoteListener = struct
3432
Note that, although the messages still contain the pool_secret for historical reasons,
3533
access has already been applied by the RBAC code in Xapi_http.add_handler. *)
3634
let process_xmlrpc xml =
37-
let with_lock = Xapi_stdext_threads.Threadext.Mutex.execute in
38-
with_lock ctr_mutex (fun () -> calls_processed := !calls_processed + 1) ;
35+
Atomic.incr calls_processed ;
3936
let fn_name, args =
4037
match XMLRPC.From.array (fun x -> x) xml with
4138
| [fn_name; _; args] ->

ocaml/database/redo_log.ml

+9-17
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ type redo_log_conf = {
7777
; backoff_delay: int ref
7878
; sock: Unix.file_descr option ref
7979
; pid: (Forkhelpers.pidty * string * string) option ref
80-
; dying_processes_mutex: Mutex.t
81-
; num_dying_processes: int ref
80+
; num_dying_processes: int Atomic.t
8281
; mutex: Mutex.t (** exclusive access to this configuration *)
8382
}
8483

@@ -585,14 +584,10 @@ let shutdown log =
585584
(Thread.create
586585
(fun () ->
587586
D.debug "Waiting for I/O process with pid %d to die..." ipid ;
588-
with_lock log.dying_processes_mutex (fun () ->
589-
log.num_dying_processes := !(log.num_dying_processes) + 1
590-
) ;
587+
Atomic.incr log.num_dying_processes ;
591588
ignore (Forkhelpers.waitpid p) ;
592589
D.debug "Finished waiting for process with pid %d" ipid ;
593-
with_lock log.dying_processes_mutex (fun () ->
594-
log.num_dying_processes := !(log.num_dying_processes) - 1
595-
)
590+
Atomic.decr log.num_dying_processes
596591
)
597592
()
598593
) ;
@@ -633,13 +628,11 @@ let startup log =
633628
() (* We're already started *)
634629
| None -> (
635630
(* Don't start if there are already some processes hanging around *)
636-
with_lock log.dying_processes_mutex (fun () ->
637-
if
638-
!(log.num_dying_processes)
639-
>= Db_globs.redo_log_max_dying_processes
640-
then
641-
raise TooManyProcesses
642-
) ;
631+
if
632+
Atomic.get log.num_dying_processes
633+
>= Db_globs.redo_log_max_dying_processes
634+
then
635+
raise TooManyProcesses ;
643636
match !(log.device) with
644637
| None ->
645638
D.info "Could not find block device" ;
@@ -793,8 +786,7 @@ let create ~name ~state_change_callback ~read_only =
793786
; backoff_delay= ref Db_globs.redo_log_initial_backoff_delay
794787
; sock= ref None
795788
; pid= ref None
796-
; dying_processes_mutex= Mutex.create ()
797-
; num_dying_processes= ref 0
789+
; num_dying_processes= Atomic.make 0
798790
; mutex= Mutex.create ()
799791
}
800792
in

0 commit comments

Comments
 (0)