Skip to contentSkip to conversation

attr: convert to new threadsafe API

PATCH Superseded Stefan Beller 4 participants 16 messages Oct 12, 2016 Documentation

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 attr: convert to new threadsafe API

This revamps the API of the attr subsystem to be thread safe. Before we had the question and its results in one struct type. The typical usage of the API was

    static struct git_attr_check check;

    if (!check)
        check = git_attr_check_initl("text", NULL);

    git_check_attr(path, check);
    act_on(check->value[0]);

This has a couple of issues when it comes to thread safety:

* the initialization is racy in this implementation. To make it thread safe, we need to acquire a mutex, such that only one thread is executing the code in git_attr_check_initl. As we do not want to introduce a mutex at each call site, this is best done in the attr code. However to do so, we need to have access to the check variable, i.e. the API has to look like

    git_attr_check_initl(struct git_attr_check*, ...);

Then one of the threads calling git_attr_check_initl will acquire the mutex and init the check, while all other threads will wait on the mutex just to realize they're late to the party and they'll return with no work done.

* While the check for attributes to be questioned only need to be initalized once as that part will be read only after its initialisation, the answer may be different for each path. Because of that we need to decouple the check and the answer, such that each thread can obtain an answer for the path it is currently processing.

This commit changes the API and adds locking in git_attr_check_initl that provides the thread safety.

---

I think this patch is the most interesting patch, so I'll refrain from resending the other 27 patches, though I have adressed the review comments locally. I'll resend everything once we are in agreement for this one.

This version adds the actual thread safety,
that is promised in the documentation, however it doesn't add any optimization, i.e. it's a single global lock. But as we do not expect contention, that is fine.

Also there is no example of how to use the thread safe API for asking questions from multiple threads.

Thanks,
Stefan

 Documentation/technical/api-gitattributes.txt |  93 +++++++++++++------
 archive.c                                     |  14 ++-
 attr.c                                        | 128 +++++++++++++++++---------
 attr.h                                        |  80 ++++++++++------
 builtin/check-attr.c                          |  30 +++---
 builtin/pack-objects.c                        |  12 ++-
 convert.c                                     |  35 +++----
 ll-merge.c                                    |  27 ++++--
 userdiff.c                                    |  19 ++--
 ws.c                                          |  15 +--
 10 files changed, 292 insertions(+), 161 deletions(-)
Documentation/technical/api-gitattributes.txt +64 -29
index 92fc32a..ac97244 100644
@@ -8,6 +8,18 @@ attributes to set of paths.
 Data Structure
 --------------
 
+extern struct git_attr *git_attr(const char *);
+
+/*
+ * Return the name of the attribute represented by the argument.  The
+ * return value is a pointer to a null-delimited string that is part
+ * of the internal data structure; it should not be modified or freed.
+ */
+extern const char *git_attr_name(const struct git_attr *);
+
+extern int attr_name_valid(const char *name, size_t namelen);
+extern void invalid_attr_name_message(struct strbuf *, const char *, int);
+
 `struct git_attr`::
 
 	An attribute is an opaque object that is identified by its name.
@@ -16,15 +28,17 @@ Data Structure
 	of no interest to the calling programs.  The name of the
 	attribute can be retrieved by calling `git_attr_name()`.
 
-`struct git_attr_check_elem`::
-
-	This structure represents one attribute and its value.
-
 `struct git_attr_check`::
 
-	This structure represents a collection of `git_attr_check_elem`.
+	This structure represents a collection of `struct git_attrs`.
 	It is passed to `git_check_attr()` function, specifying the
-	attributes to check, and receives their values.
+	attributes to check, and receives their values into a corresponding
+	`struct git_attr_result`.
+
+`struct git_attr_result`::
+
+	This structure represents a collection of results to its
+	corresponding `struct git_attr_check`, that has the same order.
 
 
 Attribute Values
@@ -53,19 +67,30 @@ value of the attribute for the path.
 Querying Specific Attributes
 ----------------------------
 
-* Prepare `struct git_attr_check` using git_attr_check_initl()
+* Prepare a `struct git_attr_check` using `git_attr_check_initl()`
   function, enumerating the names of attributes whose values you are
   interested in, terminated with a NULL pointer.  Alternatively, an
-  empty `struct git_attr_check` can be prepared by calling
-  `git_attr_check_alloc()` function and then attributes you want to
-  ask about can be added to it with `git_attr_check_append()`
-  function.
-
-* Call `git_check_attr()` to check the attributes for the path.
-
-* Inspect `git_attr_check` structure to see how each of the
-  attribute in the array is defined for the path.
-
+  empty `struct git_attr_check` as allocated by git_attr_check_alloc()
+  can be prepared by calling `git_attr_check_alloc()` function and
+  then attributes you want to ask about can be added to it with
+  `git_attr_check_append()` function.
+  `git_attr_check_initl()` is thread safe, i.e. you can call it
+  from different threads at the same time; when check determines
+  the initialzisation is still needed, the threads will use a
+  single global mutex to perform the initialization just once, the
+  others will wait on the the thread to actually perform the
+  initialization.
+
+* Prepare a `struct git_attr_result` using `git_attr_result_init()`
+  for the check struct. The call to initialize the result is not thread
+  safe, because different threads need their own thread local result
+  anyway.
+
+* Call `git_check_attr()` to check the attributes for the path,
+  the returned `git_attr_result` contains the result.
+
+* Inspect the returned `git_attr_result` structure to see how
+  each of the attribute in the array is defined for the path.
 
 Example
 -------
