Archive

Archive for the ‘网络编程’ Category

如何用gdb调试erlang运行期(高级)

October 6th, 2009 No comments

原创文章,转载请注明: 转载自Erlang非业余研究

本文链接地址: 如何用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.

Categories: 网络编程 Tags:

Literal XML in Erlang with parse_transform/2

September 30th, 2009 No comments

原创文章,转载请注明: 转载自Erlang非业余研究

本文链接地址: Literal XML in Erlang with parse_transform/2

原文地址: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.

Categories: 网络编程 Tags:

erlang的abstract code

September 30th, 2009 1 comment

原创文章,转载请注明: 转载自Erlang非业余研究

本文链接地址: erlang的abstract code

erlang的abstract code是编译的中间代码,很多工具如 erl_pp lint什么的都是根据这个做调整的。还有进一步的parse_transform也是基于它的。 所以,了解它非常重要。 erts user guide里面详细了描述了它的定义。我这里展示的是如何获取到某个模块的abstract code 以便进一步研究:

[root@localhost ~]# erl -s hello
Erlang R13B02 (erts-5.7.3) 1 [64-bit] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]

hello world
[root@localhost ~]# cat hello.erl
-module(hello).
-export([start/0]).

start()->
    io:format("hello world~n",[]).
[root@localhost ~]# erlc +debug_info hello.erl
[root@localhost ~]# erl
Erlang R13B02 (erts-5.7.3) 1 [64-bit] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.3  (abort with ^G)
1> rp(beam_lib:chunks(hello, [abstract_code])).
{ok,{hello,[{abstract_code,{raw_abstract_v1,[{attribute,1,
                                                        file,
                                                        {"./hello.erl",1}},
                                             {attribute,1,module,hello},
                                             {attribute,2,export,[{start,0}]},
                                             {function,4,start,0,
                                                       [{clause,4,[],[],
                                                                [{call,5,
                                                                       {remote,5,{atom,5,io},{atom,5,format}},
                                                                       [{string,5,"hello world~n"},{nil,5}]}]}]},
                                             {eof,6}]}}]}}
ok
2>

对着文档开始好好分析吧。 Have fun!

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

Categories: 网络编程 Tags:

Latest News from the Erlang/OTP team at Ericsson September 5 2009

September 28th, 2009 No comments

原创文章,转载请注明: 转载自Erlang非业余研究

本文链接地址: Latest News from the Erlang/OTP team at Ericsson September 5 2009

原文地址:http://erlang-factory.com/upload/presentations/167/KennethLundin-LatestnewsfromtheErlangOTPteamatEricssonErlang_Workshop2009.pdf

Coming Open Source releases

OTP R13B02 September 23

OTP R13B03 November 25

OTP R13B04 Jan-Feb 2010

OTP R13B05 ???

OTP R14B May-June 2010

OTP R14B01

OTP R14B02

Plans for later releases

Multicore performance improvements

– Delayed deallocation, let the right scheduler do it (R13B03)

– Improved handling of process table

– Separate allocators per scheduler

– Use NUMA info for grouping of schedulers

– Separate poll sets per scheduler (IO)

– Support Scheduler bindings, cpu_topology on Windows as well.

– Optimize Erlang applications in Erlang/OTP

– Fine grained parallelism, language and library functions.

– Better and more benchmarks

New way to build the documentation

– Using XSLTPROC to produce html, man and XSL-FO

– Using Apache FOP to produce PDF

Easier to interface C libraries and to make your own ”BIFs”

– Dynamically linked in BIF’s (for C-code , easier to write and more efficient

than drivers)

Support for validation in xmerl_sax_parser

BIFs for search in binaries (EEP-?)

红色的部分 好期待哦。。。

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

Categories: 网络编程 Tags:

答erlang静态数据查询方式

September 4th, 2009 No comments

原创文章,转载请注明: 转载自Erlang非业余研究

本文链接地址: 答erlang静态数据查询方式

主题:erlang静态数据查询方式的一种构想 http://www.javaeye.com/topic/461367

解决这个问题有2种方式:

1.  函数匹配
2.  per module constant pool

针对这个问题我做了个试验, 构建一个atom->int的查询。

yu-fengdemacbook-2:~ yufeng$ cat t.erl
-module(t).
-export([start/1, start/2]).

start([A1, A2])-&gt;
start(list_to_integer(atom_to_list(A1)), A2).

start(N, Meth)-&gt;
Start = erlang:now(),
dotimes(N, case Meth of m-&gt;fun dict_lookup/1; f-&gt;fun fun_lookup/1 end),
Stop = erlang:now(),
erlang:display( N / time_diff(Start, Stop)).

dotimes(0, _) -&gt;
done;
dotimes(N, F) -&gt;
F(N),
dotimes(N - 1, F).

