Skip to content

Commit 44414fa

Browse files
derrickstoleedscho
authored andcommitted
Merge gvfs-helper prefetch feature
Includes these pull requests: #227 #228 #229 #231 #240 Signed-off-by: Derrick Stolee <[email protected]>
2 parents 86dd678 + 653d77d commit 44414fa

File tree

10 files changed

+1786
-335
lines changed

10 files changed

+1786
-335
lines changed

Documentation/config/core.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,10 @@ core.gvfs::
791791
is first accessed and brought down to the client. Git.exe can't
792792
currently tell the first access vs subsequent accesses so this
793793
flag just blocks them from occurring at all.
794+
GVFS_PREFETCH_DURING_FETCH::
795+
Bit value 128
796+
While performing a `git fetch` command, use the gvfs-helper to
797+
perform a "prefetch" of commits and trees.
794798
--
795799

796800
core.useGvfsHelper::

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2863,7 +2863,7 @@ gettext.sp gettext.s gettext.o: GIT-PREFIX
28632863
gettext.sp gettext.s gettext.o: EXTRA_CPPFLAGS = \
28642864
-DGIT_LOCALE_PATH='"$(localedir_relative_SQ)"'
28652865

2866-
http-push.sp http.sp http-walker.sp remote-curl.sp imap-send.sp: SP_EXTRA_FLAGS += \
2866+
http-push.sp http.sp http-walker.sp remote-curl.sp imap-send.sp gvfs-helper.sp: SP_EXTRA_FLAGS += \
28672867
-DCURL_DISABLE_TYPECHECK
28682868

28692869
pack-revindex.sp: SP_EXTRA_FLAGS += -Wno-memcpy-max-count

