1. 程式人生 > >系統技術非業餘研究 » init_debug檢視系統boot過程

系統技術非業餘研究 » init_debug檢視系統boot過程

erl啟動的時候有個引數 -init_debug 作用是

Makes init write some debug information while interpreting the boot script.

參見erlang system_principles的1.3節:Boot Scripts, 我們可以知道,每個erlang vm啟動的時候,init程序都會去讀取boot檔案,來獲取系統要啟動那些服務。

如果我們不輸入任何引數的話,,預設是其他start_clean.boot檔案, 其內容如下:

$ cat ~/otp/erts/start_scripts/start_clean.script
%% script generated at {2012,12,26} {16,12,32}
{script,
    {"OTP  APN 181 01","R15B03"},
    [{preLoaded,
         [erl_prim_loader,erlang,init,otp_ring0,prim_file,prim_inet,prim_zip,
          zlib]},
     {progress,preloaded},
     {path,["$ROOT/lib/kernel-2.15.3/ebin","$ROOT/lib/stdlib-1.18.3/ebin"]},
     {primLoad,[error_handler]},
     {kernel_load_completed},
     {progress,kernel_load_completed},
     {path,["$ROOT/lib/kernel-2.15.3/ebin"]},
     {primLoad,
         [application,application_controller,application_master,
          application_starter,auth,code,code_server,disk_log,disk_log_1,
          disk_log_server,disk_log_sup,dist_ac,dist_util,erl_boot_server,
          erl_ddll,erl_distribution,erl_epmd,erl_reply,error_logger,
          erts_debug,file,file_io_server,file_server,gen_sctp,gen_tcp,gen_udp,
          global,global_group,global_search,group,heart,hipe_unified_loader,
          inet,inet6_sctp,inet6_tcp,inet6_tcp_dist,inet6_udp,inet_config,
          inet_db,inet_dns,inet_gethost_native,inet_hosts,inet_parse,inet_res,
          inet_sctp,inet_tcp,inet_tcp_dist,inet_udp,kernel,kernel_config,net,
          net_adm,net_kernel,os,packages,pg2,ram_file,rpc,seq_trace,
          standard_error,user,user_drv,user_sup,wrap_log_reader]},
     {path,["$ROOT/lib/stdlib-1.18.3/ebin"]},
     {primLoad,
         [array,base64,beam_lib,binary,c,calendar,dets,dets_server,dets_sup,
          dets_utils,dets_v8,dets_v9,dict,digraph,digraph_utils,edlin,
          edlin_expand,epp,erl_bits,erl_compile,erl_eval,erl_expand_records,
          erl_internal,erl_lint,erl_parse,erl_posix_msg,erl_pp,erl_scan,
          erl_tar,error_logger_file_h,error_logger_tty_h,escript,ets,
          eval_bits,file_sorter,filelib,filename,gb_sets,gb_trees,gen,
          gen_event,gen_fsm,gen_server,io,io_lib,io_lib_format,io_lib_fread,
          io_lib_pretty,lib,lists,log_mf_h,math,ms_transform,orddict,ordsets,
          otp_internal,pg,pool,proc_lib,proplists,qlc,qlc_pt,queue,random,re,
          sets,shell,shell_default,slave,sofs,string,supervisor,
          supervisor_bridge,sys,timer,unicode,win32reg,zip]},
     {progress,modules_loaded},
     {path,["$ROOT/lib/kernel-2.15.3/ebin","$ROOT/lib/stdlib-1.18.3/ebin"]},
     {kernelProcess,heart,{heart,start,[]}},
     {kernelProcess,error_logger,{error_logger,start_link,[]}},
     {kernelProcess,application_controller,
         {application_controller,start,
             [{application,kernel,
                  [{description,"ERTS  CXC 138 10"},
                   {vsn,"2.15.3"},
                   {id,[]},
                   {modules,
                       [application,application_controller,application_master,
                        application_starter,auth,code,packages,code_server,
                        dist_util,erl_boot_server,erl_distribution,erl_reply,
                        error_handler,error_logger,file,file_server,
                        file_io_server,global,global_group,global_search,
                        group,heart,hipe_unified_loader,inet6_tcp,
                        inet6_tcp_dist,inet6_udp,inet6_sctp,inet_config,
                        inet_hosts,inet_gethost_native,inet_tcp_dist,kernel,
                        kernel_config,net,net_adm,net_kernel,os,ram_file,rpc,
                        user,user_drv,user_sup,disk_log,disk_log_1,
                        disk_log_server,disk_log_sup,dist_ac,erl_ddll,
                        erl_epmd,erts_debug,gen_tcp,gen_udp,gen_sctp,inet,
                        inet_db,inet_dns,inet_parse,inet_res,inet_tcp,
                        inet_udp,inet_sctp,pg2,seq_trace,standard_error,
                        wrap_log_reader]},
                   {registered,
                       [application_controller,erl_reply,auth,boot_server,
                        code_server,disk_log_server,disk_log_sup,
                        erl_prim_loader,error_logger,file_server_2,
                        fixtable_server,global_group,global_name_server,heart,
                        init,kernel_config,kernel_sup,net_kernel,net_sup,rex,
                        user,os_server,ddll_server,erl_epmd,inet_db,pg2]},
                   {applications,[]},
                   {included_applications,[]},
                   {env,[{error_logger,tty}]},
                   {maxT,infinity},
                   {maxP,infinity},
                   {mod,{kernel,[]}}]}]}},
     {progress,init_kernel_started},
     {apply,
         {application,load,
             [{application,stdlib,
                  [{description,"ERTS  CXC 138 10"},
                   {vsn,"1.18.3"},
                   {id,[]},
                   {modules,
                       [array,base64,beam_lib,binary,c,calendar,dets,
                        dets_server,dets_sup,dets_utils,dets_v8,dets_v9,dict,
                        digraph,digraph_utils,edlin,edlin_expand,epp,
                        eval_bits,erl_bits,erl_compile,erl_eval,
                        erl_expand_records,erl_internal,erl_lint,erl_parse,
                        erl_posix_msg,erl_pp,erl_scan,erl_tar,
                        error_logger_file_h,error_logger_tty_h,escript,ets,
                        file_sorter,filelib,filename,gb_trees,gb_sets,gen,
                        gen_event,gen_fsm,gen_server,io,io_lib,io_lib_format,
                        io_lib_fread,io_lib_pretty,lib,lists,log_mf_h,math,
                        ms_transform,orddict,ordsets,otp_internal,pg,pool,
                        proc_lib,proplists,qlc,qlc_pt,queue,random,re,sets,
                        shell,shell_default,slave,sofs,string,supervisor,
                        supervisor_bridge,sys,timer,unicode,win32reg,zip]},
                   {registered,
                       [timer_server,rsh_starter,take_over_monitor,
                        pool_master,dets]},
                   {applications,[kernel]},
                   {included_applications,[]},
                   {env,[]},
                   {maxT,infinity},
                   {maxP,infinity}]}]}},
     {progress,applications_loaded},
     {apply,{application,start_boot,[kernel,permanent]}},
     {apply,{application,start_boot,[stdlib,permanent]}},
     {apply,{c,erlangrc,[]}},
     {progress,started}]}.

