Erlang与外面世界的交互主要通过port来进行的,特别是和外部程序的协作,通常是通过管道进行的。
基本上有2种方法可以调用外部程序: 1. os:cmd 2. erlang:open_port, 这二种方式各有利弊,先看文档:
Read more…
match_spec是什么呢?
A “match specification” (match_spec) is an Erlang term describing a small “program” that will try to match something (either the parameters to a function as used in the erlang:trace_pattern/2 BIF, or the objects in an ETS table.). The match_spec in many ways works like a small function in Erlang, but is interpreted/compiled by the Erlang runtime system to something much more efficient than calling an Erlang function. The match_spec is also very limited compared to the expressiveness of real Erlang functions.
具体参见这里
说白了它就是个erlang term得过滤器,可以让用户来自己选择需要匹配什么,需要从term里面抽取什么数据。那同学可能就有疑问了,Erlang的函数不是很强大吗,它能做的函数也能做,那为什么要重新费劲做一个呢?
Erlang实现这个match_spec得原因有2个:1. 运行效率 2. 小巧可以在运行期使用。
它的实现思路是: match_spec是个引擎,有自己的语法,先把语句编译成专用的opcode, 然后在在匹配的时候运行opcode,获取结果,可以理解为erlang的DSL。
接下来我带大家先感性的认识下这个DSL:
Read more…
参见官方网站: http://www.erlang.org/news/26, 主要变更如下:
Erlang/OTP R14B04 has been released as planned on October 5:th 2011. It is the fourth R14 service release.
See the release notes in the readme file.
Download the new release from the download page.
This release is mainly a stabilization of the R14B03 release (but as usual there are some new functionality as well).
这次发布主要是bug fix版本,修正了大量的小问题, 谢谢OTP团队!
具体见这里
祝玩得开心!
Erlang Shell下有很多内置的命令,在平时交互的时候很好用,文档里面都是一行带过,大家可能没什么感觉。
我来重点讲解和演示下:
$ erl
Erlang R14B04 (erts-5.8.5) [source][/source][/source] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.8.5 (abort with ^G)
1> help().
** shell internal commands **
b() -- display all variable bindings
e(N) -- repeat the expression in query <N>
f() -- forget all variable bindings
f(X) -- forget the binding of variable X
h() -- history
history(N) -- set how many previous commands to keep
results(N) -- set how many previous command results to keep
catch_exception(B) -- how exceptions are handled
v(N) -- use the value of query <N>
rd(R,D) -- define a record
rf() -- remove all record information
rf(R) -- remove record information about R
rl() -- display all record information
rl(R) -- display record information about R
rp(Term) -- display Term using the shell's record information
rr(File) -- read record information from File (wildcards allowed)
rr(F,R) -- read selected record information from file(s)
rr(F,R,O) -- read selected record information with options
我最经常用的有以下几个,熟悉了感觉就很爽:
Read more…
我们知道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.
祝玩得开心!
做过网络集群服务器的的同学都知道,集群服务通常由不同的服务器组成,这些不同角色的服务器组合在一起共同完成了特定的服务。一个服务通常需要一个协调者,和不同的工作者。 协调者负责派发任务,接收工作者的完成情况,最终回馈给用户。举个例子来讲,拨打电话:首先需要确认你的号码是在有效的,同时还要看下你的帐号里面有钱不,还要看下你拨打的电话号码是不是由权限,电话打的时候需要扣钱,等等。 这些服务中间的任何一个环节出问题了,服务就不正常了。那么我们在服务出问题的时候,如何定位问题呢?通常的办法是打日志,在所有的参与服务的节点上打开日志记录,之后到所有的节点上收集日志,集中分析日志,相当的麻烦。
这时候seq_trace来救助了,seq_trace的目标就是能够跟踪一条消息经过的所有环节,最终把路径展现给用户。
铺垫材料:
seq_trace工作原理,请参考这里
ttb对seq_trace的支持参考这里
tdbg对seq_trace的支持参考这里
Read more…
hibernate的作用是在进程闲的时候或者内存紧张的时候,通过重新整理进程的堆和栈内存来减少内存的消耗,同时维持进程之前的状态,但是误用会引起些问题,这里我们来展开下。
erlang:hibernate文档参考这里
Read more…
Recent Comments