Скачать презентацию Practical Erlang Programming Simon Thompson University of Kent Скачать презентацию Practical Erlang Programming Simon Thompson University of Kent

4aad37d617e0f699c13c40bf30a513c9.ppt

  • Количество слайдов: 95

Practical Erlang Programming Simon Thompson University of Kent Course Introduction Course Title @ Course Practical Erlang Programming Simon Thompson University of Kent Course Introduction Course Title @ Course Author 2007

Contents • • Erlang History Erlang Highlights Open Telecom Platform Frequency Server Example Products Contents • • Erlang History Erlang Highlights Open Telecom Platform Frequency Server Example Products The Community Events & Questions Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 2

Erlang History: The Telecom Industry • • Routers, Switches Base Stations Network Infrastructure Cell Erlang History: The Telecom Industry • • Routers, Switches Base Stations Network Infrastructure Cell Phones Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 3

Telecom Applications: Issues • Complex • No down time • Scalable • Maintainable • Telecom Applications: Issues • Complex • No down time • Scalable • Maintainable • Distributed Past Single-service networks Present Multiservice networks/client server Content Services Control vs Communication applications Cellular PLMN CATV • Time to Market Backbone Network PSTN/ ISDN Data/ IP Networks Access transport and switching networks Practical Erlang Programming Media Gateways Access Clients/applications © 2001 -2009, Simon Thompson and Erlang Training and Consulting 4

Erlang History It’s not “Find enough to rest - – you good themistakes on Erlang History It’s not “Find enough to rest - – you good themistakes on a Make right methods And the have ideas design scale, not in a must also small by to implement them to be able prototyping. ” is history. . . know that they work production project. Mike Willliams, CS LAB Mike Willliams First projects launched Prototypes of Telecom applications 1987 1988 1989 Experiments started at the Computer Science Lab Practical Erlang Programming 1990 1991 1992 First products launched 1993 1994 First Erlang Book Published 1995 OTP R 1 released 1996 Major projects started © 2001 -2009, Simon Thompson and Erlang Training and Consulting 1997 1998 Released as Open Source 5

Erlang History “Erlang Next generation very important language. is going to be a high-performance, Erlang History “Erlang Next generation very important language. is going to be a high-performance, …if we had to start again we highly It could be the next Java. ” to be reliable web sites are going would And this is probably use Erlang… implemented using Erlang and Berkeley DB. just the beginning. . . Ralph Johnson Mike Shaver Dr. Margo Seltzer Co-author, “Design Patterns” (the “Gang-of-Four book”) Mozilla Foundation Original author, Berkeley DB January, 36, 000 hits on erlang. org 1999 2000 Major new projects started within Ericsson 2001 ETC is founded Bluetail AB acquired for 152 M USD Practical Erlang Programming 2002 2003 August, 1 million hits on erlang. org 2004 2005 Erlang R 11 goes multicore 2006 The first Erlang Factory San Francisco 2007 2008 The Future December 2 million hits on erlang. org © 2001 -2009, Simon Thompson and Erlang Training and Consulting 6

Erlang History: The Ancestors Concurrent languages like Functional languages Ada, Modula or Chill like Erlang History: The Ancestors Concurrent languages like Functional languages Ada, Modula or Chill like ML or Miranda Logic languages like Prolog Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 7

Contents • • Erlang History Erlang Highlights Open Telecom Platform Frequency Server Example Products Contents • • Erlang History Erlang Highlights Open Telecom Platform Frequency Server Example Products The Community Events & Questions Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 8

Erlang: Properties • Declarative Practical Erlang Programming Functional programming language, high abstraction level, pattern Erlang: Properties • Declarative Practical Erlang Programming Functional programming language, high abstraction level, pattern matching and concise readable programs © 2001 -2009, Simon Thompson and Erlang Training and Consulting 9

Erlang Highlights: Factorial using Recursion Implementation Definition 1 n! = n=0 n*(n-1)! n 1 Erlang Highlights: Factorial using Recursion Implementation Definition 1 n! = n=0 n*(n-1)! n 1 -module(ex 1). -export([factorial/1]). factorial(0) -> 1; factorial(N) when N >= 1 -> N * factorial(N-1). Eshell V 5. 0. 1 (abort with ^G) 1> c(ex 1). {ok, ex 1} 2> ex 1: factorial(6). 720 Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 10

Erlang Highlights: High-level Constructs Quick. Sort using List Comprehensions -module(ex 2). -export([qsort/1]). qsort([Head|Tail]) -> Erlang Highlights: High-level Constructs Quick. Sort using List Comprehensions -module(ex 2). -export([qsort/1]). qsort([Head|Tail]) -> First = qsort([X || X <- Tail, X =< Head]), Last = qsort([Y || Y <- Tail, Y > Head]), First ++ [Head|Last]; qsort([]) -> []. Eshell V 5. 0. 1 (abort with ^G) 1> c(ex 2). {ok, ex 2} 2> ex 2: qsort([7, 5, 3, 8, 1]). [1, 3, 5, 7, 8] Practical Erlang Programming "all objects Y taken from the list Tail, where Y > Head" © 2001 -2009, Simon Thompson and Erlang Training and Consulting 11

