diff mbox

[1/2] linux-generic: cpumask_task: use cpumask got at init

Message ID 1453501492-9721-2-git-send-email-ivan.khoronzhuk@linaro.org
State New
Headers show

Commit Message

Ivan Khoronzhuk Jan. 22, 2016, 10:24 p.m. UTC
It's not correct to read affinity each time to get worker
cpumask. The affinity for main thread can be changed, but
cpunum is still for old cpumask. So, better to remember at
init cpumask for available cpus and then use it, thus,
if main thread changed it`s affinity, the available CPUs
are still the same.

Signed-off-by: Ivan Khoronzhuk <ivan.khoronzhuk@linaro.org>
---
 platform/linux-generic/include/odp_internal.h |  3 +++
 platform/linux-generic/odp_cpumask_task.c     |  9 +++------
 platform/linux-generic/odp_system_info.c      | 22 ++++++++++++++--------
 3 files changed, 20 insertions(+), 14 deletions(-)
diff mbox

Patch

diff --git a/platform/linux-generic/include/odp_internal.h b/platform/linux-generic/include/odp_internal.h
index b22f956..b795af6 100644
--- a/platform/linux-generic/include/odp_internal.h
+++ b/platform/linux-generic/include/odp_internal.h
@@ -20,6 +20,7 @@  extern "C" {
 
 #include <odp/init.h>
 #include <odp/thread.h>
+#include <sched.h>
 
 extern __thread int __odp_errno;
 
@@ -30,6 +31,7 @@  typedef struct {
 	int      cache_line_size;
 	int      cpu_count;
 	char     model_str[128];
+	cpu_set_t cpuset;
 } odp_system_info_t;
 
 struct odp_global_data_s {
@@ -61,6 +63,7 @@  int _odp_term_local(enum init_stage stage);
 
 int odp_system_info_init(void);
 int odp_system_info_term(void);
+cpu_set_t _odp_cpuset(void);
 
 int odp_thread_init_global(void);
 int odp_thread_init_local(odp_thread_type_t type);
diff --git a/platform/linux-generic/odp_cpumask_task.c b/platform/linux-generic/odp_cpumask_task.c
index 41f2bc9..43a5281 100644
--- a/platform/linux-generic/odp_cpumask_task.c
+++ b/platform/linux-generic/odp_cpumask_task.c
@@ -10,19 +10,16 @@ 
 #include <pthread.h>
 
 #include <odp/cpumask.h>
+#include <odp_internal.h>
 #include <odp_debug_internal.h>
 
 int odp_cpumask_default_worker(odp_cpumask_t *mask, int num)
 {
-	int ret, cpu, i;
+	int cpu, i;
 	cpu_set_t cpuset;
 
-	ret = pthread_getaffinity_np(pthread_self(),
-				     sizeof(cpu_set_t), &cpuset);
-	if (ret != 0)
-		ODP_ABORT("failed to read CPU affinity value\n");
-
 	odp_cpumask_zero(mask);
+	cpuset = _odp_cpuset();
 
 	/*
 	 * If no user supplied number or it's too large, then attempt
diff --git a/platform/linux-generic/odp_system_info.c b/platform/linux-generic/odp_system_info.c
index a948fce..395e6b7 100644
--- a/platform/linux-generic/odp_system_info.c
+++ b/platform/linux-generic/odp_system_info.c
@@ -24,8 +24,6 @@ 
 #include <sys/types.h>
 #include <dirent.h>
 
-
-
 typedef struct {
 	const char *cpu_arch_str;
 	int (*cpuinfo_parser)(FILE *file, odp_system_info_t *sysinfo);
@@ -41,17 +39,20 @@  typedef struct {
 /*
  * Report the number of CPUs in the affinity mask of the main thread
  */
-static int sysconf_cpu_count(void)
+static int sysconf_cpu_count(cpu_set_t *cpuset)
 {
-	cpu_set_t cpuset;
+	cpu_set_t tmp;
 	int ret;
 
+	CPU_ZERO(&tmp);
 	ret = pthread_getaffinity_np(pthread_self(),
-				     sizeof(cpuset), &cpuset);
+				     sizeof(cpuset), &tmp);
 	if (ret != 0)
 		return 0;
 
-	return CPU_COUNT(&cpuset);
+	*cpuset = tmp;
+
+	return CPU_COUNT(cpuset);
 }
 
 #if defined __x86_64__ || defined __i386__ || defined __OCTEON__ || \
@@ -282,7 +283,7 @@  static int systemcpu(odp_system_info_t *sysinfo)
 {
 	int ret;
 
-	ret = sysconf_cpu_count();
+	ret = sysconf_cpu_count(&sysinfo->cpuset);
 	if (ret == 0) {
 		ODP_ERR("sysconf_cpu_count failed.\n");
 		return -1;
@@ -320,7 +321,7 @@  static int systemcpu(odp_system_info_t *sysinfo)
 {
 	int ret;
 
-	ret = sysconf_cpu_count();
+	ret = sysconf_cpu_count(&sysinfo->cpuset);
 	if (ret == 0) {
 		ODP_ERR("sysconf_cpu_count failed.\n");
 		return -1;
@@ -370,6 +371,11 @@  int odp_system_info_init(void)
 	return 0;
 }
 
+cpu_set_t _odp_cpuset(void)
+{
+	return odp_global_data.system_info.cpuset;
+}
+
 /*
  * System info termination
  */