git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] config.mak.dev: enable -Wunused-function
@ 2018-10-18  7:05 Jeff King
  2018-10-18  7:08 ` Jeff King
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Jeff King @ 2018-10-18  7:05 UTC (permalink / raw)
  To: git; +Cc: Nguyễn Thái Ngọc Duy

We explicitly omitted -Wunused-function when we added
-Wextra, but there is no need: the current code compiles
cleanly with it. And it's worth having, since it can let you
know when there are cascading effects from a cleanup (e.g.,
deleting one function lets you delete its static helpers).

There are cases where we may need an unused function to
exist, but we can handle these easily:

  - macro-generated code like commit-slab; there we have the
    MAYBE_UNUSED annotation to silence the compiler

  - conditional compilation, where we may or may not need a
    static helper. These generally fall into one of two
    categories:

      - the call should not be conditional, but rather the
	function body itself should be (and may just be a
	no-op on one side of the #if). That keeps the
	conditional pollution out of the main code.

      - call-chains of static helpers should all be in the
        same #if block, so they are all-or-nothing

    And if there's some case that doesn't cover, we still
    have MAYBE_UNUSED as a fallback.

Signed-off-by: Jeff King <peff@peff.net>
---
 config.mak.dev | 1 -
 1 file changed, 1 deletion(-)

diff --git a/config.mak.dev b/config.mak.dev
index 92d268137f..bbeeff44fe 100644
--- a/config.mak.dev
+++ b/config.mak.dev
@@ -34,7 +34,6 @@ ifeq ($(filter extra-all,$(DEVOPTS)),)
 CFLAGS += -Wno-empty-body
 CFLAGS += -Wno-missing-field-initializers
 CFLAGS += -Wno-sign-compare
-CFLAGS += -Wno-unused-function
 CFLAGS += -Wno-unused-parameter
 endif
 endif
-- 
2.19.1.823.g84a1dd67bf

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH] config.mak.dev: enable -Wunused-function
  2018-10-18  7:05 [PATCH] config.mak.dev: enable -Wunused-function Jeff King
@ 2018-10-18  7:08 ` Jeff King
  2018-10-18 15:48 ` Duy Nguyen
  2018-10-18 17:01 ` [PATCH] config.mak.dev: enable -Wunused-function Ramsay Jones
  2 siblings, 0 replies; 13+ messages in thread
From: Jeff King @ 2018-10-18  7:08 UTC (permalink / raw)
  To: git; +Cc: Nguyễn Thái Ngọc Duy

On Thu, Oct 18, 2018 at 03:05:22AM -0400, Jeff King wrote:

> diff --git a/config.mak.dev b/config.mak.dev
> index 92d268137f..bbeeff44fe 100644
> --- a/config.mak.dev
> +++ b/config.mak.dev
> @@ -34,7 +34,6 @@ ifeq ($(filter extra-all,$(DEVOPTS)),)
>  CFLAGS += -Wno-empty-body
>  CFLAGS += -Wno-missing-field-initializers
>  CFLAGS += -Wno-sign-compare
> -CFLAGS += -Wno-unused-function
>  CFLAGS += -Wno-unused-parameter

By the way, I wondered how close we were to being able to use
-Wunused-parameter. The answer is "not very".

However, I've been digging into the results, and it does find a number
of bugs. I'm 168 (rough) patches deep right now, and I have it compiling
cleanly. Most of those are just annotations, but I'll start posting
fixes as I organize and clean them up.

-Peff

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] config.mak.dev: enable -Wunused-function
  2018-10-18  7:05 [PATCH] config.mak.dev: enable -Wunused-function Jeff King
  2018-10-18  7:08 ` Jeff King
@ 2018-10-18 15:48 ` Duy Nguyen
  2018-10-18 17:09   ` Jeff King
  2018-10-18 17:01 ` [PATCH] config.mak.dev: enable -Wunused-function Ramsay Jones
  2 siblings, 1 reply; 13+ messages in thread
From: Duy Nguyen @ 2018-10-18 15:48 UTC (permalink / raw)
  To: Jeff King; +Cc: Git Mailing List

On Thu, Oct 18, 2018 at 9:05 AM Jeff King <peff@peff.net> wrote:
>
> We explicitly omitted -Wunused-function when we added
> -Wextra, but there is no need: the current code compiles
> cleanly with it. And it's worth having, since it can let you
> know when there are cascading effects from a cleanup (e.g.,
> deleting one function lets you delete its static helpers).
>
> There are cases where we may need an unused function to
> exist, but we can handle these easily:
>
>   - macro-generated code like commit-slab; there we have the
>     MAYBE_UNUSED annotation to silence the compiler
>
>   - conditional compilation, where we may or may not need a
>     static helper. These generally fall into one of two
>     categories:
>
>       - the call should not be conditional, but rather the
>         function body itself should be (and may just be a
>         no-op on one side of the #if). That keeps the
>         conditional pollution out of the main code.
>
>       - call-chains of static helpers should all be in the
>         same #if block, so they are all-or-nothing

Grouping is not always desired because it could break better function
layout. Have a look at read-cache.c where _ieot_extension functions
are #if'd because the only call sites are from pthread code (#if'd far
away).

