From 0122f6e65c5f92da210d3fc024b781cc4faa8a6f Mon Sep 17 00:00:00 2001 From: Joe Stringer Date: Fri, 6 Dec 2013 07:42:20 +0000 Subject: [PATCH] lib: Refactor gathering CPU core count Signed-off-by: Joe Stringer Signed-off-by: Ethan Jackson Acked-by: Ethan Jackson --- lib/ovs-thread.c | 10 ++++++++++ lib/ovs-thread.h | 4 ++++ ofproto/ofproto.c | 2 +- vswitchd/system-stats.c | 2 +- 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/ovs-thread.c b/lib/ovs-thread.c index b3a87bb12..02436e5b5 100644 --- a/lib/ovs-thread.c +++ b/lib/ovs-thread.c @@ -309,4 +309,14 @@ may_fork(void) { return !must_not_fork; } + +/* 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 diff --git a/lib/ovs-thread.h b/lib/ovs-thread.h index 7f3195d69..c6a714269 100644 --- a/lib/ovs-thread.h +++ b/lib/ovs-thread.h @@ -496,5 +496,9 @@ pid_t xfork_at(const char *where); void forbid_forking(const char *reason); bool may_fork(void); + +/* Useful functions related to threading. */ + +unsigned int count_cpu_cores(void); #endif /* ovs-thread.h */ diff --git a/ofproto/ofproto.c b/ofproto/ofproto.c index c4ce8a2b9..b282abee4 100644 --- a/ofproto/ofproto.c +++ b/ofproto/ofproto.c @@ -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; } } diff --git a/vswitchd/system-stats.c b/vswitchd/system-stats.c index 2960b87e4..1d9cb78ed 100644 --- a/vswitchd/system-stats.c +++ b/vswitchd/system-stats.c @@ -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); } -- 2.43.0