23
Erlang 调度初探 庄晓 2017/01/06

Erlang scheduler

Embed Size (px)

Citation preview

  • Erlang 2017/01/06

  • Ping-Pong

  • Ping-Pong

    ping_pong.ex

    Context Switch1.x us on my machine(2.5 GHz Intel Core i7).

    https://gist.github.com/killme2008/1f8a58ebd198ffbb04d4af25f9eb9135

  • 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.

    https://github.com/erlang/otp/blob/9cb4770469218f65dbaec6c71d12b4aa722ac791/erts/emulator/beam/erl_process.h#L935

  • Erlang Process

  • Stack and Heap

  • Process

    http://www.cnblogs.com/me-sa/archive/2011/11/13/erlang0014.html

    http://www.cnblogs.com/me-sa/archive/2011/11/13/erlang0014.htmlhttp://www.cnblogs.com/me-sa/archive/2011/11/13/erlang0014.html

  • Message Passing send / receive

    Coping message

    Sends a message to it self, doesnt 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.

    https://github.com/erlang/otp/blob/86d1fb0865193cce4e308baa6472885a81033f10/erts/emulator/beam/erl_vm.h#L39https://github.com/erlang/otp/blob/9cb4770469218f65dbaec6c71d12b4aa722ac791/erts/emulator/beam/erl_process.c#L9484

  • 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

    https://github.com/erlang/otp/blob/9cb4770469218f65dbaec6c71d12b4aa722ac791/erts/emulator/beam/erl_process.c#L56https://github.com/erlang/otp/blob/9cb4770469218f65dbaec6c71d12b4aa722ac791/erts/emulator/beam/erl_process.c#L4621

  • Number of schedulers

  • Next active schedulers

  • Work migration

  • Memory Management

    http://erlang.org/doc/man/erts_alloc.html

    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

    https://www.erlang-solutions.com/blog/erlang-19-0-garbage-collector.htmlhttps://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

    https://www.erlang-solutions.com/blog/erlang-19-0-garbage-collector.htmlhttps://www.erlang-solutions.com/blog/erlang-19-0-garbage-collector.htmlhttps://hamidreza-s.github.io/erlang/scheduling/real-time/preemptive/migration/2016/02/09/erlang-scheduler-details.htmlhttps://hamidreza-s.github.io/erlang/scheduling/real-time/preemptive/migration/2016/02/09/erlang-scheduler-details.htmlhttps://www.zhihu.com/question/24732869