File 2009-Fix-scheduler_SUITE-update_cpu_info-test-case.patch of Package erlang

From a6ccff7ba9659cfac6d94f49b2aa39faf13e7e7d Mon Sep 17 00:00:00 2001
From: Rickard Green <rickard@erlang.org>
Date: Fri, 17 Mar 2017 17:59:03 +0100
Subject: [PATCH] Fix scheduler_SUITE:update_cpu_info test-case

---
 erts/emulator/test/scheduler_SUITE.erl | 78 +++++++++++++++++++++++++++-------
 1 file changed, 62 insertions(+), 16 deletions(-)

diff --git a/erts/emulator/test/scheduler_SUITE.erl b/erts/emulator/test/scheduler_SUITE.erl
index b178dede5..885298ce3 100644
--- a/erts/emulator/test/scheduler_SUITE.erl
+++ b/erts/emulator/test/scheduler_SUITE.erl
@@ -807,21 +807,31 @@ update_cpu_info(Config) when is_list(Config) ->
 			      %% Nothing much to test; just a smoke test
 			      ok;
 			  Onln0 ->
-			      %% unset least significant bit
-			      Aff = (OldAff band (OldAff - 1)),
-			      set_affinity_mask(Aff),
-			      Onln1 = Avail - 1,
-			      case adjust_schedulers_online() of
-					{Onln0, Onln1} ->
-					    Onln1 = erlang:system_info(schedulers_online),
-					    receive after 500 -> ok end,
-					    io:format("TEST - Affinity mask: ~p - Schedulers online: ~p - Scheduler bindings: ~p~n",
-							    [Aff, Onln1, erlang:system_info(scheduler_bindings)]),
-					    unchanged = adjust_schedulers_online(),
-					    ok;
-					Fail ->
-					    ct:fail(Fail)
-				    end
+			      Cpus = bits_in_mask(OldAff),
+			      RmCpus = case Cpus > Onln0 of
+					   true -> Cpus - Onln0 + 1;
+					   false -> Onln0 - Cpus + 1
+				       end,
+			      Onln1 = Cpus - RmCpus,
+			      case Onln1 > 0 of
+				  false ->
+				      %% Nothing much to test; just a smoke test
+				      ok;
+				  true ->
+				      Aff = restrict_affinity_mask(OldAff, RmCpus),
+				      set_affinity_mask(Aff),
+				      case adjust_schedulers_online() of
+					  {Onln0, Onln1} ->
+					      Onln1 = erlang:system_info(schedulers_online),
+					      receive after 500 -> ok end,
+					      io:format("TEST - Affinity mask: ~p - Schedulers online: ~p - Scheduler bindings: ~p~n",
+							[Aff, Onln1, erlang:system_info(scheduler_bindings)]),
+					      unchanged = adjust_schedulers_online(),
+					      ok;
+					  Fail ->
+					      ct:fail(Fail)
+				      end
+			      end
 		      end
 		  after
 		      set_affinity_mask(OldAff),
@@ -835,13 +845,49 @@ update_cpu_info(Config) when is_list(Config) ->
 		  end
 	  end.
 
+bits_in_mask(Mask) ->
+    bits_in_mask(Mask, 0, 0).
+
+bits_in_mask(0, Shift, N) ->
+    N;
+bits_in_mask(Mask, Shift, N) ->
+    case Mask band (1 bsl Shift) of
+	0 -> bits_in_mask(Mask, Shift+1, N);
+	_ -> bits_in_mask(Mask band (bnot (1 bsl Shift)),
+			  Shift+1, N+1)
+    end.
+
+restrict_affinity_mask(Mask, N) ->
+    try
+	restrict_affinity_mask(Mask, 0, N)
+    catch
+	throw : Reason ->
+	    exit({Reason, Mask, N})
+    end.
+
+restrict_affinity_mask(Mask, _Shift, 0) ->
+    Mask;
+restrict_affinity_mask(0, _Shift, _N) ->
+    throw(overresticted_affinity_mask);
+restrict_affinity_mask(Mask, Shift, N) ->
+    case Mask band (1 bsl Shift) of
+	0 -> restrict_affinity_mask(Mask, Shift+1, N);
+	_ -> restrict_affinity_mask(Mask band (bnot (1 bsl Shift)),
+				    Shift+1, N-1)
+    end.
+
 adjust_schedulers_online() ->
     case erlang:system_info(update_cpu_info) of
 	unchanged ->
 	    unchanged;
 	changed ->
 	    Avail = erlang:system_info(logical_processors_available),
-	    {erlang:system_flag(schedulers_online, Avail), Avail}
+	    Scheds = erlang:system_info(schedulers),
+	    SOnln = case Avail > Scheds of
+	    	    	 true -> Scheds;
+			 false -> Avail
+		    end,
+	    {erlang:system_flag(schedulers_online, SOnln), SOnln}
     end.
 
 read_affinity(Data) ->
-- 
2.12.2

openSUSE Build Service is sponsored by