@@ -89,15 +114,20 @@ static void setup_check(void)
 
 ------------
 	const char *path;
+	struct git_attr_result *result;
 
 	setup_check();
-	git_check_attr(path, check);
+	result = git_check_attr(path, check);
 ------------
 
-. Act on `.value` member of the result, left in `check->check[]`:
+The `result` must not be free'd as it is owned by the attr subsystem.
+It is reused by the same thread, so a subsequent call to git_check_attr
+in the same thread will overwrite the result.
+
+. Act on `result->value[]`:
 
 ------------
-	const char *value = check->check[0].value;
+	const char *value = result->value[0];
 
 	if (ATTR_TRUE(value)) {
 		The attribute is Set, by listing only the name of the
@@ -123,6 +153,8 @@ the first step in the above would be different.
 static struct git_attr_check *check;
 static void setup_check(const char **argv)
 {
+	if (check)
+		return; /* already done */
 	check = git_attr_check_alloc();
 	while (*argv) {
 		struct git_attr *attr = git_attr(*argv);
@@ -138,17 +170,20 @@ Querying All Attributes
 
 To get the values of all attributes associated with a file:
 
-* Prepare an empty `git_attr_check` structure by calling
-  `git_attr_check_alloc()`.
+* Setup a local variables on the stack for both the question
+  `struct git_attr_check` as well as the result `struct git_attr_result`.
+  Zero them out via their respective _INIT macro.
 
-* Call `git_all_attrs()`, which populates the `git_attr_check`
-  with the attributes attached to the path.
+* Call `git_all_attrs()`
 
-* Iterate over the `git_attr_check.check[]` array to examine
-  the attribute names and values.  The name of the attribute
-  described by a  `git_attr_check.check[]` object can be retrieved via
-  `git_attr_name(check->check[i].attr)`.  (Please note that no items
+* Iterate over the `git_attr_check.attr[]` array to examine the
+  attribute names.  The name of the attribute described by a
+  `git_attr_check.attr[]` object can be retrieved via
+  `git_attr_name(check->attr[i])`.  (Please note that no items
   will be returned for unset attributes, so `ATTR_UNSET()` will return
   false for all returned `git_array_check` objects.)
+  The respective value for an attribute can be found in the same
+  index position in of `git_attr_result`.
 
-* Free the `git_array_check` by calling `git_attr_check_free()`.
+* Clear the local variables by calling `git_attr_check_clear()` and
+  `git_attr_result_clear()`.
archive.c +9 -5
index 11e3951..ae98df2 100644
@@ -111,6 +111,7 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,
 	struct archiver_args *args = c->args;
 	write_archive_entry_fn_t write_entry = c->write_entry;
 	static struct git_attr_check *check;
+	static struct git_attr_result result;
 	const char *path_without_prefix;
 	int err;
 
@@ -124,12 +125,15 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,
 		strbuf_addch(&path, '/');
 	path_without_prefix = path.buf + args->baselen;
 
-	if (!check)
-		check = git_attr_check_initl("export-ignore", "export-subst", NULL);
-	if (!git_check_attr(path_without_prefix, check)) {
-		if (ATTR_TRUE(check->check[0].value))
+	if (!check) {
+		git_attr_check_initl(&check, "export-ignore", "export-subst", NULL);
+		git_attr_result_init(&result, check);
+	}
+
+	if (!git_check_attr(path_without_prefix, check, &result)) {
+		if (ATTR_TRUE(result.value[0]))
 			return 0;
-		args->convert = ATTR_TRUE(check->check[1].value);
+		args->convert = ATTR_TRUE(result.value[1]);
 	}
 
 	if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
attr.c +87 -41

246 lines not on this page · raw message

attr.h +54 -26

104 lines not on this page · raw message

builtin/check-attr.c +19 -11
index ec61476..a3df756 100644
@@ -24,13 +24,17 @@ static const struct option check_attr_options[] = {
 	OPT_END()
 };
 
-static void output_attr(struct git_attr_check *check, const char *file)
+static void output_attr(struct git_attr_check *check,
+			struct git_attr_result *result, const char *file)
 {
 	int j;
 	int cnt = check->check_nr;
 
+	if (check->check_nr != result->check_nr)
+		die("BUG: confused check and result internally");
+
 	for (j = 0; j < cnt; j++) {
-		const char *value = check->check[j].value;
+		const char *value = result->value[j];
 
 		if (ATTR_TRUE(value))
 			value = "set";
@@ -44,11 +48,11 @@ static void output_attr(struct git_attr_check *check, const char *file)
 			       "%s%c" /* attrname */
 			       "%s%c" /* attrvalue */,
 			       file, 0,
-			       git_attr_name(check->check[j].attr), 0, value, 0);
+			       git_attr_name(check->attr[j]), 0, value, 0);
 		} else {
 			quote_c_style(file, NULL, stdout, 0);
 			printf(": %s: %s\n",
-			       git_attr_name(check->check[j].attr), value);
+			       git_attr_name(check->attr[j]), value);
 		}
 	}
 }
@@ -60,14 +64,18 @@ static void check_attr(const char *prefix,
 	char *full_path =
 		prefix_path(prefix, prefix ? strlen(prefix) : 0, file);
 	if (check != NULL) {
-		if (git_check_attr(full_path, check))
-			die("git_check_attr died");
-		output_attr(check, file);
+		struct git_attr_result result = GIT_ATTR_RESULT_INIT;
+		git_attr_result_init(&result, check);
+		git_check_attr(full_path, check, &result);
+		output_attr(check, &result, file);
+		git_attr_result_clear(&result);
 	} else {
-		check = git_attr_check_alloc();
-		git_all_attrs(full_path, check);
-		output_attr(check, file);
-		git_attr_check_free(check);
+		struct git_attr_check check = GIT_ATTR_CHECK_INIT;
+		struct git_attr_result result = GIT_ATTR_RESULT_INIT;
+		git_all_attrs(full_path, &check, &result);
+		output_attr(&check, &result, file);
+		git_attr_result_clear(&result);
+		git_attr_check_clear(&check);
 	}
 	free(full_path);
 }
builtin/pack-objects.c +8 -4
index 3918c07..962c87b 100644
@@ -899,12 +899,16 @@ static void write_pack_file(void)
 static int no_try_delta(const char *path)
 {
 	static struct git_attr_check *check;
+	static struct git_attr_result result;
 
-	if (!check)
-		check = git_attr_check_initl("delta", NULL);
-	if (git_check_attr(path, check))
+	if (!check) {
+		git_attr_check_initl(&check, "delta", NULL);
+		git_attr_result_init(&result, check);
+	}
+
+	if (git_check_attr(path, check, &result))
 		return 0;
-	if (ATTR_FALSE(check->check[0].value))
+	if (ATTR_FALSE(result.value[0]))
 		return 1;
 	return 0;
 }
convert.c +14 -21
index bb2435a..dd8d25b 100644
@@ -718,10 +718,8 @@ static int ident_to_worktree(const char *path, const char *src, size_t len,
 	return 1;
 }
 
-static enum crlf_action git_path_check_crlf(struct git_attr_check_elem *check)
+static enum crlf_action git_path_check_crlf(const char *value)
 {
-	const char *value = check->value;
-
 	if (ATTR_TRUE(value))
 		return CRLF_TEXT;
 	else if (ATTR_FALSE(value))
@@ -735,10 +733,8 @@ static enum crlf_action git_path_check_crlf(struct git_attr_check_elem *check)
 	return CRLF_UNDEFINED;
 }
 
-static enum eol git_path_check_eol(struct git_attr_check_elem *check)
+static enum eol git_path_check_eol(const char *value)
 {
-	const char *value = check->value;
-
 	if (ATTR_UNSET(value))
 		;
 	else if (!strcmp(value, "lf"))
@@ -748,9 +744,8 @@ static enum eol git_path_check_eol(struct git_attr_check_elem *check)
 	return EOL_UNSET;
 }
 
-static struct convert_driver *git_path_check_convert(struct git_attr_check_elem *check)
+static struct convert_driver *git_path_check_convert(const char *value)
 {
-	const char *value = check->value;
 	struct convert_driver *drv;
 
 	if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
@@ -761,10 +756,8 @@ static struct convert_driver *git_path_check_convert(struct git_attr_check_elem
 	return NULL;
 }
 
-static int git_path_check_ident(struct git_attr_check_elem *check)
+static int git_path_check_ident(const char *value)
 {
-	const char *value = check->value;
-
 	return !!ATTR_TRUE(value);
 }
 
@@ -778,25 +771,25 @@ struct conv_attrs {
 static void convert_attrs(struct conv_attrs *ca, const char *path)
 {
 	static struct git_attr_check *check;
+	static struct git_attr_result result;
 
 	if (!check) {
-		check = git_attr_check_initl("crlf", "ident",
-					     "filter", "eol", "text",
-					     NULL);
+		git_attr_check_initl(&check, "crlf", "ident", "filter",
+				     "eol", "text", NULL);
+		git_attr_result_init(&result, check);
 		user_convert_tail = &user_convert;
 		git_config(read_convert_config, NULL);
 	}
 
-	if (!git_check_attr(path, check)) {
-		struct git_attr_check_elem *ccheck = check->check;
-		ca->crlf_action = git_path_check_crlf(ccheck + 4);
+	if (!git_check_attr(path, check, &result)) {
+		ca->crlf_action = git_path_check_crlf(result.value[4]);
 		if (ca->crlf_action == CRLF_UNDEFINED)
-			ca->crlf_action = git_path_check_crlf(ccheck + 0);
+			ca->crlf_action = git_path_check_crlf(result.value[0]);
 		ca->attr_action = ca->crlf_action;
-		ca->ident = git_path_check_ident(ccheck + 1);
-		ca->drv = git_path_check_convert(ccheck + 2);
+		ca->ident = git_path_check_ident(result.value[1]);
+		ca->drv = git_path_check_convert(result.value[2]);
 		if (ca->crlf_action != CRLF_BINARY) {
-			enum eol eol_attr = git_path_check_eol(ccheck + 3);
+			enum eol eol_attr = git_path_check_eol(result.value[3]);
 			if (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_LF)
 				ca->crlf_action = CRLF_AUTO_INPUT;
 			else if (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_CRLF)
ll-merge.c +17 -10
index bc6479c..086f5c7 100644
@@ -355,6 +355,7 @@ int ll_merge(mmbuffer_t *result_buf,
 {
 	static struct git_attr_check *check;
 	static const struct ll_merge_options default_opts;
+	static struct git_attr_result result;
 	const char *ll_driver_name = NULL;
 	int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
 	const struct ll_merge_driver *driver;
@@ -368,13 +369,15 @@ int ll_merge(mmbuffer_t *result_buf,
 		normalize_file(theirs, path);
 	}
 
-	if (!check)
-		check = git_attr_check_initl("merge", "conflict-marker-size", NULL);
+	if (!check) {
+		git_attr_check_initl(&check, "merge", "conflict-marker-size", NULL);
+		git_attr_result_init(&result, check);
+	}
 
-	if (!git_check_attr(path, check)) {
-		ll_driver_name = check->check[0].value;
-		if (check->check[1].value) {
-			marker_size = atoi(check->check[1].value);
+	if (!git_check_attr(path, check, &result)) {
+		ll_driver_name = result.value[0];
+		if (result.value[1]) {
+			marker_size = atoi(result.value[1]);
 			if (marker_size <= 0)
 				marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
 		}
@@ -394,12 +397,16 @@ int ll_merge(mmbuffer_t *result_buf,
 int ll_merge_marker_size(const char *path)
 {
 	static struct git_attr_check *check;
+	static struct git_attr_result result;
 	int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
 
-	if (!check)
-		check = git_attr_check_initl("conflict-marker-size", NULL);
-	if (!git_check_attr(path, check) && check->check[0].value) {
-		marker_size = atoi(check->check[0].value);
+	if (!check) {
+		git_attr_check_initl(&check, "conflict-marker-size", NULL);
+		git_attr_result_init(&result, check);
+	}
+
+	if (!git_check_attr(path, check, &result) && result.value[0]) {
+		marker_size = atoi(result.value[0]);
 		if (marker_size <= 0)
 			marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
 	}
userdiff.c +12 -7
index 46dfd32..e88568a 100644
@@ -263,21 +263,26 @@ struct userdiff_driver *userdiff_find_by_name(const char *name) {
 struct userdiff_driver *userdiff_find_by_path(const char *path)
 {
 	static struct git_attr_check *check;
+	static struct git_attr_result result;
+
+	if (!check) {
+		git_attr_check_initl(&check, "diff", NULL);
+		git_attr_result_init(&result, check);
+	}
 
-	if (!check)
-		check = git_attr_check_initl("diff", NULL);
 	if (!path)
 		return NULL;
-	if (git_check_attr(path, check))
+
+	if (git_check_attr(path, check, &result))
 		return NULL;
 
-	if (ATTR_TRUE(check->check[0].value))
+	if (ATTR_TRUE(result.value[0]))
 		return &driver_true;
-	if (ATTR_FALSE(check->check[0].value))
+	if (ATTR_FALSE(result.value[0]))
 		return &driver_false;
-	if (ATTR_UNSET(check->check[0].value))
+	if (ATTR_UNSET(result.value[0]))
 		return NULL;
-	return userdiff_find_by_name(check->check[0].value);
+	return userdiff_find_by_name(result.value[0]);
 }
 
 struct userdiff_driver *userdiff_get_textconv(struct userdiff_driver *driver)
ws.c +8 -7
index bb3270c..8bdbbf7 100644
@@ -73,15 +73,16 @@ unsigned parse_whitespace_rule(const char *string)
 
 unsigned whitespace_rule(const char *pathname)
 {
-	static struct git_attr_check *attr_whitespace_rule;
+	static struct git_attr_check *check;
+	static struct git_attr_result result;
 
-	if (!attr_whitespace_rule)
-		attr_whitespace_rule = git_attr_check_initl("whitespace", NULL);
-
-	if (!git_check_attr(pathname, attr_whitespace_rule)) {
-		const char *value;
+	if (!check) {
+		git_attr_check_initl(&check, "whitespace", NULL);
+		git_attr_result_init(&result, check);
+	}
 
-		value = attr_whitespace_rule->check[0].value;
+	if (!git_check_attr(pathname, check, &result)) {
+		const char *value = result.value[0];
 		if (ATTR_TRUE(value)) {
 			/* true (whitespace) */
 			unsigned all_rule = ws_tab_width(whitespace_rule_cfg);
Signature · 1 line
2.10.1.432.g8a36cd8.dirty
#1 lore raw
Quoted 3 lines · Stefan Beller

I think this patch is the most interesting patch, so I'll refrain from resending the other 27 patches, though I have adressed the review comments locally. I'll resend everything once we are in agreement for this one.

What is the primary purpose of this patch? Is it to prepare callers so that the way they interact with the attr subsystem will not have to change when they become threaded and the attr subsystem becomes thread ready?

I am not sure if the updates to the callers fulfill that purpose. For example, look at this hunk.

archive.c static int write_archive_entry(const unsigned char *sha1, const char *base, · line 114

Quoted 5 lines
archive.c +1 -0
@@ -111,6 +111,7 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,
 	struct archiver_args *args = c->args;
 	write_archive_entry_fn_t write_entry = c->write_entry;
 	static struct git_attr_check *check;
+	static struct git_attr_result result;

As we discussed, this caller, even when threaded, will always want to ask for a fixed two attributes, so "check" being static and shared across threads is perfectly fine. But we do not want to see "result" shared, do we?

Quoted 5 lines
archive.c +0 -0
 	const char *path_without_prefix;
 	int err;
 
@@ -124,12 +125,15 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,
 		strbuf_addch(&path, '/');

archive.c static int write_archive_entry(const unsigned char *sha1, const char *base, · line 131

Quoted 10 lines
archive.c +4 -4
 	path_without_prefix = path.buf + args->baselen;
 
-	if (!check)
-		check = git_attr_check_initl("export-ignore", "export-subst", NULL);
-	if (!git_check_attr(path_without_prefix, check)) {
-		if (ATTR_TRUE(check->check[0].value))
+	if (!check) {
+		git_attr_check_initl(&check, "export-ignore", "export-subst", NULL);
+		git_attr_result_init(&result, check);
+	}

Are we assuming that storing and checking of a single pointer is atomic? I would not expose that assumption to the callers. On a platform where that assumption holds, "if check is not NULL, somebody must have done it already, so return without doing nothing" can be the first thing git_attr_check_initl()'s implementation does, though. Or it may not hold anywhere without some barriers. All that implementation details should be hidden inside _initl()'s implementation. So this caller should instead just do an unconditional:

	git_attr_check_initl(&check, "export-ignore", "export-subst", NULL);

Also, as "result" should be per running thread, hence non-static, and because we do not want repeated heap allocations and releases but luckily most callers _know_ not just how many but what exact attributes they are interested in (I think there are only two callers that do not know it; check-all-attrs one, and your pathspec magic one that does not exist at this point in the series), I would think it is much more preferrable to allow the caller to prepare an on-stack array and call it "initialized already".

In other words, ideally, I think this part of the patch should rather read like this:

	static struct git_attr_check *check;
	struct git_attr_result result[2];

	...
	git_attr_check_initl(&check, "export-ignore", "export-subst", NULL);
	if (!git_check_attr(path_without_prefix, check, result)) {
		... use result[0] and result[1] ...

For sanity checking, it is OK to add ARRAY_SIZE(result) as the final and extra parameter to git_check_attr() so that the function can make sure it matches (or exceeds) check->nr.

#2 lore raw
Quoted 14 lines · Junio C Hamano
Quoted 3 lines · Stefan Beller

I think this patch is the most interesting patch, so I'll refrain from resending the other 27 patches, though I have adressed the review comments locally. I'll resend everything once we are in agreement for this one.

What is the primary purpose of this patch? Is it to prepare callers so that the way they interact with the attr subsystem will not have to change when they become threaded and the attr subsystem becomes thread ready?

I am not sure if the updates to the callers fulfill that purpose. For example, look at this hunk.

Quoted 10 lines

archive.c static int write_archive_entry(const unsigned char *sha1, const char *base, · line 114

Quoted 5 lines
archive.c +1 -0
@@ -111,6 +111,7 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,
      struct archiver_args *args = c->args;
      write_archive_entry_fn_t write_entry = c->write_entry;
      static struct git_attr_check *check;
+     static struct git_attr_result result;

As we discussed, this caller, even when threaded, will always want to ask for a fixed two attributes, so "check" being static and shared across threads is perfectly fine. But we do not want to see "result" shared, do we?

Well all of the hunks in the patch are not threaded, so they don't follow a threading pattern, but the static pattern to not be more expensive than needed.

Quoted 49 lines
Quoted 6 lines
archive.c +0 -0
      const char *path_without_prefix;
      int err;
 
@@ -124,12 +125,15 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,
              strbuf_addch(&path, '/');

archive.c static int write_archive_entry(const unsigned char *sha1, const char *base, · line 131

Quoted 10 lines
archive.c +4 -4
      path_without_prefix = path.buf + args->baselen;
 
-     if (!check)
-             check = git_attr_check_initl("export-ignore", "export-subst", NULL);
-     if (!git_check_attr(path_without_prefix, check)) {
-             if (ATTR_TRUE(check->check[0].value))
+     if (!check) {
+             git_attr_check_initl(&check, "export-ignore", "export-subst", NULL);
+             git_attr_result_init(&result, check);
+     }

Are we assuming that storing and checking of a single pointer is atomic? I would not expose that assumption to the callers. On a platform where that assumption holds, "if check is not NULL, somebody must have done it already, so return without doing nothing" can be the first thing git_attr_check_initl()'s implementation does, though. Or it may not hold anywhere without some barriers. All that implementation details should be hidden inside _initl()'s implementation. So this caller should instead just do an unconditional:

        git_attr_check_initl(&check, "export-ignore", "export-subst", NULL);

Also, as "result" should be per running thread, hence non-static, and because we do not want repeated heap allocations and releases but luckily most callers _know_ not just how many but what exact attributes they are interested in (I think there are only two callers that do not know it; check-all-attrs one, and your pathspec magic one that does not exist at this point in the series), I would think it is much more preferrable to allow the caller to prepare an on-stack array and call it "initialized already".

In other words, ideally, I think this part of the patch should rather read like this:

        static struct git_attr_check *check;
        struct git_attr_result result[2];

        ...
        git_attr_check_initl(&check, "export-ignore", "export-subst", NULL);
        if (!git_check_attr(path_without_prefix, check, result)) {
                ... use result[0] and result[1] ...
Quoted 3 lines

For sanity checking, it is OK to add ARRAY_SIZE(result) as the final and extra parameter to git_check_attr() so that the function can make sure it matches (or exceeds) check->nr.

That seems tempting from a callers perspective; I'll look into that.

#3 lore raw
Quoted 14 lines · Stefan Beller
Quoted 13 lines

I am not sure if the updates to the callers fulfill that purpose. For example, look at this hunk.

archive.c static int write_archive_entry(const unsigned char *sha1, const char *base, · line 114

Quoted 5 lines
archive.c +1 -0
@@ -111,6 +111,7 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,
      struct archiver_args *args = c->args;
      write_archive_entry_fn_t write_entry = c->write_entry;
      static struct git_attr_check *check;
+     static struct git_attr_result result;

As we discussed, this caller, even when threaded, will always want to ask for a fixed two attributes, so "check" being static and shared across threads is perfectly fine. But we do not want to see "result" shared, do we?

Quoted 3 lines

Well all of the hunks in the patch are not threaded, so they don't follow a threading pattern, but the static pattern to not be more expensive than needed.

Is it too invasive a change to make them as if they are thread-ready users of API that happen to know their callers are not threading? It would be ideal if we can prepare them so that the way they interact with the attr subsystem will not have to change after this step.

Quoted 11 lines
Quoted 11 lines

In other words, ideally, I think this part of the patch should rather read like this:

        static struct git_attr_check *check;
        struct git_attr_result result[2];

        ...
        git_attr_check_initl(&check, "export-ignore", "export-subst", NULL);
        if (!git_check_attr(path_without_prefix, check, result)) {
                ... use result[0] and result[1] ...
Quoted 5 lines
Quoted 3 lines

For sanity checking, it is OK to add ARRAY_SIZE(result) as the final and extra parameter to git_check_attr() so that the function can make sure it matches (or exceeds) check->nr.

That seems tempting from a callers perspective; I'll look into that.

For callers that prepare "check" and "result" before asking check-attr about the attributes in "check" for many paths, it is OK to use your "allocate with attr_result_init()" pattern. The "result" still needs to be made non-static, though.

But many callers do not follow that; rather they do

	loop to iterate over paths {
		call a helper func to learn attr X for path
		use the value of attr X
	}

using a callchain that embeds a helper function deep inside, and "check" is kept in the helper, check-attr function is called from there, and "result" is not passed from the caller to the helper (obviously, because it does not exist in the current API). See the callchain that leads down to convert.c::convert_attrs() for a typical example. When converted to the new API, it needs to have a new "result" structure every time it is called, and cannot reuse the one that was used in its previous call.

#4 lore raw
Quoted 20 lines · Junio C Hamano
Quoted 14 lines · Stefan Beller
Quoted 13 lines

I am not sure if the updates to the callers fulfill that purpose. For example, look at this hunk.

archive.c static int write_archive_entry(const unsigned char *sha1, const char *base, · line 114

Quoted 5 lines
archive.c +1 -0
@@ -111,6 +111,7 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,
      struct archiver_args *args = c->args;
      write_archive_entry_fn_t write_entry = c->write_entry;
      static struct git_attr_check *check;
+     static struct git_attr_result result;

As we discussed, this caller, even when threaded, will always want to ask for a fixed two attributes, so "check" being static and shared across threads is perfectly fine. But we do not want to see "result" shared, do we?

Quoted 3 lines

Well all of the hunks in the patch are not threaded, so they don't follow a threading pattern, but the static pattern to not be more expensive than needed.

Quoted 5 lines

Is it too invasive a change to make them as if they are thread-ready users of API that happen to know their callers are not threading? It would be ideal if we can prepare them so that the way they interact with the attr subsystem will not have to change after this step.

As far as I see the future, we do not need to change those in the future, unless we add the threading to the current callers, which is usually a very invasive thing?

Quoted 16 lines
Quoted 16 lines
Quoted 15 lines

In other words, ideally, I think this part of the patch should rather read like this:

        static struct git_attr_check *check;
        struct git_attr_result result[2];

        ...
        git_attr_check_initl(&check, "export-ignore", "export-subst", NULL);
        if (!git_check_attr(path_without_prefix, check, result)) {
                ... use result[0] and result[1] ...

For sanity checking, it is OK to add ARRAY_SIZE(result) as the final and extra parameter to git_check_attr() so that the function can make sure it matches (or exceeds) check->nr.

Quoted 6 lines
Quoted 1 line

That seems tempting from a callers perspective; I'll look into that.

For callers that prepare "check" and "result" before asking check-attr about the attributes in "check" for many paths, it is OK to use your "allocate with attr_result_init()" pattern. The "result" still needs to be made non-static, though.

I do not see why we would want to have a non static result for non threaded callers.

Quoted 8 lines

But many callers do not follow that; rather they do

loop to iterate over paths {
call a helper func to learn attr X for path use the value of attr X
}

Quoted 8 lines

using a callchain that embeds a helper function deep inside, and "check" is kept in the helper, check-attr function is called from there, and "result" is not passed from the caller to the helper (obviously, because it does not exist in the current API). See the callchain that leads down to convert.c::convert_attrs() for a typical example. When converted to the new API, it needs to have a new "result" structure every time it is called, and cannot reuse the one that was used in its previous call.

Why would that be? i.e. I do not understand the reasoning/motivation as well as what you propose to change here. Do you think of the result being held at the top of the call chain (and there it may be just allocated on the stack) and passed down all the way to convert_attrs, that writes into that result?

Currently the static result in convert_attrs just works fine (as all tests pass) and that is not the place where we'd add threading to?

Threading would be added in dir.c, a new place that will use attrs.

Maybe instead of discussing the meaning of this patch further, I should rebase the "pathspecs can ask for attributes" patches to either experience the problems myself or at least have a series that has a meaningful endgoal. I thought this preparatory series may be better than sending an even larger series.

Thanks,
Stefan

#7 lore raw
In reply to Stefan Beller

Am 12.10.2016 um 01:59 schrieb Stefan Beller:

Quoted 14 lines
attr.c +9 -1
+void git_attr_check_initl(struct git_attr_check **check_,
+			  const char *one, ...)
 {
-	struct git_attr_check *check;
 	int cnt;
 	va_list params;
 	const char *param;
+	struct git_attr_check *check;
+
+	if (*check_)
+		return;
+
+	attr_lock();
+	if (*check_) {

attr.c void git_attr_set_direction(enum git_attr_direction new, struct index_state *ist

Quoted 10 lines
attr.c +6 -1
+		attr_unlock();
+		return;
+	}
...
 	check = xcalloc(1,
-			sizeof(*check) + cnt * sizeof(*(check->check)));
+			sizeof(*check) + cnt * sizeof(*(check->attr)));
...
+	*check_ = check;
+	attr_unlock();

Sigh. DCLP, the Double Checked Locking Pattern. These days, it should be common knowledge among professionals that this naïve version _does_not_work_ [1]!

I suggest you go without it, then measure, and only *then* optimize if it is a bottleneck. Did I read "we do not expect much contention" somewhere?

[1] http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf C++ centric, but applies to C just as well

-- Hannes

#13 lore raw
In reply to Stefan Beller
Quoted 3 lines · Stefan Beller

This version adds the actual thread safety,
that is promised in the documentation, however it doesn't add any optimization, i.e. it's a single global lock. But as we do not expect contention, that is fine.

Because we have to start _somewhere_, I agree it is a good approach to first try the simplest implementation and then optimize later, but is it an agreed consensus that we do not expect contention?

#14 lore raw
Quoted 10 lines · Junio C Hamano
Quoted 3 lines · Stefan Beller

This version adds the actual thread safety,
that is promised in the documentation, however it doesn't add any optimization, i.e. it's a single global lock. But as we do not expect contention, that is fine.

Because we have to start _somewhere_, I agree it is a good approach to first try the simplest implementation and then optimize later, but is it an agreed consensus that we do not expect contention?

I agree on that. Did you mean this is obvious to the reader? I just wanted to state how I'll do it, such that if in the future someone digs up this code, they'll
know I did not look at performance in the first run.

#9 lore raw
In reply to Junio C Hamano
Quoted 6 lines · Junio C Hamano
Quoted 3 lines · Johannes Sixt

Sigh. DCLP, the Double Checked Locking Pattern. ...
I suggest you go without it, then measure, and only *then* optimize if it is a bottleneck.

Quoted 7 lines

That comes from me in earlier discussion before the patch, namely in <[email protected]>, where I wondered if a cheap check outside the lock may be a possible optimization opportunity, as this is a classic singleton that will not be deinitialized, and once the codepath gets exercised, we would be taking the "nothing to do" route 100% of the time.

Having followed that advice (and internally having a DCLP), I think we have Triple Checked Locking Pattern in this patch. Nobody wrote a paper on how that would not work, yet. ;)

In the reroll I plan to reduce it to a Single Checked (inside a mutex) Locking Pattern, though I would expect that performance (or lack thereof) will show then. But let's postpone measuring until we have a working patch.

#8 lore raw
In reply to Johannes Sixt
Quoted 3 lines · Johannes Sixt

Sigh. DCLP, the Double Checked Locking Pattern. ...
I suggest you go without it, then measure, and only *then* optimize if it is a bottleneck.

That comes from me in earlier discussion before the patch, namely in <[email protected]>, where I wondered if a cheap check outside the lock may be a possible optimization opportunity, as this is a classic singleton that will not be deinitialized, and once the codepath gets exercised, we would be taking the "nothing to do" route 100% of the time.

#15 lore raw
In reply to Stefan Beller
Quoted 7 lines · Stefan Beller
Quoted 6 lines · Junio C Hamano
Quoted 3 lines · Stefan Beller

This version adds the actual thread safety,
that is promised in the documentation, however it doesn't add any optimization, i.e. it's a single global lock. But as we do not expect contention, that is fine.

Quoted 5 lines
Quoted 3 lines

Because we have to start _somewhere_, I agree it is a good approach to first try the simplest implementation and then optimize later, but is it an agreed consensus that we do not expect contention?

I agree on that. Did you mean this is obvious to the reader?

I meant to say that "But as we do not expect" sounded like a justification for the approach based on an unwarranted assumption that is not even the list concensus. I do not think it is obvious to the reader that there is no need to worry about contention.

It all is outside the log message, so as long as readers understand what we meant from this discussion, that is OK.

#11 lore raw
In reply to Johannes Sixt
Quoted 5 lines · Johannes Sixt

Sigh. DCLP, the Double Checked Locking Pattern. These days, it should be common knowledge among professionals that this naïve version _does_not_work_ [1]!

Quoted 8 lines

I suggest you go without it, then measure, and only *then* optimize if it is a bottleneck. Did I read "we do not expect much contention" somewhere?

[1] http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf C++ centric, but applies to C just as well

-- Hannes

You know, I always wondered why Linux Kernel code needed memory barriers but userspace programs didn't seem to use them.. turns out they actually *do* need them for the same exact types of problems...

Thanks,
Jake

#5 lore raw
In reply to Stefan Beller
Quoted 10 lines · Stefan Beller
Quoted 9 lines
Quoted 3 lines

Well all of the hunks in the patch are not threaded, so they don't follow a threading pattern, but the static pattern to not be more expensive than needed.

Is it too invasive a change to make them as if they are thread-ready users of API that happen to know their callers are not threading? It would be ideal if we can prepare them so that the way they interact with the attr subsystem will not have to change after this step.

Quoted 3 lines

As far as I see the future, we do not need to change those in the future, unless we add the threading to the current callers, which is usually a very invasive thing?

It does not matter how invasive the thread set-up and teardown that happens in the callers.

I am talking about the part of _THIS_ code that you are updating, that interacts with attr API. The way they prepare "check" and "result", the way they ask questions by calling git_check_attr() function.

Think of a thread-safe library function (like malloc()). If you write

	func (...) {
		buf = malloc(20);
		...
		free(buf);
	}

in a function that happens to be only called in a non-threaded program today, you do not have to update these calls to malloc(3) and free(3) when you update the callchain to threadable, right?

That kind of thread-preparedness is what I am trying to see if we can achieve with this update.

#10 lore raw
In reply to Stefan Beller
Quoted 14 lines · Stefan Beller
Quoted 6 lines · Junio C Hamano
Quoted 3 lines · Johannes Sixt

Sigh. DCLP, the Double Checked Locking Pattern. ...
I suggest you go without it, then measure, and only *then* optimize if it is a bottleneck.

Quoted 6 lines

That comes from me in earlier discussion before the patch, namely in <[email protected]>, where I wondered if a cheap check outside the lock may be a possible optimization opportunity, as this is a classic singleton that will not be deinitialized, and once the codepath gets exercised, we would be taking the "nothing to do" route 100% of the time.

Quoted 7 lines

Having followed that advice (and internally having a DCLP), I think we have Triple Checked Locking Pattern in this patch. Nobody wrote a paper on how that would not work, yet. ;)

In the reroll I plan to reduce it to a Single Checked (inside a mutex) Locking Pattern, though I would expect that performance (or lack thereof) will show then. But let's postpone measuring until we have a working patch.

Oh, that wasn't even an "advice" (read it again). I fully agree that starting simple would be the way to go.

#12 lore raw
In reply to Jacob Keller
Quoted 16 lines · Jacob Keller
Quoted 5 lines · Johannes Sixt

Sigh. DCLP, the Double Checked Locking Pattern. These days, it should be common knowledge among professionals that this naïve version _does_not_work_ [1]!

Quoted 8 lines

I suggest you go without it, then measure, and only *then* optimize if it is a bottleneck. Did I read "we do not expect much contention" somewhere?

[1] http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf C++ centric, but applies to C just as well

-- Hannes

Quoted 6 lines

You know, I always wondered why Linux Kernel code needed memory barriers but userspace programs didn't seem to use them.. turns out they actually *do* need them for the same exact types of problems...

Thanks,
Jake

In a former job I made use of them, too. So I am kinda embarrassed. (I cannot claim I did not know about these patterns and memory fencing, it just escaped my consciousness).

#6 lore raw
In reply to Stefan Beller
Quoted 16 lines · Stefan Beller
Quoted 7 lines

But many callers do not follow that; rather they do

loop to iterate over paths {
call a helper func to learn attr X for path use the value of attr X
}

Quoted 8 lines

using a callchain that embeds a helper function deep inside, and "check" is kept in the helper, check-attr function is called from there, and "result" is not passed from the caller to the helper (obviously, because it does not exist in the current API). See the callchain that leads down to convert.c::convert_attrs() for a typical example. When converted to the new API, it needs to have a new "result" structure every time it is called, and cannot reuse the one that was used in its previous call.

Quoted 2 lines

Why would that be? i.e. I do not understand the reasoning/motivation as well as what you propose to change here.

The leaf function may look like

	check_eol(const char *path)
	{
                static struct git_attr_check *check;

		initl(&check, "eol", NULL);
		git_check_attr(&check, path, result);
		return nth_element_in_result(result, 0);
	}                

and we want "result" to be thread-ready.

A naive and undesired way to put it on stack is like so:

	const char *check_eol(const char *path)
	{
                static struct git_attr_check *check;
		struct git_attr_result result = RESULT_INIT;
		const char *eol;

		initl(&check, "eol", NULL);
		init_result(&check, &result);
		git_check_attr(&check, path, &result);
		eol = nth_element_in_result(&result, 0);
		clear_result(&result);
		return eol;
	}                

where your "struct git_attr_result" has a fixed size, and the actual result array is allocated via ALLOC_GROW() etc. That's overhead that we do not want. Instead can't we do this?

	const char *check_eol(const char *path)
	{
                static struct git_attr_check *check;
		/* we know we only want one */
		struct git_attr_result result[1];
		const char *eol;

		initl(&check, "eol", NULL);
		git_check_attr(&check, path, result);
		return result[0];
	}                

That way, we won't be doing ALLOC_GROW() in init_result() or free in clear_result().

If you use a structure that has pointer to an array (i.e. the "naive and undesired way" above), you cannot amortize the alloc/free by making result "static" if you want to be thread-ready.