View Single Post
Old 2017-02-23, 03:42   #1
Explorer09
 
May 2014

3·11 Posts
Default [Patch] CPU affinity prompt problem in mprime Linux / OS X build

[Patch] CPU affinity prompt problem in mprime Linux / OS X build

Steps to reproduce: suppose you have 2 CPU threads...

$ mv local.txt local.bak.txt
$ echo 'Affinity=98' >local.txt
$ mprime -m
(Choose "2. Test/Worker threads", then press Enter to accept default
for all questions and answer Y to "Accept the answers above", and then
choose "5. Test/Exit".)
$ grep Affinity local.txt
(Expected result: 'Affinity=98' unchanged)
(Actual result: 'Affinity=99' which means "any CPU" now instead of
specific CPU #98)

This off-by-one is ugly in asking CPU affinity from user. Suppose we label the
first CPU as #1 to user and second as #2 and so on, I would suggest using -1
and 0 for "any CPU" and "smart assignment" settings respecively here.

Also caps the (NUM_CPUS * CPU_HYPERTHREADS) number presented in question to 99,
because the local.txt format does not yet support pinning to 100th specific CPU
(CPU #99) or above.

Note: Windows GUI is not affected because it presents "any CPU" and
"smart assignment" as first of two items in the drop-down, avoiding the problem
that could happen in text prompt.

Code:
--- a/linux/menu.c	2016-09-15 10:15:10.000000000 +0800
+++ b/linux/menu.c	2017-02-23 11:00:36.432632390 +0800
@@ -450,16 +450,18 @@
 		}
 
 		if (NUM_CPUS * user_configurable_hyperthreads () > 1) {
-			char question[200];
-			unsigned long affinity;
+			char question[80];
+			long affinity;
+			int max_affinity;
+			max_affinity = NUM_CPUS * CPU_HYPERTHREADS >= 99U ? 99 : (int) (NUM_CPUS * CPU_HYPERTHREADS);
+			/* The values used in CPU_AFFINITY[] and local.txt are
+			 * 0-98=specific CPU, 99=any, 100=smart assignment */
 			sprintf (question,
-				 "CPU affinity (1-%d=specific CPU, 99=any CPU, 100=smart assignment)",
-				 (int) (NUM_CPUS * CPU_HYPERTHREADS));
-			affinity = m_affinity[i];
-			if (affinity < 99) affinity++;
-			askNum (question, &affinity, 1, 100);
-			if (affinity < 99) affinity--;
-			m_affinity[i] = affinity;
+				 "CPU affinity (1-%d=specific CPU, -1=any CPU, 0=smart assignment)",
+				 max_affinity);
+			affinity = m_affinity[i] >= 99 ? (long) m_affinity[i] - 100 : (long) m_affinity[i];
+			askInt (question, &affinity, -1, max_affinity);
+			m_affinity[i] = affinity < 1 ? affinity + 100 : affinity;
 		}
 
 		if (NUM_CPUS * user_configurable_hyperthreads () > 1) {
Explorer09 is offline   Reply With Quote