path: root/thread-utils.c
diff options
authorAndreas Ericsson <>2008-02-23 02:11:56 (GMT)
committerJunio C Hamano <>2008-02-23 20:00:32 (GMT)
commit833e3df17188e48b2d31db0966a4051dad5e03b9 (patch)
tree265fb9405efb644ff564ac1b77991d62795006a2 /thread-utils.c
parent923d44aeb7cb6d21eeb459fdc1b58137e37c2b1c (diff)
pack-objects: Add runtime detection of online CPU's
Packing objects can be done in parallell nowadays, but it's only done if the config option pack.threads is set to a value above 1. Because of that, the code-path used is often not the most optimal one. This patch adds a routine to detect the number of online CPU's at runtime (online_cpus()). When pack.threads (or --threads=) is given a value of 0, the number of threads is set to the number of online CPU's. This feature is also documented. As per Nicolas Pitre's recommendations, the default is still to run pack-objects single-threaded unless explicitly activated, either by configuration or by command line parameter. The routine online_cpus() is a rework of "numcpus.c", written by one Philip Willoughby <>. numcpus.c is in the public domain and can presently be downloaded from Signed-off-by: Andreas Ericsson <> Signed-off-by: Brandon Casey <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'thread-utils.c')
1 files changed, 48 insertions, 0 deletions
diff --git a/thread-utils.c b/thread-utils.c
new file mode 100644
index 0000000..55e7e29
--- /dev/null
+++ b/thread-utils.c
@@ -0,0 +1,48 @@
+#include "cache.h"
+#ifdef _WIN32
+# define WIN32_LEAN_AND_MEAN
+# include <windows.h>
+#elif defined(hpux) || defined(__hpux) || defined(_hpux)
+# include <sys/pstat.h>
+ * By doing this in two steps we can at least get
+ * the function to be somewhat coherent, even
+ * with this disgusting nest of #ifdefs.
+ */
+# ifdef _SC_NPROC_ONLN
+# elif defined _SC_CRAY_NCPU
+# endif
+int online_cpus(void)
+ long ncpus;
+#ifdef _WIN32
+ GetSystemInfo(&info);
+ if ((int)info.dwNumberOfProcessors > 0)
+ return (int)info.dwNumberOfProcessors;
+#elif defined(hpux) || defined(__hpux) || defined(_hpux)
+ struct pst_dynamic psd;
+ if (!pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0))
+ return (int)psd.psd_proc_cnt;
+ if ((ncpus = (long)sysconf(_SC_NPROCESSORS_ONLN)) > 0)
+ return (int)ncpus;
+ return 1;