原创作者: mryufeng
阅读:2219次
评论:0条
更新时间:2011-06-01
在多处理器机器上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事件的发生。
/*
* 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 条 请登录后发表评论