Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Debugging techniques in Elixir

Slide 3

Slide 3 text

debug |dēˈbəɡ| the process of identifying and removing errors from computer hardware or software.

Slide 4

Slide 4 text

https://twitter.com/cerealvelocity/status/575826348414885888

Slide 5

Slide 5 text

"Elixir leverages the Erlang VM" Erlang |> Elixir

Slide 6

Slide 6 text

Erlang/OTP has applications to debug and maintain your application

Slide 7

Slide 7 text

What I learned in my journey… • Tools used to debug applications. • When I can use each tool. • How I can analyze the system information. • How I connect to different nodes.

Slide 8

Slide 8 text

@erichkist

Slide 9

Slide 9 text

Sao Paulo - Brazil https://flic.kr/p/fyybDP

Slide 10

Slide 10 text

Organize the tour to catch pokémons and back all of us with life

Slide 11

Slide 11 text

consulting and software engineering Elixir coaching Elixir design review Custom development

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

http://elixir-lang.org/getting-started/introduction.html

Slide 14

Slide 14 text

http://elixir-lang.org/getting-started/introduction.html

Slide 15

Slide 15 text

IO.puts/2

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

iex(1)> IO.puts 1 1 :ok

Slide 18

Slide 18 text

iex(1)> IO.puts {:a, :b} ** (Protocol.UndefinedError) protocol String.Chars not implemented for {:a, :b} (elixir) lib/string/chars.ex:3: String.Chars.impl_for!/1 (elixir) lib/string/chars.ex:17: String.Chars.to_string/ 1 (elixir) lib/io.ex:500: IO.puts/2

Slide 19

Slide 19 text

IO.inspect/2

Slide 20

Slide 20 text

iex(1)> IO.inspect {:a, :b} {:a, :b} {:a, :b} Win! \o/

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

IO.puts vs IO.inspect

Slide 23

Slide 23 text

IO.puts: Chardata strings or lists of characters and strings

Slide 24

Slide 24 text

IO.inspect: Inspect protocol transform any data structure into a readable textual representation

Slide 25

Slide 25 text

http://elixir-lang.org/getting-started/protocols.html

Slide 26

Slide 26 text

“From now on I'll only use inspect"

Slide 27

Slide 27 text

|> Pipeability

Slide 28

Slide 28 text

1..10 |> Enum.take_random(3) |> Enum.map(&(&1 * 2))

Slide 29

Slide 29 text

list = Enum.take_random(1..10, 3) IO.inspect list list |> Enum.map(&(&1 * 2))

Slide 30

Slide 30 text

1..10 |> Enum.take_random(3) |> IO.inspect |> Enum.map(&(&1 * 2)) Awesome!!!

Slide 31

Slide 31 text

1..10 |> Enum.take_random(3) |> IO.inspect |> Enum.map(&(&1 * 2)) |> IO.inspect |> … |> IO.inspect |> … Awesome!!!

Slide 32

Slide 32 text

Inspect.Opts

Slide 33

Slide 33 text

1..10 |> Enum.take_random(3) |> IO.inspect(label: "list") |> Enum.map(&(&1 * 2)) list: [10, 2, 3] [20, 4, 6] Label

Slide 34

Slide 34 text