In this particular case though I think we should be able to avoid so
much #if if we make a wrapper for pthread api that would return an
error or something when pthread is not available. But similar
situation may happen elsewhere too.

Having said that, if people do consider MAYBE_UNUSED before #if'ing
everywhere (and opening up more conditional build problems in future),
I think this change is fine.
-- 
Duy

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] config.mak.dev: enable -Wunused-function
  2018-10-18  7:05 [PATCH] config.mak.dev: enable -Wunused-function Jeff King
  2018-10-18  7:08 ` Jeff King
  2018-10-18 15:48 ` Duy Nguyen
@ 2018-10-18 17:01 ` Ramsay Jones
  2018-10-19  1:23   ` Junio C Hamano
  2 siblings, 1 reply; 13+ messages in thread
From: Ramsay Jones @ 2018-10-18 17:01 UTC (permalink / raw)
  To: Jeff King, git; +Cc: Nguyễn Thái Ngọc Duy, Junio C Hamano



On 18/10/2018 08:05, Jeff King wrote:
> We explicitly omitted -Wunused-function when we added
> -Wextra, but there is no need: the current code compiles
> cleanly with it. And it's worth having, since it can let you
> know when there are cascading effects from a cleanup (e.g.,
> deleting one function lets you delete its static helpers).
> 
> There are cases where we may need an unused function to
> exist, but we can handle these easily:
> 
>   - macro-generated code like commit-slab; there we have the
>     MAYBE_UNUSED annotation to silence the compiler
> 
>   - conditional compilation, where we may or may not need a
>     static helper. These generally fall into one of two
>     categories:
> 
>       - the call should not be conditional, but rather the
> 	function body itself should be (and may just be a
> 	no-op on one side of the #if). That keeps the
> 	conditional pollution out of the main code.
> 
>       - call-chains of static helpers should all be in the
>         same #if block, so they are all-or-nothing
> 
>     And if there's some case that doesn't cover, we still
>     have MAYBE_UNUSED as a fallback.
> 
> Signed-off-by: Jeff King <peff@peff.net>
> ---
>  config.mak.dev | 1 -
>  1 file changed, 1 deletion(-)
> 
> diff --git a/config.mak.dev b/config.mak.dev
> index 92d268137f..bbeeff44fe 100644
> --- a/config.mak.dev
> +++ b/config.mak.dev
> @@ -34,7 +34,6 @@ ifeq ($(filter extra-all,$(DEVOPTS)),)
>  CFLAGS += -Wno-empty-body
>  CFLAGS += -Wno-missing-field-initializers
>  CFLAGS += -Wno-sign-compare
> -CFLAGS += -Wno-unused-function
>  CFLAGS += -Wno-unused-parameter
>  endif
>  endif
> 

Heh, as luck would have it, tonight I had an -Wunused-function
warning on cygwin, but not Linux, when building the 'pu' branch.

[On linux, I am using DEVELOPER=1 in my config.mak etc., whereas
on cygwin I am still using an explicit (but very similar) list
of -W args.]

I haven't looked too deeply, but this seems to be caused by
Junio's commit 42c89ea70a ("SQUASH??? - convert the other user of
string-list as db", 2018-10-17) which removes a call to the
add_existing() function - the subject of the warning.

[BTW there is another 'static add_existing()' in builtin/show_ref.c]

ATB,
Ramsay Jones


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] config.mak.dev: enable -Wunused-function
  2018-10-18 15:48 ` Duy Nguyen
@ 2018-10-18 17:09   ` Jeff King
  2018-10-18 18:05     ` [PATCH/RFC] thread-utils: better wrapper to avoid #ifdef NO_PTHREADS Nguyễn Thái Ngọc Duy
  0 siblings, 1 reply; 13+ messages in thread
From: Jeff King @ 2018-10-18 17:09 UTC (permalink / raw)
  To: Duy Nguyen; +Cc: Git Mailing List

On Thu, Oct 18, 2018 at 05:48:16PM +0200, Duy Nguyen wrote:

> >   - conditional compilation, where we may or may not need a
> >     static helper. These generally fall into one of two
> >     categories:
> >
> >       - the call should not be conditional, but rather the
> >         function body itself should be (and may just be a
> >         no-op on one side of the #if). That keeps the
> >         conditional pollution out of the main code.
> >
> >       - call-chains of static helpers should all be in the
> >         same #if block, so they are all-or-nothing
> 
> Grouping is not always desired because it could break better function
> layout. Have a look at read-cache.c where _ieot_extension functions
> are #if'd because the only call sites are from pthread code (#if'd far
> away).

True, though as long as they are triggered by the same set of #if
conditions, that is fine. Putting them in the same block  is just an
easy way to make sure that is the case. ;)

> In this particular case though I think we should be able to avoid so
> much #if if we make a wrapper for pthread api that would return an
> error or something when pthread is not available. But similar
> situation may happen elsewhere too.

Yeah, I think that is generally the preferred method anyway, just
because of readability and simplicity.

> Having said that, if people do consider MAYBE_UNUSED before #if'ing
> everywhere (and opening up more conditional build problems in future),
> I think this change is fine.

I'd like to use it as a last resort, certainly. Mostly the fact that we
compile cleanly _now_ makes me think that it probably won't be that hard
to keep it going.

I think the biggest potential problem with this is going to be obscure
configurations where some functions are used or not used. So somebody
silencing a compiler warning may inadvertently break another case if
they're not careful. But that's already a problem to some degree (and
part of why we try to push the conditionality out to the whole-function
level).

-Peff

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH/RFC] thread-utils: better wrapper to avoid #ifdef NO_PTHREADS
  2018-10-18 17:09   ` Jeff King
