生成新进程是否使用长生不老药中的所有 CPU 个核心
Does spawning new processes use all CPU cores in elixir
假设,我在 4 核 CPU 机器上。
如果我在我的 elixir VM 中 运行 以下内容:
1..4 |> Enum.map fn(x) -> spawn(computationally_heavy_process) end
这是否使用了我机器的所有 4 个内核。每个计算量大的进程之一?
好吧,elixir VM 就是 Erlang VM——它安排如何将生成的进程分配给机器 CPU。您没有直接控制权 AFAIK。但是,您可以通过在 运行 您自己的代码之前发出 :observer.start()
来使用图表的可视化来查看 4 个调度程序在 VM 中是如何被占用的。我发现它似乎做得很合理。对于现实检查,您还可以在 linux OS 上 运行 top
。这可以显示每个 cpu 实际使用了多少 - 尝试从同一台机器上的不同控制台在顶部按 1。
是的。默认情况下,Erlang 为每个 CPU 启动一个调度程序(OS 线程),并尝试在调度程序之间平均分配负载。但是,不能保证这四个进程最终会在四个不同的 CPU 中处理,因为通常并行发生的事情要多得多。如果你想知道 Erlang 启动了多少个调度器,只需启动 iex
(或 erl
)。
~$ iex
Erlang/OTP 17 [erts-6.4] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
...
例如,我的电脑CPU有8个核心。在输出的第一行,您可以看到当前配置。从[smp:8:8]
部分可以看出SMP is configured with 8 schedulers, 8 of which are online. The number of schedulers/threads can be overridden with the +S
flag.
+S Schedulers:SchedulerOnline
Sets the number of scheduler threads to create and scheduler threads
to set online when SMP support has been enabled. The maximum for both
values is 1024. If the Erlang runtime system is able to determine the
amount of logical processors configured and logical processors
available, Schedulers will default to logical processors configured,
and SchedulersOnline will default to logical processors available;
otherwise, the default values will be 1. [...]
也可以通过 :erlang.system_flag(:schedulers_online, n)
在 运行 时间更改在线调度程序的数量。但是,我建议不要更改任何默认值,除非您 运行 遇到特定问题。
假设,我在 4 核 CPU 机器上。
如果我在我的 elixir VM 中 运行 以下内容:
1..4 |> Enum.map fn(x) -> spawn(computationally_heavy_process) end
这是否使用了我机器的所有 4 个内核。每个计算量大的进程之一?
好吧,elixir VM 就是 Erlang VM——它安排如何将生成的进程分配给机器 CPU。您没有直接控制权 AFAIK。但是,您可以通过在 运行 您自己的代码之前发出 :observer.start()
来使用图表的可视化来查看 4 个调度程序在 VM 中是如何被占用的。我发现它似乎做得很合理。对于现实检查,您还可以在 linux OS 上 运行 top
。这可以显示每个 cpu 实际使用了多少 - 尝试从同一台机器上的不同控制台在顶部按 1。
是的。默认情况下,Erlang 为每个 CPU 启动一个调度程序(OS 线程),并尝试在调度程序之间平均分配负载。但是,不能保证这四个进程最终会在四个不同的 CPU 中处理,因为通常并行发生的事情要多得多。如果你想知道 Erlang 启动了多少个调度器,只需启动 iex
(或 erl
)。
~$ iex
Erlang/OTP 17 [erts-6.4] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
...
例如,我的电脑CPU有8个核心。在输出的第一行,您可以看到当前配置。从[smp:8:8]
部分可以看出SMP is configured with 8 schedulers, 8 of which are online. The number of schedulers/threads can be overridden with the +S
flag.
+S Schedulers:SchedulerOnline
Sets the number of scheduler threads to create and scheduler threads to set online when SMP support has been enabled. The maximum for both values is 1024. If the Erlang runtime system is able to determine the amount of logical processors configured and logical processors available, Schedulers will default to logical processors configured, and SchedulersOnline will default to logical processors available; otherwise, the default values will be 1. [...]
也可以通过 :erlang.system_flag(:schedulers_online, n)
在 运行 时间更改在线调度程序的数量。但是,我建议不要更改任何默认值,除非您 运行 遇到特定问题。