而boot檔案是script檔案的term_to_binary版本,演示下init載入過程:

$ erl -init_debug
{progress,preloaded}
{progress,kernel_load_completed}
{progress,modules_loaded}
{start,heart}
{start,error_logger}
{start,application_controller}
{progress,init_kernel_started}
{apply,{application,load,[{application,stdlib,[{description,"ERTS  CXC 138 10"},{vsn,"1.19.1"},{id,[]},{modules,[array,base64,beam_lib,binary,c,calendar,dets,dets_server,dets_sup,dets_utils,dets_v8,dets_v9,dict,digraph,digraph_utils,edlin,edlin_expand,epp,eval_bits,erl_bits,erl_compile,erl_eval,erl_expand_records,erl_internal,erl_lint,erl_parse,erl_posix_msg,erl_pp,erl_scan,erl_tar,error_logger_file_h,error_logger_tty_h,escript,ets,file_sorter,filelib,filename,gb_trees,gb_sets,gen,gen_event,gen_fsm,gen_server,io,io_lib,io_lib_format,io_lib_fread,io_lib_pretty,lib,lists,log_mf_h,math,ms_transform,orddict,ordsets,otp_internal,pg,pool,proc_lib,proplists,qlc,qlc_pt,queue,random,re,sets,shell,shell_default,slave,sofs,string,supervisor,supervisor_bridge,sys,timer,unicode,win32reg,zip]},{registered,[timer_server,rsh_starter,take_over_monitor,pool_master,dets]},{applications,[kernel]},{included_applications,[]},{env,[]},{maxT,infinity},{maxP,infinity}]}]}}
{progress,applications_loaded}
{apply,{application,start_boot,[kernel,permanent]}}
Erlang R16B (erts-5.10.1)  [64-bit] [smp:16:16] [async-threads:10] [hipe] [kernel-poll:false]

