Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERL-933: Port usage is maxing out CPU when migrating from 21.1 to 21.3 #4000

Closed
OTP-Maintainer opened this issue May 3, 2019 · 2 comments
Closed
Labels
bug Issue is reported as a bug priority:medium team:VM Assigned to OTP team VM

Comments

@OTP-Maintainer
Copy link

Original reporter: josevalim
Affected version: OTP-21.3
Component: erts
Migrated from: https://bugs.erlang.org/browse/ERL-933


This bug has been found on Windows Subsystem for Linux with Erlang installed via ESL debian repos. It is unclear if it happens on regular Linux systems. The minimum reproduction case requires inotify_wait. The bug does not happen on 21.1, but it happens on 21.3.

{code:text}
$ uname -a
Linux <snip> 4.4.0-43-Microsoft #1-Microsoft Wed Dec 31 14:42:53 PST 2014 x86_64 x86_64 x86_64 GNU/Linux
{code}

Take the following Erlang code:

{code:erlang}
-module(fsdebug).
-compile(export_all).

start_link() ->
  gen_server:start_link(?MODULE, ok, []).

init(ok) ->
  BashArgs = ["-c", os:find_executable("inotifywait") ++ " $0 $@ & PID=$!; read a; kill -KILL $PID"],
  PortArgs = ["-e", "modify", "--quiet", "-m", "-r", "/tmp"],
  Port = open_port(
    {spawn_executable, "/bin/sh"},
    [stream, exit_status, {line, 16384}, {args, BashArgs ++ PortArgs}, {cd, "/tmp"}]
  ),
  link(Port),
  process_flag(trap_exit, true),
  {ok, Port}.

handle_info({Port, Event}, Port) ->
  io:format("~p~n", [Event]),
  {noreply, Port}.
{code}

Now run it:

{code:text}
$ erl
1> c("fsdebug").
fsdebug.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,fsdebug}
2> fsdebug:start_link().
{code}

And on another terminal, write to the "/tmp" directory

{code:text}
$ for i in $(seq 1 20); do echo "koko" >> /tmp/koko; sleep 0.5; done
{code}

On 21.1, everything works as expected, but on 21.3 it stops printing the events and CPU usage grows very high. The original issue was [reported here](https://github.com/phoenixframework/phoenix/issues/3365). We are trying to see if we can reduce it even further to something that does not require `inotify_wait`.


@OTP-Maintainer
Copy link
Author

josevalim said:

Here are some updates from the original issue.

1. The bug also happens on 21.2 (but not on 21.1)

2. The bug doesn't happen on native Linux (i.e. Ubuntu)

3. The bug can be reproduced by using `tail -f /tmp/koko` instead of inotify_wait. Here is an updated program:


{code:erlang}
-module(fsdebug).
-compile(export_all).

start_link() ->
  gen_server:start_link(?MODULE, ok, []).

init(ok) ->
  BashArgs = ["-c", os:find_executable("tail") ++ " $0 $@ & PID=$!; read a; kill -KILL $PID"],
  PortArgs = ["-f", "/tmp/koko"],
  Port = open_port(
    {spawn_executable, "/bin/sh"},
    [stream, exit_status, {line, 16384}, {args, BashArgs ++ PortArgs}, {cd, "/tmp"}]
  ),
  link(Port),
  process_flag(trap_exit, true),
  {ok, Port}.

handle_info({Port, Event}, Port) ->
  io:format("~p~n", [Event]),
  {noreply, Port}.
{code}

@OTP-Maintainer
Copy link
Author

sverker said:

I reproduced this and it seems to be caused by the WSL emulation of epoll not behaving correctly. Events repeatedly triggers falsely causing the 100% CPU.

One workaround is to build OTP with {{--disable-kernel-poll}} configuration option.

To do some code workaround for WSL kernel poll does not seem high enough prio as WSL2 is announced to contain a real Linux kernel with a real epoll implementation.
https://devblogs.microsoft.com/commandline/announcing-wsl-2/

@OTP-Maintainer OTP-Maintainer added bug Issue is reported as a bug team:VM Assigned to OTP team VM priority:medium labels Feb 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue is reported as a bug priority:medium team:VM Assigned to OTP team VM
Projects
None yet
Development

No branches or pull requests

1 participant