Skip to content

Commit

Permalink
virtualization: add new builtin command to print hydration level
Browse files Browse the repository at this point in the history
GVFS users can easily (and accidentally) over-hydrate their
enlistments. This causes some commands to be very slow.

Create a command to print the current hydration level.  This
should help our support team investigate the state of their
enlistment.

Signed-off-by: Jeff Hostetler <jeffhostetler@github.com>
  • Loading branch information
jeffhostetler committed Jun 19, 2024
1 parent 1748df7 commit 6d9e562
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
/git-verify-pack
/git-verify-tag
/git-version
/git-virtualization
/git-web--browse
/git-whatchanged
/git-worktree
Expand Down
24 changes: 24 additions & 0 deletions Documentation/git-virtualization.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
git-virtualization(1)
=====================

NAME
----

git-virtualization - EXPERIMENTAL: Measure virtualization level of worktree


SYNOPSIS
--------
[verse]
(EXPERIMENTAL!) `git virtualization` <options>


DESCRIPTION
-----------

Measure the virtualization level of the worktree.


GIT
---
Part of the linkgit:git[1] suite
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,7 @@ BUILTIN_OBJS += builtin/var.o
BUILTIN_OBJS += builtin/verify-commit.o
BUILTIN_OBJS += builtin/verify-pack.o
BUILTIN_OBJS += builtin/verify-tag.o
BUILTIN_OBJS += builtin/virtualization.o
BUILTIN_OBJS += builtin/worktree.o
BUILTIN_OBJS += builtin/write-tree.o

Expand Down
1 change: 1 addition & 0 deletions builtin.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ int cmd_var(int argc, const char **argv, const char *prefix);
int cmd_verify_commit(int argc, const char **argv, const char *prefix);
int cmd_verify_tag(int argc, const char **argv, const char *prefix);
int cmd_version(int argc, const char **argv, const char *prefix);
int cmd_virtualization(int argc, const char **argv, const char *prefix);
int cmd_whatchanged(int argc, const char **argv, const char *prefix);
int cmd_worktree(int argc, const char **argv, const char *prefix);
int cmd_write_tree(int argc, const char **argv, const char *prefix);
Expand Down
102 changes: 102 additions & 0 deletions builtin/virtualization.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include "builtin.h"
#include "config.h"
#include "json-writer.h"
#include "parse-options.h"
#include "read-cache-ll.h"
#include "repository.h"
#include "trace2.h"

static const char * const virt_usage[] = {
N_("(EXPERIMENTAL!) git virtualization"),
NULL,
};

static struct option virt_options[] = {
OPT_END(),
};

static int virt_load_config_cb(const char *var,
const char *value,
const struct config_context *ctx,
void *pvoid)
{
return git_default_config(var, value, ctx, pvoid);
}

static void virt_load_config(void)
{
git_config(virt_load_config_cb, NULL);
}

static uint64_t count_skipped(struct repository *repo)
{
uint64_t k;
uint64_t count = 0;

for (k = 0; k < repo->index->cache_nr; k++) {
const struct cache_entry *ce_k = repo->index->cache[k];

if (ce_skip_worktree(ce_k))
count++;
}

return count;
}

static void print_hydration(struct repository *repo)
{
uint64_t c_skipped;
uint64_t c_total;
uint64_t c_hydrated;
double hydration;

c_skipped = count_skipped(the_repository);
c_total = (uint64_t)the_repository->index->cache_nr;
c_hydrated = c_total - c_skipped;

if (c_total > 0)
hydration = (double)(c_hydrated * 100) / (double)c_total;
else
hydration = 0.0;

printf("Skipped: %" PRIu64 "\n", c_skipped);
printf("Hydrated: %" PRIu64 "\n", c_hydrated);
printf("Total: %" PRIu64 "\n", c_total);

printf("Hydration: %.2f%%\n", hydration);

if (trace2_is_enabled()) {
struct json_writer jw = JSON_WRITER_INIT;

jw_object_begin(&jw, 0);
{
jw_object_intmax(&jw, "skipped", c_skipped);
jw_object_intmax(&jw, "hydrated", c_hydrated);
jw_object_intmax(&jw, "total", c_total);

jw_object_double(&jw, "hydration", 2, hydration);
}
jw_end(&jw);

trace2_data_json("virt", repo, "stats", &jw);

jw_release(&jw);
}
}

int cmd_virtualization(int argc, const char **argv, const char *prefix)
{
prepare_repo_settings(the_repository);
the_repository->settings.command_requires_full_index = 1;

virt_load_config();

argc = parse_options(argc, argv, prefix, virt_options, virt_usage, 0);

if (repo_read_index(the_repository) < 0)
die("index file corrupt");

print_hydration(the_repository);

return 0;
}
1 change: 1 addition & 0 deletions command-list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ git-verify-commit ancillaryinterrogators
git-verify-pack plumbinginterrogators
git-verify-tag ancillaryinterrogators
git-version ancillaryinterrogators
git-virtualization mainporcelain
git-whatchanged ancillaryinterrogators complete
git-worktree mainporcelain
git-write-tree plumbingmanipulators
Expand Down
1 change: 1 addition & 0 deletions git.c
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ static struct cmd_struct commands[] = {
{ "verify-pack", cmd_verify_pack },
{ "verify-tag", cmd_verify_tag, RUN_SETUP },
{ "version", cmd_version },
{ "virtualization", cmd_virtualization, RUN_SETUP | NEED_WORK_TREE },
{ "whatchanged", cmd_whatchanged, RUN_SETUP },
{ "worktree", cmd_worktree, RUN_SETUP },
{ "write-tree", cmd_write_tree, RUN_SETUP },
Expand Down
11 changes: 11 additions & 0 deletions t/t1093-virtualfilesystem.sh
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ test_expect_success 'verify skip-worktree bit is set for absolute path' '
test_cmp expected actual
'

test_expect_success 'verify hydration percentage' '
git virtualization >actual &&
cat >expected <<-\EOF &&
Skipped: 3
Hydrated: 1
Total: 4
Hydration: 25.00%
EOF
test_cmp expected actual
'

test_expect_success 'verify skip-worktree bit is cleared for absolute path' '
clean_repo &&
write_script .git/hooks/virtualfilesystem <<-\EOF &&
Expand Down

0 comments on commit 6d9e562

Please sign in to comment.