KILLER features of the BEAM. And what makes the BEAM a unique and powerful tool that really stands out!

Similar documents
Introduction to Erlang. Franck Petit / Sebastien Tixeuil

Mechanized Operational Semantics

The HALFWORD HEAP EMULATOR

Parallel Programming in Erlang (PFP Lecture 10) John Hughes

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Compiler Construction Lent Term 2013 Lectures 9 & 10 (of 16)

Last week. Data on the stack is allocated automatically when we do a function call, and removed when we return

BEAM VM Wisdoms. Release 1.0. Dmytro Lytovchenko

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

Erlang 101. Google Doc

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

Dispatch techniques and closure representations

Lecture 4: Mechanism of process execution. Mythili Vutukuru IIT Bombay

So far. Next: scheduling next process from Wait to Run. 1/31/08 CSE 30341: Operating Systems Principles

Processes, PCB, Context Switch

Computer Architecture Review CS 595

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

Performance Optimization 101. Louis-Philippe Gauthier Team AdGear Trader

Programming Language Implementation

Erlang: An Overview. Part 2 Concurrency and Distribution. Thanks to Richard Carlsson for most of the slides in this part

Sista: Improving Cog s JIT performance. Clément Béra

CS 11 C track: lecture 8

Erlang Concepts. Programming for Beginners, Summer 2011

Run-time Environment

Processes (Intro) Yannis Smaragdakis, U. Athens

Data Structure. Recitation VII

Arrays and Linked Lists

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 22: Remote Procedure Call (RPC)

Course: Operating Systems Instructor: M Umair. M Umair

CS 111. Operating Systems Peter Reiher

