diff mbox

[PATCHv3,8/9] helper: linux: add odph_linux_cpumask_default

Message ID 1421275706-11176-9-git-send-email-anders.roxell@linaro.org
State Accepted
Commit 5e47a0cf301367e47a3e1dfefbd6c5b4677916b8
Headers show

Commit Message

Anders Roxell Jan. 14, 2015, 10:48 p.m. UTC
From: Robbie King <robking@cisco.com>

A default cpumask based on number of requested CPUs and bounded by
number of CPUs in the system.

Signed-off-by: Robbie King <robking@cisco.com>
Signed-off-by: Anders Roxell <anders.roxell@linaro.org>
---
 helper/include/odph_linux.h        | 11 +++++++++++
 platform/linux-generic/odp_linux.c | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)
diff mbox

Patch

diff --git a/helper/include/odph_linux.h b/helper/include/odph_linux.h
index 26ff278..6458fde 100644
--- a/helper/include/odph_linux.h
+++ b/helper/include/odph_linux.h
@@ -22,6 +22,7 @@ 
 extern "C" {
 #endif
 
+#include <odp.h>
 
 #include <pthread.h>
 #include <sys/types.h>
@@ -50,6 +51,16 @@  typedef struct {
 	int   status;   /**< Process state change status */
 } odph_linux_process_t;
 
+/**
+ * Creates default pthread/process cpumask
+ *
+ * Creates cpumask based on starting count, actual value returned
+ *
+ * @param mask          CPU mask to initialize
+ * @param num           Number of threads to create, zero for all available
+ * @return Actual values of CPUs used to create mask
+ */
+int odph_linux_cpumask_default(odp_cpumask_t *mask, int num);
 
 /**
  * Creates and launches pthreads
diff --git a/platform/linux-generic/odp_linux.c b/platform/linux-generic/odp_linux.c
index ba1da1f..a051024 100644
--- a/platform/linux-generic/odp_linux.c
+++ b/platform/linux-generic/odp_linux.c
@@ -25,6 +25,41 @@ 
 #include <odp_system_info.h>
 #include <odp_debug_internal.h>
 
+int odph_linux_cpumask_default(odp_cpumask_t *mask, int num_in)
+{
+	int i;
+	int first_cpu = 1;
+	int num = num_in;
+	int cpu_count;
+
+	cpu_count = odp_sys_cpu_count();
+
+	/*
+	 * If no user supplied number or it's too large, then attempt
+	 * to use all CPUs
+	 */
+	if (0 == num)
+		num = cpu_count;
+	if (cpu_count < num)
+		num = cpu_count;
+
+	/*
+	 * Always force "first_cpu" to a valid CPU
+	 */
+	if (first_cpu >= cpu_count)
+		first_cpu = cpu_count - 1;
+
+	/* Build the mask */
+	odp_cpumask_zero(mask);
+	for (i = 0; i < num; i++) {
+		int cpu;
+
+		cpu = (first_cpu + i) % cpu_count;
+		odp_cpumask_set(mask, cpu);
+	}
+
+	return num;
+}
 
 static void *odp_run_start_routine(void *arg)
 {