1..10 |> Enum.take_random(3) |> IO.inspect(label: “list") |> Enum.map(&(&1 * 2)) |> IO.inspect(label: “after multiply") |> … |> IO.inspect(label: “after foo") |> … Label

Slide 35

Slide 35 text

https://github.com/elixir-lang/elixir/commit/d6513a7 Label whatyouhide michalmuskala Thank you guys <3 Released?

Slide 36

Slide 36 text

IO.inspect conn Limit

Slide 37

Slide 37 text

%Plug.Conn{adapter: {Plug.Adapters.Cowboy.Conn, :...}, assigns: %{}, before_send: [#Function<0.7834419/1 in Plug.CSRFProtection.call/2>, #Function<4.100417920/1 in Phoenix.Controller.fetch_flash/2>, #Function<0.82590416/1 in Plug.Session.before_send/2>, #Function<1.75806487/1 in Plug.Logger.call/2>, #Function<0.96656268/1 in Phoenix.LiveReloader.before_send_inject_reloader/1>], body_params: %{}, cookies: %{}, halted: false, host: "localhost", method: "GET", owner: #PID<0.381.0>, params: %{}, path_info: [], peer: {{127, 0, 0, 1}, 63732}, port: 4000, private: %{Chat.Router => {[], %{}}, :phoenix_action => :index, :phoenix_controller => Chat.PageController, :phoenix_endpoint => Chat.Endpoint, :phoenix_flash => %{}, :phoenix_format => "html", :phoenix_layout => {Chat.LayoutView, :app}, :phoenix_pipelines => [:browser], :phoenix_route => #Function<1.12450572/1 in Chat.Router.match_route/4>, :phoenix_router => Chat.Router, :phoenix_view => Chat.PageView, :plug_session => %{}, :plug_session_fetch => :done}, query_params: %{}, query_string: "", remote_ip: {127, 0, 0, 1}, req_cookies: %{}, req_headers: [{"host", "localhost:4000"}, {"connection", "keep-alive"}, {"cache-control", "max-age=0"}, {"upgrade-insecure-requests", "1"}, {"user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"}, {"accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"}, {"accept-encoding", "gzip, deflate, sdch"}, Limit

Slide 38

Slide 38 text

IO.inspect conn, limit: 5 %Plug.Conn{adapter: {Plug.Adapters.Cowboy.Conn, :...}, assigns: %{}, before_send: [#Function<0.7834419/1 in Plug.CSRFProtection.call/2>, #Function<4.100417920/1 in Phoenix.Controller.fetch_flash/ 2>, ...], body_params: %{}, cookies: %{}, ...} Limit

Slide 39

Slide 39 text

http://elixir-lang.org/docs/stable/elixir/Inspect.Opts.html

Slide 40

Slide 40 text

“From now on I’m SURE I'll only use inspect"

Slide 41

Slide 41 text

Code debuging

Slide 42

Slide 42 text

IEx.pry/0 Inspect the state of a particular process

Slide 43

Slide 43 text

defmodule Example do def double(list) do list |> Enum.map(&(&1 * 2)) end end

Slide 44

Slide 44 text

require IEx + IEx.pry

Slide 45

Slide 45 text

require IEx defmodule Example do def double(list) do IEx.pry list |> Enum.map(&(&1 * 2)) end end

Slide 46

Slide 46 text

Things to keep in mind: 1) lexical scope (its bindings and process info) 2) after allowed the pry, shell will be reset 3) a new shell will be started after respawn

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

IEx.pry with Phoenix

Slide 49

Slide 49 text

chrismccord/phoenix_chat_example

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

defmodule Chat.RoomChannel do ... require IEx def handle_in("new:msg", msg, socket) do IEx.pry broadcast! socket, "new:msg", %{user: msg["user"], body: msg["body"]} {:reply, {:ok, %{msg: msg["body"]}}, assign(socket, :user, msg["user"])} end end web/channels/room_channel.ex

Slide 52

Slide 52 text

fail $ mix phoenix.server Cannot pry #PID<0.336.0> at web/channels/ room_channel.ex:44. Is an IEx shell running?

Slide 53

Slide 53 text

$ iex -S mix phoenix.server success

Slide 54

Slide 54 text

IEx.pry with tests

Slide 55

Slide 55 text

$ mix test Cannot pry #PID<0.244.0> at test/ chat_test.exs:7. Is an IEx shell running? fail

Slide 56

Slide 56 text

$ iex -S mix test success

Slide 57

Slide 57 text

It blocks the process Don’t use it in production

Slide 58

Slide 58 text

IEx.pry isn’t a tradicional debugger

Slide 59

Slide 59 text

:debugger Erlang Debugger for debugging and testing

Slide 60

Slide 60 text

iex(1)> :debugger.start() {:ok, #PID<0.87.0>}

Slide 61

Slide 61 text

You can set almost anything in the graphical tool

Slide 62

Slide 62 text

iex(2)> :int.ni(Chat.RoomChannel) {:module, Chat.RoomChannel}

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

It blocks the process Don’t use in production environment

Slide 67

Slide 67 text

Unlike some debuggers, you can’t change all the values of the bound variables…

Slide 68

Slide 68 text

pid, reference, binary, or port values cannot be changed

Slide 69

Slide 69 text

development & production mode

Slide 70

Slide 70 text

Code & Behavior debugging

Slide 71

Slide 71 text

https://flic.kr/p/8k9JhZ

Slide 72

Slide 72 text

Erlang VM

Slide 73

Slide 73 text

Multicore, Schedulers and Reductions

Slide 74

Slide 74 text

Symmetric Multi-Processing SMP takes full advantage of multiple CPU architectures

Slide 75

Slide 75 text

For every core, the BEAM virtual machine starts a thread that runs a scheduler

Slide 76

Slide 76 text

https://hamidreza-s.github.io/erlang/scheduling/real-time/preemptive/migration/2016/02/09/erlang-scheduler- details.html

Slide 77

Slide 77 text

Preemptive and reduction count

Slide 78

Slide 78 text

Scheduler working Queue Scheduler Done

Slide 79

Slide 79 text

Process done Queue Scheduler Done

Slide 80

Slide 80 text

Scheduler runs new process Queue Scheduler Done

Slide 81

Slide 81 text

Scheduler will run the process Process reduction count: 0 Scheduler reduction limit: 500 Scheduler fake values

Slide 82

Slide 82 text

process is running Process reduction count: 123 Scheduler reduction limit: 500 Scheduler fake values

Slide 83

Slide 83 text

process is running Process reduction count: 123 Scheduler reduction limit: 500 Scheduler cheap function fake values

Slide 84

Slide 84 text

process is running Process reduction count: 999 Scheduler reduction limit: 500 Scheduler expensive function fake values

Slide 85

Slide 85 text

Process is preempted Queue Scheduler Done

Slide 86

Slide 86 text

Scheduler working Queue Scheduler Done

Slide 87

Slide 87 text

Scheduler working Queue Scheduler Done

Slide 88

Slide 88 text

:observer Tracing and investigation of distributed systems

Slide 89

Slide 89 text

iex(1)> :observer.start() :ok

Slide 90

Slide 90 text

Common errors

Slide 91

Slide 91 text

CPU bottleneck

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

CPU is very hard to profile

Slide 95

Slide 95 text

The VM does a lot of work unrelated to processes when it comes to scheduling

Slide 96

Slide 96 text

To avoid going to sleep when work is low, the threads that control the Erlang schedulers will do busy looping

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

High reduction count usually means a high amount of CPU usage

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

No content

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

No content

Slide 104

Slide 104 text

Memory leak

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

Memory types

Slide 107

Slide 107 text

3 mistakes to avoid

Slide 108

Slide 108 text

ETS tables are never garbage collected Only removing records manually will reclaim memory

Slide 109

Slide 109 text

Don’t use dynamic atoms Atoms go in a global table and are cached forever!

Slide 110

Slide 110 text

Message queue the most common cause of failure

Slide 111

Slide 111 text

http://learnyousomeerlang.com/the-hitchhikers-guide-to-concurrency

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

Identifying errors

Slide 114

Slide 114 text

Crash Dump Travel machine!

Slide 115

Slide 115 text

erl_crash.dump

Slide 116

Slide 116 text

:observer > File > Examine Crashdump

Slide 117

Slide 117 text

No content

Slide 118

Slide 118 text

Tracing

Slide 119

Slide 119 text

:et, :dbg, :ttb (:observer) Some tracing modules in the Erlang

Slide 120

Slide 120 text

No content

Slide 121

Slide 121 text

No content

Slide 122

Slide 122 text

No content

Slide 123

Slide 123 text

No content

Slide 124

Slide 124 text

No content

Slide 125

Slide 125 text

No content

Slide 126

Slide 126 text

No content

Slide 127

Slide 127 text

No content

Slide 128

Slide 128 text

No content

Slide 129

Slide 129 text

andytill/erlyberly

Slide 130

Slide 130 text

No content

Slide 131

Slide 131 text

Connect to a remote node

Slide 132

Slide 132 text

It isn’t hard! but… you may find troubles.

Slide 133

Slide 133 text

module :observer can be not available Releases are self contained packages without unnecessary applications

Slide 134

Slide 134 text

iex(node@remote)1> :observer.start ** (UndefinedFunctionError) undefined function :observer.start/0 (module :observer is not available) :observer.start()

Slide 135

Slide 135 text

:runtime_tools low footprint tracing/debugging tools in a production system

Slide 136

Slide 136 text

def application do [mod: {Chat, []}, applications: [:runtime_tools, …]] end mix.exs

Slide 137

Slide 137 text

server without graphical interface

Slide 138

Slide 138 text

Name your remote node

Slide 139

Slide 139 text

## Name of the node -sname my_app ## Cookie for distributed erlang -setcookie my_cookie running-config/vm.args

Slide 140

Slide 140 text

Erlang Port Mapper Daemon daemon acts as a name server on all hosts involved in distributed Erlang computations

Slide 141

Slide 141 text

EPMD Port: 4369

Slide 142

Slide 142 text

$ iex --sname my_app@localhost --cookie my_cookie -S mix phoenix.server

Slide 143

Slide 143 text

EPMD Port: 4369 Port: 64722 my_app

Slide 144

Slide 144 text

$ epmd -names epmd: up and running on port 4369 with data: name my_app at port 64722

Slide 145

Slide 145 text

Run :observer in a standalone node to minimize the impact of the system being observed

Slide 146

Slide 146 text

$ iex --sname observer@localhost --cookie my_cookie --hidden -e ":observer.start"

Slide 147

Slide 147 text

EPMD Port: 4369 my_app Port: 64722 :observer Port: 64749

Slide 148

Slide 148 text

$ epmd -names epmd: up and running on port 4369 with data: name observer at port 64749 name my_app at port 64722

Slide 149

Slide 149 text

No content

Slide 150

Slide 150 text

SSH Port forwarding local EPMD port -> remote EPMD port

Slide 151

Slide 151 text

http://blog.plataformatec.com.br/2016/05/ tracing-and-observing-your-remote-node/

Slide 152

Slide 152 text

SASL System Architecture Support Libraries

Slide 153

Slide 153 text

Redirects supervisor, crash and progress reports to Logger

Slide 154

Slide 154 text

09:27:51.872 [info] Child Logger.ErrorHandler of Supervisor Logger.Supervisor started Pid: #PID<0.78.0> Start Call: Logger.Watcher.watcher(:error_logger, Logger.ErrorHandler, {true, true, 500}, :link) Restart: :permanent Shutdown: 5000 Type: :worker 09:27:51.884 [info] Application logger started at :nonode@nohost 09:27:52.174 [info] Child Mix.State of Supervisor Mix.Supervisor started Pid: #PID<0.86.0> Start Call: Mix.State.start_link() Restart: :permanent Shutdown: 5000 Type: :worker

Slide 155

Slide 155 text

$ iex --logger-sasl-reports true -S mix

Slide 156

Slide 156 text

def application do [mod: {Chat, []}, applications: [:sasl, …]] end mix.exs

Slide 157

Slide 157 text

Recap!

Slide 158

Slide 158 text

Some tools we can use only for development..

Slide 159

Slide 159 text

System information is different…

Slide 160

Slide 160 text

crash dump is your friend

Slide 161

Slide 161 text

You can run observer (and others tools) locally connected in your remote nodes!

Slide 162

Slide 162 text

Next steps

Slide 163

Slide 163 text

http://erlang.org/doc/applications.html

Slide 164

Slide 164 text

No content

Slide 165

Slide 165 text

h4cc/awesome-elixir

Slide 166

Slide 166 text

No content

Slide 167

Slide 167 text

"Measuring your Elixir Application” Talk by Renan Ranelli - Track 2 - 3:30PM

Slide 168

Slide 168 text

https://www.youtube.com/watch?v=4u6c2FNauYE

Slide 169

Slide 169 text

https://www.youtube.com/watch?v=mrKwM9g6baQ

Slide 170

Slide 170 text

https://www.erlang-in-anger.com/

Slide 171

Slide 171 text

Thank you! @erichkist https://speakerdeck.com/erichkist