* [PATCH] read-cache: call verify_hdr() in a background thread
@ 2017-03-24 13:27 git
2017-03-24 13:27 ` git
0 siblings, 1 reply; 7+ messages in thread
From: git @ 2017-03-24 13:27 UTC (permalink / raw)
To: git; +Cc: gitster, peff, Jeff Hostetler
From: Jeff Hostetler <jeffhost@microsoft.com>
This patch contains a performance optimization to run
verify_hdr() in a background thread while the foreground
thread parses the index. This allows do_read_index() to
complete faster.
This idea was recently discussed on the mailing list in:
https://public-inbox.org/git/85221b97-759f-b7a9-1256-21515d163cbf@jeffhostetler.com/
during a discussion on sha1dc.
Our testing on Windows showed that verifying the SHA1 hash
on the index file takes approximately the same amount of time
as parsing the file and building the array of cache_entries.
(It could also be that having 2 threads better ammortizes the
page faults of reading from the mmap'd file.)
An earlier version of this change has been in use in GfW since December:
https://github.com/git-for-windows/git/pull/978
Jeff Hostetler (1):
read-cache: call verify_hdr() in a background thread
read-cache.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 86 insertions(+), 1 deletion(-)
--
2.7.4
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH] read-cache: call verify_hdr() in a background thread
2017-03-24 13:27 [PATCH] read-cache: call verify_hdr() in a background thread git
@ 2017-03-24 13:27 ` git
2017-03-24 15:36 ` Jeff King
2017-03-24 16:35 ` Jonathan Nieder
0 siblings, 2 replies; 7+ messages in thread
From: git @ 2017-03-24 13:27 UTC (permalink / raw)
To: git; +Cc: gitster, peff, Jeff Hostetler
From: Jeff Hostetler <jeffhost@microsoft.com>
Teash do_read_index() in read-cache.c to call verify_hdr()
in a background thread while the forground thread parses
the index and builds the_index.
This is a performance optimization to reduce the overall
time required to get the index into memory.
Testing on Windows (using the OpenSSL SHA1 routine) showed
that parsing the index and computing the SHA1 take almost
equal time, so this patch effectively reduces the startup
time by 1/2.
Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com>
---
read-cache.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 86 insertions(+), 1 deletion(-)
diff --git a/read-cache.c b/read-cache.c
index 9054369..27c63a3 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -1564,6 +1564,83 @@ static void post_read_index_from(struct index_state *istate)
tweak_untracked_cache(istate);
}
+#ifdef NO_PTHREADS
+
+struct verify_hdr_thread_data {
+ struct cache_header *hdr;
+ size_t size;
+};
+
+/*
+ * Non-threaded version does all the work immediately.
+ * Returns < 0 on error or bad signature.
+ */
+static int verify_hdr_start(struct verify_hdr_thread_data *d)
+{
+ return verify_hdr(d->hdr, (unsigned long)d->size);
+}
+
+static int verify_hdr_finish(struct verify_hdr_thread_data *d)
+{
+ return 0;
+}
+
+#else
+
+#include <pthread.h>
+
+/*
+ * Require index file to be larger than this threshold before
+ * we bother using a background thread to verify the SHA.
+ */
+#define VERIFY_HDR_THRESHOLD (1024)
+
+struct verify_hdr_thread_data {
+ pthread_t thread_id;
+ struct cache_header *hdr;
+ size_t size;
+ int result;
+};
+
+/*
+ * Thread proc to run verify_hdr() computation in a background thread.
+ */
+static void *verify_hdr_thread_proc(void *_data)
+{
+ struct verify_hdr_thread_data *d = _data;
+ d->result = verify_hdr(d->hdr, (unsigned long)d->size);
+ return NULL;
+}
+
+/*
+ * Threaded version starts background thread and returns zero
+ * to indicate that we don't know the hash is bad yet. If the
+ * index is too small, we just do the work imediately.
+ */
+static int verify_hdr_start(struct verify_hdr_thread_data *d)
+{
+ if (d->size < VERIFY_HDR_THRESHOLD)
+ return verify_hdr(d->hdr, (unsigned long)d->size);
+
+ if (pthread_create(&d->thread_id, NULL, verify_hdr_thread_proc, d))
+ die_errno("unable to start verify_hdr_thread");
+
+ return 0;
+}
+
+static int verify_hdr_finish(struct verify_hdr_thread_data *d)
+{
+ if (d->size < VERIFY_HDR_THRESHOLD)
+ return 0;
+
+ if (pthread_join(d->thread_id, NULL))
+ die_errno("unable to join verify_hdr_thread");
+
+ return d->result;
+}
+
+#endif
+
/* remember to discard_cache() before reading a different cache! */
int do_read_index(struct index_state *istate, const char *path, int must_exist)
{
@@ -1574,6 +1651,7 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
void *mmap;
size_t mmap_size;
struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;
+ struct verify_hdr_thread_data data;
if (istate->initialized)
return istate->cache_nr;
@@ -1600,7 +1678,10 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
close(fd);
hdr = mmap;
- if (verify_hdr(hdr, mmap_size) < 0)
+
+ data.hdr = hdr;
+ data.size = mmap_size;
+ if (verify_hdr_start(&data) < 0)
goto unmap;
hashcpy(istate->sha1, (const unsigned char *)hdr + mmap_size - 20);
@@ -1649,6 +1730,10 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
src_offset += 8;
src_offset += extsize;
}
+
+ if (verify_hdr_finish(&data) < 0)
+ goto unmap;
+
munmap(mmap, mmap_size);
return istate->cache_nr;
--
2.7.4
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH] read-cache: call verify_hdr() in a background thread
2017-03-24 13:27 ` git
@ 2017-03-24 15:36 ` Jeff King
2017-03-24 15:46 ` Jeff Hostetler
2017-03-24 16:35 ` Jonathan Nieder
1 sibling, 1 reply; 7+ messages in thread
From: Jeff King @ 2017-03-24 15:36 UTC (permalink / raw)
To: git; +Cc: git, gitster, Jeff Hostetler
On Fri, Mar 24, 2017 at 01:27:51PM +0000, git@jeffhostetler.com wrote:
> From: Jeff Hostetler <jeffhost@microsoft.com>
>
> Teash do_read_index() in read-cache.c to call verify_hdr()
> in a background thread while the forground thread parses
> the index and builds the_index.
>
> This is a performance optimization to reduce the overall
> time required to get the index into memory.
>
> Testing on Windows (using the OpenSSL SHA1 routine) showed
> that parsing the index and computing the SHA1 take almost
> equal time, so this patch effectively reduces the startup
> time by 1/2.
Have you considered just skipping the sha1 check on read (possibly with
an option)?
Its purpose is to detect disk bit-rot. Checking it for every single
operation may be a bit excessive, especially because it gets rewritten a
lot. Nobody really cared until now because they don't have index files
that are hundreds of megabytes.
-Peff
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] read-cache: call verify_hdr() in a background thread
2017-03-24 15:36 ` Jeff King
@ 2017-03-24 15:46 ` Jeff Hostetler
0 siblings, 0 replies; 7+ messages in thread
From: Jeff Hostetler @ 2017-03-24 15:46 UTC (permalink / raw)
To: Jeff King; +Cc: git, gitster, Jeff Hostetler
On 3/24/2017 11:36 AM, Jeff King wrote:
> On Fri, Mar 24, 2017 at 01:27:51PM +0000, git@jeffhostetler.com wrote:
>
>> From: Jeff Hostetler <jeffhost@microsoft.com>
>>
>> Teash do_read_index() in read-cache.c to call verify_hdr()
>> in a background thread while the forground thread parses
>> the index and builds the_index.
>>
>> This is a performance optimization to reduce the overall
>> time required to get the index into memory.
>>
>> Testing on Windows (using the OpenSSL SHA1 routine) showed
>> that parsing the index and computing the SHA1 take almost
>> equal time, so this patch effectively reduces the startup
>> time by 1/2.
>
> Have you considered just skipping the sha1 check on read (possibly with
> an option)?
>
> Its purpose is to detect disk bit-rot. Checking it for every single
> operation may be a bit excessive, especially because it gets rewritten a
> lot. Nobody really cared until now because they don't have index files
> that are hundreds of megabytes.
Yes, we have done that in a version of the client customized
for the virtual file system effort and have been using it
internally without incident for quite a while.
And we can get a patch for that instead (or include it with
this one) if there's interest.
I tried to limit my proposal here to increasing performance while
preserving the existing behavior.
Jeff
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] read-cache: call verify_hdr() in a background thread
2017-03-24 13:27 ` git
2017-03-24 15:36 ` Jeff King
@ 2017-03-24 16:35 ` Jonathan Nieder
2017-03-24 17:33 ` Jeff Hostetler
1 sibling, 1 reply; 7+ messages in thread
From: Jonathan Nieder @ 2017-03-24 16:35 UTC (permalink / raw)
To: git; +Cc: git, gitster, peff, Jeff Hostetler
git@jeffhostetler.com wrote:
> From: Jeff Hostetler <jeffhost@microsoft.com>
>
> Teash do_read_index() in read-cache.c to call verify_hdr()
s/Teash/Teach/
> in a background thread while the forground thread parses
s/forground/foreground/
> the index and builds the_index.
>
> This is a performance optimization to reduce the overall
> time required to get the index into memory.
>
> Testing on Windows (using the OpenSSL SHA1 routine) showed
> that parsing the index and computing the SHA1 take almost
> equal time, so this patch effectively reduces the startup
> time by 1/2.
>
> Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com>
Nice. Do you have example commands I can run to reproduce
that benchmark? (Even better if you can phrase that as a
patch against t/perf/.)
[...]
> --- a/read-cache.c
> +++ b/read-cache.c
> @@ -1564,6 +1564,83 @@ static void post_read_index_from(struct index_state *istate)
> tweak_untracked_cache(istate);
> }
>
> +#ifdef NO_PTHREADS
> +
> +struct verify_hdr_thread_data {
> + struct cache_header *hdr;
> + size_t size;
'size' appears to always be cast to an unsigned long when it's
used. Why not use unsigned long consistently?
> +};
> +
> +/*
> + * Non-threaded version does all the work immediately.
> + * Returns < 0 on error or bad signature.
> + */
> +static int verify_hdr_start(struct verify_hdr_thread_data *d)
> +{
> + return verify_hdr(d->hdr, (unsigned long)d->size);
> +}
> +
> +static int verify_hdr_finish(struct verify_hdr_thread_data *d)
> +{
> + return 0;
> +}
> +
> +#else
> +
> +#include <pthread.h>
Please put this at the top of the file with other #includes. One
simple way to do that is to #include "thread-utils.h" at the top of
the file unconditionally.
> +
> +/*
> + * Require index file to be larger than this threshold before
> + * we bother using a background thread to verify the SHA.
> + */
> +#define VERIFY_HDR_THRESHOLD (1024)
nits: (1) no need for parens for a numerical macro like this
(2) comment can be made briefer and more explicit:
/*
* Index size threshold in bytes before it's worth bothering to
* use a background thread to verify the index file.
*/
How was this value chosen?
> +
> +struct verify_hdr_thread_data {
> + pthread_t thread_id;
> + struct cache_header *hdr;
> + size_t size;
> + int result;
> +};
All structs are data. Other parts of git seem to name this kind of
callback cookie *_cb_data, so perhaps verify_hdr_cb_data?
On the other hand this seems to also be used by the caller as a handle
to the async verify_hdr process. Maybe verify_hdr_state?
This seems to be doing something similar to the existing 'struct
async' interface. Could it use that instead, or does it incur too
much overhead? An advantage would be avoiding having to handle the
NO_PTHREADS ifdef-ery.
> +
> +/*
> + * Thread proc to run verify_hdr() computation in a background thread.
> + */
> +static void *verify_hdr_thread_proc(void *_data)
Please don't name identifiers with a leading underscore --- those are
reserved names.
> +{
> + struct verify_hdr_thread_data *d = _data;
> + d->result = verify_hdr(d->hdr, (unsigned long)d->size);
> + return NULL;
> +}
> +
> +/*
> + * Threaded version starts background thread and returns zero
> + * to indicate that we don't know the hash is bad yet. If the
> + * index is too small, we just do the work imediately.
> + */
> +static int verify_hdr_start(struct verify_hdr_thread_data *d)
This comment restates what the code says. Is there background or
something about the intent behind the code that could be said instead
to help the reader? Otherwise I'd suggest removing the comment.
[...]
> @@ -1574,6 +1651,7 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
> void *mmap;
> size_t mmap_size;
> struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;
> + struct verify_hdr_thread_data data;
>
> if (istate->initialized)
> return istate->cache_nr;
> @@ -1600,7 +1678,10 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
> close(fd);
>
> hdr = mmap;
> - if (verify_hdr(hdr, mmap_size) < 0)
> +
> + data.hdr = hdr;
> + data.size = mmap_size;
> + if (verify_hdr_start(&data) < 0)
> goto unmap;
What happens if there is an error before the code reaches the end of
the function? I think there needs to be a verify_hdr_finish call in
the 'unmap:' cleanup section.
The rest looks reasonable.
Thanks and hope that helps,
Jonathan
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] read-cache: call verify_hdr() in a background thread
2017-03-24 16:35 ` Jonathan Nieder
@ 2017-03-24 17:33 ` Jeff Hostetler
2017-03-24 17:53 ` Jonathan Nieder
0 siblings, 1 reply; 7+ messages in thread
From: Jeff Hostetler @ 2017-03-24 17:33 UTC (permalink / raw)
To: Jonathan Nieder; +Cc: git, gitster, peff, Jeff Hostetler
On 3/24/2017 12:35 PM, Jonathan Nieder wrote:
> git@jeffhostetler.com wrote:
>
>> From: Jeff Hostetler <jeffhost@microsoft.com>
>>
>> Teash do_read_index() in read-cache.c to call verify_hdr()
>...
> Nice. Do you have example commands I can run to reproduce
> that benchmark? (Even better if you can phrase that as a
> patch against t/perf/.)
I debated doing a t/perf and/or t/helper to demonstrate this
like I did for the lazy-init-name-hash changes the other day,
but decided against it. I'll put together something and
include it in the next version.
>
> [...]
>> --- a/read-cache.c
>> +++ b/read-cache.c
>> @@ -1564,6 +1564,83 @@ static void post_read_index_from(struct index_state *istate)
>> tweak_untracked_cache(istate);
>> }
>>
>> +#ifdef NO_PTHREADS
>> +
>> +struct verify_hdr_thread_data {
>> + struct cache_header *hdr;
>> + size_t size;
>
> 'size' appears to always be cast to an unsigned long when it's
> used. Why not use unsigned long consistently?
>
>> +};
>> +
>> +/*
>> + * Non-threaded version does all the work immediately.
>> + * Returns < 0 on error or bad signature.
>> + */
>> +static int verify_hdr_start(struct verify_hdr_thread_data *d)
>> +{
>> + return verify_hdr(d->hdr, (unsigned long)d->size);
>> +}
>> +
>> +static int verify_hdr_finish(struct verify_hdr_thread_data *d)
>> +{
>> + return 0;
>> +}
>> +
>> +#else
>> +
>> +#include <pthread.h>
>
> Please put this at the top of the file with other #includes. One
> simple way to do that is to #include "thread-utils.h" at the top of
> the file unconditionally.
>
>> +
>> +/*
>> + * Require index file to be larger than this threshold before
>> + * we bother using a background thread to verify the SHA.
>> + */
>> +#define VERIFY_HDR_THRESHOLD (1024)
>
> nits: (1) no need for parens for a numerical macro like this
> (2) comment can be made briefer and more explicit:
>
> /*
> * Index size threshold in bytes before it's worth bothering to
> * use a background thread to verify the index file.
> */
>
> How was this value chosen?
This was somewhat at random. I'll update with the t/perf stuff.
>
>> +
>> +struct verify_hdr_thread_data {
>> + pthread_t thread_id;
>> + struct cache_header *hdr;
>> + size_t size;
>> + int result;
>> +};
>
> All structs are data. Other parts of git seem to name this kind of
> callback cookie *_cb_data, so perhaps verify_hdr_cb_data?
>
> On the other hand this seems to also be used by the caller as a handle
> to the async verify_hdr process. Maybe verify_hdr_state?
>
> This seems to be doing something similar to the existing 'struct
> async' interface. Could it use that instead, or does it incur too
> much overhead? An advantage would be avoiding having to handle the
> NO_PTHREADS ifdef-ery.
>
>> +
>> +/*
>> + * Thread proc to run verify_hdr() computation in a background thread.
>> + */
>> +static void *verify_hdr_thread_proc(void *_data)
>
> Please don't name identifiers with a leading underscore --- those are
> reserved names.
>
>> +{
>> + struct verify_hdr_thread_data *d = _data;
>> + d->result = verify_hdr(d->hdr, (unsigned long)d->size);
>> + return NULL;
>> +}
I was just modeling the code on what I saw in preload-index.c.
There the #ifdef side defines the trivial functions. Then the #else
and the #include, the struct thread_data, and then the
preload_thread(void *_data) function declaration. But blame reports
that code dating back to 2008. Is there an example of a newer
preferred style somewhere ?
>> +
>> +/*
>> + * Threaded version starts background thread and returns zero
>> + * to indicate that we don't know the hash is bad yet. If the
>> + * index is too small, we just do the work imediately.
>> + */
>> +static int verify_hdr_start(struct verify_hdr_thread_data *d)
>
> This comment restates what the code says. Is there background or
> something about the intent behind the code that could be said instead
> to help the reader? Otherwise I'd suggest removing the comment.
>
> [...]
>
> What happens if there is an error before the code reaches the end of
> the function? I think there needs to be a verify_hdr_finish call in
> the 'unmap:' cleanup section.
But the "unmap" section calls die(). Do need to join first ??
(It's OK if we do, just asking.)
>
> The rest looks reasonable.
>
> Thanks and hope that helps,
> Jonathan
>
Thanks,
Jeff
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] read-cache: call verify_hdr() in a background thread
2017-03-24 17:33 ` Jeff Hostetler
@ 2017-03-24 17:53 ` Jonathan Nieder
0 siblings, 0 replies; 7+ messages in thread
From: Jonathan Nieder @ 2017-03-24 17:53 UTC (permalink / raw)
To: Jeff Hostetler; +Cc: git, gitster, peff, Jeff Hostetler
Jeff Hostetler wrote:
> On 3/24/2017 12:35 PM, Jonathan Nieder wrote:
>> What happens if there is an error before the code reaches the end of
>> the function? I think there needs to be a verify_hdr_finish call in
>> the 'unmap:' cleanup section.
>
> But the "unmap" section calls die(). Do need to join first ??
> (It's OK if we do, just asking.)
Good point. Now I wonder why the 'unmap:' section exists at all.
$ git log -Sunmap: read-cache.c
commit e83c5163316f89bfbde7d9ab23ca2e25604af290
Author: Linus Torvalds <torvalds@ppc970.osdl.org>
Date: Thu Apr 7 15:13:13 2005 -0700
Initial revision of "git", the information manager from hell
So the unmap: section has been there since day one. At that point
it used 'return error(...)', so it made more sense. Where did the
die() come from, then?
commit 5d1a5c02e8ac1c16688ea4a44512245f25a49f8a
Author: Linus Torvalds <torvalds@osdl.org>
Date: Sat Oct 1 13:24:27 2005 -0700
[PATCH] Better error reporting for "git status"
I think it should be safe to eliminate the 'unmap' section altogether
and that that change just forgot to, but in any case it's orthogonal
to your patch.
Thanks and sorry for the confusion,
Jonathan
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2017-03-24 17:53 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-03-24 13:27 [PATCH] read-cache: call verify_hdr() in a background thread git
2017-03-24 13:27 ` git
2017-03-24 15:36 ` Jeff King
2017-03-24 15:46 ` Jeff Hostetler
2017-03-24 16:35 ` Jonathan Nieder
2017-03-24 17:33 ` Jeff Hostetler
2017-03-24 17:53 ` Jonathan Nieder
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).