Skip to content

Commit

Permalink
Recompute cache size at application start
Browse files Browse the repository at this point in the history
  • Loading branch information
astrada committed Nov 19, 2016
1 parent 5f6b98f commit b218efe
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/cache.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1236,3 +1236,21 @@ let clean_up_cache cache =
(Sys.readdir cache.cache_dir)
end

let compute_cache_size cache =
if Sys.file_exists cache.cache_dir &&
Sys.is_directory cache.cache_dir then begin
Array.fold_left
(fun size file ->
try
let path = Filename.concat cache.cache_dir file in
if Sys.file_exists path && path <> cache.db_path then begin
let stats = Unix.LargeFile.stat path in
let file_size = stats.Unix.LargeFile.st_size in
Int64.add size file_size
end else size
with e -> Utils.log_exception e; size
)
0L
(Sys.readdir cache.cache_dir)
end else 0L

15 changes: 15 additions & 0 deletions src/drive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -728,13 +728,28 @@ let get_metadata () =
SessionM.return updated_metadata
in

let resync_cache_size db_metadata =
let old_cache_size = db_metadata.Cache.Metadata.cache_size in
Utils.log_with_header
"BEGIN: Recalculating cache size (old value=%Ld)\n%!"
old_cache_size;
let cache_size = Cache.compute_cache_size context.Context.cache in
Utils.log_with_header
"END: Recalculating cache size (new value=%Ld)\n%!"
cache_size;
db_metadata |> Cache.Metadata.cache_size ^= cache_size
in

with_metadata_lock
(fun () ->
let metadata =
let context = Context.get_ctx () in
if Option.is_none context.Context.metadata then begin
Utils.log_with_header "BEGIN: Loading metadata from db\n%!";
let db_metadata =
Cache.Metadata.select_metadata context.Context.cache in
let db_metadata =
Option.map resync_cache_size db_metadata in
Context.update_ctx (Context.metadata ^= db_metadata);
db_metadata
end else begin
Expand Down

0 comments on commit b218efe

Please sign in to comment.