6.172 Performance Engineering of Software Systems Spring Lecture 9. P after. Figure 1: A diagram of the stack (Image by MIT OpenCourseWare.

CS3330 Exam 3 Spring 2015

Pointer Analysis in the Presence of Dynamic Class Loading. Hind Presented by Brian Russell

Older geometric based addressing is called CHS for cylinder-head-sector. This triple value uniquely identifies every sector.

Python Implementation Strategies. Jeremy Hylton Python / Google

What are compilers? A Compiler

Run-time Program Management. Hwansoo Han

MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science. Issued: Wed. 26 April 2017 Due: Wed.

Jatha. Common Lisp in Java. Ola Bini JRuby Core Developer ThoughtWorks Studios.

TinyVM: User's Guide

CS 211. Project 5 Infix Expression Evaluation

HiPE Copyright Ericsson AB. All Rights Reserved. HiPE March 11, 2019

Kasper Lund, Software engineer at Google. Crankshaft. Turbocharging the next generation of web applications

Algorithms and Data Structures

Creating Object Classes in True BASIC

Processes. OS Structure. OS Structure. Modes of Execution. Typical Functions of an OS Kernel. Non-Kernel OS. COMP755 Advanced Operating Systems

CS 111. Operating Systems Peter Reiher

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - III Processes. Louisiana State University. Processes. September 1 st, 2009

Tick: Concurrent GC in Apache Harmony

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

Java Without the Jitter

Operating Systems and Protection CS 217

Course Syllabus. Course Title. Who should attend? Course Description. PHP ( Level 1 (

Protection and System Calls. Otto J. Anshus

regsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13

Garbage Collection. Hwansoo Han

Towards Lean 4: Sebastian Ullrich 1, Leonardo de Moura 2.

The role of semantic analysis in a compiler

CSC369 Lecture 2. Larry Zhang

Tizen/Artik IoT Lecture Chapter 3. JerryScript Parser & VM

CSC369 Lecture 2. Larry Zhang, September 21, 2015

Q.1 Explain Computer s Basic Elements

High-Level Language VMs

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Last Time. Think carefully about whether you use a heap Look carefully for stack overflow Especially when you have multiple threads

CS558 Programming Languages

Azul Systems, Inc.

Precept 2: Non-preemptive Scheduler. COS 318: Fall 2018

Program Exploitation Intro

register allocation saves energy register allocation reduces memory accesses.

Offensive Security My First Buffer Overflow: Tutorial

Motivation. Threads. Multithreaded Server Architecture. Thread of execution. Chapter 4

CPSC 341 OS & Networks. Processes. Dr. Yingwu Zhu

Systems I. Machine-Level Programming V: Procedures

ò Paper reading assigned for next Tuesday ò Understand low-level building blocks of a scheduler User Kernel ò Understand competing policy goals

Administration CS 412/413. Why build a compiler? Compilers. Architectural independence. Source-to-source translator

Operating Systems Comprehensive Exam. Spring Student ID # 3/20/2013

Introduction to Erlang. Franck Petit / Sebastien Tixeuil

Postfix (and prefix) notation

Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

Erlang: distributed programming

Compiling Techniques

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1

CS 326: Operating Systems. Process Execution. Lecture 5

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

COMP 3430 Robert Guderian

Recursive Data Structures and Grammars

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Performance Analysis of Java Communications with and without CORBA

THREADS: (abstract CPUs)

Let s make release upgrades great again!

Symbolic Computation and Common Lisp

Java Internals. Frank Yellin Tim Lindholm JavaSoft

Memory Management. 3. What two registers can be used to provide a simple form of memory protection? Base register Limit Register

Intro to x86 Binaries. From ASM to exploit

CS W3134: Data Structures in Java

C++ for System Developers with Design Pattern

GUJARAT TECHNOLOGICAL UNIVERSITY MASTER OF COMPUTER APPLICATION SEMESTER: III

In Java we have the keyword null, which is the value of an uninitialized reference type

Transcription:

KILLER features of the BEAM And what makes the BEAM a unique and powerful tool that really stands out!

Actor Model Hundreds, Thousands, Millions of processes System limit can be between1,024-134,217,727 They share NOTHING Communicate through message passing

Demo 1 A hypothetical server

JUGGLER a simplified DEMO SERVER https://github.com/iguberman/erljuggler_demo accept_loop() -> receive %% to get a message in my mailbox Req -> dispatch(req, self()) %% self() = my process id end, accept_loop(). accept(numrequests)-> [dispatch(req, self()) Req <- lists:seq(1, NumRequests)], collect_responses(0, 0, 0, 0, NumRequests). dispatch(req, AcceptorPid) -> spawn(?module, %% Module kick_off_request_handler, %% Function [Req, AcceptorPid]). %% Arguments

JUGGLER a simplified DEMO SERVER https://github.com/iguberman/erljuggler_demo %%% a kind of a supervisor kick_off_request_handler(req, AcceptorPid) -> RequestHandlerPid = spawn(?module, handle_request, [Req, self()]), Start = os:system_time(millisecond), receive {RequestHandlerPid, Resp} -> End = os:system_time(millisecond), Duration = End - Start, io:format("...~p [~b]...", [Req, Duration]), AcceptorPid! {RequestHandlerPid, Resp, Duration}; end. Unexpected -> AcceptorPid! {error, Unexpected}

JUGGLER a simplified DEMO SERVER https://github.com/iguberman/erljuggler_demo handle_request(req, ParentPid) when is_integer(req) -> Resp = count_to_1000_and_do_other_stuff_too(req, 0), HandlerPid = self(), ParentPid! {HandlerPid, Resp} end. count_to_1000_and_do_other_stuff_too(_req, 1000) -> ok; count_to_1000_and_do_other_stuff_too(req, C) -> case (Req rem 2) of 0 -> binary:copy(<<req/integer>>,300); 1 -> binary:copy(<<(req + 1)/integer>>,200) end, count_to_1000_and_do_other_stuff_too(req, C+1).

Demo 2 server with a bug

JUGGLER a simplified DEMO SERVER https://github.com/iguberman/erljuggler_demo handle_request(req, ParentPid) when is_integer(req) -> case Req rem 100 of 0 -> io:format("~n**** ~p [INF]*****~n", [Req]), ParentPid! dont_wait_for_me, handle_with_inf_loop_bug(); _Other -> Resp = count_to_1000_and_do_other_stuff_too(req, 0), HandlerPid = self(), ParentPid! {HandlerPid, Resp} end. end. handle_with_inf_loop_bug()-> infinite_loop(0). infinite_loop(c) -> _A = binary:copy(<<1>>,200), _B = math:sqrt(1235), infinite_loop(c+1).

BEAM Scheduler Cooperative? Cooperative at C level Preemptive? Preemptive at Erlang level (by means of reduction counting) 2000 reductions Reduction ~= function call Word of caution: BIFs and NIFs

BEAM Memory model Process PCB Stack Free GC Old Heap MBox Heap hipe_bifs:show_heap(pid). hipe_bifs:show_estack(pid). hipe_bifs:show_pcb(pid). %% Look at heap_sz!

JVM (simplified! SORRY! ) Heap

GC Stop the world. Is that a thing in Erlang?

DEMO 3 The KILLER

KILLER_JUGGLER a simplified DEMO SERVER https://github.com/iguberman/erljuggler_demo %%% a kind of a supervisor kick_off_request_handler(req, AcceptorPid) -> RequestHandlerPid = spawn(?module, handle_request, [Req, self()]), Start = os:system_time(millisecond), receive {RequestHandlerPid, Resp} -> End = os:system_time(millisecond), Duration = End - Start, io:format("...~p [~b]...", [Req, Duration]), AcceptorPid! {RequestHandlerPid, Resp, Duration}; Other -> AcceptorPid! {error, Other} after 5000 -> exit(handlerpid, timedout), end. AcceptorPid! {HandlerPid, killed}

STACK BASED REGISTER BASED More Instructions Instructions are simple Fewer instructions Instructions have more info 1.POP 20 2.POP 7 3.ADD 20, 7, result 4.PUSH result 1. ADD R1, R2, R3 ; # Add contents of R1 and R2, store result in R3

Performance Survey on Stack-based and Registerbased VirtualMachines Ruijie Fang, Siqi Liu, 2016 CONCEPTUM (stack-based like JVM) VM feature comparisons: INERTIA (register-based like BEAM) Scientific comparison of REGISTER (Erlang) vs. STACK (JVM) VM Inertia spends 66.42% less time in instruction dispatch than Conceptum, on average However, Inertia is still slower in the overall fetch time, spending 23.5% more time on average in fetching operands than Conceptum does Based on our test results, stack-based virtual machines typically perform better on benchmarks featuring a high amount of arithmetic operations. In contrast to the stack-based virtual machine s performance, the register-based virtual machine performed much better on recursions and memory operations.

MBox PCB Stack MBox Intern Free MBox Inbox Heap Old Heap M-buf M-buf M-buf M-buf

DEMO 4 Bring it down by sending messages to the process affected by inf loop!

TYPE SYSTEM Strong typed. So every type has a tag. Dynamically typed. Hot code loading anyone?

TAGGING In the memory representation of an Erlang term a few bits are reserved for a type tag. LEVEL 1 tags: 00 Header (on heap) CP (on stack) 01 List (cons) 10 Boxed 11 Immediate LEVEL 2 tags (Immediate): 00 11 Pid 01 11 Port 10 11 Immediate 2 11 11 Small integer <- pointers to the heap <- fit into one word on the stack LEVEL 3 tags (Immediate 2): 00 10 11 Atom 01 10 11 Catch 10 10 11 [UNUSED] 11 10 11 Nil <- for empty list []

Q. How is cooperative scheduling implemented? A. If there are untagged values no preempting

REFERENCES https://happi.github.io/thebeambook https://www.researchgate.net/publication/ 309631798_A_Performance_Survey_on_Stackbased_and_Register-based_Virtual_Machines (pdf available) https://llvm.org/devmtg/2014-04/pdfs/talks/drejhammar.pdf https://markfaction.wordpress.com/2012/07/15/stack-based-vs-registerbased-virtual-machine-architecture-and-the-dalvik-vm/