Archive

Archive for April, 2010

对try 异常 运行的疑问,为什么出现两种结果

April 5th, 2010 3 comments

原创文章,转载请注明: 转载自系统技术非业余研究

本文链接地址: 对try 异常 运行的疑问,为什么出现两种结果

郎咸武<langxianzhe@163.com>  同学在erlang-china上post了一个问题:
请注意编号为91和92两行运行结果,请问为什么会出现两种结果。
一个抛出 {error,{badmatch,5}}
另一个抛出** exception error: no match of right hand side value 4

root@ubuntu:/usr/src/otp# erl
Erlang R13B04 (erts-5.7.5) [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false
88> X=1.
1
89> try (X=5) of Val ->{normal,Val} catch error:Error -> {error,Error}  end.
{error,{badmatch,5}}
90> try (X=1) of Val ->{normal,Val} catch error:Error -> {error,Error}  end.
{normal,1}
91> try (X=5) of Val ->{normal,Val} catch error:Error -> {error,Error}  end.
{error,{badmatch,5}}
92> try (X=4) of Val ->{normal,Val} catch error:Error -> {error,Error}  end.
** exception error: no match of right hand side value 4
93> self().
<0.36.0>
94> catch try (X=4) of Val  ->{normal,Val} catch error:Error -> {error,Error}  end. %%这个异常是 shell捕获到了 进一步处理后的结果
{'EXIT',{{badmatch,4},[{erl_eval,expr,3}]}}
95> try (X=4) of Val ->{normal,Val} catch error:Error ->  {error,Error}  end.
** exception error: no match of right hand  side value 4
96> self().
<0.36.0>

竟然是EXIT, 这时候shell也换了pid了, 这是怎么回事呢?
由于在shell输入的程序是公司允许的, 我们也可以从出错的stack里面看到是 erl_eval:expr函数异常了.
现在让我们对系统打patch如下:
lib/stdlib/src/erl_eval.erl

 282expr({match,_,Lhs,Rhs0},  Bs0, Lf, Ef, RBs) ->
 283    {value,Rhs,Bs1} = expr(Rhs0, Bs0,  Lf, Ef, none),
 284    case match(Lhs, Rhs, Bs1) of
 285         {match,Bs} ->
 286            ret_expr(Rhs, Bs, RBs);
 287        nomatch ->
 288            io:format("expr nomatch->pid:~p~n~p~n",[self(), Rhs]),   %%添加诊断
 289             erlang:raise(error, {badmatch,Rhs}, stacktrace())
 290    end;

erts/emulator/beam/bif.c

1142/**********************************************************************/
1143/*  raise an exception of given class, value and  stacktrace.
1144  *
1145 * If there is an error in the argument  format,
1146 * return the atom 'badarg'  instead.
1147 */
1148Eterm
1149raise_3(Process *c_p, Eterm class,  Eterm value, Eterm stacktrace) {
...
1222     erts_print(ERTS_PRINT_STDOUT, NULL, "raise->proc:%T\nclass=%T\nvalue=%T\nstacktrace=%T\n", c_p->id, class, value,  c_p->ftrace); /*添加诊断*/
1223    BIF_ERROR(c_p, reason);
...
}

然 后我们在运行上面的语句:

root@ubuntu:~/otp#  bin/erl
Erlang R13B04 (erts-5.7.5) [source]  [smp:2:2] [rq:2]  [async-threads:0] [kernel-poll:false]

Eshell  V5.7.5  (abort with  ^G)
1> X=1.
 1
2>  try (X=5) of Val ->{normal,Val}  catch error:Error ->  {error,Error}  end.
expr nomatch->pid:<0.32.0>
5
raise->proc:<0.32.0>
class=error
value={badmatch,5}
stacktrace=[[{erl_eval,expr,3}
{error,{badmatch,5}}
3>  try  (X=4) of Val ->{normal,Val} catch  error:Error -> {error,Error}   end.
expr nomatch->pid:<0.32.0>
4
raise->proc:<0.32.0>
class=error
value={badmatch,4}
stacktrace=[[{erl_eval,expr,3}]|-000000000000000016]
raise->proc:<0.32.0>
class=error
value={badmatch,4}
stacktrace=[[{erl_eval,expr,3}]|-000000000000000016]

很奇怪的是第3句raise了2次. 让我们回到程序好好看下:

X=1.   %%这行绑定了一个变量X=1
try (X=5) of Val ->{normal,Val} catch  error:Error -> {error,Error}  end.   %%这行由于异常会试图绑定变量Error={badmatch,5}, 由于之前Error不存在, 绑定成功.
try  (X=4) of Val ->{normal,Val} catch error:Error -> {error,Error}   end.   %%这行由于异常会绑定了变量Error={badmatch,4}, 由于Error存在,  而且值是{badmatch,5},所以这时候catch就出
现异常了,  往外抛出{'EXIT',{{badmatch,4},[{erl_eval,expr,3}]}}.

这 下我们明白了, 是这个catch惹的祸了.
我们再实验下我们的分析:

root@ubuntu:~#  erl
Erlang R13B04 (erts-5.7.5) [source] [smp:2:2] [rq:2]  [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.5   (abort with ^G)
1> X=1.
1
2> b().
X = 1
ok
3>  try (X=5) of Val ->{normal,Val} catch error:Error ->  {error,Error}  end.
{error,{badmatch,5}}
4> b().
Error =  {badmatch,5}
X = 1
ok
5> try (X=4) of Val ->{normal,Val}  catch error:Error -> {error,Error}  end.
** exception error: no  match of right hand side value 4
6> f(Error).
ok
7>  b().
X = 1
ok
8> try (X=4) of Val ->{normal,Val}  catch error:Error -> {error,Error}  end.
{error,{badmatch,4}}
9>

Bingo成功.
结论: 要非常小心Erlang语法的变量绑定,在不同的路线会有不同的绑定,容易出问题.

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

Latest news from the Erlang/OTP team at Ericsson(Erlang Factory SF Bay Area 2010)

April 2nd, 2010 1 comment

参考Talk http://www.erlang-factory.com/upload/presentations/238/ErlangFView postactorySFBay2010-KennethLundin.pdf
摘抄我感兴趣的:

R14要实现的:

1. June 16: R14A, a beta release
2. Multi-core performance improvements

  • – optimized rwlocks
  • – delayed deallocation
  • – ”lock-free” process table

3. NIF improvements (Native Implemented Function)

  • – sending messages from a NIF
  • – crypto application as NIFs, now using a driver.

4. search in binaries (as of EEP-31)

  • – new module called binary with functions: match, matches, split, replace, longest_common_prefix, … part, at, copy, first,last

5. new SSL ready to replace old SSL
6. improved Reltool

  • – for easy creation of minimal target systems and standalone deployments from an installed development system.

7. Tentative

  • –Parameterized modules officially supported and with more efficient implementation.

Longer term plans:
1. More multi core performance improvements

  • – lock free pre-allocators (thread specific pre allocated buffers)
  • – Scheduler specific mseg_alloc reducing lock contention and necessary for future NUMA optimizations. Intel Nehalem, AMD Opteron

2. Clustered shared heap or other solution to allow parallell computing on large sets of data avoiding copying.
3 . New XML-schema/dtd validator complementing the XML SAX parser we already have.
4. SMP optimizations in existing applications (Mnesia, ASN.1 …)
5. Improved eprof profiler

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

Categories: Erlang探索 Tags: , , , ,

从Megaco学如何写诊断代码

April 1st, 2010 3 comments

原创文章,转载请注明: 转载自系统技术非业余研究

本文链接地址: 从Megaco学如何写诊断代码

Megaco/H.248 is a protocol for control of elements in a physically decomposed multimedia gateway, enabling separation of call control from media conversion. A Media Gateway Controller (MGC) controls one or more Media Gateways (MG).

Megaco有非常清晰的日志和诊断供我们参考, 精细到几行代码一个诊断信息, 非常好的风格. 有了这些信息对系统的运行完全了如指掌.

有3种方式:
1. 大量的调试日志, 在DEBUG方式下, 会有大量的系统允许期间的轨迹和消息

%%%----------------------------------------------------------------------                                                                                                                         %%% Debug                                                                                                                                                                                                      
%%%----------------------------------------------------------------------  
-ifdef(megaco_debug).                                                                               
-define(d(F,A), io:format("~w: " ++ F ++ "~n",[?MODULE|A])). 
-else.
-define(d(F,A), ok).
-endif.

用起来很方便, 摘抄一段如下

?d("encode(~p) -> entry with"
"~n   PackageItem: ~p"
"~n   SubItem:     ~p", [Scope, PackageItem, SubItem]),

2. 异常日志, 用于记录程序允许中间产生的各种各样的异常, 便于事后调查.

%%%----------------------------------------------------------------------                                                                                                                         %%% Error/warning/info message macro(s)                                                                                                                                                                        
%%%----------------------------------------------------------------------                                                                                                                         -define(megaco_info(F, A),                                                                                                                                                                                     
        (catch error_logger:info_msg("[ ~w : ~w : ~p ] ~n" ++ F ++ "~n", [?APPLICATION, ?MODULE, self()|A]))).
                                                                                                                                                                                                               
-define(megaco_warning(F, A),                                                                                                                                                                                  
        (catch error_logger:warning_msg("[ ~w : ~w : ~p ] ~n" ++ F ++ "~n", [?APPLICATION, ?MODULE, self()|A]))).
                                                                                                                                                                                                               
-define(megaco_error(F, A),                                                                                                                                                                                    
        (catch error_logger:error_msg("[ ~w : ~w : ~p ] ~n" ++ F ++ "~n",[?APPLICATION, ?MODULE, self()|A]))). 

使用, 摘抄一段如下

warning_msg(F, A) ->                                                                                
   ?megaco_warning("Transaction sender: " ++ F, A). 
error_msg(F, A) ->                                       
   ?megaco_error("Transaction sender: " ++ F, A).

3. Event Trace机制
先进的trace能够让系统的消息交互,运行轨迹以可视的方式体现在用户面前.

%%%----------------------------------------------------------------------                                                                                                                         
%%% Event Trace                                                                                                                                                                                                
%%%----------------------------------------------------------------------                                                                                                                         -ifdef(megaco_trace_io).
-define(report(Level, C, Label, Contents),
        io:format("*** [~s] ~p ~p *** "
                  "~n   ~p[~p] " ++ Label ++
                  "~n   ~p"
                  "~n   ~p"
                  "~n",
                  [megaco:format_timestamp(now()),
                   self(), Level, ?MODULE, ?LINE, C, Contents])).
-else.
-define(report(Level, C, Label, Contents),
        megaco:report_event(Level, ?APPLICATION, Label,
                            [{line, ?MODULE, ?LINE}, C | Contents])).
-endif.

-define(report_important(C, Label, Contents), ?report(20, C, Label, Contents)).
-define(report_verbose(  C, Label, Contents), ?report(40, C, Label, Contents)).
-define(report_debug(    C, Label, Contents), ?report(60, C, Label, Contents)).
-define(report_trace(    C, Label, Contents), ?report(80, C, Label, Contents)).

使用, 摘抄一段如下

?report_trace(ReceiveHandle, "callback: syntax error", [ErrorDesc, Error]), 

实际效果图(点击放大):

总结: ET很强大, 而且是专门为Megaco开发的, 目的就是能够可视化看到Megaco系统组件见的消息流程交互.

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

Categories: Erlang探索 Tags: , ,