4. Erlang History 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 “ Find the right methods - design by prototyping.” Mike Willliams, CS LAB Make mistakes on a small scale, not in a production project. Mike Willliams It’s not good enough to have ideas – you must also be able to implement them to know that they work Mike Willliams And the rest is history... Prototypes of Telecom applications First projects launched First products launched Major projects started OTP R1 released Released as Open Source Experiments started at the Computer Science Lab First Erlang Book Published First C-Based Virtual Machine
5. Erlang History And this is just the beginning... “ Erlang is going to be a very important language. It could be the next Java.” Ralph Johnson Co-author, “Design Patterns” (the “Gang-of-Four book”) To build a large scale message handling system that really had to be up all the time, I would unhesitatingly choose Erlang…. Tim Bray Sun Microsystems … if we had to start again we would probably use Erlang… Mike Shaver Mozilla Foundation 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 The Future January, 36,000 hits on erlang.org Bluetail AB acquired for 152M USD August, 1 million hits on erlang.org Erlang R11 goes multicore The first Erlang Factory Major new projects started within Ericsson ETC is founded
9. Erlang Highlights: Factorial Factorial using Recursion n! = 1 n*(n-1)! n = 0 n 1 Definition -module(ex1). -export([factorial/1]). factorial(0) -> 1; factorial(N) when N >= 1 -> N * factorial(N-1). Implementation Eshell V5.0.1 (abort with ^G) 1> c(ex1). {ok,ex1} 2> ex1:factorial(6). 720
10. Erlang Highlights: High-level Constructs -define(IP_VERSION, 4). -define(IP_MIN_HDR_LEN, 5). …… DgramSize = size(Dgram), <<?IP_VERSION:4, HLen:4, SrvcType:8, TotLen:16, ID:16, Flgs:3, FragOff:13, TTL:8, Proto:8, HdrChkSum:16, SrcIP:32, DestIP:32, Body/binary>> = Dgram, if (HLen >= 5) and (4*HLen =< DgramSize) -> OptsLen = 4*(HLen - ?IP_MIN_HDR_LEN), <<Opts:OptsLen/binary, Data/binary>> = Body, … .. end. Parsing an IP Datagram using the Bit Syntax
11.
12. Erlang Highlights: Concurrency Creating a new process using spawn -module(ex3). -export([activity/3]). activity(Name,Pos,Size) -> ………… Pid = spawn(ex3,activity,[Joe,75,1024]) activity(Joe,75,1024)
13. Erlang Highlights: Concurrency 10 100 1,000 10,000 100,000 Number of processes 1 10 100 1,000 Microseconds/process erlang java C# Source: Joe Armstrong SICS Process creation time
21. Erlang Highlights: Distribution loop() -> receive {From, {apply, M, F, A}} -> Answer = apply(M, F, A), From ! {rex, node(), Answer} loop(); _Other -> loop() end. {rex, Node} ! {self(), {apply, M, F, A}}, receive {rex, Node, What} -> What end {rex, Node} ! {self(), {apply, M, F, A}}, receive {rex, Node, What} -> What end {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. loop() -> receive {From, {apply, M, F, A}} -> Answer = apply(M, F, A), From ! {rex, node(), Answer} loop(); _Other -> loop() end. loop() -> receive {From, {apply, M, F, A}} -> Answer = apply(M, F, A), From ! {rex, node(), Answer} loop(); _Other -> loop() end. Simple Remote Procedure Call
22.
23.
24.
25.
26. Erlang SMP Erlang VM Scheduler run queue non-SMP VM Erlang VM Scheduler #2 run queue run queue run queue SMP VM Scheduler #1 Scheduler #N migration logic
27.
28.
29.
30. Erlang: It’s Happening! Amazon Simple DB Yahoo! Delicious T-Mobile WAP, SMS, IN services Facebook Chat channel servers Powerset/Microsoft Key/Value store 37 Signals Backend servers
31. Erlang: It’s Happening! CouchDB Distributed Robust document database Wings 3D 3D modeller based on Nendo YAWS Yet Another Web Server RabbitMQ High performance enterprise messaging Ejabberd XMPP instant messaging server
This is the essential loop needed for an RPC server. The server receives requests from clients, uses apply to evaluate the requests and then returns the answer to the originator. This is a simplified solution where no error handling is done. The BIF node/0 returns the node name.
Ulf Wiger, Ericsson AB 2009-01-16 ProTest - Property-Based Testing The non-SMP version of the Erlang virtual machine uses a cooperative reduction-counting scheduler with a single run queue (the run queue itself is partitioned according to priorities – ’low’, ’normal’, ’high’ and ’max’). Each runnable process gets a ”time slice” of 1000 reductions (1 reduction is basically one function call), but the time slice can be cut short if the process pauses to wait for a message (i.e. whenever a receive statement would block). There is also a yield() function, allowing processes to deliberately end the time slice. From an Erlang programmer’s perspective, the scheduling is preemptive, since it cannot be certain that it won’t be scheduled out at any given moment. In practice, it has been possible to stack the deck by calling yield() and then performing a sequence of calls that one wants to be ”atomic”. This is obviously questionable, and the practice has always been discouraged. Still, some programs have relied on the fact that the non-SMP scheduler was highly predictable – it was possible to ”know” that preemption wouldn’t occur at certain points in the code. (Another ”feature” was that the priorities ’high’ and ’max’ were strict – as long as there were runnable jobs on these priority levels, ’normal’ and ’low’ priority jobs would never get to execute. Partly for this reason, use of priorities is discourages in Erlang.)