[tor-commits] [tor/master] Move compute_num_cpus to lib/thread

nickm at torproject.org nickm at torproject.org
Thu Jun 28 19:21:00 UTC 2018


commit 6178a9f758d60f5fc896644f1b9b0aaf4c32f2a5
Author: Nick Mathewson <nickm at torproject.org>
Date:   Thu Jun 28 12:08:18 2018 -0400

    Move compute_num_cpus to lib/thread
---
 src/common/compat.c       | 75 --------------------------------------
 src/common/compat.h       |  2 -
 src/lib/thread/include.am |  4 +-
 src/lib/thread/numcpus.c  | 93 +++++++++++++++++++++++++++++++++++++++++++++++
 src/lib/thread/numcpus.h  | 11 ++++++
 src/or/config.c           |  1 +
 src/test/test_util.c      |  1 +
 7 files changed, 109 insertions(+), 78 deletions(-)

diff --git a/src/common/compat.c b/src/common/compat.c
index f8333624c..6d89e1c08 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -354,81 +354,6 @@ get_uname,(void))
  *   Process control
  */
 
-/** Implementation logic for compute_num_cpus(). */
-static int
-compute_num_cpus_impl(void)
-{
-#ifdef _WIN32
-  SYSTEM_INFO info;
-  memset(&info, 0, sizeof(info));
-  GetSystemInfo(&info);
-  if (info.dwNumberOfProcessors >= 1 && info.dwNumberOfProcessors < INT_MAX)
-    return (int)info.dwNumberOfProcessors;
-  else
-    return -1;
-#elif defined(HAVE_SYSCONF)
-#ifdef _SC_NPROCESSORS_CONF
-  long cpus_conf = sysconf(_SC_NPROCESSORS_CONF);
-#else
-  long cpus_conf = -1;
-#endif
-#ifdef _SC_NPROCESSORS_ONLN
-  long cpus_onln = sysconf(_SC_NPROCESSORS_ONLN);
-#else
-  long cpus_onln = -1;
-#endif
-  long cpus = -1;
-
-  if (cpus_conf > 0 && cpus_onln < 0) {
-    cpus = cpus_conf;
-  } else if (cpus_onln > 0 && cpus_conf < 0) {
-    cpus = cpus_onln;
-  } else if (cpus_onln > 0 && cpus_conf > 0) {
-    if (cpus_onln < cpus_conf) {
-      log_notice(LD_GENERAL, "I think we have %ld CPUS, but only %ld of them "
-                 "are available. Telling Tor to only use %ld. You can over"
-                 "ride this with the NumCPUs option",
-                 cpus_conf, cpus_onln, cpus_onln);
-    }
-    cpus = cpus_onln;
-  }
-
-  if (cpus >= 1 && cpus < INT_MAX)
-    return (int)cpus;
-  else
-    return -1;
-#else
-  return -1;
-#endif /* defined(_WIN32) || ... */
-}
-
-#define MAX_DETECTABLE_CPUS 16
-
-/** Return how many CPUs we are running with.  We assume that nobody is
- * using hot-swappable CPUs, so we don't recompute this after the first
- * time.  Return -1 if we don't know how to tell the number of CPUs on this
- * system.
- */
-int
-compute_num_cpus(void)
-{
-  static int num_cpus = -2;
-  if (num_cpus == -2) {
-    num_cpus = compute_num_cpus_impl();
-    tor_assert(num_cpus != -2);
-    if (num_cpus > MAX_DETECTABLE_CPUS) {
-      /* LCOV_EXCL_START */
-      log_notice(LD_GENERAL, "Wow!  I detected that you have %d CPUs. I "
-                 "will not autodetect any more than %d, though.  If you "
-                 "want to configure more, set NumCPUs in your torrc",
-                 num_cpus, MAX_DETECTABLE_CPUS);
-      num_cpus = MAX_DETECTABLE_CPUS;
-      /* LCOV_EXCL_STOP */
-    }
-  }
-  return num_cpus;
-}
-
 #if defined(HW_PHYSMEM64)
 /* This appears to be an OpenBSD thing */
 #define INT64_HW_MEM HW_PHYSMEM64
diff --git a/src/common/compat.h b/src/common/compat.h
index 13e20a4ac..5cd8e7215 100644
--- a/src/common/compat.h
+++ b/src/common/compat.h
@@ -133,8 +133,6 @@ typedef unsigned long rlim_t;
 int set_max_file_descriptors(rlim_t limit, int *max);
 MOCK_DECL(int, get_total_system_memory, (size_t *mem_out));
 
