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 7, 2022
1 parent 670b7fb commit 3ce5c97
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 62 deletions.
33 changes: 29 additions & 4 deletions bin/cache.ml
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,42 @@ 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
"Queries the size of the Dune cache and outputs a human-readable \
result."
]
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 ]
71 changes: 15 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,21 @@ 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.
Queries the size of the Dune cache and outputs a human-readable
result.

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 +68,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 +81,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
40 changes: 40 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,40 @@
Testing the dune cache size command.

$ 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
> (system "dd if=/dev/zero of=target_a bs=1M count=1 status=none")))
> (rule
> (deps target_a)
> (targets target_b)
> (action
> (with-outputs-to
> target_b
> (run dune_cmd stat size target_a))))
> EOF

$ dune build target_b --display=short
sh target_a
dune_cmd target_b

$ rm _build/default/target_a

$ dune cache size
1.05MB

$ dune cache size --machine-readable
1048576
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 3ce5c97

Please sign in to comment.