javaeye上我写的erlang的帖子电子书下载
原创文章,转载请注明: 转载自系统技术非业余研究
本文链接地址: javaeye上我写的erlang的帖子电子书下载
如题, 方便大家阅读,这个电子书记录了俺的研究过程,希望对大家有帮助。
erlang深度分析文章下载
Post Footer automatically generated by wp-posturl plugin for wordpress.
原创文章,转载请注明: 转载自系统技术非业余研究
本文链接地址: javaeye上我写的erlang的帖子电子书下载
如题, 方便大家阅读,这个电子书记录了俺的研究过程,希望对大家有帮助。
erlang深度分析文章下载
Post Footer automatically generated by wp-posturl plugin for wordpress.
原创文章,转载请注明: 转载自系统技术非业余研究
本文链接地址: leex文法分析的效率
R13B新添加的leex相当于c的lex, 在做文法分析非常方便,但是效率如何呢? leex的example里面带了个erlang_scan和erlang标准的发布版的erl_scan兼容,所以我们来对比测试下效率。
注意用R13B03,因为R13B02的erlc漏掉了编译xrl格式。
以下是实验:
root@nd-desktop:~# git clone git://github.com/rvirding/leex.git root@nd-desktop:~# cd leex/examples/ root@nd-desktop:~/leex/examples# cat test_scan.erl
-module(test_scan). -export([start/1]). start([A])-> {ok, F} = file:open(atom_to_list(?MODULE)++".erl", [read]), {ok, S} = file:read(F, 9999999), file:close(F), N = list_to_integer(A), test(N, fun erlang_scan:string/1, S, "erlang_scan"), test(N, fun erl_scan:string/1, S, "erl_scan"), ok. test(N, F, S, Ts)-> Start = erlang:now(), dotimes(N, fun (_) -> F(S) end), io:format("~s run ~w ms~n", [Ts,round(timer:now_diff(now(), Start) /1000)]). dotimes(0, _) -> done; dotimes(N, F) -> F(N), dotimes(N - 1, F).
root@nd-desktop:~/leex/examples# erlc erlang_scan.xrl root@nd-desktop:~/leex/examples# ls *.erl erlang_scan.erl test_scan.erl root@nd-desktop:~/leex/examples# erlc *.erl root@nd-desktop:~/leex/examples# erl -noshell -run test_scan start 10000 -s erlang halt erlang_scan run 2208 ms erl_scan run 1181 ms
%% 这个版本稍微慢点
root@nd-desktop:~/leex/examples# erlc +native *.erl root@nd-desktop:~/leex/examples# erl -noshell -run test_scan start 10000 -s erlang halt erlang_scan run 1292 ms erl_scan run 1238 ms
结论是: leex产生的代码和手写的效率几乎差不多。
Post Footer automatically generated by wp-posturl plugin for wordpress.
原创文章,转载请注明: 转载自系统技术非业余研究
本文链接地址: erlang动态解释
erlang的代码运行有2种方式 1. 编译成opcode 2. eval. 这2种形式都需要把erl代码翻译成abstract code, 然后在不同的途径分别开.
通常情况下我们在eshell 里面输入的东西都是动态eval的. erl还支持命令行动态解释. erl -eval
看下init.erl
start_it({eval,Bin}) -> Str = binary_to_list(Bin), {ok,Ts,_} = erl_scan:string(Str), Ts1 = case reverse(Ts) of [{dot,_}|_] -> Ts; TsR -> reverse([{dot,1} | TsR]) end, {ok,Expr} = erl_parse:parse_exprs(Ts1), erl_eval:exprs(Expr, []), ok;
我们看到express先文法扫描, 确认以dot结尾, 然后进行语法分析,形成abstract code, 最后 erl_eval解释abstact code.
我们注意到 在erl_parse的时候 表达式是这样的 f()->expr. 这就解释了为什么在shell里面无法定义函数,无法定义module什么的.
io模块和shell模块都是以上面的形式来动态eval的. 这个特性很好用, 配合parse_transform, 我们可以很容易自己实现个DSL语言.
Post Footer automatically generated by wp-posturl plugin for wordpress.
原创文章,转载请注明: 转载自系统技术非业余研究
本文链接地址: erlang高级原理和应用PPT
公司培训用的 凑合看吧 主要讲erlang系统的特点,分布集群以及mnesia的使用, 从比较高的角度来看erlang, 让你有了大体观.
Post Footer automatically generated by wp-posturl plugin for wordpress.
原创文章,转载请注明: 转载自系统技术非业余研究
原文地址:http://hyperstruct.net/2007/6/26/literal-xml-in-erlang-with-parse-transform-2
One of the things I dislike about Erlang is that it severely impairs bragging opportunities. Yesterday I wrote a module that allows writing literal XML in the source and have it parsed into Erlang structures at compile time—sort of like E4X minus the manipulation goodies at runtime (at least for now).
You write:
Doc = '<greeting>Hello!</greeting>', io:format("~p~n", [Doc]).
And it prints…
{xmlElement,greeting, greeting, [], {xmlNamespace,[],[]}, [], 1, [], [{xmlText,[{greeting,1}],1,[],"Hello!",text}], [], "/tmp", undeclared}
In most languages I’m familiar with, this would have granted the author instant Yacc-demigod status. With Erlang… it was less than 40 LOC. Hardly something you’d wear at a party.
Anyway, this code owes everything to Philip’s writings. It also uses parse_transform/2, and “programmers are strongly advised not to engage in parse transformations and no support is offered for problems encountered”. So unless you, like me, are still at the kid-in-a-candy-shop stage of Erlang experience, think twice before using this in production, ok?
The code is here http://repo.hyperstruct.net/inline_xml/
Post Footer automatically generated by wp-posturl plugin for wordpress.
原创文章,转载请注明: 转载自系统技术非业余研究
本文链接地址: 如何用gdb调试erlang运行期(高级)
前些天在erlang的源码里面闲逛的时候发现了 bin目录下的cerl,一看原来是个调试的高级货。
我之前写过一篇文章http://mryufeng.javaeye.com/blog/113852 如何调试erlang 但是这是土八路的方法, cerl才是现代化工业。
# This is a script to start Erlang/OTP for debugging. PATH is set to
# include this script so if slave nodes are started they will use this
# script as well.
#
# usage: cerl [ OPTIONS ] [ ARGS ]
#
# The OPTIONS are
#
# -rootdir $MYROOTDIR
# Run an installed emulator built from this source
# -debug Run debug compiled emulator
# -gdb Run the debug compiled emulator in emacs and gdb.
# You have to start beam in gdb using “run”.
# -break F Run the debug compiled emulator in emacs and gdb and set break.
# The session is started, i.e. “run” is already don for you.
# -xxgdb FIXME currently disabled
# -purify Run emulator compiled for purify
# -quantify Run emulator compiled for quantify
# -purecov Run emulator compiled for purecov
# -gcov Run emulator compiled for gcov
# -valgrind Run emulator compiled for valgrind
# -lcnt Run emulator compiled for lock counting
# -nox Unset the DISPLAY variable to disable us of X Windows
#
要使用cerl 我们最好准备个调试版本的erlang。R13B 修正了些编译错误,可以编译出debug版本的系统:./configure && make TYPE=debug && make
这样就生成了个beam.debug的运行期。
我们要调试的时候 就可以在otp的binx目录下运行 cerl -debug -gdb -break main
这时候cerl自动会加载 emacs 启动 gud, 整个过程都是自动的。但是这个脚本有点小问题, gud模型下没有把源码和当前对应的调试对应起来。可以通过以下方式修正:
yu-fengdemacbook-2:bin yufeng$ diff cerl cerl2 284c284 < exec $EMACS --eval "(progn (gdb \"gdb $EMU\") $gdbcmd)" --- > exec $EMACS --eval "(progn (gdb \"gdb --annotate=3 $EMU\") $gdbcmd)"
具体的操作和界面可以参照这篇文章:
http://www.nabble.com/printing-of-Eterm%27s-from-gdb-td19240493.html
在调试的时候 我们会希望查看 eterm的值,但是由于eterm的格式非常复杂, gdb的print什么的无法满足我们的需求。 otp开发团队于是开发出了一套方法来减轻我们的负担:
1. erts/etc/unix/etp-commands 这是gdb的脚本 包含了几十个etp方法,而且文档非常详细。
2. 源码里面的 pp, ps等函数, 这些函数是专门为gdb调试开发的。 可以用gdb的 call pp(xxx)来调用。
有了这些工具 调试和研究erts变成了一件很快乐的事情!
Post Footer automatically generated by wp-posturl plugin for wordpress.
原创文章,转载请注明: 转载自系统技术非业余研究
本文链接地址: binary的常量优化
erlang的binary在这个网络程序里面占着非常重要的地位,所以otp团队采用了非常多的优化手段包括:
1. binary操作对应着 opcode
2. 根据生命期和作用,有4种类型的binary
3. hipe优化,把bs_操作直接翻译成asm指令
4. 编译器层面消除无必须的操作。
下面的例子就是演示4的特性:
yu-fengdemacbook-2:~ yufeng$ cat bin.erl
-module(bin). -export([start/1]). start(A)-> B1= <<12>>, B2 = <<B1/binary, 5.0/float>>, B3= <<B2/binary, "yes">>, % B3的值是预先可以知道的, 无需一步步的构造 <<"abcd", 3:32,B3:128/binary,_/binary>> = <<"abcd1234",A/binary,2:32, 8773:64, "a", 5.0/float>>. % 2:32, 8773:64, "a", 5.0/float 这些都是预先知道的 直接翻译成二进制流。
yu-fengdemacbook-2:~ yufeng$ erlc +"'S'" bin.erl yu-fengdemacbook-2:~ yufeng$ cat bin.S
{module, bin}. %% version = 0 {exports, [{module_info,0},{module_info,1},{start,1}]}. {attributes, []}. {labels, 8}. {function, start, 1, 2}. {label,1}. {func_info,{atom,bin},{atom,start},1}. {label,2}. {move,{integer,0},{x,1}}. {gc_bif,byte_size,{f,0},2,[{x,0}],{x,2}}. {bs_add,{f,0},[{x,1},{x,2},1],{x,1}}. {bs_add,{f,0},[{x,1},{integer,29},1],{x,1}}. {bs_init2,{f,0},{x,1},0,1,{field_flags,[]},{x,1}}. {bs_put_string,8,{string,"abcd1234"}}. {bs_put_binary,{f,0},{atom,all},8,{field_flags,[unsigned,big]},{x,0}}. %% 一步到位 {bs_put_string,21, {string,[0,0,0,2,0,0,0,0,0,0,34,69,97,64,20,0,0,0,0,0,0]}}. {test,bs_start_match2,{f,3},[{x,1},2,0,{x,0}]}. {test,bs_match_string,{f,3},[{x,0},64,{string,[97,98,99,100,0,0,0,3]}]}. %% 一步到位 {test,bs_get_binary2, {f,3}, [{x,0}, 2, {integer,128}, 8, {field_flags,[{anno,[8,{file,"./bin.erl"}]},unsigned,big]}, {x,2}]}. {test,bs_skip_bits2, {f,3}, [{x,0}, {atom,all}, 8, {field_flags,[{anno,[8,{file,"./bin.erl"}]},unsigned,big]}]}. {test,is_eq_exact, {f,3}, [{x,2},{literal,<<12,64,20,0,0,0,0,0,0,121,101,115>>}]}. %% 一步到位 {move,{x,1},{x,0}}. return. {label,3}. {badmatch,{x,1}}. {function, module_info, 0, 5}. {label,4}. {func_info,{atom,bin},{atom,module_info},0}. {label,5}. {move,{atom,bin},{x,0}}. {call_ext_only,1,{extfunc,erlang,get_module_info,1}}. {function, module_info, 1, 7}. {label,6}. {func_info,{atom,bin},{atom,module_info},1}. {label,7}. {move,{x,0},{x,1}}. {move,{atom,bin},{x,0}}. {call_ext_only,2,{extfunc,erlang,get_module_info,2}}.
所以我们在使用binary的时候, 尽可能的利用这个特性。
Post Footer automatically generated by wp-posturl plugin for wordpress.
Recent Comments