Erlang Highlights: High-level Constructs Parsing an IP Datagram using the Bit Syntax -define(IP_VERSION, 4). Erlang Highlights: High-level Constructs Parsing an IP Datagram using the Bit Syntax -define(IP_VERSION, 4). -define(IP_MIN_HDR_LEN, 5). …… Dgram. Size = size(Dgram), <> = Dgram, if (HLen >= 5) and (4*HLen =< Dgram. Size) -> Opts. Len = 4*(HLen - ? IP_MIN_HDR_LEN), <> = Body, …. . end. Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 12

Erlang Highlights: High-level Constructs Serialising tree data [8, 6, 2, cat, 2, dog, emu, Erlang Highlights: High-level Constructs Serialising tree data [8, 6, 2, cat, 2, dog, emu, fish] Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 13

Erlang Highlights: High-level Constructs Serialising tree data deserialize([_|Ls]) -> list. To. Tree(Ls). list. To. Erlang Highlights: High-level Constructs Serialising tree data deserialize([_|Ls]) -> list. To. Tree(Ls). list. To. Tree([2, N]) -> {leaf, N}; list. To. Tree([M|Rest] = Code) -> {Code 1, Code 2} = lists: split(M-1, Rest), {node, list. To. Tree(Code 1), list. To. Tree(Code 2) }. Practical Erlang Programming tree. To. List({leaf, N}) -> [2, N]; tree. To. List({node, T 1, T 2}) -> TTL 1 = tree. To. List(T 1), [Size 1|_] = TTL 1, TTL 2 = tree. To. List(T 2), [Size 2|List 2] = TTL 2, [Size 1+Size 2|TTL 1++List 2]. © 2001 -2009, Simon Thompson and Erlang Training and Consulting 14

Erlang Highlights: High-level Constructs Functions as arguments, expressions, results evens([]) -> []; evens([X|Xs]) -> Erlang Highlights: High-level Constructs Functions as arguments, expressions, results evens([]) -> []; evens([X|Xs]) -> case X rem 2 == 0 of true -> [X| evens(Xs)]; _ -> evens(Xs) end. filter(P, []) -> []; filter(P, [X|Xs]) -> case P(X) of true -> [X| filter(P, Xs)]; _ -> filter(P, Xs) end. evens([2, 3, 2, 1, 5, 4]) = [2, 2, 4] evens(Xs) -> filter (fun(X) -> X rem 2 == 0 end, Xs). Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 15

Erlang: Properties • Declarative Functional programming language, high abstraction level, pattern matching and concise Erlang: Properties • Declarative Functional programming language, high abstraction level, pattern matching and concise readable programs • Concurrency Either transparent or explicit concurrency, light-weight processes and highly scalable Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 16

Erlang Highlights: Concurrency Creating a new process using spawn -module(ex 3). -export([activity/3]). activity(Name, Pos, Erlang Highlights: Concurrency Creating a new process using spawn -module(ex 3). -export([activity/3]). activity(Name, Pos, Size) -> ………… activity(Joe, 75, 1024) Pid = spawn(ex 3, activity, [Joe, 75, 1024]) Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 17

Erlang Highlights: Concurrency 1, 000 Microseconds/process Process creation time erlang java C# 100 10 Erlang Highlights: Concurrency 1, 000 Microseconds/process Process creation time erlang java C# 100 10 Source: Joe Armstrong SICS 1 Number of processes Practical Erlang Programming 10 100 1, 000 10, 000 © 2001 -2009, Simon Thompson and Erlang Training and Consulting 100, 000 18

Erlang Highlights: Concurrency Processes communicate by asynchronous message passing Pid ! {data, 12, 13} Erlang Highlights: Concurrency Processes communicate by asynchronous message passing Pid ! {data, 12, 13} Practical Erlang Programming receive {start} -> ……… {stop} -> ……… {data, X, Y} -> ……… end © 2001 -2009, Simon Thompson and Erlang Training and Consulting 19

Erlang Highlights: Concurrency Processes communicate by asynchronous message passing Selective receive: Whatever the arrival Erlang Highlights: Concurrency Processes communicate by asynchronous message passing Selective receive: Whatever the arrival order of the messages receive {first, Payload 1} -> … process Payload 1 … receive {second, Payload 2} -> … process Payload 2 … end; Practical Erlang Programming {first, Payload 1} {second, Payload 2} the first is processed first. It won't deadlock if the second arrives first. © 2001 -2009, Simon Thompson and Erlang Training and Consulting 20

Erlang Highlights: Concurrency 10, 000 Microseconds/message Message passing times 100, 000 erlang java C# Erlang Highlights: Concurrency 10, 000 Microseconds/message Message passing times 100, 000 erlang java C# 1, 000 10 Source: Joe Armstrong SICS 1 1 Number of processes Practical Erlang Programming 10 100 1, 000 10, 000 © 2001 -2009, Simon Thompson and Erlang Training and Consulting 100, 000 21

Erlang: Properties • Declarative Functional programming language, high abstraction level, pattern matching and concise Erlang: Properties • Declarative Functional programming language, high abstraction level, pattern matching and concise readable programs • Concurrency Either transparent or explicit concurrency, light-weight processes and highly scalable • Soft real-time Response times in the order of milliseconds per-process garbage collection Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 22

Erlang: Properties • Declarative Functional programming language, high abstraction level, pattern matching and concise Erlang: Properties • Declarative Functional programming language, high abstraction level, pattern matching and concise readable programs • Concurrency Either transparent or explicit concurrency, light-weight processes and highly scalable • Soft real-time Response times in the order of milliseconds per-process garbage collection • Robustness Practical Erlang Programming Simple and consistent error recovery, supervision hierarchies and "Program for the correct case" © 2001 -2009, Simon Thompson and Erlang Training and Consulting 23

Erlang Highlights: Robustness Cooperating processes may be linked together using spawn_link(…, …, …) or Erlang Highlights: Robustness Cooperating processes may be linked together using spawn_link(…, …, …) or link(Pid) Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 24

Erlang Highlights: Robustness When a process terminates, an exit signal is sent to all Erlang Highlights: Robustness When a process terminates, an exit signal is sent to all linked processes … and the termination is propagated Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 25

Erlang Highlights: Robustness Exit signals can be trapped and received as messages receive {‘EXIT’, Erlang Highlights: Robustness Exit signals can be trapped and received as messages receive {‘EXIT’, Pid, . . . } ->. . . end Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 26

Erlang Highlights: Robustness Robust systems can be built by layering “Supervisors” “Workers” Practical Erlang Erlang Highlights: Robustness Robust systems can be built by layering “Supervisors” “Workers” Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 27

Erlang Highlights: Robustness Supervision hierarchies are policy-based. Supervision policies can govern • how many Erlang Highlights: Robustness Supervision hierarchies are policy-based. Supervision policies can govern • how many restarts in a given time • when to delegate restarts to your supervisor • do you restart your siblings? • … Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 28

Erlang: Properties • Declarative Functional programming language, high abstraction level, pattern matching and concise Erlang: Properties • Declarative Functional programming language, high abstraction level, pattern matching and concise readable programs • Concurrency Either transparent or explicit concurrency, light-weight processes and highly scalable • Soft real-time Response times in the order of milliseconds per-process garbage collection • Robustness Simple and consistent error recovery, supervision hierarchies and "Program for the correct case" • Distribution Explicit or transparent distribution Network-aware runtime system Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 29

Erlang Highlights: Distribution B ! Msg C ! Msg Erlang Run-Time System network Practical Erlang Highlights: Distribution B ! Msg C ! Msg Erlang Run-Time System network Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 30

Erlang Highlights: Distribution Simple Remote Procedure Call {rex, Node} ! {self(), {apply, M, F, Erlang Highlights: Distribution Simple Remote Procedure Call {rex, Node} ! {self(), {apply, M, F, A}}, receive {rex, Node, What} -> What end loop() -> receive {From, {apply, M, F, A}} -> Answer = apply(M, F, A), From ! {rex, node(), Answer} loop(); _Other -> loop() end.

Erlang Highlights: Distribution Built in support for distribution through socket-based libraries for SNMP, TCP, Erlang Highlights: Distribution Built in support for distribution through socket-based libraries for SNMP, TCP, HTTP, SSL, … … allowing the systems to have more security than the default distribution mechanism. Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 32

Erlang: Properties • Declarative Functional programming language, high abstraction level, pattern matching and concise Erlang: Properties • Declarative Functional programming language, high abstraction level, pattern matching and concise readable programs • Concurrency Either transparent or explicit concurrency, light-weight processes and highly scalable • Soft real-time Response times in the order of milliseconds per-process garbage collection • Robustness Simple and consistent error recovery, supervision hierarchies and "Program for the correct case" • Distribution Explicit or transparent distribution Network-aware runtime system • Hot code loading Practical Erlang Programming Easily change code in a running system. Enables nonstop operation Simplifies testing © 2001 -2009, Simon Thompson and Erlang Training and Consulting 33

Erlang Highlights: Hot Code Swap Version 1 Practical Erlang Programming Version 2 © 2001 Erlang Highlights: Hot Code Swap Version 1 Practical Erlang Programming Version 2 © 2001 -2009, Simon Thompson and Erlang Training and Consulting 34

Erlang: Properties • Declarative Functional programming language, high abstraction level, pattern matching and concise Erlang: Properties • Declarative Functional programming language, high abstraction level, pattern matching and concise readable programs • Concurrency Either transparent or explicit concurrency, light-weight processes and highly scalable • Soft real-time Response times in the order of milliseconds per-process garbage collection • Robustness Simple and consistent error recovery, supervision hierarchies and "Program for the correct case" • Distribution Explicit or transparent distribution Network-aware runtime system • Hot code loading • External interfaces Practical Erlang Programming Easily change code in a running system. Enables nonstop operation Simplifies testing "Ports" to the outside world behave as Erlang processes © 2001 -2009, Simon Thompson and Erlang Training and Consulting 35

Erlang Highlights: External Interfaces Port External process Port ! {self(), {command, [1, 2, 3]}} Erlang Highlights: External Interfaces Port External process Port ! {self(), {command, [1, 2, 3]}} Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 36

Erlang Highlights: External Interfaces Port External process receive {Port, {data, Info}} -> end Practical Erlang Highlights: External Interfaces Port External process receive {Port, {data, Info}} -> end Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 37

Erlang Highlights: Jinterface: Erlang+Java public class Server. Node { -module(myrpc). public static void main Erlang Highlights: Jinterface: Erlang+Java public class Server. Node { -module(myrpc). public static void main (String[] _args) throwsf(N) -> Exception{ Otp. Node bar = new Otp. Node("bar"); {facserver, 'bar@STC'} ! {self(), N}, Otp. Mbox mbox = bar. create. Mbox("facserver"); receive Otp. Erlang. Object o; {ok, Res} -> Otp. Erlang. Tuple msg; io: format("~p! is ~p. ~n", [N, Res]) Otp. Erlang. Pid from; end. Big. Integer n; Otp. Erlang. Atom ok = new Otp. Erlang. Atom("ok"); while(true) try { o = mbox. receive(); msg = (Otp. Erlang. Tuple)o; from = (Otp. Erlang. Pid)(msg. element. At(0)); n = ((Otp. Erlang. Long)(msg. element. At(1))). big. Integer. Value(); Otp. Erlang. Object[] reply = new Otp. Erlang. Object[2]; reply[0] = ok; reply[1] = new Otp. Erlang. Long(Factorial. factorial(n)); Otp. Erlang. Tuple tuple = new Otp. Erlang. Tuple(reply); mbox. send(from, tuple); } catch(Otp. Erlang. Exit e) { break; } } } Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 38

Erlang Highlights: Other interfaces • • C Ruby Python Emacs Lisp Perl Scheme Haskell Erlang Highlights: Other interfaces • • C Ruby Python Emacs Lisp Perl Scheme Haskell … Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 39

Erlang: Properties • Declarative Functional programming language, high abstraction level, pattern matching and concise Erlang: Properties • Declarative Functional programming language, high abstraction level, pattern matching and concise readable programs • Concurrency Either transparent or explicit concurrency, light-weight processes and highly scalable • Soft real-time Response times in the order of milliseconds per-process garbage collection • Robustness Simple and consistent error recovery, supervision hierarchies and "Program for the correct case" • Distribution Explicit or transparent distribution Network-aware runtime system • Hot code loading • External interfaces • Portability Practical Erlang Programming Easily change code in a running system. Enables nonstop operation Simplifies testing "Ports" to the outside world behave as Erlang processes Erlang runs on any UNIX, Windows, Vx Works, . . . Supports heterogeneous networks © 2001 -2009, Simon Thompson and Erlang Training and Consulting 40

Erlang: Properties • Declarative Functional programming language, high abstraction level, pattern matching and concise Erlang: Properties • Declarative Functional programming language, high abstraction level, pattern matching and concise readable programs • Concurrency Either transparent or explicit concurrency, light-weight processes and highly scalable • Soft real-time Response times in the order of milliseconds per-process garbage collection • Robustness Simple and consistent error recovery, supervision hierarchies and "Program for the correct case" • Distribution Explicit or transparent distribution Network-aware runtime system • Hot code loading • External interfaces Easily change code in a running system. Enables nonstop operation Simplifies testing "Ports" to the outside world behave as Erlang processes • Portability Erlang runs on any UNIX, Windows, Vx Works, . . . Supports heterogeneous networks • SMP Support Symmetric multiprocessing support. Takes full advantage of multiple CPU architectures. Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 41

Erlang Highlights: SMP Support The Industry’s Dilema • • The shift to multicore is Erlang Highlights: SMP Support The Industry’s Dilema • • The shift to multicore is inevitable. Parallelizing legacy C (and Java) code is very hard. Debugging parallelized C (and Java) is even harder. ”. . . but what choice do we have? ” Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 42

Erlang Highlights: SMP Support Erlang SMP ”Credo” SMP should be transparent to the programmer Erlang Highlights: SMP Support Erlang SMP ”Credo” SMP should be transparent to the programmer in much the same way as Erlang Distribution • You shouldn’t have to think about it. . . but sometimes you must • Use SMP mainly for stuff that you’d make concurrent anyway • Erlang uses concurrency as a structuring principle • Model for the natural concurrency in your problem Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 43

Erlang Highlights: SMP Support • Erlang on multicore • SMP prototype ‘ 97, First Erlang Highlights: SMP Support • Erlang on multicore • SMP prototype ‘ 97, First OTP release May ‘ 06. • Mid -06 we ran a benchmark mimicking call handling (axdmark) on the (experimental) SMP emulator. Observed speedup/core: 0. 95 • First Ericsson product (TGC) released on SMP Erlang in Q 207. ”Big bang” benchmark on Sunfire T 2000 1 scheduler 16 schedulers Simultaneous processes http: //www. franklinmint. fm/blog/archives/000792. html Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 44

Erlang Highlights: SMP Support Case Study: Telephony Gateway Controller AXE • • • TGC Erlang Highlights: SMP Support Case Study: Telephony Gateway Controller AXE • • • TGC Mediates between legacy telephony and multimedia networks. Hugely complex state machines + massive concurrency. GW GW GW Developed in Erlang. Multicore version shipped to customer Q 207. Porting from 1 -core PPC to 2 -core Inteltook < 1 man-year (including testing). Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 45

Erlang Highlights: SMP Support Case Study: Telephony Gateway Controller Traffic scenario POTS-POTS /AGW IS/GCP Erlang Highlights: SMP Support Case Study: Telephony Gateway Controller Traffic scenario POTS-POTS /AGW IS/GCP IS/GEP 1 slot/board Dual core One core running 2 slots/board Dual core Two cores running 2 slots/board X call/sec 2. 3 X call/sec One core used ISUP-ISUP /Inter MGW 3. 6 X call/sec 7. 7 X call/sec One core used ISUP-ISUP /Intra MGW 5. 5 X call/sec Practical Erlang Programming 4. 3 X call/sec OTP R 11_3 beta+patches 13 X call/sec OTP R 11_3 beta+patches 26 X call/sec AXD CPB 5 AXD CPB 6 0. 4 X call/sec 2. 1 X call/sec 1. 55 X call/sec 7. 6 X call/sec 3. 17 X call/sec 14 X call/sec © 2001 -2009, Simon Thompson and Erlang Training and Consulting 46

Erlang Highlights: SMP support • Chatty • 1000 processes created • Each process randomly Erlang Highlights: SMP support • Chatty • 1000 processes created • Each process randomly sends req/recieves ack from all other processes Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 47

Erlang Highlights: SMP Support non-SMP VM Erlang VM run queue Scheduler Practical Erlang Programming Erlang Highlights: SMP Support non-SMP VM Erlang VM run queue Scheduler Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 48

Erlang Highlights: SMP Support Current SMP VM OTP R 11/R 12 Erlang VM Scheduler Erlang Highlights: SMP Support Current SMP VM OTP R 11/R 12 Erlang VM Scheduler #1 run queue Scheduler #2 Scheduler #N Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 49

Erlang Highlights: SMP Support Latest SMP VM OTP R 13 Erlang VM Scheduler #1 Erlang Highlights: SMP Support Latest SMP VM OTP R 13 Erlang VM Scheduler #1 run queue Scheduler #2 Scheduler #N Practical Erlang Programming run queue migration logic run queue © 2001 -2009, Simon Thompson and Erlang Training and Consulting 50

Erlang Highlights: SMP Support Multiple run queues Speedup: Ca 0. 43 * N @ Erlang Highlights: SMP Support Multiple run queues Speedup: Ca 0. 43 * N @ 32 cores Memory allocation locks dominate. . . Single run queue • Speedup of ”Big Bang” on a Tilera Tile 64 chip (R 13α) • 1000 processes, all talking to each other Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 51

Contents • • Erlang History Erlang Highlights Open Telecom Platform Frequency Server Example Products Contents • • Erlang History Erlang Highlights Open Telecom Platform Frequency Server Example Products The Community Events & Questions Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 52

Open Telecom Platform Applications & Libraries System Design Principles T O P Practical Erlang Open Telecom Platform Applications & Libraries System Design Principles T O P Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 53

OTP: System Design Principles • A set of abstract principles and design rules. § OTP: System Design Principles • A set of abstract principles and design rules. § They describe the software architecture of an Erlang System § Needed so existing tools will be compatible with them § Facilitate understanding of the system among teams • A set of generic behaviours. § Each behaviour is a formalisation of a design pattern § Contains frameworks with generic code § Solve a common problem § Have built in support for debugging and software upgrade § Facilitate understanding of the sub blocks in the system Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 54

OTP: Design Patterns Server Callback module generic behaviour module • The idea is to OTP: Design Patterns Server Callback module generic behaviour module • The idea is to split the code in two parts. • The generic part is called the generic behaviour. § They are provided by OTP as library modules. • The specific part is called the callback module. § They are implemented by the programmer. Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 55

OTP: Design Patterns • Generic Servers § Used to model client server behaviour • OTP: Design Patterns • Generic Servers § Used to model client server behaviour • Generic Finite State Machines § Used for finite state machine programming • Generic Event Server / Manager § Used for writing event handlers • Supervisor § Used for fault-tolerant supervision trees • Application § Used to encapsulate resources and functionality Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 56

OTP: Design Patterns Application Supervisor Workers • An application, its supervision tree and its OTP: Design Patterns Application Supervisor Workers • An application, its supervision tree and its workers. • They can be implemented using generic behaviours. Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 57

OTP: Applications and Libraries • Basic Applications § Erlang Runtime System, Kernel, Compiler, Standard OTP: Applications and Libraries • Basic Applications § Erlang Runtime System, Kernel, Compiler, Standard Lib § System Architecture Support Library • Database Applications § Mnesia • Distributed relational database with query language § ODBC • Interface to accessing SQL databases • Operations and Maintenance Applications § Operating System Monitor § Simple Network Management Protocol • OTP MIBs, generic SNMP Erlang related MIBs • Corba Object Request Broker Applications Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 58

OTP: Applications and Libraries • Interface and Communications Applications § ASN 1 compiler § OTP: Applications and Libraries • Interface and Communications Applications § ASN 1 compiler § Crypto § Graphics System § Inets • Ip related services including TCP, UDP, HTTP and FTP § Java Interface § Megaco Stack • Megaco/H. 248 Stack § SSH, SSL § XML Parsing § Erlang to C Interface Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 59

OTP: Applications and Libraries • Tool Applications § Appmon, Pman, Debugger, Event Trace, Table OTP: Applications and Libraries • Tool Applications § Appmon, Pman, Debugger, Event Trace, Table Visualiser • Graphical debugging tools § Dialyzer • Type checking tool § Docbuilder, Edoc • Documentation tools § Invisio, Observer § Percept • Erlang Concurrency Profiler Tool § Parse & Syntax Tools • YECC Erlang Implementation • Handling abstract Erlang syntax trees § Runtime and Profiling Tools • Tools for debugging production systems • Tools for profiling, coverage analysis, and development support Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 60

OTP: Systems Overview OTP Applications written in Erlang Applications written in C, C++ or OTP: Systems Overview OTP Applications written in Erlang Applications written in C, C++ or Java OTP Components Standard Libraries Erlang Run-Time System Hardware and Operating System Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 61

Contents • • Erlang History Erlang Highlights Open Telecom Platform Frequency Server Example Products Contents • • Erlang History Erlang Highlights Open Telecom Platform Frequency Server Example Products The Community Events & Questions Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 62

Mobile Frequency Server • Scenario: allocation of frequencies for mobile communication. § A client Mobile Frequency Server • Scenario: allocation of frequencies for mobile communication. § A client can request that a frequency be allocated. § A client can deallocate a frequency. • Model § The frequency server is a separate process … § … communicating with the clients. • Alternatives § Communication mechanism? § Communication protocol? § Code the server yourself, or use OTP? Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 63

Frequency Allocation and Deallocation allocate({[], Allocated}, _Pid) -> {{[], Allocated}, {error, no_frequency}}; • A Frequency Allocation and Deallocation allocate({[], Allocated}, _Pid) -> {{[], Allocated}, {error, no_frequency}}; • A pair of lists models the free and allocated frequencies. allocate({[Freq|Free], Allocated}, Pid) -> {{Free, [{Freq, Pid}|Allocated]}, {ok, Freq}}. • Return the new pair plus a result message indicating failure or success. deallocate({Free, Allocated}, Freq) -> New. Allocated =lists: keydelete(Freq, 1, Allocated), {[Freq|Free], New. Allocated}. • Deallocation always succeeds, assuming the Freq was already allocated. • Nice example of pattern matching! Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 64

Server 1: everything explicit loop(Frequencies) -> receive {request, Pid, allocate} -> {New. Frequencies, Reply} Server 1: everything explicit loop(Frequencies) -> receive {request, Pid, allocate} -> {New. Frequencies, Reply} = allocate(Frequencies, Pid), Pid ! {reply, Reply}, loop(New. Frequencies); {request, Pid , {deallocate, Freq}} -> New. Frequencies = deallocate(Frequencies, Freq), Pid ! {reply, ok}, loop(New. Frequencies); {request, Pid, stop} -> Pid ! {reply, ok} end. Practical Erlang Programming • Messages are triples of § request, § process ID of the sender and § service required. • Replies are a pair § reply, § result (if any). • Then loop again, with (possibly) updated frequency data. • Pattern matching again! © 2001 -2009, Simon Thompson and Erlang Training and Consulting 65

Setting up the server start() -> register(frequency, spawn(frequency 0, init, [])). init() -> Frequencies Setting up the server start() -> register(frequency, spawn(frequency 0, init, [])). init() -> Frequencies = {get_frequencies(), []}, loop(Frequencies). % Hard Coded get_frequencies() -> [10, 11, 12, 13, 14, 15]. %% The Main Loop loop(Frequencies) -> receive {request, Pid, allocate} -> etc. Practical Erlang Programming • The system is started by spawning a process to run the init function … • … and then registering this process with the name frequency. • init itself will set up the loop data and call the loop for the first time. • DIY control. © 2001 -2009, Simon Thompson and Erlang Training and Consulting 66

Client 1: everything explicit 1> c(frequency). {ok, frequency} 2> frequency: start(). true 3> frequency Client 1: everything explicit 1> c(frequency). {ok, frequency} 2> frequency: start(). true 3> frequency ! {request, self(), allocate}. {request, <0. 40. 0>, allocate} 4> receive {reply, Reply} -> Reply end. {ok, 10} 5> … • Messages are triples of § request, § process ID of the sender and § service required. • Replies are a pair § reply, § result (if any). • Process structure and message protocol exposed. Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 67

Client 2: a functional interface allocate() -> frequency ! {request, self(), allocate}, receive {reply, Client 2: a functional interface allocate() -> frequency ! {request, self(), allocate}, receive {reply, Reply} -> Reply end. • A functional API for the operations hides the process information and message protocol deallocate(Freq) -> frequency ! {request, self(), {deallocate, Freq}}, receive {reply, Reply} -> Reply end. • The function sends the message and handles the reply. 1> frequency: start(). true 2> frequency: allocate(). {ok, 10} 3> … Practical Erlang Programming • Compare the example interaction. • Higher-level API but concurrent behaviour is still hand coded. © 2001 -2009, Simon Thompson and Erlang Training and Consulting 68

Building the server in OTP: gen_server • Generic server behaviour. § Concurrency, error recovery, Building the server in OTP: gen_server • Generic server behaviour. § Concurrency, error recovery, supervision protocols, timeouts, … are all handled generically. § In contrast to our spawn / register / init / loop implementation which was all hand coded, and did nothing beyond simple looping. • Specific behaviour is confined to § Message exchanges with the server. § Server setup and termination options. • The system has the same client API as before … another reason for the functional interface. Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 69

Setup and client API functions • The behavior implies the implementation of various callback Setup and client API functions • The behavior implies the implementation of various callback functions. -behavior(gen_server). start() -> gen_server: start_link({local, ? MODULE}, ? MODULE, get_frequencies(), []). stop() -> gen_server: cast(? MODULE, stop). %% The client API allocate() -> gen_server: call(? MODULE, allocate). • The server is started using one of the gen_server API functions. • Communication too, through a call to the gen_server. deallocate(Freq) -> gen_server: call(? MODULE, {deallocate, Freq}). • Practical Erlang Programming ? MODULE is the name used for the server. © 2001 -2009, Simon Thompson and Erlang Training and Consulting 70

The callback functions • These functions give the particular pieces of behaviour which specify The callback functions • These functions give the particular pieces of behaviour which specify setup, tear down and comms. init(Freq. List) -> Freqs = {Freq. List, []}, {ok, Freqs}. terminate(_, _) -> ok. handle_cast(stop, Freqs) -> {stop, normal, Freqs}. handle_call(allocate, From, Freqs) -> {New. Freqs, Reply} = allocate(Frequencies, From), {reply, Reply, New. Freqs}; • There's nothing here about the communication medium, but the protocol is limited to simple request / response. handle_call({deallocate, Freq}, _From, Freqs) -> • New. Freqs = deallocate(Freqs, Freq), {reply, ok, New. Freqs}. Practical Erlang Programming Can be synchronous or asynchronous. © 2001 -2009, Simon Thompson and Erlang Training and Consulting 71

Contents • • Erlang History Erlang Highlights Open Telecom Platform Frequency Server Example Products Contents • • Erlang History Erlang Highlights Open Telecom Platform Frequency Server Example Products The Community Events & Questions Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 72

Products: Ericsson • • Mobility Server (roaming over DECT networks) Teletrain (Call center solution) Products: Ericsson • • Mobility Server (roaming over DECT networks) Teletrain (Call center solution) ANx (Broadband access system) Net Sim (AXE simulator) AXD 301 (ATM Switch) GPRS (Packet switching over GSM) Integrated Site Telephony Gateway Controller Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 73

Products: AXD 301 Switch • A Telephony-Class, scalable (10 – 160 GBps) ATM switch Products: AXD 301 Switch • A Telephony-Class, scalable (10 – 160 GBps) ATM switch • Designed from scratch in less than 3 years • AXD 301 Success factors: § Competent organisation and people § Efficient process § Excellent technology (e. g. Erlang/OTP) Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 74

Products: AXD 301 Switch • Erlang: ca 1. 5 million lines of code § Products: AXD 301 Switch • Erlang: ca 1. 5 million lines of code § Nearly all the complex control logic § Operation & Maintenance § Web server and runtime HTML/ Java. Script generation • C/C++: ca 500 k lines of code § Third party software § Low-level protocol drivers § Device drivers • Java: ca 13 k lines of code § Operator GUI applets Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 75

Products: AXD 301 Switch • Using Erlang in Complex Systems § Fits very well Products: AXD 301 Switch • Using Erlang in Complex Systems § Fits very well with the incremental design method § High programmer satisfaction § Outstanding support for robustness and concurrency § Very few side-effects easier to add/change single components § Small directed teams can achieve impressive results • Productivity estimates § Similar line/hour programmer productivity § 4 -10 fewer lines of source code (compared to C/C++, Java, PLEX) § Similar number of faults per 1000 lines of source code Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 76

Products: AXD 301 Switch “Since cut-over of the first nodes in BT’s network in Products: AXD 301 Switch “Since cut-over of the first nodes in BT’s network in January 2002, only one minor fault has occurred, resulting in 99. 9999999% availability. ” "We a matter of fact, the network “As are extremely pleased with the outcome of the initial phase of this performance has been so reliable that project. almost a risk that our field there is This is major step in the phased development ofmaintenance skills” engineers do not learn what we believe is a world-leading Next Generation Network, " said Richard Newman, General Bert Nilsson, Director Manager of Planning and Delivery of NGS-Programs Ericsson Network Transport at BT Wholesale. Ericsson Press Release 5 July, 2002 Ericsson Contact, Issue 19 2002 Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 77

Products: Outside Ericsson • Telia (Sweden) Call Center Applications • Facebook (USA) Chat channel Products: Outside Ericsson • Telia (Sweden) Call Center Applications • Facebook (USA) Chat channel servers • Tail-F (Sweden) NETCONF, WEB, CLI, SNMP • Kreditor (Sweden), e-commerce • T-Mobile (UK) WAP, SMS, IN services • Teba Bank (South Africa) Commercial Banking Systems Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 78

Contents • • Erlang History Erlang Highlights Open Telecom Platform Frequency Server Example Products Contents • • Erlang History Erlang Highlights Open Telecom Platform Frequency Server Example Products The Community Events & Questions Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 79

Community: Downloads from erlang. org Practical Erlang Programming © 2001 -2009, Simon Thompson and Community: Downloads from erlang. org Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 80

Community: Page hits on erlang. org Practical Erlang Programming © 2001 -2009, Simon Thompson Community: Page hits on erlang. org Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 81

Community: Research • HIPE (Uppsala University, Sweden) • Telecom Programming (Heriot-Watt University, Scotland) • Community: Research • HIPE (Uppsala University, Sweden) • Telecom Programming (Heriot-Watt University, Scotland) • Major Projects (University of Corũna, Spain) • Code Refactoring (University of Kent, Canterbury, UK) • Code Refactoring (Eötvös Loránd University, Hungary) • Robots & Agents (University of Catania, Italy) Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 82

Community: Open Source • Couch. DB, A Distributed Robust document database server • Wings Community: Open Source • Couch. DB, A Distributed Robust document database server • Wings 3 D, a 3 D modeller based on Nendo • YAWS, Yet Another Web Server • Rabbit. MQ, high performance enterprise messaging • Ejabberd, instant messaging server • Erlyweb, component-based web development framework Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 83

Couch. DB • Schema-less database, document oriented. • Distributed, fault-tolerant, • Robust, incremental replication. Couch. DB • Schema-less database, document oriented. • Distributed, fault-tolerant, • Robust, incremental replication. • Bi-directional conflict detection and resolution. • Queryable and indexable using a table-oriented view engine. • Java. Script acting as the default view definition language. • Apache project … • … supported by IBM. Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 84

Community: Wings 3 D Examples from www. wings 3 d. com Practical Erlang Programming Community: Wings 3 D Examples from www. wings 3 d. com Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 85

Community: YAWS • YAWS thoughput (KBytes/second) vs. load. Practical Erlang Programming © 2001 -2009, Community: YAWS • YAWS thoughput (KBytes/second) vs. load. Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 86

Community: Web Sites www. planeterlang. org www. trapexit. org www. erlang-consulting. com www. erlang. Community: Web Sites www. planeterlang. org www. trapexit. org www. erlang-consulting. com www. erlang. org www. erlang-factory. com Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 87

Community: Blogs http: //armstrongonsoftware. blogspot. com/ http: //dukesoferl. blogspot. com http: //yarivsblog. com/ http: Community: Blogs http: //armstrongonsoftware. blogspot. com/ http: //dukesoferl. blogspot. com http: //yarivsblog. com/ http: //blog. tornkvist. org/ http: //planet. trapexit. org/ Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 88

Community: New Books • • Programming Erlang, § Software for a Concurrent World § Community: New Books • • Programming Erlang, § Software for a Concurrent World § by Joe Armstrong First English book in 10 years! § The previous one published in 2002 was in French! • Concurrent Programming with Erlang/OTP § by Martin Logan, Eric Merritt, Richard Carlsson and Robert Calco • Erlang Web Applications: Problem-Design-Solution § by Nick Gerakines Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 89

Shameless Advertisement …. Erlang Programming A Concurrent Approach to Software Development Francesco Cesarini Simon Shameless Advertisement …. Erlang Programming A Concurrent Approach to Software Development Francesco Cesarini Simon Thompson www. erlangprogramming. org Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 90

Contents • • Erlang History Erlang Highlights Open Telecom Platform Frequency Server Example Products Contents • • Erlang History Erlang Highlights Open Telecom Platform Frequency Server Example Products The Community Events & Questions Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 91

Events • ACM SIGPLAN Erlang Workshop § Held every year as a satellite event Events • ACM SIGPLAN Erlang Workshop § Held every year as a satellite event of the International Conference of Functional Programming § Alternates between the US and Europe § A forum to submit academic papers • International Erlang User Conference § Held every year in Stockholm, Sweden § Informal mixture of industrial applications and research Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 92

Events • Erlang Factory (www. erlang-factory. com) § Held twice a year in US Events • Erlang Factory (www. erlang-factory. com) § Held twice a year in US and Europe § A mix of talks, tutorials, close industrial connections § London June 24 th and 25 th, 2009 • Erl. Lounge (Noun) § A gathering of Erlang developers who meet to drink beer, have a bite and discuss Erlang (In that order). The were first held in Sweden in 2001 and are now regularly held worldwide. Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 93

Questions Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting Questions Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 94

Contents • • Erlang History Erlang Highlights Open Telecom Platform Frequency Server Example Products Contents • • Erlang History Erlang Highlights Open Telecom Platform Frequency Server Example Products The Community Events & Questions Practical Erlang Programming © 2001 -2009, Simon Thompson and Erlang Training and Consulting 95