multithread traverse_commit_list (aka rev-list)
cover multithread traverse_commit_list (aka rev-list)
rev-list is quite cpu extensive. While there's better approach to tackle this (e.g. Shawn's bitmap cache idea), I was curious if making it parellel could give us anything. So this is a naive (and failed) attempt at that. I post it anyway, so people, especially gsoc students if the related proposal is chosen, might learn from it, or learn to avoid it, or just laugh at my attempt to relieve stress.
While this series does not break rev-list (but breaks all external commands, I don't care anyway), and it proves lock-free pack access works, it does not improve rev-list performance. I suspect extensive locking around "struct object *" may be the culprit. Or maybe it's just my dumb partition method. A better partition method that avoids locking might improve it. We may split the object hash table into 4, one for each object type and assign one thread for tree, one for blob. Or something else..
Anyway I'll stop here to finish my ongoing topics for 1.7.11 and maybe implement Shawn's idea if I still have more time. Effectively generating the cache from index-pack could be challenging.
Nguyễn Thái Ngọc Duy (5):
Remove global pointer "packed_git" in favor or set/get function pair
sha1_file: stuff various pack reading variables into a struct Make lookup_*() functions thread-safe
Teach traverse_commit_list callsites about new parameter, nr_threads Support multithread in traverse_commit_list and rev-list
blob.c | 11 ++- builtin/count-objects.c | 4 +- builtin/fsck.c | 4 +- builtin/gc.c | 2 +- builtin/pack-objects.c | 12 +- builtin/pack-redundant.c | 4 +- builtin/rev-list.c | 5 +- cache.h | 8 +- commit.c | 12 ++- git.c | 1 + list-objects.c | 157 +++++++++++++++++++++++++- list-objects.h | 2 +- object.c | 9 ++- object.h | 6 + pack-revindex.c | 4 +- revision.c | 16 +++ revision.h | 2 + server-info.c | 4 +- sha1_file.c | 276 ++++++++++++++++++++++++++++------------------ sha1_name.c | 2 +- tag.c | 9 +- tree.c | 18 +++- upload-pack.c | 2 +- 23 files changed, 419 insertions(+), 151 deletions(-)
Signature · 1 line
1.7.8.36.g69ee2
PATCH 1/5 Remove global pointer "packed_git" in favor or set/get function pair
--- builtin/count-objects.c | 4 ++-- builtin/fsck.c | 4 ++-- builtin/gc.c | 2 +- builtin/pack-objects.c | 10 +++++----- builtin/pack-redundant.c | 4 ++-- cache.h | 7 +++++-- pack-revindex.c | 4 ++-- server-info.c | 4 ++-- sha1_file.c | 33 ++++++++++++++++++++++----------- sha1_name.c | 2 +- 10 files changed, 44 insertions(+), 30 deletions(-)
builtin/count-objects.c +2 -2
@@ -104,9 +104,9 @@ int cmd_count_objects(int argc, const char **argv, const char *prefix)struct packed_git *p;unsigned long num_pack =0 ;off_t size_pack =0 ; -if (!packed_git) +if (!get_packed_git ())prepare_packed_git (); -for (p = packed_git; p; p = p->next) { +for (p =get_packed_git (); p; p = p->next) {if (!p->pack_local)continue ;if (open_pack_index (p))
builtin/fsck.c +2 -2
@@ -669,7 +669,7 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)prepare_packed_git ();if (show_progress) { -for (p = packed_git; p; p = p->next) { +for (p =get_packed_git (); p; p = p->next) {if (open_pack_index (p))continue ; total += p->num_objects;
@@ -677,7 +677,7 @@ int cmd_fsck(int argc, const char **argv, const char *prefix) progress =start_progress ("Checking objects" , total); } -for (p = packed_git; p; p = p->next) { +for (p =get_packed_git (); p; p = p->next) {/* verify gives error messages itself */ if (verify_pack (p, fsck_obj_buffer, progress, count))
builtin/gc.c +1 -1
@@ -130,7 +130,7 @@ static int too_many_packs(void)return 0 ;prepare_packed_git (); -for (cnt =0 , p = packed_git; p; p = p->next) { +for (cnt =0 , p =get_packed_git (); p; p = p->next) {if (!p->pack_local)continue ;if (p->pack_keep)
builtin/pack-objects.c +5 -5
@@ -840,7 +840,7 @@ static int add_object_entry(const unsigned char *sha1, enum object_type type,if (!exclude && local &&has_loose_object_nonlocal (sha1))return 0 ; -for (p = packed_git; p; p = p->next) { +for (p =get_packed_git (); p; p = p->next) {off_t offset =find_pack_entry_one (sha1, p);if (offset) {if (!found_pack) {
@@ -2183,7 +2183,7 @@ static void add_objects_in_unpacked_packs(struct rev_info *revs)memset (&in_pack,0 ,sizeof (in_pack)); -for (p = packed_git; p; p = p->next) { +for (p =get_packed_git (); p; p = p->next) {const unsigned char *sha1;struct object *o;
@@ -2221,7 +2221,7 @@ static int has_sha1_pack_kept_or_nonlocal(const unsigned char *sha1)static struct packed_git *last_found = (void *)1 ;struct packed_git *p; - p = (last_found != (void *)1 ) ? last_found : packed_git; + p = (last_found != (void *)1 ) ? last_found :get_packed_git ();while (p) { if ((!p->pack_local || p->pack_keep) &&
@@ -2230,7 +2230,7 @@ static int has_sha1_pack_kept_or_nonlocal(const unsigned char *sha1)return 1 ; }if (p == last_found) - p = packed_git; + p =get_packed_git ();else p = p->next;if (p == last_found)
@@ -2245,7 +2245,7 @@ static void loosen_unused_packed_objects(struct rev_info *revs)uint32_t i;const unsigned char *sha1; -for (p = packed_git; p; p = p->next) { +for (p =get_packed_git (); p; p = p->next) {if (!p->pack_local || p->pack_keep)continue ;
builtin/pack-redundant.c +2 -2
@@ -569,7 +569,7 @@ static struct pack_list * add_pack(struct packed_git *p)static struct pack_list *add_pack_file (const char *filename) { -struct packed_git *p = packed_git; +struct packed_git *p =get_packed_git ();if (strlen (filename) <40 )die ("Bad pack filename: %s" , filename);
@@ -584,7 +584,7 @@ static struct pack_list * add_pack_file(const char *filename)static void load_all (void ) { -struct packed_git *p = packed_git; +struct packed_git *p =get_packed_git ();while (p) {add_pack (p);
cache.h +5 -2
@@ -975,7 +975,7 @@ struct pack_window {unsigned int inuse_cnt; }; -extern struct packed_git { +struct packed_git {struct packed_git *next;struct pack_window *windows;off_t pack_size;
@@ -993,7 +993,10 @@ extern struct packed_git {unsigned char sha1[20 ];/* something like ".git/objects/pack/xxxxx.pack" */ char pack_name[FLEX_ARRAY ];/* more */ -} *packed_git; +}; + +extern struct packed_git *get_packed_git (void ); +extern void set_packed_git (struct packed_git *);struct pack_entry {off_t offset;
pack-revindex.c +2 -2
@@ -45,13 +45,13 @@ static void init_pack_revindex(void)int num;struct packed_git *p; -for (num =0 , p = packed_git; p; p = p->next) +for (num =0 , p =get_packed_git (); p; p = p->next) num++;if (!num)return ; pack_revindex_hashsz = num *11 ; pack_revindex =xcalloc (sizeof (*pack_revindex), pack_revindex_hashsz); -for (p = packed_git; p; p = p->next) { +for (p =get_packed_git (); p; p = p->next) { num =pack_revindex_ix (p); num = -1 - num; pack_revindex[num].p = p;
server-info.c +2 -2
@@ -161,7 +161,7 @@ static void init_pack_info(const char *infofile, int force) objdirlen =strlen (objdir);prepare_packed_git (); -for (p = packed_git; p; p = p->next) { +for (p =get_packed_git (); p; p = p->next) {/* we ignore things on alternate path since they are * not available to the pullers in general. */
@@ -171,7 +171,7 @@ static void init_pack_info(const char *infofile, int force) } num_pack = i; info =xcalloc (num_pack,sizeof (struct pack_info *)); -for (i =0 , p = packed_git; p; p = p->next) { +for (i =0 , p =get_packed_git (); p; p = p->next) {if (!p->pack_local)continue ; info[i] =xcalloc (1 ,sizeof (struct pack_info ));
sha1_file.c +22 -11
@@ -33,6 +33,17 @@ static inline uintmax_t sz_fmt(size_t s) { return s; }const unsigned char null_sha1[20 ]; +static struct packed_git *packed_git_; +struct packed_git *get_packed_git (void ) +{ +return packed_git_; +} +void set_packed_git (struct packed_git *p) +{ + packed_git_ = p; +} + +/* * This is meant to hold a *small* number of objects that you would * want read_sha1_file() to be able to return, but yet you do not want
@@ -618,7 +629,7 @@ static int unuse_one_window(struct packed_git *current, int keep_fd)if (current)scan_windows (current, &lru_p, &lru_w, &lru_l); -for (p = packed_git; p; p = p->next) +for (p =get_packed_git (); p; p = p->next)scan_windows (p, &lru_p, &lru_w, &lru_l);if (lru_p) {munmap (lru_w->base, lru_w->len);
@@ -708,7 +719,7 @@ void close_pack_index(struct packed_git *p)*/ void free_pack_by_name (const char *pack_name) { -struct packed_git *p, **pp = &packed_git; +struct packed_git *p, **pp = &packed_git_;while (*pp) { p = *pp;
@@ -990,8 +1001,8 @@ void install_packed_git(struct packed_git *pack)if (pack->pack_fd !=-1 ) pack_open_fds++; - pack->next = packed_git; - packed_git = pack; + pack->next =get_packed_git (); +set_packed_git (pack); }static void prepare_packed_git_one (char *objdir,int local)
@@ -1026,7 +1037,7 @@ static void prepare_packed_git_one(char *objdir, int local)/* Don't reopen a pack we already have. */ strcpy (path + len, de->d_name); -for (p = packed_git; p; p = p->next) { +for (p =get_packed_git (); p; p = p->next) {if (!memcmp (path, p->pack_name, len + namelen -4 ))break ; }
@@ -1076,14 +1087,14 @@ static void rearrange_packed_git(void)struct packed_git **ary, *p;int i, n; -for (n =0 , p = packed_git; p; p = p->next) +for (n =0 , p =get_packed_git (); p; p = p->next) n++;if (n <2 )return ;/* prepare an array of packed_git for easier sorting */ ary =xcalloc (n,sizeof (struct packed_git *)); -for (n =0 , p = packed_git; p; p = p->next) +for (n =0 , p =get_packed_git (); p; p = p->next) ary[n++] = p;qsort (ary, n,sizeof (struct packed_git *), sort_pack);
@@ -1092,7 +1103,7 @@ static void rearrange_packed_git(void)for (i =0 ; i < n -1 ; i++) ary[i]->next = ary[i +1 ]; ary[n -1 ]->next =NULL ; - packed_git = ary[0 ]; +set_packed_git (ary[0 ]);free (ary); }
@@ -1139,7 +1150,7 @@ static const struct packed_git *has_packed_and_bad(const unsigned char *sha1)struct packed_git *p;unsigned i; -for (p = packed_git; p; p = p->next) +for (p =get_packed_git (); p; p = p->next)for (i =0 ; i < p->num_bad_objects; i++)if (!hashcmp (sha1, p->bad_object_sha1 +20 * i))return p;
@@ -2058,13 +2069,13 @@ static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)struct packed_git *p;prepare_packed_git (); -if (!packed_git) +if (!get_packed_git ())return 0 ;if (last_found_pack &&fill_pack_entry (sha1, e, last_found_pack))return 1 ; -for (p = packed_git; p; p = p->next) { +for (p =get_packed_git (); p; p = p->next) {if (p == last_found_pack || !fill_pack_entry (sha1, e, p))continue ;
sha1_name.c +1 -1
@@ -78,7 +78,7 @@ static int find_short_packed_object(int len, const unsigned char *match, unsigneint found =0 ;prepare_packed_git (); -for (p = packed_git; p && found <2 ; p = p->next) { +for (p =get_packed_git (); p && found <2 ; p = p->next) {uint32_t num, last;uint32_t first =0 ;open_pack_index (p);
Signature · 1 line
1.7.8.36.g69ee2
PATCH 2/5 sha1_file: stuff various pack reading variables into a struct
In reply to Nguyễn Thái Ngọc DuyThis requires any threads (including the main one) to call init_pack_context() first. All pack data is per thread.
Original patch is written by Thomas Rast <[email protected]>
--- cache.h | 1 + git.c | 1 + sha1_file.c | 251 +++++++++++++++++++++++++++++++++++------------------------ 3 files changed, 152 insertions(+), 101 deletions(-)
cache.h +1 -0
@@ -997,6 +997,7 @@ struct packed_git {extern struct packed_git *get_packed_git (void );extern void set_packed_git (struct packed_git *); +extern void init_pack_context (void );struct pack_entry {off_t offset;
git.c +1 -0
@@ -533,6 +533,7 @@ int main(int argc, const char **argv)const char *cmd; startup_info = &git_startup_info; +init_pack_context (); cmd =git_extract_argv0_path (argv[0 ]);if (!cmd)
sha1_file.c +150 -101
525 lines not on this page · raw message
Signature · 1 line
1.7.8.36.g69ee2
PATCH 3/5 Make lookup_*() functions thread-safe
In reply to Nguyễn Thái Ngọc Duy--- blob.c | 11 +++++++++-- commit.c | 12 +++++++++--- list-objects.c | 2 ++ object.c | 9 ++++++++- object.h | 6 ++++++ tag.c | 9 +++++++-- tree.c | 18 ++++++++++++++---- 7 files changed, 55 insertions(+), 12 deletions(-)
blob.c +9 -2
@@ -6,8 +6,13 @@ const char *blob_type = "blob";struct blob *lookup_blob (const unsigned char *sha1) {struct object *obj =lookup_object (sha1); -if (!obj) -return create_object (sha1,OBJ_BLOB ,alloc_blob_node ()); +LOCK (); +if (!obj) { + obj =create_object (sha1,OBJ_BLOB ,alloc_blob_node ()); +UNLOCK (); +return obj; + } +UNLOCK ();if (!obj->type) obj->type =OBJ_BLOB ;if (obj->type !=OBJ_BLOB ) {
@@ -20,6 +25,8 @@ struct blob *lookup_blob(const unsigned char *sha1)int parse_blob_buffer (struct blob *item,void *buffer,unsigned long size) { +LOCK (); item->object.parsed =1 ; +UNLOCK ();return 0 ; }
commit.c +9 -3
@@ -54,9 +54,15 @@ struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_nstruct commit *lookup_commit (const unsigned char *sha1) { -struct object *obj =lookup_object (sha1); -if (!obj) -return create_object (sha1,OBJ_COMMIT ,alloc_commit_node ()); +struct object *obj; +LOCK (); + obj =lookup_object (sha1); +if (!obj) { + obj =create_object (sha1,OBJ_COMMIT ,alloc_commit_node ()); +UNLOCK (); +return (struct commit *)obj; + } +UNLOCK ();if (!obj->type) obj->type =OBJ_COMMIT ;return check_commit (obj, sha1,0 );
list-objects.c +2 -0
@@ -123,8 +123,10 @@ static void process_tree(struct rev_info *revs, cb_data); }strbuf_setlen (base, baselen); +LOCK ();free (tree->buffer); tree->buffer =NULL ; +UNLOCK (); } static void mark_edge_parents_uninteresting(struct commit *commit,
object.c +8 -1
@@ -8,6 +8,9 @@static struct object **obj_hash;static int nr_objs, obj_hash_size; +int multithread_object_access; +pthread_mutex_t mtx; +unsigned int get_max_object_index (void ) {return obj_hash_size;
@@ -126,9 +129,13 @@ void *create_object(const unsigned char *sha1, int type, void *o)struct object *lookup_unknown_object (const unsigned char *sha1) { -struct object *obj =lookup_object (sha1); +struct object *obj; + +LOCK (); + obj =lookup_object (sha1);if (!obj) obj =create_object (sha1,OBJ_NONE ,alloc_object_node ()); +UNLOCK ();return obj; }
object.h +6 -0
@@ -32,6 +32,12 @@ struct object {unsigned char sha1[20 ]; }; +extern int multithread_object_access; +extern pthread_mutex_t mtx; + +#define LOCK () { if (multithread_object_access) pthread_mutex_lock(&mtx); } +#define UNLOCK () { if (multithread_object_access) pthread_mutex_unlock(&mtx); } +extern const char *typename (unsigned int type);extern int type_from_string (const char *str);
tag.c +7 -2
@@ -39,8 +39,13 @@ struct object *deref_tag_noverify(struct object *o)struct tag *lookup_tag (const unsigned char *sha1) {struct object *obj =lookup_object (sha1); -if (!obj) -return create_object (sha1,OBJ_TAG ,alloc_tag_node ()); +LOCK (); +if (!obj) { + obj =create_object (sha1,OBJ_TAG ,alloc_tag_node ()); +UNLOCK (); +return obj; + } +UNLOCK ();if (!obj->type) obj->type =OBJ_TAG ;if (obj->type !=OBJ_TAG ) {
tree.c +14 -4
@@ -180,9 +180,15 @@ int read_tree(struct tree *tree, int stage, struct pathspec *match)struct tree *lookup_tree (const unsigned char *sha1) { -struct object *obj =lookup_object (sha1); -if (!obj) -return create_object (sha1,OBJ_TREE ,alloc_tree_node ()); +struct object *obj; +LOCK (); + obj =lookup_object (sha1); +if (!obj) { + obj =create_object (sha1,OBJ_TREE ,alloc_tree_node ()); +UNLOCK (); +return obj; + } +UNLOCK ();if (!obj->type) obj->type =OBJ_TREE ;if (obj->type !=OBJ_TREE ) {
@@ -195,11 +201,15 @@ struct tree *lookup_tree(const unsigned char *sha1)int parse_tree_buffer (struct tree *item,void *buffer,unsigned long size) { -if (item->object.parsed) +LOCK (); +if (item->object.parsed) { +UNLOCK ();return 0 ; + } item->object.parsed =1 ; item->buffer = buffer; item->size = size; +UNLOCK ();return 0 ; }
Signature · 1 line
1.7.8.36.g69ee2
PATCH 4/5 Teach traverse_commit_list callsites about new parameter, nr_threads
In reply to Nguyễn Thái Ngọc Duy--- builtin/pack-objects.c | 2 +- builtin/rev-list.c | 4 ++-- list-objects.c | 2 +- list-objects.h | 2 +- upload-pack.c | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-)
builtin/pack-objects.c +1 -1
@@ -2292,7 +2292,7 @@ static void get_object_list(int ac, const char **av)if (prepare_revision_walk (&revs))die ("revision walk setup failed" );mark_edges_uninteresting (revs.commits, &revs, show_edge); -traverse_commit_list (&revs, show_commit, show_object,NULL ); +traverse_commit_list (&revs, show_commit, show_object,NULL ,0 );if (keep_unreachable)add_objects_in_unpacked_packs (&revs);
builtin/rev-list.c +2 -2
@@ -290,7 +290,7 @@ static int show_bisect_vars(struct rev_list_info *info, int reaches, int all)strcpy (hex,sha1_to_hex (revs->commits->item->object.sha1));if (flags &BISECT_SHOW_ALL ) { -traverse_commit_list (revs, show_commit, show_object, info); +traverse_commit_list (revs, show_commit, show_object, info,0 );printf ("------\n" ); }
@@ -395,7 +395,7 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)return show_bisect_vars (&info, reaches, all); } -traverse_commit_list (&revs, show_commit, show_object, &info); +traverse_commit_list (&revs, show_commit, show_object, &info,0 );if (revs.count) {if (revs.left_right && revs.cherry_mark)
list-objects.c +1 -1
@@ -170,7 +170,7 @@ static void add_pending_tree(struct rev_info *revs, struct tree *tree)void traverse_commit_list (struct rev_info *revs,show_commit_fn show_commit,show_object_fn show_object, -void *data) +void *data,int nr_threads_) {int i;struct commit *commit;
list-objects.h +1 -1
@@ -3,7 +3,7 @@typedef void (*show_commit_fn )(struct commit *,void *);typedef void (*show_object_fn )(struct object *,const struct name_path *,const char *,void *); -void traverse_commit_list (struct rev_info *,show_commit_fn ,show_object_fn ,void *); +void traverse_commit_list (struct rev_info *,show_commit_fn ,show_object_fn ,void *,int );typedef void (*show_edge_fn )(struct commit *);void mark_edges_uninteresting (struct commit_list *,struct rev_info *,show_edge_fn );
upload-pack.c +1 -1
@@ -128,7 +128,7 @@ static int do_rev_list(int in, int out, void *user_data)for (i =0 ; i < extra_edge_obj.nr; i++)fprintf (pack_pipe,"-%s\n" ,sha1_to_hex ( extra_edge_obj.objects[i].item->sha1)); -traverse_commit_list (&revs, show_commit, show_object,NULL ); +traverse_commit_list (&revs, show_commit, show_object,NULL ,0 );fflush (pack_pipe);fclose (pack_pipe);return 0 ;
Signature · 1 line
1.7.8.36.g69ee2
PATCH 5/5 Support multithread in traverse_commit_list and rev-list
In reply to Nguyễn Thái Ngọc Duy--- builtin/rev-list.c | 3 +- list-objects.c | 153 +++++++++++++++++++++++++++++++++++++++++++++++++-- revision.c | 16 ++++++ revision.h | 2 + 4 files changed, 167 insertions(+), 7 deletions(-)
builtin/rev-list.c +2 -1
@@ -395,7 +395,8 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)return show_bisect_vars (&info, reaches, all); } -traverse_commit_list (&revs, show_commit, show_object, &info,0 ); +traverse_commit_list (&revs, show_commit, show_object, &info, +getenv ("REV_LIST" ) ?2 :0 );if (revs.count) {if (revs.left_right && revs.cherry_mark)
list-objects.c +147 -6
207 lines not on this page · raw message
revision.c +16 -0
@@ -40,6 +40,22 @@ char *path_name(const struct name_path *path, const char *name)return n; } +struct name_path *dup_name_path (const struct name_path *path) +{ +const struct name_path *src; +struct name_path *dup, *dst; +int len; +for (len =0 , src = path; src; src = src->up) + len++; + dup =xmalloc (sizeof (*dup) * len); +for (src = path, dst = dup; src; src = src->up, dst++) { +memcpy (dst, src,sizeof (*src)); +if (dst > dup) + dst[-1 ].up = dst; + } +return dup; +} +static int show_path_component_truncated (FILE *out,const char *name,int len) {int cnt;
revision.h +2 -0
@@ -208,6 +208,8 @@ struct name_path {char *path_name (const struct name_path *path,const char *name); +struct name_path *dup_name_path (const struct name_path *); +extern void show_object_with_name (FILE *,struct object *,const struct name_path *,const char *); extern void add_object(struct object *obj,
Signature · 1 line
1.7.8.36.g69ee2
Quoted 5 lines · Nguyễn Thái Ngọc Duy
While this series does not break rev-list (but breaks all external commands, I don't care anyway), and it proves lock-free pack access works, it does not improve rev-list performance. I suspect extensive locking around "struct object *" may be the culprit.
Or if it is memory contention and your threads share the same cache, it won't help much either,
-Martin
Signature · 2 lines
Employee of Qualcomm Innovation Center, Inc. which is a member of Code Aurora Forum