builtin/fetch.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
#include "string-list.h"
1919
#include "remote.h"
2020
#include "transport.h"
21+
#include "gvfs.h"
22+
#include "gvfs-helper-client.h"
23+
#include "packfile.h"
2124
#include "run-command.h"
2225
#include "parse-options.h"
2326
#include "sigchain.h"
@@ -1160,6 +1163,13 @@ static int store_updated_refs(struct display_state *display_state,
11601163

11611164
opt.exclude_hidden_refs_section = "fetch";
11621165
rm = ref_map;
1166+
1167+
/*
1168+
* Before checking connectivity, be really sure we have the
1169+
* latest pack-files loaded into memory.
1170+
*/
1171+
reprepare_packed_git(the_repository);
1172+
11631173
if (check_connected(iterate_ref_map, &rm, &opt)) {
11641174
rc = error(_("%s did not send all necessary objects\n"),
11651175
display_state->url);
@@ -2402,6 +2412,9 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
24022412
}
24032413
string_list_remove_duplicates(&list, 0);
24042414

2415+
if (core_gvfs & GVFS_PREFETCH_DURING_FETCH)
2416+
gh_client__prefetch(0, NULL);
2417+
24052418
if (negotiate_only) {
24062419
struct oidset acked_commits = OIDSET_INIT;
24072420
struct oidset_iter iter;

gvfs-helper-client.c

Lines changed: 116 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ static struct hashmap gh_server__subprocess_map;
2828
static struct object_directory *gh_client__chosen_odb;
2929

3030
/*
31-
* The "objects" capability has 2 verbs: "get" and "post".
31+
* The "objects" capability has verbs: "get" and "post" and "prefetch".
3232
*/
3333
#define CAP_OBJECTS (1u<<1)
3434
#define CAP_OBJECTS_NAME "objects"
3535

3636
#define CAP_OBJECTS__VERB_GET1_NAME "get"
3737
#define CAP_OBJECTS__VERB_POST_NAME "post"
38+
#define CAP_OBJECTS__VERB_PREFETCH_NAME "prefetch"
3839

3940
static int gh_client__start_fn(struct subprocess_entry *subprocess)
4041
{
@@ -133,6 +134,44 @@ static int gh_client__send__objects_get(struct child_process *process,
133134
return 0;
134135
}
135136

137+
/*
138+
* Send a request to gvfs-helper to prefetch packfiles from either the
139+
* cache-server or the main Git server using "/gvfs/prefetch".
140+
*
141+
* objects.prefetch LF
142+
* [<seconds-since_epoch> LF]
143+
* <flush>
144+
*/
145+
static int gh_client__send__objects_prefetch(struct child_process *process,
146+
timestamp_t seconds_since_epoch)
147+
{
148+
int err;
149+
150+
/*
151+
* We assume that all of the packet_ routines call error()
152+
* so that we don't have to.
153+
*/
154+
155+
err = packet_write_fmt_gently(
156+
process->in,
157+
(CAP_OBJECTS_NAME "." CAP_OBJECTS__VERB_PREFETCH_NAME "\n"));
158+
if (err)
159+
return err;
160+
161+
if (seconds_since_epoch) {
162+
err = packet_write_fmt_gently(process->in, "%" PRItime "\n",
163+
seconds_since_epoch);
164+
if (err)
165+
return err;
166+
}
167+
168+
err = packet_flush_gently(process->in);
169+
if (err)
170+
return err;
171+
172+
return 0;
173+
}
174+
136175
/*
137176
* Update the loose object cache to include the newly created
138177
* object.
@@ -180,7 +219,7 @@ static void gh_client__update_packed_git(const char *line)
180219
}
181220

182221
/*
183-
* Both CAP_OBJECTS verbs return the same format response:
222+
* CAP_OBJECTS verbs return the same format response:
184223
*
185224
* <odb>
186225
* <data>*
@@ -220,6 +259,8 @@ static int gh_client__objects__receive_response(
220259
const char *v1;
221260
char *line;
222261
int len;
262+
int nr_loose = 0;
263+
int nr_packfile = 0;
223264
int err = 0;
224265

225266
while (1) {
@@ -238,13 +279,13 @@ static int gh_client__objects__receive_response(
238279
else if (starts_with(line, "packfile")) {
239280
gh_client__update_packed_git(line);
240281
ghc |= GHC__CREATED__PACKFILE;
241-
*p_nr_packfile += 1;
282+
nr_packfile++;
242283
}
243284

244285
else if (starts_with(line, "loose")) {
245286
gh_client__update_loose_cache(line);
246287
ghc |= GHC__CREATED__LOOSE;
247-
*p_nr_loose += 1;
288+
nr_loose++;
248289
}
249290

250291
else if (starts_with(line, "ok"))
@@ -258,6 +299,8 @@ static int gh_client__objects__receive_response(
258299
}
259300

260301
*p_ghc = ghc;
302+
*p_nr_loose = nr_loose;
303+
*p_nr_packfile = nr_packfile;
261304

262305
return err;
263306
}
@@ -314,7 +357,7 @@ static struct gh_server__process *gh_client__find_long_running_process(
314357
/*
315358
* Find an existing long-running process with the above command
316359
* line -or- create a new long-running process for this and
317-
* subsequent 'get' requests.
360+
* subsequent requests.
318361
*/
319362
if (!gh_server__subprocess_map_initialized) {
320363
gh_server__subprocess_map_initialized = 1;
@@ -351,10 +394,14 @@ static struct gh_server__process *gh_client__find_long_running_process(
351394

352395
void gh_client__queue_oid(const struct object_id *oid)
353396
{
354-
// TODO consider removing this trace2. it is useful for interactive
355-
// TODO debugging, but may generate way too much noise for a data
356-
// TODO event.
357-
trace2_printf("gh_client__queue_oid: %s", oid_to_hex(oid));
397+
/*
398+
* Keep this trace as a printf only, so that it goes to the
399+
* perf log, but not the event log. It is useful for interactive
400+
* debugging, but generates way too much (unuseful) noise for the
401+
* database.
402+
*/
403+
if (trace2_is_enabled())
404+
trace2_printf("gh_client__queue_oid: %s", oid_to_hex(oid));
358405

359406
if (!oidset_insert(&gh_client__oidset_queued, oid))
360407
gh_client__oidset_count++;
@@ -435,10 +482,14 @@ int gh_client__get_immediate(const struct object_id *oid,
435482
int nr_packfile = 0;
436483
int err = 0;
437484

438-
// TODO consider removing this trace2. it is useful for interactive
439-
// TODO debugging, but may generate way too much noise for a data
440-
// TODO event.
441-
trace2_printf("gh_client__get_immediate: %s", oid_to_hex(oid));
485+
/*
486+
* Keep this trace as a printf only, so that it goes to the
487+
* perf log, but not the event log. It is useful for interactive
488+
* debugging, but generates way too much (unuseful) noise for the
489+
* database.
490+
*/
491+
if (trace2_is_enabled())
492+
trace2_printf("gh_client__get_immediate: %s", oid_to_hex(oid));
442493

443494
entry = gh_client__find_long_running_process(CAP_OBJECTS);
444495
if (!entry)
@@ -467,3 +518,55 @@ int gh_client__get_immediate(const struct object_id *oid,
467518

468519
return err;
469520
}
521+
522+
/*
523+
* Ask gvfs-helper to prefetch commits-and-trees packfiles since a
524+
* given timestamp.
525+
*
526+
* If seconds_since_epoch is zero, gvfs-helper will scan the ODB for
527+
* the last received prefetch and ask for ones newer than that.
528+
*/
529+
int gh_client__prefetch(timestamp_t seconds_since_epoch,
530+
int *nr_packfiles_received)
531+
{
532+
struct gh_server__process *entry;
533+
struct child_process *process;
534+
enum gh_client__created ghc;
535+
int nr_loose = 0;
536+
int nr_packfile = 0;
537+
int err = 0;
538+
539+
entry = gh_client__find_long_running_process(CAP_OBJECTS);
540+
if (!entry)
541+
return -1;
542+
543+
trace2_region_enter("gh-client", "objects/prefetch", the_repository);
544+
trace2_data_intmax("gh-client", the_repository, "prefetch/since",
545+
seconds_since_epoch);
546+
547+
process = &entry->subprocess.process;
548+
549+
sigchain_push(SIGPIPE, SIG_IGN);
550+
551+
err = gh_client__send__objects_prefetch(process, seconds_since_epoch);
552+
if (!err)
553+
err = gh_client__objects__receive_response(
554+
process, &ghc, &nr_loose, &nr_packfile);
555+
556+
sigchain_pop(SIGPIPE);
557+
558+
if (err) {
559+
subprocess_stop(&gh_server__subprocess_map,
560+
(struct subprocess_entry *)entry);
561+
FREE_AND_NULL(entry);
562+
}
563+
564+
trace2_data_intmax("gh-client", the_repository,
565+
"prefetch/packfile_count", nr_packfile);
566+
trace2_region_leave("gh-client", "objects/prefetch", the_repository);
567+
568+
if (nr_packfiles_received)
569+
*nr_packfiles_received = nr_packfile;
570+
571+
return err;
572+
}

gvfs-helper-client.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,22 @@ void gh_client__queue_oid_array(const struct object_id *oids, int oid_nr);
6666
*/
6767
int gh_client__drain_queue(enum gh_client__created *p_ghc);
6868

69+
/*
70+
* Ask `gvfs-helper server` to fetch any "prefetch packs"
71+
* available on the server more recent than the requested time.
72+
*
73+
* If seconds_since_epoch is zero, gvfs-helper will scan the ODB for
74+
* the last received prefetch and ask for ones newer than that.
75+
*
76+
* A long-running background process is used to subsequent requests
77+
* (either prefetch or regular immediate/queued requests) more efficient.
78+
*
79+
* One or more packfiles will be created in the shared-cache ODB.
80+
*
81+
* Returns 0 on success, -1 on error. Optionally also returns the
82+
* number of prefetch packs received.
83+
*/
84+
int gh_client__prefetch(timestamp_t seconds_since_epoch,
85+
int *nr_packfiles_received);
86+
6987
#endif /* GVFS_HELPER_CLIENT_H */

0 commit comments

Comments
 (0)