@ 2018-10-18 18:05     ` Nguyễn Thái Ngọc Duy
  2018-10-23 20:28       ` Jeff King
  0 siblings, 1 reply; 13+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2018-10-18 18:05 UTC (permalink / raw)
  To: git; +Cc: Jeff King, Nguyễn Thái Ngọc Duy

On Thu, Oct 18, 2018 at 7:09 PM Jeff King <peff@peff.net> wrote:
> > In this particular case though I think we should be able to avoid so
> > much #if if we make a wrapper for pthread api that would return an
> > error or something when pthread is not available. But similar
> > situation may happen elsewhere too.
>
> Yeah, I think that is generally the preferred method anyway, just
> because of readability and simplicity.

I've wanted to do this for a while, so let's test the water and see if
it's well received.

This patch is a proof of concept that adds just enough macros so that
I can build index-pack.c on a single thread mode with zero #ifdef
related to NO_PTHREADS.

Besides readability and simplicity, it reduces the chances of breaking
conditional builds (e.g. you rename a variable name but forgot that
the variable is in #if block that is not used by your
compiler/platform).

Performance-wise I don't think there is any loss for single thread
mode. I rely on compilers recognizing HAVE_THREADS being a constant
and remove dead code or at least optimize in favor of non-dead code.

Memory-wise, yes we use some more memory in single thread mode. But we
don't have zillions of mutexes or thread id, so a bit extra memory
does not worry me so much.

Hmm?
---
 Makefile             |  2 +-
 builtin/index-pack.c | 68 ++++++++++++--------------------------------
 thread-utils.c       | 30 +++++++++++++++++++
 thread-utils.h       | 38 +++++++++++++++++++++++--
 4 files changed, 84 insertions(+), 54 deletions(-)

diff --git a/Makefile b/Makefile
index 5bf1af369e..ef852031bd 100644
--- a/Makefile
+++ b/Makefile
@@ -981,6 +981,7 @@ LIB_OBJS += sub-process.o
 LIB_OBJS += symlinks.o
 LIB_OBJS += tag.o
 LIB_OBJS += tempfile.o
+LIB_OBJS += thread-utils.o
 LIB_OBJS += tmp-objdir.o
 LIB_OBJS += trace.o
 LIB_OBJS += trailer.o
@@ -1664,7 +1665,6 @@ ifdef NO_PTHREADS
 else
 	BASIC_CFLAGS += $(PTHREAD_CFLAGS)
 	EXTLIBS += $(PTHREAD_LIBS)
-	LIB_OBJS += thread-utils.o
 endif
 
 ifdef HAVE_PATHS_H
diff --git a/builtin/index-pack.c b/builtin/index-pack.c
index 2004e25da2..bbd66ca025 100644
--- a/builtin/index-pack.c
+++ b/builtin/index-pack.c
@@ -42,9 +42,7 @@ struct base_data {
 };
 
 struct thread_local {
-#ifndef NO_PTHREADS
 	pthread_t thread;
-#endif
 	struct base_data *base_cache;
 	size_t base_cache_used;
 	int pack_fd;
@@ -98,8 +96,6 @@ static uint32_t input_crc32;
 static int input_fd, output_fd;
 static const char *curr_pack;
 
-#ifndef NO_PTHREADS
-
 static struct thread_local *thread_data;
 static int nr_dispatched;
 static int threads_active;
@@ -179,26 +175,6 @@ static void cleanup_thread(void)
 	free(thread_data);
 }
 
-#else
-
-#define read_lock()
-#define read_unlock()
-
-#define counter_lock()
-#define counter_unlock()
-
-#define work_lock()
-#define work_unlock()
-
-#define deepest_delta_lock()
-#define deepest_delta_unlock()
-
-#define type_cas_lock()
-#define type_cas_unlock()
-
-#endif
-
-
 static int mark_link(struct object *obj, int type, void *data, struct fsck_options *options)
 {
 	if (!obj)
@@ -364,22 +340,20 @@ static NORETURN void bad_object(off_t offset, const char *format, ...)
 
 static inline struct thread_local *get_thread_data(void)
 {
-#ifndef NO_PTHREADS
-	if (threads_active)
-		return pthread_getspecific(key);
-	assert(!threads_active &&
-	       "This should only be reached when all threads are gone");
-#endif
+	if (HAVE_THREADS) {
+		if (threads_active)
+			return pthread_getspecific(key);
+		assert(!threads_active &&
+		       "This should only be reached when all threads are gone");
+	}
 	return &nothread_data;
 }
 
-#ifndef NO_PTHREADS
 static void set_thread_data(struct thread_local *data)
 {
 	if (threads_active)
 		pthread_setspecific(key, data);
 }
-#endif
 
 static struct base_data *alloc_base_data(void)
 {
@@ -1092,7 +1066,6 @@ static void resolve_base(struct object_entry *obj)
 	find_unresolved_deltas(base_obj);
 }
 
-#ifndef NO_PTHREADS
 static void *threaded_second_pass(void *data)
 {
 	set_thread_data(data);
@@ -1116,7 +1089,6 @@ static void *threaded_second_pass(void *data)
 	}
 	return NULL;
 }
