Slide 1

Slide 1 text

Elixir を⽀える技術 -「落ちない」システムの秘密に迫る Elixir: Under the Hood Takanori Ishikawa / builderscon tokyo 2019

Slide 2

Slide 2 text

Copyright © ABEJA, Inc. All rights reserved. Elixir: Under the Hood Elixir を⽀える技術 -「落ちない」システムの秘密に迫る

Slide 3

Slide 3 text

Copyright © ABEJA, Inc. All rights reserved. • @takanori_is • Takanori Ishikawa • ABEJA, inc. • Ex) mixi, Drecom • Developer in ABEJA Platform • ❤ Elixir and • https://github.com/ishikawa • Born in 愛媛県松⼭市

Slide 4

Slide 4 text

Copyright © ABEJA, Inc. All rights reserved. Summary • Erlang & Elixir design and principal • Systems that run forever • BEAM internal • Optimizations for BEAM

Slide 5

Slide 5 text

Copyright © ABEJA, Inc. All rights reserved. Erlang

Slide 6

Slide 6 text

Copyright © ABEJA, Inc. All rights reserved. History of Erlang • 1982 - 1985: Experiments using > 20 different languages • 1985 - 86: Experiments with Lisp,Prolog, Parlog • 1987: Erlang • 1993: Distribution • 1998: Open Sourced

Slide 7

Slide 7 text

Copyright © ABEJA, Inc. All rights reserved.

Slide 8

Slide 8 text

Copyright © ABEJA, Inc. All rights reserved. https://medium.com/elixirlabs/anatomy-of-an-elixir-process-878030c37166

Slide 9

Slide 9 text

Copyright © ABEJA, Inc. All rights reserved.

Slide 10

Slide 10 text

Copyright © ABEJA, Inc. All rights reserved. Elixir

Slide 11

Slide 11 text

Copyright © ABEJA, Inc. All rights reserved. Elixir Design Goals • Compatibility • the Erlang VM is Elixir’s strongest asset • Productivity • Extensibility

Slide 12

Slide 12 text

Copyright © ABEJA, Inc. All rights reserved.

Slide 13

Slide 13 text

Copyright © ABEJA, Inc. All rights reserved.

Slide 14

Slide 14 text

Copyright © ABEJA, Inc. All rights reserved.

Slide 15

Slide 15 text

Copyright © ABEJA, Inc. All rights reserved.

Slide 16

Slide 16 text

Copyright © ABEJA, Inc. All rights reserved.

Slide 17

Slide 17 text

Copyright © ABEJA, Inc. All rights reserved. https://blog.stenmans.org/theBeamBook/#ERTS

Slide 18

Slide 18 text

Copyright © ABEJA, Inc. All rights reserved. 「落ちないシステム」

Slide 19

Slide 19 text

Copyright © ABEJA, Inc. All rights reserved. 「100% バグがないシステムを 作れば落ちない!」

Slide 20

Slide 20 text

Copyright © ABEJA, Inc. All rights reserved. Systems run forever • Process Isolation • Fault tolerance • Self healing

Slide 21

Slide 21 text

Copyright © ABEJA, Inc. All rights reserved. The Erlang Virtual Machine: BEAM

Slide 22

Slide 22 text

Copyright © ABEJA, Inc. All rights reserved.

Slide 23

Slide 23 text

Copyright © ABEJA, Inc. All rights reserved.

Slide 24

Slide 24 text

Copyright © ABEJA, Inc. All rights reserved. The Erlang Virtual Machine: BEAM • BEAM (Bogumil’s/Björn’s Abstract Machine) • garbage collecting • reduction counting • non-preemptive • Interpreter loop: directly threaded • register machine

Slide 25

Slide 25 text

Copyright © ABEJA, Inc. All rights reserved.

Slide 26

Slide 26 text

Copyright © ABEJA, Inc. All rights reserved.

Slide 27

Slide 27 text

Copyright © ABEJA, Inc. All rights reserved. Process • Heap • Stack • Mailbox • PCB (Process Control Block)

Slide 28

Slide 28 text

Copyright © ABEJA, Inc. All rights reserved. https://hamidreza-s.github.io/erlang/scheduling/real-time/preemptive/migration/2016/02/09/erlang-scheduler- details.html Erlang VM +--------------------------------------------------------+ | | | +-----------------+-----------------+ | | | | | | | | Scheduler # 1 | Run Queue # 1 <--+ | | | | | | | | +-----------------+-----------------+ | | | | | | +-----------------+-----------------+ | | | | | | | | | | Scheduler # 2 | Run Queue # 2 <----> Migration | | | | | | Logic | | +-----------------+-----------------+ | | | | | | +-----------------+-----------------+ | | | | | | | | | | Scheduler # N | Run Queue # N <--+ | | | | | | | +-----------------+-----------------+ | | | +--------------------------------------------------------+

Slide 29

Slide 29 text

Copyright © ABEJA, Inc. All rights reserved. https://www.erlang-solutions.com/blog/erlang-garbage-collector.html

Slide 30

Slide 30 text

Copyright © ABEJA, Inc. All rights reserved. Optimization

Slide 31

Slide 31 text

Copyright © ABEJA, Inc. All rights reserved. HiPE • High Performance Erlang • Native code compiler for Erlang • Including LLVM backend • Compiles individual Erlang functions

Slide 32

Slide 32 text

Copyright © ABEJA, Inc. All rights reserved. BEAMJIT • Since 2014 • Tracing JIT • LLVM backend • Future: • BEAM registers to machine registers • Cross trace optimizations

Slide 33

Slide 33 text

Copyright © ABEJA, Inc. All rights reserved. BEAM improvements • Interpreter Overhaul • Instruction size reduced • New IR: SSA based

Slide 34

Slide 34 text

Copyright © ABEJA, Inc. All rights reserved. SSA • SSA (Static Single Assignment) • Each variable is assigned exactly once, • and every variable is defined before it is used. • @variable:5 • Simplifies compiler optimizations

Slide 35

Slide 35 text

Copyright © ABEJA, Inc. All rights reserved.

Slide 36

Slide 36 text

Copyright © ABEJA, Inc. All rights reserved.

Slide 37

Slide 37 text

Copyright © ABEJA, Inc. All rights reserved. Conclusion

Slide 38

Slide 38 text

Copyright © ABEJA, Inc. All rights reserved. Summary • Erlang & Elixir design and principal • Systems that run forever • BEAM internal • Optimizations for BEAM

Slide 39

Slide 39 text

Copyright © ABEJA, Inc. All rights reserved. Thanks!