Skip to content

Commit

Permalink
Fix cores detection for multiple single-core CPUs
Browse files Browse the repository at this point in the history
Systems with multiple physical CPUs where each CPU
only has one core doesn't return the same structure as
a system with one CPU and multiple cores in that CPU.

This fixes that case.

The original code expected this structure:
1> erlang:system_info(cpu_topology).
[{processor,[{core,[{thread,{logical,0}},
                    {thread,{logical,1}}]},
             {core,[{thread,{logical,2}},{thread,{logical,3}}]},
             {core,[{thread,{logical,4}},{thread,{logical,5}}]},
             {core,[{thread,{logical,6}},{thread,{logical,7}}]}]}]

...while on some single-core (yet with multiple physical CPUs),
the result of the same function looks like:

1> erlang:system_info(cpu_topology).
[{processor,{logical,0}},{processor,{logical,1}}]
  • Loading branch information
Tomas Morstein authored and mattsta committed Jun 8, 2013
1 parent 8514b5e commit 275dbc7
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/stdinout_pool_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ count_cpus(undefined, Count) ->
Count;
count_cpus([], Count) ->
Count;
count_cpus([{node, [{processor, Cores}]} | T], Count) ->
count_cpus([{node, [{processor, Cores}]} | T], Count) when is_list (Cores) ->
count_cpus(T, Count + length(Cores));
count_cpus([{processor, Cores} | T], Count) ->
count_cpus(T, Count + length(Cores)).
count_cpus([{node, [{processor, _}]} | T], Count) ->
count_cpus(T, Count + 1);
count_cpus([{processor, Cores} | T], Count) when is_list (Cores) ->
count_cpus(T, Count + length(Cores));
count_cpus([{processor, _} | T], Count) ->
count_cpus(T, Count + 1).

%%====================================================================
%% gen_server callbacks
Expand Down

0 comments on commit 275dbc7

Please sign in to comment.