-#endif
 
 /*
  * First pass:
@@ -1213,7 +1185,6 @@ static void resolve_deltas(void)
 		progress = start_progress(_("Resolving deltas"),
 					  nr_ref_deltas + nr_ofs_deltas);
 
-#ifndef NO_PTHREADS
 	nr_dispatched = 0;
 	if (nr_threads > 1 || getenv("GIT_FORCE_THREADS")) {
 		init_thread();
@@ -1229,7 +1200,6 @@ static void resolve_deltas(void)
 		cleanup_thread();
 		return;
 	}
-#endif
 
 	for (i = 0; i < nr_objects; i++) {
 		struct object_entry *obj = &objects[i];
@@ -1531,11 +1501,11 @@ static int git_index_pack_config(const char *k, const char *v, void *cb)
 		if (nr_threads < 0)
 			die(_("invalid number of threads specified (%d)"),
 			    nr_threads);
-#ifdef NO_PTHREADS
-		if (nr_threads != 1)
-			warning(_("no threads support, ignoring %s"), k);
-		nr_threads = 1;
-#endif
+		if (!HAVE_THREADS) {
+			if (nr_threads != 1)
+				warning(_("no threads support, ignoring %s"), k);
+			nr_threads = 1;
+		}
 		return 0;
 	}
 	return git_default_config(k, v, cb);
@@ -1723,12 +1693,12 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
 				nr_threads = strtoul(arg+10, &end, 0);
 				if (!arg[10] || *end || nr_threads < 0)
 					usage(index_pack_usage);
-#ifdef NO_PTHREADS
-				if (nr_threads != 1)
-					warning(_("no threads support, "
-						  "ignoring %s"), arg);
-				nr_threads = 1;
-#endif
+				if (!HAVE_THREADS) {
+					if (nr_threads != 1)
+						warning(_("no threads support, "
+							  "ignoring %s"), arg);
+					nr_threads = 1;
+				}
 			} else if (starts_with(arg, "--pack_header=")) {
 				struct pack_header *hdr;
 				char *c;
@@ -1791,14 +1761,12 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
 	if (strict)
 		opts.flags |= WRITE_IDX_STRICT;
 
-#ifndef NO_PTHREADS
-	if (!nr_threads) {
+	if (HAVE_THREADS && !nr_threads) {
 		nr_threads = online_cpus();
 		/* An experiment showed that more threads does not mean faster */
 		if (nr_threads > 3)
 			nr_threads = 3;
 	}
-#endif
 
 	curr_pack = open_pack_file(pack_name);
 	parse_pack_header();
diff --git a/thread-utils.c b/thread-utils.c
index a2135e0743..d205a474e0 100644
--- a/thread-utils.c
+++ b/thread-utils.c
@@ -20,6 +20,9 @@
 
 int online_cpus(void)
 {
+#ifdef NO_PTHREADS
+	return 1;
+#else
 #ifdef _SC_NPROCESSORS_ONLN
 	long ncpus;
 #endif
@@ -59,10 +62,12 @@ int online_cpus(void)
 #endif
 
 	return 1;
+#endif
 }
 
 int init_recursive_mutex(pthread_mutex_t *m)
 {
+#ifndef NO_PTHREADS
 	pthread_mutexattr_t a;
 	int ret;
 
@@ -74,4 +79,29 @@ int init_recursive_mutex(pthread_mutex_t *m)
 		pthread_mutexattr_destroy(&a);
 	}
 	return ret;
+#else
+	return ENOSYS;
+#endif
+}
+
+#ifdef NO_PTHREADS
+int dummy_pthread_create(pthread_t *pthread, const void *attr,
+			 void *(*fn)(void *), void *data)
+{
+	return ENOSYS;
 }
+
+int dummy_pthread_init(void *data)
+{
+	/*
+	 * Do nothing.
+	 *
+	 * The main purpose of this function is to break compiler's
+	 * flow analysis or it may realize that functions like
+	 * pthread_mutex_init() is no-op, which means the (static)
+	 * variable is not used/initialized at all and trigger
+	 * -Wunused-variable
+	 */
+	return ENOSYS;
+}
+#endif
diff --git a/thread-utils.h b/thread-utils.h
index d9a769d190..b8c6500c42 100644
--- a/thread-utils.h
+++ b/thread-utils.h
@@ -4,12 +4,44 @@
 #ifndef NO_PTHREADS
 #include <pthread.h>
 
