Skip to content

Commit

Permalink
Mark the DB lock as high priority: try to avoid voluntary Thread.yiel…
Browse files Browse the repository at this point in the history
…d while we hold it

We'd still yield when the tick thread yields, or on syscalls.
  • Loading branch information
edwintorok committed Nov 18, 2024
2 parents 01388b3 + 0540f51 commit ac7a1b2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
8 changes: 7 additions & 1 deletion ocaml/database/db_cache_types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,12 @@ module Database = struct
let unregister_callback name x =
{x with callbacks= List.filter (fun (x, _) -> x <> name) x.callbacks}

let[@inline never] [@specialize never] notify_begin () = ()

let[@inline never] [@specialize never] notify_end () = ()

let notify e db =
notify_begin () ;
List.iter
(fun (name, f) ->
try f e db
Expand All @@ -376,7 +381,8 @@ module Database = struct
(Printexc.to_string e) name ;
()
)
db.callbacks
db.callbacks ;
notify_end ()

let reindex x =
let g = x.manifest.Manifest.generation_count in
Expand Down
10 changes: 9 additions & 1 deletion ocaml/database/db_lock.ml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ module ReentrantLock : REENTRANT_LOCK = struct

let current_tid () = Thread.(self () |> id)

let[@inline never] [@specialize never] lock_acquired () =
Xapi_timeslice.Timeslice.lock_acquired ()

let[@inline never] [@specialize never] lock_released () =
Xapi_timeslice.Timeslice.lock_released ()

let lock l =
let me = current_tid () in
match Atomic.get l.holder with
Expand All @@ -91,6 +97,7 @@ module ReentrantLock : REENTRANT_LOCK = struct
while not (Atomic.compare_and_set l.holder None intended) do
Condition.wait l.condition l.lock
done ;
lock_acquired () ;
let stats = l.statistics in
let delta = Clock.Timer.span_to_s (Mtime_clock.count counter) in
stats.total_time <- stats.total_time +. delta ;
Expand All @@ -109,7 +116,8 @@ module ReentrantLock : REENTRANT_LOCK = struct
let () = Atomic.set l.holder None in
Mutex.lock l.lock ;
Condition.signal l.condition ;
Mutex.unlock l.lock
Mutex.unlock l.lock ;
lock_released ()
)
| _ ->
failwith
Expand Down
1 change: 1 addition & 0 deletions ocaml/database/dune
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
xapi-stdext-std
xapi-stdext-threads
xapi-stdext-unix
xapi_timeslice
xml-light2
xmlm
)
Expand Down

0 comments on commit ac7a1b2

Please sign in to comment.