inside the erlang vm

33
Inside the Erlang VM Yu Feng [email protected] 2008/12/20 blog: http://mryufeng.javaeye.com

Upload: feng-yu

Post on 22-Jun-2015

1.777 views

Category:

Technology


8 download

DESCRIPTION

Chinese version PPT

TRANSCRIPT

Page 1: Inside the Erlang Vm

Inside the Erlang VM

Yu [email protected]/12/20blog: http://mryufeng.javaeye.com

Page 2: Inside the Erlang Vm

BEAM 的优势

• 高性能• 多核心 SMP 的支持• 透明分布的支持• 轻量进程的支持• 完善的监控信息• 商业产品上经过时间的验证成熟

Page 3: Inside the Erlang Vm

为什么其他语言要移植到 Erlang 虚拟机• 语言 Reia 会成功吗• 作者看中什么?

o 框架o 概念o 成熟度o 性能o 模型

Page 4: Inside the Erlang Vm

ERTS 的代码规模

• 200K 行 C 代码 几千行 Erlang 代码o 同等的 ACE 框架

• 代码成熟度• 尺寸很小 适合于嵌入式

o 1.5M• 也适合做桌面程序 如 p2p

Page 5: Inside the Erlang Vm

ERTS 的物理结构

• erlexec• escript• …

beambeam.smpbeam.hybird

emulatordriversysutilts

unixwindows…

tty_slram_file_drvzlib_drvudp_inettcp_inetefileasync

Page 6: Inside the Erlang Vm

BEAM 的分类

• BEAM 分成不同版本的目的是提高性能 避免不必要的开销

• beam.plain• beam.smp • beam.hybrid ( 目前支持的不好 )

Page 7: Inside the Erlang Vm

ERTS 启动过程

• 概念模拟操作系统• erl_exec• 系统资源初始化• erl_first_process_otp opt_ring0• 转入 Erlang 进程调度 开始执行 init:boot

Page 8: Inside the Erlang Vm

ERTS 是个典型的网络服务器框架

• IO 处理o kernel poll 如 epoll kqueue

• 定时器处理o timewheel

• 逻辑处理处理o process

coroutine fiber

o smp

Page 9: Inside the Erlang Vm

强大的 PORT

• 仿照 Unix 的哲学 : 一切都是文件• 管道通讯类似 CGI• 支持可执行文件和动态库

o fd_driver_entry vanilla_driver_entry spawn_driver_entry • kernel poll 支持大量的句柄

o 高度优化的 kpoll {lazy_updates,true},

{pending_updates,0}, {batch_updates,false}, {concurrent_updates,true},

Page 10: Inside the Erlang Vm

定时器

• 支持 time jump detection and correction • Erlang 使用 timer 有 3 种方式:

o 语法层面的 receive ... after ... o BIF: erlang:send_after(Time, Dest, Msg)   

erlang:start_timer(Time, Dest, Msg) o driver 层面的。

int driver_set_timer(ErlDrvPort port, unsigned long time);  

Page 11: Inside the Erlang Vm

SMP

• 参见 EUC_SMPo http://www.erlang.se/euc/08/euc_smp.pdf

Page 12: Inside the Erlang Vm

多处理器利用技术

• 线程o 调度器o 异步线程o driver 发起的线程

• 精巧的锁 process_lock 快速的 mutex

• 进程o PORT 管道通讯

Page 13: Inside the Erlang Vm

虚拟机部分

• 基于 register• JIT

o Hipe native 代码执行 效率高好多o 语言速度评测中表现不俗

• opcodeo R12B 大概 200 条 goto 和 case 优化o arithmetic, comparsions and boolean logic; manipulating

strings, tuples and lists; stack and heap allocation and freeing; type tests for Erlang primitives (numbers, lists, process IDs, references, and so on);  jumps, structured exception handling, and calls and returns; sending messages and reading from the process' mailbox; waiting and timeouts

Page 14: Inside the Erlang Vm

系统调度

• 处理 timer 超时 • 处理子进程退出的情况 • 处理 port_task 事件,也就是 port 的 IO 事件

o port_task 独立调度 有自己的调度队列• 如果没有活跃的进程 就 sys_schdule 阻塞在底层的 IO 中。

check_io• 根据 process 的优先级选出一个进程来调度。

o PRIORITY_MAXo PRIORITY_HIGH o PRIORITY_NORMALo PRIORITY_LOW o PRIORITY_LEVELS 

Page 15: Inside the Erlang Vm

数据结构

• 用地址来区分数据类型• pid <X, Y, Z>• atom

o index 表示o cache 传送的是 index