-extern int online_cpus(void);
-extern int init_recursive_mutex(pthread_mutex_t*);
+#define HAVE_THREADS 1
 
 #else
 
-#define online_cpus() 1
+#define HAVE_THREADS 0
+
+/*
+ * macros instead of typedefs because pthread definitions may have
+ * been pulled in by some system dependencies even though the user
+ * wants to disable pthread.
+ */
+#define pthread_t int
+#define pthread_mutex_t int
+
+#define pthread_mutex_init(mutex, attr) dummy_pthread_init(mutex)
+#define pthread_mutex_lock(mutex)
+#define pthread_mutex_unlock(mutex)
+#define pthread_mutex_destroy(mutex)
+
+#define pthread_key_create(key, attr) dummy_pthread_init(key)
+#define pthread_key_delete(key)
+
+#define pthread_create(thread, attr, fn, data) \
+	dummy_pthread_create(thread, attr, fn, data)
+#define pthread_join(thread, reval) ENOSYS
+
+#define pthread_setspecific(key, data)
+#define pthread_getspecific(key) NULL
+
+int dummy_pthread_create(pthread_t *pthread, const void *attr,
+			 void *(*fn)(void *), void *data);
+
+int dummy_pthread_init(void *);
 
 #endif
+
+int online_cpus(void);
+int init_recursive_mutex(pthread_mutex_t*);
+
+
 #endif /* THREAD_COMPAT_H */
-- 
2.19.1.647.g708186aaf9


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH] config.mak.dev: enable -Wunused-function
  2018-10-18 17:01 ` [PATCH] config.mak.dev: enable -Wunused-function Ramsay Jones
@ 2018-10-19  1:23   ` Junio C Hamano
  0 siblings, 0 replies; 13+ messages in thread
From: Junio C Hamano @ 2018-10-19  1:23 UTC (permalink / raw)
  To: Ramsay Jones; +Cc: Jeff King, git, Nguyễn Thái Ngọc Duy

Ramsay Jones <ramsay@ramsayjones.plus.com> writes:

> I haven't looked too deeply, but this seems to be caused by
> Junio's commit 42c89ea70a ("SQUASH??? - convert the other user of
> string-list as db", 2018-10-17) which removes a call to the
> add_existing() function - the subject of the warning.

That is very understandable and it is immediately obvious to me that
the unused-function warning is being useful there ;-)

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH/RFC] thread-utils: better wrapper to avoid #ifdef NO_PTHREADS
  2018-10-18 18:05     ` [PATCH/RFC] thread-utils: better wrapper to avoid #ifdef NO_PTHREADS Nguyễn Thái Ngọc Duy
@ 2018-10-23 20:28       ` Jeff King
  2018-10-24  2:58         ` Junio C Hamano
  2018-10-26 14:09         ` Ben Peart
  0 siblings, 2 replies; 13+ messages in thread
From: Jeff King @ 2018-10-23 20:28 UTC (permalink / raw)
  To: Nguyễn Thái Ngọc Duy; +Cc: git

On Thu, Oct 18, 2018 at 08:05:22PM +0200, Nguyễn Thái Ngọc Duy wrote:

> On Thu, Oct 18, 2018 at 7:09 PM Jeff King <peff@peff.net> wrote:
> > > In this particular case though I think we should be able to avoid so
> > > much #if if we make a wrapper for pthread api that would return an
> > > error or something when pthread is not available. But similar
> > > situation may happen elsewhere too.
> >
> > Yeah, I think that is generally the preferred method anyway, just
> > because of readability and simplicity.
> 
> I've wanted to do this for a while, so let's test the water and see if
> it's well received.
> 
> This patch is a proof of concept that adds just enough macros so that
> I can build index-pack.c on a single thread mode with zero #ifdef
> related to NO_PTHREADS.
> 
> Besides readability and simplicity, it reduces the chances of breaking
> conditional builds (e.g. you rename a variable name but forgot that
> the variable is in #if block that is not used by your
> compiler/platform).

Yes, I love this. We're already halfway there with things like
read_lock() in index-pack and elsewhere, which are conditionally no-ops.
The resulting code is much easier to read, I think.

> Performance-wise I don't think there is any loss for single thread
> mode. I rely on compilers recognizing HAVE_THREADS being a constant
> and remove dead code or at least optimize in favor of non-dead code.
> 
> Memory-wise, yes we use some more memory in single thread mode. But we
> don't have zillions of mutexes or thread id, so a bit extra memory
> does not worry me so much.

Yeah, I don't think carrying around a handful of ints is going to be a
big deal.

I also think we may want to make a fundamental shift in our view of
thread support. In the early days, it was "well, this is a thing that
modern systems can take advantage of for certain commands". But these
days I suspect it is more like "there are a handful of legacy systems
that do not even support threads".

I don't think we should break the build on those legacy systems, but
it's probably OK to stop thinking of it as "non-threaded platforms are
the default and must pay zero cost" and more as "threaded platforms are
the default, and non-threaded ones are OK to pay a small cost as long as
they still work".