time_diff({A1,A2,A3}, {B1,B2,B3}) -&gt;
(B1 - A1) * 1000000 + (B2 - A2) + (B3 - A3) / 1000000.0 .

dict_lookup(I) -&gt;
{ok, I} = dict:find(list_to_atom("x" ++ integer_to_list(I)), h1:get_dict()) .

fun_lookup(I) -&gt;
I = h2:lookup(list_to_atom("x" ++ integer_to_list(I))).
yu-fengdemacbook-2:~ yufeng$ cat make_dict
#!/opt/local/bin/escript
main([A])-&gt;
N = list_to_integer(A),
L = [{list_to_atom("x" ++ integer_to_list(X)), X} || X&lt;-lists:seq(1, N)],
D = dict:from_list(L),
io:format("-module(h1).~n-export([get_dict/0]).~nget_dict()-&gt;~n",[]),
erlang:display(D),
io:format(".~n"),
ok.
yu-fengdemacbook-2:~ yufeng$ cat make_fun
#!/opt/local/bin/escript
main([A])-&gt;
N = list_to_integer(A),
io:format("-module(h2).~n-export([lookup/1]).~n",[]),
[io:format("lookup(~p)-&gt;~p;~n",[list_to_atom("x" ++ integer_to_list(X)), X]) || X&lt;-lists:seq(1, N)],
io:format("lookup(_)-&gt;err.~n", []),
ok.
yu-fengdemacbook-2:~ yufeng$ head h1.erl
-module(h1).
-export([get_dict/0]).
get_dict()-&gt;
{dict,100,20,32,16,100,60,{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},{{[[x10|10],[x30|30],[x50|50],[x70|70],[x90|90]],[[x11|11],[x31|31],[x51|51],[x71|71],[x91|91]],[[x12|12],[x32|32],[x52|52],[x72|72],[x92|92]],[[x13|13],[x33|33],[x53|53],[x73|73],[x93|93]],[[x4|4],[x14|14],[x24|24],[x34|34],[x44|44],[x54|54],[x64|64],[x74|74],[x84|84],[x94|94]],[[x5|5],[x15|15],[x25|25],[x35|35],[x45|45],[x55|55],[x65|65],[x75|75],[x85|85],[x95|95]],[[x6|6],[x16|16],[x26|26],[x36|36],[x46|46],[x56|56],[x66|66],[x76|76],[x86|86],[x96|96]],[[x7|7],[x17|17],[x27|27],[x37|37],[x47|47],[x57|57],[x67|67],[x77|77],[x87|87],[x97|97]],[[x8|8],[x18|18],[x28|28],[x38|38],[x48|48],[x58|58],[x68|68],[x78|78],[x88|88],[x98|98]],[[x9|9],[x19|19],[x29|29],[x39|39],[x49|49],[x59|59],[x69|69],[x79|79],[x89|89],[x99|99]],[],[],[],[],[],[]},{[[x20|20],[x40|40],[x60|60],[x80|80],[x100|100]],[[x1|1],[x21|21],[x41|41],[x61|61],[x81|81]],[[x2|2],[x22|22],[x42|42],[x62|62],[x82|82]],[[x3|3],[x23|23],[x43|43],[x63|63],[x83|83]],[],[],[],[],[],[],[],[],[],[],[],[]}}}
.
yu-fengdemacbook-2:~ yufeng$ head h2.erl
-module(h2).
-export([lookup/1]).
lookup(x1)-&gt;1;
lookup(x2)-&gt;2;
lookup(x3)-&gt;3;
lookup(x4)-&gt;4;
lookup(x5)-&gt;5;
lookup(x6)-&gt;6;
lookup(x7)-&gt;7;
lookup(x8)-&gt;8;
yu-fengdemacbook-2:~ yufeng$ cat test.sh
#!/bin/bash

#OPT=+native
OPT=

echo "build $1..."
echo "make h1..."
./make_dict $1 &gt;h1.erl
echo "make h2..."
./make_fun $1 &gt;h2.erl
echo "compile h1..."
erlc $OPT  h1.erl
echo "compile h2..."
erlc $OPT h2.erl
echo "compile t..."
erlc $OPT t.erl

echo "running..."
echo "map..."
erl -s t start $1 m -noshell -s erlang halt
echo "fun..."
erl -s t start $1 f -noshell -s erlang halt

yu-fengdemacbook-2:~ yufeng$ ./test.sh 10000
build 10000...
make h1...
make h2...
compile h1...
compile h2...
compile t...
running...
map...
2.767323e+05
fun...
2.656819e+05
done.

在10000条记录的情况下 每个查询几个us, 速度不是很快.

结果发现 函数和constant pool在处理上上差不多快的。在实践中根据需要采用把。

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