• binary bitstringo 非常高效 静态分析 opcode 执行o 非线性处理

• listo 单链表 表头加入

• tupleo 相当于数组

Page 16: Inside the Erlang Vm

bitstring 非线性数据处理

• 和 scatter read, gather write 配对• 静态分析 opcode 实现 bitstring 操作• 减少内存搬动 大大提高操作效率• 对程序员透明

Page 17: Inside the Erlang Vm

消息传递

• 高效 compat• 内部格式• 外部格式

o erts/emulator/internal_doc/erl_ext_dist.txt 描述了 erlang ext term 的格式 , epmd 通讯的流程协议和流程 同时还要 node 间通讯的协议 PID变换

o pid {X,Y,Z} 在发到网络的时候发出去的格式是 {sysname, Y, Z} 因为节点之前互相联系过 所以互相知道对方的 sysname, 而且 sysname 在 dist_entry里保存,当对端收到 dec_pid 的时候,用 peer sysname 的查到在自己 dist_entry里面的索引,然后用这个 index 来构造新的 pid ,即 {index, Y, Z} 。

Page 18: Inside the Erlang Vm

内存分配器

• 每个 CPU 一个内存池• 自动配置• 有 200 多个 alloc type• private Heap, hybrid heap• binary_alloc • std_alloc • ets_alloc • fix_alloc • eheap_alloc • ll_alloc • mseg_alloc • sl_alloc • temp_alloc • sys_alloc

Page 19: Inside the Erlang Vm

信号处理

• SIGCHLD• 用户信号• 异步处理和 poll 结合

Page 20: Inside the Erlang Vm

驱动程序

• 完善的驱动程序开发接口 甚至可以多线程编程• 文件驱动

o 异步操作• inet_drv 实现网络底层服务 如 tcp udp sctp

o丰富的 socket 选项o 支持常见的消息编解码

HTTP CDR ASN.1 等o scatter read, gather writeo 消息接收变通提高效率

Page 21: Inside the Erlang Vm

异步线程

• 完成驱动程序耗时操作• 和调度器不相干

Page 22: Inside the Erlang Vm

BIF trap 机制

• distribution trap functions o dsend2_trapo dsend3_trap o dsend_nosuspend_trap o dlink_trap o dunlink_trap o dmonitor_node_trap o dgroup_leader_trap dexit_trap o dmonitor_p_trap

if the emulator wants to perform a distributed commandand%% a connection is not established to the actual node the following %% functions is called in order to set up the connection and then %% reactivate the command.

Page 23: Inside the Erlang Vm

透明分布

• 需要 net_kernel 的协助• group_leader 的设计和用途• 可替换的传输介质

o inet_tcp_disto inet_ssl_dist

• dist trap 透明的进行握手动作o connect and handshake

• 名称登记和维护o localo global

• 维护网络全联通o net ticko nodeup nodedowno erlsnoop 查看交互

Page 24: Inside the Erlang Vm

beam 加载和代码的热部署

• 支持代码从 archive,inet,file里面读取 实现无盘工作站

•最小系统需要的 beam preload otp_ring0 init prim_inet prim_file zlib prim_zip

erl_prim_loader erlang

• Erlang 的热部署包括 beam(.beam) 级别的和driver(.dll .so) 级别的 .

Page 25: Inside the Erlang Vm

对系统的干预

• 内置强大的 shell• CTRL+C• 动态改变调度器的数目• crash dumps

Page 26: Inside the Erlang Vm

异常处理

• 语法层面的实现o link monitor

•详尽的日志 完善的日志系统o error_logger

• port 是进程隔离的

Page 27: Inside the Erlang Vm

自省机制

• trace• system_flag• system_info• system_profile• system_monitor• erts_debug• the erlang crash dumps

Page 28: Inside the Erlang Vm

ETS 内存数据库

• hash 和 tree• match VM instruction• 不参与 GC

Page 29: Inside the Erlang Vm

GC

• 不能保证 real time,特别是 root set比较大的时候

• mark-and-sweep copying collector • 分代算法 2 代

o old_heap heap

Page 30: Inside the Erlang Vm

稳定性

• heart 心跳检查 自动加载• process crash 不会影响整个虚拟机• 代码可热升级 容易修复 bug 回滚功能

Page 31: Inside the Erlang Vm

平台移植

• Windowso( smp 支持的不好)

• Unix•关键语义屏蔽平台变化

Page 32: Inside the Erlang Vm

Tips: 小心参数设置

• 大量的参数可以通过环境变量来配置• 进程数目•最大文件句柄数

Page 33: Inside the Erlang Vm

谢谢大家

Q & A