Skip to content

Commit

Permalink
feature(cache): add dune cache size command
Browse files Browse the repository at this point in the history
<!-- ps-id: 4b121081-bff4-484a-87fc-15e1f4648dc9 -->

Signed-off-by: Ali Caglayan <alizter@gmail.com>
  • Loading branch information
Alizter committed Dec 11, 2022
1 parent 130365b commit 8c00aba
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 62 deletions.
30 changes: 26 additions & 4 deletions bin/cache.ml
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,39 @@ let trim =
(User_message.make
[ Pp.textf "Freed %s" (Bytes_unit.pp trimmed_bytes) ])

let size =
let info =
let doc = "Query the size of the Dune cache" in
let man =
[ `P "Output the size of the Dune cache in a human-readable form." ]
in
Cmd.info "size" ~doc ~man
in
Cmd.v info
@@ let+ machine_readble =
Arg.(
value & flag
& info [ "machine-readable" ]
~doc:"Outputs size as a plain number of bytes.")
in
let size = Dune_cache.Trimmer.overhead_size () in
if machine_readble then
User_message.print (User_message.make [ Pp.textf "%Ld" size ])
else
User_message.print
(User_message.make [ Pp.textf "%s" (Bytes_unit.pp size) ])

let command =
let info =
let doc = "Manage the shared cache of build artifacts" in
let man =
[ `S "DESCRIPTION"
; `P
"Dune can share build artifacts between workspaces. Currently, the \
only action supported by this command is `trim`, but we plan to \
provide more functionality soon."
"Dune can share build artifacts between workspaces. We currently \
only support a few subcommands however we plan to provide more \
functionality soon."
]
in
Cmd.info "cache" ~doc ~man
in
Cmd.group info [ trim ]
Cmd.group info [ trim; size ]
70 changes: 14 additions & 56 deletions test/blackbox-tests/test-cases/dune-cache/cache-man.t
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,14 @@ Here we observe the documentation for the dune cache commands.
dune cache COMMAND …

DESCRIPTION
Dune can share build artifacts between workspaces. Currently, the only
action supported by this command is `trim`, but we plan to provide
more functionality soon.
Dune can share build artifacts between workspaces. We currently only
support a few subcommands however we plan to provide more
functionality soon.

COMMANDS
trim [--size=BYTES] [--trimmed-size=BYTES] [OPTION]…
Trim the Dune cache

COMMON OPTIONS
--help[=FMT] (default=auto)
Show this help in format FMT. The value FMT must be one of auto,
pager, groff or plain. With auto, the format is pager or plain
whenever the TERM env var is dumb or undefined.

--version
Show version information.
size [--machine-readable] [OPTION]…
Query the size of the Dune cache

EXIT STATUS
cache exits with the following status:

0 on success.

123 on indiscriminate errors reported on standard error.

124 on command line parsing errors.

125 on unexpected internal errors (bugs).

SEE ALSO
dune(1)


Man pages of the deprecated start and stop commands.

$ dune cache start --help=plain
NAME
dune-cache - Manage the shared cache of build artifacts

SYNOPSIS
dune cache COMMAND …

DESCRIPTION
Dune can share build artifacts between workspaces. Currently, the only
action supported by this command is `trim`, but we plan to provide
more functionality soon.

COMMANDS
trim [--size=BYTES] [--trimmed-size=BYTES] [OPTION]…
Trim the Dune cache

Expand All @@ -81,22 +42,20 @@ Man pages of the deprecated start and stop commands.
SEE ALSO
dune(1)

Testing the output of dune cache size --machine-readable

$ dune cache stop --help=plain
$ dune cache size --help=plain
NAME
dune-cache - Manage the shared cache of build artifacts
dune-cache-size - Query the size of the Dune cache

SYNOPSIS
dune cache COMMAND
dune cache size [--machine-readable] [OPTION]

DESCRIPTION
Dune can share build artifacts between workspaces. Currently, the only
action supported by this command is `trim`, but we plan to provide
more functionality soon.
Output the size of the Dune cache in a human-readable form.

COMMANDS
trim [--size=BYTES] [--trimmed-size=BYTES] [OPTION]…
Trim the Dune cache
OPTIONS
--machine-readable
Outputs size as a plain number of bytes.

COMMON OPTIONS
--help[=FMT] (default=auto)
Expand All @@ -108,7 +67,7 @@ Man pages of the deprecated start and stop commands.
Show version information.

EXIT STATUS
cache exits with the following status:
size exits with the following status:

0 on success.

Expand All @@ -121,7 +80,6 @@ Man pages of the deprecated start and stop commands.
SEE ALSO
dune(1)


Testing the output of dune cache trim.

$ dune cache trim --help=plain
Expand Down
3 changes: 3 additions & 0 deletions test/blackbox-tests/test-cases/dune-cache/size.t/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(lang dune 3.7)
(cache enabled)
(cache-storage-mode copy)
6 changes: 6 additions & 0 deletions test/blackbox-tests/test-cases/dune-cache/size.t/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(rule
(targets target_a)
(action
(with-outputs-to
target_a
(echo Hello World!))))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(lang dune 3.7)
26 changes: 26 additions & 0 deletions test/blackbox-tests/test-cases/dune-cache/size.t/run.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
This test checks that the dune cache size command returns the correct size of
the cache.

$ export DUNE_CACHE=enabled
$ export DUNE_CACHE_ROOT=$PWD/.cache

We build a simple file which the contents of "Hello World".

$ dune build target_a --display=short

Now we remove it so that we are checking the size of the file rather than the
link Dune created.

$ rm _build/default/target_a

The size command reports the size of the file in bytes in human readable form.
It correctly reports 12 bytes.

$ dune cache size
12B

We also have a machine readable version of the command which reports the size in
bytes directly without any units.

$ dune cache size --machine-readable
12
2 changes: 0 additions & 2 deletions test/blackbox-tests/test-cases/dune-cache/trim.t
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,3 @@ are part of the same rule.
TODO: Test trimming priority in the [copy] mode. In PR #4497 we added a test but
it turned out to be flaky so we subsequently deleted it in #4511.

0 comments on commit 8c00aba

Please sign in to comment.