> @@ -74,4 +79,29 @@ int init_recursive_mutex(pthread_mutex_t *m)
>  		pthread_mutexattr_destroy(&a);
>  	}
>  	return ret;
> +#else
> +	return ENOSYS;
> +#endif
> +}

I suspect some of these ENOSYS could just become a silent success.
("yep, I initialized your dummy mutex"). But it probably doesn't matter
much either way, as we would not generally even bother checking this
return.

> +#ifdef NO_PTHREADS
> +int dummy_pthread_create(pthread_t *pthread, const void *attr,
> +			 void *(*fn)(void *), void *data)
> +{
> +	return ENOSYS;
>  }

Whereas for this one, ENOSYS makes a lot of sense (we should avoid the
threaded code-path anyway when we see that online_cpus()==1, and this
would let us know when we mess that up).

> +int dummy_pthread_init(void *data)
> +{
> +	/*
> +	 * Do nothing.
> +	 *
> +	 * The main purpose of this function is to break compiler's
> +	 * flow analysis or it may realize that functions like
> +	 * pthread_mutex_init() is no-op, which means the (static)
> +	 * variable is not used/initialized at all and trigger
> +	 * -Wunused-variable
> +	 */
> +	return ENOSYS;
> +}

It might be worth marking the dummy variables as MAYBE_UNUSED, exactly
to avoid this kind of compiler complaint.

-Peff

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH/RFC] thread-utils: better wrapper to avoid #ifdef NO_PTHREADS
  2018-10-23 20:28       ` Jeff King
@ 2018-10-24  2:58         ` Junio C Hamano
  2018-10-26 14:09         ` Ben Peart
  1 sibling, 0 replies; 13+ messages in thread
From: Junio C Hamano @ 2018-10-24  2:58 UTC (permalink / raw)
  To: Jeff King; +Cc: Nguyễn Thái Ngọc Duy, git

Jeff King <peff@peff.net> writes:

> I also think we may want to make a fundamental shift in our view of
> thread support. In the early days, it was "well, this is a thing that
> modern systems can take advantage of for certain commands". But these
> days I suspect it is more like "there are a handful of legacy systems
> that do not even support threads".
>
> I don't think we should break the build on those legacy systems, but
> it's probably OK to stop thinking of it as "non-threaded platforms are
> the default and must pay zero cost" and more as "threaded platforms are
> the default, and non-threaded ones are OK to pay a small cost as long as
> they still work".

Good suggestion.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH/RFC] thread-utils: better wrapper to avoid #ifdef NO_PTHREADS
  2018-10-23 20:28       ` Jeff King
  2018-10-24  2:58         ` Junio C Hamano
@ 2018-10-26 14:09         ` Ben Peart
  2018-10-27  7:12           ` can we deprecate NO_PTHREADS?, was: " Jeff King
  2018-10-27  7:26           ` [PATCH/RFC] thread-utils: " Duy Nguyen
  1 sibling, 2 replies; 13+ messages in thread
From: Ben Peart @ 2018-10-26 14:09 UTC (permalink / raw)
  To: Jeff King, Nguyễn Thái Ngọc Duy; +Cc: git



On 10/23/2018 4:28 PM, Jeff King wrote:
> On Thu, Oct 18, 2018 at 08:05:22PM +0200, Nguyễn Thái Ngọc Duy wrote:
> 
>> On Thu, Oct 18, 2018 at 7:09 PM Jeff King <peff@peff.net> wrote:
>>>> In this particular case though I think we should be able to avoid so
>>>> much #if if we make a wrapper for pthread api that would return an
>>>> error or something when pthread is not available. But similar
>>>> situation may happen elsewhere too.
>>>
>>> Yeah, I think that is generally the preferred method anyway, just
>>> because of readability and simplicity.
>>
>> I've wanted to do this for a while, so let's test the water and see if
>> it's well received.
>>
>> This patch is a proof of concept that adds just enough macros so that
>> I can build index-pack.c on a single thread mode with zero #ifdef
>> related to NO_PTHREADS.
>>
>> Besides readability and simplicity, it reduces the chances of breaking
>> conditional builds (e.g. you rename a variable name but forgot that
>> the variable is in #if block that is not used by your
>> compiler/platform).
> 
> Yes, I love this. We're already halfway there with things like
> read_lock() in index-pack and elsewhere, which are conditionally no-ops.
> The resulting code is much easier to read, I think.
> 

I am also very much in favor of this.  I updated a couple of places 
threading is being used that I've been working in (preload-index and 
read-cache) and both are much simplified using your proof of concept patch.

>> Performance-wise I don't think there is any loss for single thread
>> mode. I rely on compilers recognizing HAVE_THREADS being a constant
>> and remove dead code or at least optimize in favor of non-dead code.
>>
>> Memory-wise, yes we use some more memory in single thread mode. But we
>> don't have zillions of mutexes or thread id, so a bit extra memory
>> does not worry me so much.
> 
> Yeah, I don't think carrying around a handful of ints is going to be a
> big deal.
> 