{apply,{application,start_boot,[stdlib,permanent]}}
{apply,{c,erlangrc,[]}}
{progress,started}
Eshell V5.10.1  (abort with ^G)
1> 

當然如果只是簡單的boot不會有什麼問題,但是真實地情況是reltool在打包的時候,會把 xxx.app 檔案裡面,依賴的application都打進去,順便啟動起來。
比如:

{applications,[kernel,stdlib,sasl,os_mon,mnesia,ump_lib,ump_log,
ump_zk,ump_mq]}]}.

我們在填寫.app檔案的時候,可能會漏掉幾個,結果發現系統不正常運作,這時候init_debug就能幫上大忙。
摘抄一段診斷:

{running_on_load_handler,ump_la_nifs}
ump_la_nifs on load result: ok
{on_load_handler_returned_ok,ump_la_nifs}
{running_on_load_handler,dyntrace}
{on_load_handler_returned_ok,dyntrace}
{running_on_load_handler,erlsha2}
{on_load_handler_returned_ok,erlsha2}
{running_on_load_handler,crypto}
{on_load_handler_returned_ok,crypto}
{running_on_load_handler,asn1rt_nif}
{on_load_handler_returned_ok,asn1rt_nif}
{apply,{application,start_boot,[stdlib,permanent]}}
{apply,{application,start_boot,[sasl,permanent]}}
{apply,{application,start_boot,[os_mon,permanent]}}
{apply,{application,start_boot,[mnesia,permanent]}}

=INFO REPORT==== 24-Jun-2013::17:24:34 ===
alarm_handler: {set,{{disk_almost_full,”/usr”},[]}}

=ERROR REPORT==== 24-Jun-2013::17:24:34 ===
Mnesia(‘[email protected]’): ** ERROR ** mnesia_event got {inconsistent_database, starting_partitioned_network, ‘[email protected]’}

=ERROR REPORT==== 24-Jun-2013::17:24:34 ===
Mnesia(‘[email protected]’): ** ERROR ** mnesia_event got {inconsistent_database, starting_partitioned_network, ‘[email protected]’}
{apply,{application,start_boot,[ump_lib,permanent]}}
{apply,{application,start_boot,[lager,permanent]}}
{apply,{application,start_boot,[syslog,permanent]}}
{apply,{application,start_boot,[ump_log,permanent]}}
17:24:34.755 [info] Application lager started on node ‘[email protected]
{apply,{application,start_boot,[erlzk,permanent]}}
{apply,{application,start_boot,[ump_zk,permanent]}}

我們很清楚的看到,這些依賴的application是如何啟動的,按照什麼樣的順序。問題馬上就能定位出來。

祝玩的開心。

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