erlang的进程调度器工作流程

在多处理器机器上erlang默认是有几个cpu就有几个调度器线程,除非你通过+S N 参数来改变。每个调度器线程的入口函数是process_main, 外加一个主线程阻塞在select上等待中断事件的发生. process_main会调用schedule选择一个合适的process来执行。每个process里面都包含了要执行的的MFA,执行funtcion的opcode。beam_emu的opcode是基于register的,大概有180条左右opcode,每个版本都在增加,特别是最近的版本为了增加binary出来的效率加多了很多.

/*
* schedule() is called from BEAM (process_main()) or HiPE
* (hipe_mode_switch()) when the current process is to be
* replaced by a new process. 'calls' is the number of reduction
* steps the current process consumed.
* schedule() returns the new process, and the new process'
* ->fcalls field is initialised with its allowable number of
* reduction steps.
*
* When no process is runnable, or when sufficiently many reduction
* steps have been made, schedule() calls erl_sys_schedule() to
* schedule system-level activities.
*
* We use the same queue for normal and low prio processes.
* We reschedule low prio processes a certain number of times
* so that normal processes get to run more frequently.
*/
Process *schedule(Process *p, int calls);
erlang系统调度的核心就是这个函数。

schedule调度的顺序是这样的:
1. 首先处理timer超时。
2. 处理子进程退出的情况。
3. 处理port_task事件,也就是port的IO事件
4. 如果没有活跃的进程 就sys_schdule阻塞在底层的IO中。
5. 根据process的优先级选出一个进程来调度。

这5个调度schdule要去平衡调度的力度,以期公平。

void
erl_sys_schedule(int runnable)
{
#ifdef ERTS_SMP
ERTS_CHK_IO(!runnable);
ERTS_SMP_LC_ASSERT(!ERTS_LC_IS_BLOCKING);
#endif
}

check_io就阻塞在poll上等待IO时间的发生,也就是说如果系统不繁忙的时候所以的scheduler都在等待IO事件的发生。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值