Just to be complete, there _is_ an additional cost.  Today, code paths 
that are only executed when there are pthreads available are excluded 
from the binary (via #ifdef).  With this change, those code paths would 
now be included causing some code bloat to NO_PTHREAD threaded images.

One example of this is in read-cache.c where the ieot read/write 
functions aren't included for NO_PTHREAD but now would be.

> I also think we may want to make a fundamental shift in our view of
> thread support. In the early days, it was "well, this is a thing that
> modern systems can take advantage of for certain commands". But these
> days I suspect it is more like "there are a handful of legacy systems
> that do not even support threads".
> 
> I don't think we should break the build on those legacy systems, but
> it's probably OK to stop thinking of it as "non-threaded platforms are
> the default and must pay zero cost" and more as "threaded platforms are
> the default, and non-threaded ones are OK to pay a small cost as long as
> they still work".
> 

I agree though I'm still curious if there are still no-threaded 
platforms taking new versions of git.  Perhaps we should do the 
depreciation warning you suggested elsewhere and see how much push back 
we get.  It's unlikely we'd get lucky and be able to stop supporting 
them completely but it's worth asking!

>> @@ -74,4 +79,29 @@ int init_recursive_mutex(pthread_mutex_t *m)
>>   		pthread_mutexattr_destroy(&a);
>>   	}
>>   	return ret;
>> +#else
>> +	return ENOSYS;
>> +#endif
>> +}
> 
> I suspect some of these ENOSYS could just become a silent success.
> ("yep, I initialized your dummy mutex"). But it probably doesn't matter
> much either way, as we would not generally even bother checking this
> return.
> 
>> +#ifdef NO_PTHREADS
>> +int dummy_pthread_create(pthread_t *pthread, const void *attr,
>> +			 void *(*fn)(void *), void *data)
>> +{
>> +	return ENOSYS;
>>   }
> 
> Whereas for this one, ENOSYS makes a lot of sense (we should avoid the
> threaded code-path anyway when we see that online_cpus()==1, and this
> would let us know when we mess that up).
> 

This highlights something anyone writing multi-threaded code will need 
to pay attention to that wasn't an issue before.  If you attempt to 
create more threads than online_cpus(), the pthread_create() call will 
fail and needs to be handled gracefully.

One example of this is in preload-index.c where (up to) 20 threads are 
created irrespective of what online_cpus() returns and if 
pthread_create() fails, it just dies.  The logic would need to be 
updated for this to work correctly.

I still think this is a much simpler issue to deal with than what we 
have today with having to write/debug multiple code paths but I did want 
to point it out for completeness.

>> +int dummy_pthread_init(void *data)
>> +{
>> +	/*
>> +	 * Do nothing.
>> +	 *
>> +	 * The main purpose of this function is to break compiler's
>> +	 * flow analysis or it may realize that functions like
>> +	 * pthread_mutex_init() is no-op, which means the (static)
>> +	 * variable is not used/initialized at all and trigger
>> +	 * -Wunused-variable
>> +	 */
>> +	return ENOSYS;
>> +}
> 
> It might be worth marking the dummy variables as MAYBE_UNUSED, exactly
> to avoid this kind of compiler complaint.
> 
> -Peff
> 

^ permalink raw reply	[flat|nested] 13+ messages in thread