-int compute_num_cpus(void);
-
 /** Macros for MIN/MAX.  Never use these when the arguments could have
  * side-effects.
  * {With GCC extensions we could probably define a safer MIN/MAX.  But
diff --git a/src/lib/thread/include.am b/src/lib/thread/include.am
index 0eac7edee..9ec23d166 100644
--- a/src/lib/thread/include.am
+++ b/src/lib/thread/include.am
@@ -14,6 +14,7 @@ endif
 
 src_lib_libtor_thread_a_SOURCES =			\
 	src/lib/thread/compat_threads.c			\
+	src/lib/thread/numcpus.c			\
 	$(threads_impl_source)
 
 src_lib_libtor_thread_testing_a_SOURCES = \
@@ -22,4 +23,5 @@ src_lib_libtor_thread_testing_a_CPPFLAGS = $(AM_CPPFLAGS) $(TEST_CPPFLAGS)
 src_lib_libtor_thread_testing_a_CFLAGS = $(AM_CFLAGS) $(TEST_CFLAGS)
 
 noinst_HEADERS +=					\
-	src/lib/thread/threads.h
+	src/lib/thread/threads.h			\
+	src/lib/thread/numcpus.h
diff --git a/src/lib/thread/numcpus.c b/src/lib/thread/numcpus.c
new file mode 100644
index 000000000..534b0570f
--- /dev/null
+++ b/src/lib/thread/numcpus.c
@@ -0,0 +1,93 @@
+/* Copyright (c) 2003-2004, Roger Dingledine
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "orconfig.h"
+#include "lib/thread/numcpus.h"
+#include "lib/log/torlog.h"
+#include "lib/log/util_bug.h"
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#ifdef _WIN32
+#include <windows.h>
+#endif
+
+#include <stdlib.h>
+
+/** Implementation logic for compute_num_cpus(). */
+static int
+compute_num_cpus_impl(void)
+{
+#ifdef _WIN32
+  SYSTEM_INFO info;
+  memset(&info, 0, sizeof(info));
+  GetSystemInfo(&info);
+  if (info.dwNumberOfProcessors >= 1 && info.dwNumberOfProcessors < INT_MAX)
+    return (int)info.dwNumberOfProcessors;
+  else
+    return -1;
+#elif defined(HAVE_SYSCONF)
+#ifdef _SC_NPROCESSORS_CONF
+  long cpus_conf = sysconf(_SC_NPROCESSORS_CONF);
+#else
+  long cpus_conf = -1;
+#endif
+#ifdef _SC_NPROCESSORS_ONLN
+  long cpus_onln = sysconf(_SC_NPROCESSORS_ONLN);
+#else
+  long cpus_onln = -1;
+#endif
+  long cpus = -1;
+
+  if (cpus_conf > 0 && cpus_onln < 0) {
+    cpus = cpus_conf;
+  } else if (cpus_onln > 0 && cpus_conf < 0) {
+    cpus = cpus_onln;
+  } else if (cpus_onln > 0 && cpus_conf > 0) {
+    if (cpus_onln < cpus_conf) {
+      log_notice(LD_GENERAL, "I think we have %ld CPUS, but only %ld of them "
+                 "are available. Telling Tor to only use %ld. You can over"
+                 "ride this with the NumCPUs option",
+                 cpus_conf, cpus_onln, cpus_onln);
+    }
+    cpus = cpus_onln;
+  }
+
+  if (cpus >= 1 && cpus < INT_MAX)
+    return (int)cpus;
+  else
+    return -1;
+#else
+  return -1;
+#endif /* defined(_WIN32) || ... */
+}
+
+#define MAX_DETECTABLE_CPUS 16
+
+/** Return how many CPUs we are running with.  We assume that nobody is
+ * using hot-swappable CPUs, so we don't recompute this after the first
+ * time.  Return -1 if we don't know how to tell the number of CPUs on this
+ * system.
+ */
+int
+compute_num_cpus(void)
+{
+  static int num_cpus = -2;
+  if (num_cpus == -2) {
+    num_cpus = compute_num_cpus_impl();
+    tor_assert(num_cpus != -2);
+    if (num_cpus > MAX_DETECTABLE_CPUS) {
+      /* LCOV_EXCL_START */
+      log_notice(LD_GENERAL, "Wow!  I detected that you have %d CPUs. I "
+                 "will not autodetect any more than %d, though.  If you "
+                 "want to configure more, set NumCPUs in your torrc",
+                 num_cpus, MAX_DETECTABLE_CPUS);
+      num_cpus = MAX_DETECTABLE_CPUS;
+      /* LCOV_EXCL_STOP */
+    }
+  }
+  return num_cpus;
+}
diff --git a/src/lib/thread/numcpus.h b/src/lib/thread/numcpus.h
new file mode 100644
index 000000000..2899a9ec8
--- /dev/null
+++ b/src/lib/thread/numcpus.h
@@ -0,0 +1,11 @@
+/* Copyright (c) 2003-2004, Roger Dingledine
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#ifndef TOR_NUMCPUS_H
+#define TOR_NUMCPUS_H
+
+int compute_num_cpus(void);
+
+#endif
diff --git a/src/or/config.c b/src/or/config.c
index 690025911..55dd1d7f8 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -117,6 +117,7 @@
 #include "lib/process/setuid.h"
 #include "lib/process/subprocess.h"
 #include "lib/net/gethostname.h"
+#include "lib/thread/numcpus.h"
 
 #include "lib/encoding/keyval.h"
 #include "lib/fs/conffile.h"
diff --git a/src/test/test_util.c b/src/test/test_util.c
index a6fb0ce84..bdc6fca7d 100644
--- a/src/test/test_util.c
+++ b/src/test/test_util.c
@@ -28,6 +28,7 @@
 #include "lib/process/pidfile.h"
 #include "lib/process/subprocess.h"
 #include "lib/intmath/weakrng.h"
+#include "lib/thread/numcpus.h"
 
 #ifdef HAVE_PWD_H
 #include <pwd.h>





More information about the tor-commits mailing list