关于Reduction 论文  Characterizing the Scalability of ErlangVM on Many-core Processors 的一段解释:

 

   Erlang schedulers are based on reduction counting as a method for measuring execution time. A reduction is roughly equivalent to a function call. Since each function call may take a different amount of time, the actual periods are not the same between different reductions. When a process is scheduled to run, it is assigned a number of reductions that it is allowed to execute (by default 2000 reductions in R13B04). The process can execute until it consumes all its reduction quantum or pauses to wait for a message. A process waiting for a message is rescheduled when a new message comes or a timer expires. Rescheduled or new processes are put to the end of corresponding run queues.Suspended (blocked) processes are not stored in the run queues.! 

 

不过上面"A reduction is roughly equivalent to a function call."这个还是不太明确,请教了一下霸爷:

 

Q: Erlang的Trap机制会保证调度的公平,我有个疑问:Reduction是对应一次完整的函数调用完成么?比如 一个耗时的操作,执行过程中让出CPU时间片之后再次被调度执行Reduciton会变化么?


A:- 霸爷的回复:


公平调度实际上有3部分:

  1.  erlang函数调用,由于erlang的代码翻译成opcode,由虚拟机执行,所以一次完整的函数调用为一个reduction. 因为erlang的函数通常都是递归执行的,所以函数体一般很小。
  2.  bif的trap机制。简单的说就是bif会执行到大概几个reduction这样的时间片后放弃执行,把当前的执行情况记录下来,然后退出。等下次调度再执行的时候,会继续之前的位置往下。
  3.  IO的调度。 IO也是公平调度的,把IO的处理量换算成reduction,算在宿主进程的时间片里面。

 

更多参考:

Erlang ERTS的Trap机制的设计及其用途 http://mryufeng.iteye.com/blog/334744

 

最后小图一张 这个软件现在已经停止服务了,当时的一个截图: