lib: Refactor gathering CPU core count
authorJoe Stringer <joestringer@nicira.com>
Fri, 6 Dec 2013 07:42:20 +0000 (07:42 +0000)
committerEthan Jackson <ethan@nicira.com>
Tue, 10 Dec 2013 02:37:34 +0000 (18:37 -0800)
Signed-off-by: Joe Stringer <joestringer@nicira.com>
Signed-off-by: Ethan Jackson <ethan@nicira.com>
Acked-by: Ethan Jackson <ethan@nicira.com>
lib/ovs-thread.c
lib/ovs-thread.h
ofproto/ofproto.c
vswitchd/system-stats.c

index b3a87bb..02436e5 100644 (file)
@@ -309,4 +309,14 @@ may_fork(void)
 {
     return !must_not_fork;
 }
+\f
+/* Returns the total number of cores on this system, or 0 if the number cannot
+ * be determined. */
+unsigned int
+count_cpu_cores(void)
+{
+    long int n_cores = sysconf(_SC_NPROCESSORS_ONLN);
+
+    return n_cores > 0 ? n_cores : 0;
+}
 #endif
index 7f3195d..c6a7142 100644 (file)
@@ -496,5 +496,9 @@ pid_t xfork_at(const char *where);
 
 void forbid_forking(const char *reason);
 bool may_fork(void);
+\f
+/* Useful functions related to threading. */
+
+unsigned int count_cpu_cores(void);
 
 #endif /* ovs-thread.h */
index c4ce8a2..b282abe 100644 (file)
@@ -738,7 +738,7 @@ ofproto_set_n_handler_threads(unsigned limit)
     if (limit) {
         n_handler_threads = limit;
     } else {
-        int n_proc = sysconf(_SC_NPROCESSORS_ONLN);
+        int n_proc = count_cpu_cores();
         n_handler_threads = n_proc > 2 ? n_proc - 2 : 1;
     }
 }
index 2960b87..1d9cb78 100644 (file)
@@ -59,7 +59,7 @@ VLOG_DEFINE_THIS_MODULE(system_stats);
 static void
 get_cpu_cores(struct smap *stats)
 {
-    long int n_cores = sysconf(_SC_NPROCESSORS_ONLN);
+    long int n_cores = count_cpu_cores();
     if (n_cores > 0) {
         smap_add_format(stats, "cpu", "%ld", n_cores);
     }