1 / 33

Inside the Erlang VM

Inside the Erlang VM. Yu Feng mryufeng@gmail.com 2008/12/20 blog: http://mryufeng.javaeye.com. BEAM 的 优势. 高性能 多核心 SMP 的支持 透明分布的支持 轻量进程的支持 完善的 监控信息 商 业产品上经过时间的验证成熟. 为什么其他语言要移植到 Erlang 虚 拟机. 语言 Reia 会成功 吗 作者看中什么? 框架 概念 成熟度 性能 模型. ERTS 的代 码规模. 200K 行 C 代 码 几千行 Erlang 代 码

lela
Download Presentation

Inside the Erlang VM

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inside the Erlang VM Yu Feng mryufeng@gmail.com 2008/12/20 blog: http://mryufeng.javaeye.com

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

  3. 为什么其他语言要移植到Erlang虚拟机 语言Reia会成功吗 作者看中什么? 框架 概念 成熟度 性能 模型

  4. ERTS的代码规模 200K行C代码几千行Erlang代码 同等的ACE框架 代码成熟度 尺寸很小适合于嵌入式 1.5M 也适合做桌面程序如p2p

  5. ERTS的物理结构 tty_sl ram_file_drvzlib_drv udp_inet tcp_inet efile async emulator driver sys utilts beam beam.smp beam.hybird erlexec escript … unix windows …

  6. BEAM的分类 BEAM 分成不同版本的目的是提高性能避免不必要的开销 beam.plain beam.smp beam.hybrid (目前支持的不好)

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

  8. ERTS是个典型的网络服务器框架 IO处理 kernel poll 如epoll kqueue 定时器处理 timewheel 逻辑处理处理 process coroutine fiber smp

  9. 强大的PORT 仿照Unix的哲学: 一切都是文件 管道通讯类似CGI 支持可执行文件和动态库 fd_driver_entry vanilla_driver_entry spawn_driver_entry kernel poll支持大量的句柄 高度优化的kpoll {lazy_updates,true}, {pending_updates,0}, {batch_updates,false}, {concurrent_updates,true},

  10. 定时器 支持time jump detection and correction Erlang使用timer有3种方式: 语法层面的 receive ... after ... BIF: erlang:send_after(Time, Dest, Msg)    erlang:start_timer(Time, Dest, Msg) driver层面的。   int driver_set_timer(ErlDrvPort port, unsigned long time);  

  11. SMP 参见EUC_SMP http://www.erlang.se/euc/08/euc_smp.pdf

  12. 多处理器利用技术 线程 调度器 异步线程 driver发起的线程 精巧的锁 process_lock 快速的mutex 进程 PORT 管道通讯

  13. 虚拟机部分 基于register JIT Hipe native代码执行效率高好多 语言速度评测中表现不俗 opcode R12B大概200条 goto 和 case 优化 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

  14. 系统调度 处理timer超时 处理子进程退出的情况 处理port_task事件,也就是port的IO事件 port_task独立调度有自己的调度队列 如果没有活跃的进程就sys_schdule阻塞在底层的IO中。 check_io 根据process的优先级选出一个进程来调度。 PRIORITY_MAX PRIORITY_HIGH  PRIORITY_NORMAL PRIORITY_LOW  PRIORITY_LEVELS 

  15. 数据结构 用地址来区分数据类型 pid <X, Y, Z> atom index表示 cache 传送的是index binary bitstring 非常高效静态分析 opcode执行 非线性处理 list 单链表表头加入 tuple 相当于数组

  16. bitstring 非线性数据处理 和scatter read, gather write配对 静态分析 opcode实现bitstring操作 减少内存搬动大大提高操作效率 对程序员透明

  17. 消息传递 高效 compat 内部格式 外部格式 erts/emulator/internal_doc/erl_ext_dist.txt 描述了erlang ext term的格式, epmd通讯的流程协议和流程同时还要node 间通讯的协议 PID变换 pid {X,Y,Z} 在发到网络的时候发出去的格式是{sysname, Y, Z} 因为节点之前互相联系过所以互相知道对方的sysname, 而且sysname在dist_entry里保存,当对端收到dec_pid的时候,用peer sysname 的查到在自己dist_entry里面的索引,然后用这个index 来构造新的pid,即 {index, Y, Z}。

  18. 内存分配器 每个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

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

  20. 驱动程序 完善的驱动程序开发接口甚至可以多线程编程 文件驱动 异步操作 inet_drv实现网络底层服务如tcp udp sctp 丰富的socket选项 支持常见的消息编解码 HTTP CDR ASN.1等 scatter read, gather write 消息接收变通提高效率

  21. 异步线程 完成驱动程序耗时操作 和调度器不相干

  22. BIF trap机制 distribution trap functions dsend2_trap dsend3_trap dsend_nosuspend_trap dlink_trap dunlink_trap dmonitor_node_trap dgroup_leader_trap dexit_trap 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.

  23. 透明分布 需要net_kernel的协助 group_leader的设计和用途 可替换的传输介质 inet_tcp_dist inet_ssl_dist dist trap 透明的进行握手动作 connect and handshake 名称登记和维护 local global 维护网络全联通 net tick nodeup nodedown erlsnoop 查看交互

  24. 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)级别的.

  25. 对系统的干预 内置强大的shell CTRL+C 动态改变调度器的数目 crash dumps

  26. 异常处理 语法层面的实现 link monitor 详尽的日志完善的日志系统 error_logger port是进程隔离的

  27. 自省机制 trace system_flag system_info system_profile system_monitor erts_debug the erlang crash dumps

  28. ETS内存数据库 hash和tree match VM instruction 不参与GC

  29. GC 不能保证real time,特别是root set比较大的时候 mark-and-sweep copying collector 分代算法 2代 old_heap heap

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

  31. 平台移植 Windows (smp支持的不好) Unix 关键语义屏蔽平台变化

  32. Tips: 小心参数设置 大量的参数可以通过环境变量来配置 进程数目 最大文件句柄数

  33. 谢谢大家 Q & A

More Related