* can we deprecate NO_PTHREADS?, was: better wrapper to avoid #ifdef NO_PTHREADS
  2018-10-26 14:09         ` Ben Peart
@ 2018-10-27  7:12           ` Jeff King
  2018-10-27  7:26           ` [PATCH/RFC] thread-utils: " Duy Nguyen
  1 sibling, 0 replies; 13+ messages in thread
From: Jeff King @ 2018-10-27  7:12 UTC (permalink / raw)
  To: Ben Peart; +Cc: Nguyễn Thái Ngọc Duy, git

On Fri, Oct 26, 2018 at 10:09:46AM -0400, Ben Peart wrote:

> > Yeah, I don't think carrying around a handful of ints is going to be a
> > big deal.
> 
> Just to be complete, there _is_ an additional cost.  Today, code paths that
> are only executed when there are pthreads available are excluded from the
> binary (via #ifdef).  With this change, those code paths would now be
> included causing some code bloat to NO_PTHREAD threaded images.
> 
> One example of this is in read-cache.c where the ieot read/write functions
> aren't included for NO_PTHREAD but now would be.

Yeah. I think that is also an OK cost. It may bloat the binary a little,
but if we're not running those instructions, they're probably not
bloating CPU cache, etc.

> > I don't think we should break the build on those legacy systems, but
> > it's probably OK to stop thinking of it as "non-threaded platforms are
> > the default and must pay zero cost" and more as "threaded platforms are
> > the default, and non-threaded ones are OK to pay a small cost as long as
> > they still work".
> 
> I agree though I'm still curious if there are still no-threaded platforms
> taking new versions of git.  Perhaps we should do the depreciation warning
> you suggested elsewhere and see how much push back we get.  It's unlikely
> we'd get lucky and be able to stop supporting them completely but it's worth
> asking!

I'm trying to think how that might look. I think putting it into the
binary and warning at runtime is probably a little _too_ obnoxious. Here
are some other options ranging from less to more annoying:

  - mention it in the release notes (guaranteed not to hurt anybody, but
    also likely to be missed)

  - a build-time warning when the NO_PTHREADS is set. Also easy to miss,
    but at least hits the people who are using it.

  - rename NO_PTHREADS to NO_PTHREADS_REALLY, and error out the build
    when NO_PTHREADS is set. That would certainly get people's
    attention.

I guess it would make sense to do them in ascending order. I.e., maybe
start with something like:

diff --git a/Makefile b/Makefile
index b08d5ea258..8ac234e4c3 100644
--- a/Makefile
+++ b/Makefile
@@ -1670,6 +1670,11 @@ ifdef RUNTIME_PREFIX
 endif
 
 ifdef NO_PTHREADS
+$(warning The NO_PTHREADS flag is being considered for deprecation,)
+$(warning which would require all platforms supported by Git to have)
+$(warning some kind of threading support. If your platform does not)
+$(warning support threading, please report it by sending an email to)
+$(warning git@vger.kernel.org.)
 	BASIC_CFLAGS += -DNO_PTHREADS
 else
 	BASIC_CFLAGS += $(PTHREAD_CFLAGS)

If we get no takers, that doesn't prove much, but it builds confidence
in moving to the "REALLY" variant. And if we do get a report, we can be
told this is a bad idea before moving further.

Also, the posting of the patch itself may gather some feedback. ;)

-Peff

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH/RFC] thread-utils: better wrapper to avoid #ifdef NO_PTHREADS
  2018-10-26 14:09         ` Ben Peart
  2018-10-27  7:12           ` can we deprecate NO_PTHREADS?, was: " Jeff King
@ 2018-10-27  7:26           ` Duy Nguyen
  2018-10-27  8:17             ` Jeff King
  1 sibling, 1 reply; 13+ messages in thread
From: Duy Nguyen @ 2018-10-27  7:26 UTC (permalink / raw)
  To: Ben Peart; +Cc: Jeff King, Git Mailing List

On Fri, Oct 26, 2018 at 4:09 PM Ben Peart <peartben@gmail.com> wrote:
> I agree though I'm still curious if there are still no-threaded
> platforms taking new versions of git.  Perhaps we should do the
> depreciation warning you suggested elsewhere and see how much push back
> we get.  It's unlikely we'd get lucky and be able to stop supporting
> them completely but it's worth asking!

NO_PTHREADS can also be used even though the platform supports
multithread: to make keep git execution in a single core/thread. It
might matter on hosted systems with limited cpu power and you don't
want git to hog it all. Yes it can also be achieved by setting a
zillion config keys to "1", this way is just simpler.
-- 
Duy

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH/RFC] thread-utils: better wrapper to avoid #ifdef NO_PTHREADS
  2018-10-27  7:26           ` [PATCH/RFC] thread-utils: " Duy Nguyen
@ 2018-10-27  8:17             ` Jeff King
  0 siblings, 0 replies; 13+ messages in thread
From: Jeff King @ 2018-10-27  8:17 UTC (permalink / raw)
  To: Duy Nguyen; +Cc: Ben Peart, Git Mailing List

On Sat, Oct 27, 2018 at 09:26:28AM +0200, Duy Nguyen wrote:

> On Fri, Oct 26, 2018 at 4:09 PM Ben Peart <peartben@gmail.com> wrote:
> > I agree though I'm still curious if there are still no-threaded
> > platforms taking new versions of git.  Perhaps we should do the
> > depreciation warning you suggested elsewhere and see how much push back
> > we get.  It's unlikely we'd get lucky and be able to stop supporting
> > them completely but it's worth asking!
> 
> NO_PTHREADS can also be used even though the platform supports
> multithread: to make keep git execution in a single core/thread. It
> might matter on hosted systems with limited cpu power and you don't
> want git to hog it all. Yes it can also be achieved by setting a
> zillion config keys to "1", this way is just simpler.

Yeah, I wondered about that use case (also with your patches, and
whether they might run into problems on systems that _do_ have pthreads,
but just don't want to compile with them).

But I think that is pretty easily solved by just having a single runtime
option (e.g., to just pretend that oneline_cpus is always 1 by default).

-Peff

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2018-10-27  8:17 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-18  7:05 [PATCH] config.mak.dev: enable -Wunused-function Jeff King
2018-10-18  7:08 ` Jeff King
2018-10-18 15:48 ` Duy Nguyen
2018-10-18 17:09   ` Jeff King
2018-10-18 18:05     ` [PATCH/RFC] thread-utils: better wrapper to avoid #ifdef NO_PTHREADS Nguyễn Thái Ngọc Duy
2018-10-23 20:28       ` Jeff King
2018-10-24  2:58         ` Junio C Hamano
2018-10-26 14:09         ` Ben Peart
2018-10-27  7:12           ` can we deprecate NO_PTHREADS?, was: " Jeff King
2018-10-27  7:26           ` [PATCH/RFC] thread-utils: " Duy Nguyen
2018-10-27  8:17             ` Jeff King
2018-10-18 17:01 ` [PATCH] config.mak.dev: enable -Wunused-function Ramsay Jones
2018-10-19  1:23   ` Junio C Hamano

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).