- 浏览: 969848 次
- 性别:
- 来自: 广州
最新评论
-
qingchuwudi:
有用,非常感谢!
erlang进程的优先级 -
zfjdiamond:
你好 这条命令 在那里输入??
你们有yum 我有LuaRocks -
simsunny22:
这个是在linux下运行的吧,在window下怎么运行escr ...
escript的高级特性 -
mozhenghua:
http://www.erlang.org/doc/apps/ ...
mnesia 分布协调的几个细节 -
fxltsbl:
A new record of 108000 HTTP req ...
Haproxy 1.4-dev2: barrier of 100k HTTP req/s crossed
gen_server在erlang otp编程中的地位是无可撼动的,几乎都是gen_server或者gen_fsm的模型。那么程序运行起来的时候 我们如何查看gen_server的内部状态呢。有2种方法:
1. 自己写个类似于info这样的函数,来获取状态。
2. 利用系统现有的架构。sasl应用带了一个si的东西 全名是status inspector, 这个东西就是设计来帮用户解决这个问题的。
实验开始:
root@nd-desktop:~# cat abc.erl
-module(abc).
-behaviour(gen_server).
-export([start_link/0]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-export([format_status/2]).
-export([test/0]).
-record(state, {a, b}).
-define(SERVER, ?MODULE).
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
test()->
gen_server:call(?SERVER, {test, "param1"}).
init([]) ->
{ok, #state{a=hello, b=world}}.
handle_call({test, _} = Request, _From, State) ->
io:format("got msg ~p~n", [Request]),
{reply, ok, State};
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
format_status(_Opt, [_PDict, #state{a=A,
b = B
}]) ->
[{data, [{"a===", A},
{"b===", B}]}].
root@nd-desktop:~# erl -boot start_sasl
Erlang R13B03 (erts-5.7.4) [source] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]
=PROGRESS REPORT==== 29-Oct-2009::16:07:24 ===
supervisor: {local,sasl_safe_sup}
started: [{pid,<0.35.0>},
{name,alarm_handler},
{mfa,{alarm_handler,start_link,[]}},
{restart_type,permanent},
{shutdown,2000},
{child_type,worker}]
=PROGRESS REPORT==== 29-Oct-2009::16:07:24 ===
supervisor: {local,sasl_safe_sup}
started: [{pid,<0.36.0>},
{name,overload},
{mfa,{overload,start_link,[]}},
{restart_type,permanent},
{shutdown,2000},
{child_type,worker}]
=PROGRESS REPORT==== 29-Oct-2009::16:07:24 ===
supervisor: {local,sasl_sup}
started: [{pid,<0.34.0>},
{name,sasl_safe_sup},
{mfa,
{supervisor,start_link,
[{local,sasl_safe_sup},sasl,safe]}},
{restart_type,permanent},
{shutdown,infinity},
{child_type,supervisor}]
=PROGRESS REPORT==== 29-Oct-2009::16:07:24 ===
supervisor: {local,sasl_sup}
started: [{pid,<0.37.0>},
{name,release_handler},
{mfa,{release_handler,start_link,[]}},
{restart_type,permanent},
{shutdown,2000},
{child_type,worker}]
=PROGRESS REPORT==== 29-Oct-2009::16:07:24 ===
application: sasl
started_at: nonode@nohost
Eshell V5.7.4 (abort with ^G)
1> si:start(). %必须手动启动
=PROGRESS REPORT==== 29-Oct-2009::16:07:31 ===
supervisor: {local,sasl_sup}
started: [{pid,<0.43.0>},
{name,si_server},
{mfa,{si_sasl_supp,start_link,[[]]}},
{restart_type,temporary},
{shutdown,brutal_kill},
{child_type,worker}]
{ok,<0.43.0>}
2> si:help().
Status Inspection tool - usage
==============================
For all these functions, Opt is an optional argument
which can be 'normal' or 'all'; default is 'normal'.
If 'all', all information will be printed.
A Pid can be: "<A.B.C>", {A, B, C}, B, a registered_name or an abbrev.
ANY PROCESS
si:pi([Opt,] Pid) - Formatted information about any process that
SI recognises.
si:pi([Opt,] A,B,C) - Same as si:pi({A, B, C}).
si:ppi(Pid) - Pretty formating of process_info.
Works for any process.
MISC
si:abbrevs() - Lists valid abbreviations.
si:start_log(Filename) - Logging to file.
si:stop_log()
si:start() - Starts Status Inspection (the si_server).
si:start([{start_log, FileName}])
si:stop() - Shut down SI.
ok
3> abc:start_link().
{ok,<0.46.0>}
4> abc:test().
got msg {test,"param1"}
ok
5> sys:log(abc,true). %打开gen_server的消息log功能
ok
6> abc:test(). %这个请求消息被记录
got msg {test,"param1"}
ok
7> si:pi(abc). %好戏开始
Status for generic server abc
===============================================================================
Pid <0.46.0>
Status running
Parent <0.41.0>
Logged events %这个是log到的消息
{10,
[{{out,ok,<0.41.0>,{state,hello,world}},
abc,
{gen_server,print_event}},
{{in,{'$gen_call',{<0.41.0>,#Ref<0.0.0.85>},{test,"param1"}}},
abc,
{gen_server,print_event}}]}
%这个是format_status的结果 如果没有format_status那么导出是 {a=hello, b=world}
a=== hello
b=== world
ok
8> si:ppi(abc).
Pretty Process Info
-------------------
[{registered_name,abc},
{current_function,{gen_server,loop,6}},
{initial_call,{proc_lib,init_p,5}},
{status,waiting},
{message_queue_len,0},
{messages,[]},
{links,[<0.41.0>]},
{dictionary,[{'$ancestors',[<0.41.0>]},{'$initial_call',{abc,init,1}}]},
{trap_exit,false},
{error_handler,error_handler},
{priority,normal},
{group_leader,<0.25.0>},
{total_heap_size,233},
{heap_size,233},
{stack_size,9},
{reductions,117},
{garbage_collection,[{fullsweep_after,65535},{minor_gcs,0}]},
{suspending,[]}]
ok
9> sys:get_status(abc). %当然你也可以这么看
{status,<0.46.0>,
{module,gen_server},
[[{'$ancestors',[<0.41.0>]},{'$initial_call',{abc,init,1}}],
running,<0.41.0>,
[{log,{10,
[{{out,ok,<0.41.0>,{state,hello,world}},
abc,
{gen_server,print_event}},
{{in,{'$gen_call',{<0.41.0>,#Ref<0.0.0.85>},
{test,"param1"}}},
abc,
{gen_server,print_event}}]}}],
[abc,{state,hello,world},abc,infinity]]}
结论:
这个是文档未公开的功能。上面演示了如何sys打开log, 如何察看gen_server的状态
1. 自己写个类似于info这样的函数,来获取状态。
2. 利用系统现有的架构。sasl应用带了一个si的东西 全名是status inspector, 这个东西就是设计来帮用户解决这个问题的。
实验开始:
root@nd-desktop:~# cat abc.erl
-module(abc).
-behaviour(gen_server).
-export([start_link/0]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-export([format_status/2]).
-export([test/0]).
-record(state, {a, b}).
-define(SERVER, ?MODULE).
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
test()->
gen_server:call(?SERVER, {test, "param1"}).
init([]) ->
{ok, #state{a=hello, b=world}}.
handle_call({test, _} = Request, _From, State) ->
io:format("got msg ~p~n", [Request]),
{reply, ok, State};
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
format_status(_Opt, [_PDict, #state{a=A,
b = B
}]) ->
[{data, [{"a===", A},
{"b===", B}]}].
root@nd-desktop:~# erl -boot start_sasl
Erlang R13B03 (erts-5.7.4) [source] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]
=PROGRESS REPORT==== 29-Oct-2009::16:07:24 ===
supervisor: {local,sasl_safe_sup}
started: [{pid,<0.35.0>},
{name,alarm_handler},
{mfa,{alarm_handler,start_link,[]}},
{restart_type,permanent},
{shutdown,2000},
{child_type,worker}]
=PROGRESS REPORT==== 29-Oct-2009::16:07:24 ===
supervisor: {local,sasl_safe_sup}
started: [{pid,<0.36.0>},
{name,overload},
{mfa,{overload,start_link,[]}},
{restart_type,permanent},
{shutdown,2000},
{child_type,worker}]
=PROGRESS REPORT==== 29-Oct-2009::16:07:24 ===
supervisor: {local,sasl_sup}
started: [{pid,<0.34.0>},
{name,sasl_safe_sup},
{mfa,
{supervisor,start_link,
[{local,sasl_safe_sup},sasl,safe]}},
{restart_type,permanent},
{shutdown,infinity},
{child_type,supervisor}]
=PROGRESS REPORT==== 29-Oct-2009::16:07:24 ===
supervisor: {local,sasl_sup}
started: [{pid,<0.37.0>},
{name,release_handler},
{mfa,{release_handler,start_link,[]}},
{restart_type,permanent},
{shutdown,2000},
{child_type,worker}]
=PROGRESS REPORT==== 29-Oct-2009::16:07:24 ===
application: sasl
started_at: nonode@nohost
Eshell V5.7.4 (abort with ^G)
1> si:start(). %必须手动启动
=PROGRESS REPORT==== 29-Oct-2009::16:07:31 ===
supervisor: {local,sasl_sup}
started: [{pid,<0.43.0>},
{name,si_server},
{mfa,{si_sasl_supp,start_link,[[]]}},
{restart_type,temporary},
{shutdown,brutal_kill},
{child_type,worker}]
{ok,<0.43.0>}
2> si:help().
Status Inspection tool - usage
==============================
For all these functions, Opt is an optional argument
which can be 'normal' or 'all'; default is 'normal'.
If 'all', all information will be printed.
A Pid can be: "<A.B.C>", {A, B, C}, B, a registered_name or an abbrev.
ANY PROCESS
si:pi([Opt,] Pid) - Formatted information about any process that
SI recognises.
si:pi([Opt,] A,B,C) - Same as si:pi({A, B, C}).
si:ppi(Pid) - Pretty formating of process_info.
Works for any process.
MISC
si:abbrevs() - Lists valid abbreviations.
si:start_log(Filename) - Logging to file.
si:stop_log()
si:start() - Starts Status Inspection (the si_server).
si:start([{start_log, FileName}])
si:stop() - Shut down SI.
ok
3> abc:start_link().
{ok,<0.46.0>}
4> abc:test().
got msg {test,"param1"}
ok
5> sys:log(abc,true). %打开gen_server的消息log功能
ok
6> abc:test(). %这个请求消息被记录
got msg {test,"param1"}
ok
7> si:pi(abc). %好戏开始
Status for generic server abc
===============================================================================
Pid <0.46.0>
Status running
Parent <0.41.0>
Logged events %这个是log到的消息
{10,
[{{out,ok,<0.41.0>,{state,hello,world}},
abc,
{gen_server,print_event}},
{{in,{'$gen_call',{<0.41.0>,#Ref<0.0.0.85>},{test,"param1"}}},
abc,
{gen_server,print_event}}]}
%这个是format_status的结果 如果没有format_status那么导出是 {a=hello, b=world}
a=== hello
b=== world
ok
8> si:ppi(abc).
Pretty Process Info
-------------------
[{registered_name,abc},
{current_function,{gen_server,loop,6}},
{initial_call,{proc_lib,init_p,5}},
{status,waiting},
{message_queue_len,0},
{messages,[]},
{links,[<0.41.0>]},
{dictionary,[{'$ancestors',[<0.41.0>]},{'$initial_call',{abc,init,1}}]},
{trap_exit,false},
{error_handler,error_handler},
{priority,normal},
{group_leader,<0.25.0>},
{total_heap_size,233},
{heap_size,233},
{stack_size,9},
{reductions,117},
{garbage_collection,[{fullsweep_after,65535},{minor_gcs,0}]},
{suspending,[]}]
ok
9> sys:get_status(abc). %当然你也可以这么看
{status,<0.46.0>,
{module,gen_server},
[[{'$ancestors',[<0.41.0>]},{'$initial_call',{abc,init,1}}],
running,<0.41.0>,
[{log,{10,
[{{out,ok,<0.41.0>,{state,hello,world}},
abc,
{gen_server,print_event}},
{{in,{'$gen_call',{<0.41.0>,#Ref<0.0.0.85>},
{test,"param1"}}},
abc,
{gen_server,print_event}}]}}],
[abc,{state,hello,world},abc,infinity]]}
结论:
这个是文档未公开的功能。上面演示了如何sys打开log, 如何察看gen_server的状态
发表评论
-
OTP R14A今天发布了
2010-06-17 14:36 2618以下是这次发布的亮点,没有太大的性能改进, 主要是修理了很多B ... -
R14A实现了EEP31,添加了binary模块
2010-05-21 15:15 2973Erlang的binary数据结构非常强大,而且偏向底层,在作 ... -
如何查看节点的可用句柄数目和已用句柄数
2010-04-08 03:31 4750很多同学在使用erlang的过程中, 碰到了很奇怪的问题, 后 ... -
获取Erlang系统信息的代码片段
2010-04-06 21:49 3396从lib/megaco/src/tcp/megaco_tcp_ ... -
iolist跟list有什么区别?
2010-04-06 20:30 6439看到erlang-china.org上有个 ... -
erlang:send_after和erlang:start_timer的使用解释
2010-04-06 18:31 8270前段时间arksea 同学提出这个问题, 因为文档里面写的很不 ... -
Latest news from the Erlang/OTP team at Ericsson 2010
2010-04-05 19:23 1945参考Talk http://www.erlang-factor ... -
对try 异常 运行的疑问,为什么出现两种结果
2010-04-05 19:22 2793郎咸武<langxianzhe@163.com> ... -
Erlang ERTS Async基础设施
2010-03-19 00:03 2446其实Erts的Async做的很不错的, 相当的完备, 性能又高 ... -
CloudI 0.0.9 Released, A Cloud as an Interface
2010-03-09 22:32 2432基于Erlang的云平台 看了下代码 质量还是不错的 完成了不 ... -
Memory matters - even in Erlang (再次说明了了解内存如何工作的必要性)
2010-03-09 20:26 3380原文地址:http://www.lshift.net/blog ... -
Some simple examples of using Erlang’s XPath implementation
2010-03-08 23:30 2006原文地址 http://www.lshift.net/blog ... -
lcnt 环境搭建
2010-02-26 16:19 2555抄书:otp_doc_html_R13B04/lib/tool ... -
Erlang强大的代码重构工具 tidier
2010-02-25 16:22 2435Jan 29, 2010 We are very happy ... -
[Feb 24 2010] Erlang/OTP R13B04 has been released
2010-02-25 00:31 1344Erlang/OTP R13B04 has been rele ... -
R13B04 Installation
2010-01-28 10:28 1350R13B04后erlang的源码编译为了考虑移植性,就改变了编 ... -
Running tests
2010-01-19 14:51 1402R13B03以后 OTP的模块加入了大量的测试模块,这些模块都 ... -
R13B04在细化Binary heap
2010-01-14 15:11 1461从github otp的更新日志可以清楚的看到otp R13B ... -
R13B03 binary vheap有助减少binary内存压力
2009-11-29 16:07 1606R13B03 binary vheap有助减少binary内存 ... -
erl_nif 扩展erlang的另外一种方法
2009-11-26 01:02 3146我们知道扩展erl有2种方法, driver和port. 这2 ...
相关推荐
gen_lex_hash for pc,mysql交叉编译需要用到的文件,5.1.51 and 5.1.63两个版本,5.1.67版本始终编译不出来。有需要的下载。
Gen_Signature_Android2.apk、获取签名工具。
通用 TCP 服务器 通用 TCP 服务器( gen_tcp_server ) 是一种 Erlang 行为,提供快速简便的方法将 TCP 服务器功能添加到您的应用程序。 它被实现为管理 TCP 连接的主管,因为它是孩子。如何使用它? 运行make来构建。...
NULL 博文链接:https://lzy.iteye.com/blog/324962
Gen_Signature_Android.apk
关于在 Erlang 风格的 gen_server 上进行 Ocaml/Async 尝试。
包名签名查看,输入包名则可查看签名值,用于微信第三方分享 登录等设置填写,安装则可以使用,没需要任何设置,可以在安卓模拟器上使用
华硕Z87-C主板升级BIOS的工具,BIOS_updater_for_4th_Gen_Intel_Core_CPU.zip
srio_response_gen_srio_gen2_0_srio_gen_srio_reponse_SRIO_gen2_SR
gen_lyricist.dll 万能播放器仿千千静听
个人学习Erlang的时候自己写的一个基于gen_tcp的聊天室,功能有注册,登陆,获得登陆时间,聊天次数,上次登陆时间等等,资源里有完整代码,注释也很详细.
gen_tags.vim, 用来轻松使用 ctags/gtags的vim和neovim的异步插件 gen_tags.vim 为方便用户使用 Vim/ NeoVim,简化了 ctags/ gtags的使用。它用于为你生成和维护多个平台支持的标签,在 Windows/Linux/macOS. 上测试...
1、将2560及ramps1.4集合在一块板子上,解决了Ramps1.4组合接口繁琐,易出故障的问题。 2、可更换电机驱动,支持4988驱动和8825驱动。 3、电路板采用高质量的4层板,并专门作了散热优化处理;...
pwm_gen,PWN波形发生器,开关波形
采用verilog实现RTLAB多路驱动程序
android 导航选项卡效果源码,带有彩色交互的导航选项卡栏。