Erlang 调度初探
庄晓丹丹 2017/01/06
Ping-Pong
Ping-Pong
• ping_pong.ex
• Context Switch:1.x us on my machine(2.5 GHz
Intel Core i7).
Process Structure
• C Struct : erl_process.h
• PCB: process control block
• process id
• stack/heap position
• argument registers
• program counter
• stack
• private heap : compound data structures, such as tuples, lists etc.
• heap fragments: used when there is not enough free space in the heap and
garbage collection cannot be performed to get more free memory.
Erlang Process
Stack and Heap
Process
来⾃自: http://www.cnblogs.com/me-sa/archive/2011/11/13/
erlang0014.html
Message Passing
• send / receive
• Coping message
• Sends a message to it self, doesn’t copy.
• binary 拷⻉贝引⽤用
• < 64 : private heap
• >= 64 : 全局共享
• 推荐:⼩小消息,⼤大计算
Scheduler
• Reductions: 2000
• erl_vm.h :
• #define CONTEXT_REDS 2000 /* Swap process out after this number */
• erts_schedule in erl_process.c
• consumes all its reductions
• or pauses to wait for a message
• Four priorities: low | normal | high | max
• Four queues: ports, maximum, high, normal/low.
• ports > max > high > normal/low
• normal/low: skip a low priority process for a number of times
• Round-Robin in queue.
A Scheduler
图⽚片来⾃自 Google images
SMP
• Multiple processors => Multiple scheduler
• erl +S Schedulers:SchedulerOnline
• defaults to logical processors configured
• Balancing workload on multiple processors
SMP Schedulers
图⽚片来源:http://ithelp.ithome.com.tw/articles/10161709
Workload Balancing
• Check workload periodically by one scheduler
• Default period: 2000*2000 reductions.
• erl_process.c:
• #define ERTS_RUNQ_CHECK_BALANCE_REDS_PER_SCHED
(2000*CONTEXT_REDS)
• check_balance in erl_process.c
• During a period:
• work sharing/stealing
• determines the number of active schedulers for the next period based on
the load of the current period
Number of schedulers
Next active schedulers
Work migration
Memory Management
http://erlang.org/doc/man/erts_alloc.html
Garbage Collection
• Process heap: copying generational garbage
collector.
• 共享 Binary: 引⽤用计数
• https://www.erlang-solutions.com/blog/
erlang-19-0-garbage-collector.html
GC
Control GC
• erl +h min_heap_size
• spawn min_heap_size/min_bin_vheap_size
• :erlang.garbage_collect()
• Statistics:
• :erlang.process_info(pid, :garbage_collection)
• :erlang.system_info(:garbage_collection)
参考资料料
• http://www.diva-portal.org/smash/get/diva2:392243/
FULLTEXT01.pdf
• https://www.erlang-solutions.com/blog/erlang-19-0-garbage-
collector.html
• https://hamidreza-s.github.io/erlang/scheduling/real-time/
preemptive/migration/2016/02/09/erlang-scheduler-details.html
• http://www.cnblogs.com/me-sa/archive/2011/11/13/erlang0014.html
• https://www.zhihu.com/question/24732869

Erlang scheduler