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 30, 2022
1 parent fa3aa28 commit 81786a7
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 62 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ Unreleased
- Validate the command line arguments for `$ dune ocaml top-module`. This
command requires one positional argument (#6796, fixes #6793, @rgrinberg)

- Add a `dune cache size` command for displaying the size of the cache (#6638,
@Alizter)

3.6.1 (2022-11-24)
------------------

Expand Down
34 changes: 30 additions & 4 deletions bin/cache.ml
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,43 @@ 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
"Compute the total size of files in the Dune cache which are not \
hardlinked from any build directory and output it 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 ]
72 changes: 16 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,22 @@ 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.
Compute the total size of files in the Dune cache which are not
hardlinked from any build directory and output it 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 +69,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 +82,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
45 changes: 45 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,45 @@
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

$ cat > config << EOF
> (lang dune 3.7)
> (cache enabled)
> (cache-storage-mode copy)
> EOF

$ cat > dune-project << EOF
> (lang dune 3.7)
> EOF

$ cat > dune << EOF
> (rule
> (targets target_a)
> (action
> (with-outputs-to
> target_a
> (echo Hello World!))))
> EOF

We build a simple file with 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 cache 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 of
the cache 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 81786a7

Please sign in to comment.