技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> 其他 --> Erlang进程简单的主动负载管制实现

Erlang进程简单的主动负载管制实现

浏览:1436次  出处信息
    我们知道Erlang的调度器是公平的,当进程的时间片用完了后,会强制切出,但是这个粒度是比较粗的。比如说进程进行了大量的Io操作,这个操作换成时间片是不准确的,会导致某些CPU计算密集型的比较吃亏,IO密集型的合算。

    为了避免这个情况,IO密集型的经常可以互动要求短暂放弃执行,最简单的方法就是用消息等待机制。当进程在等消息的时候,就会被切出,我们就达到目的。

    我们可以参考mnesia的实现:

%%mnesia_dumper.erl:L1181
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Load regulator
%%
%% This is a poor mans substitute for a fair scheduler algorithm
%% in the Erlang emulator. The mnesia_dumper process performs many
%% costly BIF invokations and must pay for this. But since the
%% Emulator does not handle this properly we must compensate for
%% this with some form of load regulation of ourselves in order to
%% not steal all computation power in the Erlang Emulator ans make
%% other processes starve. Hopefully this is a temporary solution.                                                           

start_regulator() ->
    case mnesia_monitor:get_env(dump_log_load_regulation) of
        false ->
            nopid;
        true ->
            N = ?REGULATOR_NAME,
            case mnesia_monitor:start_proc(N, ?MODULE, regulator_init, [self()]) of
                {ok, Pid} ->
                    Pid;
                {error, Reason} ->
                    fatal("Failed to start ~n: ~p~n", [N, Reason])
            end
    end.

regulator_init(Parent) ->
    %% No need for trapping exits.
    %% Using low priority causes the regulation
    process_flag(priority, low),
    register(?REGULATOR_NAME, self()),
    proc_lib:init_ack(Parent, {ok, self()}),
    regulator_loop().

regulator_loop() ->
    receive
        {regulate, From} ->
            From ! {regulated, self()},
            regulator_loop();
        {stop, From} ->
            From ! {stopped, self()},
            exit(normal)
    end.

regulate(nopid) ->
    ok;
regulate(RegulatorPid) ->
    RegulatorPid ! {regulate, self()},
    receive
        {regulated, RegulatorPid} -> ok
    end.

    祝玩得开心!

    Post Footer automatically generated by wp-posturl plugin for wordpress.

    Post Footer automatically generated by wp-posturl plugin for wordpress.

建议继续学习:

  1. Erlang match_spec引擎介绍和应用    (阅读:4576)
  2. whatsapp深度使用Erlang有感    (阅读:4633)
  3. php-erlang    (阅读:4319)
  4. gen_tcp调用进程收到{empty_out_q, Port}消息奇怪行为分析    (阅读:3555)
  5. hibernate使用注意事项    (阅读:3228)
  6. Erlang linkin driver用port_control方式时的一些经验分享    (阅读:2975)
  7. Erlang如何限制节点对集群的访问之net_kernel:allow    (阅读:3028)
  8. ERLANG OTP源码分析 – gen_server    (阅读:2870)
  9. erlang学习手记    (阅读:2703)
  10. gen_tcp容易误用的一点解释    (阅读:2647)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1